Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old April 30th, 2006   #1
Sutekh
Chocolate Ubuntu Mocha Blend
 
Join Date: Oct 2005
Location: Sydney
Beans: 1,983
Xubuntu 6.06
Create your own udev rules to control removable devices

I'm sure many people who use removable devices have noticed that sometimes they don't appear where they were before.

You plug your USB drive in, and use fdisk to find the device node and then mount it. Sometimes the USB appears as /dev/sda1, sometimes /dev/sdb1. It can depend on what order you plug in your USB devices, and where you plug them in. This is a real pain if you mount devices manually or if you are trying to customise your /etc/fstab.

udev allows the assignment of a persistant device node, /dev/..., based on a rule match defined by your specific hardware. In other words, if a device is attached that matches certain criteria it is given it's own device node, rather than being assigned a dynamic one.

It's actually really easy to setup.

______

To start with you need to know the dynamic device node that is given to a device when attached for the first time. The way that I would do this is to use the tail command
Code:
tail -f /var/log/messages
When a device is plugged in, the screen will update with the newest messages, usually imparting some knowledge on the location of the new device. My USB flash disc for example, produced this output when plugged in.
Quote:
Apr 30 16:37:01 localhost kernel: [4294885.683000] usb 1-3: new full speed USB device using ohci_hcd and address 6
Apr 30 16:37:01 localhost kernel: [4294885.853000] scsi5 : SCSI emulation for USB Mass Storage devices
Apr 30 16:37:02 localhost usb.agent[10421]: usb-storage: already loaded
Apr 30 16:37:06 localhost kernel: [4294890.859000] Vendor: Model: TS128MJFLASHA Rev: 1.00
Apr 30 16:37:06 localhost kernel: [4294890.859000] Type: Direct-Access ANSI SCSI revision: 02
Apr 30 16:37:06 localhost kernel: [4294890.883000] SCSI device sdd: 253400 512-byte hdwr sectors (130 MB)
Apr 30 16:37:06 localhost kernel: [4294890.896000] sdd: Write Protect is off
Apr 30 16:37:06 localhost kernel: [4294890.924000] SCSI device sdd: 253400 512-byte hdwr sectors (130 MB)
Apr 30 16:37:06 localhost kernel: [4294890.937000] sdd: Write Protect is off
Apr 30 16:37:07 localhost kernel: [4294890.937000] /dev/scsi/host5/bus0/target0/lun0: p1
Apr 30 16:37:07 localhost kernel: [4294891.046000] Attached scsi removable disk sdd at scsi5, channel 0, id 0, lun 0
Apr 30 16:37:07 localhost scsi.agent[10469]: sd_mod: loaded sucessfully (for disk)
The output shows that the USB device attached is assigned the device node /dev/sdd. You may also want to determine the partition number using
Code:
sudo fdisk -l
For my USB flash disc, the partition is /dev/sdd1.

So next thing is to find out some unique information from the device, information that will be used in defining the udev rule, remembering a match is required to assign the persistant node. The next command I have used is from the Writing udev rules link at the bottom of this HOWTO
Code:
udevinfo -a -p $(udevinfo -q path -n /dev/sdd)
This command outputs a lot information about the hardware associated with /dev/sdd. I've left out a lot of the information, leaving only the section I think is most relevant. This section contains the more specific information about my USB flash disc.

Note the bolded text in the output. It is important that information used in a udev rule is contained in the one section.
Code:
udevinfo starts with the device the node belongs to and then walks up the
device chain, to print for every device found, all possibly useful attributes
in the udev key format.
Only attributes within one device section may be used together in one rule,
to match the device for which the node will be created.
  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.0/usb1/1-3':
    BUS=="usb"
    ID=="1-3"
    DRIVER=="usb"
    SYSFS{bConfigurationValue}=="1"
    SYSFS{bDeviceClass}=="00"
    SYSFS{bDeviceProtocol}=="00"
    SYSFS{bDeviceSubClass}=="00"
    SYSFS{bMaxPower}=="100mA"
    SYSFS{bNumConfigurations}=="1"
    SYSFS{bNumInterfaces}==" 1"
    SYSFS{bcdDevice}=="0100"
    SYSFS{bmAttributes}=="80"
    SYSFS{configuration}==""
    SYSFS{devnum}=="6"
    SYSFS{idProduct}=="0005"
    SYSFS{idVendor}=="0c76"
    SYSFS{maxchild}=="0"
    SYSFS{product}=="TS128MJFLASHA"
    SYSFS{speed}=="12"
    SYSFS{version}==" 1.10"
The items that contain specific information regarding my USB disc have been coloured red. These are the two items of interest to me, the device is connected to the USB bus, and the product is identified by TS128MJFLASHA

______

The next step is to create the udev rule concerning this device. I'll start by creating my own .rules file
Code:
sudo nano /etc/udev/rules.d/10-local.rules
By naming my set of rules 10-local.rules they should be looked at before the other rules set in this folder.

The rule I will use for the flash disc looks like this.
Quote:
BUS=="usb", SYSFS{product}=="TS128MJFLASHA", KERNEL=="sd?1", NAME="transcend128mb", SYMLINK="usbdevices/transcend128mb"
A quick explanation.

- The BUS==”usb” and SYSFS{product}==”TS128MJFLASHA” options are the same as those I picked out from the udevinfo output.

- The option KERNEL="sd?1" will only match locations like /dev/sda1, /dev/sdb1 and more importantly, it won't match nodes like /dev/sda, /dev/sdb, which can be fdisk'ed. The 'Writing udev rules' guide also mentions this.

- The options NAME="128FLASH" and SYMLINK="usbdisc/128FLASH" will create the persistant node at /dev/transcend128mb and a symlink /dev/usbdisc/transcend128mb that points to the persistant node /dev/transcend128mb. The SYMLINK option is not required. The reason I have included it is so that all my USB devices will have a symlink starting with /dev/usbdevices/...
I just think its neater.

There are other options that could be used to create udev rules, such as GROUP=”some_group”, if you want to assigned the group ownership of the device node to a specific group, and MODE=”0660”, which would give the owner/group read and write permissions, like chmod.

The Writing udev rules guide contains some more detailed information on these other options.

______

To start using these new rules, you need to run the command udevstart
Code:
sudo udevstart
You can also restart udev using
Code:
sudo /etc/init.d/udev restart
This should work for later versions of udev, that don't appear to use the udevstart command

- thanks to ash211 for pointing this out

Now to quickly check that the new node for my example has been created.
Code:
user@ubuntu:~$ ls -l /dev/trans*
brw-r-----  1 root plugdev 8, 49 2006-04-30 16:37 /dev/transcend128mb
user@ubuntu:~$ ls -l /dev/usbdevices/
lrwxrwxrwx  1 root root 17 2006-04-30 16:37 transcend128mb -> ../transcend128mb
______

Finally the fstab can be edited to include the new persistant device node, but first we'll back it up
Code:
sudo cp /etc/fstab /etc/fstab_backup
sudo nano /etc/fstab
Then we can add an entry for the example USB device using either the device node or the symlink (if used), so in my example I could either use the new device node
Code:
/dev/transcend128mb  /media/usb128mb  vfat  iocharset=utf8,umask=000   0   0
or the symlink to the node, which I prefer as all my USB devices have symlinks in /dev/usbdevices. I think makes the fstab look neater.
Code:
/dev/usbdevices/transcend128mb  /media/usb128mb vfat iocharset=utf8,umask=000  0  0
When the entry has been correctly entered, you can save the file (Ctrl+O) and exit (Ctrl+X), and then mount the device, in this example my USB disc using
Code:
sudo mount /media/usb128mb
or
Code:
sudo mount -a
Once this is all completed, the example USB drive will always appear at /dev/transcend128mb so the entry in the fstab can remain unchanged and will always find the device.


And you're all done!


Hope that helps some people, like it did me.

Please let me know if this works for you, and of course if there are any typos, errors or things that need clarifying.



The useful links I needed to get this working
Kernel.org - udev

Writing udev rules

Last edited by Sutekh; May 27th, 2006 at 09:02 AM..
Sutekh is offline   Reply With Quote
Old May 5th, 2006   #2
Sutekh
Chocolate Ubuntu Mocha Blend
 
Join Date: Oct 2005
Location: Sydney
Beans: 1,983
Xubuntu 6.06
Re: Create your own udev rules to control removable devices

I feel so cheap for doing this, but that was a lot of work. I would love to know if anyone has tried this, and how it went.
Sutekh is offline   Reply With Quote
Old May 14th, 2006   #3
DrBair
Just Give Me the Beans!
 
Join Date: Nov 2005
Beans: 62
Re: Create your own udev rules to control removable devices

I just used the guide to set up a custom node/mount point for my flash card reader. Its been a long time since I've played with udev and I needed the help. Thanks!

I think theres just not a lot of people out there that need this or are willing to dabble with config files. But I'm sure plenty of people (like me) will get good use out of this guide.
DrBair is offline   Reply With Quote
Old May 15th, 2006   #4
Sutekh
Chocolate Ubuntu Mocha Blend
 
Join Date: Oct 2005
Location: Sydney
Beans: 1,983
Xubuntu 6.06
Re: Create your own udev rules to control removable devices

Thanks for the feedback.

I actually wrote this after a thread where several people were encountering the same issue with USB devices. It does look like hard work, but it's mostly documenting the output from commands. There's only really a handful of commands that you have to do.

Glad it helped you out.
Sutekh is offline   Reply With Quote
Old May 22nd, 2006   #5
woot
Just Give Me the Beans!
 
Join Date: Apr 2006
Location: Belgium
Beans: 49
Ubuntu 6.06
Re: Create your own udev rules to control removable devices

*edit* rebooting helped solving my problem

Last edited by woot; May 22nd, 2006 at 07:38 AM..
woot is offline   Reply With Quote
Old May 22nd, 2006   #6
Ocxic
Iced Almond Soy Ubuntu, No Foam
 
Ocxic's Avatar
 
Join Date: Oct 2005
Location: Southern Ontario, Canada
Beans: 1,183
Ubuntu 8.04 Hardy Heron
Send a message via MSN to Ocxic
Re: Create your own udev rules to control removable devices

this is a great guide my usb audio keeps getting shuffled this should help me get it under control, thanks.
__________________
There is an easy way, and a hard way to do things.
If you're anything like me, you've tried to do things things the hard way,

and broke something.......

(\ /)
(O.o)
(> <)
Ocxic is offline   Reply With Quote
Old May 22nd, 2006   #7
DROP
5 Cups of Ubuntu
 
Join Date: May 2006
Beans: 25
Re: Create your own udev rules to control removable devices

I am going to try this when I get home. It looks very useful.
DROP is offline   Reply With Quote
Old May 23rd, 2006   #8
DROP
5 Cups of Ubuntu
 
Join Date: May 2006
Beans: 25
Re: Create your own udev rules to control removable devices

Thanks - this is pretty cool.
DROP is offline   Reply With Quote
Old May 23rd, 2006   #9
EReckase
Just Give Me the Beans!
 
EReckase's Avatar
 
Join Date: Apr 2006
Beans: 81
Ubuntu 10.04 Lucid Lynx
Question Re: Create your own udev rules to control removable devices

I have a 512 MB Intelligent Stick USB drive that I want to use for my GnuPG keys. I want the USB drive to automatically be mounted at a known location every time it's plugged in, so that the GnuPG paths can stay fixed regardless of whether or not I plug in another device. This HOWTO worked great, except for one little detail...how can I make the USB drive automount now that I've set up a mount point for it? When I remove the drive, re-inserting it does not automount it like it did before I made the changes above. I'll happily install autofs if need be, but I could use some pointers (/me is a n00b).

The one difference I had from your tutorial is that my external drive is formatted ext3 rather than vfat, so my mount line in fstab is
Code:
/dev/usbdevices/usb_gnupg  /media/usb_gnupg ext3 defaults,errors=remount-ro  0  0
Regards, and thanks for the tutorial.
Is that correct?
EReckase is offline   Reply With Quote
Old May 24th, 2006   #10
Sutekh
Chocolate Ubuntu Mocha Blend
 
Join Date: Oct 2005
Location: Sydney
Beans: 1,983
Xubuntu 6.06
Re: Create your own udev rules to control removable devices

Quote:
Originally Posted by woot
*edit* rebooting helped solving my problem
The version of udev in Dapper doesn't seem to have or use udevstart. I ended up re-booting to affect the changes when I tried this in Dapper.

I'm sure there must be another way though.

Quote:
Originally Posted by Ocxic
this is a great guide my usb audio keeps getting shuffled this should help me get it under control, thanks.
Well make sure you let me know if this works for your audio devices, and if you wouldn't mind post your rules here too.


I think udev rules might also be useful for people with multiple network cards (such as me!) and who find that they get swapped after updates (). This is something I wan't to give a try soon.

Quote:
Originally Posted by DROP
Thanks - this is pretty cool.
I'm glad it helped you out. Feel free to post your udev rules if you think they had something extra to them. The ones I used here were pretty plain.
Sutekh is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:54 PM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry