Automount USB-Stick on Linux

If you have a dedicated linux pc like the Raspberry Pi for your printer with a touch display, you might also like to be able to upload files directly at the printer. With a small extension cord you can put one usb output to your printer front, where you could connect a usb flash drive. If you try this you might detect one of two problems. The stick is not mounted or every stick gets a different mounting point. What you really want is a single mounting point that we can enter in the servers folder list, so we can just select the folder from the printer front end.

In this tutorial I will show how to do this on a Raspberry Pi running Jessie. On other linux distributions it will work similar. I will mention the points to watch out for you. When we are finished, every usb stick connected will be mounted to /media/flash.

Adding the Mount/Unmount Scripts

First we need two small scripts to do the mount and unmount. It does not matter where you install them, but we will create them in the Repetier-Server storage directory.

cd /var/lib/Repetier-Server
mkdir scripts
cd scripts

Now we create the mount script:

sudo nano mountflash

With the following content:

#!/bin/bash
if [ -b "/dev/usbmemorystick" ]
then
	mkdir /media/flash
	chmod 777 /media/flash
	chown repetierserver /media/flash
	mount -o uid=repetierserver /dev/usbmemorystick /media/flash
fi

As you see, we mount the drive only when the special device /dev/usbmemorystick is present. Later we will create this if a usb stick is connected. We make the drive for repetierserver accessible and mount it with this user. That way the server has read and write access if required. Next is the unmount script:

sudo nano unmountflash

With this content:

#!/bin/bash
umount /media/flash
rmdir /media/flash

Next we make the two scripts executable:

sudo chmod 755 mountflash unmountflash

Automounting

Now it is time to tell linux that we want the usbmemorystick and to run our two scripts at the right time. This is done with two simple udev rules.

sudo nano /etc/udev/rules.d/10-flash.rules

With this content:

KERNEL=="sda1", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/var/lib/Repetier-Server/scripts/mountflash", SYMLINK+="usbmemorystick"
KERNEL=="sda1", SUBSYSTEMS=="usb", ACTION=="remove", RUN+="/var/lib/Repetier-Server/scripts/unmountflash"

The thing you must be careful about is the KERNEL parameter. If you have a fixed hard drive it will most likely have the sda identifier. In that case the next drive connected gets sdb as device identifier. As the Raspberry has only a sd card, it is sda1 in this case. The RUN parameter call our two scripts. If you have chosen a different position, update the path accordingly. The SYMLINK creates our special device we use for mounting.

Recent linux versions use newer udev version where mounts are executed in a sandbox. Therefore you also need to edit the file /lib/systemd/system/udev.service

sudo nano /lib/systemd/system/udev.service

and make sure the fiel contains in section Service the line

MountFlags=shared

to tell udev to not mount into a sandbox.

All you now have to do is reboot your pc and test if it works. Add the folder in Repetier-Server only if a drive is connected or you can not browse to it.