VulpesOS/tundra/devices/sargo/native-root.sh

106 lines
5.7 KiB
Shell

#!/bin/sh
# SPDX-License-Identifier: MPL-2.0
# Called by rescue PID 1 only after the read-only storage check succeeds.
set -eu
BUILD_EPOCH=1795212545
APPLY_NATIVE_CONFIG=1
SYSTEM_IMAGE=/volume/tundra/images/c3017c3decc0824ed4a64124b5f7c47a7ceac5d116419bfd4fecdc4bdffd70b0.ext4
SYSTEM_IMAGE_SHA=c3017c3decc0824ed4a64124b5f7c47a7ceac5d116419bfd4fecdc4bdffd70b0
SYSTEM_IMAGE_BYTES=2684354560
STATE_IMAGE=/volume/tundra/data/54b47c1b-1a3a-47b9-bace-11badc025f82.ext4
STATE_UUID=54b47c1b-1a3a-47b9-bace-11badc025f82
STATE_BYTES=2147483648
# A kernel fallback date must not precede the build; RTC is not modified.
if [ "$(date +%s)" -lt "${BUILD_EPOCH:-0}" ]; then date -s "@$BUILD_EPOCH"; fi
trap 'echo TUNDRA_NATIVE_SETUP_FAILED; while :; do /bin/busybox sleep 5; done' EXIT
: "${SYSTEM_IMAGE:?A bundled system image is required}"
LOWER=/run/tundra-system
if [ -n "${SYSTEM_IMAGE:-}" ]; then
test -f "$SYSTEM_IMAGE" && test ! -L "$SYSTEM_IMAGE"
test "$(stat -c %s "$SYSTEM_IMAGE")" = "$SYSTEM_IMAGE_BYTES"
mkdir -p /run/tundra-system
mount -t ext4 -o loop,ro,noload "$SYSTEM_IMAGE" /run/tundra-system
LOWER=/run/tundra-system
fi
test -f "$LOWER/etc/systemd/system/tundra.target"
UPPER=/run/native-upper
WORK=/run/native-work
if [ -n "${STATE_IMAGE:-}" ]; then
test -f "$STATE_IMAGE" && test ! -L "$STATE_IMAGE"
test "$(stat -c %s "$STATE_IMAGE")" = "$STATE_BYTES"
blkid "$STATE_IMAGE" | grep -F "UUID=\"$STATE_UUID\"" >/dev/null
repaired=0
e2fsck -p "$STATE_IMAGE" || repaired=$?
test "$repaired" -le 1
mkdir -p /run/tundra-state
mount -t ext4 -o loop,rw,nosuid,nodev "$STATE_IMAGE" /run/tundra-state
# A data layer belongs to one immutable system image. Refuse accidental reuse.
test "$(cat /run/tundra-state/system.sha256)" = "$SYSTEM_IMAGE_SHA"
UPPER=/run/tundra-state/upper
WORK=/run/tundra-state/work
fi
mkdir -p "$UPPER" "$WORK" /newroot
chmod 755 "$UPPER"
mount -t overlay overlay -o "lowerdir=$LOWER,upperdir=$UPPER,workdir=$WORK" /newroot
if [ -z "${SYSTEM_IMAGE:-}" ] || [ "${APPLY_NATIVE_CONFIG:-0}" = 1 ]; then
# Only the selected upper layer is edited; the system image stays read-only.
if [ -n "${STATE_IMAGE:-}" ]; then
mkdir -p /run/tundra-identity
for identity in machine-id passwd group shadow gshadow; do
if [ -s "/newroot/etc/$identity" ]; then
cp -p "/newroot/etc/$identity" "/run/tundra-identity/$identity"
fi
done
fi
rm -f /newroot/etc/tundra/hardware-enabled /newroot/etc/systemd/system/tundra.target.wants/ofono.service /newroot/etc/systemd/system/tundra.target.wants/tundra-audio.service /newroot/etc/tundra/network-enabled /newroot/etc/systemd/system/tundra.target.wants/NetworkManager.service /newroot/etc/systemd/system/tundra.target.wants/tundra-wifi.service
tar -xJf /native-config.tar -C /newroot
if [ -d /run/tundra-identity ]; then
for identity in /run/tundra-identity/*; do
[ -f "$identity" ] && cp -p "$identity" /newroot/etc/
done
chmod 644 /newroot/etc/machine-id
fi
fi
if [ -f /shared-ui.tar ]; then tar -xJf /shared-ui.tar -C /newroot; fi
ENGINE=/volume/tundra/engines/p26-161
test -x "$ENGINE/firefox/firefox"
grep -qx 'Milestone=161.0' "$ENGINE/firefox/platform.ini"
mkdir -p /newroot/opt/vulpes/engines/161.0/firefox
mount --bind "$ENGINE/firefox" /newroot/opt/vulpes/engines/161.0/firefox
mount --bind "$ENGINE/engine-lock.json" /newroot/opt/vulpes/engine-lock.json
# A previous successful session must never count as proof of this boot.
for log in test.json home.png session.pid gecko.log server.log compositor.log network-gecko.json wifi-ui.json; do
rm -f "/newroot/opt/vulpes/logs/$log"
done
mkdir -p /newroot/reference /newroot/run /newroot/root/.ssh
if [ -z "${SYSTEM_IMAGE:-}" ]; then
: > /newroot/reference/android-rootfs.img
mount --bind /volume/var/lib/lxc/android/android-rootfs.img /newroot/reference/android-rootfs.img
else
test -s /newroot/reference/android-rootfs.img
fi
mkdir -p /newroot/reference
test -s /volume/tundra/hardware/vendor.img
: > /newroot/reference/vendor.img
mount --bind /volume/tundra/hardware/vendor.img /newroot/reference/vendor.img
mount -o remount,bind,ro /newroot/reference/vendor.img
test -s /volume/tundra/hardware/modem.img
: > /newroot/reference/modem.img
mount --bind /volume/tundra/hardware/modem.img /newroot/reference/modem.img
mount -o remount,bind,ro /newroot/reference/modem.img
mkdir -p /newroot/etc/tundra
printf '%s\n' '{"images":{"vendor.img":{"source":"vendor_a.img","sha256":"d584429873c88db741b81144b55977bbd8126b3e877445901bee6a0bda7fe4d0","bytes":805306368},"modem.img":{"source":"modem_a.img","sha256":"fd0624b37ac1d80c064a9763c7e2d2d6f94c3fd5e4617965b735ad411a473d0a","bytes":115343360}}}' > /newroot/etc/tundra/hardware-images.json
mkdir -p /newroot/usr/libexec /newroot/etc/systemd/system/tundra.target.wants
mkdir -p /newroot/usr/share/doc/qbootctl
cp /qbootctl-copyright /newroot/usr/share/doc/qbootctl/copyright
cp /sbin/tundra-qbootctl /newroot/usr/libexec/tundra-qbootctl
cp /standalone-boot-success.py /newroot/usr/libexec/tundra-mark-boot-success.py
cp /standalone-boot-success.service /newroot/etc/systemd/system/tundra-boot-success.service
ln -sf ../tundra-boot-success.service /newroot/etc/systemd/system/tundra.target.wants/tundra-boot-success.service
# Installed lower layers stay reachable for the overlay/loop driver until reboot.
mkdir -p /newroot/reference/tundra-volume
mount --move /volume /newroot/reference/tundra-volume
# Public boot: no USB maintenance account or shared SSH key.
for fs in dev proc sys run; do mount --move "/$fs" "/newroot/$fs"; done
# A static watchdog survives switch_root without needing the old /bin or /proc.
exec switch_root /newroot /sbin/init --unit=tundra.target