Duncb82
Posts: 7
Joined: Tue May 21, 2013 6:22 am

Mount ANY usb drive in same location

Wed May 22, 2013 7:43 am

I'm looking for a way for any USB drive plugged in to mount to the same location

Almost all the information I've found has been for the opposite, having a specific USB drive mount in the same location.

I assume this can be done using udev rules, but I'm not sure of the details. There will never be a situation where 2 USB drives are plugged in at the same time,

thanks

sdjf
Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: Mount ANY usb drive in same location

Wed May 22, 2013 2:24 pm

I can think of ways to write a script that would pull out the device information and then mount the current drive to a specified mount point, but am wondering if there is an easier, more straightforward way to accomplish the task.

I would think you then could call that script with a udev rule that watched for some parameter that was true of all your usb drives, and then ran the script.

But cannot really test this as do not have a usb drive.

When you run lsusb -v, is there anything unique to your drives that you could use as a selection parameter for such a udev rule? Are they the only devices you own that have a specific manufacturer? Or maybe keywords in the description that appears in lsusb -v or in /var/log/everything.log for them?
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

User avatar
rpdom
Posts: 17171
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Mount ANY usb drive in same location

Wed May 22, 2013 4:25 pm

There are a number of things that can be used to identify a USB drive in udev rules.

For example, manufacturer and serial number, the UUID, or the label of one of the partitions for a start.

The last two can be found with the blkid command when the drive is connected.

Duncb82
Posts: 7
Joined: Tue May 21, 2013 6:22 am

Re: Mount ANY usb drive in same location

Wed May 22, 2013 4:54 pm

The idea is that there is nothing in common to the drives, simply that they are connected via USB

The RPi is being used to display a slide show of images pulled from the usb drive. Ideally anyone should be able to use the drive they have to display pictures on it.

I'll try running lsusb -v on as many drives as I can find and see if there is a common string

User avatar
rpdom
Posts: 17171
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Mount ANY usb drive in same location

Wed May 22, 2013 5:32 pm

Well, you'd be better off looking at udevadm to see how to get the attributes of the drive.

If you are saying that you want *any* usb drive to mount at (for example) /media/usb, that should be easy. In the udev rule the device should show up as /dev/sda1 (first partition on first drive using the "scsi" compatible sd driver).

I think this will work, put this code in /etc/udev/rules.d/99-usbdisk.rules

Code: Select all

KERNEL=="sda1", ACTION=="add", RUN="/bin/mount /dev/sda1"
and have the mount point set in /etc/fstab

You will have to unmount the drive before removing it though.

If you are only going to read the drive, you could make it mount read-only, then add this line to the rules file

Code: Select all

KERNEL=="sda1", ACTION=="remove", RUN="/bin/umount /dev/sda1"
As the drive is mounted read-only, it won't matter that it gets unplugged before the umount command is run.

Duncb82
Posts: 7
Joined: Tue May 21, 2013 6:22 am

Re: Mount ANY usb drive in same location

Wed May 22, 2013 5:45 pm

Thanks for the reply, I'll try udevadm

Whilst testing the RPi, every now and again, a usb drive plugged in would show up as /sdb and wouldn't mount (from fstab). Not sure why, and there didn't seem to be any logic to it, but it happened enough to be an issue.

If I can find something in common with the drives I can use your =="add" udev rule that way though

sdjf
Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: Mount ANY usb drive in same location

Wed May 22, 2013 8:02 pm

Yeah, I agree, I did not think you could use /dev/sda1 because once in a while the Pi will assign a different special device reference to a device. If they all say DRIVE or HDD or something in their official product name that shows in your lsusb or everything log, I think that would be the way to go.
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

User avatar
rpdom
Posts: 17171
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Mount ANY usb drive in same location

Thu May 23, 2013 3:19 am

In that case

Code: Select all

KERNEL=="sd[a-z]1", ACTION=="add", RUN="/usr/local/sbin/usbmount"
and create /usr/local/sbin/usbmount

Code: Select all

#!/bin/sh

mount -t vfat $DEVNAME /media/usb
(assuming the usb drives are all VFAT formatted. If not, it should be possible to detect the filesystem type and mount accordingly).

I'd also add some error handling in there - check nothing is already mounted on /media/usb and things like that.

Duncb82
Posts: 7
Joined: Tue May 21, 2013 6:22 am

Re: Mount ANY usb drive in same location

Thu May 23, 2013 5:11 am

Yeah that would work. Obvious now I think about it!

Thanks for the help everyone

User avatar
0xdr01d
Posts: 12
Joined: Fri Aug 17, 2012 5:52 pm
Location: Germany

Re: Mount ANY usb drive in same location

Fri May 31, 2013 5:37 pm

In my opinion, usbmount would be the easiest solution.
It automatically mounts usb-drives as /media/usb[0-7] and creates a symlink by the modelname of your usb-drive. :)

See http://packages.debian.org/en/wheezy/usbmount for more infos.

Return to “Advanced users”