我正在尝试自动挂载所有ATA/SCSI在启动时使用驱动systemd
器udev
没有利用/etc/fstab
。
这是必要的,因为文件系统需要挂载在目录上以其 UUID 命名,并且驱动器总是被冷交换为新驱动器,因此不断修改fstab
变得繁琐乏味。
为了做到这一点,我使用了以下方法脚本,系统单元文件, 和udev 规则改编自针对 USB 驱动器的类似问题的答案。
然而,虽然这个答案适用于 USB 驱动器,但它不起作用适用于 ATA/SCSI 驱动器:脚本似乎成功执行并创建了所有必要的目录,但启动完成后,没有安装任何内容。脚本确实有效但是,当手动运行时。
/usr/local/bin/automount.sh:
#!/bin/bash
ACTION=$1
DEVBASE=$2
DEVICE="/dev/${DEVBASE}"
SAVE="/root/${DEVBASE}"
# See if this drive is already mounted, and if so where
MOUNT_POINT=$(/bin/mount | /bin/grep ${DEVICE} | /usr/bin/awk '{ print $3 }')
do_mount()
{
if [[ -n ${MOUNT_POINT} ]]
then
echo "Warning: ${DEVICE} is already mounted at ${MOUNT_POINT}"
exit 1
fi
# Get info for this drive: $ID_FS_LABEL, $ID_FS_UUID, and $ID_FS_TYPE
eval $(/sbin/blkid -o udev ${DEVICE})
# Check if drive is already mounted / duplicate UUID
LABEL=${ID_FS_UUID}
/bin/grep -q " /media/${LABEL} " /etc/mtab \
&& ( echo "UUID ${LABEL} already in use"'!'; exit 1 )
# Set mount point and save for later (in case of "yanked" unmount)
MOUNT_POINT="/media/${LABEL}"
echo "MOUNT_POINT=${MOUNT_POINT}" | /usr/bin/tee "${SAVE}"
# Create mount point
/bin/mkdir -p ${MOUNT_POINT}
# Global mount options
OPTS="rw,relatime"
# File system type specific mount options
case "${ID_FS_TYPE}" in
vfat)
OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"
;;
esac
if ! /bin/mount -o ${OPTS} ${DEVICE} ${MOUNT_POINT}
then
echo "Error mounting ${DEVICE} (status = $?)"
/bin/rmdir ${MOUNT_POINT}
/bin/rm ${SAVE}
exit 1
fi
echo "**** Mounted ${DEVICE} at ${MOUNT_POINT} ****"
}
do_unmount()
{
eval $(/bin/cat "${SAVE}")
[[ -z ${MOUNT_POINT} ]] \
&& ( echo "Warning: ${DEVICE} is not mounted"; exit 0)
/bin/umount -l ${DEVICE} \
&& ( echo "**** Unmounted ${DEVICE}"; /bin/rm ${SAVE} ) \
|| ( echo "Unmounting ${DEVICE} failed"'!'; exit 1 )
}
case "${ACTION}" in
add)
do_mount
;;
remove)
do_unmount
;;
*)
echo "BAD OPTION ${ACTION}"'!'
exit 1
;;
esac
/etc/systemd/系统/[电子邮件保护]:
[Unit]
Description=Automount ATA/SCSI drive on %i
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/automount.sh add %i
ExecStop=/usr/local/bin/automount.sh remove %i
/etc/udev/rules.d/11-automount-ata-scsi.rules:
KERNEL!="sd[a-z][0-9]", SUBSYSTEMS!="scsi", GOTO="automount_ata_scsi_end"
ACTION=="add", RUN+="/bin/systemctl start automount@%k.service"
ACTION=="remove", RUN+="/bin/systemctl stop automount@%k.service"
LABEL="automount_ata_scsi_end"
(从外观上看,ATA/SCSI 驱动器确实会通过这种设置短暂地安装,但随后在启动过程中以某种方式卸载。)