<<
Udev Rules
Udev rules can be used to achieve static, consistent mount points for removable drives (e.g. USB pen drive) in Linux. This is especially useful when combined with applications such as Amarok, to achieve consistent handling of Media Devices.
Note All commands should be run in a terminal.
Update later versions of udev may use
ATTRS instead of
SYSFS and
DRIVERS instead of
BUS
Step One
Gather information about the drive to be mounted.
Plug in your removable drive.
run
dmesg, and examine the output to determine what
/dev point your new drive has been assigned. The output should look something like:
SCSI device sda: 2013184 512-byte hdwr sectors (1031 MB)
sda: Write Protect is off
sda: Mode Sense: 23 00 00 00
sda: assuming drive cache: write through
sda: sda1
sd 13:0:0:0: Attached scsi removable disk sda
In this case the device has been assigned
/dev/sda1
Run the following command to get the udev information, replacing
/dev/sda1 with the above device point:
udevinfo -a -p $(udevinfo -q path -n /dev/sda1)
You will see output similar to this:
looking at device '/block/sda/sda1':
KERNEL=="sda1"
SUBSYSTEM=="block"
SYSFS{stat}==" 129 339 0 0"
SYSFS{size}=="2013152"
SYSFS{start}=="32"
SYSFS{dev}=="8:1"
looking at parent device '/block/sda':
ID=="sda"
BUS=="block"
DRIVER==""
SYSFS{stat}==" 65 73 411 339 0 0 0 0 0 329 339"
SYSFS{size}=="2013184"
SYSFS{removable}=="1"
SYSFS{range}=="16"
SYSFS{dev}=="8:0"
looking at parent device '/devices/pci0000:00/0000:00:10.4/usb5/5-3/5-3:1.0/host13/target13:0:0/13:0:0:0':
ID=="13:0:0:0"
BUS=="scsi"
DRIVER=="sd"
SYSFS{ioerr_cnt}=="0x2"
SYSFS{iodone_cnt}=="0x9d"
SYSFS{iorequest_cnt}=="0x9d"
SYSFS{iocounterbits}=="32"
SYSFS{timeout}=="60"
SYSFS{state}=="running"
SYSFS{rev}=="5.00"
SYSFS{model}=="STORE N GO "
SYSFS{vendor}=="Verbatim"
SYSFS{scsi_level}=="3"
SYSFS{type}=="0"
SYSFS{queue_type}=="none"
SYSFS{queue_depth}=="1"
SYSFS{device_blocked}=="0"
SYSFS{max_sectors}=="240"
Step Two
Edit Udev rules for target device
In the above udev output, you can see 3 distinct blocks of information. In order to uniquely identify your device you will need to choose a subset of the lines beginning
SYSFS{ from a SINGLE block and optionally a SINGLE parent.
In this case, some good lines to choose would be the following from the third block:
BUS=="scsi"
SYSFS{model}=="STORE N GO "
SYSFS{vendor}=="Verbatim"
As root, add a new entry to the file
/etc/udev/rules.d/10-local.rules using the lines chosen above (this file may need to be created)
BUS=="scsi", SYSFS{model}=="STORE N GO ", SYSFS{vendor}=="Verbatim", SYMLINK+="usb_verbatim"
Note the "==" for the matches - this is important
The above command will create a symlink
/dev/usb_verbatim when you attach the removable device. Alternatively, instead of
SYMLINK+="usb_verbatim" you can use
NAME="usb_verb" which would cause the device to be created as
/dev/usb_verb instead of symlinked.
There are many more options available for udev, including auto-running scripts at mount and unmount. Please see the links below for more information. As a quick taster, adding
ACTION=="add/remove" will cause the rule to be applied at the appropriate point. To run another script, add
RUN+="/my/script.sh"
Step Three
Create mount point
First, make the directory where you want the new device to be mounted, e.g.
/mnt/usb/verbatim
Now, as root edit
/etc/fstab to create the mount rule. You will need to add a line similar to:
/dev/usb_verbatim /mnt/usb/verbatim auto rw,user,noauto 0 0
This line should cause your device to be mounted at the target location when plugged int. Test by unplugging the device and re-attaching it.
Further information
... can be found at:
fstab
udev rules