Automount USB stick on OpenBSD

       287 words, 2 minutes

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

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