mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-03 10:20:16 +08:00
guest: add a docker shim to the Debian image that installs Docker on first use, configures it for the kernel (no netfilter, bridge, BPF or cgroup namespaces; data root off the overlay; crun without a device cgroup) and starts the service
This commit is contained in:
@@ -28,7 +28,9 @@ missing).
|
||||
serves several.
|
||||
- State in `/data/distros/NAME/`: `upper`, `work`, `lower` (the image id
|
||||
the upper was last used with), `history` (image changes),
|
||||
`discard` (a pending discard), `discarded-TIME/` (set-aside changes).
|
||||
`discard` (a pending discard), `discarded-TIME/` (set-aside changes),
|
||||
and on Debian `docker` (Docker's data root, which the docker shim puts
|
||||
there; kernel/guest.md).
|
||||
Nothing else on `/data` is read (a disk written by an earlier guest, with
|
||||
`/data/overlay`, keeps it untouched and starts with no changes).
|
||||
`/run/kw-base` describes the boot (`name=`, `image=`,
|
||||
|
||||
@@ -281,6 +281,33 @@ layer: boot to a prompt 0.25 s (Alpine) and 0.32 s (Debian) from
|
||||
The user's `~/.bashrc` (from `/etc/skel`, whose two prompt lines the
|
||||
build makes no-ops) leaves the prompt to kw-prompt.sh; the build adds
|
||||
the group input (gid 101), which udev's package would.
|
||||
- **Docker (Debian)**: `/usr/local/bin/docker` (ahead of `/usr/bin` on
|
||||
PATH, run for every call, described at its top) installs docker.io,
|
||||
docker-cli (a Recommends in trixie) and crun on first use (apt-get,
|
||||
through `sudo -n`; exit 127 offline), adds the user to the group docker
|
||||
(until the next login it runs the CLI through `sudo -n -E -u USER`),
|
||||
starts the service when dockerd is not running and waits for its
|
||||
socket; sysv-rc starts it at every boot after that. It writes
|
||||
`/etc/docker/daemon.json` (when missing) for what the kernel lacks:
|
||||
iptables, ip6tables and the default bridge off (no netfilter, no
|
||||
bridge), cgroupns host (no CGROUP_NS), and the data root outside the
|
||||
root's overlay, on which overlay2 cannot put its upper layers:
|
||||
`/data/distros/debian/docker` with a data disk (a `kw-overlay discard`
|
||||
keeps it), else a tmpfs on `/var/lib/docker`. The runtime is
|
||||
`/usr/local/libexec/kw-docker-runtime`: crun after two edits of the
|
||||
bundle's config.json, one rule allowing every device (the device
|
||||
cgroup is a BPF program, there is no bpf(2), and crun goes on without
|
||||
it when no rule denies; runc adds rules of its own and fails) and no
|
||||
empty `blockIO` (crun then wants the io controller; cgroup2 has only
|
||||
pids). So containers can open any device node they make, and Docker
|
||||
drops `--memory` with a warning. The default network would be an
|
||||
empty namespace, so the shim adds `--network host` to run, create and
|
||||
build unless the arguments name a network; `-p` is ignored, a
|
||||
container's ports are the guest's. `docker build` is the legacy builder
|
||||
(no buildx); no compose. Node CLI, 1 GiB, data disk, 2026-11-19, Docker
|
||||
26.1.5: first `docker run --rm alpine` 58 s (45 MB of packages, the
|
||||
pull), then 1-3 s; debian:trixie-slim pulled and run 21 s; a build with
|
||||
`apk add` 5 s; dockerd and containerd about 130 MB RSS.
|
||||
|
||||
## Data disk template (`kernel/build.sh datadisk`)
|
||||
|
||||
|
||||
@@ -23,6 +23,11 @@ Things to try
|
||||
Installed into ~/.local/bin the first time you run
|
||||
one (claude and opencode are 250-360 MB, so they
|
||||
need the data disk or plenty of memory).
|
||||
@if debian
|
||||
docker run --rm -it alpine
|
||||
Docker is installed and started the first time you
|
||||
run docker. Containers use the guest's network.
|
||||
@end
|
||||
glimmer A tiny game, with graphics and sound.
|
||||
python3 -m http.server 8000
|
||||
A guest web server (on the web page it opens in the
|
||||
|
||||
@@ -32,6 +32,12 @@
|
||||
# /usr/lib/kw-commands "NAME PACKAGE" per command of main, for
|
||||
# kw-apt-install (command not found:
|
||||
# etc/profile.d/kw-apt.sh; tools/commands.py)
|
||||
# /usr/local/bin/docker the docker shim: installs docker.io, docker-cli
|
||||
# and crun on first use, writes daemon.json for
|
||||
# this kernel, starts the service, defaults to
|
||||
# --network host; its runtime is
|
||||
# /usr/local/libexec/kw-docker-runtime (crun
|
||||
# without a device cgroup)
|
||||
# user the default user of the console and kwtty
|
||||
# sessions (uid 1000, bash, /home/user with
|
||||
# /etc/skel's files and the README), in sudo,
|
||||
|
||||
139
guest/distro/debian/rootfs/usr/local/bin/docker
Executable file
139
guest/distro/debian/rootfs/usr/local/bin/docker
Executable file
@@ -0,0 +1,139 @@
|
||||
#!/bin/bash
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
||||
# docker shim of the Debian image, /usr/local/bin/docker (ahead of
|
||||
# /usr/bin on PATH): makes sure Docker is installed, configured for this
|
||||
# kernel and running, then runs the real CLI (/usr/bin/docker).
|
||||
# - Install, once: docker.io, docker-cli (a Recommends, which the image
|
||||
# leaves out) and crun, with apt-get as root (directly, or through
|
||||
# `sudo -n`), after writing /etc/docker/daemon.json so that the service
|
||||
# the package starts already uses it. The user is added to the group
|
||||
# docker; until their next login this shim runs the CLI through
|
||||
# `sudo -n -E -u USER`, which picks up the new group.
|
||||
# - daemon.json (written when missing): the kernel has no netfilter and no
|
||||
# bridge (no iptables, no default bridge network), no cgroup namespaces
|
||||
# (cgroupns host), and the root is an overlay, on which overlay2 cannot
|
||||
# put its upper layers: with a data disk the data root is
|
||||
# /data/distros/debian/docker (outside the root's overlay, so a
|
||||
# kw-overlay discard leaves the images there), else a tmpfs mounted on
|
||||
# /var/lib/docker for this boot. The runtime is
|
||||
# /usr/local/libexec/kw-docker-runtime (crun; there is no BPF for the
|
||||
# device cgroup).
|
||||
# - Start: the init script (`service docker start`, which sysv-rc also
|
||||
# runs at boot) when dockerd is not running; then wait for its socket.
|
||||
# - Network: containers get the guest's own (--network host) for run,
|
||||
# create and build unless the arguments name a network: the default
|
||||
# network of a container would be an empty namespace, with no route
|
||||
# out. Ports a container listens on are the guest's (-p is ignored).
|
||||
# Exit 127 when Docker is missing and cannot be installed (no network, no
|
||||
# root), 1 when dockerd does not start (the end of /var/log/docker.log).
|
||||
|
||||
# unkernel: install, configure and start Docker, then run its CLI.
|
||||
real=/usr/bin/docker
|
||||
sock=/run/docker.sock
|
||||
conf=/etc/docker/daemon.json
|
||||
gray= reset=
|
||||
[ -t 2 ] && gray=$'\033[90m' reset=$'\033[0m'
|
||||
say() { printf '%s%s%s\n' "$gray" "docker: $*" "$reset" >&2; }
|
||||
die() { printf '%s\n' "docker: $*" >&2; exit "${rc:-1}"; }
|
||||
sudo=()
|
||||
asroot() { "${sudo[@]}" "$@"; }
|
||||
need_root() {
|
||||
[ "$EUID" = 0 ] && return
|
||||
sudo -n true 2>/dev/null || { rc=127 die "$1 needs root: run it as root once (sudo docker ...)"; }
|
||||
sudo=(sudo -n)
|
||||
}
|
||||
# (/run/docker.pid is root's only)
|
||||
running() { pgrep -x dockerd >/dev/null; }
|
||||
|
||||
# --- install --------------------------------------------------------------------
|
||||
# The data root on a file system that overlay2 can use: the data disk, else
|
||||
# a tmpfs over /var/lib/docker.
|
||||
data_root() {
|
||||
if grep -qx 'upper=data' /run/kw-base 2>/dev/null && mountpoint -q /data; then
|
||||
echo "/data/distros/$(cat /usr/lib/kw-distro)/docker"
|
||||
else
|
||||
mountpoint -q /var/lib/docker || asroot sh -c 'mkdir -p /var/lib/docker && mount -t tmpfs -o mode=710 kw-docker /var/lib/docker'
|
||||
fi
|
||||
}
|
||||
write_conf() {
|
||||
local root
|
||||
root=$(data_root)
|
||||
asroot mkdir -p /etc/docker || return
|
||||
{
|
||||
echo '{'
|
||||
[ -z "$root" ] || echo " \"data-root\": \"$root\","
|
||||
cat <<-'EOF'
|
||||
"iptables": false,
|
||||
"ip6tables": false,
|
||||
"bridge": "none",
|
||||
"default-cgroupns-mode": "host",
|
||||
"default-runtime": "kw-crun",
|
||||
"runtimes": { "kw-crun": { "path": "/usr/local/libexec/kw-docker-runtime" } }
|
||||
}
|
||||
EOF
|
||||
} | asroot tee "$conf" >/dev/null
|
||||
}
|
||||
installed() { [ -x $real ] && [ -x /usr/sbin/dockerd ] && [ -x /usr/bin/crun ]; }
|
||||
if ! installed || [ ! -e $conf ]; then
|
||||
need_root "setting up Docker"
|
||||
installed || awk '$2 == "00000000" { f = 1 } END { exit !f }' /proc/net/route 2>/dev/null ||
|
||||
rc=127 die "not installed yet, and installing it needs a network (this machine has none)"
|
||||
[ -e $conf ] || write_conf || die "cannot write $conf"
|
||||
if ! installed; then
|
||||
# Not on the terminal's input: dpkg would eat what was typed ahead.
|
||||
install() { asroot apt-get install -y -q --no-install-recommends docker.io docker-cli crun </dev/null >&2; }
|
||||
say "${sudo:+sudo }apt-get install docker.io docker-cli crun"
|
||||
printf %s "$gray" >&2
|
||||
install || { asroot apt-get update -q </dev/null >&2 && install; } # the image's lists may be stale
|
||||
ok=$?
|
||||
printf %s "$reset" >&2
|
||||
[ $ok = 0 ] || rc=127 die "installing docker.io, docker-cli and crun failed"
|
||||
[ "$EUID" = 0 ] || asroot usermod -aG docker "$(id -un)" || die "cannot add $(id -un) to the group docker"
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- start ----------------------------------------------------------------------
|
||||
if ! running; then
|
||||
need_root "starting dockerd"
|
||||
# Without a data root in daemon.json: the tmpfs, again after a reboot.
|
||||
grep -q '"data-root"' $conf || data_root >/dev/null
|
||||
say "${sudo:+sudo }service docker start"
|
||||
asroot service docker start >/dev/null || die "service docker start failed"
|
||||
fi
|
||||
for _ in $(seq 600); do
|
||||
[ -S $sock ] && break
|
||||
running || die "dockerd stopped; the end of /var/log/docker.log:"$'\n'"$(tail -n 5 /var/log/docker.log 2>/dev/null)"
|
||||
sleep 0.1
|
||||
done
|
||||
[ -S $sock ] || die "dockerd did not create $sock in 60 s (/var/log/docker.log)"
|
||||
|
||||
# --- network: the guest's, unless the arguments name one --------------------------
|
||||
# The command: the first argument after the CLI's own options.
|
||||
args=("$@")
|
||||
i=0
|
||||
while [ $i -lt ${#args[@]} ]; do
|
||||
case ${args[i]} in
|
||||
--config|-c|--context|-H|--host|-l|--log-level|--tlscacert|--tlscert|--tlskey) i=$((i + 2)) ;;
|
||||
-*) i=$((i + 1)) ;;
|
||||
*) break ;;
|
||||
esac
|
||||
done
|
||||
cmd=${args[i]-}
|
||||
case $cmd in container|image|builder) i=$((i + 1)) cmd="$cmd ${args[i]-}" ;; esac
|
||||
case $cmd in
|
||||
run|create|build|"container run"|"container create"|"image build"|"builder build")
|
||||
net=
|
||||
for a in "${args[@]:i+1}"; do
|
||||
case $a in --network|--network=*|--net|--net=*) net=1; break ;; esac
|
||||
done
|
||||
[ -n "$net" ] || args=("${args[@]:0:i+1}" --network host "${args[@]:i+1}") ;;
|
||||
esac
|
||||
|
||||
# The socket is root:docker 0660: a user just added to the group has it
|
||||
# only from the next login on.
|
||||
if [ -w $sock ] || [ "$EUID" = 0 ]; then
|
||||
exec $real "${args[@]}"
|
||||
fi
|
||||
sudo -n true 2>/dev/null || die "no access to $sock: log in again (the group docker), or use sudo"
|
||||
exec sudo -n -E -u "$(id -un)" -- $real "${args[@]}"
|
||||
28
guest/distro/debian/rootfs/usr/local/libexec/kw-docker-runtime
Executable file
28
guest/distro/debian/rootfs/usr/local/libexec/kw-docker-runtime
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
||||
# Docker's runtime in the Debian image (/etc/docker/daemon.json, written by
|
||||
# the docker shim, /usr/local/bin/docker): crun, called as runc is by
|
||||
# containerd's shim, with two changes to the bundle's config.json first.
|
||||
# - The device cgroup: on cgroup2 it is a BPF program, and the kernel has
|
||||
# no bpf(2). crun goes on without it when no rule denies anything, so the
|
||||
# rules become one: allow every device. runc cannot: it adds rules of its
|
||||
# own that allow only mknod, and then fails.
|
||||
# - "blockIO":{}, which Docker always sets: crun then wants the io
|
||||
# controller, and cgroup2 here has only pids.
|
||||
# Docker writes the spec as compact JSON in Go's field order (resources
|
||||
# starts with devices, whose rules hold no brackets), which the sed
|
||||
# expressions expect; if that changes, crun's own error shows.
|
||||
|
||||
# unkernel: crun without a device cgroup or the io controller.
|
||||
bundle= prev=
|
||||
for a; do
|
||||
case $prev in --bundle|-b) bundle=$a ;; esac
|
||||
case $a in --bundle=*) bundle=${a#--bundle=} ;; esac
|
||||
prev=$a
|
||||
done
|
||||
if [ -n "$bundle" ] && [ -f "$bundle/config.json" ]; then
|
||||
sed -i -e 's/"resources":{"devices":\[[^]]*\]/"resources":{"devices":[{"allow":true,"access":"rwm"}]/' \
|
||||
-e 's/,"blockIO":{}//' "$bundle/config.json" || exit 1
|
||||
fi
|
||||
exec /usr/bin/crun "$@"
|
||||
@@ -95,7 +95,7 @@ for (const name of ["alpine", "debian"]) {
|
||||
await poweroff(vm);
|
||||
});
|
||||
|
||||
test("the kw layer's shims: fx, claude, opencode, node, npm, npx, bun, bunx, uv and uvx last on PATH; without a network the first call exits 127 and installs nothing", async (t) => {
|
||||
test("the kw layer's shims: fx, claude, opencode, node, npm, npx, bun, bunx, uv and uvx last on PATH; without a network the first call exits 127 and installs nothing (Debian's docker shim too)", async (t) => {
|
||||
const vm = await boot(t, { distro: img!.file });
|
||||
const tools = ["fx", "claude", "opencode", "node", "npm", "npx", "bun", "bunx", "uv", "uvx"];
|
||||
const which = `for n in ${tools.join(" ")}; do command -v $n; done`;
|
||||
@@ -110,6 +110,14 @@ for (const name of ["alpine", "debian"]) {
|
||||
// Anything else of the name wins: a program in ~/.local/bin (where an install goes).
|
||||
await sh(vm, "mkdir -p ~/.local/bin && ln -s /bin/true ~/.local/bin/fx");
|
||||
assert.equal(await sh(vm, "command -v fx"), `${HOME}/.local/bin/fx`);
|
||||
if (name === "debian") {
|
||||
// The image's docker shim, ahead of /usr/bin; offline it sets up nothing.
|
||||
assert.equal(await sh(vm, "command -v docker"), "/usr/local/bin/docker");
|
||||
const d = await vm.exec("docker", ["ps"], EXEC);
|
||||
assert.equal(d.exitCode, 127, d.stderr);
|
||||
assert.match(d.stderr, /^docker: not installed yet, and installing it needs a network/);
|
||||
assert.equal(await sh(vm, "[ -e /etc/docker ] && echo yes || echo no"), "no");
|
||||
}
|
||||
await poweroff(vm);
|
||||
});
|
||||
|
||||
@@ -378,6 +386,10 @@ for (const name of ["alpine", "debian"]) {
|
||||
const s = await vm.exec("node", ["--version"], { ...EXEC, env: { KW_NODE_VERSION: "24.0.0" } });
|
||||
assert.equal(s.exitCode, 127);
|
||||
assert.match(s.stderr, /node: sudo apk add libgcc \(node needs libgcc_s\.so\.1\)/);
|
||||
} else {
|
||||
const d = await vm.exec("docker", ["ps"], EXEC);
|
||||
assert.equal(d.exitCode, 127, d.stderr);
|
||||
assert.match(d.stderr, /docker: sudo apt-get install docker\.io docker-cli crun/);
|
||||
}
|
||||
await poweroff(vm);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user