Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 22 Jun 2018, 23:13
All times are UTC - 4
 Forum index » House Training » HOWTO ( Solutions )
Howto make permanent USB mount points with "udev"
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [17 Posts]   Goto page: 1, 2 Next
Author Message
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Thu 18 Jan 2018, 13:12    Post subject:  Howto make permanent USB mount points with "udev"
Subject description: Hotplugging, suspending, power loss, accidental disconnects, and reboots handled gracefully
 

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.
udev-mount.tar.gz
Description 
gz

 Download 
Filename  udev-mount.tar.gz 
Filesize  1.66 KB 
Downloaded  76 Time(s) 

Last edited by jafadmin on Fri 19 Jan 2018, 16:23; edited 6 times in total
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Thu 18 Jan 2018, 14:01    Post subject:  

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 ..
udev-wizard.tar.gz
Description 
gz

 Download 
Filename  udev-wizard.tar.gz 
Filesize  1.86 KB 
Downloaded  82 Time(s) 
Back to top
View user's profile Send private message 
greengeek


Joined: 20 Jul 2010
Posts: 5054
Location: Republic of Novo Zelande

PostPosted: Tue 13 Feb 2018, 04:55    Post subject:  

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!
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Tue 13 Feb 2018, 16:16    Post subject:  

@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, 08:53; edited 2 times in total
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Tue 13 Feb 2018, 16:23    Post subject:  

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:
-> 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, 08:51; edited 1 time in total
Back to top
View user's profile Send private message 
B.K. Johnson

Joined: 12 Oct 2009
Posts: 653

PostPosted: Tue 13 Feb 2018, 20:52    Post subject:  

@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.

_________________
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

Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Tue 13 Feb 2018, 21:45    Post subject:  

@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 ..
Back to top
View user's profile Send private message 
Mike Walsh


Joined: 28 Jun 2014
Posts: 3800
Location: King's Lynn, UK.

PostPosted: Wed 14 Feb 2018, 06:35    Post subject:  

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.)





Cheers, jafa..!


Mike. Wink

_________________
MY PUPPY PACKAGES | 'Thanks' are always appreciated!
--------------------------------------

Back to top
View user's profile Send private message Visit poster's website 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Wed 14 Feb 2018, 15:02    Post subject:  

I've often wondered why Puppies don't have a similar script built into all the distros. It seems so completely organic, to me.
Back to top
View user's profile Send private message 
greengeek


Joined: 20 Jul 2010
Posts: 5054
Location: Republic of Novo Zelande

PostPosted: Tue 20 Feb 2018, 04:29    Post subject:  

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, 15:32; edited 2 times in total
Back to top
View user's profile Send private message 
greengeek


Joined: 20 Jul 2010
Posts: 5054
Location: Republic of Novo Zelande

PostPosted: Tue 20 Feb 2018, 05:14    Post subject: Re: Howto make permanent USB mount points with "udev"
Subject description: Hotplugging, suspending, power loss, accidental disconnects, and reboots handled gracefully
 

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?
Back to top
View user's profile Send private message 
Mike Walsh


Joined: 28 Jun 2014
Posts: 3800
Location: King's Lynn, UK.

PostPosted: Tue 20 Feb 2018, 06:19    Post subject:  

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:
./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

_________________
MY PUPPY PACKAGES | 'Thanks' are always appreciated!
--------------------------------------

Back to top
View user's profile Send private message Visit poster's website 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Tue 20 Feb 2018, 12:47    Post subject:  

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:

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!

.
Back to top
View user's profile Send private message 
TyroBGinner

Joined: 30 Mar 2016
Posts: 218

PostPosted: Tue 20 Feb 2018, 15:08    Post subject:  

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!
Back to top
View user's profile Send private message 
greengeek


Joined: 20 Jul 2010
Posts: 5054
Location: Republic of Novo Zelande

PostPosted: Tue 20 Feb 2018, 15:24    Post subject:  

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:

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!
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [17 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » House Training » HOWTO ( Solutions )
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0783s ][ Queries: 13 (0.0096s) ][ GZIP on ]