Skip to content

Commit 45b3356

Browse files
authored
hook-mdev: persistent-storage: further sanitize model/serial parsed from wwid (#229)
#### hook-mdev: `persistent-storage`: further sanitize model/serial parsed from wwid - some hardware vendors have literal `\0` (backslash-zero) instead of proper null bytes in wwid - encoding that in YAML is painful, and systemd udev trims those out Signed-off-by: Ricardo Pardini <[email protected]>
2 parents 19d73bd + ac2babd commit 45b3356

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

images/hook-mdev/persistent-storage.sh

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,26 @@ else
107107
fi
108108
echo "SYSFS parse model/serial from wwid_raw:'${wwid_raw}'" >&2
109109
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
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+
wwid_model=$(echo "${rest}" | sed "s/ ${wwid_serial}$//") # Remove the serial from the rest of the string
115+
: # sanitize model ----------------------------------------------------------------------
116+
wwid_model=$(echo "${wwid_model}" | tr ' ' '_' | tr '.' '_' | tr '/' '_') # Replace any remaining spaces, dots, slashes in the rest part with underscores
117+
wwid_model=$(echo "${wwid_model}" | sed 's/\\0//g') # Remove all instances of literal backslash-zero "\0" (not really nulls)
118+
wwid_model=$(echo "${wwid_model}" | sed 's/\\/_/g') # replace any remaining backslashes with underscores
119+
wwid_model=$(echo "${wwid_model}" | sed 's/__*/_/g') # Remove consecutive underscores
120+
wwid_model=$(echo "${wwid_model}" | sed 's/^_//;s/_$//') # Remove leading and trailing underscores
121+
: # sanitize serial ---------------------------------------------------------------------
122+
wwid_serial=$(echo "${wwid_serial}" | tr ' ' '_' | tr '.' '_' | tr '/' '_') # Replace any remaining spaces, dots, slashes in the rest part with underscores
123+
wwid_serial=$(echo "${wwid_serial}" | sed 's/\\0//g') # Remove all instances of literal backslash-zero "\0" (not really nulls)
124+
wwid_serial=$(echo "${wwid_serial}" | sed 's/\\/_/g') # replace any remaining backslashes with underscores
125+
wwid_serial=$(echo "${wwid_serial}" | sed 's/__*/_/g') # Remove consecutive underscores
126+
wwid_serial=$(echo "${wwid_serial}" | sed 's/^_//;s/_$//') # Remove leading and trailing underscores
127+
128+
unset rest wwid_prefix
129+
echo "WWID parsing came up with wwid_model='${wwid_model}', wwid_serial='${wwid_serial}'" >&2
121130
else
122131
echo "WWID is empty or not found" >&2
123132
fi

0 commit comments

Comments
 (0)