better naming of usb nvme devs master
authorIan Kelling <iank@fsf.org>
Mon, 30 Mar 2026 11:46:16 +0000 (07:46 -0400)
committerIan Kelling <iank@fsf.org>
Mon, 30 Mar 2026 11:46:16 +0000 (07:46 -0400)
fai/config/distro-install-common/devbyid
fai/config/hooks/partition.DEFAULT

index 445fd7516cc7c72823d5c738e79921dcf557ebc4..a538f24a1aa530e355dfc9a1587de71991f5c71d 100755 (executable)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 # 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:
 # 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 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
 
 short_dev=$1
 if [[ ! -e $short_dev ]]; then
@@ -30,9 +37,11 @@ if [[ ! -e $short_dev ]]; then
   exit 1
 fi
 
   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.
 # 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
 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
     exit
   fi
 done
+
 # a vm may not have a by-id link.
 printf '%s\n' "$short_dev"
 # a vm may not have a by-id link.
 printf '%s\n' "$short_dev"
index ebe24b89fc0ad63ad726cf92251907ce5e58b509..c66bb43669470a482003a7e59e4f140975f52989 100755 (executable)
@@ -644,7 +644,7 @@ if [[ $special_disk ]]; then
   #tmps=$(devbyid $special_disk)
   #devs=("$tmps")
 elif [[ $HOSTNAME == kd ]]; 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
 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
 
   # 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
   vgs+=("$vg")
   devs+=("$dev")
   if fsf; then
@@ -797,9 +807,10 @@ boot_space=0
 first=true
 boot_devs=()
 boot2_devs=()
 first=true
 boot_devs=()
 boot2_devs=()
+declare -i dev_i=0
 for dev in ${devs[@]}; do
 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
   # 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
 
     fi
   fi
 
+  dev_i=0
   for dev in ${devs[@]}; do
   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.
 
     # parted will round up the disk size. Do -1 so we can have
     # fully 1MiB unit partitions for easy resizing of the last partition.