USB flash drive

Carl waldbie at
Fri May 6 03:50:36 UTC 2005

On Wednesday 04 May 2005 5:23 pm, sara vasquez wrote:
> Ok so I am back to kubuntu after trying gnome and xfce but now whe I
> plug my usb flash drive I don't get it to automatically go to the
> desktop how do I get it back that was one of the things i loved about
> kubuntu.

I had noticed how that worked under Gnome back in Warty, but not under KDE at 
the time.  I cobbled together my own hotplug script, which works pretty well, 
though I never did get it to successfully remove the desktop icon when my 
flashdrive was unplugged.

For those who are interested, this is what I did:

At the konsole:

$ lsusb

Take note of the output.  Now plug in your flash drive and issue the command 
again.  There should be an extra line that corresponds to your USB device.  
There may even be a description that identifies it.  There is an ID number 
for the device in the format ####:####.  You need to record that ID number 
for later.

Now you should have a directory called /etc/hotplug/usb.  It may or may not 
already have some stuff in there.  I needed two add two files to hook my 
drive into the hotplug system.  The first is a file that you call something 
like "flashdrive.usermap", and the second is a matching script that would 
just be called "flashdrive" (substitute "ipod", "pendrive", or whatever).

The flashdrive.usermap file should contain a line like this:

flashdrive 0x0000 0x05dc 0x0080 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 

The first field is the name of the other file (the script file).  The second 
field is just zeroes.  The 3rd and 4th fields match the pieces of the ID you 
copied down before.  In thi example, my usb drive had an ID 05dc:0080.  The 
"0x" prefixes on each number is notation to indicate these numbers are 
hexidecimal numbers (base 16).  The rest of the fields are just zeroes.

Now the second file is a script you write to do something when the hotplug 
system detects your device going in or out of the usb port.  I wrote the 
following little script, which creates a device icon on my desktop:

---------- start script "flashdrive" ---------------------

#If the USB device is not plugged in (e.g. during bootup), then skip trying to 
perform an action.
if [ -z "$(lsusb | grep 05dc:0080)" ]; then #usb device ID goes here.
    exit 0
if [ "$ACTION" = "add" ]; then
    #Sleep until the device becomes available.
    while [ ! -e /dev/sdb1 ]; do
        sleep 1

    #Make the mount point and mount if it does not exist.
    mkdir -p /media/sdb1
    mount /dev/sdb1

    #Create the desktop icons on each user's desktop.
    for desktop in /home/*/Desktop
        cat > "$desktop/USB Drive.desktop" << DRIVEFILE
[Desktop Entry]
---------end script "flashdrive" ------------

The 5th line in the script reads:

if [ -z "$(lsusb | grep 05dc:0080)" ]; then #usb device ID goes here.

And you need to replace the 05dc:0080 with your device ID.  This part of the 
script makes sure the device is actually plugged in before trying to do 
anything else-- the hotplug system fires it when you boot up, even though the 
drive isn't plugged in, so you need to ignore it or you could end up hanging 
for a while.

The next part of the script just makes a mount point and mounts the drive.  
You may need to tweak this if your device is not mounted on /dev/sdb1.  The 
last part of the script creates the device icon file on your desktop.

Not exactly the most elegant solution in the world, but DYI folks may want to 
give it a try.  Maybe someone will come up with something better that doesn't 
require hard coding in the specific device IDs...

Anyway, hope that is some help.

Carl Waldbieser

More information about the kubuntu-users mailing list