Howto make permanent USB mount points with "udev"

How to do things, solutions, recipes, tutorials
Message
Author
jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

Howto make permanent USB mount points with "udev"

#1 Post by jafadmin »

Depending on what USB storage devices you have plugged in and in what order, the kernel is going to automatically assign a /dev/sd? descriptor to it. It may very well not always wind up with the same descriptor.

If we want to run cron jobs or scripts to backup or sync data, we need to have reliable, permanent device descriptors so our scripts don't break. What to do?

Udev, that's what. This is the tool sysadmins in datacenters use on the "bigiron". It guarantees that no matter what, your USB drive will always have a unique, permanent device descriptor. This will persist through hibernation or sleep modes. It will survive accidental unplugging or power loss.

And you can use it with puppy. It was tested on Precise 5.7.1 ram mode. Everything you need is in the "udev-mount.tar.gz" archive.

It assumes you have a /root/bin directory. if you don't, create one. Put the "mkrule" and "mnt-MyUSB" files in that directory. Put the "trigger-udev" file in /root/Startup. Make sure they are all executable.

Now just plug in the USB disk you want to use for this, then open a console in /root/bin and type: "./mkrule /dev/sdX" (where X is your usb device). This will create the udev rule and put it where it needs to go.

At this point all partitions on your USB disk should be mounted at: /MyUSB

From now on, that, and only that USB disk, will always mount as /MyUSB/MyUSB-1, /MyUSB/MyUSB-2, etc ..

Drag a link to /MyUSB to your desktop.

Prosper.

That is all.
Last edited by jafadmin on Fri 19 Jan 2018, 20:23, edited 6 times in total.

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#2 Post by jafadmin »

What it does:

When a USB device is plugged into linux, the kernel looks to see if there is something on the system that has instructions on what to do with it. If there are no instructions, it just performs some default action.

So how do I instruct the kernel what to do with MY device? We make a rule that is associated with that device using udev/udevadm and put it in /etc/udev/rules.d/. Now every time my USB disk is hot plugged, the kernel will use that rule to see what I want done with it.

Our rule, "70-MyUSB.rules", tells the kernel to create custom device descriptors for all the partitions on my USB disk when plugged and put them in /dev. Next it tells the kernel to execute our script, "mnt-MyUSB", which handles the directories, mount points, and mounting everything.

The "trigger-udev" script in /root/Startup makes sure the device gets mounted properly if it was already plugged in at boot time, since it won't trigger a unique PnP event to process unless unplugged and re-plugged.

After a hibernate or sleep session, if the device triggers a PnP event, it just resets to normal.

Exceptions:

When using USB cradles, just remember that udev only sees the cradle metrics, not the disk metrics. This means the same rule will always be triggered regardless of which disk is in the cradle. This is actually a good thing if you are using the cradle to do daily backups to different disks.


[ A Cool Update .. ]
Below is an improvement in the form of a wizard. It is for folks that are juggling several USB disks and want each to mount by a "friendly" name. This one also processes the "unplug" event and performs cleanup in the newer puppies. (Older versions of udev don't generate "remove" 'ENV' events.)

In this implementation you put the wizard files in "/root/udev" which you will need to create.

Now just open a console in /root/udev and run: "./mkrule /dev/sd? FriendlyName"
where "sd?" is the device descriptor and "FriendlyName" is frequently the model name, i.e.: "WD-Mybook"

That's all you do. Now you can run it for each USB disk and you will have mountpoints for them in "/", i.e.: /WD-Mybook, etc ..
Attachments
udev-wizard.tar.gz
(1.88 KiB) Downloaded 449 times
Last edited by jafadmin on Sat 11 Jan 2020, 05:06, edited 1 time in total.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#3 Post by greengeek »

This sounds awesome. Do you have any idea if it should work with Slacko 5.6 and/or 5.7? (I would rather not fiddle yet if you think the utilities are only Precise compatible)

cheers!

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#4 Post by jafadmin »

@greengeek

They should actually run on any Puppy that has udevadm installed, which I think is all since at least 3.12. Use the wizard version.

udev/udevadm are a standard part of linux ..

.
Last edited by jafadmin on Wed 14 Feb 2018, 12:53, edited 2 times in total.

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#5 Post by jafadmin »

PRO TIP:

If you have several USB drives of the same make/model that you use for backups, for instance, and you want them to all mount to the same mountpoint when connected (suppose you have a scheduled backup script, but want to change the USB disk every day), just open the /etc/udev/rules.d/ "71.ModelName.rules" file and delete the field for the serial number along with the comma, of course.

This one:

Code: Select all

-> ATTRS{serial}=="4Z530722230610118279",<- 
Then from a console type: "udevadm control --reload". From then on, it will mount any matching make/model, regardless of serial number, to your fixed mountpoint.
Last edited by jafadmin on Wed 14 Feb 2018, 12:51, edited 1 time in total.

B.K. Johnson
Posts: 807
Joined: Mon 12 Oct 2009, 17:11

#6 Post by B.K. Johnson »

@jafadmin
Thanks for this. I have only been able to take a cursory look but I think I'll be spending some time with it. I have been seeking a way to use udev and uuid with flash drives when designations are changed on insertion and removal.
Take a look at my post from 2012. Not a single response in over 5 years. I think I may be able to make some progress now.
[color=blue]B.K. Johnson
tahrpup-6.0.5 PAE (upgraded from 6.0 =>6.0.2=>6.0.3=>6.0.5 via quickpet/PPM=Not installed); slacko-5.7 occasionally. Frugal install, pupsave file, multi OS flashdrive, FAT32 , SYSLINUX boot, CPU-Dual E2140, 4GB RAM[/color]

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#7 Post by jafadmin »

@B.K. Johnson

Peruse the "mkrule" script and examine the "udevadm info" command. This is how we harvest the fields we want the udev rule to consider.

IMHO, you won't need the UUID filter to make your project work ..

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#8 Post by Mike Walsh »

Morning, all.

I can confirm that this works very well indeed.....at least for me.

As a full-time carer, although I get a lot of free time, I also have to go off at a moment's notice.....sometimes for a while. I don't want to leave the computer 'ticking-over' all that time, nor do I want repeated start/stop cycles; the big Compaq desktop is quite an elderly 'dowager duchess' of a machine, and I don't want to impose any more strain on the circuits than I have to.

I've been implementing the desktop 'suspend script' frenchiveruti posted about here, across the kennels. For some reason, whenever I do this in a 'buntu-based Pup, the external 3 TB Seagate USB 3.0 hard drive that I use for pretty much all my personal data insists on 're-mounting' (at 'wake-up') as the next set of mount-points up.

Example; currently in Upup Raring, I suspended last night with the mount-points for this at sdj1/sdj2. This morning, upon waking-up, they're now at sdk1/sdk2. (Normally, these mount as sdg 1/2; I know it seems rather high, but I have an internal multi-card reader, which occupies several of the lower mount-points.I have no idea why, but the Slackos seem to be immune to this; 560 & 570 're-mount' at wakeup with the same mount-points as before... (??))

---------------------------------------------
Be that as it may, this plays havoc when you have a lot of stuff sym-linked from a 'permanent' external (the way I do) into each Pup.....quite a bit of it needing to be executed at startup. Udev-rules neatly sidesteps all this, providing a set of permanent, stable mount-points in "/". Jafadmin's 'wizard' is especially handy, making the whole process much easier.

Now, I run all my sym-links from the directory in "/".....and everything works as it should. (I simply 'ignore' Pup's desktop drive icons for the Seagate; the 'step-up' of increasing mount-points is of no consequence any more.)


Image


Cheers, jafa..!


Mike. :wink:

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#9 Post by jafadmin »

I've often wondered why Puppies don't have a similar script built into all the distros. It seems so completely organic, to me.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#10 Post by greengeek »

Mike Walsh wrote:...the 'step-up' of increasing mount-points is of no consequence any more.)
Any idea why the 'step up' happens?
Last edited by greengeek on Tue 20 Feb 2018, 19:32, edited 2 times in total.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

Re: Howto make permanent USB mount points with "udev"

#11 Post by greengeek »

jafadmin wrote:At this point all partitions on your USB disk should be mounted at: /MyUSB
It appears that the script also creates a MyUSB mount point for the fourth partition on my usb drive, which is just a swap partition, not a data partition. Should I be concerned about that? Do you think it might have any negative implications?

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#12 Post by Mike Walsh »

Hi, GG.

I can only assume that the mount-point 'step-up' is occurring because Pup sees that mount-point as already in use, so proceeds to assign the next available one. I've tried numerous ways to stop this from happening (Marv's helped out with a few pointers), but without truly understanding how BK's original scripts work the way they do, well.....I'm groping in the dark.

I only suspend perhaps a couple or three times between re-boot cycles anyway, so it's not as though the 'mount-points' would build up to a point where it's getting silly.

--------------------------------------------------------

I, too, find that it gives my 'swap' partition a mount-point.....although it doesn't actually mount it. (This is on the external; I have a swap partition on the main, internal HDD; the small, secondary SSD; and one on the external, as well...)

Try the 'wizard' version as above; with this one, instead of having My-USB1, My-USB2, etc, you can name it what you want. In my case:-

Code: Select all

./mkrule /dev/sdg SeaDesk
My external is a Seagate Desktop Expansion.....so I've named it 'SeaDesk'. (Giving you the result as in the above screenshot.) And then I've given the directory in "/" an icon.....and dragged it to the desktop, where it now lives permanently. And my sym-links all come from that, now.

I've got this working in all Pups in the kennels (apart from Tahr64, but I'll sort it next time I boot into it).....including 560. Works bee-yootifully..!


Mike. :wink:

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#13 Post by jafadmin »

greengeek wrote:It appears that the script also creates a MyUSB mount point for the fourth partition on my usb drive, which is just a swap partition, not a data partition. Should I be concerned about that? Do you think it might have any negative implications?
So true, my puppy peeps. So true ..

So I patched the udev-wizard "mnt-template" for this bug. You can dload again, or just add the following "if" statement to your installed versions:

Code: Select all

for j in $MyDevs
do
    NewDir=$(echo $j |  cut -d '/' -f3)

    # Skip swap partitions:
    if [ ! -z "$(file -sL /dev/$NewDir | grep 'swap')" ]; then continue; fi

    mkdir -p /"$DevName"/$NewDir		# force creation of directory tree.
    mount $j /"$DevName"/$NewDir
done
Many thanks to GG & MW for the testing and feedback!

.

TyroBGinner
Posts: 236
Joined: Wed 30 Mar 2016, 20:18

#14 Post by TyroBGinner »

I very much like this idea. I like it so much, some time ago I tried to make a utility that was similar in spirit. It would have worked like what is seen in Ubuntu...the usb storage device would be mounted with a mount point corresponding to the UUID of the filesystem.

Is there any way to modify this utility so that drive uniqueness can be preserved with fanout devices like usb hubs and multi-drive HDD "docks"? I gather that is a lot to ask since the udev rules seem to correspond to the device immediately in contact with the computer's usb connection.
Win10 and lovin' it!

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#15 Post by greengeek »

jafadmin wrote:So I patched the udev-wizard "mnt-template" for this bug. You can dload again, or just add the following "if" statement to your installed versions:

Code: Select all

for j in $MyDevs
do
    NewDir=$(echo $j |  cut -d '/' -f3)

    # Skip swap partitions:
    if [ ! -z "$(file -sL /dev/$NewDir | grep 'swap')" ]; then continue; fi

    mkdir -p /"$DevName"/$NewDir		# force creation of directory tree.
    mount $j /"$DevName"/$NewDir
done
I have not tried the wizard yet - just manually using the original scripts - so i have a few questions:

1) Is the new anti-swap code applicable to the "non-wizard" scripts? If so which one should I add it to?
2) If I am not using the wizard, and want to change the "MyUSB" name within the manual scripts - how many times do I need to change it? I tried changing it in the mkrule script and in the mnt-MyUSB scripts but it failed to mount. I presume there must be somewhere else i need to change it also?
3) I wondered if there might be some name length restriction for the device. For example, I changed "MyUSB" to "ToshibaExternalDrive" - could that be the reason for my mount failure?
4) Should the drive be mounted or unmounted before the mkrule script is run? Does it matter?
5) Do you think you are likely to keep offering the manual scripts or should I graduate to the wizard version?

cheers!

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#16 Post by jafadmin »

@GG

Switch to the wizard. The first one is just a demo that shows proof of concept.

Read the README ..

I am not aware of name length restrictions on devices. You might just be afflicted by voodoo at your outpost ..

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#17 Post by jafadmin »

TyroBGinner wrote:there any way to modify this utility so that drive uniqueness can be preserved with fanout devices like usb hubs and multi-drive HDD "docks"? I gather that is a lot to ask since the udev rules seem to correspond to the device immediately in contact with the computer's usb connection.
It works with USB hubs as long as you plug them in one at a time to create the initial ".rules" file. After that you can plug a bunch into the hub and they seem to mount ok. Anyways, they do for me. I can get two identical Sandisk Cruisers to work right in a hub.

I may spend some time later working with the udevadm info dumps to try to tweak that.

As far as the HD cradles are concerned, one would have to do a bunch more research on how to grab those. Maybe grab events from dmesg or something ..

User avatar
gychang
Posts: 414
Joined: Sat 29 Nov 2008, 20:30
Location: San Diego, CA

Re: Howto make permanent USB mount points with "udev"

#18 Post by gychang »

jafadmin wrote:
Now just plug in the USB disk you want to use for this, then open a console in /root/bin and type: "./mkrule /dev/sdX" (where X is your usb device). This will create the udev rule and put it where it needs to go.

At this point all partitions on your USB disk should be mounted at: /MyUSB

From now on, that, and only that USB disk, will always mount as /MyUSB/MyUSB-1, /MyUSB/MyUSB-2, etc ..

Drag a link to /MyUSB to your desktop.

Prosper.

That is all.
On my Bionipup64, after typing in the command ./mkrule /dev/sdg there is no error but no /MyUSB directory is created.

What am I doing wrong?
---
trying to learn puppylinux... :D
---

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#19 Post by Mike Walsh »

gychang wrote:On my Bionipup64, after typing in the command ./mkrule /dev/sdg there is no error but no /MyUSB directory is created.

What am I doing wrong?
Greg:-

You're not looking for a /MyUSB 'directory', as such. What you're looking for is a 'mount-point'.....and you won't find it in /root.

IF 'MyUSB' is the name you've used for your drive (it's really just an example - you could use any name you like), then you should see it by doing the following.

In ROX's /root 'window', click on the upward-pointing arrow at the far left end of the menu bar - this'll take you up a level into the main file-system. You should see MyUSB alongside /root, /var, /lib, /bin, /usr, etc.

The instructions for this are a wee bit misleading, unfortunately. There's no /root/bin directory. What you're looking for is the /root/udev directory. Like this:-


Image


Click on that to open it:-


Image


Mine has 3 files; the middle one is my personal 'mount-point'. It's a Seagate Expansion Desktop drive, so I've named it SeaDesk. You should only see the two outside ones, because the 3rd one won't exist until you've run the command in the terminal.

After you've done that, you should then see the new 'mount-point' alongside the other system files, under '/'. Like this:-


Image


I've dressed it up with a drive icon, to make it more obvious what it is. From this point on, this drive will always mount to the new mount-point that you've just created.


Mike. :wink:

User avatar
gychang
Posts: 414
Joined: Sat 29 Nov 2008, 20:30
Location: San Diego, CA

#20 Post by gychang »

Mike Walsh wrote:
gychang wrote:On my Bionipup64, after typing in the command ./mkrule /dev/sdg there is no error but no /MyUSB directory is created.

What am I doing wrong?
Greg:-

You're not looking for a /MyUSB 'directory', as such. What you're looking for is a 'mount-point'.....and you won't find it in /root.

IF 'MyUSB' is the name you've used for your drive (it's really just an example - you could use any name you like), then you should see it by doing the following.

Mike. :wink:
MIke I really appreciate your effort, now working fine.
---
trying to learn puppylinux... :D
---

Post Reply