I would like to have my home Ubuntu server automount USB devices. Currently, I mannually write
sudo mount /dev/sda1 /media/libraryHowever, I tried to write an Upstart script, start-library.conf in /etc/init, but that did not work. Am I doing something wrong? The contents of that file are:
exec sudo mount /dev/sda1 /media/libraryI experimented with stanzas as well, such as
script sudo mount /dev/sda1 /media/library
end scriptBut that did not work either. Am I missing something in the Upstart template? Or does it just not work because those usb devices haven't loaded yet at the time this script exectutes? Can I do something about that? specify the runlevel? If so, which one? Etc. Idk.
2 Answers
usbmount is the tool to automatically mount and unmount USB mass storage devices.
You can install it like so:
Run in the terminal:
sudo apt updateThen run:
sudo apt install usbmountThen run:
sudo systemctl restart udevDone. When a USB storage device is connected, it will be mounted under /media/usb[0-7] and it will be unmounted when disconnected.
I solved my problem by writing a script and executing that script on boot by rc.local:
- Script. Change the label "MYUSB" to the label of your usb device.
#!/bin/bash
# FUNCTION: Mount or unmount usb device to library
# ARGUMENTS: none
# AUTHOR: Jeremy Lanssiers
# COPYRIGHT: 2021 GNU
# VERSION: 1.0
# REQUIRES: mount
# Check arguments
if [ -z "$1" ] || [[ ( $1 != "mount" ) && ( $1 != "umount" ) ]]
then echo "USAGE: [mount/umount] [device]" exit 1
fi
# Set variables
DEVICE=$(blkid | egrep -o '\/dev\/(.*)\:\ LABEL="MYUSB"')
DEVICE=$(echo $DEVICE | egrep -o '\/dev\/[a-zA-Z0-9]{1,10}')
LIBRARY=/media/library
# Mount or unmount library
if [ $1 = "mount" ]
then mount $DEVICE $LIBRARY exit 0
fi
if [ $1 = "umount" ]
then umount $DEVICE $LIBRARY exit 0
fi
exit 1- rc.local
#!/bin/bash
# /etc/rc.local
# FUNCTION: execute scripts at startup
# ARGUMENTS: none
# AUTHOR:
# COPYRIGHT: 2021 GNU
# VERSION: 1.0
# REQUIRES: mount
/opt/mount-lib.sh mount
/etc/sysctl.d
/etc/init.d/procps restart
exit 0