« Good Products are hard to findCommercial Websites SUCK! »

Command line mounting with HAL via DBus

I recently got a new machine at work and as it's shared between 3 people so installing a custom distribution (ArchLinux would have been my choice) is not really an option. So, as my company default Operating System is Ubuntu, that's what I've got.

I can't really use my normal way of doing things like mounting on this system so I had to work out new ways to do things in the console instead ;)

Follow up:

Turns out these GUI oriented distributions run some new fangled things by default including some interesting daemons. One such daemon will show up a little icon in GUI file managers etc when you insert a remote storage device. This allows you to then simply click on the icon and BAM! ... your device is mounted. To unmount, simply right-click and select unmount.

This is great and all but I'm more of a command line user. I actually have a bunch of directories pre-created in /mnt (such as /mnt/sdb/1 , /mnt/sdb/2 etc) and entries in my /etc/fstab like these:

/dev/sdb1 /mnt/sdb/1 auto defaults,noauto,user 0 0
/dev/sdb2 /mnt/sdb/2 auto defaults,noauto,user 0 0

This works for me, but I can't really do it on this shared box, plus it's really quite messy. Thing is I really don't want to have to use a GUI to mount things ... what to do.

Turns out HAL ( http://freedesktop.org/wiki/Software/hal ) is responsible for mounting these things in the GUI so I figured why not in a terminal too! HAL stands for Hardware Abstraction Layer and is basically designed to make desktop hardware use easier.

Devices register with HAL to be notified of various events. These can be a device being plugged in of a certain type etc. All this is done via a message (bus) system which provides "a simple way for applications to talk to each other". This system is DBus ( http://www.freedesktop.org/wiki/Software/dbus ).

So, with this new knowledge of DBus and HAL, we can create a nice quick and easy script to mount and unmount devices (halmount). You can download it from the URL below:
http://www.datapax.com.au/apps/halmount/

Some advantages with using HAL and DBus is that you can mount with default "sane" options without so much as a destination mount point. For example, if I insert a USB key/thumbdrive/whateveryoucallit with a volume label of "mydevice" into my machine, simply executing something like:

halmount /dev/sdb1

Will result in the first (primary) partition of my USB key thingy being mounted to /media/mydevice . This compaired to my old method of something like:

sudo mkdir /media/mydevice
sudo mount -o uid=${UID} -t auto /dev/sdb1 /media/mydevice

Permalink 11/02/09 01:06:36 pm, by Todd Harbour Email , 453 words, Categories: Linux, Coding, Software , 2 comments »Send a trackback »

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

2 comments

Comment from: KNT [Visitor]
While using the halmount script as a normal user I've noticed that the ownership of the mount point is set to root.root.
This is a problem as I don't have rw access to the mounted partition.
18/05/09 @ 05:56
Comment from: oto.finder [Visitor]
It's very nice script. I have haved to corect it for me. Function "hal-find-by-property" have found two records for /dev/sr0. There was in $udi list of two words. There is no nice hack, it wil put in $udi first value of the list:

for a in $udi; do b=$a; break; done
udi=$b

You should put it before line:
# Get the mounted status of this udi

Do you know to write nicer hack?

Oto
11/06/09 @ 01:02

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

You can just use your OpenID to provide your name, e-mail and url.