From fcc369ee7f9d224b0b333e0e9c0faf29eb162fe1 Mon Sep 17 00:00:00 2001 From: quatrixone Date: Sat, 7 Nov 2026 23:33:16 +0100 Subject: [PATCH 1/5] initramfs: allow a guarded NFS root for bring-up Let any device test an Arch root over its USB network link before using userdata. When an NFS root is requested, make every UFS partition read-only, including the usual allowlist, and stay in the initramfs shell if the host or root is unavailable. --- initramfs/init | 66 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/initramfs/init b/initramfs/init index f33cbd631..bcf15c356 100755 --- a/initramfs/init +++ b/initramfs/init @@ -162,9 +162,13 @@ ufs_unguarded() { echo "$name${part:+ ($part)}" done } +ufs_ro_start() { + ( while true; do ufs_ro_pass; sleep 0.2 2>/dev/null || sleep 1; done ) & + ufs_guard_pid=$! + ufs_ro_log "guard running (pid $ufs_guard_pid)" +} ufs_ro_pass -( while true; do ufs_ro_pass; sleep 0.2 2>/dev/null || sleep 1; done ) & -ufs_ro_log "guard running (pid $!)" +ufs_ro_start hostname omamobile @@ -425,6 +429,56 @@ grow_root() { rm -f /run/omamobile-resizing } +# Boot the Arch root from NFS instead of the UFS, for bring-up: nothing on the +# phone is written and the stock OS is left alone, so a root filesystem can be +# tried before anyone decides to install it. Enabled only by the presence of +# /etc/omamobile-nfsroot, holding one line, ":" -- no file, no change. +# The USB gadget and its address are already up by the time this runs, and the +# kernel keeps them across the switch, so the mount survives switch_root. +boot_nfs() { + local spec + [ -r /etc/omamobile-nfsroot ] || return 1 + spec=$(cat /etc/omamobile-nfsroot) + [ -n "$spec" ] || return 1 + say "nfsroot: trying $spec" + mkdir -p /newroot + # busybox mount cannot work out the server address on its own here, so pass it. + # shortcut: IPv4 hosts only; add bracketed IPv6 parsing if a bring-up needs it. + # Retry: the phone boots faster than the host brings its usb0 end up, so the + # first mount can race the link coming up. ~30s covers a late host. + local i=0 + until mount -t nfs -o "nolock,vers=3,addr=${spec%%:*}" "$spec" /newroot 2>/dev/null; do + i=$((i + 1)) + [ "$i" -ge 15 ] && { + say "nfsroot: cannot mount $spec (staying in the initramfs)"; return 1; } + sleep 2 + done + if [ ! -x /newroot/usr/lib/systemd/systemd ]; then + say "nfsroot: no systemd on $spec"; umount /newroot; return 1 + fi + # NFS mode must not expose writable UFS partitions, including the boot allowlist. + UFS_RW="" + kill "$ufs_guard_pid" 2>/dev/null || true + wait "$ufs_guard_pid" 2>/dev/null || true + rm -f /run/ufs-ro/* + ufs_ro_pass + ufs_ro_start + local open + open=$(ufs_unguarded) + if [ -n "$open" ]; then + say "nfsroot: UFS remains writable: $(echo "$open" | tr "\n" " " ); staying in the initramfs" + umount /newroot + return 1 + fi + say "nfsroot: switching to Arch Linux on $spec" + kill -9 -1 2>/dev/null + sleep 1 + for m in /dev /proc /sys /run; do + mkdir -p "/newroot$m" && mount --move "$m" "/newroot$m" + done + exec switch_root /newroot /usr/lib/systemd/systemd +} + # Keep a shell on the screen too # Hand over to Arch on userdata, unless omamobile.initramfs is on the command # line. Everything is checked before the irreversible exec; if anything is @@ -466,7 +520,13 @@ boot_arch() { done exec switch_root /newroot /usr/lib/systemd/systemd } -boot_arch +# A requested NFS root must not silently fall through to a writable UFS root. +if [ -s /etc/omamobile-nfsroot ]; then + boot_nfs + say "nfsroot: requested root unavailable (staying in the initramfs)" +else + boot_arch +fi # PID 1 must never exit: if it does the kernel panics and freezes (silently, # since ABL sets console=null). So keep a shell on the screen in a loop From 5952f2256ea22df225906ec5ecf5050968bee228 Mon Sep 17 00:00:00 2001 From: quatrixone Date: Sun, 8 Nov 2026 09:29:09 +0100 Subject: [PATCH 2/5] initramfs: guard all UFS before NFS root --- bin/omamobile | 10 ++++++++++ initramfs/init | 29 +++++++++++++++++++++-------- tests/storage-guard | 16 ++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/bin/omamobile b/bin/omamobile index 16802719c..79f22bb7d 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -625,6 +625,11 @@ cmd_initramfs() { [[ -f $init ]] || die "no init script $init" # Every per-device value is checked before anything is downloaded or built initramfs_config + if [[ -n ${OMAMOBILE_NFSROOT:-} ]]; then + ((release == 0)) || die "OMAMOBILE_NFSROOT is for local bring-up only, not --release" + [[ $OMAMOBILE_NFSROOT =~ ^[A-Za-z0-9.-]+:/[A-Za-z0-9._/-]+$ && $OMAMOBILE_NFSROOT != *..* && $OMAMOBILE_NFSROOT != *//* ]] || + die "OMAMOBILE_NFSROOT must be a host and absolute export path, for example 172.16.42.2:/srv/nfs/dipper" + fi local tarball="alpine-minirootfs-$ver-aarch64.tar.gz" local sum=9bf70a7f18ea44094cbb5f70c58f9af129c8214745743db0e68e5502cc2ce773 mkdir -p "$dl" @@ -673,6 +678,11 @@ cmd_initramfs() { install -m 755 "$init" "$stage/init" initramfs_config "$stage/etc" + if [[ -n ${OMAMOBILE_NFSROOT:-} ]]; then + printf '%s\n' "$OMAMOBILE_NFSROOT" > "$stage/etc/omamobile-nfsroot" + else + rm -f "$stage/etc/omamobile-nfsroot" + fi # Modules the init loads before anything else (INITRAMFS_MODULES, in load # order), from this kernel build's modules_install: the directory of this # build's release, never an older one left in modules-staging diff --git a/initramfs/init b/initramfs/init index bcf15c356..73a4c2a5c 100755 --- a/initramfs/init +++ b/initramfs/init @@ -65,6 +65,10 @@ UFS_RW_LUNS="" ufs_rw_disk() { case " sda $UFS_RW_LUNS " in *" $1 "*) true ;; *) false ;; esac; } ufs_rw_load() { local list p + if [ -s /etc/omamobile-nfsroot ]; then + ufs_ro_log "NFS root requested: every UFS partition read-only" + return 0 + fi if [ ! -s /etc/omamobile-ufs-rw ]; then ufs_ro_log "no allowlist in this image (/etc/omamobile-ufs-rw): every partition read-only" return 0 @@ -121,6 +125,18 @@ ufs_ro_pass() { [ -e "/run/ufs-ro/$name" ] && continue [ -b "/dev/$name" ] || continue # devtmpfs node not there yet: next pass if ufs_rw_disk "$name"; then + if [ -s /etc/omamobile-nfsroot ]; then + blockdev --setro "/dev/$name" + read -r ro < "$s/ro" + if [ "$ro" = 1 ]; then + : > "/run/ufs-ro/$name" + ufs_ro_log "/dev/$name set read-only for NFS root" + continue + fi + ufs_ro_log "FAILED to set /dev/$name read-only for NFS root" + echo "omamobile: WARNING: /dev/$name is NOT read-only" > /dev/tty0 2>/dev/null + continue + fi rm -f "/dev/$name" : > "/run/ufs-ro/$name" ufs_ro_log "/dev/$name: whole-disk node removed (partitions guarded individually)" @@ -152,7 +168,7 @@ ufs_unguarded() { for s in /sys/block/sd* /sys/block/sd*/sd*; do [ -r "$s/ro" ] || continue name=${s##*/} - ufs_rw_disk "$name" && continue + ufs_rw_disk "$name" && [ ! -s /etc/omamobile-nfsroot ] && continue read -r ro < "$s/ro" [ "$ro" = 0 ] || continue part=$(ufs_partname "$s") @@ -437,6 +453,7 @@ grow_root() { # kernel keeps them across the switch, so the mount survives switch_root. boot_nfs() { local spec + grep -q omamobile.initramfs /proc/cmdline && return 1 [ -r /etc/omamobile-nfsroot ] || return 1 spec=$(cat /etc/omamobile-nfsroot) [ -n "$spec" ] || return 1 @@ -456,13 +473,7 @@ boot_nfs() { if [ ! -x /newroot/usr/lib/systemd/systemd ]; then say "nfsroot: no systemd on $spec"; umount /newroot; return 1 fi - # NFS mode must not expose writable UFS partitions, including the boot allowlist. - UFS_RW="" - kill "$ufs_guard_pid" 2>/dev/null || true - wait "$ufs_guard_pid" 2>/dev/null || true - rm -f /run/ufs-ro/* - ufs_ro_pass - ufs_ro_start + # The first guard pass already used an empty allowlist in NFS mode. local open open=$(ufs_unguarded) if [ -n "$open" ]; then @@ -471,6 +482,7 @@ boot_nfs() { return 1 fi say "nfsroot: switching to Arch Linux on $spec" + # kill -9 -1 also kills the feeder; Dipper's systemd takes over at 60 seconds. kill -9 -1 2>/dev/null sleep 1 for m in /dev /proc /sys /run; do @@ -513,6 +525,7 @@ boot_arch() { say "arch: switching to Arch Linux on $dev" # Stop the initramfs helpers (dropbear, getty, guards); the kernel keeps # the USB gadget and the partition read-only flags across the switch + # systemd takes over the hardware watchdog here; Dipper uses RuntimeWatchdogSec=60s. kill -9 -1 2>/dev/null sleep 1 for m in /dev /proc /sys /run; do diff --git a/tests/storage-guard b/tests/storage-guard index 8cce6ad78..a7105c514 100755 --- a/tests/storage-guard +++ b/tests/storage-guard @@ -171,6 +171,22 @@ awk '/^boot_arch\(\) \{/ {on = 1} on && /ufs_unguarded/ {u = NR} on && /kill -9 || fail "boot_arch checks ufs_unguarded before it stops the initramfs's processes" pass "the hand-over check reports a late or reopened partition, and runs before SSH is stopped" +fake_disks +printf '172.16.42.2:/srv/nfs/dipper\n' > "$B/etc/omamobile-nfsroot" +guard +[[ -z $(writable) ]] || fail "NFS root's first pass leaves all UFS partitions read-only" +[[ $(cat "$B/sys/block/sda/ro") == 1 ]] || fail "NFS root marks the main UFS disk read-only" +[[ $(cat "$B/sys/block/sdb/ro") == 1 && $(cat "$B/sys/block/sdb/sdb1/ro") == 1 ]] || fail "NFS root marks boot LUNs read-only" +grep -q 'NFS root requested: every UFS partition read-only' "$B/dev/kmsg" || fail "NFS mode is selected before first guard pass" +[[ -z $(unguarded) ]] || fail "NFS root handover has no writable UFS device" +pass "NFS-root mode protects the main disk and every partition on the first guard pass" + +awk '/^boot_nfs\(\) \{/ {on = 1} on && /omamobile.initramfs/ {marker = NR} on && /switch_root/ {handover = NR} on && /^}/ {exit} END {exit !(marker && handover && marker < handover)}' initramfs/init \ + || fail "boot_nfs honors omamobile.initramfs before switching roots" +awk '/^if \[ -s \/etc\/omamobile-nfsroot \]; then/ {on = 1} on && /^else$/ {exit} on && /boot_nfs/ {nfs = 1} on && /boot_arch/ {arch = 1} END {exit !(nfs && !arch)}' initramfs/init \ + || fail "a failed requested NFS mount cannot fall through to boot_arch" +pass "NFS cmdline opt-out works and a failed mount stays in the initramfs" + fake_disks cp "$t/etc-oriole/omamobile-ufs-rw" "$B/etc/omamobile-ufs-rw" guard From 392ed9e935e907cd22f7078ab79450b67a968ff9 Mon Sep 17 00:00:00 2001 From: quatrixone Date: Mon, 9 Nov 2026 17:54:22 +0100 Subject: [PATCH 3/5] initramfs: test NFS root bypasses disk allowlist --- initramfs/init | 4 ++-- tests/storage-guard | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/initramfs/init b/initramfs/init index 73a4c2a5c..2c1ce6967 100755 --- a/initramfs/init +++ b/initramfs/init @@ -482,7 +482,7 @@ boot_nfs() { return 1 fi say "nfsroot: switching to Arch Linux on $spec" - # kill -9 -1 also kills the feeder; Dipper's systemd takes over at 60 seconds. + # kill -9 -1 also stops the feeder; systemd takes over if the new root enables its watchdog. kill -9 -1 2>/dev/null sleep 1 for m in /dev /proc /sys /run; do @@ -525,7 +525,7 @@ boot_arch() { say "arch: switching to Arch Linux on $dev" # Stop the initramfs helpers (dropbear, getty, guards); the kernel keeps # the USB gadget and the partition read-only flags across the switch - # systemd takes over the hardware watchdog here; Dipper uses RuntimeWatchdogSec=60s. + # systemd takes over the hardware watchdog here if the new root enables it. kill -9 -1 2>/dev/null sleep 1 for m in /dev /proc /sys /run; do diff --git a/tests/storage-guard b/tests/storage-guard index a7105c514..de352419f 100755 --- a/tests/storage-guard +++ b/tests/storage-guard @@ -172,9 +172,11 @@ awk '/^boot_arch\(\) \{/ {on = 1} on && /ufs_unguarded/ {u = NR} on && /kill -9 pass "the hand-over check reports a late or reopened partition, and runs before SSH is stopped" fake_disks +cp "$t/etc-cheetah/omamobile-ufs-rw" "$B/etc/omamobile-ufs-rw" printf '172.16.42.2:/srv/nfs/dipper\n' > "$B/etc/omamobile-nfsroot" guard [[ -z $(writable) ]] || fail "NFS root's first pass leaves all UFS partitions read-only" +! grep -q '^allowlist: ' "$B/dev/kmsg" || fail "NFS mode skips the packed allowlist" [[ $(cat "$B/sys/block/sda/ro") == 1 ]] || fail "NFS root marks the main UFS disk read-only" [[ $(cat "$B/sys/block/sdb/ro") == 1 && $(cat "$B/sys/block/sdb/sdb1/ro") == 1 ]] || fail "NFS root marks boot LUNs read-only" grep -q 'NFS root requested: every UFS partition read-only' "$B/dev/kmsg" || fail "NFS mode is selected before first guard pass" From ad4ceb169733fb322130fd5cb0cfdcdccb17334e Mon Sep 17 00:00:00 2001 From: quatrixone Date: Mon, 9 Nov 2026 22:47:57 +0100 Subject: [PATCH 4/5] initramfs: match NFS allowlist log prefix --- tests/storage-guard | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/storage-guard b/tests/storage-guard index de352419f..2dd22297d 100755 --- a/tests/storage-guard +++ b/tests/storage-guard @@ -176,7 +176,7 @@ cp "$t/etc-cheetah/omamobile-ufs-rw" "$B/etc/omamobile-ufs-rw" printf '172.16.42.2:/srv/nfs/dipper\n' > "$B/etc/omamobile-nfsroot" guard [[ -z $(writable) ]] || fail "NFS root's first pass leaves all UFS partitions read-only" -! grep -q '^allowlist: ' "$B/dev/kmsg" || fail "NFS mode skips the packed allowlist" +! grep -q 'ufs-ro: allowlist: ' "$B/dev/kmsg" || fail "NFS mode skips the packed allowlist" [[ $(cat "$B/sys/block/sda/ro") == 1 ]] || fail "NFS root marks the main UFS disk read-only" [[ $(cat "$B/sys/block/sdb/ro") == 1 && $(cat "$B/sys/block/sdb/sdb1/ro") == 1 ]] || fail "NFS root marks boot LUNs read-only" grep -q 'NFS root requested: every UFS partition read-only' "$B/dev/kmsg" || fail "NFS mode is selected before first guard pass" From 8b32e2ea92c0366c8d921a8f77ccad6b9a33770d Mon Sep 17 00:00:00 2001 From: quatrixone Date: Tue, 10 Nov 2026 08:59:27 +0100 Subject: [PATCH 5/5] initramfs: use BusyBox mount for NFS root --- initramfs/init | 2 +- tests/storage-guard | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/initramfs/init b/initramfs/init index 2c1ce6967..91c1d2c67 100755 --- a/initramfs/init +++ b/initramfs/init @@ -464,7 +464,7 @@ boot_nfs() { # Retry: the phone boots faster than the host brings its usb0 end up, so the # first mount can race the link coming up. ~30s covers a late host. local i=0 - until mount -t nfs -o "nolock,vers=3,addr=${spec%%:*}" "$spec" /newroot 2>/dev/null; do + until busybox mount -t nfs -o "nolock,vers=3,addr=${spec%%:*}" "$spec" /newroot 2>/dev/null; do i=$((i + 1)) [ "$i" -ge 15 ] && { say "nfsroot: cannot mount $spec (staying in the initramfs)"; return 1; } diff --git a/tests/storage-guard b/tests/storage-guard index 2dd22297d..9602ef7e5 100755 --- a/tests/storage-guard +++ b/tests/storage-guard @@ -185,6 +185,7 @@ pass "NFS-root mode protects the main disk and every partition on the first guar awk '/^boot_nfs\(\) \{/ {on = 1} on && /omamobile.initramfs/ {marker = NR} on && /switch_root/ {handover = NR} on && /^}/ {exit} END {exit !(marker && handover && marker < handover)}' initramfs/init \ || fail "boot_nfs honors omamobile.initramfs before switching roots" +grep -q 'busybox mount -t nfs' initramfs/init || fail "NFS root uses BusyBox mount on minimal initramfs images" awk '/^if \[ -s \/etc\/omamobile-nfsroot \]; then/ {on = 1} on && /^else$/ {exit} on && /boot_nfs/ {nfs = 1} on && /boot_arch/ {arch = 1} END {exit !(nfs && !arch)}' initramfs/init \ || fail "a failed requested NFS mount cannot fall through to boot_arch" pass "NFS cmdline opt-out works and a failed mount stays in the initramfs"