Udev

UDEV:如何使用 %s{file} 替換說明符?

  • March 4, 2019

我正在嘗試創建一個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

如果您閱讀該man頁面(強調我的):

$attr{文件}, %s{文件}
在規則的所有鍵所在的設備上找到的 sysfs 屬性的值
有匹配。如果匹配的設備沒有這樣的屬性,並且
之前的 KERNELS、SUBSYSTEMS、DRIVERS 或 ATTRS*測試選擇了一個父項
設備,然後使用來自該父設備的屬性*。

匹配設備沒有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_vendorid_model的多個設備,連結將指向最後一個附加的設備。為避免這種情況,您可以添加ID_SERIAL_SHORT例如SYMLINK+="usb_%E{ID_VENDOR}_%E{ID_MODEL}_%E{ID_SERIAL_SHORT}"

引用自:https://unix.stackexchange.com/questions/230998