如何知道设备是否已安装在 udev 规则中?

如何知道设备是否已安装在 udev 规则中?

我试图避免挂载之前使用 fstab 挂载的设备。这是现在发生的情况:这是现在发生的情况:

/dev/sda1 on /boot
/dev/sda2 on /
/dev/sda3 on /home
/dev/sda2 on /media/ROOT
/dev/sdc1 on /media/Maximum
/dev/sda3 on /media/HOME
/dev/sda1 on /media/BOOT

正如您所见,它是双重安装的。

这是我的规则:

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
IMPORT{program}="/sbin/blkid -o udev -p %N"
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="%E{ID_PART_ENTRY_UUID}"
ACTION=="add", ENV{mount_options}="relatime"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
ACTION=="add", PROGRAM="findmnt -rno SOURCE %N", RESULT!="",GOTO="media_by_label_auto_mount_end"
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
LABEL="media_by_label_auto_mount_end"

我尝试使用它findmnt来检查目标是否存在但没有执行任何操作。

希望说清楚。谢谢。

答案1

找到了:

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
IMPORT{program}="/sbin/blkid -o udev -p %N"
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="%E{ID_PART_ENTRY_UUID}"
ACTION=="add", PROGRAM=="/bin/grep -q ' /dev/%k ' /proc/self/mountinfo", GOTO="media_by_label_auto_mount_end"
ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"
ACTION=="add", ENV{mount_options}="relatime"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},gid=100,dmask=000,fmask=111,utf8"
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options} /dev/%k '/media/%E{dir_name}'"
ACTION=="add", ENV{ID_FS_TYPE}!="ntfs", RUN+="/bin/mount -t auto -o %E{mount_options} /dev/%k '/media/%E{dir_name}'"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"
LABEL="media_by_label_auto_mount_end"

相关内容