# HOWTO Fully Automated USB mounting using udev [WIP]

## MHenry676

Well, after a few day playing with this, I'm finally happy with what I got working, especially seeing that my experience with Linux is about 3 months (1st month of time spread out over the last few years, last 2 months straight Gentoo). This is my first HOWTO, so please be understanding. 

Summary:

This is a HOWTO for having udev automatically mount USB flash drives, create the mount point for it, and create a KDE Desktop link when such device is plugged into a USB port. When it is removed, the script will unmount the drive, remove the mount point, and remove the Desktop Icon for it. No entry in /etc/fstab needs to be defined, unless a device is to be handle in a specific way. I have included the portion of the script with my camera as an example of this. No extra auto-mount services, such as pmount, usbmount, or ivman are used. I will breakdown the script and explain what is needed to define a specific device and what is not needed if you just want it to handle USB flash devices generically. Note: I have not worked on getting USB Hard drives to work properly with this. Follow the references at the end of this post for information on this.

This is written for the experienced and the noob alike. From a noob.

Foreword:

I've been using Gentoo (using livecd 2005.0) for the last two months straight. I've been doing Windows (or has Windows been doing me?) for years. I tried it last year, but never got too far (M$ kept sucking me back.) Then after being inspired by an article in Maximum PC about the writer of the article about how he went to straight Linux, I decided to go full Gentoo. The writer had used Debian (he mentioned Gentoo briefly.) There was a time during my installation of Gentoo that I did have a problem, and proceeded to start downloading Debian's DVD image, but while that was taking it's time downloading (which I think finished in a total of 24+ hours) I was able to fix Gentoo. But I'm a tweaker, a somewhat conservative tweaker, but I do have my moments. My Windows XP installation was about as tweaked as I could. It's main running directories (like the Windows folder, Documents and Settings, and pagefile) were spread across three different drives, all unnecessary services were stopped even Automatic Updates except when I needed to use Windows Update. Heck, my Athlon XP 2500, which is suppose to be running at 1.8GHz, is pushing over 2.5GHz, out running a 3.7 P4 (darn my 333 FSB and memory, and yeah they are overclocked slightly too) So Gentoo was the way for me. And you can't beat the fast and very helpful support from the Gentoo Forums. Kudos to all who has helped someone in these forums.

After figuring out how to best deal with USE flags (and this was a while), I got Gentoo running well. Put KDE 3.2 on which I got up to 3.4, switched to using KDM with some cool themes thanks to kde-look.org (someone get that LCARS working right  though I haven't tried it  yet.) Got dual-monitors working well. Spent a good deal of time getting alsa up, XINE playing DVDs in 5.1 sound, and dumping aRts. Got TV working good through my ATI TV Wonder. Got WINE running some windows programs I couldn't live without. Got my printing and scanning working right through CUPS and xsane on a HP PSC 500. And I can't wait to get some R&R from my Neverwinter Nights running through Gentoo.

After all that, next I wanted my USB flash drives to have the same plug-in-and-go functionality I enjoyed with Windows XP. This is where I took things into my own hands. After searching, it seemed like I'd have to have several services running to make this easy. I found pmount (which seems to only be for Debian/RH for the moment,) ivman (which seemed kinda cool,) and usbmount (again a Debian/RH thing.). I also found a mention of using gnome-volume-manager, but I just don't like the looks of gnome (just my preference, not a slander of gnome.) 

Then I found this: How to automate auto mounting/unmounting of USB devices in Linux  and Writing udev rules. So thanks to diwaker and Daniel Drake! And while I'm giving thanks, thanks to my wife for her patience and support.

So after finding out how udev could do this for me, especially seeing that it was running on my system, and didn't appear to need any extra auto-mounting services. I set out to do it my own way, which was to take any USB flash drive, plug it in, have it mounted, and a link appear somewhere to it (for this I chose my KDE desktop) but if it was a specific device, like my Fuji S5000 camera, handle that in its own special way. The react properly when the flash drive is unplugged, like umount the drive and remove the link. So far, it works exactly like this.

Getting Started:

If there is something that needs to be corrected, that can be optimized, or if there is any info you think I should add/change/delete, please feel free to post and I'll correct it here. I welcome all comments. Just note, I'm no Linux guru. In fact this forum right now has my status as noob, which I am to Linux. 

First, you will need to be running with udev, not devfs. You will also have to be running with a kernel capable of supporting udev, with the necessary modules loaded (either in the kernel or out.) If you don't have USB working on your system, follow the Gentoo Linux USB Guide I don't know exactly what is needed besides this, so please post what should be needed.

Second, the script will have to be placed in the directory /etc/dev.d/block/ and the file will have to end in .dev for it to be run. You will have to create the block/ directory under /etc/dev.d/, if it does not exist. Scripts put here, and I think only one can exist here, will be ran when udev detects a change, such as plugging in a USB flash drive. 

Third, you might need a rule added to /etc/udev/rules.d/. NOTE: I'm not sure of this, cause I don't have any real scsi devices. USB flash devices are considered emulated scsi devices. Seeing that I don't have any scsi devices, not using a rule seems to work fine for me. But I tried the script with the rule defined in my /etc/udev/rules.d/10-local.rules as 

```
BUS="usb", KERNEL="sd?1", NAME="%k", SYMLINK="usb%k"
```

 and it works fine. It will just create both /dev/sda1 and /dev/usbsda1 to the first USB device I have plugged in, /dev/sdb1 and /dev/usbsdb1 for the second, and so on. So if you do have scsi devices, you may need this rule. This continues as if you don't have any real scsi! Please someone post a response if I am right or wrong on this!

If you ls the directory (/etc/udev/rules.d/), you'll see a file named '50-udev.rules'. The rules listed in this file are what makes /dev/hda and /dev/hde5 and /dev/cdrom work the way they do, by default. Check out this file, but NEVER EDIT THIS! What you can do to add your own rules, is to create a file in this directory with the naming convention of '#<50-whatever.rules' Mine is named 10-local.rules cause it will run before 50-udev.rules, but yours can be 10-ILOVESPAM.rules. As long as the leading number is less than the default rules file, it will be processed first. Also, any rules in the first rules file will take precedence over any other rules processed afterwards for the same device. Now, no rule file has to be added to /etc/udev/rules.d/ for this script to work, unless you need to have a device handled in its own way. In fact, here is my 10-local.rules

```

BUS="usb", SYSFS{serial}="serial number of my camera", KERNEL="sd?1", NAME="%k", SYMLINK="Fuji_S5000"

#BUS="usb", KERNEL="sd?1", NAME="%k", SYMLINK="usb%k"

```

The first line is for my camera cause I wanted it mounted always to a specific mount point, which is defined in my fstab. Notice the second line is commented, therefore, not used. Originally, the first script I had was similar to what I found on diwaker's page (see references at the end of this post.) My first 10-local.rules only had the line 

```
BUS="usb", KERNEL="sd?1", NAME="%k", SYMLINK="usb%k"
```

. It would redefine any usb flash device as /dev/usbsdX1 (X being the letter the kernel would define for it, for example /dev/sda1 or /dev/sdb1) then mount it when it was plugged in. Then I needed each of those possible mount points defined in my fstab. This to me seemed unnecessary, which is why I wrote my own script.

So, now for the script. First I will break it down and explain each section, then list the script in its entirety.

Breakdown of the Script:

From now on, I will use 'sdX1', where X can be any letter that is used. When you plug in two or more flash devices, the first one will use sda1 and the second will use sdb1 and so on.

The first part of the script comes from diwaker's script. It simply sets the variable $base to the device that is assigned to the flash device, be it sda1, sdb1, sdc1, etc. It then sends information to the system logger about what's being done. (I don't know more about the system logger other than it logs and I have syslog-ng running, yet, like I said I'm a Linux noob.) It then makes sure that the only devices that are controlled by this script are of the sd type (like sda1, sde5, etc) which are scsi devices. (Make sure you read the bolded note above if you have real scsi devices!)

```

#!/bin/bash

base=$(basename $DEVNAME)

#Send info to logger

logger -t automount_dev "`env`"

#Check to see if the device is a sdX1 device

if [[ $base != sd[a-g][1-9] ]]

then

exit

fi

```

The next part starts what to do when the device is added. It will first gather information about the device such as the name of the device the maker of the device and it's serial number. This information is used to name the link created on the desktop. Remember, this is for KDE. I don't know how gnome does this or any other window manager. It also stores this information in a file located in the /tmp directory as '/tmp/sdX1'. Storing this information is not necessary if you don't need any special handling for specific devices, but it won't harm anything and might even be useful to you. I used the '+' as the delimiter because the serial number for my camera contains dashes and carrots (- and ^) so I was being safe, but you might need to change it if you need to.

```

#Do this when adding the plugged device

if [ "$ACTION" == "add" ]

then

#Retrieve info about the added device and store into variables

product=$(udevinfo -a -p $DEVPATH | grep SYSFS{product} -m 1 | cut --delim='"' -f2)

vendor=$(udevinfo -a -p $DEVPATH | grep SYSFS{vendor} -m 1 | cut --delim='"' -f2)

serial=$(udevinfo -a -p $DEVPATH | grep SYSFS{serial} -m 1 | cut --delim='"' -f2)

#Store the device info into a file named after the /dev/sdX1 point

#--Only needed when retrieving info for specific devices when unplugged

#--because you can't query a non-existent device

echo "$base+$DEVPATH+$vendor+$product+$serial" >> /tmp/$base

```

(Optional) This next section is an example of detecting a specific device, so if you don't need to do this you can just remove this section. I chose to check the serial number of the device, and if it is that device to do the stated action. In this example, it will verify that the plugged in device is my camera, then mount it simply with a 'mount /dev/Fuji_S5000'. This is because /dev/Fuji_S5000 is defined in my /etc/fstab as '/dev/Fuji_S5000 /mnt/Fuji_S5000/camera auto noauto,gid=100,umask=0707 0 0' and a mount point is always available at /mnt/Fuji_S5000/camera/. It then exits cause that is all that is needed. The link icon exists on my desktop at all times. Now this part is optional even for the camera, it will just work as any other usb flash device would if I didn't have this section in there. The only thing I haven't figured out is how to get the icon for the camera to switch from the unmounted to mounted icon. 

Now as I'm writing this I just thought of something. Is this serial for the camera? Or the memory card? Where is my other memory card? I'm not sure, but this I can easily just make it check for the vendor name which is FUJIFILM. Once I find that other memory card, I'll know for sure.

```

#If device is my Fuji S5000 by checking the serial of the plugged device

#--do this then exit

#--this device is added into /etc/fstab as...

#--/dev/Fuji_S5000 /mnt/Fuji_S5000/camera auto noauto,gid=100,umask=0707 0 0

if [ "$serial" == "serial number of my camera" ]

then

mount /dev/Fuji_S5000

exit

fi

```

This now continues as if the camera was not the device plugged in. It will create the mount point at /mnt/usb/sdX1 then mount it. The mount options I used are to mount it asynchronously (so that all reads/writes are done then and there) and allow all users rw access. You may need to change this based on your needs.

```

#Create mount point and mount the device to it with proper access

mkdir /mnt/usb/$base

mount -o async,gid=100,umask=0707 /dev/$base /mnt/usb/$base

```

Now we create the KDE desktop icon. Pretty simple here. First it is created in /tmp and filled with the information...

```

#Create a KDE link file for use on the desktop (used for KDE 3.4)

#--Link name is generated from the devices' vendor, product, serial and $base 

echo "[Desktop Entry]" >> /tmp/link2$base

echo "Dev=/dev/$base" >> /tmp/link2$base

echo "Encoding=UTF-8" >> /tmp/link2$base

echo "Icon=usbpendrive_mount" >> /tmp/link2$base

echo "MountPoint=/mnt/usb/$base" >> /tmp/link2$base

echo "Name=$vendor $product [$serial] on $base" >> /tmp/link2$base

echo "Name[en_US]=$vendor $product [$serial] on $base" >> /tmp/link2$base

echo "ReadOnly=False" >> /tmp/link2$base

echo "Type=FSDevice" >> /tmp/link2$base

echo "UnmountIcon=usbpendrive_unmount" >> /tmp/link2$base

echo "Encoding=UTF-8" >> /tmp/link2$base

```

...then copied to all users under /home

```

#Copy the link file to all users' Desktops and to the root's

for x in /home/*; do cp /tmp/link2$base $x/Desktop/link2$base; done

cp /tmp/link2$base /root/Desktop/link2$base

```

And of course the whole desktop icon part (the last two sections) is completely removable/changeable. As in the script, the icon will appear on your desktop titled according to the device info the script gathered, but the actual file is at /home/yourusername/Desktop/link2sdX1. For example, my flash drive will appear as 'SanDisk Cruzer Micro [SERIALNUMBERHERE] on sda1'. This should give enough distinction if you plug in two of them, especially if the are both Cruzer Micros for example.

And now what to do when the device is removed...

```

#Do this when the device is unplugged

elif [ "$ACTION" == "remove" ]

then

```

(Optional) The device has been removed. You can't retrieve information from the device if it doesn't exist. So you retrieve it from the file that was created upon insertion of the device. (Wow, that last part sounded so covert operational) This file is /tmp/$base/. 

```

#Retrieve the serial number of the device from the /tmp/$base file

#--only needed for checking for specific devices

serial=$(cat /tmp/$base | cut --delim='+' -f5)

```

Now this is for checking my camera, which like I said before, checking via serial may be wrong. So you could use vendor by changing this next line to 

```
vendor=$(cat /tmp/$base | cut delim'+' -f3)
```

(Optional) This is where you find out if the device removed was that specific device you specifically mounted in the ADD section. Like my camera for example, I check the serial and if it matches, it umounts my camera. The -l makes the system unmount it when the system is done with it, to avoid any issues. It would then exit.

```

#If the device removed was my Fuji S5000

if [ "$serial" == "serial number of my camera" ]

then

umount -l /dev/Fuji_S5000

rm -f /tmp/$base

exit

fi

```

Now, continuing if it was just a generic device. Unmount the device at /mnt/usb/sdX1, remove the mount point, and all tmp files. 

```

#Unmount the device and cleanup references

umount -l /mnt/usb/$base

rmdir /mnt/usb/$base

rm -f /tmp/link2$base

rm -f /tmp/$base

```

Then delete the desktop links.

```

#Remove KDE Desktop links created

for x in /home/*; do rm -f $x/Desktop/link2$base; done

rm -f /tmp/$base /root/Desktop/$base

fi

```

And that's it!

Now, the full script:

```

#!/bin/bash

base=$(basename $DEVNAME)

#Send info to logger

logger -t automount_dev "`env`"

#Check to see if the device is a sdX1 device

if [[ $base != sd[a-g][1-9] ]]

then

exit

fi

#Do this when adding the plugged device

if [ "$ACTION" == "add" ]

then

#Retrieve info about the added device and store into variables

product=$(udevinfo -a -p $DEVPATH | grep SYSFS{product} -m 1 | cut --delim='"' -f2)

vendor=$(udevinfo -a -p $DEVPATH | grep SYSFS{vendor} -m 1 | cut --delim='"' -f2)

serial=$(udevinfo -a -p $DEVPATH | grep SYSFS{serial} -m 1 | cut --delim='"' -f2)

#Store the device info into a file named after the /dev/sdX1 point

#--Only needed when retrieving info for specific devices when unplugged

#--because you can't query a non-existent device

echo "$base+$DEVPATH+$vendor+$product+$serial" >> /tmp/$base

#If device is my Fuji S5000 by checking the serial of the plugged device

#--do this then exit

#--this device is added into /etc/fstab as...

#--/dev/Fuji_S5000 /mnt/Fuji_S5000/camera auto noauto,gid=100,umask=0707 0 0

if [ "$serial" == "<my camera's serial number>" ]

then

mount /dev/Fuji_S5000

exit

fi

#Create mount point and mount the device to it with proper access

mkdir /mnt/usb/$base

mount -o async,gid=100,umask=0707 /dev/$base /mnt/usb/$base

#Create a KDE link file for use on the desktop (used for KDE 3.4)

#--Link name is generated from the devices' vendor, product, serial and $base 

echo "[Desktop Entry]" >> /tmp/link2$base

echo "Dev=/dev/$base" >> /tmp/link2$base

echo "Encoding=UTF-8" >> /tmp/link2$base

echo "Icon=usbpendrive_mount" >> /tmp/link2$base

echo "MountPoint=/mnt/usb/$base" >> /tmp/link2$base

echo "Name=$vendor $product [$serial] on $base" >> /tmp/link2$base

echo "Name[en_US]=$vendor $product [$serial] on $base" >> /tmp/link2$base

echo "ReadOnly=False" >> /tmp/link2$base

echo "Type=FSDevice" >> /tmp/link2$base

echo "UnmountIcon=usbpendrive_unmount" >> /tmp/link2$base

echo "Encoding=UTF-8" >> /tmp/link2$base

#Copy the link file to all users' Desktops and to the root's

for x in /home/*; do cp /tmp/link2$base $x/Desktop/link2$base; done

cp /tmp/link2$base /root/Desktop/link2$base

#Do this when the device is unplugged

elif [ "$ACTION" == "remove" ]

then

#Retrieve the serial number of the device from the /tmp/$base file

#--only needed for checking for specific devices

serial=$(cat /tmp/$base | cut --delim='+' -f5)

#If the device removed was my Fuji S5000

if [ "$serial" == "<my camera's serial number>" ]

then

umount -l /dev/Fuji_S5000

rm -f /tmp/$base

exit

fi

#Unmount the device and cleanup references

umount -l /mnt/usb/$base

rmdir /mnt/usb/$base

rm -f /tmp/link2$base

rm -f /tmp/$base

#Remove KDE Desktop links created

for x in /home/*; do rm -f $x/Desktop/link2$base; done

rm -f /tmp/$base /root/Desktop/$base

fi

```

References:

Gentoo Linux USB Guide

 Wouldn't have made it too far without this.

How to automate auto mounting/unmounting of USB devices in Linux 

-- What helped me figure this script out.

Writing udev rules

-- Lots of useful information. How I found I could retrieve the information of the devices. 

Thanks to:

Shyam Mani, diwaker and Daniel Drake; the authors of the above articles, in order.

Well, there ya go. Hope you get it working for you. Feel free to post questions (and post and corrections.) 

Things to do:

Find that memory card!

Get this working with USB Hard Drives. (Are they all called 'USB 2.0 Storage Device' for SYSFS{product}?)

Re-word some of this. (Currently quite sleepy, but wanted to get this out)

Fix any errors/misconceptions in this HOWTO.

Have sections to create desktop shortcuts for other windows managers. Please post what you can, cause I probably won't use anything but KDE.

Network shares?

CD/DVD automount on disc insertion, eject with eject button (for non-usb drives)

Anything else I can think of.Last edited by MHenry676 on Mon Jul 18, 2005 10:21 pm; edited 1 time in total

----------

## johntramp

 *Quote:*   

> 
> 
> When it is removed, the script will unmount the drive

 

Hi.  Would you not want to umount the device BEFORE you disconnect it from the computer.  Doesn't the defeat the purpose of umounting a device if it doesn't have access to it anymore?

PS:  I only skimmed throught the howto, but it does look like you put a lot of effort into it.  It looks very good,  hopefully it will help out other users like yourself  :Smile: 

----------

## hadur

I was looking for something like this. I hated the idea of running extra services just for USB auto-mounting. This is perfect. Thank you!

----------

## MHenry676

johntramp said:

 *Quote:*   

> Hi. Would you not want to umount the device BEFORE you disconnect it from the computer. Doesn't the defeat the purpose of umounting a device if it doesn't have access to it anymore? 

 

You still have the option. It's kinda like with Windows, before unplugging the USB device to click on that hardware icon on the sys tray, to stop the device. I'm assuming cause this is to make sure any queued read/writes are finished before unpluging it. For the mount options, i'm not sure if mounting with sync or async is the best way. I couldn't find a solid answer on what should be used, though the man page was my only reference on this. So I think I have it working where all data handleing should be done on the spot (is it?). I tried both with the understanding that async would perform all actions asynchronously (meaning out of sync with the system cache and doing all read/writes then and there) and that sync would perform all actions synchronously (meaning sync'ed with the system cache and doing all read/writes the next time it could, just not neccessarily right away.) 

Now, as far as just right clicking on that created desktop icon and selecting unmount? It doesn't do that as of now. I probably have to change the mount statement to allow this, just not sure exactly how at the moment. So if you wish to unmount the drive before unplugging it, you'll have to unmount it as root. Again, I think I just need a better mount statement.

----------

## Headrush

I know you mentioned doing this without any programs like pmount, or ivman but isn't that a lot of work for nothing? Aren't you just repeating what they do? I'm not trying to discourage you, but seems like a lot of unneeded work.

Pmount is such a small program and once installed I only needed to edit 1 or lines and all my usb devices worked automatically in KDE. (several flash keys, portable hard drive, and camera.

This includes a desktop icon for each that appears when inserted and removed when unplugged.

Right clicking any icon has a safely remove option to make sure write cache is empty before unmounting.

Absolutely no problems with this system and auto-mounting of CDs works flawless too.

----------

## MHenry676

Headrush wrote:

 *Quote:*   

> I know you mentioned doing this without any programs like pmount, or ivman but isn't that a lot of work for nothing? Aren't you just repeating what they do? I'm not trying to discourage you, but seems like a lot of unneeded work.
> 
> Pmount is such a small program and once installed I only needed to edit 1 or lines and all my usb devices worked automatically in KDE. (several flash keys, portable hard drive, and camera.
> 
> This includes a desktop icon for each that appears when inserted and removed when unplugged.
> ...

 

Well, it was just one of those things I wanted to do myself. (Why would you climb the mountain when you can take a hellicopter ride to the top?) Plus, the state it is in now doesn't require all that much work to suit it to your needs, and it's quite easy.

For example, if you want your Flash drive to be specifically mounted to a certain location all the time, plug it in with this script running. You'll then see the information about the drive that you can use and do something similar like I did with my camera.

BTW, Simpsons fan? (Location: Bizarro Wolrd)

----------

## Headrush

 *MHenry676 wrote:*   

> 
> 
> Well, it was just one of those things I wanted to do myself. (Why would you climb the mountain when you can take a hellicopter ride to the top?) Plus, the state it is in now doesn't require all that much work to suit it to your needs, and it's quite easy.
> 
> For example, if you want your Flash drive to be specifically mounted to a certain location all the time, plug it in with this script running. You'll then see the information about the drive that you can use and do something similar like I did with my camera.

 

I applaud your work, but I thought it just seemed like a solution needing a problem with the latest KDEs and HAL support. But that is the great thing about Gentoo, choose your own preference  :Smile: 

 *MHenrey676 wrote:*   

> BTW, Simpsons fan? (Location: Bizarro Wolrd)

 

Actually a Seinfeld fan  :Smile:  , but the Simpsons are great too!

----------

## MHenry676

 *Quote:*   

> ...with the latest KDEs and HAL support.

 

Curious, what KDE support is there for this kind of thing? I've read that gnome has the gnome-volume-manager, but have found nothing like what I got going for KDE.

----------

## nephros

 *Quote:*   

>  For the mount options, i'm not sure if mounting with sync or async is the best way. I couldn't find a solid answer on what should be used, though the man page was my only reference on this.

 

From what I gather from a recent (and lengthy) thread on LKML, flash drives should in any case be mounted async, because the current FAT implementations are poison for flash media.

(In particular, the FAT table gets written to a lot when using sync, leading to strain of the physical media containing the FAT table, eventually damaging it permanently).

----------

## Headrush

 *MHenry676 wrote:*   

>  *Quote:*   ...with the latest KDEs and HAL support. 
> 
> Curious, what KDE support is there for this kind of thing? I've read that gnome has the gnome-volume-manager, but have found nothing like what I got going for KDE.

 

It may not be exact to what you have, but with KDE 3.4 devices are placed on the desktop when inserting removeable media. 

When you double click the icon, the drive is automatically mounted and entries created in /etc/fstab.

The contextual menu for each icon has "unmount, safely remove, and eject" options.

As far as I know, names are  determined by volume name on device and mount points are in /media, not /mnt.

This works with my USB hard drive, usb flash key, and my usb camera.

----------

## RinkyDinks_RJ

Using KDE 3.4, with udev, hald, dbus, and ivman, I have links automatically put onto my desktop when inserted. You do have to have the Storage Media Applet in the panel (takes up no space when devices are mounted) and you might have to do some customizing of which types of devices you want shown on the desktop. KControl -> Behavior -> Device Icons.

Also, you can access your devices from the media:/ kioslave in Konqueror.

Anyhow, great writeup.

----------

## Headrush

 *RinkyDinks_RJ wrote:*   

> You do have to have the Storage Media Applet in the panel (takes up no space when devices are mounted) and you might have to do some customizing of which types of devices you want shown on the desktop. KControl -> Behavior -> Device Icons.

 

I do not have the applet loaded and it works fine.

----------

## maguire

I believe that the original author has the definitions of "sync" and "async" mixed up.  He says:

 *Quote:*   

> 
> 
> mount it asynchronously (so that all reads/writes are done then and there)
> 
> 

 

A synchronous write is "done then and there"; i.e. the write command only returns when the data is actually written to the media.  An asynchronous write command will return successfully, even though the data may only exist in a cache somewhere---not actually on the media.

This definition makes sense, because synchronous means "happening, existing, or arising at precisely the same time" (Merriam Webster Online Dictionary).  So a synchronous write is one that really occurs when you issued the command.  Asynchronous writes may not occur at the time you commanded, because the data may be stored in a cache somewhere ("buffered-up") until some time in the future when the system determines that the real write should occur.

So, at first blush, it may appear that synchronous (sync) would be the best mode for removable media---since the media may be removed at any time, and any writes to it had better been done as soon as possible.  Unfortunately, with Flash media, there is a counterbalancing concern: The media is only good for a some number of writes (hundreds of thousands or millions).  That may not seem to be a problem to you, but it may be a problem for the filesystem.  I believe that is what one of the other posters meant when he referenced a discussion about "sync" mounts and the FAT filesystem.  If the filesystem does many writes to a small area of the disk (like a File Allocation Table), then that inherent limitation of the Flash memory can occur much earlier than you would have thought.

Adding even more confusion to the issue, is that fact that the mount manpage says this:

 *Quote:*   

> 
> 
> ...the sync option today has effect only for [sic] ext2, ext3 and ufs...
> 
> 

 

So, I really don't know the definitive answer, but hopefully this clears-up the sync/async definitions and issues.

Bruce.

----------

## urcindalo

I use Gentoo AMD64, and followed this guide to the letter. However, I see no icon appear on my desktop one I insert my USB flash disk.

This is what I did:

1) I created the file /etc/udev/rules.d/10-local.rules with the content specified in the thread:

```
$ ls -lA /etc/udev/rules.d/

total 20

-rw-r--r--  1 root root    54 nov 14 16:57 10-local.rules

-rw-r--r--  1 root root 11492 nov  4 17:21 50-udev.rules

-rw-r--r--  1 root root    42 nov 10 10:10 60-vmware.rules

$ ls -lA /etc/udev/rules.d/10-local.rules

-rw-r--r--  1 root root 54 nov 14 16:57 /etc/udev/rules.d/10-local.rules

$ cat /etc/udev/rules.d/10-local.rules

BUS="usb", KERNEL="sd?1", NAME="%k", SYMLINK="usb%k"

$
```

2) I saved the script in this thread in /etc/dev.d/block after commenting out all the lines pertaining to the author's digital camera, and gave it exec permissions:

```
$ ls -lA /etc/dev.d/block

total 4

-rwxr-xr-x  1 root root 2787 nov 14 16:26 automontaje.dev

$
```

3) I commented out the line in my /etc/fstab that I used in the past to mount the usb sitck:

```
$ cat /etc/fstab | grep usb

none /proc/bus/usb usbfs defaults,devmode=0666 0 0

#/dev/sda       /mnt/usbdisk    vfat    noauto,users,exec,noatime       0       0

$
```

4) I rebooted

However, nothing happens when I plug the usb flash disk. Why is that? I use the very latest AMD64 stable KDE and udev.

----------

## incabolocabus

When you plug in your flash disk, you should see something like this

```
Nov 15 11:39:03 foo ohci_hcd 0000:00:02.0: wakeup

Nov 15 11:39:03 foo usb 1-8: new full speed USB device using ohci_hcd and address 5

Nov 15 11:39:03 foo scsi3 : SCSI emulation for USB Mass Storage devices

Nov 15 11:39:03 foo usb-storage: device found at 5

Nov 15 11:39:03 foo usb-storage: waiting for device to settle before scanning

Nov 15 11:39:08 foo Vendor:           Model: USB DISK          Rev: 2.08

Nov 15 11:39:08 foo Type:   Direct-Access                      ANSI SCSI revision: 00

Nov 15 11:39:08 foo SCSI device sda: 1015808 512-byte hdwr sectors (520 MB)

Nov 15 11:39:08 foo sda: Write Protect is off

Nov 15 11:39:08 foo sda: Mode Sense: 43 00 00 00

Nov 15 11:39:08 foo sda: assuming drive cache: write through

Nov 15 11:39:08 foo SCSI device sda: 1015808 512-byte hdwr sectors (520 MB)

Nov 15 11:39:08 foo sda: Write Protect is off

Nov 15 11:39:08 foo sda: Mode Sense: 43 00 00 00

Nov 15 11:39:08 foo sda: assuming drive cache: write through

Nov 15 11:39:08 foo sda: sda1

Nov 15 11:39:08 foo Attached scsi removable disk sda at scsi3, channel 0, id 0, lun 0

Nov 15 11:39:08 foo usb-storage: device scan complete

Nov 15 11:39:09 foo scsi.agent[8108]: disk at /devices/pci0000:00/0000:00:02.0/usb1/1-8/1-8:1.0/host3/target3:0:0/3:0:0:0

Nov 15 11:39:09 foo automount_dev: PHYSDEVPATH=/devices/pci0000:00/0000:00:02.0/usb1/1-8/1-8:1.0/host3/target3:0:0/3:0:0:0 ID_MODEL=USB_DISK ID_REVISION=2.08 ID_BUS=usb SUBSYSTEM=block ID_SERIAL=_USB_DISK_19320FA20597 DEVPATH=/block/sda MINOR=0 ACTION=add PWD=/ MAJOR=8 UDEVD_EVENT=1 DEVNAME=/dev/sda SHLVL=1 PHYSDEVDRIVER=sd ID_TYPE=disk ID_VENDOR= PHYSDEVBUS=scsi ID_PATH=usb-19320FA20597:0:0:0 SEQNUM=1102 _=/usr/bin/env

Nov 15 11:39:09 foo automount_dev: PHYSDEVPATH=/devices/pci0000:00/0000:00:02.0/usb1/1-8/1-8:1.0/host3/target3:0:0/3:0:0:0 ID_MODEL=USB_DISK ID_REVISION=2.08 ID_FS_LABEL_SAFE= ID_FS_LABEL= ID_BUS=usb SUBSYSTEM=block ID_SERIAL=_USB_DISK_19320FA20597 ID_FS_UUID=1CE4-0F4D DEVPATH=/block/sda/sda1 ID_FS_VERSION=FAT16 MINOR=1 ACTION=add PWD=/ ID_FS_TYPE=vfat MAJOR=8 UDEVD_EVENT=1 DEVNAME=/dev/sda1 SHLVL=1 ID_FS_USAGE=filesystem PHYSDEVDRIVER=sd ID_TYPE=disk ID_VENDOR= PHYSDEVBUS=scsi ID_PATH=usb-19320FA20597:0:0:0 SEQNUM=1103 _=/usr/bin/env
```

The script assumes you already have a /mnt/usb directory and will fail if you don't.

Once I made this directory, it worked well.

----------

## urcindalo

 *incabolocabus wrote:*   

> When you plug in your flash disk, you should see something like this
> 
> {....}
> 
> The script assumes you already have a /mnt/usb directory and will fail if you don't.
> ...

 

Thanks for trying to help me out here.

1) Where should I see the output you mention?

2) I created the directory as you suggested, and _rebooted_ (to be sure the new config is correctly read). However, still the same: nothing happens on my desktop. I see no icon appear automounting the flash disk.

Just in case, this is what I get from lsusb:

```
# lsusb

Bus 005 Device 001: ID 0000:0000

Bus 004 Device 001: ID 0000:0000

Bus 003 Device 001: ID 0000:0000

Bus 002 Device 001: ID 0000:0000

Bus 001 Device 004: ID 054c:011d Sony Corp.

Bus 001 Device 003: ID 054c:011c Sony Corp.

Bus 001 Device 002: ID 054c:011e Sony Corp.

Bus 001 Device 001: ID 0000:0000

# lsusb -t

Bus#  5

`-Dev#   1 Vendor 0x0000 Product 0x0000

Bus#  4

`-Dev#   1 Vendor 0x0000 Product 0x0000

Bus#  3

`-Dev#   1 Vendor 0x0000 Product 0x0000

Bus#  2

`-Dev#   1 Vendor 0x0000 Product 0x0000

Bus#  1

`-Dev#   1 Vendor 0x0000 Product 0x0000

  `-Dev#   2 Vendor 0x054c Product 0x011e

    |-Dev#   3 Vendor 0x054c Product 0x011c

    `-Dev#   4 Vendor 0x054c Product 0x011d

#
```

It is a Sony USB 2.0 128 MB flash disk with a memory stick reader incorporated. This is the info from "dmesg":

```
usb 1-8: new high speed USB device using ehci_hcd and address 2

hub 1-8:1.0: USB hub found

hub 1-8:1.0: 2 ports detected

usb 1-8.1: new high speed USB device using ehci_hcd and address 3

scsi2 : SCSI emulation for USB Mass Storage devices

usb-storage: device found at 3

usb-storage: waiting for device to settle before scanning

usb 1-8.2: new high speed USB device using ehci_hcd and address 4

scsi3 : SCSI emulation for USB Mass Storage devices

usb-storage: device found at 4

usb-storage: waiting for device to settle before scanning

  Vendor: Sony      Model: Storage Media     Rev: 1.00

  Type:   Direct-Access                      ANSI SCSI revision: 00

SCSI device sda: 256000 512-byte hdwr sectors (131 MB)

sda: Write Protect is off

sda: Mode Sense: 00 06 00 00

sda: assuming drive cache: write through

SCSI device sda: 256000 512-byte hdwr sectors (131 MB)

sda: Write Protect is off

sda: Mode Sense: 00 06 00 00

sda: assuming drive cache: write through

 sda: unknown partition table

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

Attached scsi generic sg0 at scsi2, channel 0, id 0, lun 0,  type 0

usb-storage: device scan complete

  Vendor: Sony      Model: USM-MS            Rev: 1.00

  Type:   Direct-Access                      ANSI SCSI revision: 00

Attached scsi removable disk sdb at scsi3, channel 0, id 0, lun 0

Attached scsi generic sg1 at scsi3, channel 0, id 0, lun 0,  type 0

usb-storage: device scan complete
```

----------

## AmosMutke

 *urcindalo wrote:*   

> 1) Where should I see the output you mention?

 

From the output of "dmesg" (as you seemed to have figured out) or alternatively should be printed in the "F12" terminal window too. <ctrl>+<alt>+F12

Isn't there an obvious problem with pulling a USB memory device without unmouting it first if you hav async?  Everything in the cache will be lost and it's possible files where left open which might cause them to be damaged.

BTW.. ef'n great tutorial.  I recently bought a USB2.0/IEEE hard drive and want it mounted automatically in the same place.. of course my problem is that I also mount my digital camera and usb stick.  At different times and in different order.  That means I was required to manually mount each device in /mnt/usb1 /mnt/usb2 and /mnt/usb3 and then remember what each was.  (I have a laptop that I bring with me to work everyday to two different offices and at home. Hence the constant re-plugging of devices).  This was next on my list to tackle. You save me lots of leg work.  Next time you're in Northern Honshu, Japan, I'll buy you an Asahi.

-Amos

----------

## AmosMutke

OK.. thanx to this howto, I was quickly able to get my digital camera, usb memory stick, and external ieee1394 hard drive to auto mount.  I'm adding my experience to the knowlege base in the hopes it might be useful to somebody at some point.

I initially had trouble trying to find out what information was provided by "udevinfo" in my autoload.dev script, so I wrote this script to dump the contents to a temporary file.

```
#!/bin/bash

base=$(basename $DEVNAME)

debuginfo=$(udevinfo -a -p $DEVPATH)

if [[ $base == sd[a-g][1-9] ] &&  "$ACTION" == "add" ]

then

  echo "$debuginfo" > /tmp/automount.dev.debug

fi

```

creat a file called /etc/dev.d/block/test.dev with the contents of the above code block in it and make it executable

```
chmod +x /etc/dev.d/block/test.dev
```

Now when you plug in a usb storrage device, it will put the information available for that device in /tmp/automount.dev.debug

You can use this information to help build your autoload.dev script.  for me "model" was the only unique common variable for all three devices, but it is suffecient.

Here's the info on my setup.  I have a Buffalo External IEEE1394 250GB Harddrive, Sony Cybershot DSC-P200 digital camera, and a Buffalo USB2.0 250MB Clip Drive.

My IEEE1394 Harddrive holds all my media files and I want to have a link from ~/multimedia/video -> /mnt/ieee1394/sd?1/video 

(It's important that pron be easily accessable)

When the usb stick or camera is connected, I want "~/usb" and "~/camera" directories created respectively.

here's my /etc/dev.d/block/autoload.dev script

```
#!/bin/bash

base=$(basename $DEVNAME)

#Send info to logger

logger -t automount_dev "`env`"

#Check to see if the device is a sdX1 device

if [[ $base != sd[a-g][1-9] ]]

then

  exit

fi

if [ "$ACTION" == "add" ]

then

  model=$(udevinfo -a -p $DEVPATH | grep model -m 1 | cut --delim='"' -f2 | tr -d ' ')

  debuginfo=$(udevinfo -a -p $DEVPATH)

  echo "$debuginfo" > /tmp/automount.dev.debug

  

  #  See if it's the external HD, camera, or usb clip drive.

  if [ "$model" == "SonyDSC" ]

  then

    #  Create mount point and mount the device to it with proper access

    mkdir /mnt/usb/$base

    mount -o async,gid=100,umask=0707 /dev/$base /mnt/usb/$base

    echo "camera" > /tmp/$base

    ln -s /mnt/usb/$base /home/shawn/camera

  elif [ "$model" == "ClipDrive" ] # buffalo clip drive

  then

    mkdir /mnt/usb/$base

    mount -o async,gid=100,umask=0707 /dev/$base /mnt/usb/$base

    echo "buffalo clip drive" > /tmp/$base

    ln -s /mnt/usb/$base /home/shawn/usb

  elif [ "$model" == "WD2500BB-22GUC0" ]

  then

    mkdir /mnt/ieee1394/$base

    mount -o async,gid=100,umask=0707 /dev/$base /mnt/ieee1394/$base

    echo "buffalo ieee1394 hd" > /tmp/$base

    ln -s /mnt/ieee1394/$base/video /home/shawn/multimedia/video

    ln -s /mnt/ieee1394/$base/video /home/stopher/multimedia/video    

  else exit

  fi

#Do this when the device is unplugged

elif [ "$ACTION" == "remove" ]

then

  #  Unmount the device and cleanup references

  unplugged=$(cat /tmp/$base)   

  if [ "$unplugged" == "camera" ] # camera

  then

    umount -l /mnt/usb/$base

    rm -f /home/shawn/camera

  elif [ "$unplugged" == "buffalo clip drive" ] # buffalo clip drive

  then

    umount -l /mnt/usb/$base

    rm -f /home/shawn/usb

  elif [ "$unplugged" == "buffalo ieee1394 hd" ] # buffalo ieee hard drive

  then

    umount -l /mnt/ieee1394/$base

    rm -f /home/shawn/multimedia/video

    rm -f /home/stopher/multimedia/video

  else exit

  fi

  rmdir /mnt/usb/$base

fi

```

Concerns

I have some thoughts about possible problems, or rather a false sense of security, by using automount.  Namely, if you don't first umount your device, it's possible that parts of files are still in a cache and will not be written when you disconnect the device.  Does anyone know if there is a command to flush a possible cache before you pull the device?  say "flush /dev/sda" for example?

If you haven't written anything to the disk (Let's say you insert your usb memory stick, check the contents, and then remove it), is there any danger of damaging the file system?  reason would suggest it's OK.

----------

## anheuser50

I found the information here quite interesting and modified your script quite a bit. I hate icon names that are 5 miles long (Such as with my alienware 1G Pendrive) and wanted a bit more functionality.  The following script is derivied from your work, but also scans the /etc/udev/rules.d directory for matching devices to grab the SYMLINK name of the device. 

The symlink is used to named the device if available and a symlink is created in the mount point directory. Note that in this script I have created a directory of /r for the removable media to be mounted under.

```
#!/bin/bash

#Function to extract the SYMLINK="link" entry from a line from the udev rules

getSymLink () {

        for var in $line ; do

            if [[ "$var" == SYMLINK* ]] ; then

                symlink=$(echo $var | cut --delim='"' -f2)

                break

            fi

        done

}

base=$(basename $DEVNAME)

#Send info to logger

logger -t automount_dev "USB $ACTION: $DEVPATH"

#Check to see if the device is a sdX1 device // Changed a-g to a-z (I have 13 SCSI devices)

if [[ $base != sd[a-z][1-9]* ]]; then

    #echo "$base != sdX1" >> /r/debug

    exit

fi

case $ACTION in

    add)

   vendor=$(udevinfo -a -p /sys/block/$DEVNAME | grep SYSFS{vendor} -m 1 | cut --delim='"' -f2)

        #What SYSFS Entries are we going to search through to match the symlink?

   lookin="serial idProduct idVendor product vendor"

   for filename in /etc/udev/rules.d/*.rules ; do

       symlink=""

       for look in $lookin ; do

           match=$(udevinfo -a -p /sys/$DEVPATH | grep SYSFS\{$look\} -m 1 | cut --delim='"' -f2)

           line=$(grep SYSFS\{$look\}=\"$match\" -m 1 $filename)

           if [[ -n $line ]] ; then

               getSymLink

           fi

       done

       if [[ -n $symlink ]] ; then

           #Use the SymLink as the name

           linkname="$symlink [$base]"

           break

       else 

           #Use something else as the name

           linkname="$vendor [$base]"

           break

       fi

   done

    

        echo "$base+$DEVPATH+$vendor+$product+$serial" > /r/info_$base

    

    

        #Create mount point and mount the device to it with proper access

        mkdir /r/$base

        mount -t vfat -o async,gid=100,umask=0707 /dev/$base /r/$base

   if [[ -n $symlink ]] ; then

           ln -s /r/$base /r/$symlink

   fi

    

        #Create a KDE link file for use on the desktop (used for KDE 3.4)

        #--Link name is generated from the devices' vendor, product, serial and $base

        echo "[Desktop Entry]" > /tmp/link2$base

        echo "Dev=/dev/$base" >> /tmp/link2$base

        echo "Encoding=UTF-8" >> /tmp/link2$base

        echo "Icon=usbpendrive_mount" >> /tmp/link2$base

        echo "MountPoint=/mnt/usb/$base" >> /tmp/link2$base

        echo "Name=$vendor $product [$serial] on $base" >> /tmp/link2$base

        echo "Name[en_US]=$linkname" >> /tmp/link2$base

        echo "ReadOnly=False" >> /tmp/link2$base

        echo "Type=FSDevice" >> /tmp/link2$base

        echo "UnmountIcon=usbpendrive_unmount" >> /tmp/link2$base

        echo "Encoding=UTF-8" >> /tmp/link2$base

    

        #Copy the link file to all users' Desktops 

        for x in /home/*; do cp /tmp/link2$base $x/Desktop/link2$base; done

        ;;

    remove)

        #Retrieve the serial number of the device from the /tmp/$base file

        #--only needed for checking for specific devices

        serial=$(cat /r/info_$base | cut --delim='+' -f5)

        vendor=$(cat /r/info_$base | cut ?delim'+' -f3)

    

        #Unmount the device and cleanup references

        umount -l /r/$base

        rmdir /r/$base

        rm -f /tmp/link2$base

       rm -f /r/info_$base

        #Remove KDE Desktop links created

        for x in /home/*; do rm -f $x/Desktop/link2$base; done

        ;;

    *)

        #Not add? Not remove? Hmm... (grumble grumble)

        ;;

esac

```

Hope this helps someone, I changed a few lines especially 

```
if [[ $base != sd[a-z][1-9]* ]]; then
```

 due to the number of scsi devices I have along with the 3 pen drives.

Also I changed the mount command to 

```
mount -t vfat -o async,gid=100,umask=0707 /dev/$base /r/$base
```

 becuase I would rather have them mounted as VFAT instead of FAT for the long filenames... =)

For reference, my udev rules file lookes like this:

```
#SanDisk Cruzer MINI 1GB

BUS="usb", SYSFS{serial}="SNDK9BAB943875707102", KERNEL="sd?1", OWNER="anheuser50", NAME="%k", SYMLINK="cruzer"

#SanDisk Cruzer Micro 1GB

BUS="usb", SYSFS{serial}="0000290905001520", KERNEL="sd?1", OWNER="anheuser50", NAME="%k", SYMLINK="cruzerMicro"

#Alienware 1G

BUS="usb", SYSFS{idProduct}="6025", SYSFS{idVendor}="0204", KERNEL="sd?1", OWNER="anheuser50", NAME="%k", SYMLINK="alien1g"

#Iomega ZIP Drive

BUS="ide", SYSFS{size}="196608", SYSFS{removable}="1", KERNEL="hd?4", OWNER="anheuser50", NAME="%k", SYMLINK="iomega"

#Dell 2405FPW LCD Monitor card reader

#BUS="usb", SYSFS{product}="USB2223", KERNEL="sd*", OWNER="anheuser50", NAME="%k", SYMLINK="cardreader%n"

#Dell 2405FPW LCD Monitor card reader

BUS="scsi", SYSFS{model}="223 U HS-MS", KERNEL="sd?1", OWNER="anheuser50", NAME="%k", SYMLINK="memorystick"

BUS="scsi", SYSFS{model}="223 U HS-SD/MMC", KERNEL="sd?1", OWNER="anheuser50", NAME="%k", SYMLINK="multimediacard"

BUS="scsi", SYSFS{model}="223 U HS-CF", KERNEL="sd?1", OWNER="anheuser50", NAME="%k", SYMLINK="compactflash"

#Winders XPensinve Drive

BUS="scsi", SYSFS{model}="ST318404LW      ", KERNEL="sd?1", OWNER="anheuser50", NAME="%k", SYMLINK="winxp"
```

Comments are welcome expecially for cleaning up the method used to grab the UDEV SYMLINK stuff.

Thanks =)

----------

## anheuser50

 *urcindalo wrote:*   

> 
> 
> It is a Sony USB 2.0 128 MB flash disk with a memory stick reader incorporated. This is the info from "dmesg":
> 
> ```
> ...

 

Unknown partition table, means the kernel doesn't know what kind of filesystem is on the drive. Is the memory card formatted funky for a mac? or not formatted at all? Have you tried 

```
mkdir /mnt/pendrive

mount -t vfat /dev/sda /mnt/pendrive
```

*Note the missing "1" on sda

Hope this helps.

----------

## AmosMutke

fdisk will also display the partition types if it can identify them. Might want to give that a try. Just replace "hda" with your pendrive device file "sd?".

```
 ~/shawn >fdisk -l /dev/hda

Disk /dev/hda: 60.0 GB, 60011642880 bytes

255 heads, 63 sectors/track, 7296 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

/dev/hda1               1           9       72261   83  Linux

/dev/hda2              10         134     1004062+  82  Linux swap / Solaris

/dev/hda3             135         384     2008125   83  Linux

/dev/hda4             385        7296    55520640   83  Linux

```

----------

## neopath

 *AmosMutke wrote:*   

> 
> 
> [list][*]I have some thoughts about possible problems, or rather a false sense of security, by using automount.  Namely, if you don't first umount your device, it's possible that parts of files are still in a cache and will not be written when you disconnect the device.  Does anyone know if there is a command to flush a possible cache before you pull the device?  say "flush /dev/sda" for example?
> 
> 

 

Would not it be possible to use the sync command? I had a usb drive that did not always get written to due to the async in the mount command. Would running sync before removing the usb drive be a safe way to take care of unmounting when using the script? Right now I have a button on the panel that runs sync in a terminal. I press that button before pulling out the usb drive. The script then unmounts with umount -l.  I think that would take care of the cashe being written, but not sure about how good it is to just unplug the device after that or about the second concern that was given:

 *AmosMutke wrote:*   

> If you haven't written anything to the disk (Let's say you insert your usb memory stick, check the contents, and then remove it), is there any danger of damaging the file system? reason would suggest it's OK.

 

Many thanks for the howto.

----------

## soya

It does not work for uba detected devices.At least for me. I've changed a pair of lines to try to makeit work with uba detected devices but obviusly i've done it wrong cause it does not work. Can someone post what to change to work with such detected devices? thanks in advanced, hope someone can answer  :Smile: 

PD: For all people talking about pmount hald etc i think that they didn't understand the post, the main goal of this post is not to use such extra daemons which have to be started at boot time and we don't want this to be this way. It's very easy to understand, i say this cause in other posts also happens the same. So people wanting to use pmount etc don't have to read neither post here to say that exist other solutions as long as the autor seems to know them and doesn't want to use. Any other post trying to improve this method is welcome. Just an opinion.

----------

## andersbk

Beerman...or anyone that has hotpluging of CF working.

I'm trying to get udev and hotplug working with the built in multireader of my Dell 2405fpw. Udev seems to be working, kindof, but I don't get any events when I plug in a CF card. Help needed.

First, here are the relevent udev rules I'm using:

```

#Dell 2405FPW LCD Monitor card reader

BUS="scsi", SYSFS{model}="223 U HS-CF", KERNEL="sd?1", NAME="%k", SYMLINK="dellcf" 

BUS="scsi", SYSFS{model}="223 U HS-MS", KERNEL="sd?1", NAME="%k", SYMLINK="dellms"

BUS="scsi", SYSFS{model}="223 U HS-SM", KERNEL="sd?1", NAME="%k", SYMLINK="dellsm"

BUS="scsi", SYSFS{model}="223 U HS-SD/MMC", KERNEL="sd?1", NAME="%k", SYMLINK="dellsd"

```

If I turn my monitor on without any card type in the multireader, I get:

```

usb 1-2.1.1.1: new full speed USB device using ehci_hcd and address 43

  Vendor: SMSC      Model: 223 U HS-CF       Rev: 3.60

  Type:   Direct-Access                      ANSI SCSI revision: 00

sd 8:0:0:0: Attached scsi removable disk sdb

  Vendor: SMSC      Model: 223 U HS-MS       Rev: 3.60

  Type:   Direct-Access                      ANSI SCSI revision: 00

sd 8:0:0:1: Attached scsi removable disk sdc

  Vendor: SMSC      Model: 223 U HS-SM       Rev: 3.60

  Type:   Direct-Access                      ANSI SCSI revision: 00

sd 8:0:0:2: Attached scsi removable disk sdd

  Vendor: SMSC      Model: 223 U HS-SD/MMC   Rev: 3.60

  Type:   Direct-Access                      ANSI SCSI revision: 00

sd 8:0:0:3: Attached scsi removable disk sde

usb-storage: device scan complete

# ls -ltr /dev

brw-rw----  1 root disk    8,  16 Feb 22 11:39 sdb

brw-rw----  1 root disk    8,  32 Feb 22 11:39 sdc

brw-rw----  1 root disk    8,  48 Feb 22 11:39 sdd

brw-rw----  1 root disk    8,  64 Feb 22 11:39 sde

```

Next, if I plug in a CF in the reader, nothing happens, nothing changes in dmesg, and no new dev entries are created by udev.

Now if I turn the monitor off, and then power it up again, but this time with a CF card inserted, I get:

```

usb 1-2.1.1.1: new full speed USB device using ehci_hcd and address 49

  Vendor: SMSC      Model: 223 U HS-CF       Rev: 3.60

  Type:   Direct-Access                      ANSI SCSI revision: 00

SCSI device sdb: 2014992 512-byte hdwr sectors (1032 MB)

sdb: Write Protect is off

sdb: Mode Sense: 23 00 00 00

sdb: assuming drive cache: write through

SCSI device sdb: 2014992 512-byte hdwr sectors (1032 MB)

sdb: Write Protect is off

sdb: Mode Sense: 23 00 00 00

sdb: assuming drive cache: write through

 sdb: sdb1

sd 9:0:0:0: Attached scsi removable disk sdb

  Vendor: SMSC      Model: 223 U HS-MS       Rev: 3.60

  Type:   Direct-Access                      ANSI SCSI revision: 00

sd 9:0:0:1: Attached scsi removable disk sdc

  Vendor: SMSC      Model: 223 U HS-SM       Rev: 3.60

  Type:   Direct-Access                      ANSI SCSI revision: 00

sd 9:0:0:2: Attached scsi removable disk sdd

  Vendor: SMSC      Model: 223 U HS-SD/MMC   Rev: 3.60

  Type:   Direct-Access                      ANSI SCSI revision: 00

sd 9:0:0:3: Attached scsi removable disk sde

usb-storage: device scan complete

# ls -ltr /dev

brw-rw----  1 root disk    8,  16 Feb 22 11:36 sdb

brw-rw----  1 root disk    8,  32 Feb 22 11:36 sdc

brw-rw----  1 root disk    8,  48 Feb 22 11:36 sdd

brw-rw----  1 root disk    8,  17 Feb 22 11:36 sdb1

lrwxrwxrwx  1 root root         4 Feb 22 11:36 dellcf -> sdb1

brw-rw----  1 root disk    8,  64 Feb 22 11:36 sde

```

Notice that this time, the CF card is registered and there's now a /dev entry I can use to mount to.

How do I get the multi reader to "post" that a card has been inserted? Am I missing some hotplug thing here? Does anyone know?

TIA,

-anders.

----------

## andersbk

I got it working. My Dell 2405fpw multi-reader is alive!

Here's the details. Basically, I still don't get any "notification" when a CF card is inserted, however, using the following setup works and seems clean to me.

The relevant udev rules for the 2405fpw are now:

```

BUS="scsi", SYSFS{model}="223 U HS-CF",     KERNEL="sd*", NAME="dell/cf%n", OPTIONS="all_partitions"

BUS="scsi", SYSFS{model}="223 U HS-MS",     KERNEL="sd*", NAME="dell/ms%n", OPTIONS="all_partitions"

BUS="scsi", SYSFS{model}="223 U HS-SM",     KERNEL="sd*", NAME="dell/sm%n", OPTIONS="all_partitions"

BUS="scsi", SYSFS{model}="223 U HS-SD/MMC", KERNEL="sd*", NAME="dell/sd%n", OPTIONS="all_partitions"

```

I then decided to use submount (emerge --search submount for details), which incidentally am also using for my CD and DVD drives. Submount is very very nice for removable media. Good man page too.

Anyhow, the following is my fstab entry to mount the CF card, regardless of its presence:

```

/dev/dell/cf1           /mnt/dell/cf1   subfs           fs=vfat,users,exec,umask=0,defaults                0 0

```

Mount shows:

```

> mount

/dev/dell/cf1 on /mnt/dell/cf1 type subfs (rw,nosuid,nodev,fs=vfat,umask=0)

```

So, now I can just insert the card and it's there.

Just be careful to "sync" before removing to make sure all writes have taken.

out.

----------

## Gentree

Just got a link the this guide. Great work. I looked at this a couple of months back and it was taking me too much time. Thanks for this solution.

 *Quote:*   

> Would not it be possible to use the sync command? I had a usb drive that did not always get written to due to the async in the mount command. 

 

certainly, just give it time to run . This may be _several minutes_ if you have written a large file. Watch the access led on the usb device.

sync will sync all mounted fs , not just the usb though that is not usually a problem.

if you user cannot umount the device mounted by root , you can use eject. This is allowed by user and will flush the device and umount it correctly , with the additional effect that this device cannot be re-mounted without being unplugged and recreated by being plugged in. This again is probably what is required.

If you just want to flush use sync command, if you intend to unplug the device use eject.

mount -o sync on usb is BAD for a couple of reasons. On "recent" kernels (>2.6.12 from memory) the new (incorrectly documented ) behaviour of the msdos fs driver hammers that FAT. This can lead to premature device failure on some cheaper devices where this is not buffered internally. 

Even if that is not an issue , write perfomance will be upto 10x (yes and order of magnitude) slower.

I dont have kde on gentoo but I have looked at this in detail on Suse 9.x and 10.0 and it was a mess . Lots of cases where konqueror could not open the device , users could not umount ... generally a non-functional mess. kde said it suse, suse said it's kde /

My basic conclusion is that linux is not windows and trying to make it "just like windows" is detrimental to linux. linux is better because it's different, dont degrade linux, learn to use it.

That's why I like the simple approach of this guide. Just what I want.

I recommend using async and being are aware of the consequences.

The new kernel driver is ugly, slow and badly designed as far as 0_SYNC goes. This definately needs work. There was a flurry of interest earlier this year when this was again brought up on lkml but I am not aware that it got any further. 

The good news is that linux with async can be nearly twice as fast as windows for writting to usb memory devices. My guess is that windows does some flushing automatically that makes it slower than linux async but faster and more reliable than linux sync.

Any one reading this will have no problem coping with async mounting so I say enjoy the speed.

 :Cool: 

----------

## theruck

great tutor thank you very much!

----------

## marcelvs

 *Gentree wrote:*   

> Just got a link the this guide. Great work. I looked at this a couple of months back and it was taking me too much time. Thanks for this solution.
> 
>  *Quote:*   Would not it be possible to use the sync command? I had a usb drive that did not always get written to due to the async in the mount command.  
> 
> certainly, just give it time to run . This may be _several minutes_ if you have written a large file. Watch the access led on the usb device.
> ...

 

I agree with using async is better but as posted before async is not an option when we talk about (v)fat, as we can see typing  'man mount'.

The async option is ignored in certain filesystem in most recent kernels as said here too. My question is: how are you addressing this problem ? I am having to use windows to writte my pendrive and linux only for reading.

----------

## DawgG

the script is GREAT! thx a lot!!!

i modified it a little bit so that only the user logged in to X gets all the desktop-stuff and removed some parts i don't need, but basically it's the same and it works.

one big problem where i use it is its inability to use superfloppy-formatted usb-sticks. i guess thats only about ~5% of all of them, but it still happens. those usb-disks do not have an own partition-table and are mounted as (eg) just /dev/sdb.

the beginning of the script checks for the device-name:

```

#Check to see if the device is a sdX1 device

if [[ $base != sd[a-g][1-9] ]]

then

exit

fi

```

and just exits when a device w/out partition-table is inserted.

just deleting the [1-9]-part produces two device-links /dev/sdb and /dev/sdb1 and the according errors. i'm experimenting on it (without wanting to re-write the whole thing); maybe test-mounting /dev/sdb and /dev/sdb1 and checking which one works would do the trick.

better ideas welcome!

i'll post a solution when i have found it.

----------

## Truzzone

Hi to ALL   :Very Happy: 

I make all changes but at boot I see:

 *Quote:*   

> udevd[978]: add_to_rules: invalid SUBSYSTEM operation
> 
> udevd[978]: add_to_rules: invalid rule '/etc/udev/rules.d/10-local.rules:1'
> 
> udevd[978]: main: the kernel does not support inotify, udevd can't monitor configuration file changes.
> ...

 

The files:

 *Quote:*   

> ls -l /etc/udev/rules.d/10-local.rules
> 
> -rw-r--r-- 1 root root 54  3 feb 16:49 /etc/udev/rules.d/10-local.rules
> 
> cat /etc/udev/rules.d/10-local.rules
> ...

 

When I plugin my usb key with an ext3 partition (that mount without problems by command lines manually):

 *Quote:*   

> Feb  3 16:51:27 fileserver hub 4-0:1.0: over-current change on port 1
> 
> Feb  3 16:51:27 fileserver hub 4-0:1.0: over-current change on port 2
> 
> Feb  3 16:51:28 fileserver hub 1-0:1.0: over-current change on port 5
> ...

 

the results:

 *Quote:*   

> 
> 
> ls -l /mnt/usb/
> 
> total 0
> ...

 

What I do for work?   :Question: 

Please help me.

Best regards,

Truzzone   :Sad: 

----------

## fank

 *Quote:*   

> udevd[978]: main: the kernel does not support inotify, udevd can't monitor configuration file changes.

 

try to enable inotify in your kernel

----------

## Truzzone

Hi,

thanks for your reply.

I built-in the inotify support and at boot I don't received the first error but always not work I see the same lines on the /var/log/message that detect the usb key and it's partition but not mount or create the directory on /mnt/usb   :Crying or Very sad: 

From my .config:

 *Quote:*   

> cat .config | grep INO
> 
> # CONFIG_X86_SPEEDSTEP_CENTRINO is not set
> 
> # CONFIG_USB_DYNAMIC_MINORS is not set
> ...

 

Anyone help me?   :Rolling Eyes: 

Best regards,

Truzzone   :Sad: 

----------

## DawgG

i noticed that the script does not work with udev-111 anymore (after i upgraded from udev-103). i have no custom-made rules in /etc/udev/rules.d, just the script in /etc/dev.d/block/.

with the old (before etc-update) and the new rules with udev-111 the script was not executed (didn't have time to really check WHY, maybe later), with udev-103 it ran again.

----------

## Raffi

 *DawgG wrote:*   

> i noticed that the script does not work with udev-111 anymore (after i upgraded from udev-103). i have no custom-made rules in /etc/udev/rules.d, just the script in /etc/dev.d/block/.
> 
> with the old (before etc-update) and the new rules with udev-111 the script was not executed (didn't have time to really check WHY, maybe later), with udev-103 it ran again.

 

The latest udev has removed all support for /etc/dev.d and /etc/hotplug.d. As of yet, I don't know where to move things to get the functionality back. Anyone have any ideas?

----------

## Raffi

OK. I have a quick answer to how to deal with the changes to udev

If you had a script in /etc/dev.d/block/ called usbauto.dev, you can copy it to /etc/udev/scripts/usbauto.sh and then create a a file in /etc/udev/rules.d called 99-thumb.rules with the content of

```

KERNEL=="sd[a-z]*", SUBSYSTEM=="block", RUN+="/etc/udev/scripts/usbauto.sh %k"

```

This should pretty much duplicate the functionality you had in dev.d. At least it worked for me.

----------

