linux – 使用systemd自动挂载USB驱动器

前端之家收集整理的这篇文章主要介绍了linux – 使用systemd自动挂载USB驱动器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们将服务器从一个非常过时的发行版更新为现代的基于Debian Jessie的系统,包括lightdm / xfce,当然还有systemd(和udisks2).一个关键点是自动挂载USB驱动器.我们过去通过一些udev规则来实现这一点.旧规则几乎仍然有效 – 挂载点已创建并且驱动器安装正常,但几秒钟后,systemd正在执行一些破坏挂载的操作,因此后续访问尝试会导致“传输端点未连接”错误.

通过命令行手动安装驱动器工作正常.那么让一个文件管理器(thunar和thunar-volman,它反过来使用udisks2).但那些不是可行的选择 – 这些系统大多是无头的,所以thunar通常不会运行.我们需要能够为无人值守的基于cron的备份插入磁盘驱动器.

我认为修改udev脚本以生成一个在执行挂载之前等待几秒钟的分离作业可能会有所帮助,但是systemd似乎不顾一切地阻止它 – 它仍然等待分离的作业完成之前持续.

也许让udev脚本以某种方式搔痒udisks2是正确的方法?我很失败,所以任何建议都非常感激.

解决方法

经过几次错误的开始,我想出了这个.关键是在udev和安装脚本之间添加systemd单元服务.

(为了记录,我无法使用udeks2(通过类似udisksctl mount -b / dev / sdb1之类的东西)直接从udev规则或系统单元文件调用它.这似乎有竞争条件和设备节点还没有准备就绪,导致查找设备/ dev / sdb1的对象时出错.不幸的是,因为udisks2可以处理所有挂载点杂乱……)

繁重的工作由shell脚本完成,该脚本负责创建和删除安装点,以及安装和卸载驱动器.

/usr/local/bin/usb-mount.sh

#!/bin/bash

# This script is called from our systemd unit file to mount or unmount
# a USB drive.

usage()
{
    echo "Usage: $0 {add|remove} device_name (e.g. sdb1)"
    exit 1
}

if [[ $# -ne 2 ]]; then
    usage
fi

ACTION=$1
DEVBASE=$2
DEVICE="/dev/${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})

    # Figure out a mount point to use
    LABEL=${ID_FS_LABEL}
    if [[ -z "${LABEL}" ]]; then
        LABEL=${DEVBASE}
    elif /bin/grep -q " /media/${LABEL} " /etc/mtab; then
        # Already in use,make a unique one
        LABEL+="-${DEVBASE}"
    fi
    MOUNT_POINT="/media/${LABEL}"

    echo "Mount point: ${MOUNT_POINT}"

    /bin/mkdir -p ${MOUNT_POINT}

    # Global mount options
    OPTS="rw,relatime"

    # File system type specific mount options
    if [[ ${ID_FS_TYPE} == "vfat" ]]; then
        OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"
    fi

    if ! /bin/mount -o ${OPTS} ${DEVICE} ${MOUNT_POINT}; then
        echo "Error mounting ${DEVICE} (status = $?)"
        /bin/rmdir ${MOUNT_POINT}
        exit 1
    fi

    echo "**** Mounted ${DEVICE} at ${MOUNT_POINT} ****"
}

do_unmount()
{
    if [[ -z ${MOUNT_POINT} ]]; then
        echo "Warning: ${DEVICE} is not mounted"
    else
        /bin/umount -l ${DEVICE}
        echo "**** Unmounted ${DEVICE}"
    fi

    # Delete all empty dirs in /media that aren't being used as mount
    # points. This is kind of overkill,but if the drive was unmounted
    # prior to removal we no longer know its mount point,and we don't
    # want to leave it orphaned...
    for f in /media/* ; do
        if [[ -n $(/usr/bin/find "$f" -maxdepth 0 -type d -empty) ]]; then
            if ! /bin/grep -q " $f " /etc/mtab; then
                echo "**** Removing mount point $f"
                /bin/rmdir "$f"
            fi
        fi
    done
}

case "${ACTION}" in
    add)
        do_mount
        ;;
    remove)
        do_unmount
        ;;
    *)
        usage
        ;;
esac

反过来,脚本由systemd单元文件调用.我们使用“@”文件名语法,因此我们可以将设备名称作为参数传递.

/etc/systemd/system/usb-mount@.service

[Unit]
Description=Mount USB Drive on %i
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/usb-mount.sh add %i
ExecStop=/usr/local/bin/usb-mount.sh remove %i

最后,一些udev规则在hotplug / unplug上启动和停止systemd单元服务:

/etc/udev/rules.d/99-local.rules

KERNEL=="sd[a-z][0-9]",SUBSYSTEMS=="usb",ACTION=="add",RUN+="/bin/systemctl start usb-mount@%k.service"

KERNEL=="sd[a-z][0-9]",ACTION=="remove",RUN+="/bin/systemctl stop usb-mount@%k.service"

这似乎可以解决问题!一些有用的命令用于调试这样的东西:

> udevadm control -l debug打开详细日志记录/ var / log / syslog,这样你就可以看到发生了什么.>修改文件后的udevadm控制–reload-rulesrules.dir(可能没有必要,但不能伤害……).>修改systemd单元文件后的systemctl守护程序重新加载.

原文链接:https://www.f2er.com/linux/402660.html

猜你在找的Linux相关文章