Usbkey udev and autofs rules

From Devpit

Jump to: navigation, search

Disclaimer: This udev and autofs example was created for a CENTON usbkey on the Debian Gnu/Linux operating system.

Contents

Gathering usbkey information using udevinfo

The udevinfo application can help you gather sysfs information on your own usbkey. This will help you create your own rule:

«user@host»:/§ udevinfo -a -p /sys/block/sda
looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/host2/target2:0:0/2:0:0:0' :
   BUS="scsi"
   ID="2:0:0:0"
   DRIVER="sd"
   SYSFS{device_blocked}="0"
   SYSFS{iocounterbits}="32"
   SYSFS{iodone_cnt}="0xb"
   SYSFS{ioerr_cnt}="0x1"
   SYSFS{iorequest_cnt}="0xb"
   SYSFS{max_sectors}="240"
   SYSFS{model}="PDU13_512 59G2.0"
   SYSFS{queue_depth}="1"
   SYSFS{queue_type}="none"
   SYSFS{rev}="0.00"
   SYSFS{scsi_level}="3"
   SYSFS{state}="running"
   SYSFS{timeout}="30"
   SYSFS{type}="0"
   SYSFS{vendor}="CENTON  "

Add a user group

Create a user group for providing access permissions to usbkeys and add the users to be given access to the group. You may need to log that user out of all active sessions for the group add to take effect.

«root@host»:/§  addgroup usbkey
«root@host»:/§  adduser usbkey user

Create a udev rule for the device

In /etc/udev/udev.rules add the following to the "# SCSI devices" section:

«root@host»:/§  vim /etc/udev/udev.rules

# SCSI devices

BUS="scsi", SYSFS{model}="PDU13_512 59G2.0", KERNEL="sd*", NAME="%k", SYMLINK="usbkey", GROUP="usbkey", MODE="0660"

The GROUP and MODE keys indicate which group and user permissions the /dev/sd* device is set to.

NOTE: I have tested the rule using SYSFS{vendor}="CENTON" instead of SYSFS{model}="PDU13_512 59G2.0" and it worked fine. I figured that the model number was unique enough but for a finer grained udev rule you could include both the vendor and model rules and the device would have to match both fields for the udev rule to apply.

Testing your udev rule

You can now test your udev rule by plugging in your usbkey. Execute the udevtest program in the following manner to determine whether udev will apply your rule to your usbkey device. A successful rule-to-device mapping is demonstrated below:

«user@dhost»:/sys/block§ udevtest /sys/block/sda
version 056
looking at '/block/sda'
opened class_dev->name='sda'
configured rule in '/etc/udev/rules.d/udev.rules[22]' applied, added symlink 'usbkey'
configured rule in '/etc/udev/rules.d/udev.rules[22]' applied, 'sda' becomes '%k'
creating device node '/dev/sda', major = '8', minor = '0', mode = '0660', uid = '0', gid = '1003'

Tell udev to pick up the new rules

Following the addition of a udev rule you must reload the configuration file as the root user:

«root@host»:/§  udevstart

Verify that udev is creating the /dev/usbkey symlink

When the usbkey is plugged in the udev rule should direct the kernel to create the following symlink:

«user@host»:~§ ls -la /dev | grep usbkey
brw-rw----   1 root usbkey     8,   0 2006-01-03 10:43 sda
brw-rw----   1 root usbkey     8,   1 2006-01-03 10:43 sda1
lrwxrwxrwx   1 root root            4 2006-01-03 10:43 usbkey -> sda1

Notice that the symlink was not created with the same permissions as the actual device. I'm not sure why. Really, setting permissions on the device node are not necessary because later autofs mounting will create the mount point with the proper group access permissions.

Install autofs

Make sure that autofs is installed on your system. The master autofs file /etc/auto.master should be available on your system if autofs has been installed.

Create the autofs removable devices class

Open the file /etc/auto.master and add an entry for removable devices if the entry doesn't exist. Add the following entry:

«root@host»:/§  vim /etc/auto.master

/var/autofs/removable    /etc/auto.removable --timeout=2

This directs autofs that removable devices should be mounted in /var/autofs/removable and the rules for such devices exists in /etc/auto.removable. The --timeout=2 option tells autofs that two seconds after the last use count is held on the mounted directory it should umount a removable device.

Add a usbkey entry to the removable autofs devices file

Create the file /etc/auto.removable if it doesn't already exist and add the following entry:

«root@host»:/§  vim /etc/auto.removable

usbkey  -fstype=vfat,rw,user,gid=usbkey,umask=007    :/dev/usbkey

This configuration line directs autofs to create the mount point /var/autofs/removable/usbkey against the device /dev/usbkey.

The middle group of options are simply mounting options as you'd see in /etc/fstab which tell autofs the group ownership and access permissions with which to create the /var/autofs/removable/usbkey mount point.

Create the /mnt/usbkey symlink mount point

Create the ubiquitous mount point /mnt/usbkey and set the owner permissions correctly to the usbkey group:

«root@host»:/§  sudo ln -s /var/autofs/removable/usbkey /mnt/usbkey
chown root.usbkey /mnt/usbkey
«user@host»:/mnt§ ls -la /mnt/ | grep usbkey
lrwxrwxrwx   1 root usbkey   29 2005-12-25 21:25 usbkey -> /var/autofs/removable/usbkey/

When the usbkey isn't plugged in you'll notice that /var/autofs/removable/usbkey doesn't exist. This is ok. The target mount point will appear when the /mnt/usbkey mount point is touched.

Restart autofs to pick up the new rule

Restart autofs so that it picks up the new rules using the following command:

«root@host»:/§  /etc/init.d/autofs restart

Test automounting

Plug in your usbkey if it isn't already plugged in. Direct a file manager to open /mnt/usbkey. Doing so will cause autofs to create the /var/autofs/removable/usbkey mount point with the proper usbkey group permissions as indicated below:

«user@host»:/§ ls -la /var/autofs/removable | grep usbkey
drwxrwx---  2 root usbkey 16384 1969-12-31 18:00 usbkey

When you close all open file manager instances to the /mnt/usbkey directory autofs will umount the device after 2 seconds.

What I do is create desktop shortcuts to the /mnt/usbkey mount point and add a usbkey icon as the picture.

The benefit of using the autofs auto mounting method over other automounting methods is that when you're done with the usbkey you simply close your file browsers and yank the usbkey.

Personal tools
sponsored projects
Google AdSense