#!/bin/sh # License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. # # Update the system on a running phone without touching /data. # # The tree of tools/mkroottar.sh is unpacked next to the running system and # switched in: /system holds two trees, A/B like the boot slots -- # /system/current the system the next boot uses # /system/previous the one before the last update: the way back # The running system keeps running from its tree (a bind mount, # base/initramfs/init); the next boot uses the new one. /data -- /home, # /root, /var/lib and the upper directory of the /etc overlay -- is not # touched, so WLAN networks, ssh host keys, bluetooth pairings and root's # files survive (docs/NEXT.md, task B). No version in a directory name: the # version is a file in the tree, /etc/makronix-release ("Makronix 0.99.9 # Camouflage (unstable)", tools/lib.sh makronix_version). # # The way back after a bad update: # phone_ssh "$a" 'mv /system/current /system/broken && # mv /system/previous /system/current && reboot' # # Usage: update-root.sh DEVICEDIR TREE.tar [--reboot] # # The phone must run an image with the /system/current layout (one from # tools/mkrootimg.sh since 2026-11-09); an old image is refused, it has # nowhere to put the new tree without overwriting itself. set -eu . "$(dirname "$0")/lib.sh" [ $# -ge 2 ] || { echo "usage: $0 DEVICEDIR TREE.tar [--reboot]" >&2; exit 2; } load_device "$1" tree=$2 reboot=${3:-} [ -f "$tree" ] || die "no tree tar $tree (make roottar)" [ -z "$reboot" ] || [ "$reboot" = --reboot ] || die "unknown argument $reboot" a=$(phone_addr) || die "phone not reachable (tools/lib.sh: phone_addr)" q() { phone_ssh "$a" "$*"; } q 'test -d /system/current' || die "no /system/current on the phone: an image from before the new layout; flash a root image first (make flash-root)" q 'test ! -e /system/.staging' || die "/system/.staging exists: a failed update; look at it, remove it, run again" # room for the tree and a little more, in 1 KiB blocks; the old previous is # only removed once the new tree is in, so both have to fit size_kb=$(( ($(wc -c < "$tree") + 1023) / 1024 )) free_kb=$(q "df -Pk /system | awk 'NR == 2 { print \$4 }'") [ "$free_kb" -ge $(( size_kb + size_kb / 10 )) ] || die "/system has ${free_kb} KiB free, the tree needs about ${size_kb} KiB" echo "updating $DEVICE_NAME at $a: $tree -> /system/current (${size_kb} KiB)" q "mkdir /system/.staging" || die "cannot create /system/.staging" # the tree is a tar already (tools/mkroottar.sh): stream it as it is phone_ssh "$a" "tar -C /system/.staging -xpf -" < "$tree" || die "unpacking failed; /system/.staging left for inspection" # a tree that cannot boot is not switched to q "test -x /system/.staging/sbin/init || test -L /system/.staging/sbin/init" || die "no /sbin/init in the new tree" q "test -f /system/.staging/etc/shadow" || die "no /etc/shadow in the new tree" q "test -d /system/.staging/usr/lib/modules/\$(ls /system/.staging/usr/lib/modules | head -n 1)" || die "no kernel modules in the new tree" # the tree names its version (tools/assemble-rootfs.sh) release=$(q 'cat /system/.staging/etc/makronix-release' || true) [ -n "$release" ] || die "no /etc/makronix-release in the new tree (an old make roottar?)" q "sync" # the switch: the running system is a bind mount of current, so the renames # do not pull the ground out from under it; the old previous goes first q "rm -rf /system/previous" || die "cannot remove the old /system/previous" q "mv /system/current /system/previous" || die "cannot rename /system/current" if ! q "mv /system/.staging /system/current"; then q "mv /system/previous /system/current" || true die "switch failed; /system/current set back" fi q "sync" echo "switched: /system/current is $release, /system/previous the old tree" echo "the phone still runs the old tree; 'reboot' or 'reboot-mode bootloader' starts the new one" [ "$reboot" != --reboot ] || { echo "rebooting"; q 'reboot' || true; }