OpenBSD Journal

Automated Mounting of Removable Disks

Contributed by jcr on from the not-allowed-to-make-mount-jokes dept.

Alexey Vatchenko (av<at>bsdua<dot>org) wrote in to tell us about his hotplug-diskmount port for automated disk mounting.

The hotplug-diskmount port is a plug-in for the OpenBSD hotplugd(8) daemon to automatically mount removable disks to the filesystem. It works with any disk types (SD cards, USB mass storage devices, card readers and CD/DVD drives). The hotplug-diskmount program is intended to be used in single user environment on desktop or laptop where the convenience of automatic mounting is really helpful.

Configuring

First of all, you want to initialize hotplug-diskmount so it creates the directories where all mount points will be placed. By default it creates the "/vol" directory and automatically creates subdirectories under it as mount points based on the drive name (label).

# /usr/local/libexec/hotplug-diskmount init

Enable hotplugd(8) so it starts at boot time.

# echo "hotplugd_flags=\"\"" >> /etc/rc.conf.local

You'll need to either create or add to your "/etc/hotplug/attach" script and remember to set executable bit. The following is a very simple example script.

#!/bin/sh

DEVCLASS=$1
DEVNAME=$2

case $DEVCLASS in
2)
        /usr/local/libexec/hotplug-diskmount attach "$DEVNAME"
        ;;
esac

To get it running, either start or restart hotplugd(8) and hotplug-diskmount will be enabled.

# /etc/rc.d/hotplugd restart

By default hotplug-diskmount will mount the disk as the "root" user and "wheel" group with chmod mode permissions set to 0755. These can be changed with the "-u" "-g" and "-m" flags (or the "-r" flag if you want read-only) in your "/etc/hotplug/attach" script. Also by default, hotplug-diskmount will always run fsck(8) before mounting the file systems, but you can control this behavior with the "-f" flag.

What You Get

When you insert a USB thumb drive, SD card, CD/DVD, or similar disk device, all of the file systems on the device will be automatically mounted under "/vol/DRIVE_NAME" and the name is based on the label of the disk. For example, the following shows you the label of the CDROM for most recent OpenBSD snapshot:

$ /sbin/disklabel cd0 2>&1 | sed -n '/^label: /s/^label: //p'
5.4 Install CD

If you're still running OpenBSD 5.3 you might want to note that a new version of hotplug-diskmount is available even though it's not in the 5.3 ports tree. The newest 0.8 version is already available in the post-5.4 snapshots ports tree.

If you want to contribute to hotplug-diskmount, there is a page for recent hotplug-diskmount announcements and the public mecurial repository is here:

$ hg clone https://bitbucket.org/alex_vatchenko/hotplug-diskmount

(Comments are closed)


Comments
  1. By Morgan (92.103.142.226) on

    Nice plugin but how to unmount devices ? are the directories in "/vol" deleted when the device is unplugged ?

    Thanks !

    Comments
    1. By Alex Vatchenko (92.113.6.133) av@bsdua.org on www.bsdua.org

      > Nice plugin but how to unmount devices ?

      I wrote graphical tool to unmount devices:
      http://www.bsdua.org/tray-app.html

      > are the directories in "/vol" deleted when the device is unplugged ?

      yes.

      Comments
      1. By Morgan (62.35.248.148) on

        > > Nice plugin but how to unmount devices ?
        >
        > I wrote graphical tool to unmount devices:
        > http://www.bsdua.org/tray-app.html
        >
        > > are the directories in "/vol" deleted when the device is unplugged ?
        >
        > yes.

        Thanks a lot

  2. By Anonymous Coward (37.162.169.97) on

    This is a nice tool and I've been using it since it was released. However you should note that another tool was recently released which does about the same job: Toad (sysutils/toad).
    The advantage of Toad is that it follows the udev path hierarchy and file managers automatically pick up the newly inserted device. Plug in the device and see i.e. Thunar pop up automatically. It also automounts CDs. The disadvantage is that it depends on ugly stuff like consolekit etc.

  3. By Zé Loff (81.193.33.144) zeloff@zeloff.org on

    Just a minor heads-up: the "attach" script will pickup softraid volumes as they come online on boot and mount them under "/vol".
    I have full disk encryption, so I ended up with a bunch of "/vol/SR CRYPTO" folders, although only one of them was mounted (the root fs). Nothing a small 'if [ $DEVNAME != "foo" ]' couldn't fix.

    Anyway, thanks for the tool!
    Cheers

Latest Articles

Credits

Copyright © - Daniel Hartmeier. All rights reserved. Articles and comments are copyright their respective authors, submission implies license to publish on this web site. Contents of the archive prior to as well as images and HTML templates were copied from the fabulous original deadly.org with Jose's and Jim's kind permission. This journal runs as CGI with httpd(8) on OpenBSD, the source code is BSD licensed. undeadly \Un*dead"ly\, a. Not subject to death; immortal. [Obs.]