Permanent device/mount name for a USB disk

Technical issues/questions of an intermediate or advanced nature.
Post Reply
rych
Samurai
Samurai
Posts: 176
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 4.0 64 LXDE
Location: NZ

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
Samurai
Samurai
Posts: 142
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
Samurai
Samurai
Posts: 176
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 4.0 64 LXDE
Location: NZ

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
Samurai
Samurai
Posts: 176
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 4.0 64 LXDE
Location: NZ

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
Samurai
Samurai
Posts: 142
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
Samurai
Samurai
Posts: 176
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 4.0 64 LXDE
Location: NZ

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
Samurai
Samurai
Posts: 142
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

Post Reply