我无法使用udev为USB移动硬盘创建符号链接

我无法使用udev为USB移动硬盘创建符号链接

我在使用上遇到一个问题udev:我想在Ubuntu16.04上实现USB热插拔,所以我写了一条udev规则如下:

KERNEL=="sd[a-z]*", ACTION=="add",SUBSYSTEM=="block",SYMLINK+="USB",RUN+="/bin/sh /root/load_device.sh %k"

当我使用 USB 记忆棒时,它可以工作,并且符号链接/dev/USB直接指向/dev/sda1/dev/sdb1,2,3...

但是,当我使用USB移动硬盘时,直接/dev/USB/dev/sdb、 或/dev/sdc,却没有到分区。我测试了规则触发序列,它确实触发了两次,第一次sdb是 ,第二次是sdb1

为什么/dev/USB链接指向sdb而不是指向sdb1

这是系统日志:

Apr 16 09:28:40 ntfs-3g[1928]: Version 2015.3.14AR.1 integrated FUSE 28 Apr 16 09:28:40 ntfs-3g[1928]: Mounted /dev/sdb1 (Read-Write, label "BOOTCAMP", NTFS 3.1)
Apr 16 09:28:40 ntfs-3g[1928]: Cmdline options: rw Apr 16 09:28:40 ntfs-3g[1928]: Mount options: rw,allow_other,nonempty,relatime,fsname=/dev/sdb1,blkdev,blksize=4096
Apr 16 09:28:40 ntfs-3g[1928]: Ownership and permissions disabled, configuration type 7
Apr 16 09:28:40 systemd[1029]: dev-USB.device: Dev dev-USB.device appeared twice with different sysfs paths /sys/devices/pci0000:00/0000:00:15.0/usb2/2-4/2-4:1.0/host2/target2:0:0/2:0:0:0/block/sdb and /sys/devices/pci0000:00/0000:00:15.0/usb2/2-4/2-4:1.0/host2/target2:0:0/2:0:0:0/block/sdb/sdb1
Apr 16 09:28:40 systemd[869]: dev-USB.device: Dev dev-USB.device appeared twice with different sysfs paths /sys/devices/pci0000:00/0000:00:15.0/usb2/2-4/2-4:1.0/host2/target2:0:0/2:0:0:0/block/sdb and /sys/devices/pci0000:00/0000:00:15.0/usb2/2-4/2-4:1.0/host2/target2:0:0/2:0:0:0/block/sdb/sdb1
Apr 16 09:28:40 systemd[1]: dev-USB.device: Dev dev-USB.device appeared twice with different sysfs paths /sys/devices/pci0000:00/0000:00:15.0/usb2/2-4/2-4:1.0/host2/target2:0:0/2:0:0:0/block/sdb and /sys/devices/pci0000:00/0000:00:15.0/usb2/2-4/2-4:1.0/host2/target2:0:0/2:0:0:0/block/sdb/sdb1

这是udevinfo

P: /devices/pci0000:00/0000:00:15.0/usb2/2-4/2-4:1.0/host3/target3:0:0/3:0:0:0/block/sdc
N: sdc
S: USB
S: disk/by-id/ata-Samsung_SSD_840_EVO_120GB_S1D5NSBF494918T
S: disk/by-id/wwn-0x50025388a03b9a88
S: disk/by-path/pci-0000:00:15.0-usb-0:4:1.0-scsi-0:0:0:0
E: DEVLINKS=/dev/disk/by-id/ata-Samsung_SSD_840_EVO_120GB_S1D5NSBF494918T /dev/disk/by-id/wwn-0x50025388a03b9a88 /dev/disk/by-path/pci-0000:00:15.0-usb-0:4:1.0-scsi-0:0:0:0 /dev/USB
E: DEVNAME=/dev/sdc E: DEVPATH=/devices/pci0000:00/0000:00:15.0/usb2/2-4/2-4:1.0/host3/target3:0:0/3:0:0:0/block/sdc
E: DEVTYPE=disk
E: ID_ATA=1
E: ID_ATA_DOWNLOAD_MICROCODE=1
E: ID_ATA_FEATURE_SET_HPA=1
E: ID_ATA_FEATURE_SET_HPA_ENABLED=1
E: ID_ATA_FEATURE_SET_PM=1
E: ID_ATA_FEATURE_SET_PM_ENABLED=1
E: ID_ATA_FEATURE_SET_SECURITY=1
E: ID_ATA_FEATURE_SET_SECURITY_ENABLED=0
E: ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=8
E: ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=2
E: ID_ATA_FEATURE_SET_SMART=1
E: ID_ATA_FEATURE_SET_SMART_ENABLED=1
E: ID_ATA_ROTATION_RATE_RPM=0
E: ID_ATA_SATA=1
E: ID_ATA_SATA_SIGNAL_RATE_GEN1=1
E: ID_ATA_SATA_SIGNAL_RATE_GEN2=1
E: ID_ATA_WRITE_CACHE=1
E: ID_ATA_WRITE_CACHE_ENABLED=1
E: ID_BUS=ata
E: ID_MODEL=Samsung_SSD_840_EVO_120GB
E: ID_MODEL_ENC=Samsung\x20SSD\x20840\x20EVO\x20120GB\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
E: ID_PART_TABLE_TYPE=dos
E: ID_PART_TABLE_UUID=5be6511f
E: ID_PATH=pci-0000:00:15.0-usb-0:4:1.0-scsi-0:0:0:0
E: ID_PATH_TAG=pci-0000_00_15_0-usb-0_4_1_0-scsi-0_0_0_0
E: ID_REVISION=EXT0BB6Q
E: ID_SERIAL=Samsung_SSD_840_EVO_120GB_S1D5NSBF494918T
E: ID_SERIAL_SHORT=S1D5NSBF494918T
E: ID_TYPE=disk
E: ID_WWN=0x50025388a03b9a88
E: ID_WWN_WITH_EXTENSION=0x50025388a03b9a88
E: MAJOR=8
E: MINOR=32
E: SUBSYSTEM=block
E: TAGS=:systemd:
E: USEC_INITIALIZED=1066199633

当我尝试 KERNEL=="sd[az][1-9]" 时,它无法创建符号链接 /dev/USB

Apr 18 01:35:31  kernel: [ 1059.884210] usb 2-4: new SuperSpeed USB device number 9 using xhci_hcd
Apr 18 01:35:31  kernel: [ 1059.909016] usb 2-4: New USB device found, idVendor=152d, idProduct=9561
Apr 18 01:35:31  kernel: [ 1059.909021] usb 2-4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Apr 18 01:35:31  kernel: [ 1059.909023] usb 2-4: Product: Fanxiang P101/240GB
Apr 18 01:35:31  kernel: [ 1059.909025] usb 2-4: Manufacturer: Fanxiang P101/240GB
Apr 18 01:35:31  kernel: [ 1059.909026] usb 2-4: SerialNumber: 000000000443
Apr 18 01:35:31  kernel: [ 1059.914003] scsi host2: uas
Apr 18 01:35:31  mtp-probe: checking bus 2, device 9: "/sys/devices/pci0000:00/0000:00:15.0/usb2/2-4"
Apr 18 01:35:31  mtp-probe: bus: 2, device: 9 was not an MTP device
Apr 18 01:35:31  kernel: [ 1059.916507] scsi 2:0:0:0: Direct-Access     Fanxiang P101/240GB       1508 PQ: 0 ANSI: 6
Apr 18 01:35:31  kernel: [ 1059.917452] sd 2:0:0:0: Attached scsi generic sg1 type 0
Apr 18 01:35:32  kernel: [ 1060.658766] sd 2:0:0:0: [sdb] 468862128 512-byte logical blocks: (240 GB/224 GiB)
Apr 18 01:35:32  kernel: [ 1060.658770] sd 2:0:0:0: [sdb] 4096-byte physical blocks
Apr 18 01:35:32  kernel: [ 1060.659275] sd 2:0:0:0: [sdb] Write Protect is off
Apr 18 01:35:32  kernel: [ 1060.659280] sd 2:0:0:0: [sdb] Mode Sense: 53 00 00 08
Apr 18 01:35:32  kernel: [ 1060.659617] sd 2:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
Apr 18 01:35:32  kernel: [ 1060.667689]  sdb: sdb1
Apr 18 01:35:32  kernel: [ 1060.669525] sd 2:0:0:0: [sdb] Attached SCSI disk
Apr 18 01:35:33  ntfs-3g[2768]: Version 2015.3.14AR.1 integrated FUSE 28
Apr 18 01:35:33  ntfs-3g[2768]: Mounted /dev/sdb1 (Read-Write, label "longtian", NTFS 3.1)
Apr 18 01:35:33  ntfs-3g[2768]: Cmdline options: rw
Apr 18 01:35:33  ntfs-3g[2768]: Mount options: rw,allow_other,nonempty,relatime,fsname=/dev/sdb1,blkdev,blksize=4096
Apr 18 01:35:33  ntfs-3g[2768]: Ownership and permissions disabled, configuration type 7

答案1

抛开解决 USB 记忆棒和硬盘驱动器不同行为之谜的努力不谈,您可以尝试使规则udev具体化,以便它仅适用于分区,IE以数字结尾的设备标识符,通过指定

KERNEL=="sd[a-z][1-9]"

作为条件。

相关内容