1

I'm using the RPI2 with Raspbian Jessie.

I made a C++ program which records video file on a USB dongle (/dev/sda1 mounted to /media/usb1)

I would like to be able to replace the USB dongle while my program is running. (hot swap)

Therefore, I think that my USB dongle must always stays on /dev/sda1.

To do so, I'm trying to use udev rules but It does not work. Right now, if my program is running while I perform the USB dongle exchange, then I got devices in /dev/sda, /dev/sdb, /dev/sdb1.

I created two udev rules files:

First, udev rule to force the device to be always: /dev/sda

sudo nano /etc/udev/rules.d/10-local.rules
KERNEL=="sd*", SUBSYSTEMS=="usb", ATTRS{product}=="Cruzer Fit", 
SYMLINK+="sda%n"

Second, udev rule to unmount or mount depending of the usb event:

sudo nano /etc/udev/rules.d/automount.rules
# automounting usb flash drives
# umask is used to allow every user to write on the stick
# we use --sync in order to enable physical removing of mounted memory 
sticks -- this is OK for fat-based sticks
ACTION=="add",KERNEL=="sda*", RUN+="/bin/mount --sync --umask 000 %k"
ACTION=="remove", KERNEL=="sda*", RUN+="bin/umount %k"

Is it possible that the umounting is not working because my program may still hold a handle to a file on the device ?

What is the best way to force a USB storage device to be always mounted to a specifc location? (Ex: /media/usb1)

  • You're trying to dismount the usb while a process is writing a video file to it and not lose any of the video data? – dbmitch Jul 24 '16 at 20:12
  • I suppose this could happen. Usually, the video is recorded based on the state of a GPIO. – ssinfod Jul 24 '16 at 22:33
  • This article may be useful. You are not supposed to call mount from a Udev rule because Udev tends to run in a private mount namespace, and because it won't work if mount starts a long-running process like FUSE. – Metamorphic Mar 19 at 11:10
1

You can edit your /etc/fstab file. In your case, you would need to add the following line to the file, replacing <UUID-of-usb> with the UUID of the partition of your USB flash drive:

UUID=<UUID-of-usb> /media/usb1 vfat defaults,user,dmask=027,fmask=137 0 0

See here for more details.

  • Does fstab mount the device only on boot? Would it unmount and remout if I replace the USB dongle while my program is running? – ssinfod Jul 24 '16 at 22:39
  • @ssinfod It will mount it whenever you mount a usb whit that uuid by its device path, or if you run mount -a – geek1011 Jul 25 '16 at 1:08
0

I found a udev rule on this webpage:

11-media-by-label-auto-mount.rules:

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"  
# Import FS infos  
IMPORT{program}="/sbin/blkid -o udev -p %N"  
# Get a label if present, otherwise specify one  
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"  
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"  
# Global mount options  
ACTION=="add", ENV{mount_options}="relatime"  
# Filesystem-specific mount options  
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"  
# Mount the device  
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"  
# Clean up after removal  
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"  
# Exit  
LABEL="media_by_label_auto_mount_end"

It is working great for me.

  • I also modified the following line to: ENV{ID_FS_LABEL}=="", ENV{dir_name}="usb%n" – ssinfod Jul 26 '16 at 17:20

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.