metricspi/rootfs/overlay/usr/sbin/system-update

77 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
# vim: set sw=4 ts=4 sts=4 et :
check_system() {
rc=0
if ! mountpoint -q /boot; then
echo 'ERROR: /boot is not mounted' >&2
rc=1
elif [ ! -w /boot ]; then
echo 'ERROR: /boot is not writable' >&2
rc=1
fi
if ! mountpoint -q /boot/efi; then
echo 'ERROR: /boot/efi is not mounted' >&2
rc=1
elif [ ! -w /boot/efi ]; then
echo 'ERROR: /boot/efi is not writable' >&2
rc=1
fi
return ${rc}
}
die() {
rc=$?
echo "$1" >&2
exit ${rc}
}
do_update() {
if [ -d /boot/efi/.old ]; then
rm -rf /boot/efi/.old || die 'Failed to remove /boot/efi/.old'
fi
mkdir /boot/efi/.old || die 'Failed to create /boot/efi/.old'
for x in /boot/efi/*; do
mv "${x}" /boot/efi/.old/ \
|| die "Failed to move ${x} to /boot/efi/.old/"
done
mv /boot/rootfs.squashfs /boot/rootfs.squashfs.old \
|| die 'Failed to rename rootfs.squashfs'
echo 'Extracting root filesystem ...'
tar -xf "${filename}" -C /boot rootfs.squashfs \
|| die 'Failed to extract rootfs.squashfs'
echo 'Extracting kernel/initramfs/firmware ...'
tar -xf "${filename}" -C /boot/efi \
--exclude rootfs.squashfs \
--no-same-owner \
|| die 'Failed to extract kernel/initramfs/firmware'
echo 'The machine will need to be rebooted to complete the update process'
}
usage() {
printf 'usage: %s filename\n' "${0##*/}"
}
filename=
while [ $# -ge 1 ]; do
case "$1" in
-h|--help)
usage
exit 0
;;
*)
filename="$1"
shift
;;
esac
done
if [ -z "${filename}" ]; then
usage >&2
exit 2
fi
check_system || exit $?
do_update