Udev - action add - remove

How to modify linux attitude when plugging and unplugging some devices to run some scripts?
For example, it could be nice if plugging a particular usb drive starts a backup script. Let's see.

Before reading this post should be useful to read this one: udev - hal - dbus: how does linux recognize devices?

First we need to discover informations about the hardware to identify it univocally:

# tail -f /var/log/syslog 

Plugging the device we'll see in the log file the device name assigned by the kernel (I'll use my Iomega usb pendrive):

Apr 1 19:35:03 radioshocked kernel: [17195000.748000] usb 5-1.1: new high speed USB device using ehci_hcd and address 8 
Apr 1 19:35:03 radioshocked kernel: [17195000.840000] usb 5-1.1: configuration #1 chosen from 1 choice
Apr 1 19:35:03 radioshocked kernel: [17195000.840000] scsi2 : SCSI emulation for USB Mass Storage devices 
Apr 1 19:35:03 radioshocked kernel: [17195000.840000] usb-storage: device found at 8 
Apr 1 19:35:03 radioshocked kernel: [17195000.840000] usb-storage: waiting for device to settle before scanning 
Apr 1 19:35:08 radioshocked kernel: [17195005.840000] usb-storage: device scan complete 
Apr 1 19:35:08 radioshocked kernel: [17195005.840000] Vendor: I0MEGA Model: UMni512MB*IOM2J4 Rev: 1.00
Apr 1 19:35:08 radioshocked kernel: [17195005.840000] Type: Direct-Access ANSI SCSI revision: 02 
Apr 1 19:35:08 radioshocked kernel: [17195005.840000] SCSI device sdb: 1015808 512-byte hdwr sectors (520 MB) 
Apr 1 19:35:08 radioshocked kernel: [17195005.844000] sdb: Write Protect is offApr 1 19:35:08 radioshocked kernel: [17195005.844000] sdb: Mode Sense: 03 00 00 00 
Apr 1 19:35:08 radioshocked kernel: [17195005.844000] sdb: assuming drive cache: write through 
Apr 1 19:35:08 radioshocked kernel: [17195005.848000] SCSI device sdb: 1015808 512-byte hdwr sectors (520 MB) 
Apr 1 19:35:08 radioshocked kernel: [17195005.848000] sdb: Write Protect is off 
Apr 1 19:35:08 radioshocked kernel: [17195005.848000] sdb: Mode Sense: 03 00 00 00 
Apr 1 19:35:08 radioshocked kernel: [17195005.848000] sdb: assuming drive cache: write through
Apr 1 19:35:08 radioshocked kernel: [17195005.848000] sdb: sdb1 sdb2 sdb3 
Apr 1 19:35:08 radioshocked kernel: [17195005.852000] sd 2:0:0:0: Attached scsi removable disk sdb 
Apr 1 19:35:08 radioshocked kernel: [17195005.852000] sd 2:0:0:0: Attached scsi generic sg1 type 0 

In this case the device name is /dev/sdb Knowing the device name we can retrieve a lot of infos doing:

udevinfo -a -p $(udevinfo -q path -n /dev/sdb)

we get:

  looking at device '/block/sdb':
    KERNEL=="sdb"
    SUBSYSTEM=="block"
    SYSFS{stat}=="      62       75     1096      128        0        0        0        0        0       88      128"
    SYSFS{size}=="1015808"
    SYSFS{removable}=="1"
    SYSFS{range}=="16"
    SYSFS{dev}=="8:16"

  looking at device '/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1.1/5-1.1:1.0/host1/target1:0:0/1:0:0:0':
    ID=="1:0:0:0"
    BUS=="scsi"
    DRIVER=="sd"
    SYSFS{ioerr_cnt}=="0x1"
    SYSFS{iodone_cnt}=="0x53"
    SYSFS{iorequest_cnt}=="0x53"
    SYSFS{iocounterbits}=="32"
    SYSFS{timeout}=="30"
    SYSFS{state}=="running"
    SYSFS{rev}=="1.00"
    SYSFS{model}=="UMni512MB*IOM2J4"
    SYSFS{vendor}=="I0MEGA  "
    SYSFS{scsi_level}=="3"
    SYSFS{type}=="0"
    SYSFS{queue_type}=="none"
    SYSFS{queue_depth}=="1"
    SYSFS{device_blocked}=="0"
    SYSFS{max_sectors}=="240"

and much more lines ...
These are keys and values related to the connected hardware.

SYSFS{model}=="UMni512MB*IOM2J4" it's a good way to indicate univocally the pendrive in udev rules.

Now we can create some rules for udev. Here there is a good guide.
A possible rules-file should be this one:

vim /etc/udev/rules/10-dam.rules
BUS=="scsi", SYSFS{model}=="UMni512MB*IOM2J4", KERNEL=="sd?1", NAME="pendrive", OWNER="damko"
SUBSYSTEM=="block", ACTION=="add", RUN+="/etc/udev/dev.d/script/mount.dev"
SUBSYSTEM=="block", ACTION=="remove", RUN+="/etc/udev/dev.d/script/umount.dev"

the file is named 10-dam.rules but we can change it as we like but leaving the number in front. Lower is the number earlier is executed. What does the script mean?

-- line 1 --
BUS=="scsi" =>  usb pendrives are recognized as scsi disks
SYSFS{model}=="UMni512MB*IOM2J4" => it's a way to refer to THAT particular pendrive
KERNEL=="sd?1" =>                   i'm refering to the first partition found in the connected device
NAME="pendrive" =>                  setting up udev to create the device /dev/pendrive for the first partition of the connected device
OWNER="damko" =>                    /dev/pendrive is owned by user damko 
------------------------------------------------------------------------- 

-- line 2 --
SUBSYSTEM=="block", 
ACTION=="add", 
RUN+="/etc/udev/dev.d/script/mount.dev" => when the device is plugged run the script 
/etc/udev/dev.d/script/mount.dev
------------------------------------------------------------------------- 


-- line 3 --
SUBSYSTEM=="block", 
ACTION=="remove", 
RUN+="/etc/udev/dev.d/script/umount.dev" => when the device is unplugged run the script /etc/udev/dev.d/script/umount.dev
------------------------------------------------------------------------- 

If your kernel does not have inotify support, new rules will not be detected automatically. In this situation, you must run

udevcontrol reload_rules 

after making any rule file modifications for those modifications to take effect.


Invia nuovo commento

Il contenuto di questo campo è privato e non verrà mostrato pubblicamente.
  • Indirizzi web o e-mail vengono trasformati in link automaticamente
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Linee e paragrafi vanno a capo automaticamente.

Maggiori informazioni sulle opzioni di formattazione.

CAPTCHA
Questa domanda serve per verificare che tu sia un visitatore umano (non un computer) e per prevenire lo spam
7 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.