Overriding / Patching Linux System Serial Number
I'm a big fan of test driven development (TDD) for infrastructure components. I'm currently working on a hardware-related topic where we also use the system serial number as identifier. To create a proper integration test, we need to be able to start a system and set the serial number to a known value. This can easily be done with the help of virtual machines like in VMware or VirtualBox, but I couldn't find a way for changing the system serial number on hardware boxes, cloud VMs (e.g. on Alibaba Cloud) or other Linux system.
Problem Analysis
I was thinking: Linux is the operating system where I can potentially do everything. So how hard can this be? After some digging around I found out that there are those main sources for the serial number on Linux:
-
/sys/firmware/dmi/tables/DMI
contains a binary blob of Desktop Management Interface data provided by the kernel and the dmidecode utility is commonly used to decode it and extract the system serial number like this:
-
/sys/devices/virtual/dmi/id/product_serial
and/sys/devices/virtual/dmi/id/product_uuid
are text files that also contain the system serial number:
Solution Approach
The idea behind the solution presented here is to not change the serial number of the system itself, but rather change the serial number visible to software running in user space. This should be enough for most problems where software looks at the serial number to identify a system.
To solve my problem I created a little program that overrides the serial number in user space, using it looks like this:
The Code
The program can be found at override-serial-number.sh (GitHub Gist) and it looks like this:
#!/bin/bash | |
# See https://schlomo.schapiro.org/2023/01/overriding-patching-linux-system-serial.html | |
# Copyright 2023 Forto Logistics AG & Co. KG / Schlomo Schapiro | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software | |
# and associated documentation files (the "Software"), to deal in the Software without | |
# restriction, including without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the | |
# Software is furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all copies or | |
# substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | |
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
set -euo pipefail | |
function die { | |
echo -e "ERROR: $*" | |
exit 1 | |
} | |
for tool in bbe dmidecode; do | |
type "$tool" &>/dev/null || die "cannot find $tool utility, please install" | |
done | |
function removeOverride { | |
umount -v /sys/devices/virtual/dmi/id/product_serial /sys/devices/virtual/dmi/id/product_uuid /sys/firmware/dmi/tables/DMI || die "cannot remove override, please check state of /sys/devices/virtual/dmi/id/product_serial /sys/devices/virtual/dmi/id/product_uuid /sys/firmware/dmi/tables/DMI" | |
rm -f /etc/fakeDMI /etc/fakeSERIAL | |
} | |
if [[ $# -eq 0 || "$1" == "--help" ]]; then | |
echo "$0 < --remove | SERIAL >" | |
echo "Override DMI info with given SERIAL number or remove override" | |
exit 1 | |
elif [ "$1" == "--remove" ]; then | |
removeOverride | |
echo "Successfully restored serial number to original >$(dmidecode -s system-serial-number)<" | |
elif [ -s /etc/fakeSERIAL ]; then | |
die "serial number override already in place with >$(</etc/fakeSERIAL)<, please remove first" | |
else | |
# proceed to override serial number | |
new="$1" | |
old="$(dmidecode -s system-serial-number)" | |
if [ "$old" ]; then | |
echo "Old serial: $old" | |
[ ${#new} -lt ${#old} ] || die "New serial cannot be as long or longer as the old serial" | |
fi | |
echo "$new" >/etc/fakeSERIAL | |
mount --bind /etc/fakeSERIAL /sys/devices/virtual/dmi/id/product_serial | |
mount --bind /etc/fakeSERIAL /sys/devices/virtual/dmi/id/product_uuid | |
bbe -e "s/$old/$new\0/" </sys/firmware/dmi/tables/DMI >/etc/fakeDMI || die "Cannot patch DMI" | |
mount --bind /etc/fakeDMI /sys/firmware/dmi/tables/DMI | |
check="$(dmidecode -s system-serial-number)" | |
if [ "$check" == "$new" ]; then | |
echo "Successfully set >$new< as new serial number." | |
else | |
removeOverride | |
die "Overriding serial number not successful, new serial was >$check< and not >$new<." | |
fi | |
fi |
I hope that you find it useful.
Thanks for this. I needed to remove VMWare from the beginning of the serial for inventory. You can't seem to do it from the VMX. This worked great.
ReplyDeleteThanks ,Do you have any method to override uuid?
ReplyDeleteWhich UUID? Is that another file under /sys? You should be able to adapt this code for that as well.
Delete