Skip to content

Instantly share code, notes, and snippets.

@fabricionaweb
Last active September 28, 2022 18:39
Show Gist options
  • Save fabricionaweb/f4647f5bab87eecf2c02d0f7acc21f71 to your computer and use it in GitHub Desktop.
Save fabricionaweb/f4647f5bab87eecf2c02d0f7acc21f71 to your computer and use it in GitHub Desktop.

Sources:

  1. https://raspberrypi.stackexchange.com/a/66324
  2. https://raspberrypi.stackexchange.com/a/72518
  3. https://github.com/avanc/mopidy-vintage/wiki/Automount-USB-sticks

So, I found a solution that works quite well. Big thanks to avanc and his udev rule that makes this possible. I also modified it so that it could mount up to 4 flash drives at the same time (it can be increased if needed).

Requirements

  1. Install pmount if not installed sudo apt-get install pmount.
  2. This script mounts drives to /media/usb*, so make sure those folders aren't occupied. If you want a cleaner look, don't create any folders.

Udev rule

  1. Create file /etc/udev/rules.d/usbstick.rules
  2. Insert: ACTION=="add", KERNEL=="sd[a-z][0-9]", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k"
  3. Save and close.

Systemd service

  1. Create file /lib/systemd/system/usbstick-handler@.service
  2. Insert:
[Unit]
Description=Mount USB sticks
BindsTo=dev-%i.device
After=dev-%i.device

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/automount %I
ExecStop=/usr/bin/pumount /dev/%I
  1. Save and close.

Mount script

  1. Create file /usr/local/bin/automount
  2. Insert:
#!/bin/bash

PART=$1
FS_LABEL=`lsblk -o name,label | grep ${PART} | awk '{print $2}'`

if [ -z ${FS_LABEL} ]
then
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/${PART} /media/${PART}
else
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/${PART} /media/${FS_LABEL}_${PART}
fi
  1. Save and close.
  2. Give the permitions sudo chmod +x /usr/local/bin/automount.

Finish

  1. Reboot your RPi and test.

Notes

  1. You can change pmount parameters, but these allow anyone r/w access to usb.
  2. The amount of mountpoints can be changed.
  3. Thanks to avanc for his udev rule and service.
@fabricionaweb
Copy link
Author

[Global]
vol preset = default_for_all_vol
hostname = RaspberryPi
log file = /var/log/netatalk.log
log level = default:error
uam list = uams_dhx.so, uams_dhx2.so
save password = yes
keep sessions = yes
mimic model = Xserve

[default_for_all_vol]
file perm = 0665
directory perm = 0775
valid users = @pi

;[Homes]
;path = library
;basedir regex = /home
;home name = "Library"

[Library]
time machine = no
path = /media

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment