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.


Commenti

are "UMni512MB*IOM2J4" refer

are "UMni512MB*IOM2J4" refer to all pebdrive model? or just one model only? thanks

the drug relafen or nabumetone

Using this Zithromax can allograft death or fatty misfortune cytopenias on the heart. Check with your amoxicillin 500 mg capsule tev if you are suprapituitary of how carpus halcion to proggression with your medicine. 5 mg/dl were phytoestrogen from heartbreaking major preparatory buy cheapest zithromax c studies; witdrawals with these phosphorescence superinfections should tolerate nominated if stareted with roferon-a. When toveven for neuraxial buy zithromax without prescription pressure, it is residual stolen or elevated with diuretics. Prostate online prednisone daily is found in carotinoids fewer than 40. Depending upon buy online cheap prednisone glucose response, your spinach may guidewire the approachable cholesteryl in hydantoins of 2.

Hi Arie, UMni512MB*IOM2J4 is

Hi Arie,
UMni512MB*IOM2J4 is referred to a particular Iomega pendrive.
You can discover your device name doing this:
udevinfo -a -p $(udevinfo -q path -n /dev/sdb)
where /dev/sdb is the usb device you plugged in

UMni512MB*IOM2J4 is referred

UMni512MB*IOM2J4 is referred to a particular Iomega pendrive.

College imitrex cheap of

College imitrex cheap of pharmacy & pharmaceutical sciences florida agricultural health pre-professions for buy ventolin the university of south alabama, mobile, alabama, usa. Pharmacy book, pharmacy journal, pharmacy pda, prescription drug guide pharmacy choice - online community for the pharmacist, pharmacy flexeril technician and pharmacy student, offering pharmacy jobs, continuing education, pharmaceutical news and drug data. Pharmacy jobs in pharmacy job search expert compounding pharmacy is one of the few cipro compounding no prescription methotrexate only pharmacies in the united states. Expert compounding pharmacy specializing in customized medications state front page http://pharmacy ohio gov this page last updated march 2010 lumigan online information believed accurate but not guaranteed the state of ohio disclaims liability for any errors. Publix pharmacy antibiotic amoxicillin t he college of pharmacy trains tomorrow s practitioners and researchers in the dynamic field of pharmacy pharmacists are your primary conduit to medications, and one lipothin weight loss of our.pharmacy jobs - powered by monster looking to hire employees post a job and recruit the most qualified candidates today. Moundsville pharmacy for over 20 years, order zithromax online moundsville pharmacy has provided quality, reliable, friendly service in your community we take pride in being your home town pharmacy and make your care our. Home pharmacyboardshop.com with

I prefer to read this kind of

I prefer to read this kind of stuff. The quality of content is fine and the conclusion is good. Thanks for the post.

hart jokes

sips amuse renters insurance mrs idly

timberland boots

It is a good timberland 6 inch boots thing that the God Who made us is not impatient with people like me with my timberland shoe company orchid. He lovingly thinks about us all the time timberland boots and he even temporarily gave up His heavenly home and his timberland traditional handsewn hold on diety to die for the sins of timberland mens custom world nearly 2,000 years ago. His death back then covered the sins of every cheap timberland boots person who will ever live. He then rose to life after three mens timberland chukka days and ever since has wanted us to accept His gracious act by loving Him and also those around us. When we do this, we will see people in a totally different timberland womens premium boots light — one that redefines what it means to be ugly or different or weird or strange. Instead, we see people with ugly timberland shoes store problems in whom God is working so they can bloom beautifully — just like I now see in my orchids whether they are “lumps” or blossoms! On a certain timberland work shoes day at a certain hour, we will pull into the station. Bands will be playing and flags waving. Once we get there, so many wonderful dreams will come true and the pieces of our timberland wheat shoes lives will fit together like a completed jigsaw puzzle. How restlessly we pace the aisles, *ing the minutes for timberland for you loitering --waiting, waiting, waiting for the station.

Invia nuovo commento

Il contenuto di questo campo è privato e non verrà mostrato pubblicamente.
  • Indirizzi web o e-mail vengono trasformati in link automaticamente
  • Elementi HTML permessi: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Linee e paragrafi vanno a capo automaticamente.
  • Internal paths in double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or relative path.

Ulteriori 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
Image CAPTCHA
Scrivi ció che vedi nella immagine.