11

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

pauliucxz
  • 479
  • 1
  • 3
  • 9
  • 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
  • 6
    Because I want to learn more about Linux – pauliucxz Apr 24 '17 at 05:25
  • The best solution I found for this was https://github.com/Ferk/udev-media-automount based on the recommendation in https://wiki.archlinux.org/title/Udev#Mounting_drives_in_rules – Lee Ballard Feb 22 '22 at 16:59

3 Answers3

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/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 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.
Frido Emans
  • 103
  • 4
pauliucxz
  • 479
  • 1
  • 3
  • 9
  • 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 03 '17 at 19:00
  • Also you missed a 0 on /usr/bin/pmount --umask 00 -noatime -r --sync $1 usb4 ? – GramThanos Feb 03 '18 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 '18 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 Feb 10 '19 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. – steviethecat Oct 09 '19 at 11:40
  • @GeertVc your comment worked wonderfully for me, thanks! but you miss the term "chmod" in sudo chmod u+x /usr/local/bin/cpmount – Frido Emans Dec 01 '19 at 16:40
  • @xor: You're absolutely correct. Good catch! Sorry about that... – GeertVc Dec 02 '19 at 17:42
  • @GeertVc i added your suggestion to the procedure in the answer – Frido Emans Dec 03 '19 at 19:16
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/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
yy502
  • 181
  • 1
  • 2
  • Thanks. I had to remove the --sync option. The write speed was very slow on my USB external drive – Mr Hyde Jan 28 '18 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.

SDsolar
  • 2,348
  • 8
  • 25
  • 43
  • 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