LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   USB drive mounting (https://www.linuxquestions.org/questions/slackware-14/usb-drive-mounting-771987/)

bgeddy 11-27-2009 02:38 PM

USB drive mounting
 
I know this is a really simple one but it involves new (to me) technologies so I'm a bit stuck !
I recently got fed up with shuffling around my disk space and have invested in an external USB attached hard disk - an Iomega Prestige 1TB unit.
It came formatted to NTFS but I have repartitioned it and created a new ext2 and an ext3 partition. I would like these partitions to be automatically mounted to persistent mount points either on boot up or if switched on whilst my system is running. For the first requirement I added entries to /etc/fstab and running "mount -a" on a running system mounts the partitions to the required places. Starting "from cold" however doesn't mount the devices and I have to mount them again from the cli. A sample boot message from this is :
Code:

mount: special device /dev/sdc2 does not exist
Not surprisingly powering on the device while my system is running doesn't automount them.
I think some kind of udev rule is perhaps needed - a subject which I am particularly dumb about! I know I could setup an rc.local entry to handle boot up mounting but this doesn't seem right and doesn't handle run time power ons for the drive.
Put simply I would like the device to be automagically mounted, (to a persistent mount point), if powered on when booting or when powered on later. What's the recommended way of achieving this ?

Didier Spaier 11-27-2009 04:56 PM

Well, I founded a way to do it. may be not elegant but it works.

1) I add following lines to my /etc/fstab for the two reiserfs partitions on my USB HD:
Code:

/dev/usbhd1    /media/p1        reiserfs    defaults        1  0
/dev/usbhd4        /media/p4        reiserfs    defaults        1  0

2) I create corresponding directories
Code:

mkdir /media/p1 /media/p4
3) I issue lsusb to know my disk's idVendor and idProduct:

Code:

...
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 011: ID 04b4:6830 Cypress Semiconductor Corp. CYd7C68300A EZ-USB AT2 USB 2.0 to ATA/ATAPI
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
...

Thus I know that My Cypress hard disk has idVendor 04b4 and idProduct 6830

4) I create a file named /etc/udev/rules.d/70-persistent-hd.rules with only one line in it:

Code:

ATTRS{idVendor}=="04b4", ATTRS{idProduct}=="6830", SYMLINK+="usbhd%n", RUN+="/bin/mount /media/p1", RUN+="/bin/mount /media/p4/"
Now I unplug then plug in again the USB HD After a while I can see the new symlinks in /dev:
Code:

lrwxrwxrwx 1 root  root            3 2009-11-27 23:26 usbhd -> sdb
lrwxrwxrwx 1 root  root            4 2009-11-27 23:26 usbhd1 -> sdb1
lrwxrwxrwx 1 root  root            4 2009-11-27 23:26 usbhd4 -> sdb4

And even better the two partitions are automagically mounted:
Code:

ys. de fich.        Tail. Occ. Disp. %Occ. Monté sur
/dev/root            114G  53G  61G  47% /
tmpfs                1,5G  12K  1,5G  1% /dev/shm
/dev/sda2              26G  20G  6,1G  77% /windows
/dev/sdb1              46G  2,3G  43G  6% /media/p1
/dev/sdb4            254G  178G  76G  71% /media/p4
bash-3.1$

This probably could be done using HAL, but I don't use HAL at all.

[EDIT]Caveat: for now the partitions are not mounted at boot time; I will try to understand why.

bgeddy 11-27-2009 07:05 PM

Thanks for you quick reply, Didier. I copied your rule into /etc/udev/rules.d/70-persistent-hd.rules and amended the idProduct and idVendor to match my hardware. I then amended my /etc/fstab to mount my two usb partitions (now being one of /dev/usbhd[2,4]) and rebooted. The symlinks /dev/usbhd[,2,4] were now created but only the last one (/dev/usbhd4) was being mounted. To fix this I created a two line /etc/udev/rules.d/70-persistent-hd.rules file with these contents :
Code:

KERNEL=="sdc2", ATTRS{idVendor}=="059b", ATTRS{idProduct}=="0370", SYMLINK+="usbhd2", RUN+="/bin/mount /home/ed/spare13"
KERNEL=="sdc4", ATTRS{idVendor}=="059b", ATTRS{idProduct}=="0370", SYMLINK+="usbhd4", RUN+="/bin/mount /home/ed/spare14"

Here I am obviously specifying the kernel device name exactly rather than using a wild card. This worked and both devices were mounted. I thought about this and changed my /etc/fstab back to simply this:
Code:

/dev/sdc2        /home/ed/spare13 ext2                defaults      0    0
/dev/sdc4        /home/ed/spare14 ext3          defaults      0    0

- as I don't really need the symlink creating and can use the original kernel device name in fstab. This works at boot and also if I power on the drive when the box is running.
So far so good. However I decided as an experiment to try powering off the drive while the machine was running and noticed the /dev/sdc[,2,4] entries and the /dev/hd[,2,4] entries disappeared as I hoped - however the mount still persisted and the filesystem reported an error. I think I need to refine the udev rule file and create entries for 'ACTION=="add"' and 'ACTION=="remove"' to handle mounting and unmounting the devices. There is more to udev than I first thought but at least it's now mounting as I wanted - just not unmounting yet.
I'll keep at this as I'm almost there and learning a lot as I am going on. Next steps are to handle the "remove" and "add" and also loose the symlink creation as I don't use it. Udev is one of those areas that I have tended to avoid as it's always seemed a bit tortuous !
Thanks again for the advice.

Didier Spaier 11-28-2009 12:42 AM

I'll look at that more thoroughly too for auto-unmount.

Meanwhile be careful with KERNEL=="sdc2" or KERNEL=="sdc4". At first I wrote something like that but it won't work if, for instance, you first plug in an USB key then switch on the hard disk: in such a case the kernel will allocate names sdc for the key then sdd for the hard disk. IOW bear in mind that it allocates names to devices in the order they are are added.

bgeddy 11-28-2009 01:39 AM

Quote:

IOW bear in mind that it allocates names to devices in the order they are are added
Yes - I've been thinking about this as a possible reason to use the symlink. Obviously you then have a constant device name for fstab when mounting. I only have the Iomega drive to worry about for now but I'll keep this in mind. Hal/Dbus is another grey area to me and something else I will have to look into - possibly useful in this situation. I'm begining to realize my Linux knowledge is somewhat outdated nowadays ;)!

Didier Spaier 11-28-2009 01:47 AM

Quote:

Hal/Dbus is another grey area to me and something else I will have to look into - possibly useful in this situation.
Yes, noticeably for KDE, GNOME and XFCE users.

I use Fluxbox instead with pmount (package made with a SlackBuild provided by http://slackbuilds.org) to manually mount or umount removable devices as a regular user.

bgeddy 11-28-2009 10:19 AM

Quote:

Meanwhile be careful with KERNEL=="sdc2" or KERNEL=="sdc4". At first I wrote something like that but it won't work if, for instance, you first plug in an USB key then switch on the hard disk: in such a case the kernel will allocate names sdc for the key then sdd for the hard disk. IOW bear in mind that it allocates names to devices in the order they are are added.
I've been thinking about the assigned device ID's for a USB drive particularly that they are transient. I've decided on using the partition's UUID rather than have a device name change trip me up. For now I have put the two, (for the USB drive), into fstab changing device references to "UUID=".
I may change the "RUN+=" string in my 70-persistent-hd.rules rules file so this mounts by UUID itself.
Then again I may leave it with fstab handling this as that way a "mount -a" will include these partitions should I mess around in future.
Hmm - still thinking about this .....
EDIT: I've been thinking a lot about this and realised that my assumptions don't make sense. I.e if the udev rule is selected by the "KERNEL=" line then this won't get executed when the kernel changes device names. No amount of UUID cleverness in fstab will help this udev rule - I'll have to leave out the "KERNEL=" bit and just select by "idVendor" and "idProduct". It's been a late one and I'm not thinking at all straight about this. My aplogies to any readers.:EDIT

Didier Spaier 11-28-2009 04:45 PM

No need for apologies, as far as I be in concern ;)

I did try ACTION=="add" and ACTION="remove" with my USB hard disk, it works. So, as according to man udev "all rule files are sorted and processed in lexical order, regardless in which of these directories they live", we can create following files/rules:
70-plug-in-HD #create symlinks, based on ACTION=="add"
71-mkdir-for-HD #based on ACTION=="add"
72-mount-HD #based on ACTION=="add"
70-umount-HD #based on ACTION=="remove"
71-rmdir-for-HD #based on ACTION=="remove"

That way we'll have mount points for the USB HD only when needed.

BTW HAL is doing something similar if you use it...

If you prefer, you can create a set of rules for each partition.

Or... As the saying goes, "there's more than one way to skin a cat".

IMHO there is no need to delete symlinks in the /dev tree as it won't hurt if a rule tries to create one twice -- and they will be erased at next boot anyhow.

P.S. Can you see a yellow submarine around you ?

bgeddy 11-29-2009 10:28 AM

OK - I think I have cracked this for anyone following. It was a real PITA to get the add/remove functionality but I found a useful article in an Ubuntu forum here. This handles the attributes disappearing when you disconnect a device. This had me bamboozled.

So here is my final solution:

1) Add the UUID of the volumes and where to mount them to fstab - this will handle different devices being created as the UUID is constant - here's my relevant bit:
Code:

UUID=235ea638-6011-42f9-8f78-8383f117e8a1        /home/ed/spare13 ext2                defaults      0    0
UUID=9f902108-cde7-4cae-bc86-b15f88d59501      /home/ed/spare14 ext3          defaults      0    0

You can get the UUID with "/sbin/vol_id /dev/sdc2" as root for partition /dev/sdc2. The UUID as shown as "ID_FS_UUID=". Alternatively try running " /usr/sbin/udevmonitor --udev --environment" as you switch the drive on anf off - lots of useful information there with attributes you may use in a udev rule.

2) Create a couple of udev rules to add and remove the device mounts. I created a file /etc/udev/rules.d/70-persistent-hd.rules and added these lines.
Code:

ENV(ID_FS_UUID)="235ea638-6011-42f9-8f78-8383f117e8a1",ACTION=="add",RUN+="/bin/mount /home/ed/spare13"
ENV(ID_FS_UUID)="29f902108-cde7-4cae-bc86-b15f88d59501",ACTION=="add",RUN+="/bin/mount /home/ed/spare14"
ENV(ID_FS_UUID)="235ea638-6011-42f9-8f78-8383f117e8a1",ACTION=="remove",RUN+="/bin/umount /home/ed/spare13"
ENV(ID_FS_UUID)="29f902108-cde7-4cae-bc86-b15f88d59501",ACTION=="remove",RUN+="/bin/umount /home/ed/spare14"

I now have the drive partitions mounted to persistent mount points at boot or if switched on after boot up. The mounts disappear if the drive is switched off while I am still running. In short I'm well chuffed ! I hope this may help someone else.
Quote:

P.S. Can you see a yellow submarine around you ?
Occasionally - on a Saturday night - but that's a different story :)


All times are GMT -5. The time now is 05:15 PM.