Skip to content

Commit c361f85

Browse files
rpardinijacobweinstock
authored andcommitted
hook-mdev: add customized persistent-storage script which tries harder to produce /dev/disk/by-id entries by parsing wwid
- this is still not-even-close to systemd's udev, but should at least add _something_ to by-id when Alpine's mdev wouldn't - if device reports model and serial, all was/is good; - introduce: - if model or serial missing, try parsing them from wwid; - falls back to using the sanitized wwid as serial if parsing fails - last resort: falls back to using 'noserial' as serial for devices that only have a model. - also adds `util-linux` related apks, which brings a more capable `blkid` - for context: https://gitlab.alpinelinux.org/alpine/mdev-conf/-/commits/master/?ref_type=HEADS - add a log of logging, which you can see with `cat /var/log/mdev.log` on Hook's console Signed-off-by: Ricardo Pardini <[email protected]>
1 parent a29d487 commit c361f85

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

images/hook-mdev/Dockerfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ FROM alpine
22

33
USER root:root
44

5-
RUN apk add --no-cache mdev-conf && rm -rf /var/cache/apk/*
5+
RUN apk add --no-cache mdev-conf util-linux util-linux-misc busybox && rm -rf /var/cache/apk/*
6+
7+
# Overwrite the persistent storage script
8+
COPY persistent-storage.sh lib/mdev/persistent-storage
9+
RUN chmod +x lib/mdev/persistent-storage
610

711
CMD ["mdev", "-v", "-df"]
812

13+
# -v Verbose
14+
# -S Log to syslog too
15+
# -s Scan /sys and populate /dev
16+
# -d Daemon, listen on netlink
17+
# -f Run in foreground

images/hook-mdev/persistent-storage.sh

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#!/bin/sh
22

3+
echo "--> STARTING persistent-storage script with MDEV='${MDEV}' ACTION='${ACTION}' all params: $*" >&2
4+
35
symlink_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
}
@@ -58,12 +63,18 @@ else
5863
fi
5964

6065
model=$(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
6269
serial=$(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
6574
wwid=$(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-)
7283
if [ -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
7993
fi
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+
81150
if [ -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
146219
fi
220+
221+
echo "--> FINISHED persistent-storage script with MDEV='${MDEV}' ACTION='${ACTION}' all params: $*" >&2
222+
echo "" >&2

0 commit comments

Comments
 (0)