# Autmounting a USB stick with udev and ivman

## Richard

Hi,

I am writing this because doing this gave me trouble, so I thought I would share my thoughts on how I got it to work. This is a worked example, using a no-name brand 256mb usb thumb drive. The same process should also work with other usb storage devices like cameras etc.

Before you start

I am using a udev system. If you are not, please consult the excellent documentation at:

http://www.gentoo.org/doc/en/udev-guide.xml

I edited my /etc/conf.d/rc file as follows:

```
RC_DEVICE_TARBALL="no"

```

I also assume that you have compiled the relevant options into your kernel. If not, try here:

http://gentoo-wiki.com/HOWTO_USB_Mass_Storage_Device

Creating the the udev rules

To create a udev rule for my usb thumb drive I needed some basic information about it. Most of this needs to be done as root, so you may as well su to root. I followed this process:

a) plugged in my thumb drive

b) ran dmesg and looked for the information about the usb device initialisation. It should be at the end

```
>dmesg

<snip>

Initializing USB Mass Storage driver...

scsi0 : SCSI emulation for USB Mass Storage devices

usbcore: registered new driver usb-storage

USB Mass Storage support registered.

usb-storage: device found at 2

usb-storage: waiting for device to settle before scanning

  Vendor: OTi       Model: Flash Disk        Rev: 2.00

  Type:   Direct-Access                      ANSI SCSI revision: 02

SCSI device sda: 512000 512-byte hdwr sectors (262 MB)

sda: Write Protect is off

sda: Mode Sense: 03 00 00 00

sda: assuming drive cache: write through

SCSI device sda: 512000 512-byte hdwr sectors (262 MB)

sda: Write Protect is off

sda: Mode Sense: 03 00 00 00

sda: assuming drive cache: write through

 sda: sda1

Attached scsi removable disk sda at scsi0, channel 0, id 0, lun 0

usb-storage: device scan complete

```

c) look for the line giving the kernel device and partition. In this case:

```
 sda: sda1
```

The path is relative to /dev, so the device path is /dev/sda1.

d) locate the /sys path for sda1. You can use this command:

```
udevinfo -q path -n /dev/sda1
```

In my case this did not work, giving me errors. However, a quick look in the /sys directory turned up the path:

 *Quote:*   

> /sys/block/sda/sda1

 

e) gather information about the usb thumb drive with

```
udevinfo -a -p /block/sda/sda1
```

This returns a lot of information. You need something to identify the device. I used the product id.

```

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.

device '/sys/block/sda/sda1' has major:minor 8:1

  looking at class device '/sys/block/sda/sda1':

    SUBSYSTEM=="block"

    SYSFS{dev}=="8:1"

    SYSFS{size}=="511967"

    SYSFS{start}=="32"

    SYSFS{stat}=="     172      172        0        0"

<snip>

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:03.1/usb2/2-2':

    BUS=="usb"

    ID=="2-2"

    DRIVER=="usb"

    SYSFS{bConfigurationValue}=="1"

    SYSFS{bDeviceClass}=="00"

    SYSFS{bDeviceProtocol}=="00"

    SYSFS{bDeviceSubClass}=="00"

    SYSFS{bMaxPower}=="200mA"

    SYSFS{bNumConfigurations}=="1"

    SYSFS{bNumInterfaces}==" 1"

    SYSFS{bcdDevice}=="0200"

    SYSFS{bmAttributes}=="80"

    SYSFS{configuration}==""

    SYSFS{devnum}=="2"

    SYSFS{idProduct}=="2168"

    SYSFS{idVendor}=="0ea0"

    SYSFS{manufacturer}=="USB     "

    SYSFS{maxchild}=="0"

    SYSFS{product}=="Flash Disk      "

    SYSFS{serial}=="CCFD41AD91B6002E"

    SYSFS{speed}=="12"

    SYSFS{version}==" 2.00"
```

 In this case (note the trailing spaces):

```
SYSFS{product}=="Flash Disk      "
```

f) construct my udev rule

```
nano - w /etc/udev/rules.d/10-udev.rules

#usb flash disk

BUS="usb", KERNEL="sd[a-z]1", SYSFS{product}="Flash Disk      ", NAME="usb/flashdisk", SYMLINK="%k" 
```

What does this mean.

BUS="usb" - is reasonably self explanatory

KERNEL="sd[a-z]1" makes sure that we use the partition 1 of our device

SYSFS{product}="Flash Disk      " - this is our unique identifier that identifies this usb thumb drive

NAME="usb/flashdisk" - the name in /dev given to this device. ivman uses this for automounting

SYMLINK="%k" - create a symlink to the kernel device name (eg /dev/sda1) 

Thus when I plug my thumb drive, udev creates two devices:

/dev/usb/flashdisk

/dev/sda1

I can now use /dev/usb/flashdisk to always refer to my thumb drive, no matter how many usb devices are plugged in.

g) add an entry to fstab for the thumb drive

```
nano -w /etc/fstab

/dev/usb/flashdisk      /mnt/flash      vfat            noauto,user,sync,noatime        0 0
```

Automounting with ivman

a) You will need the following software: 

#  HAL

# D-BUS

# ivman 

```
emerge -p hal dbus ivman
```

b) unplug & replug my thumb drive and it mounts after a moment or two.

Tricks and traps

The main trap I ran across was ensuring that the device name in fstab is the same as appears in the NAME parameter in the udev rule. ivman will not work with a symlink.

references

Linux Format magazine, May 2005 edition (lxf66). www.linuxformat.co.uk

http://gentoo-wiki.com/HOWTO_ivman

I hope this is useful for people.

regards

Richard

----------

## iggee85

Really great how-to!!! Got it working on first try, so thanks for your work in putting this together. 

Consider this how-to bookmarked.

I do have one question though, should i still umount the usb device before i disconnect it? and if so, do i umount both dirs that udev creates?

----------

## Richard

Hi iggee85,

The short answer is that you should always properly umount the drive before removing it. This allows the OS (whether it is Linux, Windows or what ever) to finish writting to the drive. If you just pull it out, you can corrupt the contents of the drive. However there are a few provisos:

1) umount needs root credentials. You can get around this with sudo (see this excellent guide http://www.gentoo.org/doc/en/sudo-guide.xml) or with a bit of messing about with HAL (http://gentoo-wiki.com/HOWTO_ivman#Mounting_mass_storage_devices_in_userspace). Personally I either leave the drive in till I log off or use the next otion.

2) if you specified "sync" in your fstab line (as I did in the example above) you should be able to just pull the drive out with relative safety. This is because the sync option basically tells the os to always keep the actual contents of the drive in sync with its virtual contents. ie it actually writes to the disk imediately when you save or whatever. On the down side, it does slow down disk operations.

```
/dev/usb/flashdisk      /mnt/flash      vfat            noauto,user,sync,noatime        0 0
```

When you do a umount, you only need to umount the mount pount you specified in fstab. Using the above example:

```
umount /mnt/flash
```

regards

Richard

----------

## daff

Great HOWTO, thanks! I'm certainly going to need this in a few weeks.

As for unmounting the the flash disk, try 

```
# eject /mnt/flash

```

which is an even more convenient way to sync, close and unmount (removable) devices.

----------

## cgmd

Richard...

Very nice howto!

I'm trying to apply this information for use with a usb 2.0 KanguruZipper Pro external 20 GB drive. I have determined my device to be /dev/sda1, and I can manually mount it to /mnt/extdrv. However when I run 

```
udevinfo -a -p /block/sda/sda1
```

... I get the following output: udeinfo

 I'm unsure of the information to abstract and use in the udev rule (I don't have /rules.d/10-udev.rules): 

```

# nano -w /etc/udev/rules.d/50-udev.rules

snippit:

# kanguruzipper pro

BUS="usb", KERNEL="sd[a-z]1", SYSFS{product}=="USB 2.0 IDE Bridge", NAME="usb/kanguru", SYMLINK="%k"

```

No matter what I try to use as "NAME", nothing new gets added to my /dev directory.

I would surely appreciate some help with this!

Edit: Problem solved... I was violating udev rules, which I discovered, reading http://www.reactivated.net/writing_udev_rules.html. Having made that correction, all is working like a charm!

Awesome!

Thanks...

----------

## Richard

Hi cgmd,

It doen't look like you are trying to use the right device. You don't really want to be looking at the USB controller or bridge, you need to be looking for something that uniquely describes your external hdd. I am not in front of a linux box at the moment, but off the top of my head, you could try a rule along the lines of: 

```
touch /etc/udev/rule.d/10-udev.rules

nano /etc/udev/rule.d/10-udev.rules

# kanguruzipper pro

KERNEL="sd[a-z]1", SYSFS{model}=="MK2006GAL       ", NAME="usb/kanguru", SYMLINK="%k"

```

Also, you sould really create the /etc/udev/rule.d/10-udev.rules file rather than edit /etc/udev/rules.d/50-udev.rules. It will ensure your rules are enacted first and save you trouble when udev needs to update its rules in the future.

I will have a more detailed look this evening.

regards

Richard

----------

## cgmd

Richard...

Yes, I already drew those conclusions, and, now have udev working well on my 2 systems for hotplugging my usb devices...

Thank you very much for the excellent HowTo! 

I also have a firewire access to my laptop, and that isn't going so smoothly... 

Is that a whole different ball game, or should this setup likewise handle firewire access??

Thanks again...

----------

## Richard

Hi cgmd,

Sorry, I can't offer an opinion about firewire as I don't have any firewire devices. Good luck with it though.

regards

Richard

----------

## cgmd

Richard...

Thanks for your assistance...

I now have a new issue, though. My usb device is a Maxtor2 external drive with both a ntfs partition for windows stuff and an ext2 partition for my linux files. My gentoo box sees both partitions, ntfs as sd?1 and ext2 as sd?2.

My udev.rules names sd?2 "maxtor2" and /etc/fdisk mounts it at /mnt/extdrv. However, sd?1 is not mentioned in udev.rules and defaults to /media/usbdisk1 as its mount point: 

```
/dev/sda1 on /media/usbdisk1 type ntfs (rw,nosuid,nodev,umask=0007,gid=100)

/dev/usb/maxtor2 on /mnt/extdrv type ext2 (rw,noexec,nosuid,nodev,sync,noatime)

```

Is there a way to set up udev.rules to avoid auto-sync mounting the sd?1 ntfs partition?

Thanks, once again!

----------

## emp

Hi, i got linux 2.6.12.5 with all kernel modules needed on.

the message i got from dmesg is:

```
usb-storage: device found at 7

usb-storage: waiting for device to settle before scanning

  Vendor: VBTM      Model: Store 'n' Go      Rev: 4.90

  Type:   Direct-Access                      ANSI SCSI revision: 00

SCSI device sdc: 497664 512-byte hdwr sectors (255 MB)

sdc: Write Protect is off

sdc: Mode Sense: 45 00 00 08

sdc: assuming drive cache: write through

SCSI device sdc: 497664 512-byte hdwr sectors (255 MB)

sdc: Write Protect is off

sdc: Mode Sense: 45 00 00 08

sdc: assuming drive cache: write through

 sdc: sdc1

Attached scsi removable disk sdc at scsi11, channel 0, id 0, lun 0

usb-storage: device scan complete

```

the message i got from udev info is:

```

device '/sys/block/sdc/sdc1' has major:minor 8:33

  looking at class device '/sys/block/sdc/sdc1':

    SUBSYSTEM=="block"

    SYSFS{dev}=="8:33"

    SYSFS{size}=="497660"

    SYSFS{start}=="4"

    SYSFS{stat}=="      41      164        0        0"

follow the "device"-link to the physical device:

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3/2-3:1.0/host11/target11:0:0/11:0:0:0':

    BUS=="scsi"

    ID=="11:0:0:0"

    DRIVER=="sd"

    SYSFS{device_blocked}=="0"

    SYSFS{iocounterbits}=="32"

    SYSFS{iodone_cnt}=="0x19"

    SYSFS{ioerr_cnt}=="0x1"

    SYSFS{iorequest_cnt}=="0x19"

    SYSFS{max_sectors}=="240"

    SYSFS{model}=="Store _n_ Go    "

    SYSFS{queue_depth}=="1"

    SYSFS{queue_type}=="none"

    SYSFS{rev}=="4.90"

    SYSFS{scsi_level}=="3"

    SYSFS{state}=="running"

    SYSFS{timeout}=="30"

    SYSFS{type}=="0"

    SYSFS{vendor}=="VBTM    "

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3/2-3:1.0/host11/target11:0:0':

    BUS==""

    ID=="target11:0:0"

    DRIVER=="unknown"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3/2-3:1.0/host11':

    BUS==""

    ID=="host11"

    DRIVER=="unknown"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3/2-3:1.0':

    BUS=="usb"

    ID=="2-3:1.0"

    DRIVER=="usb-storage"

    SYSFS{bAlternateSetting}==" 0"

    SYSFS{bInterfaceClass}=="08"

    SYSFS{bInterfaceNumber}=="00"

    SYSFS{bInterfaceProtocol}=="50"

    SYSFS{bInterfaceSubClass}=="06"

    SYSFS{bNumEndpoints}=="02"

    SYSFS{modalias}=="usb:v08ECp0012d0200dc00dsc00dp00ic08isc06ip50"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3':

    BUS=="usb"

    ID=="2-3"

    DRIVER=="usb"

    SYSFS{bConfigurationValue}=="1"

    SYSFS{bDeviceClass}=="00"

    SYSFS{bDeviceProtocol}=="00"

    SYSFS{bDeviceSubClass}=="00"

    SYSFS{bMaxPower}=="178mA"

    SYSFS{bNumConfigurations}=="1"

    SYSFS{bNumInterfaces}==" 1"

    SYSFS{bcdDevice}=="0200"

    SYSFS{bmAttributes}=="80"

    SYSFS{configuration}==""

    SYSFS{devnum}=="7"

    SYSFS{idProduct}=="0012"

    SYSFS{idVendor}=="08ec"

    SYSFS{manufacturer}=="Verbatim"

    SYSFS{maxchild}=="0"

    SYSFS{product}=="Store _n_ Go"

    SYSFS{serial}=="09105C40611298EE"

    SYSFS{speed}=="12"

    SYSFS{version}==" 2.00"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2':

    BUS=="usb"

    ID=="usb2"

    DRIVER=="usb"

    SYSFS{bConfigurationValue}=="1"

    SYSFS{bDeviceClass}=="09"

    SYSFS{bDeviceProtocol}=="00"

    SYSFS{bDeviceSubClass}=="00"

    SYSFS{bMaxPower}=="  0mA"

    SYSFS{bNumConfigurations}=="1"

    SYSFS{bNumInterfaces}==" 1"

    SYSFS{bcdDevice}=="0206"

    SYSFS{bmAttributes}=="e0"

    SYSFS{configuration}==""

    SYSFS{devnum}=="1"

    SYSFS{idProduct}=="0000"

    SYSFS{idVendor}=="0000"

    SYSFS{manufacturer}=="Linux 2.6.12.5 ohci_hcd"

    SYSFS{maxchild}=="3"

    SYSFS{product}=="nVidia Corporation nForce2 USB Controller _#2_"

    SYSFS{serial}=="0000:00:02.1"

    SYSFS{speed}=="12"

    SYSFS{version}==" 1.10"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1':

    BUS=="pci"

    ID=="0000:00:02.1"

    DRIVER=="ohci_hcd"

    SYSFS{class}=="0x0c0310"

    SYSFS{device}=="0x0067"

    SYSFS{irq}=="5"

    SYSFS{local_cpus}=="1"

    SYSFS{modalias}=="pci:v000010DEd00000067sv00001043sd00000C11bc0Csc03i10"

    SYSFS{subsystem_device}=="0x0c11"

    SYSFS{subsystem_vendor}=="0x1043"

    SYSFS{vendor}=="0x10de"

  looking at the device chain at '/sys/devices/pci0000:00':

    BUS==""

    ID=="pci0000:00"

    DRIVER=="unknown"

```

That's my rule for udev:

```

xaser3 mnt # cat /etc/udev/rules.d/10-udev.rules

# usb flash disk

BUS="usb", KERNEL="sd[a-z]1", SYSFS{model}=="Store _n_ Go    ", NAME="usb/flashdisk", SYMLINK="%k"

```

I've tried too to set SYSFS{product}=="Store _n_ Go" and it didn't work either. It doesn't create "/dev/usb/flashdisk" and it is not automounting.

What should i tried? I'm sure the problem is in 10-udev.rules . Each time i update this file, i even do "udevstart" to update!

Thank you and sorry for the long post. I just didn't know what was useful in udevinfo.

----------

## krycek

hi,

try

```

BUS="scsi" .....

```

thats what i need for my usb hd

k

----------

## Mark Clegg

In your fstab entry 

```
/dev/usb/flashdisk      /mnt/flash      vfat            noauto,user,sync,noatime        0 0

```

The user flag means anybody can mount the drive, and that person (and only that person or root) can unmount it. Since ivman runs as root, you are having to su to unmount it.

Change the "user" to "users" and anybody should be able to unmount it, regardless of who mounted it.

----------

## AdShea

A slightly more secure method is to make the stick belong to a group such as "disk" and then apply a umask like rwxrwx--- still using the users flag in fstab.  This way only certian users can access or unmount the disk.

----------

## Gotterdammerung

Nice!

----------

## farmorg

Hi

First off, thanks for the howto, i've got my usb flash drive working.

However, I made a slight change to the udev rule as I don't have an actual partition, just using the whole drive with vfat by doing:

```
#usb flash disk

BUS="usb", KERNEL="sdb", SYSFS{product}="Flash Disk      ", NAME="usb/flashdisk", SYMLINK="%k"

and

mkfs.vfat -I /dev/sdb
```

It's only a 256mb drive & making a sdb1 partition caused 2 mount points to be added (/media/usbdisk & /media/usbdisk1) which seems rather pointless & seemed to confuse things.

I am finding that it's very slow to copy data onto the drive using konqueror, copying some data from /tmp shows as 41.1kb/s and is taking forever.

I have hal,dbus & ivman running and when I plug the drive in, a line gets added to fstab thus:

```
/dev/usb/flashdisk      /media/usbdisk          vfat    user,exec,noauto,utf8,noatime,sync,managed 0 0
```

The kernel (2.6.12-gentoo-r6) is configured to use iso8859-1 as the default NLS for fat (the default NLS is utf8)

I am wondering if the problem is related to the utf8 entry that is added to fstab?

Why is it using utf8 when I told the kernel to use iso8859-1 ?

Can anyone shed any light here?

farmorg

----------

## timbo

Richard

Great little how-to thanks...

Now I can safely use my usb camera and Palm T3's sd card without the two crossing paths...

Just need to make sure you grab information from the "udveinfo..." listing and make the rule match .i.e. if you grab info from bus=scsi then your udev rule should state that too.

Regards

Tim

 :Cool: 

----------

