Last Updated on November 29, 2021.
When I’m building some retro arcade setups on a Raspberry Pi I’m often copying over some set of disk images, media files, configuration, or ROMs. They’re not always on the same network, and sometimes it’s just easier to take a keychain thumb drive and copy some files around. I’m not always in a windows environment on these boxes, either. I searched the web for a bit on how to do it and here’s the information I’ve compiled in a step by step format.
Step 1 – Install pmount
pmount is a wrapper around the standard mount program which permits normal users to mount removable devices without a matching /etc/fstab entry. This is how we’ll manage to auto mount our USB drives.
sudo apt install pmount
Step 2 – UDEV Rule
Udev allows for rules that specify what name is given to a device, regardless of which port it is plugged into.
sudo vi /etc/udev/rules.d/usbstick.rules
ACTION=="add", KERNEL=="sd[a-z][0-9]", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k"
Step 3 – Create Systemd service
sudo vi /lib/systemd/system/usbstick-handler@.service
[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
Step 4 – Create Mount Script
sudo vi /usr/local/bin/automount
#!/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
sudo chmod +x /usr/local/bin/automount
Step 5 – Should be working, but if not…
You’ll see your USB drives mounted under /media
Per the comment from James:
And rather than reboot, UDEV should automatically detect new rules[2], but if it fails to:
udevadm control –reload
If that doesn’t work… you can always try turning it off and on again š
Awesome MAN….
Thank you!
Thanks for this. I’m planning to use it on a headless Pi running MPD to add music when it finds a specific folder on a USB stick when the USB stick is plugged in. One thing to note, according to Arch Linux documentation[1]:
/usr/lib/systemd/system/
: units provided by installed packages/etc/systemd/system/
: units installed by the system administratorAnd rather than reboot, UDEV should automatically detect new rules[2], but if it fails to:
Thank you, James!
Since it doesn’t work for me I extracted this into a separate script:
and did run in on RPI 4 BullsEye command line.
Result is
Usage: grep [OPTION]… PATTERNS [FILE]…
Try ‘grep –help’ for more information.
What exactly do you want to achieve? Do you know what $1 is in practice? Can only guess “sda1” in my case…
I’ve not tried this on the latest Bullseye. Things always have to change. š I was assuming sda1 or the like myself, since the parameter is fed in from the service. If I get a chance to try this on the latest version of Raspbian soon I’ll see if I can tweak it. This article in particular was a bit of me scrounging around per the references.
Sorry formatting messed up. The “up ticks” are there
I got this working on a Raspberry Pi 4, but I tries it on a Raspberry Pi 5 and it failed. Any idea why?
I’m sorry, Nicholas – I don’t have a V5 to try this out on and see! This is an older post though, so perhaps there are more updated methods to do this. If I run across any or can bench test this on a 5 I’ll update the post. Good luck!