Permanent device/mount name for a USB disk
-
- 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
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
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
-
- Shogun
- Posts: 206
- Joined: 18 Aug 2013, 12:09
- Distribution: Slackware PartedMagic Xubuntu
- Location: The Netherlands
Permanent device/mount name for a USB disk
/etc/udev/rules.d/73-burdi01.rules:
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.
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"
The vendor, model and, if necessary, e.g. the serial# can be derived from lsusb.
-
- 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
burdi01, Thanks, but it is already known to the system by a symlink as
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:
Would fstab magic swallow that frivolity with the names and auto-mount it to /mnt/workusb for me?
Code: Select all
/dev/disk/by-label/WorkUSB
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
-
- 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
OK, actually I don't care about the original /dev/* name. As I anyway get the symlink:
I just want it to be always and uniquely mounted to
My /etc/fstab says this:
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?
Code: Select all
/dev/disk/by-label/WorkUSB
Code: Select all
/mnt/WorkUSB
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
-
- Shogun
- Posts: 206
- Joined: 18 Aug 2013, 12:09
- Distribution: Slackware PartedMagic Xubuntu
- Location: The Netherlands
Permanent device/mount name for a USB disk
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.
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.
-
- 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
burdi01, I've found this (in porteus 4 at least):
which is run by /etc/udev/rules.d/10-porteus-fstab-update.rules,
and which indeed has the relevant line:
I'm thinking of an edit as follows please check my conditional assignment and overall approach:
Code: Select all
/sbin/udev-fstab-update
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
...
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}
...
-
- Shogun
- Posts: 206
- Joined: 18 Aug 2013, 12:09
- Distribution: Slackware PartedMagic Xubuntu
- Location: The Netherlands
Permanent device/mount name for a USB disk
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).
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).
-
- 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
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 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
'/W' -> '/mnt/sda1'
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.
-
- Shogun
- Posts: 206
- Joined: 18 Aug 2013, 12:09
- Distribution: Slackware PartedMagic Xubuntu
- Location: The Netherlands
Permanent device/mount name for a USB disk
Dear Rych: I did not disapprove your approach (who am I to do that?), I just said I did not like it.
-
- 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
It does. But just using the
Code: Select all
MNTPT=/mnt/${ID_FS_LABEL:-$DEVICE}
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)
-
- 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
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
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
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