From f31ee06d27e3d61f2fc064710ae9ec7bdd6937ac Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 30 Mar 2026 07:46:16 -0400 Subject: [PATCH] better naming of usb nvme devs --- fai/config/distro-install-common/devbyid | 22 ++++++++++++++----- fai/config/hooks/partition.DEFAULT | 28 +++++++++++++++++------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/fai/config/distro-install-common/devbyid b/fai/config/distro-install-common/devbyid index 445fd75..a538f24 100755 --- a/fai/config/distro-install-common/devbyid +++ b/fai/config/distro-install-common/devbyid @@ -16,13 +16,20 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# input: /dev/DISK[PARTITION]... -# eg: /dev/sdb1 /dev/sda +# input: /dev/DISK[PARTITION] +# eg: /dev/sdb1 # output example: -# /dev/disk/by-id/model+serial-part1 -# /dev/disk/by-id/model+serial +# /dev/disk/by-id/(ata|nvme|usb|???)-model_serial-part1 # # If no symlink exists, output is the same as input. +# + + +if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi +shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4 +set -eE -o pipefail +trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR + short_dev=$1 if [[ ! -e $short_dev ]]; then @@ -30,9 +37,11 @@ if [[ ! -e $short_dev ]]; then exit 1 fi -# devices are identified by model+serial num -# and for ssd/hdd: wwn, and for nvme: eui. +# Devices are identified by model+serial num +# and for ssd/hdd a 2nd name by wwn, and for nvme, a 2nd name by eui. # model+serial gives me more info, so use that. +# + shopt -s extglob for id in /dev/disk/by-id/!(nvme-eui*|wwn*); do [[ -e $id ]] || break # if we matched nothing @@ -41,5 +50,6 @@ for id in /dev/disk/by-id/!(nvme-eui*|wwn*); do exit fi done + # a vm may not have a by-id link. printf '%s\n' "$short_dev" diff --git a/fai/config/hooks/partition.DEFAULT b/fai/config/hooks/partition.DEFAULT index ebe24b8..c66bb43 100755 --- a/fai/config/hooks/partition.DEFAULT +++ b/fai/config/hooks/partition.DEFAULT @@ -644,7 +644,7 @@ if [[ $special_disk ]]; then #tmps=$(devbyid $special_disk) #devs=("$tmps") elif [[ $HOSTNAME == kd ]]; then - short_devs=( $(readlink /dev/disk/by-id/ata-Samsung_SSD_870_EVO_250GB_S6PENL0T709966E) ) + short_devs=( "$(readlink /dev/disk/by-id/ata-Samsung_SSD_870_EVO_250GB_S6PENL0T709966E)" ) else if ! $mkroot2 && ! $mkroot2tab && ! $mktab ! ifclass USE_MOUNTED; then @@ -761,9 +761,19 @@ for short_dev in ${short_devs[@]}; do # for vms, cant name a vg the same as the short device name, they # conflict: /dev/$vg is already taken - dname=${dev##*/} - vg=vg$dname - vg=${vg//:/} + if [[ $dev == /dev/disk/by-id/usb-* ]] && + tmps=$(smartctl -d sntasmedia -a $short_dev 2>/dev/null) && + [[ $tmps ]] && + model_serial=$(printf "%s\n" "$tmps" | + sed -rn 's/^(Model|Serial) Number:[[:space:]]+//;T;s/[[:space:]]/_/g;s/[^a-zA-Z0-9._+-]//g;p' | + sed -rn '1N;s/\n/_/p') && + [[ $model_serial == *??_??* ]]; then + vg="vgnvme-$model_serial" + else + + vg=vg${dev##*/} + vg=${vg//[^a-zA-Z0-9._+-]/} + fi vgs+=("$vg") devs+=("$dev") if fsf; then @@ -797,9 +807,10 @@ boot_space=0 first=true boot_devs=() boot2_devs=() +declare -i dev_i=0 for dev in ${devs[@]}; do - vg=vg${dev##*/} - vg=${vg//:/} + vg=${vgs[$dev_i]} + dev_i+=1 # I ran into a machine (frodo) where the bios doesn't know about some disks, # so 1st stage of grub also doesn't know about them. # Also, grub does not support mounting degraded btrfs as far as @@ -1068,9 +1079,10 @@ if $partition; then fi fi + dev_i=0 for dev in ${devs[@]}; do - vg=vg${dev##*/} - vg=${vg//:/} + vg=${vgs[$dev_i]} + dev_i+=1 # parted will round up the disk size. Do -1 so we can have # fully 1MiB unit partitions for easy resizing of the last partition. -- 2.30.2