UDEV:如何使用 %s{file} 替换说明符?

UDEV:如何使用 %s{file} 替换说明符?

我正在尝试创建一个udev规则来将我的 USB 设备命名为:Sandisk_Cruzer_16GB"。如何将%s{file}替换说明符与 一起使用SYMLINK

KERNEL=="sd[a-z]", SUBSYSTEM=="block", ATTR{ro}=="0", ATTR{removable}=="1", SUBSYSTEMS=="usb", DRIVERS=="usb-storage", SYMLINK+="usb%s{vendor}"

我注意到其中/sys/block/sdc/device/model包含部分所需数据。

输出udevadm info -a /dev/sdc|less

  looking at device '/devices/pci0000:00/0000:00:1d.7/usb4/4-3/4-3:1.0/host19/target19:0:0/19:0:0:0/block/
sdc':
    KERNEL=="sdc"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{ro}=="0"
    ATTR{size}=="30277632"
    ATTR{stat}=="     112      110     1776       80        0        0        0        0        0       72
       80"
    ATTR{range}=="16"
    ATTR{discard_alignment}=="0"
    ATTR{events}=="media_change"
    ATTR{ext_range}=="256"
    ATTR{events_poll_msecs}=="2000"
    ATTR{alignment_offset}=="0"
    ATTR{inflight}=="       0        0"
    ATTR{removable}=="1"
    ATTR{capability}=="51"
    ATTR{events_async}==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb4/4-3/4-3:1.0/host19/target19:0:0/19:0:0:0
':
    KERNELS=="19:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{rev}=="PMAP"
    ATTRS{type}=="0"
    ATTRS{scsi_level}=="7"
    ATTRS{model}=="Cruzer          "
    ATTRS{state}=="running"
    ATTRS{queue_type}=="none"
    ATTRS{iodone_cnt}=="0x16e"
    ATTRS{iorequest_cnt}=="0x16e"
    ATTRS{device_busy}=="0"
    ATTRS{evt_capacity_change_reported}=="0"
    ATTRS{timeout}=="30"
    ATTRS{evt_media_change}=="0"
    ATTRS{max_sectors}=="240"
    ATTRS{ioerr_cnt}=="0x1"
    ATTRS{queue_depth}=="1"
    ATTRS{vendor}=="SanDisk "
    ATTRS{evt_soft_threshold_reached}=="0"
    ATTRS{device_blocked}=="0"
    ATTRS{evt_mode_parameter_change_reported}=="0"
    ATTRS{evt_lun_change_reported}=="0"
    ATTRS{evt_inquiry_change_reported}=="0"
    ATTRS{iocounterbits}=="32"
    ATTRS{eh_timeout}=="10"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb4/4-3/4-3:1.0/host19/target19:0:0':
    KERNELS=="target19:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb4/4-3/4-3:1.0/host19':
    KERNELS=="host19"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb4/4-3/4-3:1.0':
    KERNELS=="4-3:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb-storage"
    ATTRS{bInterfaceClass}=="08"
    ATTRS{bInterfaceSubClass}=="06"
    ATTRS{bInterfaceProtocol}=="50"
    ATTRS{bNumEndpoints}=="02"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceNumber}=="00"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb4/4-3':
    KERNELS=="4-3"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{devpath}=="3"
    ATTRS{idVendor}=="0781"
    ATTRS{speed}=="480"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{busnum}=="4"
    ATTRS{devnum}=="22"
    ATTRS{configuration}==""
    ATTRS{bMaxPower}=="300mA"
    ATTRS{authorized}=="1"
    ATTRS{bmAttributes}=="80"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{maxchild}=="0"
    ATTRS{bcdDevice}=="0100"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{quirks}=="0x0"
    ATTRS{serial}=="FC0051E21A43B867"
    ATTRS{version}==" 2.00"
    ATTRS{urbnum}=="882"
    ATTRS{ltm_capable}=="no"
    ATTRS{manufacturer}=="SanDisk"
    ATTRS{removable}=="unknown"
    ATTRS{idProduct}=="5575"
    ATTRS{bDeviceClass}=="00"
    ATTRS{product}=="Cruzer"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb4':
    KERNELS=="usb4"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{devpath}=="0"
    ATTRS{idVendor}=="1d6b"
    ATTRS{speed}=="480"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{authorized_default}=="1"
    ATTRS{busnum}=="4"
    ATTRS{devnum}=="1"
    ATTRS{configuration}==""
    ATTRS{bMaxPower}=="0mA"
    ATTRS{authorized}=="1"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{maxchild}=="6"
    ATTRS{bcdDevice}=="0316"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{quirks}=="0x0"
    ATTRS{serial}=="0000:00:1d.7"
    ATTRS{version}==" 2.00"
    ATTRS{urbnum}=="573"
    ATTRS{ltm_capable}=="no"
    ATTRS{manufacturer}=="Linux 3.16.0-4-amd64 ehci_hcd"
    ATTRS{removable}=="unknown"
    ATTRS{idProduct}=="0002"
    ATTRS{bDeviceClass}=="09"
    ATTRS{product}=="EHCI Host Controller"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7':
    KERNELS=="0000:00:1d.7"
    SUBSYSTEMS=="pci"
    DRIVERS=="ehci-pci"
    ATTRS{irq}=="20"
    ATTRS{subsystem_vendor}=="0x1028"
    ATTRS{broken_parity_status}=="0"
    ATTRS{class}=="0x0c0320"
    ATTRS{companion}==""
    ATTRS{driver_override}=="(null)"
    ATTRS{consistent_dma_mask_bits}=="32"
    ATTRS{dma_mask_bits}=="32"
    ATTRS{local_cpus}=="00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000003"
    ATTRS{device}=="0x293a"
    ATTRS{uframe_periodic_max}=="100"
    ATTRS{enable}=="1"
    ATTRS{msi_bus}==""
    ATTRS{local_cpulist}=="0-1"
    ATTRS{vendor}=="0x8086"
    ATTRS{subsystem_device}=="0x02aa"
    ATTRS{numa_node}=="-1"
    ATTRS{d3cold_allowed}=="1"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""

输出udevadm info /dev/sdc

P: /devices/pci0000:00/0000:00:1d.7/usb4/4-3/4-3:1.0/host19/target19:0:0/19:0:0:0/block/sdc
N: sdc
S: disk/by-id/usb-SanDisk_Cruzer_FC0051E21A43B867-0:0
S: disk/by-path/pci-0000:00:1d.7-usb-0:3:1.0-scsi-0:0:0:0
S: usbSandisk
E: DEVLINKS=/dev/disk/by-id/usb-SanDisk_Cruzer_FC0051E21A43B867-0:0 /dev/disk/by-path/pci-0000:00:1d.7-usb-0:3:1.0-scsi-0:0:0:0 /dev/usbSandisk
E: DEVNAME=/dev/sdc
E: DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb4/4-3/4-3:1.0/host19/target19:0:0/19:0:0:0/block/sdc
E: DEVTYPE=disk
E: ID_BUS=usb
E: ID_DRIVE_THUMB=1
E: ID_INSTANCE=0:0
E: ID_MODEL=Cruzer
E: ID_MODEL_ENC=Cruzer\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
E: ID_MODEL_ID=5575
E: ID_PART_TABLE_TYPE=dos
E: ID_PART_TABLE_UUID=000610ac
E: ID_PATH=pci-0000:00:1d.7-usb-0:3:1.0-scsi-0:0:0:0
E: ID_PATH_TAG=pci-0000_00_1d_7-usb-0_3_1_0-scsi-0_0_0_0
E: ID_REVISION=PMAP
E: ID_SERIAL=SanDisk_Cruzer_FC0051E21A43B867-0:0
E: ID_SERIAL_SHORT=FC0051E21A43B867
E: ID_TYPE=disk
E: ID_USB_DRIVER=usb-storage
E: ID_USB_INTERFACES=:080650:
E: ID_USB_INTERFACE_NUM=00
E: ID_VENDOR=SanDisk
E: ID_VENDOR_ENC=SanDisk\x20
E: ID_VENDOR_ID=0781
E: MAJOR=8
E: MINOR=32
E: SUBSYSTEM=block
E: TAGS=:systemd:
E: USEC_INITIALIZED=89765815

答案1

如果您阅读该man页面(强调我的):

$attr{文件},%s{文件}
        在规则的所有键所在的设备上找到的 sysfs 属性的值
        已匹配。如果匹配设备没有这样的属性,并且
        以前的内核、子系统、驱动程序或属性测试选择了一个父母
        设备,然后使用该父设备的属性

匹配的设备没有属性,并且您在规则中vendor使用与特定父设备匹配的属性:SUBSYSTEMS=="usb", DRIVERS=="usb-storage"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb4/4-3/4-3:1.0':
    KERNELS=="4-3:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb-storage"
    ATTRS{bInterfaceClass}=="08"
    ATTRS{bInterfaceSubClass}=="06"
    ATTRS{bInterfaceProtocol}=="50"
    ATTRS{bNumEndpoints}=="02"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceNumber}=="00"

正如您所看到的,父设备vendor也没有属性。
解决方案是使用环境变量,例如:

KERNEL=="sd[a-z]", SUBSYSTEM=="block", ATTR{ro}=="0", ATTR{removable}=="1", \
SUBSYSTEMS=="usb", DRIVERS=="usb-storage", SYMLINK+="usb_%E{ID_VENDOR}_%E{ID_MODEL}"

这将创建一个名为 的符号链接usb_Sandisk_Cruzer。请注意,多个设备具有相同的id_供应商型号该链接将指向最后一个附加的链接。为了避免这种情况,您可以添加ID_SERIAL_SHORT例如SYMLINK+="usb_%E{ID_VENDOR}_%E{ID_MODEL}_%E{ID_SERIAL_SHORT}"

相关内容