[openbox] supporting dynamically mounted devices (usb keys, etc.)?

Ian Zimmerman itz at buug.org
Thu Feb 21 18:51:50 EST 2013


On Tue, 19 Feb 2013 10:03:33 +0100
Louis-David Mitterrand <vindex+lists-openbox at apartia.org> wrote:

Louis-David> For now I manually mount usb storage devices with an entry
Louis-David> in /etc/fstab, but I'd like these devices to automount like
Louis-David> in gnome.

Louis-David> What is the best way to do that in openbox?

First of all, this has almost nothing to do with openbox, which after
all is just a window manager.  Your question is really "I don't want to
use Gnome but I like the part of it that does automount, how can I have
that?"  Well tough, that's why the whole idea of "integrated desktop" is
faulty, you _can't_ have just a little piece of it and not the rest.

That said, I understand that there is no gnome-haters mailing list
(yet?), so I'll try to help.

Several others recommend udisks - I cannot second that.  udisks is
actually the engine behind the automount in Gnome (and maybe KDE too,
I'm not sure).  It _polls_ all your devices every second to find if
there's any media to mount.  It actually has to do that to handle CD
drives which give no indication to the kernel when a disc is inserted.

If you still use CDs and DVDs then udisks may be the only solution but I
now use them so rarely that in those few cases manual mounting is OK.
For me it's entirely about mounting various USB storage gadgets.  And
for that there are simple and elegant solutions (no polling).

If you have a Debian derived system there's the usbmount package.  I
used it for a while but I found it didn't handle removal correctly with
some of my devices.  It may work for you, though.

I now use a fully home-hacked solution, which consists of:

1. a udev rule file:

KERNEL=="sd[a-z]*",SUBSYSTEMS=="usb",ACTION=="add",\
        RUN+="/etc/udev/scripts/add-usb"
# sadly, usbmount totally flunks this.
KERNEL=="sd[a-z]*",SUBSYSTEMS=="usb",ACTION=="remove",\
        RUN+="/etc/udev/scripts/remove-usb"

2. the add-usb script:

#! /bin/sh

set -e

exec >/dev/null 2>&1

td=/run/usb-mounts
eval `blkid -p -o udev "$DEVNAME"`

if [ x"$ID_FS_USAGE" != xfilesystem ] ; then exit 1 ; fi

mount_and_record()
{
    test -e $td || mkdir -p $td
    cd $td
    df="$ID_FS_UUID"
    lockfile .$df.lock
    tf=`mktemp --tmpdir=. .XXXXX`
    if ! ln $tf $df  ; then
        # already mounted?? bail out
        rm -f $tf .$df.lock
        exit 1
    fi
    echo "$DEVNAME" >$df
    if ! mount "$@" ; then
        rm -f $tf $df .$df.lock
        exit 1
    fi
    rm -f $tf .$df.lock
}

if awk -v ID_FS_UUID="$ID_FS_UUID" 'BEGIN {st=1} ; ($1 == "UUID=" ID_FS_UUID) {st=0} ; END {exit st}' < /etc/fstab ; then
    mount_and_record -U "$ID_FS_UUID"
    exit 0
fi

if awk -v ID_FS_LABEL="$ID_FS_LABEL" 'BEGIN {st=1} ; ($1 == "LABEL=" ID_FS_LABEL) {st=0} ; END {exit st}' < /etc/fstab ; then
    mount_and_record -L "$ID_FS_LABEL"
fi

3. the remove-usb script:

#! /bin/sh

set -e

# A block or partition device has been removed.
# Test if it is mounted by us, and if so unmount it.
td=/run/usb-mounts
test -e $td || mkdir -p $td
cd $td
for x in `ls` ; do
    thisdev=`cat $x`
    if [ "$DEVNAME" = "$thisdev" ] ; then
        lockfile .$x.lock
        umount -l "$DEVNAME"
        rm -f $x
        rm -f .$x.lock
        break
    fi
done


All this does require listing the device in fstab, though, like this:

UUID=uuid-of-your-partition /media/pendrive ext3    noexec,nodev,ro,noauto 0   0

Removing this requirement is left as an exercise for the reader ...

-- 
Please *no* private copies of mailing list or newsgroup messages.

gpg public key: 1024D/C6FF61AD
fingerprint: 66DC D68F 5C1B 4D71 2EE5  BD03 8A00 786C C6FF 61AD
http://www.gravatar.com/avatar/c66875cda51109f76c6312f4d4743d1e.png


More information about the openbox mailing list