#!/bin/sh
# Armbian: a boot partition is not in the snapshots of the root. Copy it
# into the root before a snapshot, so that each snapshot has its own kernel.
# The boot partition is /boot, or else /boot/firmware: a Raspberry Pi keeps
# /boot in the root, but its firmware loads the kernel from /boot/firmware.
# armbian-snapshot restore copies it back. Callers: the snapper plugin
# 40-armbian-boot (snapper 0.10.5 and newer). For snapper 0.10.4: the apt
# hook 79armbian-snapshots, armbian-snapshot create and a drop-in for
# snapper-boot.service.
# rsync writes only the changed files: btrfs shares the others with the
# older snapshots.
# Not on a snapshot boot: its root is a RAM overlay.
grep -q 'armbian\.snapshot=' /proc/cmdline && exit 0
if mountpoint -q /boot; then
	part=/boot
	dst=/var/lib/armbian-snapshots/boot
elif mountpoint -q /boot/firmware; then
	part=/boot/firmware
	dst=/var/lib/armbian-snapshots/boot-firmware
else
	exit 0
fi
# The mark <copy>.ok says that the copy is complete. Restore uses only a
# complete copy.
rm -f "$dst.ok"
mkdir -p "$dst"
# --modify-window: vfat keeps the time in steps of 2 seconds.
if rsync -rlt --delete --modify-window=1 "$part/" "$dst/"; then
	touch "$dst.ok"
else
	logger -t armbian-snapshots "copy of $part into the root failed"
fi
exit 0
