I'd like to create a "network USB hub", a central device, where all the coworkers plug all the USB drives, and then everyone can access them.
I wonder how I could set (headless) Ubuntu to automatically mount and share on SMB all the USB drives.
31 Answer
The procedure below is described in greater detail in the articleAutomatically exporting USB drives over Samba:
The tool used is usbmount, which is called when a USB disk is inserted to
mount the disk. It executes the scripts in /etc/usbmount/mount.d using run-partsand /etc/usbmount/umount.d upon disconnection.
smb.conf
Create the empty directory /etc/samba/auto and add these lines at the end ofsmb.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.confmount.d
To automatically create the above configuration files when a drive is inserted,
create this file as /etc/usbmount/mount.d/50_add_samba_export, chmod as 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 restartumount.d
To remove the share when the disk is unmounted or removed, add in/etc/usbmount/umount.d/50_remove_samba_export, chmod as +x:
#!/bin/bash
SHARENAME=`basename $UM_MOUNTPOINT`
rm -f /etc/samba/auto/$SHARENAME.conf
/etc/init.d/samba restartTidying up
To handle the case of a reboot without previously cleanly unmounting,
create a boot script to clear out /etc/samba/auto.
Use 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
update-rc.d auto-share-clean defaults