有一段时间,我有一个有效的 udev 规则来自动挂载媒体设备。
/etc/udev/rules.d/61-mount_media_by_label.rules
#
# To propagate udev's mountpoint to the user space, MountFlags must have a value "shared" in the /usr/lib/systemd/system/systemd-udevd.service.
#
# Ignore devices that aren't storage block-devices and block-devices that are already listed in /etc/fstab.
KERNEL!="sd[a-z][1-9]*", GOTO="mount_media_by_label_end"
PROGRAM="/bin/grep -e '^UUID=%E{ID_FS_UUID}' /etc/fstab", RESULT!="", GOTO="mount_media_by_label_end"
# Decide the name for device's mountpoint directory, based on device's label.
ENV{ID_FS_LABEL}!="", ENV{mountpoint}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{mountpoint}="usb-%k"
# If device is being plugged in, set options for mount command.
ACTION=="add", ENV{mount_options}="relatime"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="%E{mount_options},utf8,gid=100,umask=002"
# If device is being plugged in, create mountpoint directory in /media and mount device node to it.
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{mountpoint}", RUN+="/bin/mount -o %E{mount_options} /dev/%k /media/%E{mountpoint}"
# If device is being plugged out, unmount it and delete its mountpoint directory.
ACTION=="remove", ENV{mountpoint}!="", RUN+="/bin/umount -l /media/%E{mountpoint}", RUN+="/bin/rmdir /media/%E{mountpoint}"
# Label for early exit.
LABEL="mount_media_by_label_end"
为了使这个规则起作用,我只需将MountFlags
选项的值更改shared
为
/usr/lib/systemd/system/systemd-udevd.service
在我更新systemd
到 version后239
,这个文件看起来就不同了。
我注意到 2 个可能有问题的更改:
MountFlags
默认设置中未指定该选项。- 有一个新选项
PrivateMounts
设置为yes
。
从systemd
的文档中我发现现在我只需要设置PrivateMounts=no
和传播挂载点就可以到达用户空间。
然而,这种情况并非如此。
我努力了
- 改变
PrivateMounts=no
- 更改
PrivateMounts=no
和添加MountFlags=shared
但两者都不起作用。
systemd v239
从 udev 规则及以后安装媒体设备的正确方法是什么?