New project was born: halrv

Posted on . Updated on .

Some days ago I started a new small project called halrv. It is a very simple Python script that allows you to manage removable volumes from the command line using HAL. This would be a command line equivalent of the typical GUI programs that let you do the same thing. In modern desktops, when you plug a USB memory stick in your computer, you get a dialog asking you if you want to open its contents on an explorer window, and they also put an icon in your desktop for similar purposes. You can see which devices are connected to your computer by looking at your desktop. If you click on the icon, an explorer window opens showing its contents, and maybe the device was mounted in the way. When you’re done, you close the window and right-click on the device icon, and then you select the option to "safely remove" the device.

However, if you don’t run X or if you run a minimalistic desktop environment or window manager like fluxbox chances are, as a user, you don’t have any program running to do the same trick. Many times these users run their systems with a few static entries on the /etc/fstab file to perform this operation. Now you can do the same from the command line using HAL, hence only needing to be part of the "plugdev" group. No need for static entries in /etc/fstab. Some of you may be thinking "didn’t this type of program exist already?". Maybe it did, but I didn’t find any other than pmount-hal, and it wasn’t working when I downloaded a copy (I wasn’t able to mount my memory stick and the error message appeared in several Google search results that didn’t give me a solution). You can still use plain pmount instead of pmount-hal, but then you are not using HAL and lose the advantages of it. You have to create a pmount config file where you whitelist devices that users can mount with it. In practice this works well, like static /etc/fstab entries. However, HAL is more flexible and, if you can use it, why not use it? Why not use the same service many desktop environments are using?

So here we go with halrv. In a mere 300 lines script, with short functions and commented code, you can have the following functionality, similar to a GUI and following the same steps:

$ # I am part of the plugdev group
$ groups
users lp audio video cdrom plugdev power
$
$ # Let's see which removable volumes are present
$ halrv
Device      Label       UUID                                  Mount point
$
$ # None, the table is empty
$ # I plug my SanDisk memory stick, labeled "SANDISK", and wait a few seconds
$ # ...
$ # Let's check again
$ halrv
Device      Label       UUID                                  Mount point
/dev/sdb1   SANDISK     4026-23C0
$ # There we go! So let's mount it
$
$ halrv mount /dev/sdb1
/dev/sdb1 mounted on /media/SANDISK
$
$ # Great! Let's see the contents
$ ls /media/SANDISK
sz3fen.pdf*
$
$ # And let's see how HAL mounted the device
$ mount
/dev/sda3 on / type ext4 (rw,relatime,barrier=1,data=ordered)
/proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
none on /proc/bus/usb type usbfs (rw)
/dev/sda4 on /boot type ext2 (rw)
none on /dev/shm type tmpfs (rw,noexec,nosuid,nodev)
/dev/sdb1 on /media/SANDISK type vfat (rw,nosuid,nodev,uhelper=hal,uid=1000)
$ # You can see the last entry above
$
$ # Now let's unmount it and eject the device (safely remove)
$ halrv eject /dev/sdb1
$ # No error messages, which is a success
$ # Let's confirm it
$
$ mount
/dev/sda3 on / type ext4 (rw,relatime,barrier=1,data=ordered)
/proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
none on /proc/bus/usb type usbfs (rw)
/dev/sda4 on /boot type ext2 (rw)
none on /dev/shm type tmpfs (rw,noexec,nosuid,nodev)
$ halrv
Device      Label       UUID                                  Mount point
$
$ # The device has been unmounted and ejected!

Apart from the basics above, the program also uses a personal config file (per user) where you can put specific mount options for some filesystems or even per-volume identified by its UUID. I’d say it’s quite flexible and useful. I hope you think the same. Don’t forget to try it and report back any problems or bugs you find!

Load comments