Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

feat: migrate to real Alpine root with OpenRC as PID 1

ExTv73bfcdc6e026
diff --git a/.gitignore b/.gitignore
index 4502887..b8c95c0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,10 +33,14 @@ config.log
# Generated assets (built by docker-build-initramfs.sh)
app/src/main/assets/vmlinuz-virt
app/src/main/assets/initrd.img
+app/src/main/assets/alpine-rootfs.squashfs
app/src/main/jniLibs/arm64-v8a/libqemu-system-aarch64.so
app/src/main/jniLibs/arm64-v8a/libslirp.so
app/src/main/jniLibs/arm64-v8a/libpodroid-bridge.so
+# Local planning / scratch dirs
+docs/superpowers/
+
# Claude Code sessions
.claude/
diff --git a/Dockerfile b/Dockerfile
index a0538c9..38d1c14 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -68,6 +68,8 @@ RUN printf '%s\n' \
'CONFIG_NETFILTER_XT_TARGET_MARK=y' \
'CONFIG_OVERLAY_FS=y' \
'CONFIG_FUSE_FS=y' \
+ 'CONFIG_EXT4_FS_SECURITY=y' \
+ 'CONFIG_SQUASHFS_XATTR=y' \
> /tmp/forced_builtin.config
RUN cd linux-${KERNEL_VERSION} \
&& make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig \
@@ -80,7 +82,8 @@ RUN cd linux-${KERNEL_VERSION} \
IP_NF_TARGET_MASQUERADE IP6_NF_TARGET_MASQUERADE \
NETFILTER_XT_TARGET_MASQUERADE NF_NAT_MASQUERADE \
NFT_COMPAT NFT_MASQ NFT_NAT \
- VETH TUN NF_TABLES NF_NAT NETFILTER OVERLAY_FS FUSE_FS; do \
+ VETH TUN NF_TABLES NF_NAT NETFILTER OVERLAY_FS FUSE_FS \
+ EXT4_FS_SECURITY SQUASHFS_XATTR; do \
grep -q "^CONFIG_${opt}=y\$" .config \
|| { echo "FATAL: CONFIG_${opt} is not =y after merge" >&2; \
grep "CONFIG_${opt}" .config >&2; exit 1; }; \
diff --git a/app/src/main/java/com/excp/podroid/PodroidApplication.kt b/app/src/main/java/com/excp/podroid/PodroidApplication.kt
index 93e5f00..4a5cf02 100644
--- a/app/src/main/java/com/excp/podroid/PodroidApplication.kt
+++ b/app/src/main/java/com/excp/podroid/PodroidApplication.kt
@@ -24,6 +24,7 @@ class PodroidApplication : Application() {
copyAssetDir("qemu", filesDir)
copyAssetIfNeeded("vmlinuz-virt", filesDir)
copyAssetIfNeeded("initrd.img", filesDir)
+ copyAssetIfNeeded("alpine-rootfs.squashfs", filesDir)
}
/**
diff --git a/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt b/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
index 2d9b997..58df3a1 100644
--- a/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
+++ b/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
@@ -520,6 +520,12 @@ class PodroidQemu @Inject constructor(
args += "-drive"; args += "file=${storagePath.absolutePath},if=none,id=drive1,format=raw,cache=writeback,aio=threads"
}
+ val rootfsImg = File(context.filesDir, "alpine-rootfs.squashfs")
+ if (rootfsImg.exists()) {
+ args += "-device"; args += "virtio-blk-pci,drive=drive2,num-queues=${config.cpus}"
+ args += "-drive"; args += "file=${rootfsImg.absolutePath},if=none,id=drive2,format=raw,readonly=on,cache=writeback,aio=threads"
+ }
+
// Downloads folder sharing via virtio-9p
val downloadsDir = android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS
diff --git a/build-all.sh b/build-all.sh
index 624c2b9..b9c35d8 100755
--- a/build-all.sh
+++ b/build-all.sh
@@ -29,9 +29,10 @@ Podroid Unified Build Tool
Usage: $0 [command] [options]
Commands:
- all Build everything (Kernel, Initramfs, QEMU, Termux JNI, APK)
+ all Build everything (Kernel, Initramfs, Rootfs, QEMU, Termux JNI, APK)
kernel Build custom kernel only (podroid_kernel.config + Linux source)
initramfs Build custom kernel + Alpine VM initramfs (vmlinuz + initrd)
+ rootfs Build Alpine rootfs squashfs (alpine-rootfs.squashfs)
qemu Build QEMU + podroid-bridge
termux Build libtermux.so (16KB page aligned)
apk Build the Android APK
@@ -118,6 +119,15 @@ build_initramfs() {
success "Kernel + initramfs ready."
}
+build_rootfs() {
+ log "Building Alpine rootfs squashfs..."
+ docker build -f "${SCRIPT_DIR}/build-rootfs/Dockerfile.rootfs" \
+ -t podroid-rootfs:latest \
+ --output type=local,dest="${ASSETS}" \
+ "${SCRIPT_DIR}/build-rootfs/"
+ success "Built ${ASSETS}/alpine-rootfs.squashfs ($(du -h "${ASSETS}/alpine-rootfs.squashfs" | cut -f1))"
+}
+
build_qemu() {
local qemu_ver
qemu_ver=$(grep -E '^podroidQemuVersion=' "${SCRIPT_DIR}/gradle.properties" | cut -d= -f2)
@@ -255,6 +265,7 @@ for arg in "$@"; do [ "$arg" == "--fast" ] && FAST=true; done
case "$1" in
kernel) build_kernel ;;
initramfs) build_initramfs ;;
+ rootfs) build_rootfs ;;
qemu) build_qemu ;;
termux) build_termux ;;
apk) build_apk ;;
@@ -262,6 +273,7 @@ case "$1" in
test) run_boot_test ;;
all)
build_initramfs
+ build_rootfs
build_qemu
build_termux
build_apk
@@ -269,7 +281,7 @@ case "$1" in
clean)
log "Cleaning up..."
./gradlew clean
- docker rmi podroid-builder podroid-qemu-builder 2>/dev/null || true
+ docker rmi podroid-builder podroid-qemu-builder podroid-rootfs:latest 2>/dev/null || true
success "Cleaned."
;;
*)
diff --git a/build-rootfs/Dockerfile.rootfs b/build-rootfs/Dockerfile.rootfs
new file mode 100644
index 0000000..243cf46
--- /dev/null
+++ b/build-rootfs/Dockerfile.rootfs
@@ -0,0 +1,27 @@
+# build-rootfs/Dockerfile.rootfs
+FROM alpine:3.23 AS builder
+
+ARG ALPINE_RELEASE=3.23.4
+ARG ALPINE_ARCH=aarch64
+
+RUN apk add --no-cache curl squashfs-tools tar openssl libcap-utils
+
+WORKDIR /work
+RUN curl -L -o minirootfs.tar.gz \
+ "https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_RELEASE%.*}/releases/${ALPINE_ARCH}/alpine-minirootfs-${ALPINE_RELEASE}-${ALPINE_ARCH}.tar.gz" \
+ && mkdir rootfs \
+ && tar -xzf minirootfs.tar.gz -C rootfs \
+ && rm minirootfs.tar.gz
+
+ENV ALPINE_VERSION=${ALPINE_RELEASE}
+COPY files /work/files
+COPY build-rootfs.sh /work/build-rootfs.sh
+RUN chmod +x /work/build-rootfs.sh && /work/build-rootfs.sh
+
+RUN mksquashfs /work/rootfs /work/alpine-rootfs.squashfs \
+ -comp gzip -all-root -noappend
+# (No -no-xattrs: rootless podman's newuidmap/newgidmap need their
+# file capabilities preserved; they're stored as security.capability xattr.)
+
+FROM scratch AS export
+COPY --from=builder /work/alpine-rootfs.squashfs /alpine-rootfs.squashfs
diff --git a/build-rootfs/build-rootfs.sh b/build-rootfs/build-rootfs.sh
new file mode 100755
index 0000000..cd05ea9
--- /dev/null
+++ b/build-rootfs/build-rootfs.sh
@@ -0,0 +1,125 @@
+#!/bin/sh
+set -eu
+ROOTFS=/work/rootfs
+
+# ALPINE_VERSION comes from the Dockerfile ENV (full release like 3.23.4).
+# Strip the patch component to get the major branch (e.g. 3.23) used in repo URLs.
+: "${ALPINE_VERSION:?ALPINE_VERSION must be set (e.g. 3.23.4)}"
+ALPINE_BRANCH="${ALPINE_VERSION%.*}"
+
+mkdir -p "$ROOTFS/etc/apk"
+cat > "$ROOTFS/etc/apk/repositories" <<EOF
+https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_BRANCH}/main
+https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_BRANCH}/community
+EOF
+
+apk -X "https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_BRANCH}/main" \
+ -X "https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_BRANCH}/community" \
+ -U --allow-untrusted --root "$ROOTFS" --initdb add \
+ alpine-base \
+ openrc \
+ busybox-openrc \
+ bash \
+ podman \
+ crun \
+ fuse-overlayfs \
+ iptables \
+ ip6tables \
+ nftables \
+ bridge-utils \
+ iproute2 \
+ dropbear dropbear-openrc \
+ curl \
+ ca-certificates \
+ shadow shadow-uidmap \
+ slirp4netns \
+ aardvark-dns netavark \
+ libcap-utils \
+ doas sudo
+
+# Apply file capabilities to newuidmap/newgidmap. apk's package install often
+# does this, but we set them explicitly so the squashfs ships with the
+# correct security.capability xattr (preserved by mksquashfs without -no-xattrs).
+if command -v setcap >/dev/null 2>&1; then
+ setcap cap_setuid+ep "$ROOTFS/usr/bin/newuidmap" 2>/dev/null || true
+ setcap cap_setgid+ep "$ROOTFS/usr/bin/newgidmap" 2>/dev/null || true
+fi
+
+# Ensure doas and sudo are setuid-root. apk usually does this, but on
+# overlay-mounted build hosts it can silently fail.
+chmod u+s "$ROOTFS/usr/bin/doas" 2>/dev/null || true
+chmod u+s "$ROOTFS/usr/bin/sudo" 2>/dev/null || true
+
+# doas: members of the `wheel` group can become root after entering their
+# password (cached for ~5 min). Standard *BSD/Alpine convention.
+mkdir -p "$ROOTFS/etc/doas.d"
+echo "permit persist :wheel" > "$ROOTFS/etc/doas.d/doas.conf"
+chmod 0400 "$ROOTFS/etc/doas.d/doas.conf"
+
+# sudo: equivalent rule for users who prefer sudo over doas.
+mkdir -p "$ROOTFS/etc/sudoers.d"
+echo "%wheel ALL=(ALL) ALL" > "$ROOTFS/etc/sudoers.d/wheel"
+chmod 0440 "$ROOTFS/etc/sudoers.d/wheel"
+
+# Set root password to "podroid" (pre-hashed with openssl).
+# We can't run chpasswd inside the aarch64 rootfs from an x86_64 host,
+# so write the SHA-512 hash directly into /etc/shadow.
+ROOT_HASH=$(openssl passwd -6 -salt podroid podroid)
+sed -i "s|^root:[^:]*:|root:${ROOT_HASH}:|" "$ROOTFS/etc/shadow"
+
+# Strip docs/man/locale to shrink squashfs
+rm -rf "$ROOTFS/usr/share/man" "$ROOTFS/usr/share/doc" \
+ "$ROOTFS/usr/share/locale" "$ROOTFS/usr/share/info"
+
+# Pre-create podman storage dirs (saves first-boot mkdir)
+mkdir -p "$ROOTFS/var/lib/containers/storage" \
+ "$ROOTFS/run/containers/storage" \
+ "$ROOTFS/run/libpod" \
+ "$ROOTFS/run/crun"
+
+# Copy custom service files into the rootfs
+cp /work/files/etc/init.d/podroid-bootstrap "$ROOTFS/etc/init.d/"
+cp /work/files/etc/init.d/podroid-network "$ROOTFS/etc/init.d/"
+cp /work/files/etc/init.d/podroid-resize "$ROOTFS/etc/init.d/"
+cp /work/files/etc/init.d/podroid-ready "$ROOTFS/etc/init.d/"
+chmod +x "$ROOTFS/etc/init.d/podroid-"*
+
+# Copy /usr/local/bin scripts (resize daemon + login wrapper)
+mkdir -p "$ROOTFS/usr/local/bin"
+cp /work/files/usr/local/bin/podroid-resize "$ROOTFS/usr/local/bin/"
+cp /work/files/usr/local/bin/podroid-login "$ROOTFS/usr/local/bin/"
+chmod +x "$ROOTFS/usr/local/bin/podroid-"*
+mkdir -p "$ROOTFS/etc/conf.d"
+cp /work/files/etc/conf.d/podroid "$ROOTFS/etc/conf.d/"
+cp /work/files/etc/inittab "$ROOTFS/etc/inittab"
+cp /work/files/etc/rc.conf "$ROOTFS/etc/rc.conf"
+
+# Hostname (read by podroid-bootstrap via `hostname -F /etc/hostname`)
+echo "podroid" > "$ROOTFS/etc/hostname"
+echo "127.0.0.1 localhost podroid" > "$ROOTFS/etc/hosts"
+echo "::1 localhost ip6-localhost" >> "$ROOTFS/etc/hosts"
+
+# Login banner shown by getty before the login prompt.
+# \S=Alpine release, \r=kernel, \m=arch, \l=tty
+cat > "$ROOTFS/etc/issue" <<'EOF'
+Welcome to Podroid (Alpine \S)
+Kernel \r on \m (\l)
+
+ Default login: root / podroid
+ Change root password: passwd
+ Create a regular user: adduser -G wheel <name>
+ (wheel group → can run doas/sudo)
+
+EOF
+
+# Set runlevels via direct symlinks (host is x86_64, can't chroot into aarch64 rootfs to run rc-update).
+# rc-update is just `ln -s /etc/init.d/X /etc/runlevels/<level>/X` under the hood.
+mkdir -p "$ROOTFS/etc/runlevels/default" "$ROOTFS/etc/runlevels/boot"
+for svc in podroid-bootstrap podroid-network podroid-resize dropbear podroid-ready; do
+ ln -sf "/etc/init.d/$svc" "$ROOTFS/etc/runlevels/default/$svc"
+done
+
+# Disable services we don't need (initramfs already handles them, or they're noise in the VM)
+for svc in hwclock swclock urandom networking sysctl bootmisc syslog; do
+ rm -f "$ROOTFS/etc/runlevels/boot/$svc" "$ROOTFS/etc/runlevels/default/$svc"
+done
diff --git a/build-rootfs/files/etc/conf.d/podroid b/build-rootfs/files/etc/conf.d/podroid
new file mode 100644
index 0000000..d5a83ba
--- /dev/null
+++ b/build-rootfs/files/etc/conf.d/podroid
@@ -0,0 +1,2 @@
+# Podroid runtime configuration (sourced by services)
+# Reserved for future tuning options.
diff --git a/build-rootfs/files/etc/init.d/podroid-bootstrap b/build-rootfs/files/etc/init.d/podroid-bootstrap
new file mode 100644
index 0000000..fe9ee0a
--- /dev/null
+++ b/build-rootfs/files/etc/init.d/podroid-bootstrap
@@ -0,0 +1,62 @@
+#!/sbin/openrc-run
+description="Podroid VM system bootstrap (cgroups, modules, devpts, sysctl)"
+
+depend() {
+ need localmount
+ before podroid-network
+}
+
+start() {
+ ebegin "Loading kernel modules..."
+ echo "Loading kernel modules..." > /dev/console
+
+ # Rootless podman needs / to be a shared mount so user namespaces can
+ # propagate mount events. Without this, every rootless `podman run`
+ # warns and may fail to mount /sys-as-fuse-overlayfs inside the userns.
+ mount --make-rshared / 2>/dev/null
+
+ # /dev sub-mounts (we replaced devfs OpenRC service with the initramfs's
+ # devtmpfs + these explicit mounts — keeps the boot path tight)
+ mkdir -p /dev/pts /dev/shm /dev/mqueue 2>/dev/null
+ mountpoint -q /dev/pts || mount -t devpts devpts /dev/pts \
+ -o gid=5,mode=0620,ptmxmode=0666,noexec,nosuid
+ mountpoint -q /dev/shm || mount -t tmpfs tmpfs /dev/shm \
+ -o noexec,nosuid,nodev,size=64m
+ mountpoint -q /dev/mqueue || mount -t mqueue mqueue /dev/mqueue \
+ -o noexec,nosuid,nodev
+ [ -c /dev/ptmx ] || mknod /dev/ptmx c 5 2 2>/dev/null
+ chmod 0666 /dev/ptmx /dev/pts/ptmx 2>/dev/null
+
+ # Hostname (Alpine's hostname service is disabled in our runlevel)
+ [ -r /etc/hostname ] && hostname -F /etc/hostname 2>/dev/null
+
+ depmod -a 2>/dev/null
+ for m in 9p 9pnet 9pnet_virtio; do modprobe "$m" 2>/dev/null; done
+ mkdir -p /dev/net 2>/dev/null
+ [ -c /dev/net/tun ] || mknod /dev/net/tun c 10 200 2>/dev/null
+ [ -c /dev/fuse ] || mknod /dev/fuse c 10 229 2>/dev/null
+ # Rootless containers need /dev/net/tun (pasta/slirp4netns) and /dev/fuse
+ # (fuse-overlayfs) accessible from non-root users.
+ chmod 0666 /dev/net/tun /dev/fuse 2>/dev/null
+
+ # cgroup v2 subtree controllers
+ mkdir -p /sys/fs/cgroup 2>/dev/null
+ mountpoint -q /sys/fs/cgroup || mount -t cgroup2 cgroup2 /sys/fs/cgroup
+ printf '+cpuset +cpu +io +memory +hugetlb +pids +rdma\n' \
+ > /sys/fs/cgroup/cgroup.subtree_control 2>/dev/null
+
+ # ZRAM swap (half RAM, lz4)
+ if [ -b /dev/zram0 ]; then
+ _mem_kb=$(awk '/^MemTotal:/{print $2}' /proc/meminfo)
+ echo lz4 > /sys/block/zram0/comp_algorithm 2>/dev/null
+ echo $((_mem_kb * 512)) > /sys/block/zram0/disksize 2>/dev/null
+ mkswap /dev/zram0 >/dev/null 2>&1 && swapon -p 100 /dev/zram0 2>/dev/null
+ fi
+
+ # sysctl for container networking
+ sysctl -qw net.ipv4.ip_forward=1 \
+ net.ipv4.conf.all.forwarding=1 \
+ net.bridge.bridge-nf-call-iptables=1 2>/dev/null
+
+ eend 0
+}
diff --git a/build-rootfs/files/etc/init.d/podroid-network b/build-rootfs/files/etc/init.d/podroid-network
new file mode 100644
index 0000000..991c41a
--- /dev/null
+++ b/build-rootfs/files/etc/init.d/podroid-network
@@ -0,0 +1,33 @@
+#!/sbin/openrc-run
+description="Podroid VM networking (10.0.2.15 via SLIRP)"
+
+depend() {
+ need podroid-bootstrap
+ before podroid-ready dropbear
+ provide net
+}
+
+start() {
+ ebegin "Configuring network..."
+ echo "Configuring containers..." > /dev/console
+ ip link set lo up 2>/dev/null
+ NETIF=""
+ for _i in $(seq 1 150); do
+ NETIF=$(ip -o link show 2>/dev/null | awk -F': ' '{print $2}' | \
+ grep -vE '^(lo|dummy[0-9]*|veth|podman|cni-|docker)' | head -1)
+ [ -n "$NETIF" ] && break
+ sleep 0.2
+ done
+ if [ -n "$NETIF" ]; then
+ ip link set "$NETIF" up
+ ip addr add 10.0.2.15/24 dev "$NETIF" 2>/dev/null
+ ip route add default via 10.0.2.2 dev "$NETIF" 2>/dev/null
+ echo "Network found" > /dev/console
+ else
+ eend 1 "no network interface"
+ return 1
+ fi
+ printf 'nameserver 10.0.2.3\nnameserver 8.8.8.8\nnameserver 1.1.1.1\n' \
+ > /etc/resolv.conf
+ eend 0
+}
diff --git a/build-rootfs/files/etc/init.d/podroid-ready b/build-rootfs/files/etc/init.d/podroid-ready
new file mode 100644
index 0000000..199e5a6
--- /dev/null
+++ b/build-rootfs/files/etc/init.d/podroid-ready
@@ -0,0 +1,22 @@
+#!/sbin/openrc-run
+description="Podroid VM ready marker (consumed by Android boot detector)"
+
+depend() {
+ need podroid-network dropbear
+ after *
+}
+
+start() {
+ # By the time we run, dropbear has started (we depend on it).
+ # Emit the boot stages PodroidQemu.detectBootStage() expects.
+ echo "Starting SSH..." > /dev/console
+ sleep 0.2
+ echo "Almost ready..." > /dev/console
+ sleep 0.3
+ echo "Ready!" > /dev/console
+ return 0
+}
+
+stop() {
+ return 0
+}
diff --git a/build-rootfs/files/etc/init.d/podroid-resize b/build-rootfs/files/etc/init.d/podroid-resize
new file mode 100644
index 0000000..a32a236
--- /dev/null
+++ b/build-rootfs/files/etc/init.d/podroid-resize
@@ -0,0 +1,10 @@
+#!/sbin/openrc-run
+description="Podroid terminal resize daemon (hvc1 → stty hvc0)"
+command="/usr/local/bin/podroid-resize"
+command_background="yes"
+pidfile="/run/podroid-resize.pid"
+
+depend() {
+ need podroid-bootstrap
+ before podroid-ready
+}
diff --git a/build-rootfs/files/etc/inittab b/build-rootfs/files/etc/inittab
new file mode 100644
index 0000000..0a5fa76
--- /dev/null
+++ b/build-rootfs/files/etc/inittab
@@ -0,0 +1,6 @@
+::sysinit:/sbin/openrc sysinit
+::sysinit:/sbin/openrc boot
+::wait:/sbin/openrc default
+
+hvc0::respawn:/sbin/getty -L -l /usr/local/bin/podroid-login hvc0 115200 vt100
+::shutdown:/sbin/openrc shutdown
diff --git a/build-rootfs/files/etc/rc.conf b/build-rootfs/files/etc/rc.conf
new file mode 100644
index 0000000..de62ba4
--- /dev/null
+++ b/build-rootfs/files/etc/rc.conf
@@ -0,0 +1,3 @@
+rc_parallel="YES"
+rc_logger="NO"
+rc_verbose="NO"
diff --git a/build-rootfs/files/usr/local/bin/podroid-login b/build-rootfs/files/usr/local/bin/podroid-login
new file mode 100644
index 0000000..7fa8b10
--- /dev/null
+++ b/build-rootfs/files/usr/local/bin/podroid-login
@@ -0,0 +1,11 @@
+#!/bin/sh
+# Replacement login shell for hvc0 getty. Restores the terminal size
+# from /run/term_size (written by podroid-resize) before exec'ing the
+# real login binary — virtio-console resets winsize to 0x0 on every
+# getty respawn, so without this TUI apps can't render until the user
+# triggers a host-side SIGWINCH.
+if [ -r /run/term_size ]; then
+ read _r _c < /run/term_size
+ [ -n "$_r" ] && [ -n "$_c" ] && stty rows "$_r" cols "$_c" 2>/dev/null
+fi
+exec /bin/login "$@"
diff --git a/build-rootfs/files/usr/local/bin/podroid-resize b/build-rootfs/files/usr/local/bin/podroid-resize
new file mode 100644
index 0000000..3aae8e0
--- /dev/null
+++ b/build-rootfs/files/usr/local/bin/podroid-resize
@@ -0,0 +1,26 @@
+#!/bin/sh
+# Reads "RESIZE rows cols" lines from /dev/hvc1 (ctrl.sock) and applies
+# them to /dev/hvc0 via stty. The Android side's libpodroid-bridge
+# debounces SIGWINCH bursts and emits one line per stable size.
+#
+# Also writes the last-known size to /run/term_size so podroid-login
+# can restore it after each getty respawn (virtio-console resets winsize
+# to 0x0 every time the line is reopened).
+set -e
+
+# Wait for the virtio-console node to appear (timing varies)
+for _i in $(seq 1 30); do
+ [ -c /dev/hvc1 ] && break
+ sleep 1
+done
+[ -c /dev/hvc1 ] || exit 0
+
+while IFS= read -r _line; do
+ case "$_line" in
+ RESIZE\ *)
+ set -- $_line
+ stty rows "$2" cols "$3" </dev/hvc0 2>/dev/null
+ printf '%s %s\n' "$2" "$3" > /run/term_size 2>/dev/null
+ ;;
+ esac
+done </dev/hvc1
diff --git a/init-podroid b/init-podroid
index 3ec323f..45b24e4 100755
--- a/init-podroid
+++ b/init-podroid
@@ -1,355 +1,49 @@
-#!/bin/bash
-# Podroid init — Alpine Linux VM bootstrap
-# Phase 1 (direct exec): mounts persistent overlay, pivots via chroot
-# Phase 2 (--main): full system setup, starts getty
+#!/bin/sh
+# Podroid initramfs bootstrap — mounts overlay, switch_root to /sbin/init.
+# All system bringup logic now lives in /etc/init.d/podroid-* services
+# on the Alpine squashfs.
+set -e
-# ══════════════════════════════════════════════════════════════════════════════
-# Phase 2: Main system init (inside persistent overlay)
-# ══════════════════════════════════════════════════════════════════════════════
-if [ "$1" = "--main" ]; then
+mount -t proc proc /proc -o noexec,nosuid,nodev
+mount -t sysfs sys /sys -o noexec,nosuid,nodev
+mount -t devtmpfs devtmpfs /dev -o exec,nosuid,mode=0755,size=2M
-mkdir -p /run /var/log
-_ILOG=/var/log/init.log
-_log() { printf '%s %s\n' "$(date +%H:%M:%S)" "$*" >> "$_ILOG" 2>/dev/null; }
-boot_stage() { printf '%s\n' "$*" > /run/boot_stage 2>/dev/null; printf '[%s] %s\n' "$(date +%H:%M:%S)" "$*"; }
-boot_stage "Initializing system..."
+echo "Mounting storage..." > /dev/console
-# ── Core filesystems ──────────────────────────────────────────────────────────
-mount -t proc proc /proc -o noexec,nosuid,nodev 2>/dev/null
-mount -t sysfs sys /sys -o noexec,nosuid,nodev 2>/dev/null
-mount -t devtmpfs devtmpfs /dev -o exec,nosuid,mode=0755,size=2M 2>/dev/null || \
- mount -t tmpfs tmpfs /dev -o exec,nosuid,mode=0755,size=2M 2>/dev/null
+# virtio_blk is built-in; modprobe is a no-op safety net
+[ -d "/lib/modules/$(uname -r)" ] && { depmod -a 2>/dev/null; modprobe virtio_blk 2>/dev/null; }
-mkdir -p /dev/pts /dev/shm /dev/mqueue /dev/net 2>/dev/null
+# /dev/vda = persistent ext4 (user data, container storage)
+# /dev/vdb = read-only Alpine squashfs (system files)
+mkdir -p /mnt/persist /mnt/lower /mnt/overlay
-# newinstance devpts — required for PTY isolation in containers
-mount -t devpts devpts /dev/pts \
- -o gid=5,mode=0620,ptmxmode=0666,newinstance,noexec,nosuid 2>/dev/null || \
- _log "devpts mount failed"
-
-# Real ptmx char device — required for crun PTY allocation
-rm -f /dev/ptmx 2>/dev/null
-mknod /dev/ptmx c 5 2 2>/dev/null || _log "mknod /dev/ptmx failed"
-chmod 0666 /dev/ptmx /dev/pts/ptmx 2>/dev/null
-
-mount -t tmpfs tmpfs /dev/shm -o noexec,nosuid,nodev,size=64m 2>/dev/null
-mount -t tmpfs tmpfs /tmp -o noexec,nosuid,nodev 2>/dev/null
-mount -t tmpfs tmpfs /run -o noexec,nosuid,nodev,mode=0755 2>/dev/null
-
-# ── cgroup2 ───────────────────────────────────────────────────────────────────
-mkdir -p /sys/fs/cgroup 2>/dev/null
-mount -t cgroup2 cgroup2 /sys/fs/cgroup 2>/dev/null
-printf '+cpuset +cpu +io +memory +hugetlb +pids +rdma\n' \
- > /sys/fs/cgroup/cgroup.subtree_control 2>/dev/null
-
-# ── Busybox + directory tree ──────────────────────────────────────────────────
-/bin/busybox --install -s 2>/dev/null
-ln -sf /bin/bash /bin/sh 2>/dev/null
-
-mkdir -p \
- /var/lib/containers/storage \
- /run/containers/storage /run/libpod /run/crun /run/lock \
- /etc/containers /etc/dropbear /etc/crun \
- /mnt/downloads \
- /usr/local/bin /usr/local/sbin \
- /root /root/.ssh /var/tmp 2>/dev/null
-
-chmod 700 /root /root/.ssh 2>/dev/null
-chmod 1777 /tmp /var/tmp 2>/dev/null
-
-printf 'podroid\n' > /etc/hostname
-hostname podroid 2>/dev/null
-printf '127.0.0.1\tlocalhost podroid\n::1\tlocalhost ip6-localhost\n' > /etc/hosts
-
-# ── Kernel modules + device nodes ────────────────────────────────────────────
-# Bridge / netfilter / overlay / fuse / virtio are =y in our custom kernel
-# (see Dockerfile forced_builtin.config), so only the few that legitimately
-# remain modular are loaded here. The check on /lib/modules still guards
-# against a kernel built without CONFIG_MODULES.
-boot_stage "Loading kernel modules..."
-_kver=$(uname -r)
-if [ -d "/lib/modules/$_kver" ]; then
- depmod -a 2>/dev/null
- for _mod in 9p 9pnet 9pnet_virtio; do
- modprobe "$_mod" 2>/dev/null
- done
-fi
-[ -c /dev/net/tun ] || mknod /dev/net/tun c 10 200 2>/dev/null
-[ -c /dev/fuse ] || mknod /dev/fuse c 10 229 2>/dev/null
-
-# ── ZRAM swap (built-in; half RAM at lz4) ────────────────────────────────────
-if [ -b /dev/zram0 ]; then
- _mem_kb=$(awk '/^MemTotal:/{print $2}' /proc/meminfo)
- echo lz4 > /sys/block/zram0/comp_algorithm 2>/dev/null || \
- echo lzo-rle > /sys/block/zram0/comp_algorithm 2>/dev/null
- echo $(( _mem_kb * 512 )) > /sys/block/zram0/disksize 2>/dev/null
- mkswap /dev/zram0 >/dev/null 2>&1 && swapon -p 100 /dev/zram0 2>/dev/null
-fi
-
-# ── Kernel cmdline ────────────────────────────────────────────────────────────
-ANDROID_IP="" SSH_ENABLED=0
-for _p in $(cat /proc/cmdline); do
- case "$_p" in
- androidip=*) ANDROID_IP="${_p#androidip=}" ;;
- ssh=1) SSH_ENABLED=1 ;;
- esac
-done
-
-# ── Networking ────────────────────────────────────────────────────────────────
-boot_stage "Waiting for network..."
-ip link set lo up 2>/dev/null
-NETIF=""
-for _i in $(seq 1 150); do
- NETIF=$(ip -o link show 2>/dev/null | awk -F': ' '{print $2}' | \
- grep -vE '^(lo|dummy[0-9]*|sit[0-9]*|gre[0-9]*|tunl[0-9]*|br-|veth|podman|cni-)' | head -1)
- [ -n "$NETIF" ] && break
- sleep 0.2
-done
-
-if [ -n "$NETIF" ]; then
- ip link set "$NETIF" up 2>/dev/null
- ip addr add 10.0.2.15/24 dev "$NETIF" 2>/dev/null
- ip route add default via 10.0.2.2 dev "$NETIF" 2>/dev/null
-else
- _log "No network interface found after 30s"
-fi
-boot_stage "Network found"
-
-printf 'nameserver 10.0.2.3\nnameserver 8.8.8.8\nnameserver 1.1.1.1\n' > /etc/resolv.conf
-
-# 9p downloads — non-critical, mount in background
-mount -t 9p -o trans=virtio,version=9p2000.L,rw,msize=1048576,cache=loose,noatime \
- downloads /mnt/downloads 2>/dev/null &
-
-# ── Container setup ───────────────────────────────────────────────────────────
-boot_stage "Configuring containers..."
-
-sysctl -qw \
- net.ipv4.ip_forward=1 \
- net.ipv4.conf.all.forwarding=1 \
- net.bridge.bridge-nf-call-iptables=1 \
- net.bridge.bridge-nf-call-ip6tables=1 \
- fs.inotify.max_user_instances=256 \
- fs.inotify.max_user_watches=65536 \
- net.core.rmem_max=26214400 \
- net.core.wmem_max=26214400 2>/dev/null
-sysctl -qw net.ipv4.ping_group_range="0 2147483647" 2>/dev/null
-
-[ -f /etc/subuid ] || printf 'root:100000:65536\n' > /etc/subuid
-[ -f /etc/subgid ] || printf 'root:100000:65536\n' > /etc/subgid
-
-cat > /etc/containers/containers.conf <<'EOF'
-[containers]
-cgroups = "enabled"
-
-[engine]
-runtime = "crun"
-events_logger = "file"
-
-[network]
-network_backend = "netavark"
-EOF
-
-cat > /etc/containers/storage.conf <<'EOF'
-[storage]
-driver = "overlay"
-runroot = "/run/containers/storage"
-graphroot = "/var/lib/containers/storage"
-
-[storage.options.overlay]
-mount_program = "/usr/bin/fuse-overlayfs"
-EOF
-
-printf 'unqualified-search-registries = ["docker.io", "quay.io"]\n' \
- > /etc/containers/registries.conf
-touch /etc/crun/crun.conf 2>/dev/null
-
-# ── SSH (Dropbear) ────────────────────────────────────────────────────────────
-if [ "$SSH_ENABLED" = "1" ]; then
- boot_stage "Starting SSH..."
- if [ ! -f /etc/podroid-ssh-init ]; then
- printf 'root:podroid\n' | chpasswd 2>/dev/null
- touch /etc/podroid-ssh-init
- fi
- rm -f /etc/dropbear/dropbear_rsa_host_key 2>/dev/null
- [ -f /etc/dropbear/dropbear_ed25519_host_key ] || \
- dropbearkey -t ed25519 -f /etc/dropbear/dropbear_ed25519_host_key >/dev/null 2>&1
- dropbear -p 22 >/dev/null 2>&1
-fi
-
-# ── Environment ───────────────────────────────────────────────────────────────
-export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
-export HOME=/root TERM=xterm-256color LANG=C.UTF-8 LC_ALL=C.UTF-8 CHARSET=UTF-8
-export PODMAN_IGNORE_CGROUPSV1_WARNING=1
-export CONTAINERS_CONF=/etc/containers/containers.conf
-export CONTAINERS_STORAGE_CONF=/etc/containers/storage.conf
-export CONTAINERS_REGISTRIES_CONF=/etc/containers/registries.conf
-
-dmesg -n 1 2>/dev/null
-trap 'podman stop -a -t 5 2>/dev/null; podman network prune -f 2>/dev/null; sync' TERM INT HUP
-
-# ── Resize daemon (reads RESIZE msgs from hvc1, applies to hvc0 terminal) ─────
-(
- for _i in $(seq 1 30); do [ -c /dev/hvc1 ] && break; sleep 1; done
- [ -c /dev/hvc1 ] || exit 0
- while IFS= read -r _line; do
- case "$_line" in
- RESIZE\ *) set -- $_line; stty rows "$2" cols "$3" </dev/hvc0 2>/dev/null; printf '%s %s\n' "$2" "$3" > /run/term_size 2>/dev/null ;;
- esac
- done </dev/hvc1
-) &
-
-# ── MOTD + profile ────────────────────────────────────────────────────────────
-boot_stage "Almost ready..."
-
-_alpine=$(cat /etc/alpine-release 2>/dev/null || echo "?")
-_kver=$(uname -r | cut -d- -f1)
-_mem_mb=$(awk '/^MemTotal:/{printf "%.0f",$2/1024}' /proc/meminfo)
-_mem_free=$(awk '/^MemAvailable:/{printf "%.0f",$2/1024}' /proc/meminfo)
-_cpus=$(nproc)
-_uptime=$(( $(cut -d. -f1 /proc/uptime) / 60 ))
-_disk_used=$(df -BG / 2>/dev/null | awk 'NR==2{gsub(/G/,"",$3);print $3}')
-_disk_total=$(df -BG / 2>/dev/null | awk 'NR==2{gsub(/G/,"",$2);print $2}')
-
-# Probe internet synchronously (max 2s) so the *first* MOTD already shows the
-# real status. The async-sed pattern was racing with first login → user always
-# saw "checking..." until they exited the shell once.
-if wget -q -O /dev/null --timeout=2 http://dl-cdn.alpinelinux.org/alpine/MIRRORS.txt 2>/dev/null; then
- _net="✓ online"
-else
- _net="✗ offline"
-fi
-
-if [ "$SSH_ENABLED" = "1" ] && [ -n "$ANDROID_IP" ]; then
- _ssh_line="SSH: ssh root@${ANDROID_IP} -p 9922 (pass: podroid)"
-else
- _ssh_line=""
-fi
-
-# ── MOTD box ──────────────────────────────────────────────────────────────────
-# Bash counts ${#var} as characters (not bytes) under LC_ALL=C.UTF-8, which is
-# already exported above — so multi-byte content like · / ✓ / ✗ pads correctly.
-_W=58
-_pad() {
- local _s="$1" _p
- _p=$((_W - ${#_s})); [ $_p -lt 0 ] && _p=0
- printf '│ %s%*s │\n' "$_s" "$_p" ''
-}
-_BAR='────────────────────────────────────────────────────────────'
-{
- printf '┌%s┐\n' "$_BAR"
- _pad ""
- _pad "Podroid · Alpine ${_alpine} · Linux ${_kver}"
- _pad "${_cpus} CPU · ${_mem_free}/${_mem_mb} MB · ${_disk_used}/${_disk_total} GB · Up ${_uptime}m · Net: ${_net}"
- [ -n "$_ssh_line" ] && _pad "$_ssh_line"
- _pad "Quickstart: podman run -it --rm alpine"
- _pad ""
- printf '└%s┘\n' "$_BAR"
-} > /etc/motd
-
-cat > /etc/profile <<'PROF'
-export HOME=/root SHELL=/bin/bash
-export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
-export TERM=xterm-256color LANG=C.UTF-8 LC_ALL=C.UTF-8 CHARSET=UTF-8
-export PODMAN_IGNORE_CGROUPSV1_WARNING=1
-export CONTAINERS_CONF=/etc/containers/containers.conf
-export CONTAINERS_STORAGE_CONF=/etc/containers/storage.conf
-export CONTAINERS_REGISTRIES_CONF=/etc/containers/registries.conf
-
-stty -ixon -ixoff -iexten -echoctl 2>/dev/null
-set +o checkwinsize 2>/dev/null
-
-alias ls='ls --color=auto' ll='ls -lah --color=auto' la='ls -A --color=auto'
-alias grep='grep --color=auto' df='df -h' free='free -m' ip='ip -c' less='less -R'
-alias dps='podman ps -a' di='podman images' dlogs='podman logs -f'
-alias dexec='podman exec -it' drun='podman run --rm -it'
-alias dstop='podman stop' drm='podman rm' drmi='podman rmi' dstats='podman stats'
-
-PS1='\[\e[1;32m\]➜\[\e[0m\] \[\e[1;34m\]\w\[\e[0m\] \[\e[1;31m\]#\[\e[0m\] '
-cd /root 2>/dev/null
-
-# Auto-source the user's ~/.bashrc if present. /root is on the persistent
-# overlay upper, so this survives reboots — drop a .bashrc there to layer
-# custom aliases / PS1 / env on top of the defaults above.
-[ -r "$HOME/.bashrc" ] && . "$HOME/.bashrc"
-PROF
-
-# ── Login wrapper ─────────────────────────────────────────────────────────────
-cat > /usr/local/bin/podroid-login <<'LOGIN'
-#!/bin/bash
-# virtio-console resets winsize to 0x0 each time the line is reopened, so
-# every getty respawn (after `exit` / Ctrl-D) starts a shell that TUI apps
-# can't draw into until the next host SIGWINCH. Restore the last-known size
-# the resize daemon recorded in /run/term_size before exec'ing bash.
-if [ -r /run/term_size ]; then
- read _r _c < /run/term_size
- [ -n "$_r" ] && [ -n "$_c" ] && stty rows "$_r" cols "$_c" 2>/dev/null
-fi
-cat /etc/motd 2>/dev/null
-exec /bin/bash --login
-LOGIN
-chmod +x /usr/local/bin/podroid-login
-grep -q '^root:' /etc/passwd 2>/dev/null || \
- printf 'root:x:0:0:root:/root:/bin/bash\n' > /etc/passwd
-
-boot_stage "Ready!"
-rm -f /run/boot_stage
-
-# getty runs on /dev/hvc0 (virtio-console, primary terminal). The serial port
-# (ttyAMA0) stays attached to the boot-monitor socket for console.log.
-while true; do
- /sbin/getty -n -l /usr/local/bin/podroid-login 0 hvc0 xterm-256color
- sleep 1
+# Persistent: format on first boot if needed
+for _try in 1 2; do
+ if mount -t ext4 -o rw,noatime /dev/vda /mnt/persist 2>/dev/null; then break; fi
+ e2fsck -y /dev/vda >/dev/null 2>&1 || mkfs.ext4 -q -F /dev/vda
done
-fi
-
-# ══════════════════════════════════════════════════════════════════════════════
-# Phase 1: Early init — mount persistent overlay and pivot
-# ══════════════════════════════════════════════════════════════════════════════
-
-mount -t proc proc /proc -o noexec,nosuid,nodev 2>/dev/null
-mount -t sysfs sys /sys -o noexec,nosuid,nodev 2>/dev/null
-mount -t devtmpfs devtmpfs /dev -o exec,nosuid,mode=0755,size=2M 2>/dev/null || \
- mount -t tmpfs tmpfs /dev -o exec,nosuid,mode=0755,size=2M 2>/dev/null
-_kver=$(uname -r)
-[ -d "/lib/modules/$_kver" ] && { depmod -a 2>/dev/null; modprobe virtio_blk 2>/dev/null; }
+mount -t squashfs -o ro /dev/vdb /mnt/lower
-echo 1 > /proc/sys/kernel/printk 2>/dev/null
+mkdir -p /mnt/persist/upper /mnt/persist/work
+mount -t overlay overlay \
+ -o lowerdir=/mnt/lower,upperdir=/mnt/persist/upper,workdir=/mnt/persist/work \
+ /mnt/overlay
-printf ' ╔═══════════════════════════════════════════╗\n'
-printf ' ║ Podroid VM is starting... ║\n'
-printf ' ║ Please wait... ║\n'
-printf ' ╚═══════════════════════════════════════════╝\n\n'
+# Move /mnt/persist into the new root so users can reach raw storage,
+# and so switch_root can fully clear initramfs (no orphan mounts left
+# under /mnt that would block its unlink pass).
+mkdir -p /mnt/overlay/mnt/persist
+mount --move /mnt/persist /mnt/overlay/mnt/persist
-mkdir -p /mnt/persist /mnt/overlay 2>/dev/null
-
-for _dev in /dev/vda /dev/vdb /dev/vdc; do
- [ -b "$_dev" ] || continue
- if ! mount -t ext4 -o rw,noatime "$_dev" /mnt/persist 2>/dev/null; then
- /bin/busybox --install -s 2>/dev/null
- e2fsck -y "$_dev" >/dev/null 2>&1
- if ! mount -t ext4 -o rw,noatime "$_dev" /mnt/persist 2>/dev/null; then
- mkfs.ext4 -q -F "$_dev" >/dev/null 2>&1
- mount -t ext4 -o rw,noatime "$_dev" /mnt/persist 2>/dev/null || continue
- fi
- fi
- break
-done
+# Move /mnt/lower (the squashfs) too — keeps /dev/vdb mount visible in the
+# new root, and lets initramfs cleanup unlink /mnt cleanly.
+mkdir -p /mnt/overlay/mnt/lower
+mount --move /mnt/lower /mnt/overlay/mnt/lower
-if mountpoint -q /mnt/persist 2>/dev/null; then
- mkdir -p /mnt/persist/upper /mnt/persist/work 2>/dev/null
- if mount -t overlay overlay \
- -o lowerdir=/,upperdir=/mnt/persist/upper,workdir=/mnt/persist/work \
- /mnt/overlay 2>/dev/null; then
- mkdir -p /mnt/overlay/mnt/persist 2>/dev/null
- mount --bind /mnt/persist /mnt/overlay/mnt/persist 2>/dev/null
- touch /mnt/persist/.overlay-active 2>/dev/null
- umount /dev /sys /proc 2>/dev/null
- exec /bin/busybox chroot /mnt/overlay /init --main
- fi
-fi
+# Move our early mounts into the new root so OpenRC inherits them
+mount --move /proc /mnt/overlay/proc
+mount --move /sys /mnt/overlay/sys
+mount --move /dev /mnt/overlay/dev
-# Fallback: boot without persistent storage (writes go to initramfs tmpfs)
-umount /dev /sys /proc 2>/dev/null
-exec /init --main
+exec /sbin/switch_root /mnt/overlay /sbin/init