11#! /bin/sh
22
3+ echo " --> STARTING persistent-storage script with MDEV='${MDEV} ' ACTION='${ACTION} ' all params: $* " >&2
4+
35symlink_action () {
46 case " $ACTION " in
5- add) ln -sf " $1 " " $2 " ;;
7+ add)
8+ echo " SYMLINK ADD: ln -sf '$1 ' '$2 '" >&2
9+ ln -sf " $1 " " $2 "
10+ ;;
611 remove) rm -f " $2 " ;;
712 esac
813}
5863fi
5964
6065model=$( sanitise_file " $SYSFS /class/block/$_check_dev /device/model" )
61- name=$( sanitise_file " $SYSFS /class/block/$_check_dev /device/name" )
66+ echo " INITIAL model: '${model} '" >&2
67+ name=$( sanitise_file " $SYSFS /class/block/$_check_dev /device/name" ) # only used for mmcblk case
68+ echo " INITIAL name: '${name} '" >&2
6269serial=$( sanitise_file " $SYSFS /class/block/$_check_dev /device/serial" )
70+ echo " INITIAL serial: '${serial} '" >&2
6371# Special case where block devices have serials attached to the block itself, like virtio-blk
6472: ${serial:= $(sanitise_file " $SYSFS /class/block/$_check_dev /serial" )}
73+ echo " DEVICE serial (after block-serial): '${serial} '" >&2
6574wwid=$( sanitise_file " $SYSFS /class/block/$_check_dev /wwid" )
75+ echo " INITIAL wwid: '${wwid} '" >&2
6676: ${wwid:= $(sanitise_file " $SYSFS /class/block/$_check_dev /device/wwid" )}
77+ echo " DEVICE wwid (from device-wwid): '${wwid} '" >&2
6778
6879# Sets variables LABEL, PARTLABEL, PARTUUID, TYPE, UUID depending on
6980# blkid output (busybox blkid will not provide PARTLABEL or PARTUUID)
@@ -72,17 +83,79 @@ eval $(blkid /dev/$MDEV | cut -d: -f2-)
7283if [ -n " $wwid " ]; then
7384 case " $MDEV " in
7485 nvme* ) symlink_action ../../$MDEV disk/by-id/nvme-${wwid}${partsuffix} ;;
86+ sd* ) symlink_action ../../$MDEV disk/by-id/scsi-${wwid}${partsuffix} ;;
87+ sr* ) symlink_action ../../$MDEV disk/by-id/scsi-ro-${wwid}${partsuffix} ;;
88+ vd* ) symlink_action ../../$MDEV disk/by-id/virtio-${wwid}${partsuffix} ;;
7589 esac
7690 case " $wwid " in
7791 naa.* ) symlink_action ../../$MDEV disk/by-id/wwn-0x${wwid# naa.}${partsuffix} ;;
7892 esac
7993fi
8094
95+ # if no model or no serial is available, lets parse the wwid and try to use it
96+ if [ -n " ${serial} " ] && [ -n " ${model} " ]; then
97+ echo " USING SYSFS model='${model} ' serial='${serial} '" >&2
98+ else
99+ echo " SYSFS model='${model} ' serial='${serial} ' insufficient, trying to parse from wwid" >&2
100+ unset wwid_raw
101+ if [ -f " $SYSFS /class/block/$_check_dev /wwid" ]; then
102+ echo " FOUND WWID FILE: '$SYSFS /class/block/$_check_dev /wwid'" >&2
103+ wwid_raw=" $( cat " $SYSFS /class/block/$_check_dev /wwid" ) "
104+ elif [ -f " $SYSFS /class/block/$_check_dev /device/wwid" ]; then
105+ echo " FOUND WWID FILE: '$SYSFS /class/block/$_check_dev /device/wwid'" >&2
106+ wwid_raw=" $( cat " $SYSFS /class/block/$_check_dev /device/wwid" ) "
107+ fi
108+ echo " SYSFS parse model/serial from wwid_raw:'${wwid_raw} '" >&2
109+ if [ -n " ${wwid_raw} " ]; then
110+ wwid_raw=$( echo " ${wwid_raw} " | sed ' s/^ *//;s/ *$//' ) # Remove leading and trailing spaces
111+ wwid_prefix=$( echo " ${wwid_raw} " | awk ' {print $1}' ) # Extract the wwid_prefix (first field)
112+ rest=$( echo " ${wwid_raw} " | sed " s/^${wwid_prefix} *//" ) # Remove the wwid_prefix from the wwid string
113+ wwid_serial=$( echo " ${rest} " | awk ' {print $NF}' ) # Extract the serial (last field)
114+ rest=$( echo " ${rest} " | sed " s/ ${wwid_serial} $//" ) # Remove the serial from the rest of the string
115+ wwid_model=$( echo " ${rest} " | tr ' ' ' _' ) # Replace any remaining spaces in the rest part with underscores
116+ wwid_model=$( echo " ${wwid_model} " | sed ' s/__*/_/g' ) # Remove consecutive underscores
117+ wwid_model=$( echo " ${wwid_model} " | sed ' s/^_//;s/_$//' ) # Remove leading and trailing underscores
118+ wwid_prefix=$( echo " ${wwid_prefix} " | sed ' s/\./-/g' ) # Replace periods in the wwid_prefix with dashes
119+ unset rest
120+ echo " WWID parsing came up with wwid_prefix='${wwid_prefix} ' wwid_model='${wwid_model} ', wwid_serial='${wwid_serial} '" >&2
121+ else
122+ echo " WWID is empty or not found" >&2
123+ fi
124+
125+ # if model is unset, replace it with the parsed wwid_model
126+ if [ -z " ${model} " ]; then
127+ echo " USING WWID model='${wwid_model} ' as model..." >&2
128+ model=" ${wwid_model} "
129+ fi
130+
131+ # if serial is unset, replace it with the parsed wwid_serial
132+ if [ -z " ${serial} " ]; then
133+ echo " USING WWID wwid_serial='${wwid_serial} ' as serial..." >&2
134+ serial=" ${wwid_serial} "
135+ fi
136+
137+ # if we still have no serial, just use the wwid as serial as fallback;
138+ if [ -z " ${serial} " ]; then
139+ echo " FALLBACK: USING WWID as serial='${wwid} '" >&2
140+ serial=" ${wwid} "
141+ fi
142+
143+ # rescue: if _still_ no serial set, set to hardcoded string 'noserial'.
144+ if [ -z " ${serial} " ]; then
145+ echo " FALLBACK: USING 'noserial' as serial..." >&2
146+ serial=" noserial"
147+ fi
148+ fi
149+
81150if [ -n " $serial " ]; then
151+ echo " GOT SERIAL: serial='${serial} ' model='${model} '" >&2
82152 if [ -n " $model " ]; then
153+ echo " GOT MODEL: serial='${serial} ' model='${model} '" >&2
83154 case " $MDEV " in
84155 nvme* ) symlink_action ../../$MDEV disk/by-id/nvme-${model} _${serial}${partsuffix} ;;
156+ sr* ) symlink_action ../../$MDEV disk/by-id/ata-ro-${model} _${serial}${partsuffix} ;;
85157 sd* ) symlink_action ../../$MDEV disk/by-id/ata-${model} _${serial}${partsuffix} ;;
158+ vd* ) symlink_action ../../$MDEV disk/by-id/virtio-${model} _${serial}${partsuffix} ;;
86159 esac
87160 fi
88161 if [ -n " $name " ]; then
@@ -138,9 +211,12 @@ if [ "${MDEV#sd}" != "$MDEV" ]; then
138211 case " $sysdev " in
139212 * usb[0-9]* )
140213 # require vfat for devices without partition
141- if ! [ -e $SYSFS /block/$MDEV ] || [ TYPE= " vfat" ]; then
214+ if ! [ -e $SYSFS /block/$MDEV ] || [ TYPE= " vfat" ]; then # @TODO: rpardini: upstream bug here? should be $TYPE
142215 symlink_action $MDEV usbdisk
143216 fi
144217 ;;
145218 esac
146219fi
220+
221+ echo " --> FINISHED persistent-storage script with MDEV='${MDEV} ' ACTION='${ACTION} ' all params: $* " >&2
222+ echo " " >&2
0 commit comments