Skip to content

Instantly share code, notes, and snippets.

@ahjohannessen
Forked from neilmayhew/update-oem-vmware.sh
Last active July 15, 2021 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahjohannessen/54845bf979e86c32390bf47917cea33a to your computer and use it in GitHub Desktop.
Save ahjohannessen/54845bf979e86c32390bf47917cea33a to your computer and use it in GitHub Desktop.
Update a Flatcar installation on VMWare to use the latest OEM content
#!/usr/bin/env bash
# Update a Flatcar installation on VMWare to use the latest OEM content
#
# Copyright 2020, Neil Mayhew <neil@kerith.ca>
# LICENSE: MIT
set -ex
shopt -s extglob nullglob
OEMCONTENT=oem-vmware.tgz
KEEPCONTENT=
if [ -n "$1" ]
then
OEMCONTENT=$1
KEEPCONTENT=yes
fi
# Cache sudo credentials
sudo true
if [ ! -f "$OEMCONTENT" ]
then
# Fetch the release-signing public key
KEYID=782B3BC9F10CF638A5DCF5105B2910CBFCBEAB91
KEYSERVER=hkp://keys.gnupg.net:80
gpg --keyserver $KEYSERVER --recv-key $KEYID
# Download the current stable VMWare Flatcar release
IMGNAME=flatcar_production_vmware_raw_image.bin
wget -N https://stable.release.flatcar-linux.net/amd64-usr/current/${IMGNAME}.bz2{,.sig}
gpg --verify ${IMGNAME}.bz2{.sig,}
bunzip2 -k ${IMGNAME}.bz2
# Mount the OEM image partition via loopback
MNT=$(mktemp -d) && trap 'rmdir "$MNT"' 0
LOOPDEV=$(sudo losetup -f --show -P ${IMGNAME})
sudo mount -r "${LOOPDEV}p6" "$MNT"
# Save the content
tar -cvzf "$OEMCONTENT" --exclude=lost+found -C "$MNT" .
# Unmount the OEM image partition
sudo umount "$MNT"
sudo losetup -d "${LOOPDEV}"
# Remove the downloaded image files
rm -f ${IMGNAME}{,.bz2{.sig,}}
fi
# Stop existing services and remove them
if [ -d /usr/share/oem/units/ ]
then
cd /usr/share/oem/units/
UNITS=(*)
cd "$OLDPWD"
sudo systemctl stop -- "${UNITS[@]}" || true
cd /etc/systemd/system/
sudo rm -f "${UNITS[@]}"
cd "$OLDPWD"
sudo systemctl daemon-reload
fi
# Remove the exiting content
sudo rm -rf /usr/share/oem/!(lost+found)
# Install the new content
sudo tar -xf "$OEMCONTENT" -C /usr/share/oem
[ -n "$KEEPCONTENT" ] || rm -f "$OEMCONTENT"
# Install new services and start them
if [ -d /usr/share/oem/units/ ]
then
cd /usr/share/oem/units/
UNITS=(*)
[ "${#UNITS[@]}" -gt 0 ] &&
sudo cp -p -- "${UNITS[@]}" /etc/systemd/system/
cd "$OLDPWD"
sudo systemctl daemon-reload
sudo systemctl start -- "${UNITS[@]}"
fi
# Inform the user
set +x
echo "New OEM content was installed and services were restarted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment