20

I want to be able to automount USB sticks fat32 and ntfs formatted on the fly, like hot-plugging without rebooting the my Raspberry Pi 2. I have ntfs-3g installed on jessie-lite.

Putting the UUID in fstab is not an option because I'd have to find the UUID of every device. I want to be able to plug different USB devices with different file-formats in and mount them automatically. Is this possible?

Also I once nearly got it working with the usbmount program. I could access my drive for a second before getting the error transport endpoint is not connected. I reinstalled the program but then I never even got to that point again, not even with the error.

Ederbit
  • 501
  • 1
  • 3
  • 10

3 Answers3

20

After alot of research I could fix my Problem with usbmount:

Adding ntfs to usbmount

  1. Install usbmount with sudo apt-get install usbmount.
  2. Install NTFS driver package ntfs-3g with sudo apt-get install ntfs-3g.

  3. Configure usbmount to mount specified filesystems by opening the usbmount file with sudo nano /etc/usbmount/usbmount.conf.

In here there is a line called FILESYSTEMS="". Only filesystems specified in this line are mounted via usbmount, so we change it to : FILESYSTEMS="vfat ntfs fuseblk ext2 ext3 ext4 hfsplus"

If you want usbmount to mount NTFS Filesystems, be sure to add ntfs and fuseblk to the line. NTFS devices are sometimes listed as fusblk by the ntfs-3g package, so those two are the same. I don't really know why though.

Also, if you don't add fuseblk in the configuration, unplugged NTFS Devices will not automatically unmount. They will stay mounted and keep the folder they are mounted to until the device is manually unmounted with unmount /dev/sda1 --> where sda1 could be another variable.

The next important line is FS_MOUNTOPTIONS="". Here you specify which filesystems should be mounted and how they should be mounted.

We change it to: FS_MOUNTOPTIONS="-fstype=ntfs-3g,nls=utf8,umask=007,gid=46 -fstype=fuseblk,nls=utf8,umask=007,gid=46 -fstype=vfat,gid=1000,uid=1000,umask=007"

With this the filesystems vfat (fat32) ntfs-3g (NTFS), and fuseblk (NTFS again) are mounted. I think most parameters can stay the same. Again here just add ntfs-3g (add ntfs-3g not ntfs) and fuseblk to be able to mount all NTFS Files. For more filesystems add more lines starting with -fstype=.

Keep NTFS mounted untill device is unplugged

Here is a solution for the problem, when the mounted NTFS drive is only accessible for a few seconds. This fix comes from Christian Weinberger.

  1. Create the file usbmount.rules in /etc/udev/rules.d/ with sudo nano /etc/udev/rules.d/usbmount.rules.

This is the content:

KERNEL=="sd*", DRIVERS=="sbp2",         ACTION=="add",  PROGRAM="/bin/systemd-escape -p --template=usbmount@.service $env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
KERNEL=="sd*", SUBSYSTEMS=="usb",       ACTION=="add",  PROGRAM="/bin/systemd-escape -p --template=usbmount@.service $env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
KERNEL=="ub*", SUBSYSTEMS=="usb",       ACTION=="add",  PROGRAM="/bin/systemd-escape -p --template=usbmount@.service $env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
KERNEL=="sd*",                          ACTION=="remove",       RUN+="/usr/share/usbmount/usbmount remove"
KERNEL=="ub*",                          ACTION=="remove",       RUN+="/usr/share/usbmount/usbmount remove"
  1. Create the file usbmount@.service in /etc/systemd/system/ with sudo nano /etc/systemd/system/usbmount@.service.

This is the content:

[Unit]
BindTo=%i.device
After=%i.device

[Service]
Type=oneshot
TimeoutStartSec=0
Environment=DEVNAME=%I
ExecStart=/usr/share/usbmount/usbmount add
RemainAfterExit=yes

Now reboot and check with cat /etc/mtab to which folder the usb devices are mounted to. By Default they are mountet to /media/usbstick0.

Source:

Christians Fix

Why fusblk?

Ederbit
  • 501
  • 1
  • 3
  • 10
3

The answer by Untermoser is correct, but it will break some of usbmount functionality.

Because he is using Systemd units to spawn processes to mount (to enable NTFS), the environment variable $DEVPATH is no longer sent to usbmount add command, resulting in the symlink creation failure.

To fix that, run the following:

sed -i '/# Determine vendor/,/if/ s/\(.*vendor=\)/if [ -z "$DEVPATH" ]; then DEVPATH="$(udevadm info --query=path --name=\/$DEVNAME)"; fi\n\1/g' /usr/share/usbmount/usbmount

The command above recreates $DEVPATH if it's not available.

There's also another bug in usbmount, with respect to usbmount remove. It loops over mountpoints, and the one that is defined in usmount config is unmounted. However, if a match is found but is not in that list, remove breaks and does nothing. This is particularly crucial if you create multiple mountpoints for one USB, because usbmount sees the first and does nothing.

To fix, the following change is needed:

sed -i '/umount.d ||/,/break/ s/break.*//; /umount.d ||/,/done/ s/:/:\n\t\tbreak 2/' /usr/share/usbmount/usbmount

This will move the break inside the inner loop (where success is guaranteed, and break 2 steps out).

AbiusX
  • 131
  • 3
1

For those that are trying to get the same to work on the newer Stretch (Lite) and are finding that the answers by Untermoser and AlbiusX don't unmount vfat drives, you'll need to follow these extra steps in addition to their provided answers:

Run the following command in terminal:

sudo mkdir -p /etc/systemd/system/systemd-udevd.service.d/

This will create the required folders needed. Next, create a conf file here, e.g:

sudo nano /etc/systemd/system/systemd-udevd.service.d/shared-mount-ns.conf

Inside this file add the following lines:

[Service] MountFlags=shared

This will change systemd-udevd's default behavior of private filesystem namespaces to use shared ones, which is equivalent to the behavior of udev in jessie.

(Sourced from https://unix.stackexchange.com/questions/330094/udev-rule-to-mount-disk-does-not-work/330156#330156)

Nacsiar
  • 11
  • 3
  • So this was the problem all along -- I was looking at outdated docs. Am running Linux raspberrypi 4.14.79-v7+ on the Pi, for reference – ctietze May 31 '19 at 07:47