Udev
Материал из noname.com.ua
Версия от 14:11, 15 июля 2011; Sirmax (обсуждение | вклад)
udev
Использование подсистемы udev в Gentoo
Захотелось сделать как обычно - автомонтирование фотоаппарата. телефона и флешек, причем в разные mount point. Второе - пробрасывать определенные устройства внутрь виртуальных машин.
Общие правила
- Udev считывает файлы из /etc/udev/rules.d в алфавитном порядке.
- Найдя первое подходящее правило для устройства, udev прекращает дальнейший поиск.
Автомониирование
Подключаю устройство (в моем случае это телефон, флешка в котором видиться как диск)
Как определилось устройство?
13:35:03-root@sirmax:~#dmesg | tail [6050777.245910] scsi 21:0:0:0: Direct-Access PQ: 0 ANSI: 0 [6050777.246123] sd 21:0:0:0: Attached scsi generic sg6 type 0 [6050777.395654] sd 21:0:0:0: [sdf] 15572992 512-byte logical blocks: (7.97 GB/7.42 GiB) [6050777.445663] sd 21:0:0:0: [sdf] Write Protect is off [6050777.445669] sd 21:0:0:0: [sdf] Mode Sense: 00 06 00 00 [6050777.445672] sd 21:0:0:0: [sdf] Assuming drive cache: write through [6050777.493599] sd 21:0:0:0: [sdf] Assuming drive cache: write through [6050777.498359] sdf: sdf1 [6050777.508225] sd 21:0:0:0: [sdf] Assuming drive cache: write through [6050777.508231] sd 21:0:0:0: [sdf] Attached SCSI removable disk
Определить всю информацию о устройстве
Полезно мониторить:
16:27:50-root@sirmax:/etc/udev#udevadm monitor monitor will print the received events for: UDEV - the event which udev sends out after rule processing KERNEL - the kernel uevent
Определить всю информацию о устройстве
13:32:56-root@sirmax:~#udevadm info -a --name=sdf Udevadm info starts with the device specified by the devpath and then walks up the chain of parent devices. It prints for every device found, all possible attributes in the udev rules key format. A rule to match, can be composed by the attributes of the device and the attributes from one single parent device. looking at device '/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2/2-3.2:1.0/host21/target21:0:0/21:0:0:0/block/sdf': KERNEL=="sdf" SUBSYSTEM=="block" DRIVER=="" ATTR{range}=="16" ATTR{ext_range}=="256" ATTR{removable}=="1" ATTR{ro}=="0" ATTR{size}=="15572992" ATTR{alignment_offset}=="0" ATTR{discard_alignment}=="0" ATTR{capability}=="51" ATTR{stat}==" 64 26 720 356 0 0 0 0 0 356 356" ATTR{inflight}==" 0 0" looking at parent device '/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2/2-3.2:1.0/host21/target21:0:0/21:0:0:0': KERNELS=="21:0:0:0" SUBSYSTEMS=="scsi" DRIVERS=="sd" ATTRS{device_blocked}=="0" ATTRS{type}=="0" ATTRS{scsi_level}=="0" ATTRS{vendor}==" " ATTRS{model}==" " ATTRS{rev}==" " ATTRS{state}=="running" ATTRS{timeout}=="30" ATTRS{iocounterbits}=="32" ATTRS{iorequest_cnt}=="0x20fc" ATTRS{iodone_cnt}=="0x20fc" ATTRS{ioerr_cnt}=="0x0" ATTRS{modalias}=="scsi:t-0x00" ATTRS{evt_media_change}=="0" ATTRS{queue_depth}=="1" ATTRS{queue_type}=="none" ATTRS{max_sectors}=="240" looking at parent device '/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2/2-3.2:1.0/host21/target21:0:0': KERNELS=="target21:0:0" SUBSYSTEMS=="scsi" DRIVERS=="" looking at parent device '/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2/2-3.2:1.0/host21': KERNELS=="host21" SUBSYSTEMS=="scsi" DRIVERS=="" looking at parent device '/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2/2-3.2:1.0': KERNELS=="2-3.2:1.0" SUBSYSTEMS=="usb" DRIVERS=="usb-storage" ATTRS{bInterfaceNumber}=="00" ATTRS{bAlternateSetting}==" 0" ATTRS{bNumEndpoints}=="02" ATTRS{bInterfaceClass}=="08" ATTRS{bInterfaceSubClass}=="06" ATTRS{bInterfaceProtocol}=="50" ATTRS{modalias}=="usb:v0BB4p0C15d0000dc00dsc00dp00ic08isc06ip50" ATTRS{supports_autosuspend}=="1" looking at parent device '/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2': KERNELS=="2-3.2" SUBSYSTEMS=="usb" DRIVERS=="usb" ATTRS{configuration}=="" ATTRS{bNumInterfaces}==" 1" ATTRS{bConfigurationValue}=="1" ATTRS{bmAttributes}=="c0" ATTRS{bMaxPower}=="500mA" ATTRS{urbnum}=="16998" ATTRS{idVendor}=="0bb4" ATTRS{idProduct}=="0c15" ATTRS{bcdDevice}=="0000" ATTRS{bDeviceClass}=="00" ATTRS{bDeviceSubClass}=="00" ATTRS{bDeviceProtocol}=="00" ATTRS{bNumConfigurations}=="1" ATTRS{bMaxPacketSize0}=="64" ATTRS{speed}=="480" ATTRS{busnum}=="2" ATTRS{devnum}=="22" ATTRS{devpath}=="3.2" ATTRS{version}==" 2.00" ATTRS{maxchild}=="0" ATTRS{quirks}=="0x0" ATTRS{avoid_reset_quirk}=="0" ATTRS{authorized}=="1" ATTRS{manufacturer}=="HTC" ATTRS{product}=="Storage" ATTRS{serial}=="3fbf5000-7351-0801-3583-530114835680" looking at parent device '/devices/pci0000:00/0000:00:0b.1/usb2/2-3': KERNELS=="2-3" SUBSYSTEMS=="usb" DRIVERS=="usb" ATTRS{configuration}=="" ATTRS{bNumInterfaces}==" 1" ATTRS{bConfigurationValue}=="1" ATTRS{bmAttributes}=="e0" ATTRS{bMaxPower}=="100mA" ATTRS{urbnum}=="480" ATTRS{idVendor}=="05e3" ATTRS{idProduct}=="0605" ATTRS{bcdDevice}=="060b" ATTRS{bDeviceClass}=="09" ATTRS{bDeviceSubClass}=="00" ATTRS{bDeviceProtocol}=="01" ATTRS{bNumConfigurations}=="1" ATTRS{bMaxPacketSize0}=="64" ATTRS{speed}=="480" ATTRS{busnum}=="2" ATTRS{devnum}=="2" ATTRS{devpath}=="3" ATTRS{version}==" 2.00" ATTRS{maxchild}=="4" ATTRS{quirks}=="0x0" ATTRS{avoid_reset_quirk}=="0" ATTRS{authorized}=="1" ATTRS{product}=="USB2.0 Hub" looking at parent device '/devices/pci0000:00/0000:00:0b.1/usb2': KERNELS=="usb2" SUBSYSTEMS=="usb" DRIVERS=="usb" ATTRS{configuration}=="" ATTRS{bNumInterfaces}==" 1" ATTRS{bConfigurationValue}=="1" ATTRS{bmAttributes}=="e0" ATTRS{bMaxPower}==" 0mA" ATTRS{urbnum}=="36" ATTRS{idVendor}=="1d6b" ATTRS{idProduct}=="0002" ATTRS{bcdDevice}=="0206" ATTRS{bDeviceClass}=="09" ATTRS{bDeviceSubClass}=="00" ATTRS{bDeviceProtocol}=="00" ATTRS{bNumConfigurations}=="1" ATTRS{bMaxPacketSize0}=="64" ATTRS{speed}=="480" ATTRS{busnum}=="2" ATTRS{devnum}=="1" ATTRS{devpath}=="0" ATTRS{version}==" 2.00" ATTRS{maxchild}=="8" ATTRS{quirks}=="0x0" ATTRS{avoid_reset_quirk}=="0" ATTRS{authorized}=="1" ATTRS{manufacturer}=="Linux 2.6.37-rc6-sirmax1 ehci_hcd" ATTRS{product}=="EHCI Host Controller" ATTRS{serial}=="0000:00:0b.1" ATTRS{authorized_default}=="1" looking at parent device '/devices/pci0000:00/0000:00:0b.1': KERNELS=="0000:00:0b.1" SUBSYSTEMS=="pci" DRIVERS=="ehci_hcd" ATTRS{vendor}=="0x10de" ATTRS{device}=="0x026e" ATTRS{subsystem_vendor}=="0x1458" ATTRS{subsystem_device}=="0x5004" ATTRS{class}=="0x0c0320" ATTRS{irq}=="23" ATTRS{local_cpus}=="f" ATTRS{local_cpulist}=="0-3" ATTRS{modalias}=="pci:v000010DEd0000026Esv00001458sd00005004bc0Csc03i20" ATTRS{dma_mask_bits}=="32" ATTRS{consistent_dma_mask_bits}=="32" ATTRS{enable}=="1" ATTRS{broken_parity_status}=="0" ATTRS{msi_bus}=="" ATTRS{companion}=="" looking at parent device '/devices/pci0000:00': KERNELS=="pci0000:00" SUBSYSTEMS=="" DRIVERS==""
Простейшее правило
Создаю простейшее правило /etc/udev/rules.d/99-usb.rules
BUS=="usb", SUBSYSTEM=="block", KERNEL=="sd*", ACTION=="add", NAME="%k", GROUP="disk", RUN+="/etc/udev/udev-flash-mount.sh add %k"
Cоответвенно скрипт
#!bin/bash LOGGER="usr/bin//logger -t [udev-flash-mount.sh]" env | $LOGGER echo $1 $2 | $LOGGER
При подключении
13:58:38-root@sirmax:/etc/udev#cat /var/log/messages | grep udev-flash-mount.sh Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_USB_DRIVER=usb-storage Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_MODEL=Storage Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_MODEL_ENC=Storage Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_REVISION=0000 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: DEVTYPE=disk Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_TABLE=1 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_BUS=usb Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: SUBSYSTEM=block Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_SERIAL=HTC_Storage_3fbf5000-7351-0801-3583-530114835680-0:0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: DEVPATH=/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2/2-3.2:1.0/host26/target26:0:0/26:0:0:0/block/sdf Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_MODEL_ID=0c15 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_VENDOR_ENC=HTC Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_TABLE_SCHEME=mbr Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: MINOR=80 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ACTION=add Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_TABLE_COUNT=1 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: PWD=/ Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDEV_LOG=3 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: MAJOR=8 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: DEVLINKS=/dev/block/8:80 /dev/disk/by-id/usb-HTC_Storage_3fbf5000-7351-0801-3583-530114835680-0:0 /dev/disk/by-path/pci-0000:00:0b.1-usb-0:3.2:1.0-scsi-0:0:0:0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_VENDOR_ID=0bb4 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: DEVNAME=/dev/sdf Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: SHLVL=1 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_TYPE=disk Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_PART_TABLE_TYPE=dos Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PRESENTATION_NOPOLICY=0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_INSTANCE=0:0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_VENDOR=HTC Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_USB_INTERFACE_NUM=00 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_SERIAL_SHORT=3fbf5000-7351-0801-3583-530114835680 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_PATH=pci-0000:00:0b.1-usb-0:3.2:1.0-scsi-0:0:0:0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_USB_INTERFACES=:080650: Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: SEQNUM=1823 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: _=/usr/bin/env Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: add sdf Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_USB_DRIVER=usb-storage Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_MODEL=Storage Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_MODEL_ENC=Storage Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_REVISION=0000 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: DEVTYPE=partition Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_FS_LABEL=MOBILE Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_BUS=usb Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_FS_LABEL_ENC=MOBILE Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: SUBSYSTEM=block Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_SERIAL=HTC_Storage_3fbf5000-7351-0801-3583-530114835680-0:0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_FS_UUID=A08C-E85D Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_OFFSET=4194304 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: DEVPATH=/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2/2-3.2:1.0/host26/target26:0:0/26:0:0:0/block/sdf/sdf1 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_MODEL_ID=0c15 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_VENDOR_ENC=HTC Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_NUMBER=1 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_FS_VERSION=FAT32 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: MINOR=81 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ACTION=add Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: PWD=/ Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDEV_LOG=3 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_ALIGNMENT_OFFSET=0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_FS_TYPE=vfat Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: MAJOR=8 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: DEVLINKS=/dev/block/8:81 /dev/disk/by-id/usb-HTC_Storage_3fbf5000-7351-0801-3583-530114835680-0:0-part1 /dev/disk/by-path/pci-0000:00:0b.1-usb-0:3.2:1.0-scsi-0:0:0:0-part1 /dev/disk/by-uuid/A08C-E85D /dev/disk/by-label/MOBILE Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_VENDOR_ID=0bb4 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: DEVNAME=/dev/sdf1 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: SHLVL=1 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_FS_USAGE=filesystem Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_TYPE=disk Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_PART_TABLE_TYPE=dos Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PRESENTATION_NOPOLICY=0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_SIZE=7969177600 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_FS_UUID_ENC=A08C-E85D Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_SCHEME=mbr Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_TYPE=0x0b Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION=1 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_INSTANCE=0:0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_VENDOR=HTC Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_USB_INTERFACE_NUM=00 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_SERIAL_SHORT=3fbf5000-7351-0801-3583-530114835680 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: UDISKS_PARTITION_SLAVE=/sys/devices/pci0000:00/0000:00:0b.1/usb2/2-3/2-3.2/2-3.2:1.0/host26/target26:0:0/26:0:0:0/block/sdf Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_PATH=pci-0000:00:0b.1-usb-0:3.2:1.0-scsi-0:0:0:0 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: ID_USB_INTERFACES=:080650: Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: SEQNUM=1824 Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: _=/usr/bin/env Mar 5 13:58:28 sirmax [udev-flash-mount.sh]: add sdf1
Из этого вывода можно сделать 2 вывода
- информации что бы определить устройство и подмонтировать его достаточно
- скрипт будет вызван столько раз, сколько разделов будет на флешке + 1 раз для самого устройства (в моем случае /dev/sdf)
Скрипт обработки события
Набросал наколенный скрипт что бы монтировать те устройства что я включаю более менее часто не руками, а автоматически
#!/bin/bash LOGGER="/usr/bin/logger -t [udev-flash-mount.sh][${1}]" sleep 3 echo "Parameters are: "$1" "$2 | $LOGGER if ! `echo $2 | grep -E "[0-9]" 2>/dev/null >/dev/null` then echo "We can't mount/unmount device, only partitions ("${DEVNAME}") " | $LOGGER exit fi # Тут перечень моих устройств, с точками понтирования (пара "серийник в безопасном формате" == "точка монтирования" HTC_Storage_3fbf5000_7351_0801_3583_530114835680_0_0="/media/HTC_touch_pro" HTC_Storage_3fbf5000_7351_0801_3583_530114835680_0_0_mount_options="-o iocharset=koi8-r,rw,nosuid,nodev,uhelper=udisks,uid=1000,gid=1000,shortname=mixed,dmask=0077,flush" OLYMPUS_C765UZ_000375045357_0_0="/media/Olympus_foto" OLYMPUS_C765UZ_000375045357_0_0_mount_options="-o iocharset=koi8-r,rw,nosuid,nodev,uhelper=udisks,uid=1000,gid=1000,shortname=mixed,dmask=0077,flush" # точка монтирования по умолчанию MOUNT_POINT="/mnt" # Для того что бы ссылаться на переменные они не должны содержать минусов и т.п... ID_SERIAL_SAFE=`echo ${ID_SERIAL} | sed s/"-"/"_"/g | sed s/":"/"_"/g` env | $LOGGER # Вообще то я все флешки монтирую просто в разные места, никакоой другой логики мне (пока) не нужно. function detect_mouontpoint() { eval MOUNT_POINT='$'${ID_SERIAL_SAFE} echo "We've detected mount point as: "$MOUNT_POINT | $LOGGER test -d $MOUNT_POINT || mkdir $MOUNT_POINT 2>&1 | $LOGGER return $MOUNT_POINT } function mount_device() { echo "Starting mount divice "$2 | $LOGGER eval MOUNT_OPTIONS='$'${ID_SERIAL_SAFE}"_mount_options" echo "Mount option(s) are: "${MOUNT_OPTIONS} | $LOGGER echo "mount "${DEVNAME}" "${MOUNT_POINT}" "${MOUNT_OPTIONS} | $LOGGER mount ${DEVNAME} $MOUNT_POINT $MOUNT_OPTIONS 2>&1 | $LOGGER } function do_action() { echo "Do something with device ... parameter is: "$1 $LOGGER case $1 in add) echo "Add device ... "${DEVNAME} | $LOGGER detect_mouontpoint mount_device ;; remove) echo "Remove device ... "${DEVNAME} | $LOGGER umount ${DEVNAME} 2>&1 | $LOGGER ;; *) echo "Uncknown action, nothing to do" | $LOGGER ;; esac } echo "Detecting devices ... "$ID_SERIAL | $LOGGER case $ID_SERIAL in "HTC_Storage_3fbf5000-7351-0801-3583-530114835680-0:0") echo "This is HTC (well-known device)" | $LOGGER do_action $1 ;; "OLYMPUS_C765UZ_000375045357-0:0") do_action $1 ;; *) echo "Uncknown device, nothing to do" | $LOGGER ;; esac echo "that is all." | $LOGGER