Files
snapd/tests/lib/reflash.sh
Valentin David d824ad253c tests/lib/prepare.sh: reflash during shutdown
Originally, to reflash from Classic to Ubuntu Core, we rebooted with a
grub entry that would use specific `root=` and `init=` script to reflash
the disk.

We do not need a full reboot most of the time and we can just instead
prepare an initramfs to switch when shutting down. Systemd will switch
to `/run/initramfs` and run `/shutdown` in there.

However, on 16.04, systemd is unmounting mounts which makes this not
as easy. So instead there we still reboot to grub, but instead of using
`init=` and mount the root disk, we provide an initrd which can do the
flashing, simplifying the method we have been using.
2024-03-14 09:54:12 +01:00

78 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# flash.sh: Install a flashing process to reboot to a new disk image
#
# Copyright (C) 2023-2024 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# This produces an Ubuntu Core image for testing in file `pc.img.gz`.
# It will initialize the spread user as well as the project directory
# (which has to be defined in `PROJECT_PATH`).
# This is expected to be called from spread.
# This is to reflash a spread runner.
# This script takes one argument: a gzipped image to be flashed onto the disk.
# After successfully running this script, a reboot or power down will
# flash the machine with the given image.
set -eux
DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends dracut-core busybox-initramfs
[ -d /run/initramfs ] || mkdir -p /run/initramfs
systemd-mount --property=DefaultDependencies=no --options=exec,size=2G none --type=tmpfs /run/initramfs
cp -T "${1}" /run/initramfs/image.gz
for try in /usr/lib/systemd/systemd-shutdown /lib/systemd/systemd-shutdown; do
if [ -x "${try}" ]; then
systemd_shutdown="${try}"
fi
done
/usr/lib/dracut/dracut-install --ldd -D/run/initramfs -a umount systemctl dd "${systemd_shutdown}"
/usr/lib/dracut/dracut-install --ldd -D/run/initramfs /usr/lib/initramfs-tools/bin/busybox /bin/busybox
ln -s busybox /run/initramfs/bin/sh
ln -s busybox /run/initramfs/bin/gunzip
if [ -b /dev/vda ]; then
DISK=/dev/vda
elif [ -b /dev/sda ]; then
DISK=/dev/sda
elif [ -b /dev/nvme0n1 ]; then
DISK=/dev/nvme0n1
else
echo "Cannot find disk" 2>&1
exit 1
fi
cat <<EOF >/run/initramfs/shutdown
#!/bin/sh
echo "SHUTTING DOWN"
set -eu
umount -l /oldroot
gunzip -c /image.gz | dd of='${DISK}' bs=32M
exec '${systemd_shutdown}' "\${@}"
EOF
chmod +x /run/initramfs/shutdown