Sunday, June 3, 2012

Putting the Linux automounter to good use

I think filesystem automounters (eg. Linux autofs) are dramatically underused. They bring a lot of benefits to the table and can save you lots of typing. As an example, this evening I was building a VM on my laptop to be a build server for more Redhat-alike VMs, such as CentOS. I wanted it to be usable when disconnected (eg. flying), so I downloaded both of the CentOS 6.2 x86_64 DVDs and saved them in my ISO folder on the VM host (my MacBook Pro running OSX, but I'm sure it'd work equally well with a Linux or Windows host).

I figured I'd use VirtualBox's shared folders feature, rather than move or copy all the ISOs into the VM. This requires the VirtualBox guest additions to be installed, and provides a new filesystem type vboxsf. You mount a shared folder (for this example, named iso) thusly:

mkdir /mnt/iso
mount -t vboxsf iso /mnt/iso

If the stars all align nicely, you can access your iso folder in the VirtualBox guest, under /mnt/iso. Now you can loopback-mount an ISO file:

mkdir /mnt/centosdvd
mount -o loop,ro -t iso9660 /mnt/iso/centosdvd.iso /mnt/centosdvd

Wheeee, CentOS! This is all mildly annoying, though. Computers are supposed to make our lives easier! And going through this process over and over for lots of ISOs gets old very rapidly indeed. Thankfully, we can use the Linux automounter to simplify both tasks. First, we tell autofs about the two automount maps we're going to create; one for the shared folders, and one for the ISOs, by adding these two lines somewhere in /etc/auto.master

/vbox /etc/auto.vbox -n 5
/iso /etc/auto.iso -n 5

Then we create the two map files. They only contain one line each. This goes in /etc/auto.vbox:

* -fstype=vboxsf :&

... and this goes in /etc/auto.iso (replace iso with the name of your VBox shared folder containing ISO files, and possibly a further path inside that share, if appropriate, eg. /vbox/myshare/isofiles):

* -fstype=iso9660,ro,loop :/vbox/iso/&

Now reload the automounter:

/etc/init.d/autofs reload

... and that should be that. Try browsing your shares in /vbox. If you have an ISO file /vbox/iso/foo.iso, you should be able to see inside it by merely looking in /iso/foo.iso. Transparent, automatic, and, best of all, you only solve the problem once.