Previous Entry Share Next Entry
Autorun for USB drive
itjustme_tech
In file /etc/udev/rules.d/90-usb.rules


ACTION=="add", KERNEL=="sd[a-z]1", SUBSYSTEM=="block", ATTRS{idVendor}=="1058", ATTRS{idProduct}=="0748", ATTRS{serial}=="575839314134335237393134", RUN+="/usr/bin/at -f /root/backup/backup2ext now"
ACTION=="add", KERNEL=="sd[a-z]1", SUBSYSTEM=="block", ATTRS{idVendor}=="1058", ATTRS{idProduct}=="0748", ATTRS{serial}=="575839314134334437353938", RUN+="/usr/bin/at -f /root/backup/backup2ext now"

#ACTION=="add", KERNEL=="sd[a-z]1", SUBSYSTEM=="block", ATTRS{idVendor}=="1058", ATTRS{idProduct}=="0748", ATTRS{serial}=="575839314134335237393134", RUN+="/root/1"
#ACTION=="add", KERNEL=="sd[a-z]1", SUBSYSTEM=="block", ATTRS{idVendor}=="1058", ATTRS{idProduct}=="0748", ATTRS{serial}=="575839314134334437353938", RUN+="/root/1"


As you see there can be different script for different devices.

Listing of all attributes of connected device (/dev/sdb ie):

[root@taora rules.d]# udevadm info --query all --name=/dev/sdb --attribute-walk

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:1d.7/usb1/1-6/1-6:1.0/host4/target4:0:0/4:0:0:0/block/sdb':
    KERNEL=="sdb"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{range}=="16"
    ATTR{ext_range}=="256"
    ATTR{removable}=="0"
    ATTR{ro}=="0"
    ATTR{size}=="3906963456"
    ATTR{alignment_offset}=="0"
    ATTR{capability}=="52"
    ATTR{stat}=="  143974      169  1152994  1813453  4558505 122902868 1019691896 2980294762        0 21791609 2982107006"
    ATTR{inflight}=="       0        0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host4/target4:0:0/4:0:0:0':
    KERNELS=="4:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{device_blocked}=="0"
    ATTRS{type}=="0"
    ATTRS{scsi_level}=="3"
    ATTRS{vendor}=="WD      "
    ATTRS{model}=="My Passport 0748"
    ATTRS{rev}=="1022"
    ATTRS{state}=="running"
    ATTRS{timeout}=="30"
    ATTRS{iocounterbits}=="32"
    ATTRS{iorequest_cnt}=="0x47c147"
    ATTRS{iodone_cnt}=="0x47c147"
    ATTRS{ioerr_cnt}=="0x0"
    ATTRS{modalias}=="scsi:t-0x00"
    ATTRS{evt_media_change}=="0"
    ATTRS{dh_state}=="detached"
    ATTRS{queue_depth}=="1"
    ATTRS{queue_type}=="none"
    ATTRS{max_sectors}=="240"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host4/target4:0:0':
    KERNELS=="target4:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host4':
    KERNELS=="host4"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0':
    KERNELS=="1-6: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:v1058p0748d1022dc00dsc00dp00ic08isc06ip50"
    ATTRS{supports_autosuspend}=="0"
    ATTRS{interface}=="MSC Bulk-Only Transport"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-6':
    KERNELS=="1-6"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}=="USB Mass Storage"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bmAttributes}=="80"
    ATTRS{bMaxPower}=="500mA"
    ATTRS{urbnum}=="134816484"
    ATTRS{idVendor}=="1058"
    ATTRS{idProduct}=="0748"
    ATTRS{bcdDevice}=="1022"
    ATTRS{bDeviceClass}=="00"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{speed}=="480"
    ATTRS{busnum}=="1"
    ATTRS{devnum}=="5"
    ATTRS{version}==" 2.10"
    ATTRS{maxchild}=="0"
    ATTRS{quirks}=="0x0"
    ATTRS{authorized}=="1"
    ATTRS{manufacturer}=="Western Digital"
    ATTRS{product}=="My Passport 0748"
    ATTRS{serial}=="575839314134335237393134"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1':
    KERNELS=="usb1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}==""
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bMaxPower}=="  0mA"
    ATTRS{urbnum}=="120"
    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}=="1"
    ATTRS{devnum}=="1"
    ATTRS{version}==" 2.00"
    ATTRS{maxchild}=="8"
    ATTRS{quirks}=="0x0"
    ATTRS{authorized}=="1"
    ATTRS{manufacturer}=="Linux 2.6.32-300.3.1.el6uek.x86_64 ehci_hcd"
    ATTRS{product}=="EHCI Host Controller"
    ATTRS{serial}=="0000:00:1d.7"
    ATTRS{authorized_default}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:1d.7':
    KERNELS=="0000:00:1d.7"
    SUBSYSTEMS=="pci"
    DRIVERS=="ehci_hcd"
    ATTRS{vendor}=="0x8086"
    ATTRS{device}=="0x3a3a"
    ATTRS{subsystem_vendor}=="0x103c"
    ATTRS{subsystem_device}=="0x330d"
    ATTRS{class}=="0x0c0320"
    ATTRS{irq}=="20"
    ATTRS{local_cpus}=="00000000,00000000,00000000,00000000,00000000,00000000,00000000,0000ffff"
    ATTRS{local_cpulist}=="0-15"
    ATTRS{modalias}=="pci:v00008086d00003A3Asv0000103Csd0000330Dbc0Csc03i20"
    ATTRS{numa_node}=="-1"
    ATTRS{enable}=="1"
    ATTRS{broken_parity_status}=="0"
    ATTRS{msi_bus}==""
    ATTRS{msi_irqs}==""
    ATTRS{companion}==""

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

[root@taora rules.d]#


I am not sure that all of this attrs can be used in 90-usb.rules but attrs mentioned in example above working well.

In script you have some environment variables. For test purposes you can simple output it to file:

#!/bin/sh

fn=/root/`date | sed "s/ /_/g" | sed "s/:/_/g"`

echo $* > $fn
echo _______________________ >> $fn
set >> $fn


The full example ouput:

_______________________
ACTION=add
BASH=/bin/sh
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="/root/1")
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="x86_64-unknown-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
DEVLINKS='/dev/block/8:17 /dev/disk/by-id/usb-WD_My_Passport_0748_575839314134335237393134-0:0-part1 /dev/disk/by-path/pci-0000:00:1d.7-usb-0:6:1.0-scsi-0:0:0:0-part1 /dev/disk/by-uuid/869edf34-c196-4b8e-a9bb-8a95331f8784'
DEVNAME=/dev/sdb1
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host5/target5:0:0/5:0:0:0/block/sdb/sdb1
DEVTYPE=partition
DIRSTACK=()
EUID=0
GROUPS=()
HOSTNAME=taora.pakatz
HOSTTYPE=x86_64
ID_BUS=usb
ID_FS_SEC_TYPE=ext2
ID_FS_TYPE=ext3
ID_FS_USAGE=filesystem
ID_FS_UUID=869edf34-c196-4b8e-a9bb-8a95331f8784
ID_FS_UUID_ENC=869edf34-c196-4b8e-a9bb-8a95331f8784
ID_FS_VERSION=1.0
ID_INSTANCE=0:0
ID_MODEL=My_Passport_0748
ID_MODEL_ENC='My\x20Passport\x200748'
ID_MODEL_ID=0748
ID_PART_TABLE_TYPE=dos
ID_PATH=pci-0000:00:1d.7-usb-0:6:1.0-scsi-0:0:0:0
ID_REVISION=1022
ID_SERIAL=WD_My_Passport_0748_575839314134335237393134-0:0
ID_SERIAL_SHORT=575839314134335237393134
ID_TYPE=disk
ID_USB_DRIVER=usb-storage
ID_USB_INTERFACES=:080650:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=WD
ID_VENDOR_ENC='WD\x20\x20\x20\x20\x20\x20'
ID_VENDOR_ID=1058
IFS='
'
MACHTYPE=x86_64-unknown-linux-gnu
MAJOR=8
MINOR=17
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/bin:/bin:/usr/bin
PIPESTATUS=([0]="0")
POSIXLY_CORRECT=y
PPID=12122
PS4='+ '
PWD=/
SEQNUM=2482
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=1
SUBSYSTEM=block
TERM=dumb
UDEV_LOG=3
UDISKS_PARTITION=1
UDISKS_PARTITION_ALIGNMENT_OFFSET=0
UDISKS_PARTITION_NUMBER=1
UDISKS_PARTITION_OFFSET=1048576
UDISKS_PARTITION_SCHEME=mbr
UDISKS_PARTITION_SIZE=2000364240896
UDISKS_PARTITION_SLAVE=/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host5/target5:0:0/5:0:0:0/block/sdb
UDISKS_PARTITION_TYPE=0x07
UDISKS_PRESENTATION_NOPOLICY=0
UID=0
_=_______________________
fn=/root/Mon_Oct__7_20_17_42_XXX_2013

?

Log in