Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions bin/omamobile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
81 changes: 77 additions & 4 deletions initramfs/init
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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")
Expand All @@ -162,9 +178,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

Expand Down Expand Up @@ -425,6 +445,52 @@ 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, "<host>:<path>" -- 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
grep -q omamobile.initramfs /proc/cmdline && return 1
[ -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 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; }
sleep 2
done
if [ ! -x /newroot/usr/lib/systemd/systemd ]; then
say "nfsroot: no systemd on $spec"; umount /newroot; return 1
fi
# The first guard pass already used an empty allowlist in NFS mode.
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 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
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
Expand Down Expand Up @@ -459,14 +525,21 @@ 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 if the new root enables it.
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
}
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
Expand Down
19 changes: 19 additions & 0 deletions tests/storage-guard
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ 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
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 '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"
[[ -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"
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"

fake_disks
cp "$t/etc-oriole/omamobile-ufs-rw" "$B/etc/omamobile-ufs-rw"
guard
Expand Down