Skip to content

Instantly share code, notes, and snippets.

@michaelmrose
Created November 1, 2024 16:08
Show Gist options
  • Save michaelmrose/6df5efce3bde8c3245d7953a8de90478 to your computer and use it in GitHub Desktop.
Save michaelmrose/6df5efce3bde8c3245d7953a8de90478 to your computer and use it in GitHub Desktop.
System update script from the now defunct project trident
#!/bin/bash
# Script to automate the installation of system updates with appropriate safeguards from ZFS boot environments
#temporary log files and state flags
_checklog="/tmp/trident-update-check.log"
_uplog="/tmp/trident-update.log"
showUsage(){
echo "Usage:
trident-update <-update | -check>
-update: Perform updates after making a snapshot of the current system boot environment
-check: Check for available updates
"
}
mkSnapshot(){
tag=${1:-pre-update}
BE=$(mount | grep " on / type zfs" | cut -d ' ' -f 1)
if [ -z "${BE}" ] ; then return ; fi
local _dt=$(date "+%F-%H-%M")
echo "Creating snapshot of current boot environment"
zfs snapshot "${BE}@${tag}-${_dt}"
}
updateXbps(){
curver=$(xbps-query -p pkgver xbps | cut -d - -f 2)
newver=$(xbps-query -R -p pkgver xbps | cut -d - -f 2)
if [ "${curver}" != "${newver}" ] ; then
mkSnapshot "pre-xbps"
xbps-install -y xbps
fi
}
checkUpdates(){
# Update repository catalogs
xbps-install -S
#Check if xbps itself needs to be updated first
updateXbps
# Scan for updates
local cnum=0
xbps-install -un
# Format the info
}
rebuildInitramfs(){
#This is a safeguard for ZFS-on-linux.
# Seen a couple cases where the initramfs was not rebuilt properly or at the right time
# preventing bootup with the new kernel.
# Just take a few seconds here to rebuild that image every time
# Get the currently-installed linux package name
linuxpkg=`xbps-query --regex -s 'linux[0-9]\.[0-9]?[0-9]$' -p "pkgname" | cut -d ' ' -f 2 | sort -V | tail -1`
echo "Got Linux Kernel Package: ${linuxpkg}"
# Reconfigure that package
xbps-reconfigure -f "${linuxpkg}"
}
doUpdates(){
# Update repository catalogs
xbps-install -S
#Check if xbps itself needs to be updated first
updateXbps
# Create ZFS snapshot of the current system pre-update
mkSnapshot
# Prune any old kernels first (available in older snapshots as needed)
# Will typically keep 2 around (currently working + new from updates about to happen)
# (Decreases update time by reducing amount of DKMS modules to build during upgrades)
vkpurge rm all
# Start doing updates
xbps-install -uy
rebuildInitramfs
}
# Check for permissions first
if [ $(id -u) -ne 0 ] ; then
echo "ERROR: This script needs to run as root"
exit 1
fi
# Evaluate arguments
case "${1}" in
-update)
doUpdates
;;
-check)
checkUpdates
;;
*)
showUsage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment