2

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.

jlanssie
  • 42
  • 1
  • 6

2 Answers2

3

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.

Raffa
  • 24,905
  • 3
  • 35
  • 79
  • @jlanssie **20.04** comes with **systemd**, but for **upstart** anyway, skip the third step and just reboot. – Raffa Jan 27 '21 at 09:35
  • jammy 22.04 doesn't have usbmount package. Do you know if it just got renamed? – dgrogan Nov 04 '22 at 07:15
  • @dgrogan Yes, that's true. Unfortunately, it appears to be removed altogether from the official repositories for 22.04 ... You, can, however, either still [build it from source](https://github.com/rbrito/usbmount) or you can use a custom made script instead which is described in detail in [my other answer here](https://askubuntu.com/a/1417803) ... I hope this helps :-) – Raffa Nov 04 '22 at 12:32
-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: jlanssie@gmail.com
# COPYRIGHT: 2021 GNU
# VERSION: 1.0
# REQUIRES: mount

/opt/mount-lib.sh mount

/etc/sysctl.d
/etc/init.d/procps restart

exit 0
jlanssie
  • 42
  • 1
  • 6