chamesle-snapshot (7756B)
1 #!/bin/bash 2 # system snapshots: one before every update, the last 3 kept, each one bootable from the boot menu 3 # chamesle-snapshot list 4 # chamesle-snapshot take (root; the apk hook runs it before upgrades) 5 # chamesle-snapshot restore [ID] (root; ID defaults to the snapshot you started from) 6 # chamesle-snapshot notice (at login: offers to keep the snapshot you started from) 7 # only on btrfs with the system in the @ subvolume, what the installer makes from 0.4 on. the snapshots are of @ 8 # only: your files are in @home and never go back in time 9 set -euo pipefail 10 KEEP=3 11 TOP=/run/chamesle-snapshots # the whole btrfs, mounted while we work on it 12 BOOT=/boot/chamesle-snapshots # kernel and initramfs of each snapshot, where grub can read them 13 CFG=/boot/grub/chamesle-snapshots.cfg 14 15 die() { echo "chamesle-snapshot: $*" >&2; exit 1; } 16 # where / comes from: /@ normally, /@snapshots/ID when started from a snapshot 17 fsroot() { findmnt -no FSROOT /; } 18 supported() { 19 [ "$(findmnt -no FSTYPE / 2>/dev/null)" = btrfs ] || return 1 20 case $(fsroot) in /@|/@snapshots/*) return 0;; esac 21 return 1 22 } 23 started_from() { case $(fsroot) in /@snapshots/*) basename "$(fsroot)";; esac; } 24 device() { findmnt -no SOURCE / | sed 's/\[.*//'; } 25 val() { sed -n "s/^$2=//p" "$BOOT/$1/info" 2>/dev/null; } 26 ids() { ls -1 "$BOOT" 2>/dev/null | grep -E '^[0-9]{8}-[0-9]{6}$' | sort || true; } 27 28 mount_top() { 29 mkdir -p $TOP 30 mountpoint -q $TOP || mount -o subvolid=5 "$(device)" $TOP 31 trap 'umount -R $TOP 2>/dev/null; rmdir $TOP 2>/dev/null; true' EXIT 32 } 33 34 # the "before an update" submenu, newest first 35 menu() { 36 if [ -z "$(ids)" ]; then rm -f $CFG; return; fi 37 local uuid args access rel id 38 # grub's own helpers, which use variables that aren't set 39 set +u 40 . /usr/share/grub/grub-mkconfig_lib 41 uuid=$(findmnt -no UUID /) 42 # the same options the system started with, pointed at the snapshot 43 args=$(tr ' ' '\n' < /proc/cmdline | grep -vE '^(BOOT_IMAGE|root|rootflags|initrd)=' | grep -v '^$' | tr '\n' ' ') 44 access=$(prepare_grub_to_access_device $(grub-probe --target=device $BOOT) | sed 's/^/\t/') 45 rel=$(grub-mkrelpath $BOOT) 46 set -u 47 # without these lines grub would look on the wrong disk: better no new entries than broken ones 48 grep -q search <<< "$access" || { echo "chamesle-snapshot: grub can't find /boot, boot menu not updated" >&2; return 0; } 49 { 50 echo "submenu 'Chamesle Linux, from before an update' {" 51 for id in $(ids | sort -r); do 52 echo " menuentry 'Before the update of $(val $id date)' {" 53 echo "$access" 54 echo " linux $rel/$id/vmlinuz root=UUID=$uuid rootflags=subvol=@snapshots/$id $args" 55 echo " initrd $rel/$id/initramfs.img" 56 echo " }" 57 done 58 echo "}" 59 } > $CFG.new 60 mv $CFG.new $CFG 61 } 62 63 take() { 64 supported || exit 0 65 [ -z "$(started_from)" ] || { echo "chamesle-snapshot: started from a snapshot, not taking another one" >&2; exit 0; } 66 local k id 67 # the kernel the system boots now, which the snapshot has the modules for 68 k=$(ls /boot/vmlinuz-*-cachyos-lts 2>/dev/null | sed 's#.*/vmlinuz-##' | sort -V | tail -n1) 69 [ -n "$k" ] || k=$(ls /boot/vmlinuz-* 2>/dev/null | sed 's#.*/vmlinuz-##' | sort -V | tail -n1) 70 [ -n "$k" ] && [ -f /boot/initramfs-$k.img ] || die "no kernel and initramfs in /boot" 71 mount_top 72 id=$(date +%Y%m%d-%H%M%S) 73 mkdir -p $TOP/@snapshots $BOOT/$id 74 btrfs -q subvolume snapshot $TOP/@ $TOP/@snapshots/$id 75 cp --reflink=auto /boot/vmlinuz-$k $BOOT/$id/vmlinuz 76 cp --reflink=auto /boot/initramfs-$k.img $BOOT/$id/initramfs.img 77 printf 'date=%s\nkernel=%s\n' "$(LC_ALL=C date '+%-d %b %Y, %H:%M')" "$k" > $BOOT/$id/info 78 # a system replaced by restore, and snapshots without their boot files, can't be started: they go first 79 for d in $TOP/@snapshots/*; do 80 [ -d "$d" ] && [ ! -f "$BOOT/${d##*/}/info" ] && btrfs -q subvolume delete "$d" || true 81 done 82 for id in $(ids | head -n -$KEEP); do 83 btrfs -q subvolume delete $TOP/@snapshots/$id 2>/dev/null || true 84 rm -rf "${BOOT:?}/$id" 85 done 86 for id in $(ids); do [ -d $TOP/@snapshots/$id ] || rm -rf "${BOOT:?}/$id"; done 87 menu 88 } 89 90 list() { 91 supported || { echo "no snapshots: they need a system installed on btrfs (from Chamesle 0.4 on)"; return; } 92 local id now 93 now=$(started_from) 94 for id in $(ids | sort -r); do 95 echo "$id before the update of $(val $id date)$([ "$id" = "$now" ] && echo ' (running now)')" 96 done 97 } 98 99 restore() { 100 supported || die "snapshots need a system installed on btrfs" 101 local id=${1:-$(started_from)} k new 102 [ -n "$id" ] || die "which one? see chamesle-snapshot list" 103 [[ $id =~ ^[0-9]{8}-[0-9]{6}$ ]] && [ -f $BOOT/$id/info ] || die "no snapshot $id" 104 mount_top 105 [ -d $TOP/@snapshots/$id ] || die "no snapshot $id" 106 # the system being replaced stays until the next update, in case 107 mv $TOP/@ $TOP/@snapshots/replaced-$(date +%Y%m%d-%H%M%S) 108 btrfs -q subvolume snapshot $TOP/@snapshots/$id $TOP/@ 109 # finish from inside the restored system, so the boot menu is made the way it sees itself 110 new=$TOP/new 111 mkdir -p $new 112 mount -o subvol=@ "$(device)" $new 113 # rslave: unmounting these again can't take the real /dev and /sys with it 114 for d in dev proc sys; do mount --rbind /$d $new/$d; mount --make-rslave $new/$d; done 115 mountpoint -q /boot && mount --bind /boot $new/boot 116 chroot $new /usr/bin/chamesle-snapshot settle $id 117 echo "done: restart and Chamesle starts as it was before the update of $(val $id date)" 118 } 119 120 # inside the restored system: its kernel back in /boot, kernels it has no modules for out, then the boot menu 121 settle() { 122 local id=$1 k f v 123 if mountpoint -q /boot; then 124 k=$(val $id kernel) 125 cp $BOOT/$id/vmlinuz /boot/vmlinuz-$k 126 cp $BOOT/$id/initramfs.img /boot/initramfs-$k.img 127 for f in /boot/vmlinuz-*; do 128 v=${f#/boot/vmlinuz-} 129 [ -d /usr/lib/modules/$v ] || rm -f /boot/vmlinuz-$v /boot/initramfs-$v.img /boot/config-$v /boot/System.map-$v 130 done 131 fi 132 grub-mkconfig -o /boot/grub/grub.cfg >/dev/null 2>&1 133 menu 134 } 135 136 # the dialogs at login are in the user's language, from chamesle-l10n 137 # python looks it up: the gettext program isn't on every system, python is 138 _() { python3 -c 'import gettext, sys; gettext.textdomain("chamesle"); sys.stdout.write(gettext.gettext(sys.argv[1]))' "$1" 2>/dev/null || printf %s "$1"; } 139 140 # at login, when started from a snapshot from the boot menu 141 notice() { 142 local id when 143 id=$(started_from) 144 [ -n "$id" ] || exit 0 145 # the date written the user's way: the id is when it was taken 146 when=$(date -d "${id:0:8} ${id:9:2}:${id:11:2}" '+%x, %H:%M' 2>/dev/null || val $id date) 147 zenity --question --width 420 --title "$(_ "Started from before an update")" \ 148 --text "$(printf "$(_ "This is Chamesle as it was before the update of %s.\n\nIf that update broke something, keep this one and it's your system from the next restart. Your files stay as they are either way.")" "$when")" \ 149 --ok-label "$(_ "Keep this one")" --cancel-label "$(_ "Not now")" || exit 0 150 if pkexec /usr/bin/chamesle-snapshot restore $id; then 151 zenity --info --width 380 --title "$(_ "Done")" --text "$(_ "Restart, and Chamesle starts as it is now.")" 152 else 153 zenity --error --width 380 --title "$(_ "That didn't work")" --text "$(_ "Nothing was changed. Try again, or ask on the board.")" 154 fi 155 } 156 157 case ${1:-} in 158 take|list|menu) "$1";; 159 restore) restore "${2:-}";; 160 settle) settle "$2";; 161 notice) notice;; 162 *) sed -n '2,8s/^# \?//p' "$0" >&2; exit 2;; 163 esac