Upstart to automount USB in Ubuntu Server 20.04

I would like to have my home Ubuntu server automount USB devices. Currently, I mannually write

sudo mount /dev/sda1 /media/library

However, 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/library

I experimented with stanzas as well, such as

script sudo mount /dev/sda1 /media/library
end script

But 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 update

Then run:

sudo apt install usbmount

Then run:

sudo systemctl restart udev

Done. When a USB storage device is connected, it will be mounted under /media/usb[0-7] and it will be unmounted when disconnected.

1

I solved my problem by writing a script and executing that script on boot by rc.local:

  1. 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
  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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like