Raspberry Pi USB Auto Mount

From ProjectPages
Jump to: navigation, search

This information came from here Auto mount USB stick on plug-in without UUID


Requirements

Install pmount if not installed sudo apt-get install pmount 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

Create file /etc/udev/rules.d/usbstick.rules

Insert:

ACTION=="add", KERNEL=="sd[a-z][0-9]", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k"

Save and close

Systemd service

Create file /lib/systemd/system/usbstick-handler@.service

Insert:

[Unit]
Description=Mount USB sticks
BindsTo=dev-%i.device
After=dev-%i.device

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/cpmount /dev/%I
ExecStop=/usr/bin/pumount /dev/%I

Save and close

Mount script

Create file /usr/local/bin/cpmount

Insert:

#!/bin/bash
if mountpoint -q /media/usb1
then
   if mountpoint -q /media/usb2
   then
      if mountpoint -q /media/usb3
      then
         if mountpoint -q /media/usb4
         then
             echo "No mountpoints available!"
             #You can add more if you need
         else
             /usr/bin/pmount --umask 000 --noatime -w --sync $1 usb4
         fi
      else
         /usr/bin/pmount --umask 000 --noatime -w --sync $1 usb3
      fi
   else
      /usr/bin/pmount --umask 000 --noatime -w --sync $1 usb2
   fi
else
   /usr/bin/pmount --umask 000 --noatime -w --sync $1 usb1
fi
Give execute permission to the (root) user:
chmod u+x /usr/local/bin/cpmount

Save and close

Finish

Reboot your RPi and test.