Automatically exporting USB drives over Samba

Submitted by davidc on Wed, 22/08/2012 - 23:59

For my new NAS, I wanted newly-connected USB disks to be automatically mounted and then exported over Samba. Debian's usbmount program is used to handle the mounting, and a couple of scripts handle the Samba export.

usbmount

Debian has the usbmount package available, a handy program that runs independent of any GUI. udev calls it when a USB disk is inserted, and usbmount then mounts the disk automatically. It also executes any scripts in /etc/usbmount/mount.d using run-parts (and the equivalent /etc/usbmount/umount.d upon disconnection). This allows a custom script to be inserted to handle the Samba exporting.

usbmount by default will mount into the directories /media/usb0 through /media/usb7.

smb.conf

I didn't want to edit the smb.conf directly. Samba does have an include directive - it doesn't allow you to include a directory, but it does quietly ignore any include files that don't exist.

Create the empty directory /etc/samba/auto, then add the following lines to the bottom of smb.conf:

include = /etc/samba/auto/usb0.conf
include = /etc/samba/auto/usb1.conf
include = /etc/samba/auto/usb2.conf
include = /etc/samba/auto/usb3.conf
include = /etc/samba/auto/usb4.conf
include = /etc/samba/auto/usb5.conf
include = /etc/samba/auto/usb6.conf
include = /etc/samba/auto/usb7.conf

mount.d

Next, a script to automatically create the above configuration files when a drive is inserted. Create the file /etc/usbmount/mount.d/50_add_samba_export with the following contents, and chmod it executable.

#!/bin/bash
SHARENAME=`basename $UM_MOUNTPOINT`
cat > /etc/samba/auto/$SHARENAME.conf <<EOF
[$SHARENAME]
   comment = $UM_VENDOR $UM_MODEL
   path = $UM_MOUNTPOINT
   read only = no
EOF
 
/etc/init.d/samba restart

Obviously you can put anything you like in the configuration file fragment in order to set the share up the way you want it.

Note that I force a restart of Samba, rather than a reload. This is up to you - a reload will not interrupt existing connections, but it will mean users have to disconnect and reconnect in order to see the new share. A restart will briefly interrupt connections, but will let them see the new share immediately. Choose whichever works best for you.

umount.d

Create a similar script to remove the share when the disk is unmounted or removed. This goes in /etc/usbmount/umount.d/50_remove_samba_export - again, remember to chmod it +x.

#!/bin/bash
SHARENAME=`basename $UM_MOUNTPOINT`
rm -f /etc/samba/auto/$SHARENAME.conf
 
/etc/init.d/samba restart

Again, choose whether to reload or restart.

Final tidying up

If the machine reboots without cleanly unmounting, the shares will appear again even though the drive may no longer be connected. To handle this eventuality, create an boot script to clear out /etc/samba/auto.

Since Debian have kindly removed the local administrator's rc.boot feature, you have to do this the complicated way using an init script, say /etc/init.d/auto-share-clean

#! /bin/sh
### BEGIN INIT INFO
# Provides:          auto-share-clean
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Cleans auto samba shares
### END INIT INFO
 
rm -f /etc/samba/auto/*

Then chmod +x /etc/init.d/auto-share-clean and update-rc.d auto-share-clean defaults.

Further work

There's probably something clever that can be done here with the UUID and persistent naming - i.e. exporting the share with a name that is persistent across reboots/reconnections. For example, with two USB drives connected, which drive is mounted first can vary, which isn't great for persistent network path names.

simonsayz

Tue, 17/03/2015 - 13:50

I have been looking for the way to do this for a long time. Thanks. Strange thing is that no matter what I try, every time I connect to the share with my Mac OS X, the system tells me "Can't connect, Original element unavailable". Can you help me guys?