From 892b9b31bf9afb768122f5c4b829e32772ce1ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdurrahman=20Kaan=20Dike=C3=A7?= Date: Fri, 9 Oct 2026 13:30:07 +0300 Subject: [PATCH] mu300-update: rollback-boot brings back the modules of the image it restores Two builds of one kernel release (6.18.55 of v2026.10.11 and of v2026.10.12) share lib/modules/. boot_update put the new bundle's modules there before it knew whether the image would change and kept nothing of the old set, so rollback-boot - and the restore after a failed write - left the old kernel with the other build's modules. On F50-B that kernel did not come up twice and the one-boot fallback took it back to Android. Now the set in place is kept in boot/prev.modules with the previous image, only when the image is about to change, and goes back with it in rollback-boot and after a failed write; an up-to-date image still gets its modules into a system the update just replaced. A 5.4 bundle (no kernel.release) keeps nothing and touches no mainline set. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DqKKtBQZCfxYnHuBCuDqqn --- rootfs/overlay/opt/mu300/bin/mu300-update | 52 ++++++++++++++++++++-- tests/test_update.py | 53 +++++++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/rootfs/overlay/opt/mu300/bin/mu300-update b/rootfs/overlay/opt/mu300/bin/mu300-update index 68d9d6f1..4316678c 100755 --- a/rootfs/overlay/opt/mu300/bin/mu300-update +++ b/rootfs/overlay/opt/mu300/bin/mu300-update @@ -739,11 +739,46 @@ bootimg_from_stock() { # bootimg_from_stock STOCK KERNEL RAMDISK DIR # on the Linux filesystem, next to the ones of the other kernel, which stay for a switch back. Ubuntu keeps them under # extra/ and indexes them (depmod; MU300_NO_DEPMOD=1 leaves that to its next boot, as the Magisk installer must: the # depmod Android has is busybox's); OpenWrt keeps them flat and loads by path. +# modules_dir OS KREL: where the modules of a kernel bundle go in OS +modules_dir() { case $1 in ubuntu) echo "$DISK/$1/lib/modules/$2/extra" ;; *) echo "$DISK/$1/lib/modules/$2" ;; esac; } + +# The set a bundle replaces belongs to the boot image it replaces: two builds of one kernel release (6.18.55 of two +# releases) share the directory, and a kernel with the other build's modules does not come up (the one-boot fallback +# takes the device back to Android). So the set in place is kept with the previous image, and goes back with it - +# in rollback-boot, and when writing the new image fails. +prev_modules_save() { # prev_modules_save BUNDLE_DIR (unpacked; a bundle without kernel.release keeps nothing) + rm -rf "$BOOTDIR/prev.modules" + _krel=$(cat "$1/kernel.release" 2>/dev/null) || return 0 + case $_krel in ''|*/*|.*) return 0 ;; esac + mkdir -p "$BOOTDIR/prev.modules" && echo "$_krel" > "$BOOTDIR/prev.modules/kernel.release" || return 1 + for _os in $(installed_systems); do + _d=$(modules_dir "$_os" "$_krel") + ls "$_d"/*.ko >/dev/null 2>&1 || continue + mkdir -p "$BOOTDIR/prev.modules/$_os" && cp "$_d"/*.ko "$BOOTDIR/prev.modules/$_os/" || return 1 + done +} +prev_modules_restore() { + _krel=$(cat "$BOOTDIR/prev.modules/kernel.release" 2>/dev/null) || return 0 + case $_krel in ''|*/*|.*) return 0 ;; esac + for _os in $(installed_systems); do + _d=$(modules_dir "$_os" "$_krel") + [ -d "$_d" ] || continue + rm -f "$_d"/*.ko + # a system that had no set of this release then gets none now (the kernel back is another release) + [ -d "$BOOTDIR/prev.modules/$_os" ] || continue + cp "$BOOTDIR/prev.modules/$_os"/*.ko "$_d/" || { echo "boot: the previous modules of $_os did not go back" >&2; return 1; } + if [ "$_os" = ubuntu ] && [ -z "${MU300_NO_DEPMOD:-}" ] && command -v depmod >/dev/null; then + depmod -b "$DISK/$_os" "$_krel" 2>/dev/null || true + fi + done + printf 'boot: the modules of the previous image (%s) are back in: %s\n' "$_krel" "$(installed_systems)" +} + kernel_modules_into_systems() { # kernel_modules_into_systems BUNDLE_DIR (unpacked) _krel=$(cat "$1/kernel.release" 2>/dev/null) case $_krel in ''|*/*|.*) echo "boot: bad kernel release '$_krel' in the bundle" >&2; return 1 ;; esac for _os in $(installed_systems); do - case $_os in ubuntu) _d=$DISK/$_os/lib/modules/$_krel/extra ;; *) _d=$DISK/$_os/lib/modules/$_krel ;; esac + _d=$(modules_dir "$_os" "$_krel") # the set of the bundle replaces the one of an earlier bundle of this release: a module it no longer has # would otherwise stay loadable (and the set is some 360 modules, 20 MiB, since FINDINGS 31o) mkdir -p "$_d" && rm -f "$_d"/*.ko && cp "$1"/modules/*.ko "$_d/" || @@ -797,13 +832,15 @@ boot_update() { # boot_update TAG (MU300_KERNEL_BUNDLE=FILE uses a local mu30 fi untar -xzf "$bundle" -C "$tmp" ./Image ./ramdisk-generic.lz4 || { echo "boot: unpacking $bundle failed" >&2; return 1; } [ -s "$tmp/Image" ] && [ -s "$tmp/ramdisk-generic.lz4" ] || { echo "boot: no kernel in the bundle" >&2; return 1; } - # the modules of a kernel the systems were not built with go in before the boot image changes + # the modules of a kernel the systems were not built with (mainline): they go in below, before the boot image + # changes, and also when the image is up to date (a system the update just replaced has none yet) + mods= if grep -qx './kernel.release' "$tmp/list"; then untar -xzf "$bundle" -C "$tmp" ./kernel.release ./modules || { echo "boot: unpacking the modules failed" >&2; return 1; } for f in modules.builtin modules.builtin.modinfo; do if grep -qx "./$f" "$tmp/list"; then untar -xzf "$bundle" -C "$tmp" "./$f" || true; fi done - kernel_modules_into_systems "$tmp" || return 1 + mods=1 fi id=$(cat "$tmp/Image" "$tmp/ramdisk-generic.lz4" | sha256sum | cut -d' ' -f1) @@ -819,6 +856,7 @@ boot_update() { # boot_update TAG (MU300_KERNEL_BUNDLE=FILE uses a local mu30 dd if="$dev" bs=4096 skip=$((osize / 4096)) count=$(( (vbs + 4095) / 4096 )) 2>/dev/null | head -c "$vbs" > "$tmp/vbmeta" cur=$(sha256sum < "$tmp/body" | cut -d' ' -f1) if [ "$cur" = "$(cat "$BOOTDIR/written.sha256" 2>/dev/null)" ] && [ "$id" = "$(cat "$BOOTDIR/installed.id" 2>/dev/null)" ]; then + [ -z "$mods" ] || kernel_modules_into_systems "$tmp" || return 1 printf 'boot: already up to date (%s)\n' "$(cat "$BOOTDIR/installed.tag" 2>/dev/null || echo "$tag")" rm -rf "$tmp"; return 0 fi @@ -839,7 +877,11 @@ boot_update() { # boot_update TAG (MU300_KERNEL_BUNDLE=FILE uses a local mu30 bootimg_assemble "$tmp" "$vbs" "$tmp/Image" "$BOOTDIR/device-ramdisk.lz4" "$tmp/ramdisk-generic.lz4" || { echo "boot: not touching $bp" >&2; return 1; } - # keep the current image for rollback-boot, then write + # keep the current image and the module set it runs with for rollback-boot, then the new modules, then write + prev_modules_save "$tmp" || { echo "boot: keeping the modules in place failed; nothing was changed" >&2; return 1; } + if [ -n "$mods" ] && ! kernel_modules_into_systems "$tmp"; then + prev_modules_restore >/dev/null; echo "boot: the boot image is unchanged" >&2; return 1 + fi cp "$tmp/body" "$BOOTDIR/prev.body"; cp "$tmp/vbmeta" "$BOOTDIR/prev.vbmeta"; cp "$tmp/footer" "$BOOTDIR/prev.footer" linux_slot > "$BOOTDIR/prev.slot" # rollback-boot puts it back only on the slot it came from cat "$BOOTDIR/installed.tag" > "$BOOTDIR/prev.tag" 2>/dev/null || echo installer > "$BOOTDIR/prev.tag" @@ -849,6 +891,7 @@ boot_update() { # boot_update TAG (MU300_KERNEL_BUNDLE=FILE uses a local mu30 echo "boot: writing $bp failed verification; restoring the previous image" >&2 write_boot "$dev" "$psize" "$BOOTDIR/prev.body" "$BOOTDIR/prev.vbmeta" "$BOOTDIR/prev.footer" || echo "boot: RESTORE FAILED - do not reboot into Linux; reinstall with ./install.sh from a computer" >&2 + prev_modules_restore >&2 return 1 fi sha256sum < "$tmp/new" | cut -d' ' -f1 > "$BOOTDIR/written.sha256" @@ -904,6 +947,7 @@ rollback_boot() { psize=$(( $(cat "/sys/class/block/${dev##*/}/size") * 512 )) write_boot "$dev" "$psize" "$BOOTDIR/prev.body" "$BOOTDIR/prev.vbmeta" "$BOOTDIR/prev.footer" || die "writing the previous boot image failed verification" + prev_modules_restore || die "the previous boot image is back but its modules are not: run mu300-update kernel \"$(cat "$BOOTDIR/prev.kernel" 2>/dev/null)\" before rebooting" sha256sum < "$BOOTDIR/prev.body" | cut -d' ' -f1 > "$BOOTDIR/written.sha256" rm -f "$BOOTDIR/installed.id" cat "$BOOTDIR/prev.tag" > "$BOOTDIR/installed.tag" 2>/dev/null diff --git a/tests/test_update.py b/tests/test_update.py index a369cde7..dc47906c 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -559,6 +559,59 @@ class FromStock(UpdateBase): for shell in self.each_shell(): self.assertNotEqual(self.up(shell, f'kernel_modules_into_systems "{b}"', MU300_NO_DEPMOD=1).returncode, 0) + def test_rollback_boot_brings_the_modules_of_its_image_back(self): + # two builds of one kernel release share lib/modules/: the set in place is kept with the image it + # runs with, and goes back with it (seen on F50-B: rollback-boot to a 6.18.55 image with the other + # release's 6.18.55 modules did not come up) + b = self.tmp / 'bundle' + (b / 'modules').mkdir(parents=True) + (b / 'modules' / 'a.ko').write_bytes(b'new') + (b / 'modules' / 'b.ko').write_bytes(b'new') + (b / 'kernel.release').write_text('6.18.55\n') + (self.disk / 'openwrt').mkdir() + dirs = ('ubuntu/lib/modules/6.18.55/extra', 'openwrt/lib/modules/6.18.55') + for shell in self.each_shell(): + shutil.rmtree(self.disk / 'boot', ignore_errors=True) + for d in dirs: + shutil.rmtree(self.disk / d, ignore_errors=True) + (self.disk / d).mkdir(parents=True) + (self.disk / d / 'a.ko').write_bytes(b'old') + r = self.up(shell, f'BOOTDIR="{self.disk}/boot"; mkdir -p "$BOOTDIR"; prev_modules_save "{b}" && ' + f'kernel_modules_into_systems "{b}" >/dev/null', MU300_NO_DEPMOD=1) + self.assertEqual(r.returncode, 0, r.stderr) + for d in dirs: + self.assertEqual(sorted(f.name for f in (self.disk / d).glob('*.ko')), ['a.ko', 'b.ko']) + r = self.up(shell, f'BOOTDIR="{self.disk}/boot"; prev_modules_restore', MU300_NO_DEPMOD=1) + self.assertEqual(r.returncode, 0, r.stderr) + self.assertIn('modules of the previous image (6.18.55) are back', r.stdout) + for d in dirs: + self.assertEqual({f.name: f.read_bytes() for f in (self.disk / d).glob('*.ko')}, {'a.ko': b'old'}) + # a system that had no set of this release then has none after the rollback + shutil.rmtree(self.disk / dirs[1]) + r = self.up(shell, f'BOOTDIR="{self.disk}/boot"; prev_modules_save "{b}" && ' + f'kernel_modules_into_systems "{b}" >/dev/null && prev_modules_restore >/dev/null', + MU300_NO_DEPMOD=1) + self.assertEqual(r.returncode, 0, r.stderr) + self.assertEqual(list((self.disk / dirs[1]).glob('*.ko')), []) + # a 5.4 bundle (no kernel.release) leaves the mainline sets alone, and keeps nothing to restore + (b / 'kernel.release').rename(b / 'kr') + r = self.up(shell, f'BOOTDIR="{self.disk}/boot"; prev_modules_save "{b}"; prev_modules_restore; echo done') + (b / 'kr').rename(b / 'kernel.release') + self.assertEqual(r.stdout, 'done\n', r.stderr) + self.assertFalse((self.disk / 'boot/prev.modules').exists()) + + def test_boot_update_keeps_the_modules_before_it_replaces_them(self): + src = (BIN / 'mu300-update').read_text() + bu = src[src.index('boot_update() {'):src.index('\n}\n', src.index('boot_update() {'))] + # the replacing set goes in after the old one is kept, and before the image is written + self.assertLess(bu.index('prev_modules_save "$tmp"'), bu.index('! kernel_modules_into_systems "$tmp"')) + self.assertLess(bu.index('! kernel_modules_into_systems "$tmp"'), bu.index('write_boot "$dev" "$psize" "$tmp/new"')) + # a failed write puts the old modules back with the old image + fail = bu[bu.index('failed verification; restoring'):] + self.assertIn('prev_modules_restore', fail[:fail.index('return 1')]) + rb = src[src.index('rollback_boot() {'):src.index('\n}\n', src.index('rollback_boot() {'))] + self.assertLess(rb.index('write_boot'), rb.index('prev_modules_restore')) + if __name__ == '__main__': unittest.main()