USB flash drives automatically mounted (headless computer)

For the project SamplerBox, up to now I was using /dev/sda1 /media auto nofail 0 0 to have USB flash drives automatically mounted when inserted on the headless computer, see also Auto-mount and auto-remount with /etc/fstab. But this seems not very reliable, for example, when an USB flash drive is removed, and then re-inserted.

What lightweight and easy-to-configure solution is available in Debian to automatically mount every /dev/sd* device to /media/?

  • If a second flash drive is plugged, ignore or mount to another folder /media2/
  • If a drive is removed (even without a proper umount), and then re-inserted a few minutes later it should be mounted again

The use case is a headless device on which the end user can plug USB flash drives, and it should be always recognized (no matter if they removed the previous USB flash drive without asking permission in the software).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Based on @FelixJN’s comment, I slightly modified this excellent guide by
Andrea Fortuna
according to my needs and here is the solution:

  1. Create a file /root/usb-mount.sh containing this (and add +x permission):
    #!/bin/bash
    ACTION=$1
    DEVBASE=$2
    DEVICE="/dev/${DEVBASE}"
    MOUNT_POINT=$(/bin/mount | /bin/grep ${DEVICE} | /usr/bin/awk '{ print $3 }')  # See if this drive is already mounted
    case "${ACTION}" in
        add)
            if [[ -n ${MOUNT_POINT} ]]; then exit 1; fi          # Already mounted, exit
            eval $(/sbin/blkid -o udev ${DEVICE})                # Get info for this drive: $ID_FS_LABEL, $ID_FS_UUID, and $ID_FS_TYPE
            OPTS="rw,relatime"                                   # Global mount options
            if [[ ${ID_FS_TYPE} == "vfat" ]]; then OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"; fi     # File system type specific mount options
            if ! /bin/mount -o ${OPTS} ${DEVICE} /media/; then exit 1; fi          # Error during mount process: cleanup mountpoint
            ;;
        remove)
            if [[ -n ${MOUNT_POINT} ]]; then /bin/umount -l ${DEVICE}; fi
            ;;
    esac
  2. Create a file /etc/systemd/system/[email protected] containing:
    [Unit]
    Description=Mount USB Drive on %i
    
    [Service]
    Type=oneshot
    RemainAfterExit=true
    ExecStart=/root/usb-mount.sh add %i
    ExecStop=/root/usb-mount.sh remove %i
  3. Create a file /etc/udev/rules.d/99-local.rules containing:
    KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/bin/systemctl start <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a6a0b1febebca6bda793">[email protected]</a>%k.service"
    KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="remove", RUN+="/bin/systemctl stop <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="215452430c4c4e544f5561">[email protected]</a>%k.service"
  4. Restart the rules:
     udevadm control --reload-rules
     systemctl daemon-reload
  5. Plug a USB flash drive. It should be mounted to /media/.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x