diff --git a/.gitea/workflows/os.yml b/.gitea/workflows/os.yml index c02a62e..c57be0c 100644 --- a/.gitea/workflows/os.yml +++ b/.gitea/workflows/os.yml @@ -28,8 +28,8 @@ jobs: - name: Compress working-directory: mkosi.output run: | - zstd -q --rm -T0 -19 git-runner_*.usr-*.raw - sha256sum git-runner_*.efi git-runner_*.usr-*.raw.zst > SHA256SUMS + zstd -q --rm -T0 -19 git-runner_*.root-*.raw + sha256sum git-runner_*.efi git-runner_*.root-*.raw.zst > SHA256SUMS - name: Find the release run: | release=$(curl -fsS -H "$AUTH" "$API/tags/latest") || diff --git a/README.md b/README.md index 2d67759..632083d 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,13 @@ A [Gitea Actions](https://docs.gitea.com/usage/actions/overview) runner packaged - **Small:** Fedora with podman, networking and zram swap, for VMs only: no firmware or drivers for real hardware, no package manager or sshd. About 300 MB of RAM idle; boots in 512 MB. - **Stateless:** container storage is wiped on every boot. - **Isolated jobs:** each job runs in a container of its own (privileged, so it can build OS images), with its own podman; jobs never get the runner's podman. The job image ships with the OS, and common base images are shared read-only, so jobs don't download them. A job that runs the VM out of memory is killed (systemd-oomd); the runner keeps running. -- **Self-updating:** images are rebuilt weekly and on every push. Hosts download updates with systemd-sysupdate into a second /usr partition (A/B) and reboot into them daily at about 04:10, after letting running jobs finish. -- **Read-only /usr:** systemd-boot starts a UKI that mounts /usr from a read-only erofs partition, checked by dm-verity. /etc and /var live on the root partition, which updates keep. +- **Self-updating:** images are rebuilt weekly and on every push. Hosts download updates with systemd-sysupdate into a second root partition (A/B) and reboot into them daily at about 04:10, after letting running jobs finish. +- **Read-only system:** systemd-boot starts a UKI that mounts the root filesystem, /etc and /usr included, from a read-only erofs partition checked by dm-verity. Updates replace it whole, so nothing drifts. All state (the runner registration, the root password, logs) lives on /var, its own partition. - **Hardened:** a firewall that drops all incoming traffic, and KSPP sysctls and kernel arguments. ## Install -Download [the disk image](https://git.plabble.org/Job79/git-runner-mkosi/releases/download/latest/git-runner.qcow2) (built weekly by the `os` workflow), resize it to the disk size you want (the root partition grows to fill it) and boot it with UEFI. The console asks for a root password, hostname, Gitea URL, registration token and number of parallel jobs; the runner then registers itself. +Download [the disk image](https://git.plabble.org/Job79/git-runner-mkosi/releases/download/latest/git-runner.qcow2) (built weekly by the `os` workflow), resize it to the disk size you want (/var grows to fill it) and boot it with UEFI. The console asks for a root password, hostname, Gitea URL, registration token and number of parallel jobs; the runner then registers itself. ## Images @@ -28,7 +28,7 @@ Download [the disk image](https://git.plabble.org/Job79/git-runner-mkosi/release mkosi build ``` -`mkosi.output/` then holds the disk image (`git-runner_.raw`), and the UKI and /usr partitions that the `os` workflow publishes as an update. To boot the disk image (needs `qemu-kvm-core edk2-ovmf`): +`mkosi.output/` then holds the disk image (`git-runner_.raw`), and the UKI and root partitions that the `os` workflow publishes as an update. To boot the disk image (needs `qemu-kvm-core edk2-ovmf`): ```sh qemu-img convert -O qcow2 mkosi.output/git-runner_.raw test.qcow2 && qemu-img resize test.qcow2 40G diff --git a/mkosi.conf b/mkosi.conf index d6cd3c3..104d1e2 100644 --- a/mkosi.conf +++ b/mkosi.conf @@ -1,6 +1,8 @@ # git-runner: a Fedora VM image that runs the Gitea Actions runner, built with mkosi. # mkosi.extra/ is copied to /, mkosi.repart/ defines the disk, mkosi.prepare adds the runner, # mkosi.images/job/ is the job image. +# The root filesystem, /etc included, is read-only and replaced whole by updates; all state +# lives on /var. [Config] Dependencies=job @@ -13,7 +15,7 @@ Release=45 Format=disk ImageId=git-runner OutputDirectory=mkosi.output -# The UKI and /usr partitions are published as updates (systemd-sysupdate). +# The UKI and root partitions are published as updates (systemd-sysupdate). SplitArtifacts=uki,partitions [Content] @@ -47,13 +49,14 @@ WithDocs=no Locale=C.UTF-8 # The UKI's file name in the ESP, as systemd-sysupdate names the ones it installs. UnifiedKernelImageFormat=%i_%v -# A writable root, the console on screen and serial port, no systemd-firstboot questions -# (gitea-runner-setup asks its own), and KSPP hardening (the last four). +# The console on screen and serial port, the machine ID from the VM's firmware (/etc is read-only; +# mkosi.extra/etc/machine-id is empty, so no boot counts as the first), no systemd-firstboot +# questions (gitea-runner-setup asks its own), and KSPP hardening (the last four). KernelCommandLine= - rw console=tty0 console=ttyS0,115200n8 quiet + systemd.machine_id=firmware systemd.firstboot=no slab_nomerge init_on_alloc=1 @@ -90,8 +93,7 @@ KernelModules= ipvlan # - System.map only for debugging the kernel # - udev hwdb hardware names and quirks, nothing a VM needs -# - homed, userdbd, nsresourced unused services; removing their units also stops them -# on hosts installed before (/etc keeps them enabled) +# - homed, userdbd, nsresourced unused services RemoveFiles= /usr/lib/modules/*/System.map /usr/lib/udev/hwdb.bin diff --git a/mkosi.extra/etc/fstab b/mkosi.extra/etc/fstab new file mode 100644 index 0000000..6069787 --- /dev/null +++ b/mkosi.extra/etc/fstab @@ -0,0 +1,2 @@ +# /var, the only writable partition (mkosi.repart/30-var.conf). Grows to fill its partition at boot. +PARTLABEL=var /var ext4 x-systemd.growfs 0 2 diff --git a/mkosi.extra/etc/machine-id b/mkosi.extra/etc/machine-id new file mode 100644 index 0000000..e69de29 diff --git a/mkosi.extra/usr/lib/gitea-runner/config.yaml b/mkosi.extra/usr/lib/gitea-runner/config.yaml index daa7ad4..d478484 100644 --- a/mkosi.extra/usr/lib/gitea-runner/config.yaml +++ b/mkosi.extra/usr/lib/gitea-runner/config.yaml @@ -1,7 +1,7 @@ # The runner's configuration (gitea-runner.service). # Every option: `gitea-runner config generate`. runner: - # Parallel jobs, asked at first boot (stored in /etc/gitea-runner/runner.env). + # Parallel jobs, asked at first boot (stored in /var/lib/gitea-runner/runner.env). capacity: ${GITEA_RUNNER_CAPACITY} # On stop (before an update reboot), take no new jobs and give running ones this long to finish. shutdown_timeout: 30m diff --git a/mkosi.extra/usr/lib/repart.d/50-root.conf b/mkosi.extra/usr/lib/repart.d/50-root.conf deleted file mode 100644 index 5773211..0000000 --- a/mkosi.extra/usr/lib/repart.d/50-root.conf +++ /dev/null @@ -1,4 +0,0 @@ -# Grow the root partition to fill the disk at boot (systemd-repart); systemd-growfs then grows -# its filesystem (GrowFileSystem= in mkosi.repart/30-root.conf). -[Partition] -Type=root diff --git a/mkosi.extra/usr/lib/repart.d/50-var.conf b/mkosi.extra/usr/lib/repart.d/50-var.conf new file mode 100644 index 0000000..9d0525a --- /dev/null +++ b/mkosi.extra/usr/lib/repart.d/50-var.conf @@ -0,0 +1,4 @@ +# Grow the /var partition to fill the disk at boot (systemd-repart); x-systemd.growfs in /etc/fstab +# then grows its filesystem. +[Partition] +Type=var diff --git a/mkosi.extra/usr/lib/systemd/network/80-dhcp.network b/mkosi.extra/usr/lib/systemd/network/80-dhcp.network index f00e17f..e55e9dd 100644 --- a/mkosi.extra/usr/lib/systemd/network/80-dhcp.network +++ b/mkosi.extra/usr/lib/systemd/network/80-dhcp.network @@ -6,3 +6,7 @@ Kind=!* [Network] DHCP=yes + +[DHCPv4] +# The hostname is the runner's name (gitea-runner.service), not the DHCP server's. +UseHostname=no diff --git a/mkosi.extra/usr/lib/systemd/system-preset/50-git-runner.preset b/mkosi.extra/usr/lib/systemd/system-preset/50-git-runner.preset index 206f49b..e0b5ca3 100644 --- a/mkosi.extra/usr/lib/systemd/system-preset/50-git-runner.preset +++ b/mkosi.extra/usr/lib/systemd/system-preset/50-git-runner.preset @@ -4,6 +4,7 @@ enable systemd-resolved.service enable nftables.service enable podman.socket enable gitea-runner-setup.service +enable etc-shadow.mount enable gitea-runner.service enable systemd-sysupdate-update.timer enable systemd-sysupdate-reboot.timer diff --git a/mkosi.extra/usr/lib/systemd/system/etc-shadow.mount b/mkosi.extra/usr/lib/systemd/system/etc-shadow.mount new file mode 100644 index 0000000..10f1807 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/etc-shadow.mount @@ -0,0 +1,14 @@ +# The root password. /etc is read-only, so gitea-runner-setup writes the shadow file to /var and +# this mounts it over /etc/shadow. +[Unit] +Description=Root password from /var +ConditionPathExists=/var/lib/shadow/etc/shadow +RequiresMountsFor=/var + +[Mount] +What=/var/lib/shadow/etc/shadow +Where=/etc/shadow +Options=bind + +[Install] +WantedBy=local-fs.target diff --git a/mkosi.extra/usr/lib/systemd/system/gitea-runner-setup.service b/mkosi.extra/usr/lib/systemd/system/gitea-runner-setup.service index b82ed23..fe94044 100644 --- a/mkosi.extra/usr/lib/systemd/system/gitea-runner-setup.service +++ b/mkosi.extra/usr/lib/systemd/system/gitea-runner-setup.service @@ -1,7 +1,7 @@ # First boot: asks for the root password and the runner's registration on the console (tty1). [Unit] Description=Gitea runner first-boot setup -ConditionPathExists=!/etc/gitea-runner/runner.env +ConditionPathExists=!/var/lib/gitea-runner/runner.env Before=getty@tty1.service kmsconvt@tty1.service [Service] diff --git a/mkosi.extra/usr/lib/systemd/system/gitea-runner.service b/mkosi.extra/usr/lib/systemd/system/gitea-runner.service index a155a55..d7dc9dd 100644 --- a/mkosi.extra/usr/lib/systemd/system/gitea-runner.service +++ b/mkosi.extra/usr/lib/systemd/system/gitea-runner.service @@ -11,8 +11,10 @@ Requires=podman.socket # never get the socket (see config.yaml). User=gitea-runner WorkingDirectory=/var/lib/gitea-runner -EnvironmentFile=/etc/gitea-runner/runner.env +EnvironmentFile=/var/lib/gitea-runner/runner.env Environment=DOCKER_HOST=unix:///run/podman/podman.sock +# The hostname is the runner's name. /etc is read-only, so it is set on every start (as root: +). +ExecStartPre=+/usr/bin/hostnamectl hostname --transient ${GITEA_RUNNER_NAME} # Register once; the result is kept in .runner. ExecStartPre=/bin/sh -c '[ -f .runner ] || exec gitea-runner register --no-interactive --config /usr/lib/gitea-runner/config.yaml --instance "$GITEA_INSTANCE_URL"' ExecStart=/usr/bin/gitea-runner daemon --config /usr/lib/gitea-runner/config.yaml diff --git a/mkosi.extra/usr/lib/sysupdate.d/10-usr.transfer b/mkosi.extra/usr/lib/sysupdate.d/10-root.transfer similarity index 68% rename from mkosi.extra/usr/lib/sysupdate.d/10-usr.transfer rename to mkosi.extra/usr/lib/sysupdate.d/10-root.transfer index 0c5a562..3275eb7 100644 --- a/mkosi.extra/usr/lib/sysupdate.d/10-usr.transfer +++ b/mkosi.extra/usr/lib/sysupdate.d/10-root.transfer @@ -1,4 +1,4 @@ -# Updates /usr: writes the next version to the unused /usr partition. +# Updates the root filesystem: writes the next version to the unused root partition. # Verify=no: releases are unsigned (no secrets in the repo); SHA256SUMS still checks each download. [Transfer] ProtectVersion=%A @@ -7,12 +7,12 @@ Verify=no [Source] Type=url-file Path=https://git.plabble.org/Job79/git-runner-mkosi/releases/download/latest/ -MatchPattern=git-runner_@v.usr-%a.@u.raw.zst +MatchPattern=git-runner_@v.root-%a.@u.raw.zst [Target] Type=partition Path=auto MatchPattern=git-runner_@v -MatchPartitionType=usr +MatchPartitionType=root PartitionFlags=0 ReadOnly=1 diff --git a/mkosi.extra/usr/lib/sysupdate.d/11-usr-verity.transfer b/mkosi.extra/usr/lib/sysupdate.d/11-root-verity.transfer similarity index 56% rename from mkosi.extra/usr/lib/sysupdate.d/11-usr-verity.transfer rename to mkosi.extra/usr/lib/sysupdate.d/11-root-verity.transfer index f91a799..938a8e7 100644 --- a/mkosi.extra/usr/lib/sysupdate.d/11-usr-verity.transfer +++ b/mkosi.extra/usr/lib/sysupdate.d/11-root-verity.transfer @@ -1,4 +1,4 @@ -# Updates the dm-verity hashes of /usr, next to 10-usr.transfer (which explains Verify=no). +# Updates the root filesystem's dm-verity hashes, next to 10-root.transfer (see it for Verify=no). [Transfer] ProtectVersion=%A Verify=no @@ -6,12 +6,12 @@ Verify=no [Source] Type=url-file Path=https://git.plabble.org/Job79/git-runner-mkosi/releases/download/latest/ -MatchPattern=git-runner_@v.usr-%a-verity.@u.raw.zst +MatchPattern=git-runner_@v.root-%a-verity.@u.raw.zst [Target] Type=partition Path=auto MatchPattern=git-runner_@v_verity -MatchPartitionType=usr-verity +MatchPartitionType=root-verity PartitionFlags=0 ReadOnly=1 diff --git a/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer b/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer index cba382f..8772bcc 100644 --- a/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer +++ b/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer @@ -1,4 +1,4 @@ -# Installs the next UKI, which systemd-boot boots by default. Verify=no: see 10-usr.transfer. +# Installs the next UKI, which systemd-boot boots by default. Verify=no: see 10-root.transfer. [Transfer] ProtectVersion=%A Verify=no diff --git a/mkosi.extra/usr/libexec/gitea-runner-setup b/mkosi.extra/usr/libexec/gitea-runner-setup index fe90966..960f39d 100755 --- a/mkosi.extra/usr/libexec/gitea-runner-setup +++ b/mkosi.extra/usr/libexec/gitea-runner-setup @@ -1,5 +1,7 @@ #!/bin/bash -# First boot: ask for the root password and the runner's registration. +# First boot: asks for the root password and the runner's registration on the console (tty1). +# /etc is read-only, so the answers go to /var: the password to /var/lib/shadow (etc-shadow.mount +# puts it over /etc/shadow), the rest to the runner's runner.env. set -euo pipefail echo "== Gitea runner setup ==" @@ -9,15 +11,18 @@ while :; do [[ -n $pw && $pw == "$pw2" ]] && break echo "Passwords do not match, try again." done -echo "root:$pw" | chpasswd +install -d -m 0700 /var/lib/shadow/etc +cp /etc/passwd /etc/shadow /var/lib/shadow/etc/ +echo "root:$pw" | chpasswd --prefix /var/lib/shadow +systemctl start etc-shadow.mount read -rp "Hostname (also the runner name): " name read -rp "Gitea URL (e.g. https://gitea.example.com): " url read -rp "Runner registration token: " token read -rp "Parallel jobs [1]: " capacity [[ $capacity =~ ^[1-9][0-9]*$ ]] || capacity=1 -hostnamectl hostname "$name" -install -D -m 0640 -g gitea-runner /dev/stdin /etc/gitea-runner/runner.env <