commit 9245b26500e329a7bcd6d5796fdd83d4a037b934 Author: Job79 Date: Sun Nov 15 15:52:10 2026 +0100 chore: init diff --git a/.gitea/workflows/os.yml b/.gitea/workflows/os.yml new file mode 100644 index 0000000..82ecb36 --- /dev/null +++ b/.gitea/workflows/os.yml @@ -0,0 +1,49 @@ +# agent OS image +# +# Builds the OS image and publishes it as version "latest" of the generic package agent, where VMs +# fetch their updates (mkosi.extra/usr/lib/sysupdate.d). Runs on every push, weekly for Fedora and +# Claude Code updates, and by hand. +name: os +on: + push: + branches: [main] + schedule: + - cron: "0 2 * * 0" # weekly (Sunday), picks up Fedora and Claude Code updates + workflow_dispatch: # run by hand, e.g. for a CVE + +jobs: + os: + runs-on: linux + env: + PKG: ${{ github.server_url }}/api/packages/${{ github.repository_owner }}/generic/agent/latest + AUTH: "Authorization: token ${{ github.token }}" + steps: + - name: Install tools + run: dnf install -yq mkosi + - name: Check out + uses: builtin:checkout + - name: Build + run: mkosi build + - name: Compress + working-directory: mkosi.output + run: | + zstd -q --rm -T0 -19 agent_*.root-*.raw + qemu-img convert -c -O qcow2 "$(basename agent_*.efi .efi).raw" agent.qcow2 + sha256sum agent_*.efi agent_*.root-*.raw.zst > SHA256SUMS + # Signs SHA256SUMS with the key whose public half is mkosi.extra/usr/lib/systemd/import-pubring.pgp. + - name: Sign + working-directory: mkosi.output + env: + KEY: ${{ secrets.SIGNING_KEY }} + run: | + export GNUPGHOME=$(mktemp -d) + gpg -q --batch --import <<< "$KEY" + gpg -q --batch -o SHA256SUMS.gpg --detach-sign SHA256SUMS + # Replaces the previous version. SHA256SUMS goes last: until it's there, VMs find no update. + - 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 SHA256SUMS.gpg SHA256SUMS; do + curl -fsS -o /dev/null -H "$AUTH" --upload-file "$f" "$PKG/$f" + done diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96100fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Build outputs. +mkosi.output/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..7b439ae --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# agent-mkosi + +A VM that runs [Claude Code](https://claude.com/claude-code) in the background, packaged as a minimal Fedora image built with [mkosi](https://github.com/systemd/mkosi). You control Claude from claude.ai/code, the Claude app (Remote Control), or log in with ssh. + +- **Resets at reboot:** the OS is read-only, so Claude can't change it, and Claude's home, `/tmp` and all containers are wiped at every boot. Only the setup answers and Claude's login survive (`/var/lib/agent`). +- **Claude as non-root, tools in containers:** Claude runs as the user `claude`, without root, and runs its toolchains in containers with rootless podman (also as `docker`). `/etc/claude-code/CLAUDE.md` tells it about the machine; `/etc/claude-code/managed-settings.json` lets it run without asking permission. The host has only git, ssh and basic command-line tools. +- **Self-updating:** like [git-runner-mkosi](../git-runner-mkosi): images are rebuilt weekly (also picking up the latest Claude Code) and on every push. Hosts download updates with systemd-sysupdate, check their signature (`SIGNING_KEY` secret), write them into a second root partition (A/B) and reboot into them daily at about 04:10. That reboot also resets Claude's work. +- **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. All state (Claude's home, its login, the root password, logs) lives on `/var`, its own partition. +- **Hardened:** a firewall that drops all incoming traffic but ssh, KSPP sysctls and kernel arguments, and systemd-oomd kills Claude's work, not the system, when it runs the VM out of memory. + +## Install + +Download [the disk image](https://git.plabble.org/api/packages/Job79/generic/agent/latest/agent.qcow2) (built by the `os` workflow), resize it to the disk size you want (/var grows to fill it) and boot it with UEFI. The screen and the serial console ask for a root password, a hostname and an ssh public key for the user `claude`. + +Then log Claude in once: + +```sh +ssh claude@ +claude # trust the folder (your home), then /login with your Claude account +``` + +As soon as Claude is logged in, `claude-remote.service` starts `claude remote-control` in Claude's home, and the VM shows up in claude.ai/code and the Claude app under its hostname. Over ssh you can also run `claude` yourself. Logs: `journalctl -u claude-remote`. + +## Development + +`mkosi.conf` defines the OS (`mkosi.extra/` is copied to `/`, `mkosi.repart/` is the disk layout, `mkosi.prepare` installs Claude Code). + +```sh +mkosi build +``` + +`mkosi.output/` then holds the disk image (`agent_.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`; ssh on port 2222): + +```sh +qemu-img convert -O qcow2 mkosi.output/agent_.raw test.qcow2 && qemu-img resize test.qcow2 40G +cp /usr/share/edk2/ovmf/OVMF_VARS.fd vars.fd +qemu-kvm -m 8G -smp 4 -cpu host -nic user,model=virtio-net-pci,hostfwd=tcp::2222-:22 -drive file=test.qcow2,if=virtio -device virtio-balloon-pci,free-page-reporting=on \ + -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.fd -drive if=pflash,format=raw,file=vars.fd +``` diff --git a/mkosi.conf b/mkosi.conf new file mode 100644 index 0000000..fb24de6 --- /dev/null +++ b/mkosi.conf @@ -0,0 +1,124 @@ +# agent: a Fedora VM image that runs Claude Code in the background, built with mkosi. +# mkosi.extra/ is copied to /, mkosi.repart/ defines the disk, mkosi.prepare adds Claude Code. +# The root filesystem, /etc included, is read-only and replaced whole by updates; all state lives +# on /var. Claude can't change the OS: it runs its tools in containers. + +[Distribution] +Distribution=fedora +Release=45 + +[Output] +Format=disk +ImageId=agent +OutputDirectory=mkosi.output +# The UKI and root partitions are published as updates (systemd-sysupdate). +SplitArtifacts=uki,partitions + +[Content] +Bootable=yes +Bootloader=systemd-boot +# - kernel-core no drivers or firmware beyond what VMs need +# - systemd-container, gnupg2 systemd-pull, which systemd-sysupdate downloads with, and gpg, +# which it checks their signature with +# - systemd-boot-unsigned the bootloader +# - util-linux, shadow-utils console login, setup, newuidmap for rootless podman +# - systemd-pam login sessions (pam_systemd): Claude's user manager and its +# D-Bus, which rootless podman needs +# - openssh-server ssh login for the user (keys only) +# - nftables-services the firewall +# - zram-generator-defaults compressed swap in RAM +# - podman, podman-docker containers, for all of Claude's toolchains (also as docker) +# - git-core, openssh-clients Claude Code uses git; it pushes over ssh +# - the rest basic command-line tools Claude uses on the host +Packages= + kernel-core + systemd + dbus-broker + systemd-udev + systemd-networkd + systemd-resolved + systemd-container + gnupg2 + systemd-boot-unsigned + util-linux + shadow-utils + systemd-pam + openssh-server + nftables-services + zram-generator-defaults + podman + podman-docker + git-core + openssh-clients + curl + tar + gzip + xz + zstd + diffutils + findutils + procps-ng + iproute +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 +# The console on screen and serial port, the machine ID from the VM's firmware +# (mkosi.extra/etc/machine-id is empty, so no boot counts as the first), no systemd-firstboot +# questions (agent-setup asks its own), freeing memory unused for an hour (damon_reclaim, in µs; the +# watermarks make it run whatever memory is free), so the host can take it back (virtio-balloon +# free page reporting), and KSPP hardening (the last four). +KernelCommandLine= + console=tty0 + console=ttyS0,115200n8 + quiet + systemd.machine_id=firmware + systemd.firstboot=no + damon_reclaim.enabled=Y + damon_reclaim.min_age=3600000000 + damon_reclaim.wmarks_high=1000 + damon_reclaim.wmarks_mid=999 + damon_reclaim.wmarks_low=0 + slab_nomerge + init_on_alloc=1 + randomize_kstack_offset=on + page_alloc.shuffle=1 +# Kernel modules in the initrd: only what finds and mounts the disks of a VM. +KernelInitrdModules= + virtio_blk + virtio_scsi + sd_mod + nvme + dm-verity + erofs + ext4 +# Kernel modules in the image: all but the hardware drivers, then the drivers of a VM (disks, +# network, screen) and of rootless podman's networks (pasta). The last matching line wins. +# Keyboard, USB and a basic screen are built into the kernel. +KernelModules= + /* + -/drivers/* + /drivers/virtio/* + /drivers/block/* + /drivers/md/* + nvme + virtio_scsi + sd_mod + virtio_net + qemu_fw_cfg + bochs + virtio-gpu + veth + tun +# - System.map only for debugging the kernel +# - udev hwdb hardware names and quirks, nothing a VM needs +# - homed, userdbd, nsresourced unused services +# - sshd-keygen sshd uses one host key, kept on /var (sshd_config.d) +RemoveFiles= + /usr/lib/modules/*/System.map + /usr/lib/udev/hwdb.bin + /usr/lib/udev/hwdb.d + /usr/lib/systemd/system/systemd-homed* + /usr/lib/systemd/system/systemd-userdbd.* + /usr/lib/systemd/system/systemd-nsresourced.* + /usr/lib/systemd/system/sshd-keygen* diff --git a/mkosi.extra/etc/claude-code/CLAUDE.md b/mkosi.extra/etc/claude-code/CLAUDE.md new file mode 100644 index 0000000..3ed66c5 --- /dev/null +++ b/mkosi.extra/etc/claude-code/CLAUDE.md @@ -0,0 +1,15 @@ +# This machine + +You run on a small Fedora VM made for you, as the user `claude`, without root. + +- **The OS is read-only**: you can't install anything on it. Run toolchains, languages and + services in containers, with rootless `podman` (also as `docker`). For quick commands: + `podman run --rm -v "$PWD:/w" -w /w docker.io/library/python:3 python script.py`. + For longer work, keep a dev container you can install into: + `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. +- Incoming traffic is blocked except ssh; to serve something, the user needs a tunnel. diff --git a/mkosi.extra/etc/claude-code/managed-settings.json b/mkosi.extra/etc/claude-code/managed-settings.json new file mode 100644 index 0000000..f25a577 --- /dev/null +++ b/mkosi.extra/etc/claude-code/managed-settings.json @@ -0,0 +1,5 @@ +{ + "permissions": { "defaultMode": "bypassPermissions" }, + "skipDangerousModePermissionPrompt": true, + "env": { "DISABLE_AUTOUPDATER": "1" } +} diff --git a/mkosi.extra/etc/environment b/mkosi.extra/etc/environment new file mode 100644 index 0000000..7befa6e --- /dev/null +++ b/mkosi.extra/etc/environment @@ -0,0 +1,2 @@ +# Claude's config, with its login, lives on /var/lib/agent (kept across reboots), not in the home. +CLAUDE_CONFIG_DIR=/var/lib/agent/claude diff --git a/mkosi.extra/etc/fstab b/mkosi.extra/etc/fstab new file mode 100644 index 0000000..8c95777 --- /dev/null +++ b/mkosi.extra/etc/fstab @@ -0,0 +1,2 @@ +# /var, the only partition on disk that is written (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/etc/profile.d/ps1.sh b/mkosi.extra/etc/profile.d/ps1.sh new file mode 100644 index 0000000..ad7af6d --- /dev/null +++ b/mkosi.extra/etc/profile.d/ps1.sh @@ -0,0 +1,2 @@ +# The prompt: hostname and directory on a purple bar (the arrow needs a Powerline/Nerd font). +export PS1="\[\e[30;45m\] \h | \w \[\e[0;35m\]\[\e[m\] " diff --git a/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf b/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf new file mode 100644 index 0000000..884354d --- /dev/null +++ b/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf @@ -0,0 +1,9 @@ +# ssh: only the user claude, only with the key asked at setup. The host key is kept on /var +# (agent-boot makes it), so it stays the same across reboots. First value wins: this file sorts +# before Fedora's 50-redhat.conf. +HostKey /var/lib/agent/ssh_host_ed25519_key +AuthorizedKeysFile /var/lib/agent/authorized_keys +AllowUsers claude +PermitRootLogin no +PasswordAuthentication no +KbdInteractiveAuthentication no diff --git a/mkosi.extra/etc/subgid b/mkosi.extra/etc/subgid new file mode 100644 index 0000000..72b2f42 --- /dev/null +++ b/mkosi.extra/etc/subgid @@ -0,0 +1 @@ +claude:524288:65536 diff --git a/mkosi.extra/etc/subuid b/mkosi.extra/etc/subuid new file mode 100644 index 0000000..72b2f42 --- /dev/null +++ b/mkosi.extra/etc/subuid @@ -0,0 +1 @@ +claude:524288:65536 diff --git a/mkosi.extra/usr/lib/agent/firewall.nft b/mkosi.extra/usr/lib/agent/firewall.nft new file mode 100644 index 0000000..db9254d --- /dev/null +++ b/mkosi.extra/usr/lib/agent/firewall.nft @@ -0,0 +1,18 @@ +# Firewall: drop all incoming traffic except replies, loopback, ICMP, DHCPv6 and ssh, and forward +# nothing (rootless podman's networks live in Claude's own network namespace). Loaded by +# nftables.service. +destroy table inet firewall +table inet firewall { + chain input { + type filter hook input priority filter; policy drop; + ct state established,related accept + ct state invalid drop + iif lo accept + meta l4proto { icmp, ipv6-icmp } accept + ip6 saddr fe80::/10 udp dport 546 accept + tcp dport 22 accept + } + chain forward { + type filter hook forward priority filter; policy drop; + } +} 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/sysctl.d/50-agent.conf b/mkosi.extra/usr/lib/sysctl.d/50-agent.conf new file mode 100644 index 0000000..f566b37 --- /dev/null +++ b/mkosi.extra/usr/lib/sysctl.d/50-agent.conf @@ -0,0 +1,31 @@ +# Kernel hardening, mostly the KSPP recommendations (Talos sets these too), and one limit raised +# for containers. Left out on purpose: user.max_user_namespaces=0 breaks rootless podman, and +# kernel.modules_disabled=1 breaks loading the network modules podman needs. + +# Hide kernel addresses (they help exploits bypass KASLR), even from root. +kernel.kptr_restrict = 2 +# Only root may read the kernel log; it leaks addresses. +kernel.dmesg_restrict = 1 +# No perf events for non-root users. +kernel.perf_event_paranoid = 3 +# Harden the BPF JIT against JIT spraying. +net.core.bpf_jit_harden = 2 +# No BPF for non-root users, and no switching it back on at runtime. +kernel.unprivileged_bpf_disabled = 1 +# No kexec: no booting another kernel past Secure Boot. +kernel.kexec_load_disabled = 1 +# No core dumps of setuid programs; they can hold secrets. +fs.suid_dumpable = 0 +# No loading TTY line disciplines on demand; old ones had exploitable bugs. +dev.tty.ldisc_autoload = 0 +# userfaultfd only for root; it makes kernel race exploits reliable. +vm.unprivileged_userfaultfd = 0 +# No opening FIFOs and files owned by others in sticky directories like /tmp. +fs.protected_fifos = 2 +fs.protected_regular = 2 +# ptrace only on your own descendants. +kernel.yama.ptrace_scope = 1 + +# Containers (file watchers, systemd in containers) run out of Fedora's 128 inotify instances +# per user; 8192 is common on Kubernetes nodes. +fs.inotify.max_user_instances = 8192 diff --git a/mkosi.extra/usr/lib/systemd/import-pubring.pgp b/mkosi.extra/usr/lib/systemd/import-pubring.pgp new file mode 100644 index 0000000..ab0c59e Binary files /dev/null and b/mkosi.extra/usr/lib/systemd/import-pubring.pgp differ diff --git a/mkosi.extra/usr/lib/systemd/network/80-dhcp.network b/mkosi.extra/usr/lib/systemd/network/80-dhcp.network new file mode 100644 index 0000000..6000185 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/network/80-dhcp.network @@ -0,0 +1,11 @@ +# DHCP on the real network cards. Kind=!* skips virtual links. +[Match] +Type=ether +Kind=!* + +[Network] +DHCP=yes + +[DHCPv4] +# The hostname is the one asked at setup (agent-boot), not 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 new file mode 100644 index 0000000..b6f8094 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset @@ -0,0 +1,20 @@ +# Services enabled in the image (mkosi runs systemctl preset-all). Fedora disables getty@.service, the +# login on the screen (tty1, and tty2-6 through its autovt@ alias); this file sorts first, so it wins. +enable systemd-networkd.service +enable systemd-resolved.service +enable systemd-oomd.service +enable nftables.service +enable sshd.service +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 +# Fedora enables these, but they do nothing here: no PAM or crypto changes on a read-only /etc, +# no system or config extensions. +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 new file mode 100644 index 0000000..527096d --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/agent-boot.service @@ -0,0 +1,15 @@ +# Applies the setup answers at every boot (see /usr/libexec/agent-boot). Before the setup is done +# there are none; agent-setup starts this when 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 new file mode 100644 index 0000000..da7f4a9 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/agent-setup@.service @@ -0,0 +1,21 @@ +# First boot: asks for the root password, hostname and ssh key, on the screen (tty1) and the serial +# console (ttyS0) at once; 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 +ExecStart=/usr/libexec/agent-setup +StandardInput=tty +StandardOutput=tty +TTYPath=/dev/%I +TTYReset=yes +TTYVHangup=yes +# Answering on one console stops the setup on the other (SIGTERM), which is no failure. +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 new file mode 100644 index 0000000..ba50a87 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/claude-remote.path @@ -0,0 +1,9 @@ +# Starts Claude (Remote Control) once it is logged in: at boot, or when the user first logs in. +[Unit] +Description=Start Claude Remote Control once logged in + +[Path] +PathExists=/var/lib/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 new file mode 100644 index 0000000..5321ac6 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/claude-remote.service @@ -0,0 +1,16 @@ +# Claude Code, in the background, controlled from claude.ai/code or the Claude app (Remote Control). +# Works in Claude's home. Started by claude-remote.path 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 + +[Service] +User=claude +WorkingDirectory=~ +# In user.slice, so systemd-oomd kills it (or Claude's containers), not the system (user.slice.d). +Slice=user.slice +Environment=CLAUDE_CONFIG_DIR=/var/lib/agent/claude XDG_RUNTIME_DIR=/run/user/1000 +ExecStart=/usr/bin/claude remote-control --name %H +Restart=always +RestartSec=10s 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..cf988c8 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/etc-shadow.mount @@ -0,0 +1,14 @@ +# The 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/nftables.service.d/agent.conf b/mkosi.extra/usr/lib/systemd/system/nftables.service.d/agent.conf new file mode 100644 index 0000000..d405ce5 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/nftables.service.d/agent.conf @@ -0,0 +1,6 @@ +# Load our firewall (/usr/lib/agent/firewall.nft) instead of /etc/sysconfig/nftables.conf. +[Service] +ExecStart= +ExecStart=/usr/sbin/nft -f /usr/lib/agent/firewall.nft +ExecReload= +ExecReload=/usr/sbin/nft -f /usr/lib/agent/firewall.nft 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 new file mode 100644 index 0000000..8524a40 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/sshd.service.d/agent.conf @@ -0,0 +1,4 @@ +# The host key is made by agent-boot, 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/systemd/system/user.slice.d/50-oomd.conf b/mkosi.extra/usr/lib/systemd/system/user.slice.d/50-oomd.conf new file mode 100644 index 0000000..f762bd4 --- /dev/null +++ b/mkosi.extra/usr/lib/systemd/system/user.slice.d/50-oomd.conf @@ -0,0 +1,7 @@ +# systemd-oomd for Claude and its containers: when they run the VM out of memory, kill the cgroup +# responsible before the VM thrashes on zram swap or the kernel's OOM killer picks sshd. +[Slice] +# Memory pressure above 60% for 30s: kill the cgroup reclaiming the most memory. +ManagedOOMMemoryPressure=kill +# Swap over 90% full: kill the cgroup using the most swap. +ManagedOOMSwap=kill diff --git a/mkosi.extra/usr/lib/sysupdate.d/10-root.transfer b/mkosi.extra/usr/lib/sysupdate.d/10-root.transfer new file mode 100644 index 0000000..af95a16 --- /dev/null +++ b/mkosi.extra/usr/lib/sysupdate.d/10-root.transfer @@ -0,0 +1,19 @@ +# Updates the root filesystem: writes the next version to the unused root partition. +# Verify=yes: SHA256SUMS.gpg must be signed by a key in /usr/lib/systemd/import-pubring.pgp, and +# SHA256SUMS checks each download. +[Transfer] +ProtectVersion=%A +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 + +[Target] +Type=partition +Path=auto +MatchPattern=agent_@v +MatchPartitionType=root +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-root-verity.transfer new file mode 100644 index 0000000..907d7aa --- /dev/null +++ b/mkosi.extra/usr/lib/sysupdate.d/11-root-verity.transfer @@ -0,0 +1,17 @@ +# Updates the root filesystem's dm-verity hashes, next to 10-root.transfer (see it for Verify=yes). +[Transfer] +ProtectVersion=%A +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 + +[Target] +Type=partition +Path=auto +MatchPattern=agent_@v_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 new file mode 100644 index 0000000..f4b66cc --- /dev/null +++ b/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer @@ -0,0 +1,17 @@ +# Installs the next UKI, which systemd-boot boots by default. Verify=yes: see 10-root.transfer. +[Transfer] +ProtectVersion=%A +Verify=yes + +[Source] +Type=url-file +Path=https://git.plabble.org/api/packages/Job79/generic/agent/latest/ +MatchPattern=agent_@v.efi + +[Target] +Type=regular-file +Path=/EFI/Linux +PathRelativeTo=boot +MatchPattern=agent_@v.efi +Mode=0444 +InstancesMax=2 diff --git a/mkosi.extra/usr/lib/sysusers.d/claude.conf b/mkosi.extra/usr/lib/sysusers.d/claude.conf new file mode 100644 index 0000000..cd3538f --- /dev/null +++ b/mkosi.extra/usr/lib/sysusers.d/claude.conf @@ -0,0 +1,2 @@ +# The user Claude runs as; /etc/subuid and /etc/subgid give it the ids of rootless podman. +u claude 1000 "Claude" /var/home/claude /bin/bash diff --git a/mkosi.extra/usr/lib/tmpfiles.d/agent.conf b/mkosi.extra/usr/lib/tmpfiles.d/agent.conf new file mode 100644 index 0000000..14df66b --- /dev/null +++ b/mkosi.extra/usr/lib/tmpfiles.d/agent.conf @@ -0,0 +1,10 @@ +# State kept across reboots: the setup answers, the ssh host key 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 - +# Wiped at every boot (D empties a directory at boot): Claude's home, with its podman storage, and +# temporary files. These, /tmp and Claude's config are all that Claude can write. +D /var/home/claude 0700 claude claude - +D /var/tmp 1777 root root - +# Start Claude's user manager at boot, for rootless podman and its own services. +f /var/lib/systemd/linger/claude diff --git a/mkosi.extra/usr/libexec/agent-boot b/mkosi.extra/usr/libexec/agent-boot new file mode 100755 index 0000000..bfe1013 --- /dev/null +++ b/mkosi.extra/usr/libexec/agent-boot @@ -0,0 +1,9 @@ +#!/bin/bash +# Every boot (agent-boot.service), and after the setup: sets the hostname asked at setup (/etc is +# read-only, so it is set on every boot), makes the ssh host key the first time, and 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 new file mode 100755 index 0000000..85f6bc0 --- /dev/null +++ b/mkosi.extra/usr/libexec/agent-setup @@ -0,0 +1,29 @@ +#!/bin/bash +# First boot: asks for the root password, the hostname and the ssh key of the user claude, on the +# screen (tty1) and the serial console (ttyS0) at once (agent-setup@.service). +# /etc is read-only, so the answers go to /var/lib/agent: the password to a shadow file there +# (etc-shadow.mount puts it over /etc/shadow), the rest is applied by agent-boot at every boot. +set -euo pipefail +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 +install -d -m 0700 /var/lib/agent/etc +cp /etc/passwd /etc/shadow /var/lib/agent/etc/ +echo "root:$pw" | chpasswd --prefix /var/lib/agent +systemctl start etc-shadow.mount + +read -rp "Hostname [agent]: " name +read -rp "SSH public key for the user claude: " key +[[ -n $key ]] && install -m 0644 /dev/stdin /var/lib/agent/authorized_keys <<<"$key" +# Written last: its presence marks the setup as done. +echo "${name:-agent}" > /var/lib/agent/hostname +systemctl start agent-boot.service sshd.service +echo "Done. Log in with ssh claude@, run claude and log in with your account." +echo "Remote Control then starts by itself." +# Stop the setup on the other console, which then shows its login. +systemctl --no-block stop "agent-setup@*.service" diff --git a/mkosi.prepare b/mkosi.prepare new file mode 100755 index 0000000..3490ab4 --- /dev/null +++ b/mkosi.prepare @@ -0,0 +1,12 @@ +#!/bin/sh +# Installs Claude Code: the latest stable release, checked against the sha256 in its release manifest. +# The image updates weekly, so Claude's own auto-updater is off (/etc/claude-code/managed-settings.json). +set -eu +u=https://storage.googleapis.com/claude-code-dist-86c565f3-f756-42ad-8dfa-d59b1c096819/claude-code-releases +v=$(curl -fsS "$u/stable") +sum=$(curl -fsS "$u/$v/manifest.json" | tr -d ' \n' | sed -n 's/.*"linux-x64":{[^}]*"checksum":"\([0-9a-f]*\)".*/\1/p') +[ -n "$sum" ] +# The binary is ~300 MB: when the download stalls (under 1 kB/s for a minute), resume it. +curl -fsS --retry 10 --retry-all-errors --speed-limit 1000 --speed-time 60 -C - -o "$BUILDROOT/usr/bin/claude" "$u/$v/linux-x64/claude" +echo "$sum $BUILDROOT/usr/bin/claude" | sha256sum -c +chmod 755 "$BUILDROOT/usr/bin/claude" diff --git a/mkosi.repart/00-esp.conf b/mkosi.repart/00-esp.conf new file mode 100644 index 0000000..f228b03 --- /dev/null +++ b/mkosi.repart/00-esp.conf @@ -0,0 +1,8 @@ +# EFI system partition: systemd-boot and the UKIs (two versions at most). +[Partition] +Type=esp +Format=vfat +CopyFiles=/efi:/ +CopyFiles=/boot:/ +SizeMinBytes=256M +SizeMaxBytes=256M diff --git a/mkosi.repart/10-root.conf b/mkosi.repart/10-root.conf new file mode 100644 index 0000000..e117697 --- /dev/null +++ b/mkosi.repart/10-root.conf @@ -0,0 +1,15 @@ +# The root filesystem, slot A: /etc and /usr, read-only erofs protected by dm-verity (roothash= in +# the UKI). Updates replace it whole. /var, the state, is its own partition (30-var.conf). +[Partition] +Type=root +Label=%M_%A +Format=erofs +Compression=zstd +CopyFiles=/ +ExcludeFilesTarget=/var/ +Verity=data +VerityMatchKey=root +SizeMinBytes=1G +SizeMaxBytes=1G +# The file name holds the partition UUID, which roothash= finds the root by; updates keep it (@u in sysupdate.d). +SplitName=%t.%U diff --git a/mkosi.repart/11-root-verity.conf b/mkosi.repart/11-root-verity.conf new file mode 100644 index 0000000..1e9761a --- /dev/null +++ b/mkosi.repart/11-root-verity.conf @@ -0,0 +1,9 @@ +# dm-verity hashes of the root filesystem, slot A. +[Partition] +Type=root-verity +Label=%M_%A_verity +Verity=hash +VerityMatchKey=root +SizeMinBytes=64M +SizeMaxBytes=64M +SplitName=%t.%U diff --git a/mkosi.repart/20-root-b.conf b/mkosi.repart/20-root-b.conf new file mode 100644 index 0000000..c8b4bdd --- /dev/null +++ b/mkosi.repart/20-root-b.conf @@ -0,0 +1,7 @@ +# The root filesystem, slot B: empty until systemd-sysupdate writes the next version. +[Partition] +Type=root +Label=_empty +SizeMinBytes=1G +SizeMaxBytes=1G +SplitName=- diff --git a/mkosi.repart/21-root-verity-b.conf b/mkosi.repart/21-root-verity-b.conf new file mode 100644 index 0000000..1a323ba --- /dev/null +++ b/mkosi.repart/21-root-verity-b.conf @@ -0,0 +1,7 @@ +# dm-verity hashes of the root filesystem, slot B. +[Partition] +Type=root-verity +Label=_empty +SizeMinBytes=64M +SizeMaxBytes=64M +SplitName=- diff --git a/mkosi.repart/30-var.conf b/mkosi.repart/30-var.conf new file mode 100644 index 0000000..78fc515 --- /dev/null +++ b/mkosi.repart/30-var.conf @@ -0,0 +1,10 @@ +# /var: Claude's home and the state that reboots and updates keep (/var/lib/agent: the setup answers +# and Claude's login, the root password). Mounted by /etc/fstab, which also grows it to fill the +# disk at boot. Claude's home, with its containers, is wiped at every boot (tmpfiles.d/agent.conf). +[Partition] +Type=var +Label=var +Format=ext4 +CopyFiles=/var:/ +Minimize=guess +SplitName=- diff --git a/mkosi.version b/mkosi.version new file mode 100755 index 0000000..11d6cd9 --- /dev/null +++ b/mkosi.version @@ -0,0 +1,3 @@ +#!/bin/sh +# The image version: the build time, so every build is newer than the last. +date -u +%Y%m%d%H%M