0

A while ago I got an udev script to run when an usb-stick is inserted into Ubuntu 16.04 (server) machine. (I've been using that to mount stick to virtualbox guest.)

Now I want to do something different in that script (mount it in the host). I've got an udev rule:

#/etc/udev/rules.d/10-usbmount.rules
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/bin/su <username> -c /home/<username>/automount"

It calls the automount script which is currently:

LOG_FILE=/home/<username>/usb_log
echo "New usb device detected at $DEVNAM doing stuff
mount $DEVNAME /home/<username>/flashes_folder &>> $LOG_FILE
#mount /dev/sdb1 /home/<username>/flashes_folder &>> $LOG_FILE
ret=$?
echo "$ret" >> $LOG_FILE

(taken from here) The log file contains the following output:

New usb device detected at /dev/sdb doing stuff
mount: only root can do that
1

Explicitly entering the disk path /dev/sdb1 (commented line) does result in the same error (the disk indeed is /dev/sdb1). Apparently, /bin/su <admin_username> has not enough privileges. How do I fix that?

2

For an ordinary user to mount a volume (even if he has complete access to the device file) an entry in /etc/fstab is necessary. This obviously does not work well with dynamic device names.

udev can only add symlinks but not change the name of a block device. But the fstab entry needs the real name.

But instead of using the device name you could use the filesystem UUID. udev can make the device readable for your user and set a symlink so that your user script can determine the UUID.

fstab entry

UUID=8186709a-ab1f-4d18-80bb-b219cb126398 /mnt/tmp   ext4       defaults,noauto,user,nofail 0 0

mount script

current_uuid=$(blkid --output export /dev/myusb | grep ^UUID=)
mount "$current_uuid"

device mapper

A different approach would be to put a device mapper device on top of the USB device (man dmsetup). The advantage is that you get a static name that way, e.g. /dev/mapper/myusb. But I do not know how the DM device reacts if you pull the USB stick. You may need udev unplug rules to handle that. This seems much more complicated and is probably not worth the static name.

| improve this answer | |
0

I've solved my problem without touching /etc/fstab by using pmount program. This program seems to be available in package repositories of multiple linux distributions.

On ubuntu automatically creates folders inside /media and mounts the disks to those. There seems to be no way to mount disks outside the /media but I personally have come to terms with that.

Program takes partition identifier (e.g. sdb1) as an argument. $DEVNAME available in udev script provides a disk identifier (e.g. sdb). With flash sticks it is reasonable to expect a disk to have only firs partition (sdb1).

So the working version of the script in the question would be:

LOG_FILE=/home/<username>/usb_log
echo "New usb device detected at $DEVNAM doing stuff
pmount "{$DEVNAME}1" &>> $LOG_FILE
ret=$?
echo "$ret" >> $LOG_FILE

As a final note: the script is run as root user and the script file must belong to the root. (on ubuntu)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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