Permanent device/mount name for a USB disk

Technical issues/questions of an intermediate or advanced nature.
rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Permanent device/mount name for a USB disk

Post#1 by rych » 17 Feb 2021, 10:23

When I feel like doing some actual work I plug a USB disk labelled "WorkUSB". Could it always be known as /dev/workusb? So that it'd be automatically mounted as /mnt/workusb (or /mnt/workusb1 if more than 1 partition I guess)? At the moment I get /mnt/sdb or /mnt/sdc depending which letter is still available after automatically mounting whatever host disks etc., which isn't permanent.

I think I need to "write a udev rule", but I've never done it before and don't even know where to look and what where to edit on my Porteus 5rc2. Please help

burdi01
Shogun
Shogun
Posts: 201
Joined: 18 Aug 2013, 12:09
Distribution: Slackware PartedMagic Xubuntu
Location: The Netherlands

Permanent device/mount name for a USB disk

Post#2 by burdi01 » 18 Feb 2021, 10:37

/etc/udev/rules.d/73-burdi01.rules:

Code: Select all

# External USB disk is sometimes /dev/sdb, sometimes /dev/sdf or whatever
KERNEL=="sd*", SUBSYSTEMS=="scsi", ATTRS{vendor}=="WD", ATTRS{model}=="Elements 25A3", SYMLINK+="sdxb%n"
KERNEL=="sd*", SUBSYSTEMS=="scsi", ATTRS{vendor}=="TOSHIBA", ATTRS{model}=="External USB 3.0", SYMLINK+="sdxd%n"
will create symlinks in /dev/ pointing to whatever the "real" sdXn happens to be -- e.g. /dev/sdxb1 -> sdc1 and /dev/sdxd5 -> /dev/sdf5. These symlinks then can be used in your fstab.
The vendor, model and, if necessary, e.g. the serial# can be derived from lsusb.
:D

rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Permanent device/mount name for a USB disk

Post#3 by rych » 19 Feb 2021, 06:03

burdi01, Thanks, but it is already known to the system by a symlink as

Code: Select all

/dev/disk/by-label/WorkUSB
I don't think I need yet another symlink for it, do I?

Anyway, I guess I can then write a fstab for mounting those symlinks (--another thing I'm not too familiar with) ...

But, could I have the true device node name as /dev/workusb ?! Am I allowed to do that? So my udev rule should use the assignment key below and be placed higher in the rules priority, maybe as 10-myudev.rules:

Code: Select all

    NAME=workusb
Would fstab magic swallow that frivolity with the names and auto-mount it to /mnt/workusb for me?

rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Permanent device/mount name for a USB disk

Post#4 by rych » 19 Feb 2021, 07:04

OK, actually I don't care about the original /dev/* name. As I anyway get the symlink:

Code: Select all

/dev/disk/by-label/WorkUSB
I just want it to be always and uniquely mounted to

Code: Select all

/mnt/WorkUSB
My /etc/fstab says this:

Code: Select all

# Do not edit this file as fstab is recreated automatically during every boot.
# Please use /etc/rc.d/rc.local or sysvinit scripts if you want to mount/unmount
# ...
/dev/sdb /mnt/sdb ext4 comment=x-gvfs-show,comment=x-gvfs-icon=drive-removable-media,user,noatime,nodiratime,suid,dev,exec,async 0 0
What "automounter" put it there like that? I love everything about that line except that if my disk (partition) had a <LABEL>, then I want to be auto-mounted to /mnt/<LABEL> instead of /mnt/sdb (or /mnt/sdb1). Where do I modify what?

burdi01
Shogun
Shogun
Posts: 201
Joined: 18 Aug 2013, 12:09
Distribution: Slackware PartedMagic Xubuntu
Location: The Netherlands

Permanent device/mount name for a USB disk

Post#5 by burdi01 » 19 Feb 2021, 10:48

Hmm, it never occurred to me to use /dev/disk/by-label/foo as such -- thks for this tip.
As per man fstab(5) a LABEL=<label> or UUID=<uuid> may be given instead of a device name.
IIRC (I do not have the time right now to research) the fstab is recreated somewhere in the init procedure.
:D

rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Permanent device/mount name for a USB disk

Post#6 by rych » 21 Feb 2021, 07:42

burdi01, I've found this (in porteus 4 at least):

Code: Select all

/sbin/udev-fstab-update
which is run by /etc/udev/rules.d/10-porteus-fstab-update.rules,
and which indeed has the relevant line:

Code: Select all

# Variables initialized by the udev environment:
# $ACTION (add, change, remove)
# $DEVNAME (device name including path)
# $ID_FS_TYPE (filesystem type)
...
# Our variables:
FSTAB=/etc/fstab
DEVICE=`echo $DEVNAME | sed s_^/dev/__`
MNTPT=/mnt/$DEVICE
...
I'm thinking of an edit as follows please check my conditional assignment and overall approach:

Code: Select all

# Variables initialized by the udev environment:
# $ACTION (add, change, remove)
# $DEVNAME (device name including path)
# $ID_FS_TYPE (filesystem type)
# $ID_FS_LABEL (filesystem label presumably)
...
# Our variables:
FSTAB=/etc/fstab
DEVICE=`echo $DEVNAME | sed s_^/dev/__`
MNTPT=/mnt/${ID_FS_LABEL:-$DEVICE}
...

burdi01
Shogun
Shogun
Posts: 201
Joined: 18 Aug 2013, 12:09
Distribution: Slackware PartedMagic Xubuntu
Location: The Netherlands

Permanent device/mount name for a USB disk

Post#7 by burdi01 » 21 Feb 2021, 10:52

Your conditional assignment is syntactically ok. Whether or not udev initializes an ID_FS_LABEL variable would require some research/fiddling to be answered.
However, I do not like your approach: replacing code instead of just adding, doing things for all labelled filesystems etc. etc.
What I would do is letting Porteus mount the USB as is and at the end of /sbin/udev-fstab-update add some code to create the workusb mountpoint as a symlink to the real mountpoint (derived from /dev/disk/by-label).
:D

rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Permanent device/mount name for a USB disk

Post#8 by rych » 08 Mar 2021, 08:25

OK, I'm going back then to my old script that I run manually after I plug in my WorkUSB and am ready to work. It makes a symbolic link

Code: Select all

'/W' -> '/mnt/sda1'
and is reentrant, i. e. save to re-execute. I hope you approve it, burdi01, and thanks for advising me so far.

Code: Select all

#!/bin/bash

# some disks in fstab might need a second chance to mount, e.g. NVMe
mount -a

#
# Work disk: either on a separate (NTFS) USB or the same disk as Porteus itself
#
# use either of these:
#Wdisk=$(findmnt $(readlink -mn /dev/disk/by-label/WorkUSB) -no TARGET)
#Wdisk=$(lsblk $(readlink -mn /dev/disk/by-label/*Work*) -no MOUNTPOINT)
Wdisk=$(lsblk $(readlink -mn /dev/disk/by-label/WorkUSB) -no MOUNTPOINT)

if [[ !  -z  $Wdisk  ]]
then
    echo "WorkUSBDisk: $Wdisk"
else
    echo "No WorkUSB disk: re-using porteus USB"
    #exit 1
    #Wdisk=$(lsblk $(readlink -mn /dev/disk/by-label/Porteus) -no MOUNTPOINT)
    Wdisk=$PORTPORT
fi
ln -sfnv $Wdisk /W
Last edited by rych on 10 Mar 2021, 06:57, edited 1 time in total.

burdi01
Shogun
Shogun
Posts: 201
Joined: 18 Aug 2013, 12:09
Distribution: Slackware PartedMagic Xubuntu
Location: The Netherlands

Permanent device/mount name for a USB disk

Post#9 by burdi01 » 08 Mar 2021, 10:43

Dear Rych: I did not disapprove your approach (who am I to do that?), I just said I did not like it.
:D

rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Permanent device/mount name for a USB disk

Post#10 by rych » 08 Feb 2022, 09:41

burdi01 wrote:
21 Feb 2021, 10:52
Whether or not udev initializes an ID_FS_LABEL variable would require some research/fiddling to be answered.
It does. But just using the

Code: Select all

MNTPT=/mnt/${ID_FS_LABEL:-$DEVICE}
makes little difference: it only updates the fstab file, under the #Hotplugged devices, with the names I want but the actual mount points remain unchanged: /mnt/sda2 etc. as if something has already mounted my partitions before this script?

If I have another USB disk plugged, labelled "Work", during boot it'll be mounted under /mnt/sdc1 etc., but if I unplug it (ignoring the error due to discrepancy with fstab I guess) and plug it again, it'll be mounted under /mnt/Work. This is what I want But I want it during the boot, and more importantly, I want the same for the Porteus USB itself: for it to be mounted under /mnt/Porteus5 at boot.

Perhaps, I should try adding to fstab in /etc/rc.d/rc.local, which probably runs earlier than udev and during boot as well? Proper Way to Mount a Drive Permanently (Post by Blaze #60404)

rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Permanent device/mount name for a USB disk

Post#11 by rych » 15 Feb 2022, 06:24

I've ended up using mount --move: no matter what random sdX1 it's mounted to originally, before actually using it, we just give it an invariant name. For example, if the USB already plugged in at boot, we can add the following into /etc/rc.d/rc.local

Code: Select all

# If WorkUSB is already mounted it'd be under a random /mnt/sdX1, give it an invariant mountpoint /W then
WorkUSB=$(findmnt $(readlink -mn /dev/disk/by-label/WorkUSB) -no TARGET)
[[ -n "$WorkUSB" ]] && mount --move $WorkUSB /W
If it's hotplugged later I have to execute a similar command manually -- partly because I don't quite know how to do it automatically the right way, so the question is not perfectly [Solved]. But the method above is okay. Thanks

Post Reply