Skip to content

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

7.9 KB
Raw Blame
#!/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

    # Seed the system clock from the host. AVF/crosvm doesn't wire an
    # emulated RTC the way QEMU TCG does, so without this the guest boots
    # at epoch (1970-01-01) and every TLS cert appears "not yet valid" —
    # `apk update` warns `server certificate not trusted`, curl/wget fail,
    # etc. AvfEngine passes `podroid.epoch=<unix-secs>` on the cmdline; we
    # parse it here. No-op when the marker is absent (QEMU path).
    PODROID_EPOCH=$(sed -n 's/.*podroid\.epoch=\([0-9]*\).*/\1/p' /proc/cmdline)
    if [ -n "$PODROID_EPOCH" ] && [ "$PODROID_EPOCH" -gt 0 ] 2>/dev/null; then
        date -s "@$PODROID_EPOCH" >/dev/null 2>&1
    fi

    # 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
    # configfs backs the LIO iSCSI target (targetcli); without it apk-added
    # targetcli has nothing to write into.
    mkdir -p /sys/kernel/config 2>/dev/null
    mountpoint -q /sys/kernel/config || mount -t configfs -o nosuid,nodev,noexec \
        configfs /sys/kernel/config || true
    [ -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

    # I/O scheduler — kernel cmdline `elevator=` was deprecated in Linux 5.0,
    # so set per-device via sysfs. mq-deadline is what overlay/ext4 prefers
    # under random-write loads (Podman/Docker graph driver).
    for q in /sys/block/vda/queue/scheduler /sys/block/vdb/queue/scheduler; do
        [ -w "$q" ] && echo mq-deadline > "$q" 2>/dev/null
    done

    # Downloads share (QEMU virtio-9p). On AVF, podroid-downloads owns
    # /mnt/downloads instead (9p over vsock, rendezvous with the Android-side
    # 9p2000.L server) — skip the virtio-9p attempt there entirely.
    #
    # QEMU path: self-gating — host only adds the virtio-9p device when the
    # user enables the toggle, so this returns ENODEV and exits silently when
    # off. Foreground so a missing /dev/fuse or kernel feature surfaces in the
    # boot log instead of being swallowed.
    # msize=262144 (256 KB) — well under QEMU's virtio-9p 512 KB ceiling and
    # large enough to amortize round-trips for big reads. cache=mmap is the
    # next-best after cache=fscache (which requires CONFIG_9P_FSCACHE=y, not
    # in this kernel build); mmap still page-caches mapped regions and is
    # noticeably better than cache=loose for re-reads.
    if ! grep -q 'podroid\.backend=avf' /proc/cmdline 2>/dev/null; then
        mkdir -p /mnt/downloads 2>/dev/null
        mount -t 9p -o trans=virtio,version=9p2000.L,rw,msize=262144,cache=mmap,noatime \
            downloads /mnt/downloads 2>/dev/null
    fi

    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 (1.5x RAM, lz4). Was half-RAM; with lz4 averaging ~3x
    # compression on text/heap pages, 1.5x RAM disksize gives ~4.5x effective
    # memory under pressure — enough headroom for Firefox + XFCE in a 2 GB VM
    # without thrashing into OOM.
    if [ -b /dev/zram0 ]; then
        _mem_kb=$(awk '/^MemTotal:/{print $2}' /proc/meminfo)
        echo lz4 > /sys/block/zram0/comp_algorithm 2>/dev/null
        # *1536 = KiB→bytes (*1024) × 1.5 ratio; disksize wants bytes.
        echo $((_mem_kb * 1536)) > /sys/block/zram0/disksize 2>/dev/null
        mkswap /dev/zram0 >/dev/null 2>&1 && swapon -p 100 /dev/zram0 2>/dev/null
    fi

    # OOM behavior: re-assert the kernel default (0 = let the OOM killer pick
    # the highest-badness victim, rather than killing whatever triggered the
    # allocation). Per-service immunity (-1000 for dropbear/Xvnc/agent) is set
    # in podroid-ready once those services exist.
    [ -w /proc/sys/vm/oom_kill_allocating_task ] && \
        echo 0 > /proc/sys/vm/oom_kill_allocating_task
    # overcommit_memory=1 (always overcommit) lets us actually hand out
    # the address space Firefox wants without ENOMEM at mmap time —
    # without this, browsers can crash with malloc failures long before
    # the kernel's OOM killer ever fires.
    [ -w /proc/sys/vm/overcommit_memory ] && \
        echo 1 > /proc/sys/vm/overcommit_memory

    # sysctl for container networking
    sysctl -qw net.ipv4.ip_forward=1 \
        net.ipv4.conf.all.forwarding=1 \
        net.ipv6.conf.all.forwarding=1 \
        net.ipv6.conf.default.forwarding=1 \
        net.bridge.bridge-nf-call-iptables=1 \
        net.bridge.bridge-nf-call-ip6tables=1 2>/dev/null

    # Bind /var/lib/docker onto raw ext4. Docker's overlay2 storage driver
    # mounts an overlay per container with /var/lib/docker as the upper.
    # Linux refuses to use an overlay as the upper of another overlay
    # (returns EINVAL), so without this, `docker run` fails on any image.
    # The bind makes Docker's state live directly on the persistent ext4
    # disk, sidestepping the nested-overlay rejection.
    mkdir -p /mnt/persist/docker /var/lib/docker 2>/dev/null
    mountpoint -q /var/lib/docker || mount --bind /mnt/persist/docker /var/lib/docker

    # Same trick for Podman. Without this its overlay graph driver works
    # (image pulls succeed) but each `podman run` falls back to
    # fuse-overlayfs for the container's runtime mount — kernel overlay
    # can't stack on the rootfs overlay we live on. Pinning Podman's
    # store onto raw ext4 lets the kernel overlay stack cleanly and
    # gives container IO the same speed Docker enjoys.
    mkdir -p /mnt/persist/containers /var/lib/containers/storage 2>/dev/null
    mountpoint -q /var/lib/containers/storage \
        || mount --bind /mnt/persist/containers /var/lib/containers/storage

    # And for LXC. The overlay storage backend (`lxc-create -B overlayfs`)
    # has the same nested-overlay problem; even the default `dir` backend
    # benefits from raw-ext4 IO over going through the rootfs overlay.
    mkdir -p /mnt/persist/lxc /var/lib/lxc 2>/dev/null
    mountpoint -q /var/lib/lxc || mount --bind /mnt/persist/lxc /var/lib/lxc

    # The lxc bridge (lxcbr0, 10.0.3.1/24) plus its MASQUERADE/DHCP are owned
    # by the stock `dnsmasq.lxcbr0` service (rc_before=lxc), which creates the
    # bridge, assigns the address, and installs NAT in its start_pre. We used
    # to pre-create it here as well, but both running double-assigned the
    # address ("ipv4: Address already assigned" in the boot log). /var/lib/lxc
    # above is still ours to bind-mount.

    eend 0
}