Automount USB stick on OpenBSD

In my previous “using OpenBSD as a workstation” review, I was told by Landry that auto-mount of USB stick can be done with hotplugd(8). Let’s see if we can achieve something useful…

First of all, you have to read the manual page for hotplugd(8). Everything is there ; mostly. What we have to do is

  • enable hotplugd(8) on boot;
  • configure attach and detach scripts;
  • use it

Quite simple…

Enable hotplugd editing the rc file:

# vi /etc/rc.conf.local (...) hotplugd_flags="" (...)

Next, decide where the USB stick will be mounted and create the attach script:

# vi /etc/hotplug/attach #!/bin/sh DEVCLASS=$1 DEVNAME=$2 MOUNTROOT="/mnt" DEBUG=0 case $DEVCLASS in 2) # disk devices disklabel=`/sbin/disklabel $DEVNAME 2>&1 | \ sed -n '/^disk: /s/^disk: //p'` [ $DEBUG == 1 ] && logger -i "hotplugd descovered DISKLABEL $disklabel" case $disklabel in "SCSI disk") slices=`/sbin/disklabel $DEVNAME 2>&1 | \ sed -n '/^ *[abd-z]: /s/^ *\([abd-z]\):.*/\1/p'` for slice in ${slices}; do [ $DEBUG == 1 ] && logger -i "hotplugd attaching SLICE $slice of DEVICE $DEVNAME" [ ! -d $MOUNTROOT/$DEVNAME$slice ] && mkdir -p -m 1777 $MOUNTROOT/$DEVNAME$slice mount /dev/$DEVNAME$slice $MOUNTROOT/$DEVNAME$slice done ;; esac ;; 3) # network devices; requires hostname.$DEVNAME sh /etc/netstart $DEVNAME ;; esac # chmod 0755 /etc/hotplug/attach

Now, start hotplugd:

# /etc/rc.d/hotplugd start

That’s all. When you plug the USB stick, it gets mounted under /mnt. Use you shell or file manager to browse there.

When you are done, just unplug the USB stick. Note that you may wait a bit between the last write operation and the unplug process to ensure every data have been written on you stick. Or you may run a sync command in you preferred terminal.

Sources: Good amd config and hotplug config for OpenBSD, from Edd’s Rant


  • Hasse Tue, 13 Mar 2012 18:31:35 +0000 Reply

    Excellent 🙂
    Thank you very much.

  • BSDevil's corner » Blog Archive » OpenBSD: hotplugd – automount devices Mon, 14 May 2012 18:19:21 +0000 Reply

    […] address: Other scripts I tested didn’t work, so BIG THANKS goes to the author of this script, Joel Carnat who posted it on his blog. Thanks, […]

  • Ron Thu, 19 Mar 2015 01:56:39 +0000 Reply

    Thank you very much.

Leave a Reply

Your email address will not be published. Required fields are marked *


%d bloggers like this: