Skip to content

Instantly share code, notes, and snippets.

@schlomo
Created January 20, 2023 10:54
Show Gist options
  • Save schlomo/87b5f353d142328e167727a4c364e5c8 to your computer and use it in GitHub Desktop.
Save schlomo/87b5f353d142328e167727a4c364e5c8 to your computer and use it in GitHub Desktop.
Overriding / Patching Linux System Serial Number
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment