与 udev 的斗争

与 udev 的斗争

我想要automount在 Ubuntu 上使用我的 SD 卡。我使用udevadm(或)尝试了以下规则/lib/systemd/systemd-udevd

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"
# Global mount options
ACTION=="add|change", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add|change", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
# Mount the device
ACTION=="add|change", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}", RUN+=" echo /bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name\
} > /tmp/debug_out.txt"
#ACTION=="add|change"
# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
# Exit
LABEL="media_by_label_auto_mount_end"

代码来自网络。

`udevadm monitor` indicates that the card is detected

KERNEL[1778159.935932] change   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc (block)
KERNEL[1778159.939553] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc/sdc1 (block)
KERNEL[1778159.942501] change   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc (block)
KERNEL[1778159.942615] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc/sdc1 (block)
UDEV  [1778160.026230] change   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc (block)
UDEV  [1778160.101285] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc/sdc1 (block)
UDEV  [1778160.184648] change   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc (block)
KERNEL[1778160.320013] add      /kernel/slab/ext4_inode_cache/cgroup/ext4_inode_cache(14341:systemd-udevd.service) (cgroup)
UDEV  [1778160.322555] add      /kernel/slab/ext4_inode_cache/cgroup/ext4_inode_cache(14341:systemd-udevd.service) (cgroup)
KERNEL[1778160.387280] add      /kernel/slab/fat_inode_cache/cgroup/fat_inode_cache(14341:systemd-udevd.service) (cgroup)
UDEV  [1778160.388849] add      /kernel/slab/fat_inode_cache/cgroup/fat_inode_cache(14341:systemd-udevd.service) (cgroup)
UDEV  [1778160.418509] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc/sdc1 (block)

和输出udevadm test /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host11/target11:0:0/11:0:0:0/block/sdc/sdc1看起来也不错

[...]
run: '/bin/mkdir -p /media/usbhd-sdc1'
run: '/bin/mount -o relatime /dev/sdc1 /media/usbhd-sdc1'
run: '/usr/bin/unshare -m /usr/bin/snap auto-import --mount=/dev/sdc1'
[...]

但是,规则只创建目录,但不挂载卡。使用权限手动执行上述规则sudo可以按预期工作(创建目录不起作用,sudo因此这不是问题)

(后续小问题:我想从卡中复制文件,触发作业和unmount卡。我是否也可以从卡中执行此操作unmount或使用不同的机制)

=======================

更改了所有内容,如下所述:

Udev/rules.d

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"

ACTION=="add", SUBSYSTEM=="block",TAG+="systemd", ENV{SYSTEMD_WANTS}=sd-automounter@%k.service
ACTION=="change", SUBSYSTEM=="block",TAG+="systemd", RUN+="systemctl start sd-automounter@%k"
# Clean up after removal  
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"  
# Exit  

服务说明

# /etc/systemd/system/[email protected]
[Service]
Type=oneshot
ExecStart=/usr/local/libexec/sd-automounter %I(base)

和服务

#!/bin/sh
# /usr/local/libexec/sd-automounter
echo "" > /tmp/debug.out
DEVICE_NAME=/dev/"$1"
echo $DEVICE_NAME >>/tmp/debug.out
udevadm info --query=property --export $DEVICE_NAME >> /tmp/debug.out
echo "" >> /tmp/debug.out
CARD_NAME=$(udevadm info --query=property --export $DEVICE_NAME | grep ID_FS_LABEL= | awk -F\' '{print $2}')
echo $CARD_NAME >> /tmp/debug.out
#CARD_NAME=$(/sbin/blkid  | grep "$DEVICE_NAME" | awk -F\" '{print $2}' |sed 's/ /_/g')
udevadm info --query=property --export /dev/sdc1
echo $CARD_NAME
mkdir -p /media/$CARD_NAME
mount -t vfat $DEVICE_NAME /media/$CARD_NAME
thanks for all help in advance

手动执行它systemctl start sd-automounter@sdd1就像一个魅力。

但是在udevadm monitor显示时移除和插入没有任何反应

monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[1977111.519970] change   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host12/target12:0:0/12:0:0:0/block/sdd (block)
KERNEL[1977111.523252] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host12/target12:0:0/12:0:0:0/block/sdd/sdd1 (block)
KERNEL[1977111.526457] change   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host12/target12:0:0/12:0:0:0/block/sdd (block)
KERNEL[1977111.526572] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host12/target12:0:0/12:0:0:0/block/sdd/sdd1 (block)
UDEV  [1977111.612257] change   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host12/target12:0:0/12:0:0:0/block/sdd (block)
UDEV  [1977111.614914] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host12/target12:0:0/12:0:0:0/block/sdd/sdd1 (block)
UDEV  [1977111.697870] change   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host12/target12:0:0/12:0:0:0/block/sdd (block)
UDEV  [1977111.791344] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3:1.0/host12/target12:0:0/12:0:0:0/block/sdd/sdd1 (block)

答案1

systemd-udev 真的不再希望您直接从规则挂载文件系统。您可以在此网站上找到很多有关它的问题:-)。

在最新版本的 systemd 中,udev 服务文件具有SystemCallFilter=@system-service @module @raw-io.这不允许挂载文件系统所需的系统调用。

(另外,我相信如果你曾经直接用于mountFUSE 文件系统,例如ntfs-3g.如果启动 FUSE 服务器进程,最好将其放在专用的 systemd 单元中,例如单元.mount。如果您让 FUSE 进程保留在当前单元内,那么 FUSE 进程的生命周期将与您正在运行的单元的生命周期耦合)。

为了避免 中的命令将来(或当前)出现问题RUN+=,您的规则可以改为使用ENV{SYSTEMD_WANTS}=my-automounter@%k.service,然后

# /etc/systemd/system/[email protected]
[Service]
Type=oneshot
ExecStart=/usr/local/libexec/my-automounter %I
#!/bin/sh
# /usr/local/libexec/my-automounter

DEV=/dev/"$1"

# You can make this script as complicated as you want.
# You can read udev properties if you want, using 
# eval "$(udevadm info --query=property --export "$DEV")"
...

如果您想测试手动调用该单元,您可以使用systemctl start my-automounter@sdc1.

如果 systemd 运行脚本时发生错误,您可以使用systemctl status my-automounter@*, 或 来查看错误消息journalctl -b -u my-automounter@*

这也避免了您必须处理udev特定的错误报告。我认为两者sh都有systemd有用的错误报告,以防程序突然死机。例如,他们应该报告程序是否被 signal 杀死SIGSYS,因为它试图调用已使用SystemCallFilter=:-) 阻止的系统调用。

答案2

第二个选项的问题是我必须使用完整路径

ACTION=="add", SUBSYSTEM=="block",TAG+="systemd", RUN+="/bin/systemctl start sd-automounter@%k"
ACTION=="change", SUBSYSTEM=="block",TAG+="systemd", RUN+="/bin/systemctl start sd-automounter@%k"

并使用运行选项来添加

相关内容