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

  • 1
    Why are you not using an out of the box media centre like OpenElec or something similar? They have all the quirks ironed out. – Dr_Bunsen Apr 23 '17 at 15:57
  • 3
    Because I want to learn more about Linux – pauliucxz Apr 24 '17 at 5:25

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/usbstick-handler@.service

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
  • Thanks. I had to remove the --sync option. The write speed was very slow on my USB external drive – Mr Hyde Jan 28 at 16:47
up vote 4 down vote accepted

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/usbstick-handler@.service
  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 mointpoint -1 /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. 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.
  • 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. – Dave Lawrence Sep 3 '17 at 19:00
  • Also you missed a 0 on /usr/bin/pmount --umask 00 -noatime -r --sync $1 usb4 ? – GramThanos Feb 3 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. – homersimpson Mar 18 at 23:13

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.

  • 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 Apr 23 '17 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 Apr 23 '17 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 Apr 24 '17 at 12:06

protected by Community Jul 30 at 9:35

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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