Last active
June 21, 2019 03:03
-
-
Save cauethenorio/86cf8ff276a03fb945792f7777121d03 to your computer and use it in GitHub Desktop.
Automount USB for SystemD OS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Source | |
# https://www.raspberrypi.org/forums/viewtopic.php?t=192291 | |
# https://raspberrypi.stackexchange.com/questions/66169/auto-mount-usb-stick-on-plug-in-without-uuid | |
set -e | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root" | |
exit 1 | |
fi | |
# 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. | |
apt-get install pmount | |
# Udev rule | |
cat > /etc/udev/rules.d/usbstick.rules <<- EOM | |
ACTION=="add", KERNEL=="sd[a-z][0-9]", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k" | |
EOM | |
# Systemd service | |
cat > /lib/systemd/system/[email protected] <<-EOM | |
[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 | |
EOM | |
cat > /usr/local/bin/automount <<-EOM | |
#!/bin/bash | |
PART=\$1 | |
FS_LABEL=\`lsblk -o name,label,UUID | grep \${PART} | awk '{print \$2"-"\$3}'\` | |
if [ -z \${FS_LABEL} ] | |
then | |
/usr/bin/pmount --umask 000 --noatime -w /dev/\${PART} /media/\${PART} | |
else | |
/usr/bin/pmount --umask 000 --noatime -w /dev/\${PART} /media/\${FS_LABEL} | |
fi | |
EOM | |
chmod +x /usr/local/bin/automount | |
echo "Reboot to continue" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment