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

pauliucxz
  • 489
  • 1
  • 3
  • 10

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
  • 489
  • 1
  • 3
  • 10
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
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,378
  • 8
  • 26
  • 43