# USB Stick Automounter

## genotix

Hey all,

Because I wasn't able to find a automounting program for an simple USB stick I created one myself in a simple script.

I think the code below says it all.

```

#!/bin/sh

# Shell script made by Eric Oud Ammerveld

# Dislclaimer : Should this script f*ck up your computer,

#               I'm not the one to blame.

#

#

                                                                                                      

# Run for ever

while [ 1 ]

do

                                                                                                      

#--------- CONFIG ------------#

#location of the stick (usually sd[x])

usbstick="sdb"

                                                                                                      

usbpart1="/dev/"$usbstick"1" #Partition 1 on the stick

mntpth1="/mnt/usb1" #Mount Path 1

fs1="vfat" #File system 1

                                                                                                      

usbpart2="/dev/"$usbstick"5" #Partition 2 on the stick (located in extended partition)

mntpth2="/mnt/usb2" #Mount Path 2

fs2="vfat" #File system 2 (vfat=dos/fat16/fat32 , ntfs ;type man mount for more fs)

#--------- CONFIG END---------#

                                                                                                      

mntd="`/bin/mount | /bin/grep -c $usbstick`"

checkstick="`/bin/ls /dev |/bin/grep -c $usbstick`"

                                                                                                      

#/bin/echo "Amount of sticks :$checkstick" # A few debug rules

#/bin/echo "Mounted sticks :$mntd"

#/bin/echo "Partition 1: "$usbpart1

#/bin/echo "Partition 2: "$usbpart2

                                                                                                      

if [ $mntd == 0 ] # If the stick is not mounted...

then

  if [ $checkstick == 4 ] # Though the stick is detected...

  then

    /bin/wall "USB Stick found! Mounting"

    /bin/mount $usbpart1 $mntpth1 -t $fs1 # Mount it

    /bin/mount $usbpart2 $mntpth2 -t $fs2 # Mount it

  fi

else

  if [ $checkstick == 0 ] # If the stick isn't present...

  then

    /bin/wall "USB Stick removed! Unmounting"

    /bin/umount -f $mntpth1 > /dev/null # Unmount it

    /bin/umount -f $mntpth2 > /dev/null # Unmount it

  fi

fi

/bin/sleep 5 # Short time-out to reduce the load

done

```

Please do inform me if you have a better way or if you have

adapted the script.

Regards,

-=@@D=-

----------

## taskara

what are u calling your script?

also r u using usb hotplug? it's very handy if ur not..

----------

## genotix

 *taskara wrote:*   

> what are u calling your script?
> 
> also r u using usb hotplug? it's very handy if ur not..

 

I called my script "usbstickd" (USB Stick Daemon)

I enabled hotplug and of course generic SCSI stuph in my kernel(/dev/sd typically indicates a SCSI device which your USB stick needs to emulate it's presence).

(Tested with kernel 2.6.0-test 11)

use "dmesg" to check weather the kernel detects your USB stick.

(a sd[x] device will be found)

----------

## oisch

I enabled supermount in the kernel and used the following entry for automounting:

```
none                   /mnt/usbstick   supermount      dev=/dev/sda1   0 0
```

This works very good, but I'm not sure if the data integrity is ensured. When I write on my usb stick I have a delay of some seconds and unmounting is denied.

So if I just unplug the usb stick it may be that not all data has been transfered.

So I prefer so old mount / umount way to acces the usb stick.

Your script seems to unmount the stick when it's been removed. Do you think it is save to remove it while it is mounted?

----------

## taskara

I also mount manually.. and I don't think it's a good idea to unplug it b4 umounting has finished.. usb devices have delay write, so ur data may be corrupt..

----------

## genotix

 *oisch wrote:*   

> I enabled supermount in the kernel and used the following entry for automounting:
> 
> ```
> none                   /mnt/usbstick   supermount      dev=/dev/sda1   0 0
> ```
> ...

 

Technically it's not very safe to remove a mounted device, especially not when it is in use.

It's just a thing you should keep in mind and try to avoid while using the USB stick.

If the device isn't present anymore the daemon will detect within 5 seconds and force a unmount.

I don't know what exactly will happen when you unplug the stick while writing to it but even

if it stays mounted and the write buffer is still filled I think the data in the write buffer will be blanked (output /dev/null) as soon as module that contols the buffer detects that it cannot write to the device.

If the buffer module is activated and deactivated controlled by the mount of the device the buffer probably will be wiped out.  :Sad: 

If the buffer module is a kernel module it's also possible that the buffer will be wiped clean (for security and robustness reasons)

Because if you would insert another USB Stick while the buffer is still filled it would probably lead to even more complications.

The way linux is programmed I think every programmer would like to avoid complications like that because every situation possible should bel programmed otherwise.

I think in both cases, the worst thing you can do is unplug your stick while writing to it.

I wonder how supermount handles the mount and unmount story when the buffer is filled and the stick is unplugged.   :Question: 

----------

## genotix

 *taskara wrote:*   

> I also mount manually.. and I don't think it's a good idea to unplug it b4 umounting has finished.. usb devices have delay write, so ur data may be corrupt..

 

I think that in both cases the buffer will be dropped so a corrupt file(system) would be inevitable in both cases.   :Rolling Eyes: 

----------

## taskara

 *genotix wrote:*   

>  *taskara wrote:*   I also mount manually.. and I don't think it's a good idea to unplug it b4 umounting has finished.. usb devices have delay write, so ur data may be corrupt.. 
> 
> I think that in both cases the buffer will be dropped so a corrupt file(system) would be inevitable in both cases.  

 

which two cases?

----------

## genotix

 *taskara wrote:*   

>  *genotix wrote:*    *taskara wrote:*   I also mount manually.. and I don't think it's a good idea to unplug it b4 umounting has finished.. usb devices have delay write, so ur data may be corrupt.. 
> 
> I think that in both cases the buffer will be dropped so a corrupt file(system) would be inevitable in both cases.   
> 
> which two cases?

 

Case 1 : supermount 

Case 2 : usbstickd

----------

## Mankane

 *oisch wrote:*   

> I enabled supermount in the kernel and used the following entry for automounting:
> 
> ```
> none                   /mnt/usbstick   supermount      dev=/dev/sda1   0 0
> ```
> ...

 

If you're doing any kind of unplugging before umounting, you should really be specifying the sync option.  By not doing so you're risking loss of data and filesystem corruption.  See 'man mount' for more info about the sync option.

That said, Windows warns you about stopping the device if you just unplug and manually umounting is along those same lines (windows also disables asynchronous transfers for jump drives as well in 2000 and xp, but still recommends stopping the device).

----------

## genotix

 *Mankane wrote:*   

>  *oisch wrote:*   I enabled supermount in the kernel and used the following entry for automounting:
> 
> ```
> none                   /mnt/usbstick   supermount      dev=/dev/sda1   0 0
> ```
> ...

 

In one of the first topics supermount was also mentioned.

Unfortunately I have not found the emerge package for it.

emerge -s supermount results in no download options.

I was thinking about a method that writes the table of contents later to the stick but this should be programmed on a lower level.

The script above was just something I needed but didn't find in any packages.

My method works good for me only you should keep in mind buffering is used.

Could you help me out with the sync option?

----------

## oisch

 *Quote:*   

> In one of the first topics supermount was also mentioned. 
> 
> Unfortunately I have not found the emerge package for it. 
> 
> emerge -s supermount results in no download options. 

 

You need to patch your kernel to use supermount. gentoo-dev-sources already have this patch applied automatically. You should find the supermount option under Filesystems.

I use supermount for CD and Floppy mounting/unmounting.

Using the SYNC option for an usb stick really slows down writing, so that ain't an option, too. I'll stick to manual mount/unmounting, as it really isn't so much work.

----------

## tuxlover

 *oisch wrote:*   

> I enabled supermount in the kernel and used the following entry for automounting:
> 
> ```
> none                   /mnt/usbstick   supermount      dev=/dev/sda1   0 0
> ```
> ...

 

As far as I know, supermount is supposed to handle just that - data integrity for removable media. From the supermount homepage:

"Supermount detects when you have finished activity on the subfs, and will flush all buffers to the disk before completing the operation. So, if you copy a file onto a supermounted floppy disk, the data will all be written to disk before the "cp" command finishes. When the command does complete, it will be safe to remove the disk."

Now I'm confused... What is supermount-ng? Is it different from supermount...?

----------

## bedouin

#1: Data integrity

If you can live with really poor write performance then mount the device in synchronous mode:

```

/dev/sda1   /mnt/usb-storage      auto      sync,dirsync   0   0

```

That way data gets written to the stick immediately, your application will be blocked until the write is complete. Of course writing is quite slow, so don't use that for swap  :Wink: 

On the other hand, there will be no stale buffers. It's OK to unplug whenever the stick is not being accessed.

The above line is the basis for everything described below.

#2: Automounting

I don't know about supermount (never used it) but for my stick I'm using the standard hotplug infrastructure: enable hotplug in the kernel and emerge sys-apps/hotplug. This will (among other things) give you scripts in /etc/hotplug.d and /etc/hotplug/<SUBSYSTEM> (for example: /etc/hotplug/usb)

The scripts in /etc/hotplug.d (usually just "default") will switch incoming hotplug events by subsystem, you'd modify that if have a new kind of hotplug system. Not interesting.

Now in /etc/hotplug/usb you would place scripts with the name of a kernel module, e.g. /etc/hotplug/usb/usb-storage, looking like this:

```

#!/bin/bash

# NOTE: to make this work, you *MUST* use the options "sync" and "dirsync" in your /etc/fstab !!!

#       otherwise you *WILL* lose data

#create a script to undo our actions upon removal

echo "#!/bin/bash" > $REMOVER

chmod a+x $REMOVER

if [ "$INTERFACE" = "8/6/80" ]; then

  mount /mnt/usb-storage

  echo "umount -f /mnt/usb-storage" >> $REMOVER

fi

```

This script will be executed when and if

* there is a USB hotplug event

* this event has already caused the module usb-storage.o to be loaded

The environment variables $REMOVER, $INTERFACE and $PRODUCT (and some others I don't remember) will be set by the hotplug infrastructure. The check for $INTERFACE=8/60/80 ensures that we are really dealing with a storage device. If your storage device needs special handling you might also check $PRODUCT (check your hotplug logs to find out your stick's product ID).

The file $REMOVER (if it exists) will be executed upon removal of the exact device it was created for.

The above script is all fine and dandy if you only have one user, one stick, and one location to mount it on. And else  :Question: 

Well, try the following script for /etc/hotplug/usb/usb-storage, which is an extension of the previous example:

```

#!/bin/bash

# NOTE: to make this work, you *MUST* use the options "sync" and "dirsync" in your /etc/fstab !!!

#       otherwise you *WILL* lose data

#create a script to undo our actions upon removal

echo "#!/bin/bash" > $REMOVER

chmod a+x $REMOVER

# INTERFACE=8/6/80 means "mass storage device"

if [ "$INTERFACE" = "8/6/80" ]; then

  mount /mnt/usb-storage

  HOSTNAME=`/bin/hostname`

  OWNER=`cat /mnt/usb-storage/.owner.$HOSTNAME`

  umount /mnt/usb-storage

  #remount as correct user

  sudo -u $OWNER mount /mnt/usb-storage

  # apply directory mappings

  HOMEDIR=`grep $OWNER /etc/passwd | awk -F: '{ print $6 }' `

  echo $HOMEDIR

  awk --assign homedir=$HOMEDIR --assign remover=$REMOVER -F= '

        {

                system( "mount --bind /mnt/usb-storage/" $1 " " homedir "/" $2 )

                system( "echo umount -f " homedir "/" $2 " >> " remover )

        }

        ' /mnt/usb-storage/.directory-map.$HOSTNAME

  echo "umount -f /mnt/usb-storage" >> $REMOVER

fi

```

This script requires

* Kernel 2.4 or higher

* file on the stick: .owner.$HOSTNAME

* file on the stick: .directory-map.$HOSTNAME

* every usb-stick-user must be able to "sudo mount"

* every usb-stick-user must be able to "mount /mnt/usb-stick" without resorting to sudo

How it works:

1) Mount the stick as root an read the file ".owner.HOSTNAME", where HOSTNAME is the local name. This file contains only a username; you can have a different username on each machine you use the stick on (very useful for taking stuff home from work!)

2) Unmount the root-mounted stick

3) Mount the stick again at the same position, but this time as the user; this action will give you correct permissions on the stick (extremely useful if you're not using FAT)

4) Now read ".directory-map.HOSTNAME", which again allows for one mapping per host. Every line in there is of the form "STICKDIR  TARGETDIR". No wildcards/regexes allowed!

5) Bind /mnt/usb-stick/STICK-DIR to ~/TARGETDIR (this is like mounting, just different  :Wink:  )

6) During all of that, keep the remove script up-to-date

The result: you can keep multiple directories on your stick (e.g. ~/Mail, ~/News, ~/ICQ/Logs), and have them mounted to the right directory automagically. You may remove the stick whenever it's not being accessed. You can do so on multiple machines, regardless of differences in username and layout of your homedir.

Hope that helps - have fun moving your data  :Cool: 

 :Exclamation:  WARNING: I've tried to keep out at least some obvious security holes. However, it's your machine and they're your users. Trust them or don't, but don't blame me if you get f*ucked using these scripts. The biggest flaw is keeping the user and directory mappings on the stick. If you know of any way to get around that please speak up.

----------

## genotix

 *bedouin wrote:*   

> #1: Data integrity
> 
> If you can live with really poor write performance then mount the device in synchronous mode:
> 
> ```
> ...

 

Looks like a nice script, you've got there.

But I'm affraid I'll keep using mine  :Smile: , it's a bit less complicated.

Thanx for the feedback though.. I you don't mind I'll use some of your idea's in a next script.

Regards,

@@D

----------

## bedouin

Thanks for the kind words  :Smile: 

I do admit that for single-user/single-host scenarios I'm committing serious overkill, but when writing it I had the explicit requirement of multi-user/multi-host. 

To give a DB example: nobody said you couldn't use Oracle for a less-than-20-entries-ever guestbook on your homepage  :Twisted Evil: 

Of course you're free to use everything in there, I'd be delighted to see your next version.

----------

## RedBeard0531

In this thread I posted about a solution that uses autofs/automountd. automounter unmounts the fs it it goes unaccessed for a set period of time. for a pen drive you can set it to 1sec for maximum safty. Will post more later, gotta run.

https://forums.gentoo.org/viewtopic.php?t=102158

----------

## genotix

Okey, well I adjusted the script a bit so it would fit my needs even more.

The stick is accessible by (config) definable users of the machine 

(I use all the users gid=100, don't! if you are on a multiuser system!)

I am the only one that uses the stick on my computer, you can set up your own group and user in the top of the script (config part)

I inserted the sync and dirsync option that was advised by bedouin.

I hope it gives a bit of stability and robustness to the write process.

```

#!/bin/sh

# Shell script made by Eric Oud Ammerveld

# Dislclaimer : Should this script damage up your computer or stick in any way,

#               I'm not the one to blame.

#

#

# Run for ever

while [ 1 ] # Don't touch.. this is for the deamon to repeat

do          # start of while loop

 

#--------- CONFIG ------------#

#location of the stick (usually sd[x])

usbstick="sdb"

 

usbpart1="/dev/"$usbstick"1" #Partition 1 on the stick

mntpth1="/mnt/usb1" #Mount Path 1

fs1="vfat" #File system 1

 

usbpart2="/dev/"$usbstick"5" #Partition 2 on the stick (located in extended partition)

mntpth2="/mnt/usb2" #Mount Path 2

fs2="vfat" #File system 2 (vfat=dos/fat16/fat32 , ntfs ;type man mount for more fs)

 

allowedusr="1000"       #Allowed user to access stick

allowedgrp="100"        #Allowed group to access stick

#--------- CONFIG END---------#

 

 

#--------- SCRIPT STUPH BEGINNING ---------#

mntd="`/bin/mount | /bin/grep -c $usbstick`"

checkstick="`/bin/ls /dev |/bin/grep -c $usbstick`"

 

#/bin/echo "Amount of sticks :$checkstick" # A few debug rules

#/bin/echo "Mounted sticks :$mntd"

#/bin/echo "Partition 1: "$usbpart1

#/bin/echo "Partition 2: "$usbpart2

 

if [ $mntd == 0 ] # If the stick is not mounted...

then

  if [ $checkstick == 4 ] # Though the stick is detected...

  then

#    /bin/wall "USB Stick found! Mounting" # Globally written mounting message

    /bin/mount -o sync,dirsync,uid=$allowedusr,gid=$allowedgrp $usbpart1 $mntpth1 -t $fs1 # Mount it

    /bin/mount -o sync,dirsync,uid=$allowedusr,gid=$allowedgrp $usbpart2 $mntpth2 -t $fs2 # Mount it

  fi

else # is the stick IS mounted

  if [ $checkstick == 0 ] # If the stick isn't present...

  then

#    /bin/wall "USB Stick removed! Unmounting" #Globally wrutten unmounting message

    /bin/umount -f $mntpth1 > /dev/null # Unmount it

    /bin/umount -f $mntpth2 > /dev/null # Unmount it

  fi

fi

/bin/sleep 5 # Short time-out to reduce the load

done # end of while loop

#--------- SCRIPT STUPH END ---------#

```

----------

## wmertens

This is what I made last year: http://sourceforge.net/mailarchive/message.php?msg_id=2673551

It works with Linux 2.4, and it doesn't need to know where the device appears. It even works when you plug in devices that have more than one partition, and it will mount them in a nice directory derived from the device name. In the story there's also a link to someone else's script that also generates a desktop entry pointing to the mount.

And recently I created a script for Linux 2.6. It works very well, much faster than the 2.4 version, and much more robust, too.

```

#!/bin/sh

#

# Automount hotplugged block devices, by Wout Mertens.

#

# Linux v2.6 version

# This script is released under version 2 of the GPL.

# To install this, either make it /sbin/hotplug, or, if you have

# linux-hotplug installed, make it /etc/hotplug.d/block/automount.hotplug .

# It needs to be executable. I tested it with busybox's ash.

#

# The devices will be mounted for the console user if applicable.

#

# To work, this needs:

# - v2.6 kernel support:

#   - hotplugging, /proc mounted, /sys mounted

#   - filesystems that will be mounted, like vfat

# - sh, echo, sed, stat or ls, getent or grep passwd,

#   mount, umount, mkdir, rmdir. logger is used if there.

#

# Possible improvements:

# - Create a desktop entry for the device.

# - Call a notifier script when mounting/unmounting. This could create the

#   desktop entry by itself, and could call user scripts from their homedir

# - Mount as supermount if available. This will improve behaviour for

#   synchronous writes and hanging mounts, although umount -lf already

#   does a good job.

# - Detect filesystem on device, so filesystems that don't need a mounting

#   user can skip user detection.

# Debugging

# set -xv

# exec > /tmp/hotplug.$$ 2>&1

PATH="/bin:/sbin:/usr/bin:/usr/sbin"

export PATH

# We only handle block device messages

[ "$1" != "block" ] && exit 0

# Proclaim stuff to the world

mesg () {

        logger -t $0 "$*"

}

# Is path already mounted?

is_path_mounted () {

        while read dev path rest

        do

                if [ "$path" = "$1" ]

                then

                        return 0

                fi

        done < /proc/self/mounts

        return 1

}

# Get mounted path for a device

get_mounted_path () {

        while read dev path rest

        do

                if [ "$dev" = "$1" ]

                then

                        echo "$path"

                        return 0

                fi

        done < /proc/self/mounts

        return 1

}

# Wait for a file to appear

wait_for () {

        local count=0

        while [ $count -lt 10 ] && [ ! -e "$1" ]

        do

                sleep 1

        done

        [ $count -eq 10 ] && return 1

        return 0

}

# Remove strange characters from a filename

clean_filename () {

        # Note the lack of quotes around $1, this strips off spaces.

        echo $1 | sed 's/[ /?*\"<>]/_/g'

}

# Figure out the device to mount

set `echo $DEVPATH | sed 's/\// /g'`

[ $# -ne 3 ] && exit 0

DEVICE=/dev/$3

if [ "$ACTION" = "remove" ]

then

        # Is it ours?

        dir=`get_mounted_path $DEVICE`

        echo "$dir" | grep -q ^/mnt/usb || exit 0

        # Unmount it

        [ -d "$dir" ] || exit 1

        umount -lf "$dir"

        rmdir "$dir"

        exit 1

fi

if [ "$ACTION" = "add" ]

then

        # Is it a usb device? Exit if not.

        ls -l /sys/$1/$2/device | sed 's/^.* -> //g' | grep -q usb || exit 0

        # Mount it

        # Make sure we have support for vfat

        modprobe -q vfat

        wait_for /sys/$1/$2/device/vendor || exit 1

        # Find the name

        PRODUCT=`cat /sys/$1/$2/device/model`

        if [ -z "$PRODUCT" ]

        then

                PRODUCT=generic

        fi

        # Find out where we mount it

        MOUNTPATH=/mnt/usb/`clean_filename "$PRODUCT"`

        if is_path_mounted "$MOUNTPATH"

        then

                count=1

                while is_path_mounted "${MOUNTPATH}_${count}"

                do

                        count=$(( $count + 1 ))

                done

                MOUNTPATH="${MOUNTPATH}_${count}"

        fi

        # Make sure it's a directory

        if [ -e "$MOUNTPATH" ]

        then

                if [ ! -d "$MOUNTPATH" ]

                then

                        mesg "$MOUNTPATH exists but is not a directory"

                        exit 1

                fi

        else

                mkdir -p "$MOUNTPATH"

                if [ $? -ne 0 ]

                then

                        mesg "Could not create mountpoint $MOUNTPATH"

                        exit 1

                fi

        fi

        # Find out who we are going to mount it as

        CONSOLEUSER=`stat -c%U /dev/console 2>/dev/null`

        if [ -z "$CONSOLEUSER" ]

        then

                set `ls -l /dev/console`

                CONSOLEUSER=$3

        fi

        [ -n "$CONSOLEUSER" ] || CONSOLEUSER=root

        PASSWD=`getent passwd $CONSOLEUSER 2>/dev/null`

        if [ -z "$PASSWD" ]

        then

                PASSWD=`grep "$CONSOLEUSER" /etc/passwd||grep root /etc/passwd`

        fi

        if [ -z "$PASSWD" ]

        then

                mesg "Could not get password entry for $CONSOLEUSER"

                exit 1

        fi

        set `echo $PASSWD | sed 's/:/ /g'`

        if [ $# -lt 4 ]

        then

                mesg "Bad password entry for $CONSOLEUSER"

                exit 1

        fi

        # These options should prevent abuse and make it writeable for the

        # console user.

        MOUNTOPTS="-s -osync,dirsync,nosuid,umask=077,uid=$3,gid=$4"

        mesg Mounting $DEVICE on $MOUNTPATH, options $MOUNTOPTS

        mount $MOUNTOPTS $DEVICE $MOUNTPATH

fi

```

Enjoy   :Wink: 

Update: Download the latest version at http://dev.gentoo.org/~wmertens/automount.hotplugLast edited by wmertens on Wed Dec 17, 2003 8:57 am; edited 1 time in total

----------

## genotix

Amai!! Dat is een behoorlijke lap code  :Wink:  >wmertens

(Dutch for something like : Cribbeley Sjizzels, that's quite a typing work  :Wink: )

----------

## wmertens

Cut'n'paste is your friend   :Cool: 

----------

## genotix

 *wmertens wrote:*   

> Cut'n'paste is your friend  

 

That's not what they teach us @ school  :Wink: 

----------

## eonnen

A thought and a question:

Thought: You probably want to mount with a noatime option. You have about 10,000 erase/write cycles on typical flash media and when mounted with atime, a simple 'ls' will result in an atime update shortening the life of your device. See http://www.linuxjournal.com/article.php?sid=6867 for a better explanation.

Question: Are you certain that sync and dirsync are honored by vfat? I'm looking at the man for mount and it says only ext2/ext3/ufs honor sync options. Just curious as I'm not using them as mount options for my drive and I haven't had a problem _yet_.

----------

## nukem996

I just got a memory stick today and configed it under redhat, still havnt set it up on my gentoo system. You need to enable USB mass storage and some more things in your kernel and add to fstab

/dev/sda1     /mnt/usb/     auto    noauto,user,rw    0 0

----------

## wmertens

 *eonnen wrote:*   

> 
> 
> Thought: You probably want to mount with a noatime option. You have about 10,000 erase/write cycles on typical flash media and when mounted with atime, a simple 'ls' will result in an atime update shortening the life of your device. See http://www.linuxjournal.com/article.php?sid=6867 for a better explanation.

 

Good point. Adding that to my script; you can download the latest version at http://dev.gentoo.org/~wmertens/automount.hotplug

 *eonnen wrote:*   

> 
> 
> Question: Are you certain that sync and dirsync are honored by vfat? I'm looking at the man for mount and it says only ext2/ext3/ufs honor sync options. Just curious as I'm not using them as mount options for my drive and I haven't had a problem _yet_.

 

Well, sync is honoured but dirsync isn't. Sigh. Type "sync" a few times before you unplug it, or get supermount_ng installed. It's ported to 2.6: http://supermount-ng.sourceforge.net/ .

I added a test, but I didn't test if my script actually works with it, though   :Embarassed: 

----------

## drekka

Hi guys, interesting discussion (I'm still waiding through it  :Wink:  But I thought I might throw a small fly in your ointment  :Wink: 

I have a Sony DSC which hooks up via USB and appears as another drive just like my TwinMos USB stick. The TwinMos always appears on 4 (i.e. sda4) and the Sony always appears on 1 (i.e. sda1)  However !!! after booting the first device plugged in get sda and the second gets sdb. So depending on the order I attach the devices, the TwinMos can be either sda4 or sdb4 and the camera can be either sda1 or sdb1. 

As you might guess this makes doing fstab entries a little tricky, not to mention any auto mounters. How do your scripts handle the possibility of different sd's? I also presume that as you keep adding devices, they would appear as sdc, sdd, sde, etc. How would you work out what mount points to assign ? and what about if you always wanted the camera on /mnt/usb/camera regardless of whether it was on sda1 or sdb1 ???

he he he  :Wink: 

cio

Derek.

----------

## genotix

 *drekka wrote:*   

> Hi guys, interesting discussion (I'm still waiding through it  But I thought I might throw a small fly in your ointment 
> 
> I have a Sony DSC which hooks up via USB and appears as another drive just like my TwinMos USB stick. The TwinMos always appears on 4 (i.e. sda4) and the Sony always appears on 1 (i.e. sda1)  However !!! after booting the first device plugged in get sda and the second gets sdb. So depending on the order I attach the devices, the TwinMos can be either sda4 or sdb4 and the camera can be either sda1 or sdb1. 
> 
> As you might guess this makes doing fstab entries a little tricky, not to mention any auto mounters. How do your scripts handle the possibility of different sd's? 

 

Look at the line : usbstick="sdb"

As soon as you plug in your USB stick / card the hotplug handler adds a device to the /dev/ directory.

In your case sda1/4 or sdb1/4.

The usbstick variable is the "trigger" that makes the stick mount or unmount. It simply looks at the /dev dir and if e.g. /dev/sdb exists the stick is detected and will try to mount your device.

So : Adapt the script to your environment so you will be able to use it.

 *Quote:*   

> I also presume that as you keep adding devices, they would appear as sdc, sdd, sde, etc. How would you work out what mount points to assign ? and what about if you always wanted the camera on /mnt/usb/camera regardless of whether it was on sda1 or sdb1 ???
> 
> he he he 
> 
> cio
> ...

 

----------

## wmertens

 *drekka wrote:*   

> 
> 
> I have a Sony DSC which hooks up via USB and appears as another drive just like my TwinMos USB stick. The TwinMos always appears on 4 (i.e. sda4) and the Sony always appears on 1 (i.e. sda1)  However !!! after booting the first device plugged in get sda and the second gets sdb. So depending on the order I attach the devices, the TwinMos can be either sda4 or sdb4 and the camera can be either sda1 or sdb1. 
> 
> As you might guess this makes doing fstab entries a little tricky, not to mention any auto mounters. How do your scripts handle the possibility of different sd's? I also presume that as you keep adding devices, they would appear as sdc, sdd, sde, etc. How would you work out what mount points to assign ? and what about if you always wanted the camera on /mnt/usb/camera regardless of whether it was on sda1 or sdb1 ???
> ...

 

Well, if you use Linux 2.6, use my script 

http://dev.gentoo.org/~wmertens/automount.hotplug  :Wink: 

If you use 2.4, you'll have to use the inferior http://dev.gentoo.org/~wmertens/automount_usb-linux2.4

Read the scripts for how to install them. Both of them handle any device, any partition, automatically, and they always get mounted at /mnt/usb/<devicename>   :Cool: 

Cheers,

Wout.

----------

## sasquach

 :Twisted Evil:  I use mtools to grab the photos from my camera... no mounting needed...

----------

## genotix

 *sasquach wrote:*   

>  I use mtools to grab the photos from my camera... no mounting needed...

 

Isn't that an acquire device tool?   :Idea: 

----------

## sasquach

I don't know a term like "acquire device tool", sorry...

Mtools reads media by interpreting the filesystem itself, so for that tool, block access to the device is all it needs.

----------

## agrippa_cash

 *Quote:*   

> I don't know a term like "acquire device tool", sorry... 

   You sound like 'Dr. Sbaitso.' (or an old Sierra game).

I just installed the script, and when I plug in my pendrive I get /mnt/usb/generic and no subdirectories or files.  Any thoughts?

----------

## sasquach

 *agrippa_cash wrote:*   

> You sound like 'Dr. Sbaitso.' (or an old Sierra game).

 

My favourite game is linux, I don't know that "Dr." and that game.

And I may sound strange because english is not my native tounge.

----------

## wmertens

 *agrippa_cash wrote:*   

> 
> 
> I just installed the script, and when I plug in my pendrive I get /mnt/usb/generic and no subdirectories or files.  Any thoughts?

 

Hmmm... What is the output of "mount" ?

and do you know what filesystem is on the pendrive? I basically only tested it with vfat....

The generic indicates that it couldn't find a product name. Maybe there's a problem with your pendrive? Can you mount it manually?

If you want to know what's going on when it tries to mount the pendrive, add the following lines near the beginning of the script:

```
set -xv

exec >/tmp/automount_output.$$ 2>&1

```

This will make a bunch of /tmp/automount_output.PID files, one for each invocation of the script. You can take a look at these to see what is not working.

Cheers,

Wout.

----------

## genotix

 *agrippa_cash wrote:*   

>  *Quote:*   I don't know a term like "acquire device tool", sorry...    You sound like 'Dr. Sbaitso.' (or an old Sierra game).
> 
> I just installed the script, and when I plug in my pendrive I get /mnt/usb/generic and no subdirectories or files.  Any thoughts?

 

Use my script   :Laughing: 

It's a bit more simple.

----------

## SNo0py

Hm, I have a very strange problem. I mount my USB Stick, I can write files and I can remount the stick. But as soon as I unplug the stick (after unmounting of course) from the computer, he looses all his files  :Sad: 

No way to store anything on the Stick... All files are lost... 

Any solution?

Thanks!

----------

## wmertens

 *SNo0py wrote:*   

> Hm, I have a very strange problem. I mount my USB Stick, I can write files and I can remount the stick. But as soon as I unplug the stick (after unmounting of course) from the computer, he looses all his files 
> 
> No way to store anything on the Stick... All files are lost... 
> 
> 

 

I've seen this happen with Linux 2.4.21 and below. Try using 2.4.22 or up.

And try typing "sync" a lot  :Smile: 

Wout.

----------

## agrippa_cash

sasquach: Your English is perfecty acceptable, and very few people played Dr. Sbaitso (as I remember it came free with Sound Blasters once upon a time), the Kings Quest series of Sierra Games was a lot of fun back then too.<p>That aside I am going to reinstall hotplug, at the moment I don't get any "/tmp/automount_output." and my HOTPLUG hangs when I try to restart the service.  I suspect that love-sources omission of supermount support is the culpret.  So I'll give it another shot.

----------

## SNo0py

 *wmertens wrote:*   

> 
> 
> I've seen this happen with Linux 2.4.21 and below. Try using 2.4.22 or up.
> 
> And try typing "sync" a lot 
> ...

 

WOW!!! Thank you!!!! Now everything works fine!!!

----------

## wmertens

 *agrippa_cash wrote:*   

> That aside I am going to reinstall hotplug, at the moment I don't get any "/tmp/automount_output." and my HOTPLUG hangs when I try to restart the service.  I suspect that love-sources omission of supermount support is the culpret.  So I'll give it another shot.

 

The automount_output is only there when you put the code i gave some posts up in the script (the exec >/tmp/automount_output 2>&1 stuff).

Supermount can not be the reason, in fact, I recently tested it but my script didn't work with it    :Rolling Eyes:  . Anyway, when hotplug hangs, try looking at the output of pstree and try to find out what it is doing while it hangs.

Cheers,

Wout.

----------

## r2dtu

 *wmertens wrote:*   

> 
> 
> Supermount can not be the reason, in fact, I recently tested it but my script didn't work with it   
> 
> 

 

Your script is working great for me with supermount and kernel 2.6.1-rc1. I've only made one change:

```

-      MOUNTOPTS="-s -t supermount -onoatime,nosuid,umask=077,uid=$3,gid=$4"

+      MOUNTOPTS="-s -t supermount -odev=$DEVICE,--,noatime,nosuid,umask=077,uid=$3,gid=$4"

```

Thanks!

----------

## MrZammler

 *wmertens wrote:*   

> 
> 
> Read the scripts for how to install them. Both of them handle any device, any partition, automatically, and they always get mounted at /mnt/usb/<devicename>  
> 
> 

 

Nice script, but I could find where I need to have it. /etc/hotplug/usb ?

Thanks.

----------

## vdboor

Until now, I always had a desktop icon on my desktop to mount usb sticks, but I'm really interested in auto-mounting the device with the hotplug system. However, I was wondering; how can the device be mounted as the current active user? (the one behind the console, or X desktop?)

My fstab entry autotically applies a 0077 umask on all removable media, and sets the owner to the user who issued the mount command...

```
/dev/sda1       /mnt/usbstick   auto    rw,noauto,user,exec,nosuid,nodev,umask=0077    0 0
```

Thanks in advise.

----------

## MasquedAvenger

Or, use the sync option in fstab, and it will continue working until all the data is on the drive.  Very handy for removeable media such as this  :Wink: 

James

----------

## malenko

There are some issues with supermount it the listed script. The device field for supermount should be "none" and the real device must be passed in the dev=... option. When umounting, parsing must be done acordingly. Little cleanup. You can download the script from http://www.ms.mff.cuni.cz/~malej9am/prog/automount.hotplug (temporary).

```

#!/bin/sh

#

# Automount hotplugged block devices, by Wout Mertens (wmertens@gentoo.org)

#

# Linux v2.6 version

# This script is released under version 2 of the GPL.

# To install this, either make it /sbin/hotplug, or, if you have

# linux-hotplug installed, make it /etc/hotplug.d/block/automount.hotplug .

# It needs to be executable. I tested it with busybox's ash.

#

# The devices will be mounted for the console user if applicable.

#

# To work, this needs:

# - v2.6 kernel support:

#   - hotplugging, /proc mounted, /sys mounted

#   - filesystems that will be mounted, like vfat

# - sh, echo, sed, stat or ls, getent or grep passwd,

#   mount, umount, mkdir, rmdir. logger is used if there.

#

# Possible improvements:

# - Create a desktop entry for the device.

# - Call a notifier script when mounting/unmounting. This could create the

#   desktop entry by itself.

# - Edit fstab instead, and give the "user" option. This depends more on

#   the user/desktop knowing what he/it is doing.

# - Mount as supermount if available. This will improve behaviour for

#   synchronous writes and hanging mounts, although umount -lf already

#   does a good job. UPDATE: added, but untested. Let me know if it works.

# - Detect filesystem on device, so filesystems that don't need a mounting

#   user can skip user detection.

#ChangeLog:

#2004-05-01: Jaromir Malenko <malenko@email.cz>

#   FIX: Supermount

#   Cleanup

#Previous changes: Wout Mertens (wmertens@gentoo.org)

# Debugging

# set -xv

# exec > /tmp/hotplug.$$ 2>&1 

PATH="/bin:/sbin:/usr/bin:/usr/sbin"

export PATH

# We only handle block device messages

[ "$1" != "block" ] && exit 0

# Proclaim stuff to the world

mesg () {

   logger -t $0 "$*"

}

# Is path already mounted?

is_path_mounted () {

   while read dev path rest

   do

      if [ "$path" = "$1" ]

      then

         return 0

      fi

   done < /proc/self/mounts

   return 1

}

# Get mounted path for a device

get_mounted_path () {

   while read dev path rest

   do

      if [ "$dev" = "$1" ]

      then

         echo "$path"

         return 0

      fi

      # Supermount

      if [ "$dev" = "none" ] && echo "$rest" | grep "$1" 2>&1 >/dev/null

      then

         echo "$path"

         return 0

      fi

   done < /proc/self/mounts

   return 1

}

# Wait for a file to appear

wait_for () {

   local count=0

   while [ $count -lt 10 ] && [ ! -e "$1" ]

   do

      sleep 1

   done

   [ $count -eq 10 ] && return 1

   return 0

}

# Remove strange characters from a filename

clean_filename () {

   # Note the lack of quotes around $1, this strips off spaces.

   echo $1 | sed 's/[ /?*\"<>]/_/g'

}

# Figure out the device to mount

set `echo $DEVPATH | sed 's/\// /g'`

[ $# -ne 3 ] && exit 0

DEVICE=/dev/$3

case $ACTION in

remove)

   # Is it ours?

   dir=`get_mounted_path $DEVICE`

   echo "$dir" | grep -q ^/mnt/usb || exit 0

   # Unmount it

   [ -d "$dir" ] || exit 1

   mesg Unmounting: umount -lf "$dir"

   umount -lf "$dir"

   rmdir "$dir"

   exit 1

   ;;

add)

   # Is it a usb device? Exit if not.

   ls -l /sys/$1/$2/device | sed 's/^.* -> //g' | grep -q usb || exit 0

   

   # Mount it

   # Make sure we have support for vfat

   modprobe -q vfat

   wait_for /sys/$1/$2/device/vendor || exit 1

   # Find the name

   PRODUCT=`cat /sys/$1/$2/device/model`

   if [ -z "$PRODUCT" ]

   then

      PRODUCT=generic

   fi

   # Find out where we mount it

   MOUNTPATH=/mnt/usb/`clean_filename "$PRODUCT"`

   if is_path_mounted "$MOUNTPATH"

   then

      count=1

      while is_path_mounted "${MOUNTPATH}_${count}"

      do

         count=$(( $count + 1 ))

      done

      MOUNTPATH="${MOUNTPATH}_${count}"

   fi

   # Make sure it's a directory

   if [ -e "$MOUNTPATH" ]

   then

      if [ ! -d "$MOUNTPATH" ]

      then

         mesg "$MOUNTPATH exists but is not a directory"

         exit 1

      fi

   else

      mkdir -p "$MOUNTPATH"

      if [ $? -ne 0 ]

      then

         mesg "Could not create mountpoint $MOUNTPATH"

         exit 1

      fi

   fi

   # Find out who we are going to mount it as

   CONSOLEUSER=`stat -c%U /dev/console 2>/dev/null`

   if [ -z "$CONSOLEUSER" ]

   then

      set `ls -l /dev/console`

      CONSOLEUSER=$3

   fi

   [ -n "$CONSOLEUSER" ] || CONSOLEUSER=root

   PASSWD=`getent passwd $CONSOLEUSER 2>/dev/null`

   if [ -z "$PASSWD" ]

   then

      PASSWD=`grep "$CONSOLEUSER" /etc/passwd||grep root /etc/passwd`

   fi

   if [ -z "$PASSWD" ]

   then

      mesg "Could not get password entry for $CONSOLEUSER"

      exit 1

   fi

   set `echo $PASSWD | sed 's/:/ /g'`

   if [ $# -lt 4 ]

   then

      mesg "Bad password entry for $CONSOLEUSER"

      exit 1

   fi

   # These options should prevent abuse and make it writeable for the

   # console user.

   if grep -q supermount /proc/filesystems

   then

      MOUNTOPTS="-s -t supermount -odev=$DEVICE,--,noatime,nosuid,umask=077,uid=$3,gid=$4 none"

   else

      MOUNTOPTS="-s -onoatime,sync,dirsync,nosuid,uid=$3,gid=$4 $DEVICE"

   fi

   mesg Mounting $DEVICE on $MOUNTPATH: mount $MOUNTOPTS $MOUNTPATH

   mount $MOUNTOPTS $MOUNTPATH

   ;;

   

*)

   mesg automount.hotplug $ACTION event not supported

   exit 1

   ;;

esac

```

----------

## Nate_S

I noticed in windows xp recently an option for disabling or enabling write cacheing on the disk (though, of course, being windows, it was not called that, that would be too simple) "optimize for performance" where you have to click a button in the taskbar, or "Optimise for safe unplugging."  So windows is no different from linux in this respect, ifyou have write cacheing on for performance, you need to unmount first.  And unplugging it while writing is never a good idea   :Wink: 

A better option at this point might be to use udev/dbus/hal, I hear it's getting fairly close to being stable.

----------

## GlennM

I've got a question about the automount.hotplug script linked above. It works, but I need some help with tweaking...

It's mounting my USB drive as msdos, and not vfat. I don't see any way to force vfat. Is this possible?

Thanks!

EDIT: Removed my other question, my mistake...

EDIT2: D'oh, maybe since I don't have supermount patched in, I should add "-t vfat" to the MOUNTOPTS at the end...all working now, what a great script.

----------

## craftyc

Just one question and some praise.

For me the device is always mounted as root, since that is what "stat -c%U /dev/console" returns. Is there something wrong with my system or the script?

Excellent script malenko. Just made my life easier.

----------

## vdboor

 *craftyc wrote:*   

> Just one question and some praise.
> 
> For me the device is always mounted as root, since that is what "stat -c%U /dev/console" returns. Is there something wrong with my system or the script?
> 
> Excellent script malenko. Just made my life easier.

 

I've disabled the pam_session module in /etc/pam.d, because I got sick of pam changing the permissions of the /dev/ files automaticallty.. It seams that I have the same problem here..

Does anyone know a better way to detect which user is currently behind the keyboard? A quick fix might be something like grepping the who output, but this hack is rather incomplete:

```
who | grep ":0" | cut -f 1 -d " "
```

I hope someone is more creative then I am, because I would like to use this automount script myself too!

----------

## craftyc

 *vdboor wrote:*   

> 
> 
> I've disabled the pam_session module in /etc/pam.d, because I got sick of pam changing the permissions of the /dev/ files automaticallty.. It seams that I have the same problem here..
> 
> Does anyone know a better way to detect which user is currently behind the keyboard? A quick fix might be something like grepping the who output, but this hack is rather incomplete:
> ...

 

I have a work around for this problem, however it does raise some security issues. Simply change the umask to 000 instead of 077. Since I am the only person using this system it is not a big issue for me. It may be for you.

Also the supermount thing doesn't work correctly. It doesn't unmount the directory, so I get a bunch of directories in /mnt/usb/.

----------

## vdboor

Today I experienced that this automount script perhaps works a little too well..

I'd attached the new digital camera of by brother to my system, and suddently there was a /mnt/usb/DIMAGE/ folder...  :Very Happy:  Only, I had to unmount that file as root, because the camera was clearly waiting for some kind of disconnect signal.

So I'm wondering, what would be a nice way to automate this? (Or provide some sudo command with "unmount.desktop" file  :Wink: )

----------

## craftyc

 *vdboor wrote:*   

> Today I experienced that this automount script perhaps works a little too well..
> 
> I'd attached the new digital camera of by brother to my system, and suddently there was a /mnt/usb/DIMAGE/ folder...  Only, I had to unmount that file as root, because the camera was clearly waiting for some kind of disconnect signal.
> 
> So I'm wondering, what would be a nice way to automate this? (Or provide some sudo command with "unmount.desktop" file )

 

I think just disconnecting the device would be safe. If you use supermount, then it "unmounts" the filesystem as soon as you've finished whatever action you needed to do on the filesystem.

----------

## vdboor

 *craftyc wrote:*   

> I think just disconnecting the device would be safe. If you use supermount, then it "unmounts" the filesystem as soon as you've finished whatever action you needed to do on the filesystem.

 

Just disconnecting sounds a little scary.. (it was the camera of my brother btw). But I think I should look for this supermount thing, it this makes sure the camera unmounts I'm happy.

----------

## craftyc

 *vdboor wrote:*   

>  *craftyc wrote:*   I think just disconnecting the device would be safe. If you use supermount, then it "unmounts" the filesystem as soon as you've finished whatever action you needed to do on the filesystem. 
> 
> Just disconnecting sounds a little scary.. (it was the camera of my brother btw). But I think I should look for this supermount thing, it this makes sure the camera unmounts I'm happy.

 

I think I lied a little about the unmounting. What supermount does is make sure that all of the data is copied/deleted as soon as the operation has been performed ie. cp foo /mnt/usb/Generic/ will copy the data while the command is executed. 

I think that the camera simply acts as a USB mass storage device anyway. I unplug my USB stick all the time and the filesystem has never been corrupted. I almost 99.99999% sure that your brother's camera won't get fubared if you just unplug it. Even moreso if you use supermount.

----------

## WildCoder

Hello,

I just installed the automount.hotplug script and it works great with my usb stick.

There is only a couple of things that I would suggest (I wish I was good enough at scripting to do it myself but that's not the case):

I noticed in the todo list that creating a desktop icon was planned. I just installed my first Gentoo desktop under KDE (been using Gentoo on servers for years) so I hacked the script to quickly add an icon to make my life easier. Since scripting isn't my forte it's really a hack but it'll work for ma at least until the script is written properly.

In case someone else is interested here's what I changed:

```

if [ "$ACTION" = "remove" ]

then

        # Is it ours?

        dir=`get_mounted_path $DEVICE`

        echo "$dir" | grep -q ^/mnt/usb || exit 0

        # Find the name

        PRODUCT=`cat /sys/$1/$2/device/model`

        if [ -z "$PRODUCT" ]

        then

                PRODUCT=generic

        fi

        # Find out who we are going to mount it as

        CONSOLEUSER=`stat -c%U /dev/console 2>/dev/null`

        if [ -z "$CONSOLEUSER" ]

        then

                set `ls -l /dev/console`

                CONSOLEUSER=$3

        fi

        # Unmount it

        [ -d "$dir" ] || exit 1

        umount -lf "$dir"

        rmdir "$dir"

        # remove the desktop icon

        mesg Unmounting $DEVICE, $dir $PRODUCT for user $CONSOLEUSER

        rm "/home/$CONSOLEUSER/Desktop/$PRODUCT.desktop"

        exit 1

fi

```

The get user and device should probably be in a function to be nice.

```

        mount $MOUNTOPTS $DEVICE $MOUNTPATH

        # Everything was mounted fine, create a Desktop icon

        echo "[Desktop Entry]" > "/home/$CONSOLEUSER/Desktop/$PRODUCT.desktop"

        echo "Encoding=UTF-8" >> "/home/$CONSOLEUSER/Desktop/$PRODUCT.desktop"

        echo "Icon=usbpendrive_mount" >> "/home/$CONSOLEUSER/Desktop/$PRODUCT.desktop"

        echo "Type=Link" >> "/home/$CONSOLEUSER/Desktop/$PRODUCT.desktop"

        echo "URL=$MOUNTPATH" >> "/home/$CONSOLEUSER/Desktop/$PRODUCT.desktop"

```

If someone knows how to detect the device type and put a proper icon that would be nice too.

I would like to change the mount point from /mnt/usb/devicename to $home/devicename

   changing the absolute path is easy, but I'd like to make it selectable with an option switch to do it either way, that where it gets way over my head as far as scripting go. With this browsing my home directory would let me see my usb devices right away instead of going to /mnt/usb/devicename.

I hope the author can do that, anyway thanks for the great script!

-WildCoder

----------

## WildCoder

Hello,

Actually with the previous hack I got lucky the first time to get the correct $DEVICE but since I only get generic so it doesn't delete the icon automatically. Script gurus, please help!

-WildCoder

----------

## WildCoder

Hello,

well in my rush to get my usb stick to create a desktop icon I failed to add icon for USB devices with more than one partitions. I just noticed after plugging in a USB HD with 2 paritions in it. BTW I noticed this script is a bit old is it even still needed nowadays or is there a better way to do that kind of thing?

-WildCoder

----------

## insaniac

Hi!

I've read many postings and googled a lot, but still found no answer. 

I'm using this script in /etc/hotplug.d/block/.

I think the script is executed before the device nodes are created by udev.

The last two lines from debug output:

```

+ mount -s -onoatime,sync,dirsync,nosuid,umask=077,uid=1000,gid=100 /dev/sda1 /mnt/usb/MP0804H

mount: special device /dev/sda1 does not exist

```

Could anyone tell me how I get this script to be executed, after the devices nodes are created?

If you need more information, let me know.

----------

## Glorandar

The symptom: Use of the /etc/autoplug.d/block/automount.hotplug script, with UDEV (/conf.d/rc's RC_DEVICE_TARBALL="no"), fails to mount the USB stick on the /mnt/usb/$DEVICE mount point.

However, subsequent manual mounting of the USB stick succeeds.

The cause: UDEV, without persistent /dev state, itself uses the default hotplug scripts (in /etc/hotplug.d/default) to create the necessary /dev block device (e.g. /dev/sdc1) upon which the automount.hotplug script depends.

When you are using UDEV, move the automount.hotplug script from

```
/etc/hotplug.d/block
```

to the 

```
/etc/dev.d/block
```

 directory. UDEV invokes the /etc/rule.d scripts after its done managing the /dev directory.

cheers

----------

## bfkeats

I changed the following line in /sbin/hotplug, and now it works.

```
-for I in "${DIR}/$1/"*.hotplug "${DIR}/"default/*.hotplug ; do

+for I in "${DIR}/"default/*.hotplug "${DIR}/$1/"*.hotplug ; do
```

----------

## digitalexpl0it

this is a great thread, the scripts work great. and the little icons hack needed tweaing but works. Is there anymore updates on this script? anymore imporvments?

I notice that the icon put on the desktop was set for root, is there a way to set it for all users? I have it set for my main user like the following

```

   mount $MOUNTOPTS $DEVICE $MOUNTPATH

# Everything was mounted fine, create a Desktop icon

        echo "[Desktop Entry]" > "/home/ddrose/Desktop/usbthumbdrive.desktop"

        echo "Version=1.0" >> "/home/ddrose/Desktop/usbthumbdrive.desktop"

        echo "Name=Thumb Drive" >> "/home/ddrose/Desktop/usbthumbdrive.desktop"

        echo "Encoding=UTF-8" >> "/home/ddrose/Desktop/usbthumbdrive.desktop"

        echo "Icon=usbpendrive_mount" >> "/home/ddrose/Desktop/usbthumbdrive.desktop"

        echo "Type=Link" >> "/home/ddrose/Desktop/usbthumbdrive.desktop"

        echo "URL=$MOUNTPATH" >> "/home/ddrose/Desktop/usbthumbdrive.desktop"

        echo "Icon=/usr/share/pixmaps/gnome-remote-desktop.png" >> "/home/ddrose/Desktop/usbthumbdrive.desktop"

   chmod 777 "/home/ddrose/Desktop/usbthumbdrive.desktop" 

   chown ddrose "/home/ddrose/Desktop/usbthumbdrive.desktop" 

   chgrp users "/home/ddrose/Desktop/usbthumbdrive.desktop" 

```

also the mount option was only for root, so my users could not access the thumbdrive only root could. So I just did this

```
# These options should prevent abuse and make it writeable for the

   # console user.

   if grep -q supermount /proc/filesystems

   then

      MOUNTOPTS="-s -t supermount -onoatime,nosuid,umask=077,uid=$3,gid=$4"

   else

      #MOUNTOPTS="-s -onoatime,sync,dirsync,nosuid,umask=077,uid=$3,gid=$4"

      MOUNTOPTS="-s -onoatime,sync,user,exec,umask=0000"

   fi
```

just a FYI

----------

## SomniOne

I have assembled everything what I found in this thread and made a few mods, here are the highlights:

#	- Multiple partitions are now correctly created as separate icons on the user's desktop

#	- use supermount and subfs successfuly.

#	- allow a definition for using diffrent icons for diffrent drives

It has been tested with ~5 diffrent usb devices and on 4 diffrent machines.

You can grab it from here (automount.hotplug)

 :Smile: 

----------

## adrien1977

Ok 

I did everything that was asked to do to have that thing work but ....

nothing is automounter.

What am i missing?

I created the folder block 

I copied the script in /etc/hotplug.d/block

Change the script /sbin/hotplug (like mentioned a couple threads above)

Restarted everything and nothing

I also change the script to point to kde 3.4 instead of 3.3

thnaks

----------

## elyre_elan

Are you using udev?  In a previous post it states to not use hotplug.d/block but instead  dev.d/block...

----------

## adrien1977

The thing is that I do not know.

What I am doing right now is make sure I am using udev, recompile with the last kernel (r9) and cross finger.

----------

## adrien1977

ok now I am pretty sure I am using only udev

I have gentoo=nodevfs in the boot

and I also unmerge devfsd

I am also using the latest kernel gentoo-sources R9

it took me a while to make all that work.

Anyway, I also did what you said, which is to put the script in the /etc/dev.d/block

I rebooted and after login I inserted my key and....... nothing happens

Do I have to put anything in the fstab file???

----------

## chebe

If you want a good automounter, then youshould try ivman with dbus ...

All there is to do to get it done is written here:

http://gentoo-wiki.com/HOWTO_ivman

I'm no expert, and I did it in just a few minutes, you shouldn't have any problems.

----------

## adrien1977

ok i'll try

----------

## adrien1977

Well I gotta be really bad at linux because I cannot make that automounter work.

----------

## cajzell

Great script! Thanks. It works so fine for my USB stick and external disc also.

Do you have a suggestion on how to modify this script to be used with an external HD that is connected at IEEE1394? The line 

```
   # Is it a usb device? Exit if not.

   ls -l /sys/$1/$2/device | sed 's/^.* -> //g' | grep -q usb || exit 0
```

makes it to exit the script.

Or, is there some other way you should handle IEEE1394?

----------

## cdjalel

Great script.

For my self I replaced  /dev/console  by /dev/pts/0.

Thank you all there.

----------

## SomniOne

 *cdjalel wrote:*   

> 
> 
> For my self I replaced  /dev/console  by /dev/pts/0.
> 
> 

 

Yes, but that's not always applicable, for example on a machine wich starts a pppd connection (adsl), the pppd will take the /dev/pts/0 first, thus making the permissions go wrong.

I had a permission problem on a few machines doing hibernate (the /dev/console would be owned root after the resume), it could be solved by searching the 'w' command for ':0' (xwindows login) and using the current user, but I was lazy and simply changed USER=theusername in the script.

Oh, yes and the script has problems with drives which don't have any partitions. (some usb sticks are delivered that way)

----------

