From c80e843cb5dd97d0d230cfa2ea0c51417002dea3 Mon Sep 17 00:00:00 2001 From: Job79 Date: Thu, 19 Nov 2026 21:09:31 +0100 Subject: [PATCH] feat: ParticleOS-like layout: /usr image, encrypted btrfs root The image now holds only the ESP and a verity-protected /usr (A/B). At first boot, systemd-repart in the initrd makes the root filesystem: btrfs, encrypted with a TPM key sealed to the signed PCR 11 policy, with /var as its own subvolume. A TPM is required. systemd.image_policy= accepts only an encrypted root and a verity-checked /usr, and FactoryReset=yes lets factory-reset.target wipe it. /etc is writable but nearly empty: mkosi.finalize keeps the build's /etc in /usr/share/factory/etc and systemd-tmpfiles links it in at boot. That removes etc-shadow.mount, the host key on /var, fstab and /var/lib/agent. /var now survives reboots. systemd-firstboot replaces agent-setup: it asks for the root password on the screen and offers a recovery key. Machines are named agent-???? from the kernel command line. The user is now agent, with its home in /home/agent and ssh keys in the default places. sshd.path starts sshd only once root or agent has a key. The test VM needs swtpm. The package cache moves to a podman volume (AGENTS.md). No existing VMs can update to this layout. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/os.yml | 10 +-- .gitignore | 1 - AGENTS.md | 16 +++-- README.md | 40 ++++++----- mkosi.conf | 71 ++++++++++--------- mkosi.extra/etc/claude-code/CLAUDE.md | 8 ++- mkosi.extra/etc/environment | 5 -- mkosi.extra/etc/fstab | 11 --- mkosi.extra/etc/machine-id | 0 .../etc/ssh/sshd_config.d/10-agent.conf | 22 ------ mkosi.extra/etc/subgid | 2 +- mkosi.extra/etc/subuid | 2 +- mkosi.extra/usr/lib/repart.d/50-root.conf | 18 +++++ mkosi.extra/usr/lib/repart.d/50-var.conf | 6 -- .../usr/lib/systemd/network/80-dhcp.network | 5 -- .../lib/systemd/system-preset/50-agent.preset | 15 ++-- .../usr/lib/systemd/system/agent-boot.service | 17 ----- .../lib/systemd/system/agent-setup@.service | 25 ------- .../usr/lib/systemd/system/claude-remote.path | 2 +- .../lib/systemd/system/claude-remote.service | 11 ++- .../usr/lib/systemd/system/etc-shadow.mount | 16 ----- mkosi.extra/usr/lib/systemd/system/sshd.path | 14 ++++ .../systemd/system/sshd.service.d/agent.conf | 7 -- .../{10-root.transfer => 10-usr.transfer} | 8 +-- ...verity.transfer => 11-usr-verity.transfer} | 8 +-- .../usr/lib/sysupdate.d/20-uki.transfer | 4 +- .../sysusers.d/{claude.conf => agent.conf} | 4 +- mkosi.extra/usr/lib/tmpfiles.d/agent.conf | 22 ++---- mkosi.extra/usr/libexec/agent-boot | 12 ---- mkosi.extra/usr/libexec/agent-setup | 52 -------------- mkosi.finalize | 42 +++++++++++ mkosi.repart/10-root.conf | 19 ----- mkosi.repart/10-usr.conf | 19 +++++ mkosi.repart/11-root-verity.conf | 11 --- mkosi.repart/11-usr-verity.conf | 11 +++ .../{20-root-b.conf => 20-usr-b.conf} | 4 +- mkosi.repart/21-root-verity-b.conf | 9 --- mkosi.repart/21-usr-verity-b.conf | 9 +++ mkosi.repart/30-var.conf | 13 ---- 39 files changed, 229 insertions(+), 342 deletions(-) delete mode 100644 mkosi.extra/etc/environment delete mode 100644 mkosi.extra/etc/fstab delete mode 100644 mkosi.extra/etc/machine-id delete mode 100644 mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf create mode 100644 mkosi.extra/usr/lib/repart.d/50-root.conf delete mode 100644 mkosi.extra/usr/lib/repart.d/50-var.conf delete mode 100644 mkosi.extra/usr/lib/systemd/system/agent-boot.service delete mode 100644 mkosi.extra/usr/lib/systemd/system/agent-setup@.service delete mode 100644 mkosi.extra/usr/lib/systemd/system/etc-shadow.mount create mode 100644 mkosi.extra/usr/lib/systemd/system/sshd.path delete mode 100644 mkosi.extra/usr/lib/systemd/system/sshd.service.d/agent.conf rename mkosi.extra/usr/lib/sysupdate.d/{10-root.transfer => 10-usr.transfer} (71%) rename mkosi.extra/usr/lib/sysupdate.d/{11-root-verity.transfer => 11-usr-verity.transfer} (57%) rename mkosi.extra/usr/lib/sysusers.d/{claude.conf => agent.conf} (61%) delete mode 100755 mkosi.extra/usr/libexec/agent-boot delete mode 100755 mkosi.extra/usr/libexec/agent-setup create mode 100755 mkosi.finalize delete mode 100644 mkosi.repart/10-root.conf create mode 100644 mkosi.repart/10-usr.conf delete mode 100644 mkosi.repart/11-root-verity.conf create mode 100644 mkosi.repart/11-usr-verity.conf rename mkosi.repart/{20-root-b.conf => 20-usr-b.conf} (78%) delete mode 100644 mkosi.repart/21-root-verity-b.conf create mode 100644 mkosi.repart/21-usr-verity-b.conf delete mode 100644 mkosi.repart/30-var.conf diff --git a/.gitea/workflows/os.yml b/.gitea/workflows/os.yml index 92fce6b..ff54d16 100644 --- a/.gitea/workflows/os.yml +++ b/.gitea/workflows/os.yml @@ -38,21 +38,23 @@ jobs: - name: Compress working-directory: mkosi.output run: | - zstd -q --rm -9 agent_*.root-*.raw - qemu-img convert -c -m 16 -W -O qcow2 "$(basename agent_*.efi .efi).raw" agent.qcow2 - sha256sum agent_*.efi agent_*.root-*.raw.zst > SHA256SUMS + zstd -q --rm -9 agent_*.usr-*.raw - name: Sign working-directory: mkosi.output env: KEY: ${{ secrets.SIGNING_KEY }} run: | + sha256sum agent_*.efi agent_*.usr-*.raw.zst > SHA256SUMS export GNUPGHOME=$(mktemp -d) gpg -q --batch --import <<< "$KEY" gpg -q --batch -o SHA256SUMS.gpg --detach-sign SHA256SUMS + - name: Build qcow2 + working-directory: mkosi.output + run: qemu-img convert -c -O qcow2 "$(basename agent_*.efi .efi).raw" agent.qcow2 - name: Publish working-directory: mkosi.output run: | curl -fsS -H "$AUTH" -X DELETE "$PKG" || true - for f in agent_*.efi agent_*.root-*.raw.zst agent.qcow2 agent_*.manifest SHA256SUMS.gpg SHA256SUMS; do + for f in agent_*.efi agent_*.usr-*.raw.zst agent.qcow2 agent_*.manifest SHA256SUMS.gpg SHA256SUMS; do curl -fsS -o /dev/null -H "$AUTH" --upload-file "$f" "$PKG/$f" done diff --git a/.gitignore b/.gitignore index 4392209..c2947e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ mkosi.output/ mkosi.key -mkosi.pkgcache/ diff --git a/AGENTS.md b/AGENTS.md index 0a2926b..7fc88aa 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,8 +7,8 @@ every change. - Keep it simple. Prefer fewer files, fewer lines and the defaults. Delete what isn't needed. -- Divide code and config into parts (files, directories, config sections) that - each stand on their own: reading one alone tells you what it does and why. +- Divide code and config into parts (files, directories, sections) that each + stand on their own: reading one part alone tells you what it does and why. - Each fact lives in one place. Elsewhere, refer to it by path instead of repeating it. - Write plain English in short sentences. @@ -40,8 +40,8 @@ the file that uses them. write clean code instead of many comments. - Put a comment right above the line or block it is about. - Write full sentences that start with the name of what they describe: - `# PackageCacheDirectory keeps downloaded packages between builds.` or - `# cred prints the credential agent., and fails if it isn't passed.` + `# ManifestFormat lists the packages and versions in the image.` or + `# link prints the tmpfiles line for /etc/$1.` - Wrap comments at 80 columns. ## Testing @@ -52,22 +52,24 @@ run mkosi in a privileged podman container that boots systemd: ```sh podman build -t mkosi-vm - <<'END' FROM registry.fedoraproject.org/fedora:45 -RUN dnf install -y mkosi qemu-kvm-core edk2-ovmf systemd-repart \ +RUN dnf install -y mkosi qemu-kvm-core edk2-ovmf systemd-repart swtpm \ openssh-clients socat && \ dnf install -y $(mkosi dependencies) CMD ["/sbin/init"] END podman run -d --name mkosi --privileged --systemd=always -v :/repo \ - -w /repo mkosi-vm + -v mkosi-cache:/root/.cache/mkosi -w /repo mkosi-vm podman exec mkosi mkosi genkey # once, if there is no mkosi.key podman exec -d -t mkosi sh -c 'mkosi -f vm > mkosi.output/vm.log 2>&1' podman exec mkosi mkosi ssh -- systemctl --failed ``` podman resolves paths on the host, so `` is the repository's path there. +The volume `mkosi-cache` keeps mkosi's package cache when the container is +made again, so builds don't download every package anew. `mkosi vm` needs a terminal, hence `-d -t`; its console goes to the log. `mkosi ssh` runs a command as root, once the VM has booted. For the user -`claude`, run `su - claude -c ''`. +`agent`, run `su - agent -c ''`. `mkosi.version` gives every run a new version, so `mkosi vm` without `-f` fails with "has not been built yet". To boot an existing build without a rebuild, pin diff --git a/README.md b/README.md index a2e02cb..817b1d1 100644 --- a/README.md +++ b/README.md @@ -5,24 +5,30 @@ It is a minimal Fedora image built with [mkosi](https://github.com/systemd/mkosi). You control Claude from claude.ai/code or the Claude app (Remote Control), or log in with ssh. -- **Resets at reboot:** the homes of Claude and root, `/tmp` and all containers - are wiped at every boot. Only the setup answers, the ssh keys and Claude's - login survive (`/var/lib/agent`). -- **Read-only OS:** the root filesystem is read-only, so Claude can't change - the OS. All state lives on `/var`. -- **Tools in containers:** Claude runs as the user `claude`, without root, and - runs its toolchains in containers with podman. The host has only git, ssh and - basic command-line tools. -- **Self-updating:** the `os` rebuilds weekly and updates with - systemd-sysupdate. +- **Read-only OS:** `/usr` is read-only, so Claude can't change the OS. Secure + Boot verifies the UKI (kernel, initrd and command line), which holds the + dm-verity hash that verifies `/usr`. +- **Encrypted with a TPM:** everything that isn't read-only, such as `/etc`, + `/var` and `/home`, is encrypted. The TPM unlocks it only for those signed + UKIs. +- **Tools in containers:** Claude runs as the user `agent`, without root, and + runs its toolchains in containers with podman. +- **Self-updating:** the image rebuilds weekly. A bad update rolls back by + itself. ## Install Download [the disk image](https://git.plabble.org/api/packages/Job79/generic/agent/latest/agent.qcow2), -resize it to the disk size you want and boot it with UEFI. At first boot, the -console asks for a root password and a hostname. Then log in as root, add an -ssh key for the user `claude` and run Claude once to sign in. The VM then shows -up in claude.ai/code and the Claude app under its hostname. +resize it to the disk size you want and boot it with UEFI and a TPM. At first +boot, the screen offers a recovery key for the encrypted disk and asks for a +root password. Then log in as root and put your ssh key in +`/home/agent/.ssh/authorized_keys` (for root: `~/.ssh/authorized_keys`), which +starts sshd. Run `claude` once as `agent` to sign in. The VM then shows up in +claude.ai/code and the Claude app under its hostname: `agent-` and four hex +digits. + +To start over, run `systemctl start factory-reset.target` as root. It reboots +and wipes `/etc` and `/var`, and the VM sets itself up as at first boot. ## Development @@ -32,10 +38,10 @@ mkosi build ``` `mkosi.output/` then holds the disk image (`agent_.raw`) and the UKI -and root partitions that make up an update. +and /usr partitions that make up an update. -To build and boot a test VM (needs `qemu-kvm-core edk2-ovmf openssh-clients -socat`): +To build and boot a test VM (needs `qemu-kvm-core edk2-ovmf swtpm +openssh-clients socat`): ```sh mkosi -f vm # opens its console: log in as root, password root diff --git a/mkosi.conf b/mkosi.conf index 2083e69..c1779e2 100644 --- a/mkosi.conf +++ b/mkosi.conf @@ -2,9 +2,10 @@ # # A Fedora VM image that runs Claude Code in the background. mkosi.extra/ is # copied to /, mkosi.repart/ defines the disk and mkosi.sandbox/ adds the -# Claude Code package repository. The root filesystem, /etc included, is -# read-only and replaced whole by updates. All state lives on /var. Claude -# can't change the OS, so it runs its tools in containers. +# Claude Code package repository. /usr is read-only and replaced whole by +# updates. The root filesystem holds the state: /var, and an /etc that is +# mostly links into /usr (mkosi.finalize). Claude can't change the OS, so it +# runs its tools in containers. # mkosi.extra/etc/claude-code/ sets Claude Code up for that: # managed-settings.json lets it run without permission prompts, and CLAUDE.md # tells it about this machine. @@ -22,12 +23,6 @@ SplitArtifacts=uki,partitions # builds a CVE affects. .gitea/workflows/os.yml publishes it with the image. ManifestFormat=json -[Build] -# PackageCacheDirectory keeps downloaded packages between builds, also when -# mkosi runs in a throwaway container (AGENTS.md). dnf still installs the -# newest versions. -PackageCacheDirectory=mkosi.pkgcache - [Content] Bootable=yes Bootloader=systemd-boot @@ -81,31 +76,44 @@ Locale=C.UTF-8 # it installs. UnifiedKernelImageFormat=%i_%v # KernelCommandLine sets: -# - console= shows the console on the screen and on the serial port. -# - systemd.machine_id=firmware takes the machine ID from the VM's firmware. -# mkosi.extra/etc/machine-id is empty, so no boot counts as the first. -# - systemd.firstboot=no leaves the questions to agent-setup. +# - console= shows the console on the serial port and the screen. The last +# one, the screen, is /dev/console, where systemd-firstboot asks for the root +# password at first boot. +# - rw mounts the root filesystem writable. The initrd finds it by its +# partition type (mkosi.extra/usr/lib/repart.d/50-root.conf). +# - systemd.hostname= names each machine agent- and four hex digits from its +# machine ID, from the initrd on. It wins over /etc/hostname, so hostnamectl +# renames only until the next boot. +# - systemd.image_policy= makes the initrd use only an encrypted root +# filesystem (or none yet, at first boot) and a verity-checked /usr. +# Otherwise someone with the disk could put in a plain root filesystem with +# their own /etc. Our UKI would boot it, and with PCR 11 unchanged, their +# code could unseal the key of the real one. # - SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST lets systemd handle more mount # changes per second. At the default of 5, it holds back mounts at boot and -# delays / and /var by up to a second each. The kernel passes it to systemd +# delays / and /usr by up to a second each. The kernel passes it to systemd # as environment. KernelCommandLine= - console=tty0 console=ttyS0,115200n8 + console=tty0 quiet - systemd.machine_id=firmware - systemd.firstboot=no + rw + systemd.hostname=agent-???? + systemd.image_policy=root=encrypted+absent:usr=verity:esp=unprotected SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST=50 -# KernelInitrdModules are the modules the initrd needs to find and mount the -# disks of a VM. +# KernelInitrdModules are the modules the initrd needs to find the disks of a +# VM, mount /usr and make, unlock and mount the root filesystem. KernelInitrdModules= virtio_blk virtio_scsi sd_mod nvme + tpm_tis + tpm_crb dm-verity + dm-crypt erofs - ext4 + btrfs # KernelModules keeps all modules but the hardware drivers, then the drivers a # VM needs (disks, network, screen) and those of rootless podman's networks # (pasta). The last matching line wins. Keyboard, USB and a basic screen are @@ -129,12 +137,6 @@ KernelModules= # - System.map is only for debugging the kernel. # - The udev hwdb holds hardware names and quirks, nothing a VM needs. # - homed, userdbd and nsresourced are unused services. -# - sshd-keygen would write host keys to the read-only /etc. sshd uses one host -# key on /var instead (mkosi.extra/etc/ssh/sshd_config.d). -# - /afs is the mount point for the AFS network filesystem. -# - /buildroot holds dnf's lock file, left by mkosi: it passes dnf5 -# persistdir=/buildroot/var/lib/dnf and dnf5 puts the install root in front -# of it again. RemoveFiles= /usr/lib/modules/*/System.map /usr/lib/udev/hwdb.bin @@ -142,9 +144,6 @@ RemoveFiles= /usr/lib/systemd/system/systemd-homed* /usr/lib/systemd/system/systemd-userdbd.* /usr/lib/systemd/system/systemd-nsresourced.* - /usr/lib/systemd/system/sshd-keygen* - /afs - /buildroot [Validation] # SecureBoot signs systemd-boot and the UKI, and puts our keys on the ESP. @@ -154,12 +153,17 @@ RemoveFiles= SecureBoot=yes SecureBootKey=mkosi.key SecureBootCertificate=mkosi.crt +# SignExpectedPcr signs the PCR 11 values that each UKI will measure, with +# mkosi.key. The TPM then unlocks the root filesystem only for UKIs we signed, +# also after updates (mkosi.extra/usr/lib/repart.d/50-root.conf). +SignExpectedPcr=yes [Runtime] # Runtime sets up the test VM of mkosi vm (AGENTS.md). Ephemeral boots a copy, -# so test runs don't change the built image. Credentials answer the setup -# (mkosi.extra/usr/libexec/agent-setup). mkosi ssh logs in as root with -# mkosi.key, which mkosi also authorizes for root in the VM. +# so test runs don't change the built image. Credentials sets the root +# password, and systemd.firstboot=no skips the questions at first boot, such +# as the offer of a recovery key. mkosi ssh logs in as root with mkosi.key, +# which mkosi also authorizes for root in the VM. Ephemeral=yes CPUs=4 RAM=8G @@ -168,4 +172,5 @@ RuntimeSize=40G # doesn't use hvc0: with Secure Boot, systemd-stub ignores the console= mkosi # passes. QemuArgs=-serial chardev:console -Credentials=agent.password=root agent.hostname=agent-test +Credentials=passwd.plaintext-password.root=root +KernelCommandLineExtra=systemd.firstboot=no diff --git a/mkosi.extra/etc/claude-code/CLAUDE.md b/mkosi.extra/etc/claude-code/CLAUDE.md index 7f07e54..01d5b60 100644 --- a/mkosi.extra/etc/claude-code/CLAUDE.md +++ b/mkosi.extra/etc/claude-code/CLAUDE.md @@ -9,6 +9,8 @@ You run on a small Fedora VM made for you, as the user `claude`, without root. `podman run -d --name dev -v ~:/w -w /w quay.io/fedora/fedora:45 sleep infinity`, then `podman exec dev dnf install -y gcc` and `podman exec dev make`. - On the host: git, ssh, curl, tar/gzip/xz/zstd, find, diff, ps, ip. -- **Everything resets at reboot**: your home (/var/home/claude), /tmp and all containers and - images. Only your Claude login survives. Keep results somewhere else (push them to git, for - example). The VM may reboot at about 04:10 to update. +- **Your files survive reboots**: your home (/home/claude), containers and images stay. + /tmp is wiped at every boot. The VM may reboot at about 04:10 to update. +- **To start over**, push what you want to keep, then remove all containers and images with + `podman system reset --force` and delete everything in your home but `~/.claude` and + `~/.claude.json`, which hold your Claude login. diff --git a/mkosi.extra/etc/environment b/mkosi.extra/etc/environment deleted file mode 100644 index 2aba1d8..0000000 --- a/mkosi.extra/etc/environment +++ /dev/null @@ -1,5 +0,0 @@ -# Environment -# -# Claude's config, with its login, lives on /var/lib/agent instead of in the -# home, because the home is wiped at every boot. -CLAUDE_CONFIG_DIR=/var/lib/agent/claude diff --git a/mkosi.extra/etc/fstab b/mkosi.extra/etc/fstab deleted file mode 100644 index e05cac9..0000000 --- a/mkosi.extra/etc/fstab +++ /dev/null @@ -1,11 +0,0 @@ -# Mounts -# -# /var is the only partition on disk that is written (mkosi.repart/30-var.conf). -# x-systemd.growfs grows its filesystem to fill the partition at boot. noatime -# skips the write that the default (relatime) makes at the first read of a file -# each day. Images that podman unpacks are full of such files; nothing here uses -# access times. -PARTLABEL=var /var ext4 noatime,x-systemd.growfs 0 2 -# /root is in memory, so root has a writable home that is wiped at every boot. -# Root's ssh key lives on /var (mkosi.extra/etc/ssh/sshd_config.d). -tmpfs /root tmpfs mode=0700 0 0 diff --git a/mkosi.extra/etc/machine-id b/mkosi.extra/etc/machine-id deleted file mode 100644 index e69de29..0000000 diff --git a/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf b/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf deleted file mode 100644 index adfdf0f..0000000 --- a/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf +++ /dev/null @@ -1,22 +0,0 @@ -# ssh login -# -# Login is with keys only, for root and the user claude. Neither has a key -# until root adds one (README.md). The first value wins, and this file sorts -# before Fedora's 50-redhat.conf. - -# HostKey lives on /var (agent-boot makes it), so it stays the same across -# reboots. -HostKey /var/lib/agent/ssh_host_ed25519_key -PasswordAuthentication no -KbdInteractiveAuthentication no - -# AuthorizedKeysFile of the user claude is owned by root, so Claude can't add -# keys. -Match User claude - AuthorizedKeysFile /var/lib/agent/authorized_keys - -# AuthorizedKeysFile of root is on /var, because its home is wiped at every -# boot (/etc/fstab). It also reads the default file, which the -# ssh.authorized_keys.root credential fills, such as for mkosi ssh. -Match User root - AuthorizedKeysFile /var/lib/agent/root_authorized_keys .ssh/authorized_keys diff --git a/mkosi.extra/etc/subgid b/mkosi.extra/etc/subgid index 72b2f42..0fa3566 100644 --- a/mkosi.extra/etc/subgid +++ b/mkosi.extra/etc/subgid @@ -1 +1 @@ -claude:524288:65536 +agent:524288:65536 diff --git a/mkosi.extra/etc/subuid b/mkosi.extra/etc/subuid index 72b2f42..0fa3566 100644 --- a/mkosi.extra/etc/subuid +++ b/mkosi.extra/etc/subuid @@ -1 +1 @@ -claude:524288:65536 +agent:524288:65536 diff --git a/mkosi.extra/usr/lib/repart.d/50-root.conf b/mkosi.extra/usr/lib/repart.d/50-root.conf new file mode 100644 index 0000000..b9f8db2 --- /dev/null +++ b/mkosi.extra/usr/lib/repart.d/50-root.conf @@ -0,0 +1,18 @@ +# Root filesystem +# +# systemd-repart makes it in the initrd at first boot, from the free space +# after /usr, and grows it when the disk grows. It holds all state: /etc +# (mkosi.finalize) and /var, which is its own btrfs subvolume. It is encrypted +# with a key in the TPM, so a TPM is required. The key is sealed to the signed +# PCR 11 policy of our UKIs (SignExpectedPcr= in mkosi.conf), and the initrd +# unlocks it at every boot. +# +# FactoryReset makes a factory reset wipe it: root runs systemctl start +# factory-reset.target, and at the next boot it is made anew, as at first boot. +[Partition] +Type=root +Format=btrfs +Subvolumes=/var +MakeDirectories=/var +Encrypt=tpm2 +FactoryReset=yes diff --git a/mkosi.extra/usr/lib/repart.d/50-var.conf b/mkosi.extra/usr/lib/repart.d/50-var.conf deleted file mode 100644 index a74d973..0000000 --- a/mkosi.extra/usr/lib/repart.d/50-var.conf +++ /dev/null @@ -1,6 +0,0 @@ -# Grow /var -# -# systemd-repart grows the /var partition to fill the disk at boot. -# 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 71bfcea..531ec36 100644 --- a/mkosi.extra/usr/lib/systemd/network/80-dhcp.network +++ b/mkosi.extra/usr/lib/systemd/network/80-dhcp.network @@ -8,8 +8,3 @@ Kind=!* [Network] DHCP=yes - -[DHCPv4] -# UseHostname=no keeps the hostname asked at setup (agent-boot) instead of the -# DHCP server's. -UseHostname=no diff --git a/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset b/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset index 4b7379a..0c3ae2a 100644 --- a/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset +++ b/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset @@ -5,19 +5,16 @@ enable systemd-networkd.service enable systemd-resolved.service -enable sshd.service +# sshd.path starts sshd once there is an ssh key, so Fedora's preset of +# sshd.service is overruled. +enable sshd.path +disable sshd.service # getty@.service is the login on the screen, which Fedora disables (tty1, and # tty2-6 through its autovt@ alias). enable getty@.service -enable agent-setup@.service tty1 ttyS0 -enable etc-shadow.mount -enable agent-boot.service enable claude-remote.path enable systemd-sysupdate-update.timer enable systemd-sysupdate-reboot.timer -# These services do nothing here, but Fedora enables them: no PAM or crypto -# changes on a read-only /etc, and no system or config extensions. +# authselect-apply-changes would rewrite /etc/authselect, which links into the +# read-only /usr (mkosi.finalize), and fails. disable authselect-apply-changes.service -disable fips-crypto-policy-overlay.service -disable systemd-sysext.service -disable systemd-confext.service diff --git a/mkosi.extra/usr/lib/systemd/system/agent-boot.service b/mkosi.extra/usr/lib/systemd/system/agent-boot.service deleted file mode 100644 index 5a11df1..0000000 --- a/mkosi.extra/usr/lib/systemd/system/agent-boot.service +++ /dev/null @@ -1,17 +0,0 @@ -# Apply setup -# -# Runs /usr/libexec/agent-boot at every boot. Before the setup is done there -# is nothing to apply, so agent-setup starts this once it is. -[Unit] -Description=Apply agent setup -ConditionPathExists=/var/lib/agent/hostname -After=systemd-tmpfiles-setup.service -Before=sshd.service claude-remote.service - -[Service] -Type=oneshot -RemainAfterExit=yes -ExecStart=/usr/libexec/agent-boot - -[Install] -WantedBy=multi-user.target diff --git a/mkosi.extra/usr/lib/systemd/system/agent-setup@.service b/mkosi.extra/usr/lib/systemd/system/agent-setup@.service deleted file mode 100644 index 08cd9c8..0000000 --- a/mkosi.extra/usr/lib/systemd/system/agent-setup@.service +++ /dev/null @@ -1,25 +0,0 @@ -# First-boot setup -# -# Runs /usr/libexec/agent-setup on the screen (tty1) and the serial console -# (ttyS0) at once, so it can be answered on either. Whichever is answered first -# stops the other. -[Unit] -Description=Agent first-boot setup on %I -ConditionPathExists=!/var/lib/agent/hostname -After=systemd-tmpfiles-setup.service -Before=getty@%i.service serial-getty@%i.service - -[Service] -Type=oneshot -ImportCredential=agent.* -ExecStart=/usr/libexec/agent-setup -StandardInput=tty -StandardOutput=tty -TTYPath=/dev/%I -TTYReset=yes -TTYVHangup=yes -# SuccessExitStatus counts being stopped by the other console as success. -SuccessExitStatus=SIGTERM - -[Install] -WantedBy=multi-user.target diff --git a/mkosi.extra/usr/lib/systemd/system/claude-remote.path b/mkosi.extra/usr/lib/systemd/system/claude-remote.path index ec51bbd..0ffd0f7 100644 --- a/mkosi.extra/usr/lib/systemd/system/claude-remote.path +++ b/mkosi.extra/usr/lib/systemd/system/claude-remote.path @@ -6,7 +6,7 @@ Description=Start Claude Remote Control once logged in [Path] -PathExists=/var/lib/agent/claude/.credentials.json +PathExists=/home/agent/.claude/.credentials.json [Install] WantedBy=multi-user.target diff --git a/mkosi.extra/usr/lib/systemd/system/claude-remote.service b/mkosi.extra/usr/lib/systemd/system/claude-remote.service index b709f32..a1ac01e 100644 --- a/mkosi.extra/usr/lib/systemd/system/claude-remote.service +++ b/mkosi.extra/usr/lib/systemd/system/claude-remote.service @@ -1,17 +1,16 @@ # Claude Remote Control # -# Runs Claude Code in the background, in Claude's home, so it can be used from -# claude.ai/code or the Claude app. claude-remote.path starts it once Claude is -# logged in. +# Runs Claude Code in the background, as the user agent in its home, so it can +# be used from claude.ai/code or the Claude app. claude-remote.path starts it +# once Claude is logged in. [Unit] Description=Claude Code Remote Control Wants=network-online.target user@1000.service -After=network-online.target user@1000.service agent-boot.service +After=network-online.target user@1000.service [Service] -User=claude +User=agent WorkingDirectory=~ -EnvironmentFile=/etc/environment Environment=XDG_RUNTIME_DIR=/run/user/1000 ExecStart=/usr/bin/claude remote-control --name %H Restart=always diff --git a/mkosi.extra/usr/lib/systemd/system/etc-shadow.mount b/mkosi.extra/usr/lib/systemd/system/etc-shadow.mount deleted file mode 100644 index 07372af..0000000 --- a/mkosi.extra/usr/lib/systemd/system/etc-shadow.mount +++ /dev/null @@ -1,16 +0,0 @@ -# Root password -# -# /etc is read-only, so agent-setup writes the shadow file to /var and this -# mounts it over /etc/shadow. -[Unit] -Description=Root password from /var -ConditionPathExists=/var/lib/agent/etc/shadow -RequiresMountsFor=/var - -[Mount] -What=/var/lib/agent/etc/shadow -Where=/etc/shadow -Options=bind - -[Install] -WantedBy=local-fs.target diff --git a/mkosi.extra/usr/lib/systemd/system/sshd.path b/mkosi.extra/usr/lib/systemd/system/sshd.path new file mode 100644 index 0000000..1a3926e --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/sshd.path @@ -0,0 +1,14 @@ +# Start sshd once there is a key +# +# sshd starts only once root or the user agent has an ssh key, so a new VM +# has no open port. Root adds the keys after the first boot (README.md). The +# ssh.authorized_keys.root credential also fills root's, such as for mkosi ssh. +[Unit] +Description=Start sshd once an ssh key is set + +[Path] +PathExists=/home/agent/.ssh/authorized_keys +PathExists=/root/.ssh/authorized_keys + +[Install] +WantedBy=multi-user.target diff --git a/mkosi.extra/usr/lib/systemd/system/sshd.service.d/agent.conf b/mkosi.extra/usr/lib/systemd/system/sshd.service.d/agent.conf deleted file mode 100644 index bc1bfd3..0000000 --- a/mkosi.extra/usr/lib/systemd/system/sshd.service.d/agent.conf +++ /dev/null @@ -1,7 +0,0 @@ -# sshd after setup -# -# sshd needs the host key, which agent-boot makes once the setup is done. -# agent-setup then starts sshd. -[Unit] -After=agent-boot.service -ConditionPathExists=/var/lib/agent/ssh_host_ed25519_key diff --git a/mkosi.extra/usr/lib/sysupdate.d/10-root.transfer b/mkosi.extra/usr/lib/sysupdate.d/10-usr.transfer similarity index 71% rename from mkosi.extra/usr/lib/sysupdate.d/10-root.transfer rename to mkosi.extra/usr/lib/sysupdate.d/10-usr.transfer index 35b4d04..0f413de 100644 --- a/mkosi.extra/usr/lib/sysupdate.d/10-root.transfer +++ b/mkosi.extra/usr/lib/sysupdate.d/10-usr.transfer @@ -1,6 +1,6 @@ -# Root update +# /usr update # -# Writes the next version of the root filesystem to the unused root partition. +# Writes the next version of /usr to the unused /usr partition. # Verify=yes: SHA256SUMS.gpg must be signed by a key in # /usr/lib/systemd/import-pubring.pgp, and SHA256SUMS checks each download. # The files come from .gitea/workflows/os.yml. @@ -11,12 +11,12 @@ Verify=yes [Source] Type=url-file Path=https://git.plabble.org/api/packages/Job79/generic/agent/latest/ -MatchPattern=agent_@v.root-%a.@u.raw.zst +MatchPattern=agent_@v.usr-%a.@u.raw.zst [Target] Type=partition Path=auto MatchPattern=agent_@v -MatchPartitionType=root +MatchPartitionType=usr PartitionFlags=0 ReadOnly=1 diff --git a/mkosi.extra/usr/lib/sysupdate.d/11-root-verity.transfer b/mkosi.extra/usr/lib/sysupdate.d/11-usr-verity.transfer similarity index 57% rename from mkosi.extra/usr/lib/sysupdate.d/11-root-verity.transfer rename to mkosi.extra/usr/lib/sysupdate.d/11-usr-verity.transfer index 5d8796a..f8129bf 100644 --- a/mkosi.extra/usr/lib/sysupdate.d/11-root-verity.transfer +++ b/mkosi.extra/usr/lib/sysupdate.d/11-usr-verity.transfer @@ -1,6 +1,6 @@ -# Root verity update +# /usr verity update # -# Writes the dm-verity hashes that go with 10-root.transfer, which explains +# Writes the dm-verity hashes that go with 10-usr.transfer, which explains # Verify=yes. [Transfer] ProtectVersion=%A @@ -9,12 +9,12 @@ Verify=yes [Source] Type=url-file Path=https://git.plabble.org/api/packages/Job79/generic/agent/latest/ -MatchPattern=agent_@v.root-%a-verity.@u.raw.zst +MatchPattern=agent_@v.usr-%a-verity.@u.raw.zst [Target] Type=partition Path=auto MatchPattern=agent_@v_verity -MatchPartitionType=root-verity +MatchPartitionType=usr-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 0327331..d84e64b 100644 --- a/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer +++ b/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer @@ -1,12 +1,12 @@ # UKI update # # Installs the next UKI, which systemd-boot then boots by default. For -# Verify=yes, see 10-root.transfer. +# Verify=yes, see 10-usr.transfer. # # The new UKI gets three tries (boot counting: the +@l-@d in its name). # systemd-bless-boot marks it good once a boot reaches boot-complete.target. # If no boot does, systemd-boot goes back to the previous UKI, and with it to -# the previous root partition, because each UKI holds its root hash. No +# the previous /usr partition, because each UKI holds its usr hash. No # Mode=0444: systemd-boot can't rename a read-only file. [Transfer] ProtectVersion=%A diff --git a/mkosi.extra/usr/lib/sysusers.d/claude.conf b/mkosi.extra/usr/lib/sysusers.d/agent.conf similarity index 61% rename from mkosi.extra/usr/lib/sysusers.d/claude.conf rename to mkosi.extra/usr/lib/sysusers.d/agent.conf index b575564..6f7156d 100644 --- a/mkosi.extra/usr/lib/sysusers.d/claude.conf +++ b/mkosi.extra/usr/lib/sysusers.d/agent.conf @@ -1,5 +1,5 @@ -# Claude user +# Agent user # # The user Claude runs as. /etc/subuid and /etc/subgid give it the ids that # rootless podman needs. -u claude 1000 "Claude" /var/home/claude /bin/bash +u agent 1000 "Agent" /home/agent /bin/bash diff --git a/mkosi.extra/usr/lib/tmpfiles.d/agent.conf b/mkosi.extra/usr/lib/tmpfiles.d/agent.conf index 863212d..bd8da8b 100644 --- a/mkosi.extra/usr/lib/tmpfiles.d/agent.conf +++ b/mkosi.extra/usr/lib/tmpfiles.d/agent.conf @@ -1,16 +1,8 @@ -# Agent directories +# Agent home # -# What survives a reboot and what doesn't. Claude can only write to the wiped -# directories, /tmp and its own config. - -# /var/lib/agent keeps the setup answers, the ssh keys and Claude's login. -# agent-boot wipes the rest of Claude's config. -d /var/lib/agent 0755 root root - -d /var/lib/agent/claude 0700 claude claude - -# D wipes Claude's home, with its podman storage, and temporary files at every -# boot. -D /var/home/claude 0700 claude claude - -D /var/tmp 1777 root root - -# linger starts Claude's user manager at boot, for rootless podman and its own -# services. -f /var/lib/systemd/linger/claude +# The home of the user agent (mkosi.extra/usr/lib/sysusers.d/agent.conf), with +# Claude's login and podman storage. +d /home/agent 0700 agent agent - +# linger starts the user manager of agent at boot, for rootless podman and +# Claude's own services. +f /var/lib/systemd/linger/agent diff --git a/mkosi.extra/usr/libexec/agent-boot b/mkosi.extra/usr/libexec/agent-boot deleted file mode 100755 index ffd0579..0000000 --- a/mkosi.extra/usr/libexec/agent-boot +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -# Apply setup -# -# Runs at every boot (agent-boot.service) and right after the setup. /etc is -# read-only, so the hostname asked at setup is set again on every boot. Makes -# the ssh host key the first time. Wipes Claude's config except its login, so -# Claude starts fresh but logged in. -set -euo pipefail -cd /var/lib/agent -hostnamectl hostname --transient "$(cat hostname)" -[[ -f ssh_host_ed25519_key ]] || ssh-keygen -q -t ed25519 -N '' -f ssh_host_ed25519_key -find claude -mindepth 1 -maxdepth 1 ! -name .credentials.json ! -name .claude.json -exec rm -rf {} + diff --git a/mkosi.extra/usr/libexec/agent-setup b/mkosi.extra/usr/libexec/agent-setup deleted file mode 100755 index 8d5b2ef..0000000 --- a/mkosi.extra/usr/libexec/agent-setup +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash -# First-boot setup -# -# Asks for the root password and the hostname (agent-setup@.service). /etc is -# read-only, so the answers go to /var/lib/agent. The password goes into a -# shadow file there, which etc-shadow.mount puts over /etc/shadow. agent-boot -# applies the rest at every boot. Root adds the ssh keys afterwards -# (README.md). -# -# With the credential agent.password, it asks nothing and takes the answers -# from the credentials agent.password and agent.hostname. mkosi vm passes them -# (mkosi.conf). -set -euo pipefail - -# cred prints the credential agent., and fails if it isn't passed. -cred() { cat "${CREDENTIALS_DIRECTORY:-/nonexistent}/agent.$1" 2>/dev/null; } - -if pw=$(cred password); then - # flock lets only one of the two consoles do the setup. - exec 9>/run/agent-setup.lock - flock -n 9 || exit 0 - name=$(cred hostname) || true -else - echo "== Agent setup ==" - while :; do - read -rsp "Root password: " pw; echo - read -rsp "Again: " pw2; echo - [[ -n $pw && $pw == "$pw2" ]] && break - echo "Passwords do not match, try again." - done - read -rp "Hostname [agent]: " name -fi - -install -d -m 0700 /var/lib/agent/etc -# etc-shadow.mount is stopped first: an interrupted setup may have started it, -# and then cp would copy the shadow file onto itself. -systemctl stop etc-shadow.mount -cp /etc/passwd /etc/shadow /var/lib/agent/etc/ -echo "root:$pw" | chpasswd --prefix /var/lib/agent -systemctl start etc-shadow.mount -# hostname is written last: its presence marks the setup as done. -echo "${name:-agent}" > /var/lib/agent/hostname -systemctl start agent-boot.service sshd.service -cat <<'EOF' -Done. Log in as root and add your ssh key for the user claude to -/var/lib/agent/authorized_keys (for root: root_authorized_keys there). Then -log in with ssh claude@, run claude and log in with your account. -Remote Control then starts by itself. -EOF -# systemctl stop ends the setup on the other console, which then shows its -# login. -systemctl --no-block stop "agent-setup@*.service" diff --git a/mkosi.finalize b/mkosi.finalize new file mode 100755 index 0000000..baa587c --- /dev/null +++ b/mkosi.finalize @@ -0,0 +1,42 @@ +#!/bin/sh +# Factory /etc +# +# The image ships only /usr (mkosi.repart), so this keeps the /etc that the +# packages and mkosi.extra/etc made in /usr/share/factory/etc. It writes +# /usr/lib/tmpfiles.d/factory-etc.conf, with which systemd-tmpfiles links each +# entry into the writable, empty /etc at boot. Updates to /usr then take +# effect in /etc too. /etc itself holds only what the machine writes: passwd, +# shadow, group, hostname, machine-id and the ssh host keys. +set -eu + +factory=$BUILDROOT/usr/share/factory/etc +cp -a --no-target-directory --update=none "$BUILDROOT/etc" "$factory" + +# skip holds what /etc keeps for itself, and what other tmpfiles.d files +# already set up, such as /etc/os-release. +skip=$(printf '%s\n' passwd group shadow gshadow hostname machine-id ssh + sed -n 's|^[^#][^ ]* */etc/\([^/ ]*\).*|\1|p' "$BUILDROOT"/usr/lib/tmpfiles.d/*.conf) + +# link prints the tmpfiles line for /etc/$1. Some entries are copied instead: +# a symlink, because a relative one would point somewhere else from the +# factory, and subuid and subgid, because shadow-utils won't read them through +# a link. +link() { + case "$1" in + subuid|subgid) echo "C /etc/$1" ;; + *) if [ -L "$factory/$1" ]; then echo "C /etc/$1"; else echo "L /etc/$1"; fi ;; + esac +} + +{ + echo "# Generated by mkosi.finalize." + for f in $(ls -A "$factory"); do + case "$f" in *-|.*) continue ;; esac + echo "$skip" | grep -qxF "$f" || link "$f" + done + # /etc/ssh is a directory of its own, because sshd-keygen writes the host + # keys there. + echo "d /etc/ssh 0755" + for f in $(ls -A "$factory/ssh"); do link "ssh/$f"; done +} > "$BUILDROOT/usr/lib/tmpfiles.d/factory-etc.conf" + diff --git a/mkosi.repart/10-root.conf b/mkosi.repart/10-root.conf deleted file mode 100644 index cf4907a..0000000 --- a/mkosi.repart/10-root.conf +++ /dev/null @@ -1,19 +0,0 @@ -# Root filesystem, slot A -# -# /etc and /usr as read-only erofs, protected by dm-verity (roothash= in the -# UKI). Updates replace it whole. /var holds the state, on its own partition -# (30-var.conf). -[Partition] -Type=root -Label=%M_%A -Format=erofs -Compression=lz4hc -CopyFiles=/ -ExcludeFilesTarget=/var/ -Verity=data -VerityMatchKey=root -SizeMinBytes=1G -SizeMaxBytes=1G -# SplitName holds the partition UUID (%U): roothash= finds the root by it, and -# updates keep it (@u in mkosi.extra/usr/lib/sysupdate.d). -SplitName=%t.%U diff --git a/mkosi.repart/10-usr.conf b/mkosi.repart/10-usr.conf new file mode 100644 index 0000000..8959193 --- /dev/null +++ b/mkosi.repart/10-usr.conf @@ -0,0 +1,19 @@ +# /usr, slot A +# +# The OS as read-only erofs, protected by dm-verity (usrhash= in the UKI). +# Updates replace it whole. The image holds nothing else but the ESP: the +# initrd makes the root filesystem, with /etc and /var, at first boot +# (mkosi.extra/usr/lib/repart.d/50-root.conf). +[Partition] +Type=usr +Label=%M_%A +Format=erofs +Compression=lz4hc +CopyFiles=/usr:/ +Verity=data +VerityMatchKey=usr +SizeMinBytes=1G +SizeMaxBytes=1G +# SplitName holds the partition UUID (%U): usrhash= finds /usr by it, and +# updates keep it (@u in mkosi.extra/usr/lib/sysupdate.d). +SplitName=%t.%U diff --git a/mkosi.repart/11-root-verity.conf b/mkosi.repart/11-root-verity.conf deleted file mode 100644 index 099a178..0000000 --- a/mkosi.repart/11-root-verity.conf +++ /dev/null @@ -1,11 +0,0 @@ -# Root verity, slot A -# -# The dm-verity hashes of the root filesystem in 10-root.conf. -[Partition] -Type=root-verity -Label=%M_%A_verity -Verity=hash -VerityMatchKey=root -SizeMinBytes=64M -SizeMaxBytes=64M -SplitName=%t.%U diff --git a/mkosi.repart/11-usr-verity.conf b/mkosi.repart/11-usr-verity.conf new file mode 100644 index 0000000..217a455 --- /dev/null +++ b/mkosi.repart/11-usr-verity.conf @@ -0,0 +1,11 @@ +# /usr verity, slot A +# +# The dm-verity hashes of /usr in 10-usr.conf. +[Partition] +Type=usr-verity +Label=%M_%A_verity +Verity=hash +VerityMatchKey=usr +SizeMinBytes=64M +SizeMaxBytes=64M +SplitName=%t.%U diff --git a/mkosi.repart/20-root-b.conf b/mkosi.repart/20-usr-b.conf similarity index 78% rename from mkosi.repart/20-root-b.conf rename to mkosi.repart/20-usr-b.conf index e502718..abcee2d 100644 --- a/mkosi.repart/20-root-b.conf +++ b/mkosi.repart/20-usr-b.conf @@ -1,8 +1,8 @@ -# Root filesystem, slot B +# /usr, slot B # # Empty until systemd-sysupdate writes the next version to it. [Partition] -Type=root +Type=usr Label=_empty SizeMinBytes=1G SizeMaxBytes=1G diff --git a/mkosi.repart/21-root-verity-b.conf b/mkosi.repart/21-root-verity-b.conf deleted file mode 100644 index 244d554..0000000 --- a/mkosi.repart/21-root-verity-b.conf +++ /dev/null @@ -1,9 +0,0 @@ -# Root verity, slot B -# -# The dm-verity hashes for 20-root-b.conf, empty until the next update. -[Partition] -Type=root-verity -Label=_empty -SizeMinBytes=64M -SizeMaxBytes=64M -SplitName=- diff --git a/mkosi.repart/21-usr-verity-b.conf b/mkosi.repart/21-usr-verity-b.conf new file mode 100644 index 0000000..22cf9d1 --- /dev/null +++ b/mkosi.repart/21-usr-verity-b.conf @@ -0,0 +1,9 @@ +# /usr verity, slot B +# +# The dm-verity hashes for 20-usr-b.conf, empty until the next update. +[Partition] +Type=usr-verity +Label=_empty +SizeMinBytes=64M +SizeMaxBytes=64M +SplitName=- diff --git a/mkosi.repart/30-var.conf b/mkosi.repart/30-var.conf deleted file mode 100644 index 1a838af..0000000 --- a/mkosi.repart/30-var.conf +++ /dev/null @@ -1,13 +0,0 @@ -# State partition -# -# /var: everything reboots and updates keep, such as the setup answers, the -# root password and Claude's login (/var/lib/agent). /etc/fstab mounts it and -# grows it to fill the disk at boot. Claude's home is on it too, but is wiped -# at every boot (mkosi.extra/usr/lib/tmpfiles.d/agent.conf). -[Partition] -Type=var -Label=var -Format=ext4 -CopyFiles=/var:/ -Minimize=guess -SplitName=-