auto mounting in Openbox

Hi

So I finally ditched Xfce 4.6 and decided to give Xfce 4.8 a skip to try something else instead. After playing with LXDE for a bit, I've ended up using standalone Openbox, tint2, and various other bits for my new desktop setup. I poked around for HAL-esque auto mounting setups, but they were all hacky or used amd(8) - not really ideal for hot plug storage devices.

I've made a small auto mounting system that uses devd(8) and integrates with Openbox via its pipemenu system. Mountable filesystems are added to a "Mount" menu entry in my Openbox menu, and clicking an entry toggles between mounted and unmounted with popup message boxes to confirm success/failure.

Filesystems are mounted to subdirectories below /media (or wherever you define), and are named in correspondence to their label if present, or their device name if not. Storage devices are probed to determine if a device is an MBR partition, BSD label, or raw disk device, and educated guesses are made to determine what is mountable. File systems are probed too, but currently only UFS and MSDOSFS are supported. Large MSDOSFS support still needs to be added too. The Openbox specific bit is also written to allow users to mix manual (via terminal) and auto mount/umounting of devices.

When it comes time to call mount/umount, sudo is used, so you will need that installed and correctly configured for this to work. I think one could also use vfs.usermount too, but I haven't tested that yet.

[2011-10-24] Defunct. See [thread=27233]volman[/thread] instead.
[2011-05-23] Latest version: [post=135232]here[/post]
 
My project's name is amount. At its core is a daemon logically named amountd. This daemon is a shell script that sets up an IPC channel with devd(8), listens for device attachment messages and attempts to discover mountable filesystems on newly attached devices.

When running, amountd creates a directory for itself at /tmp/.amountd, below which it maintains the text file mountable. This is simply a listing of file systems that amountd has discovered. As storage devices come and go, this file is updated with a listing of filesystems that are thought to be mountable, and some details about each file system.

All other components of amount essentially serve as clients to amountd - bridges between amountd, your desktop environment, and mount/umount commands. The most basic of which is a script named amount, a small CLI client to let you use amountd via a terminal, and hopefully to serve as a prototype for future clients to be written by others.

There is one more client currently included: obamount.sh. Written by me too for use with my window manager of choice, Openbox. I plan to include other clients into my distribution too, so feel free to make them public or send them my way.
 
I'm basically reciting my setup here. Feel free to experiment if you understand things. I'll formalise everything better later.

First you need to get xmountd starting in the background. Place it in ~/bin and make sure it's executable. Then add it to your X startup, in my case ~/.xsession. Here's what mine looks like:
Code:
xset m 1 1 r rate 250 30 dpms 0 0 300
xmodmap \
	-e "keycode 210 = XF86AudioLowerVolume" \
	-e "keycode 209 = XF86AudioRaiseVolume" \
	-e "keycode 219 = XF86AudioMute" \
	-e "keycode 220 = XF86AudioPause" \
	-e "keycode 207 = XF86AudioStop" \
	-e "keycode 190 = XF86AudioPrev" \
	-e "keycode 130 = XF86AudioNext" \
	-e "keycode 191 = XF86Calculator" \
	-e "keycode 132 = XF86Music" \
	-e "keycode 113 = Multi_key"
export CHARSET=UTF-8
nitrogen --restore &
xcompmgr -c -C -f -t 1 -l 1 -r5 -o.55 -I 0.4 -O 0.4 -D 50 &
~/bin/xmountd &
ssh-agent openbox-session

Place obmount.sh into ~/.config/openbox.

Open ~/.config/openbox/menu.xml and locate your root-menu block. Add a Pipemenu element to it that executes obmount.sh. Here's how mine looks:
Code:
        <menu id="root-menu" label="Openbox 3">
                <menu id="applications" label="Applications" execute="/usr/home/aragon/.config/openbox/dynmenu.pl"/>
                <menu id="system-menu"/>
                <menu id="mount" label="Mount" execute="/usr/home/aragon/.config/openbox/obmount.sh"/>
                <separator/>
                <item label="Log Out">
                        <action name="Exit">
                                <prompt>
                                        yes
                                </prompt>
                        </action>
                </item>
        </menu>

Log out and back in. Check that xmountd is running:
Code:
$ ps ax |grep xmountd
33422  ??  I      0:00.02 /bin/sh /usr/home/aragon/bin/xmountd

Your Openbox menu should have a "Mount" entry on it that is empty.

Now install security/sudo if it isn't already installed:
# pkg_add -r sudo

Use visudo to add an entry to sudoers that looks similar to mine:
Code:
aragon	ALL=(ALL) NOPASSWD: /usr/home/aragon/.config/openbox/obmount.sh

Obviously you need to adjust the above for your username.

Test that sudo works:
Code:
$ sudo ~/.config/openbox/obmount.sh
<openbox_pipe_menu>
</openbox_pipe_menu>

If you get a password prompt or error, your sudoers entry is in correct.

Finally, try plug a USB disk in and try mount/umount it via the Openbox menu. With the above setup, errors should be logged to ~/.xsession-errors in case you need to debug.

Comments/contributions appreciated. :D
 
Inspired by this, I wrote a PyQt based program that does roughly the same. It adds an icon to the system tray, notifies you when a device becomes available and has a context menu to mount / unmount devices.

It will only let you mount devices it knows about. An config entry look like:

Code:
[msdosfs/foo]
mount = sudo mount /media/foo
umount = sudo umount /media/foo
postmount = xdg-open /media/foo

So you specify the device you want to mount with the "/dev/" prefix striped and a command to execute when the device should be mounted or unmouted. The postmount command is executed after the mount command finished running and was successful.

The config files are /usr/local/etc/mountagent.conf and $HOME/.mountagent.conf (both are read, entries in the later may overwrite entries in the former).

Enjoy!
 

Attachments

  • mountagent.py.gz
    1.7 KB · Views: 345
@aragon

Hi, it doesn't work for me. I checked everything, code, permissions,..it's all there but there's no entry in openbox "mount" menu.

update: It seems that it works only with msdosfs, not with ufs. Tried with different gpart configurations, but it doesn't read it. Also, it can only mount a FS, not unmount it. I changed working dirs to ~/tmp and ~/mnt.
 
I recommend checking for error output in ~/.xsession-errors. If that doesn't work, try call the obmount.sh script manually to inspect output, eg.:

$ obmount.sh umount da0s1a aragon

(replace "aragon" with your username)
 
I'm not using xsession, but .xinitrc. Script is started nonetheless.

If I run
Code:
.config/openbox/obmount.sh umount /dev/da0s1 bbzz
I get
Code:
</openbox_pipe_menu
only as an output. That's after FS is mounted. Like I said I'm able to mount msdosfs only. But there's no entry in openbox for unmount.

I changed paths to ~/media and ~/tmp where appropriate in scripts.

I though maybe sudo is messing up with something so I tried without it, but it isn't. To make it work without sudo, there needs to be writing permission to /dev/msdosfs/ so there needs to be an entry in /etc/devfs.rules, something like this:
Code:
add path "/dev/msdofs/*" mode 660 group operator
which is...dirty. So sticking with sudo is better.

Anyway, not sure what else to try.
 
More information is really needed. Disable xmountd from starting in .xinitrc and run it manually in an xterm so you can see its output. Inspect the /tmp/.xmountd/mountable file for volumes to appear/disappear as you insert/remove devices. Finally run the obmount.sh script without parameters to inspect the menu XML it generates. If you post all that info here I should be able to help better.
 
After a restart, msdofs disk can now mount as well as umount. Don't ask. I didn't change anything.
Anyway, still can't mount ufs. Tried different gpart configurations.
First only DD mode, no slices.

Code:
# gpart show da0
=>     63  7843752  da0  MBR  (3.7G)
       63  7843752    1  freebsd  (3.7G)

xmountd gives
Code:
CREATE da0

~/tmp/.xmountd/mountable is empty, and obmount.sh prints
Code:
<openbox_pipe_menu>
</openbox_pipe_menu>

Mouse over openbox menu is empty.
So i tried with slices and labels.
Code:
# gpart show da0s1
=>      0  7843752  da0s1  BSD  (3.7G)
        0       16         - free -  (8.0K)
       16  7843736      1  freebsd-ufs  (3.7G)

This time xmountd gives
Code:
CREATE da0
CREATE da0s1
CREATE da0s1
CREATE da0s1a
CREATE da0s1c
CREATE da0s1a
CREATE da0s1a
CREATE da0s1c
CREATE da0s1a
CREATE da0s1ca
CREATE da0s1ca
Does that look like it should?
The rest of output is just like before. There's no item in openbox menu.
Does that help any? So to recap, msdosfs works fine, but ufs doesn't at all.
Another thing that would be cool for future is maybe adding support for writable ntfs via ntfs-3g.
 
bbzz said:
First only DD mode, no slices.

Code:
# gpart show da0
=>     63  7843752  da0  MBR  (3.7G)
       63  7843752    1  freebsd  (3.7G)
The above looks strange to me, but TBH I can't recall playing with DD mode since I started using gpart. I will do some testing with this.


bbzz said:
xmountd gives
Code:
CREATE da0

~/tmp/.xmountd/mountable is empty, and obmount.sh prints
Code:
<openbox_pipe_menu>
</openbox_pipe_menu>
This looks normal considering gpart's output above. I don't think my detection caters for this at the moment.

bbzz said:
So i tried with slices and labels.
Code:
# gpart show da0s1
=>      0  7843752  da0s1  BSD  (3.7G)
        0       16         - free -  (8.0K)
       16  7843736      1  freebsd-ufs  (3.7G)

This time xmountd gives
Code:
CREATE da0
CREATE da0s1
CREATE da0s1
CREATE da0s1a
CREATE da0s1c
CREATE da0s1a
CREATE da0s1a
CREATE da0s1c
CREATE da0s1a
CREATE da0s1ca
CREATE da0s1ca
Does that look like it should?
No, this looks rather strange. I will need to do some poking around here. Please do me another favour and send me your partition table as follows:

# dd if=/dev/da0 of=/tmp/parttable bs=1k count=128

Gzip and attach here please.


bbzz said:
Another thing that would be cool for future is maybe adding support for writable ntfs via ntfs-3g.
Ext2 too. :)
 
Yeah figured something doesn't look right there :e
gpart was used to create da0s1a like this:

Code:
gpart create -s mbr da0
gpart add -b 63 -t freebsd da0
gpart create -s bsd da0s1
gpart add -i 1 -b 16 -t freebsd-ufs da0s1
newfs da0s1a
 

Attachments

  • parttable.gz
    467 bytes · Views: 343
I've had some time to do more work on this. Changes include:

  • Renamed to amount and version set to 0.5.
  • Improved handling of Android and other similar devices.
  • File system detection using file(1). (thanks wblock)
  • Untested ext2fs support.
  • Command line utility added.
  • Logging moved from stdout to syslog.
  • Bug fix: incorrect mount state in OpenBox menu generation.

Script file names have changed so I recommend deleting your old version completely to save you any confusion when installing this version.

If you want to see log messages, please setup your syslog to catch user.* or tag "amountd", eg.

/etc/syslog.conf
Code:
user.*						/var/log/user
/etc/newsyslog.conf
Code:
/var/log/user				644  3     100  *     JC

Since script names have changed, please update your sudoers file with the new names.

Everything else should work the same. An example of the command line utility in action:
Code:
$ amount
mount da0 msdosfs
mount da1 msdosfs/SAMSD
$ amount da0
mounting da0 msdosfs... success
$ ls -l /media
total 32
drwxr-xr-x  1 aragon  wheel  32768 Jan  1  1980 da0/
$ amount 
umount da0 msdosfs
mount da1 msdosfs/SAMSD
$ amount da0
umounting da0 msdosfs... success
$ ls -l /media
total 0

bbzz, sorry, I have been unable to reproduce the problem you're having. Your partition table looked fine here and worked fine too. I can only guess it's a hardware issue...

Todo:
  • Large FAT32 support.
  • NTFS support.
 

Attachments

  • amount-0.5.tar.gz
    2.5 KB · Views: 356
Hi all:

I've made a custom hack of fabulous 'amount'. It runs fine in XFCE 4.8. NTFS support was added but not widely tested, but it seems to work fine.

Thanks for the original 'amount', aragon. It's a great piece of code.

Attached to this post my hack.
 

Attachments

  • amount.tar.bz2
    23.1 KB · Views: 233
@jjjesss and @aragon,

Thanks for your scripts!

@jjjess,

A couple of things:

First off, I added this line to my sudoers file
Code:
bigtoque ALL=(ALL) NOPASSWD: /etc/amount/openthunar.sh
but I still get asked for a password, so unless I run the command from a terminal, the script doesn't work for me. At the moment I'm just using the line
Code:
%wheel ALL=(ALL) ALL

Have I entered the entry I need in my sudoers file incorrectly?

Second, the command run from the launcher icon doesn't work for me. If I right-click on the icon, edit the launcher and tell it to run from the terminal, mounting and unmounting works just fine. Unfortunately, you need to edit the launcher every time you mount and unmount.

Finally, (and I have no idea if this should or should not be part of the script), but when unmounting an iPod, I have to run the command [CMD=""]camcontrol eject [device][/CMD] to properly disconnect the iPod. I'm just curious if something like this could be added to the script.
 
bigtoque said:
so unless I run the command from a terminal
Are you saying that you don't get prompted for a password when it's run from a terminal? Can you provide more information on your WM/DE setup?

bigtoque said:
Finally, (and I have no idea if this should or should not be part of the script), but when unmounting an iPod, I have to run the command [CMD=""]camcontrol eject [device][/CMD] to properly disconnect the iPod.
By "properly disconnect", do you mean so that the iPod recognises that it can be disconnected?

When amount unmounts something, it unmounts the file system only. It doesn't try to "eject" or reset the hardware in any way. This means your device won't realise what's happening, but disconnecting it should be safe in terms of data integrity.
 
aragon said:
Are you saying that you don't get prompted for a password when it's run from a terminal? Can you provide more information on your WM/DE setup?

I'm using a stock XFCE 4.8.

I can see that my post wasn't worded that well. When I run the command from the launcher (without editing the launcher to open in a terminal), nothing happens (or at least I can't see anything happening).

When I edit the launcher to open in a terminal (or just run the command from a terminal), I get asked for my password. After I enter my password, I get the output:

Code:
Sorry, user bigtoque is not allowed to execute '/etc/amount/amount doCmd mount msdosfs /dev/msdosfs/FLASH /media/FLASH stefan' as root on bsd-toque.
Mountdir = /media/FLASH

I assume this means that the entry I made in my sudoers file wasn't correct.

aragon said:
By "properly disconnect", do you mean so that the iPod recognises that it can be disconnected?

When amount unmounts something, it unmounts the file system only. It doesn't try to "eject" or reset the hardware in any way. This means your device won't realise what's happening, but disconnecting it should be safe in terms of data integrity.

Yes, when I say "properly disconnect" I mean that the iPod goes from saying "Connected - Eject before disconnecting" on it's screen to "Ejecting" and finally "OK You may now disconnect".

You're suggesting that as long as the iPod has been properly unmounted, it should be safe to just disconnect it?
 
bigtoque said:
Code:
Sorry, user bigtoque is not allowed to execute '/etc/amount/amount doCmd mount msdosfs /dev/msdosfs/FLASH /media/FLASH stefan' as root on bsd-toque.
Mountdir = /media/FLASH

I assume this means that the entry I made in my sudoers file wasn't correct.
You are running /etc/amount/amount, so you need to add that to your sudoers.

What is openthunar.sh?


bigtoque said:
You're suggesting that as long as the iPod has been properly unmounted, it should be safe to just disconnect it?
Yes, it is safe. You can add the eject command if you like, but one downside I can think of is that once you unmount, you will have to physically unplug and replug your ipod to mount it again.
 
Hi all:

My user belongs to 'wheel' group in my computer, so I put in the /usr/local/etc/sudoers:

Code:
%wheel ALL=(ALL) NOPASSWD: /etc/amount/amount
%wheel ALL=(ALL) NOPASSWD: /etc/amount/amountd 
%wheel ALL=(ALL) NOPASSWD: /etc/amount/openthunar.pl
Probably openthunar.pl does not need to be included but..

And the updatedesktop.pl is an absolutely dirty script, relying on things like whoami to guess the user who is clicking in the desktop icon but it's a matter of time (and hurry).

Next thing I'd like to implement is mount/unmount sd cards from my MMC Card Reader (/dev/mmcsd).

BR
 
aragon said:
You are running /etc/amount/amount, so you need to add that to your sudoers.

What is openthunar.sh?



Yes, it is safe. You can add the eject command if you like, but one downside I can think of is that once you unmount, you will have to physically unplug and replug your ipod to mount it again.

Thanks aragon, my problem was that I didn't have /etc/amount/amount in my sudoers. I put /etc/amount/openthunar.sh in there because that's what the launcher that came up on the desktop referenced.

Everything seems to work just fine.
 
Added support for /dev/mmcsdXXXX devices (MMC Card Reader integrated in laptops). Included some notes about permissions and the sudoers file.
 

Attachments

  • amount.tar.bz2
    23.2 KB · Views: 225
aragon, one question: amountd intends to mount devices like /dev/adaxxx, for example, in my computer, /dev/ada0s3.journal. According to the code it seems only devices starting by da or in my hack mmcsd, so any ada device should be watched by amountd. So, why does this happen? I don't want amountd watch my computer partitions. How can be this arranged?

Thanks in advance.
 
Back
Top