从脚本安装磁盘时遇到问题

从脚本安装磁盘时遇到问题

我有一个由 udev 规则触发的脚本,该规则对新添加的磁盘进行分区,并为其创建一个 systemd 单元文件并应该挂载它。但是,安装部分无法正常工作,并且会失败并出现一些错误,例如:A dependency job for mnt-volume1.mount failed. See 'journalctl -xe' for details.即使尝试使用安装命令安装它,它也不会出现任何错误,但在脚本外部检查时,分区未安装。如果我手动运行 systemd 启动,一切正常,没有任何问题。

udev 触发的脚本如下所示:

#!/bin/bash -x
LOGFILE=/tmp/set-bash.log
exec > >(tee -a $LOGFILE)
exec 2>&1

device=$1
dev=${device##*/}
FS=''

[ ${dev:0:2} = "sd" ] && FS='xfs' 
[ ${dev:0:2} = "vd" ] && FS='ext4' 

[ -z "${FS}" ] && { echo "No FS specified!"; exit ; }

[ -e "${device}1" ] || {

echo ';' | sfdisk ${device}

counter=0
while true
do
        [ "${counter}" = "60" ] && { echo "Timeout reached, exiting..."; exit; }
        [ -e "${device}1" ] && { 
        /usr/sbin/mkfs.${FS} ${device}1
        break
    }
    counter=$(( counter++))
    sleep 1
done
}

for i in {1..9999}
do
  if [[ ! -e "/mnt/volume${i}" ]]
  then
    declare -g DIRECTORY="volume${i}"
    mkdir -p /mnt/$DIRECTORY
    break
  fi
done


cat << EOF > /etc/systemd/system/mnt-${DIRECTORY}.mount
[Unit]
Description=mounting /mnt/${DIRECTORY}

[Mount]
What=${device}1
Where=/mnt/${DIRECTORY}
Type=${FS}
Options=defaults

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

systemctl enable mnt-${DIRECTORY}.mount
systemctl start mnt-${DIRECTORY}.mount

安装部分的脚本输出如下所示:

+ systemctl daemon-reload
+ systemctl enable mnt-vpsie33.mount
Created symlink /etc/systemd/system/multi-user.target.wants/mnt-vpsie33.mount → /etc/systemd/system/mnt-vpsie33.mount.
+ systemctl start mnt-vpsie33.mount
A dependency job for mnt-vpsie33.mount failed. See 'journalctl -xe' for details.

有什么办法可以做到这一点吗?

相关内容