UDEV: Como eu uso o especificador de substituição% s {file}?

2

Estou tentando criar uma regra udev para nomear meus dispositivos USB como: Sandisk_Cruzer_16GB ". Como usar o especificador de substituição %s{file} com SYMLINK ?

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

Noto que /sys/block/sdc/device/model contém parte dos dados necessários.

Saída de 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==""

Saída de 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
    
por don_crissti 21.09.2015 / 10:53

1 resposta

2

Se você ler a página man (enfatize a minha):

$attr{file}, %s{file}
        The value of a sysfs attribute found at the device where all keys of the rule
        have matched. If the matching device does not have such an attribute, and
        a previous KERNELS, SUBSYSTEMS, DRIVERS or ATTRS test selected a parent
        device, then the attribute from that parent device is used.

O dispositivo correspondente não tem o atributo vendor e você está usando SUBSYSTEMS=="usb", DRIVERS=="usb-storage" em sua regra que corresponde a um dispositivo pai específico:

  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"

e como você pode ver, o dispositivo pai também não possui o atributo vendor .
A solução é usar variáveis de ambiente, por exemplo:

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}"

isso criaria um link simbólico chamado usb_Sandisk_Cruzer . Note que com vários dispositivos com o mesmo id_vendor e id_model o link apontará para o último que foi anexado. Para evitar isso, você pode adicionar ID_SERIAL_SHORT , por exemplo SYMLINK+="usb_%E{ID_VENDOR}_%E{ID_MODEL}_%E{ID_SERIAL_SHORT}"

    
por 21.09.2015 / 12:22

Tags