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 – Reboot
You’ll see your USB drives mounted under /media