12

So, I'm making a media center. I need the Pi to auto mount ANY USB stick I plug in. No mater what filesystem (vfat,NTFS,ext). I searched everywhere, and couldn't find anything that works. Well, usbmount partially works. I can't get it to mount NTFS and make flash drives accessible to all users.

Running Raspbian Jessie Lite on RPI3

4

3 Answers 3

10

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/[email protected]
  2. 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
    
  3. Save and close

Mount script

  1. Create file /usr/local/bin/cpmount
  2. 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
    
  3. Give execute permission to the (root) user: chmod u+x /usr/local/bin/cpmount

  4. Save and close

Finish

Reboot your RPi and test.

NOTES

  • You can change pmount parameters, but these allow anyone r/w access to usb.
  • The amount of mountpoints can be changed.
  • Thanks to avanc for his udev rule and service.
8
  • This works well, except the Note (1) says this allows r/w access, but the "-r" option to pmount forces r/o. change each of the four lines above to "-w" to give full read-write access. Commented Sep 3, 2017 at 19:00
  • Also you missed a 0 on /usr/bin/pmount --umask 00 -noatime -r --sync $1 usb4 ?
    – GramThanos
    Commented Feb 3, 2018 at 16:37
  • I copied and pasted this script and it failed with errors. You need to add an extra 0 to the umask flag and an extra - to the noatime flag for your first else statement there, so the entire statement should be /usr/bin/pmount --umask 000 --noatime -r --sync $1 usb4. Commented Mar 18, 2018 at 23:13
  • 2
    @pauliucxz; Maybe also mention in your article that the file cpmount must have executable permission set for the user pi (it doesn't have that set by default, only rw permissions are set by default): sudo u+x cpmount (inside /usr/local/bin). It was not working for me just because of that reason. Now it works perfect...
    – GeertVc
    Commented Feb 10, 2019 at 14:02
  • Don't create the systemd service in /lib/systemd/system/, but use /etc/systemd/system/ instead. The former path is intended for services provided by installed packages, while the latter can be used by the system administrator for its own services. Commented Oct 9, 2019 at 11:40
8

My version based on the answer above:

Systemd service

Put:

[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

in /lib/systemd/system/[email protected]

Mount script

Put:

#!/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

In /usr/local/bin/automount, and then:

sudo chmod +x /usr/local/bin/automount

Reboot.

The mount points/folders names will be in the format of /media/<PartitionLabel>_<sdxy>. In case a partition has no label, it will just be /media/<sdxy>.

So, I normally label my USB drives with their capacity. e.g. 8G, 16G. When I plug in multiple USB disks with the same label, I can still distinguish them as, for example:

/media/500G_sdb1
/media/500G_sdc1
1
  • Thanks. I had to remove the --sync option. The write speed was very slow on my USB external drive
    – Mr Hyde
    Commented Jan 28, 2018 at 16:47
0

The best solution I have found for this is using the pmount service.

I used the normal process to install it:

sudo apt-get install pmount

The only thing you need to worry about is if you already have regular directories in /media/pi with the same names as a USB drive. If you do, it will append a numeral as it mounts the drive.

For instance, if you have a directory named /media/pi/STICK and then you install a flash drive of the name STICK, you will find it has mounted it at /media/pi/STICK1

This can happen if you have had the stick installed but not properly ejected as it was removed, before installing pmount.

When that happened to me, my programs happily kept writing to my STICK but when I pulled it out and plugged it into another computer, it was empty. It was writing to the directory, and not to the device.

So I renamed the directory to /media/pi/temp, installed pmount, mounted STICK and copied the contents of temp to /media/pi/STICK

Then when I eject it, and verify on another computer, it is doing precisely what I intended.

3
  • I installed it, but it doesn't do anything. When I plug in my falsh drive nothing happens. I checked /media and it's empty. I also checked lsblk and it wasn't mounted anywhere.
    – pauliucxz
    Commented Apr 23, 2017 at 10:02
  • It will show up under /media/pi - use file manager to drill down into there and look that way.. It should also prompt you for your password when it is installed. If it doesn't, then perhaps it is not recognizing that flash drive at all. Another way you can see it is df -h
    – SDsolar
    Commented Apr 23, 2017 at 18:24
  • fdisk -l shows the flash drive, if I type "pmount /dev/sda1 /media/pi/STICK" it mounts successfully. But df -h doesn't see it. I tried many drives, and they still wouldn't auto mount.
    – pauliucxz
    Commented Apr 24, 2017 at 12:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.