diff --git a/.gitea/workflows/os.yml b/.gitea/workflows/os.yml index 531bead..92fce6b 100644 --- a/.gitea/workflows/os.yml +++ b/.gitea/workflows/os.yml @@ -15,8 +15,8 @@ on: push: branches: [main] schedule: - - cron: "0 2 * * 0" # weekly (Sunday), picks up package updates - workflow_dispatch: # run by hand, e.g. for a CVE + - cron: "0 2 * * 0" + workflow_dispatch: jobs: os: @@ -38,8 +38,7 @@ jobs: - name: Compress working-directory: mkosi.output run: | - zstd -q --rm -T0 -9 agent_*.root-*.raw - # -m 16 -W: compress on several cores instead of one + 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 - name: Sign diff --git a/AGENTS.md b/AGENTS.md index 76b659d..0a2926b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,43 +1,78 @@ # Agent notes -How to build and test this image as a coding agent. Follow [STYLE.md](STYLE.md) for every change. +How to build and test this image as a coding agent. Follow [Style](#style) for +every change. -## Container +## Style -`mkosi vm` needs systemd on the host. A dev container has none, so run mkosi in a podman container that: +- 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. +- Each fact lives in one place. Elsewhere, refer to it by path instead of + repeating it. +- Write plain English in short sentences. -- boots systemd (`--systemd=always`, with `/sbin/init` as its command) -- is privileged and has `/dev/kvm` -- has `mkosi qemu-kvm-core edk2-ovmf systemd-repart openssh-clients` and `$(mkosi dependencies)` installed -- mounts the repository, such as at `/repo` +### File header -Then run mkosi with `podman exec mkosi ...`. - -podman resolves host paths on the host, not in the dev container, so `~` and the scratchpad don't exist there. Mount paths inside the repository instead, such as `./mkosi.output/...`, which is ignored by git. - -## Test VM - -The `[Runtime]` section of `mkosi.conf` sets up the test VM. It boots a throwaway copy (`Ephemeral=yes`), so every boot is a first boot. The setup takes its answers from credentials: root's password is `test`, and the `claude` user gets the keys in `~/.ssh/*.pub` of whoever runs mkosi (`mkosi.credentials/agent.ssh-key`). - -- If the container has no `~/.ssh`, make a throwaway key in `mkosi.output/test-ssh/` and mount it at `/root/.ssh`. -- `mkosi vm` needs a terminal. Without one, run it with `podman exec -d -t` and send its output to a file. -- ssh runs inside the container, which forwards port 2222 to the VM: `podman exec ssh -p 2222 claude@localhost`. Pass `-F /dev/null` if ssh rejects the mounted config because of its owner. -- For root, run `su -` over ssh. - -## Image version - -`mkosi.version` prints the build time, so every mkosi run has a new version. `mkosi vm` then looks for an image that was never built and fails with "has not been built yet". Either rebuild with `mkosi -f vm`, or boot an existing build by pinning its version: +Every file that allows comments starts with a header. It goes after the +shebang, if there is one: ```sh -mkosi --image-version 202611171816 vm # the version in mkosi.output/agent_.raw +#!/bin/sh +# Image version +# +# Prints the build time in UTC (YYYYMMDDhhmm). Every build gets a higher +# version than the one before, which is how systemd-sysupdate tells that an +# update is newer. ``` -Pinning saves a full rebuild when only testing. +- Line 1 is a title: a few words, in sentence case, with no full stop. +- Then a line with only the comment character. +- Then what the file does and why, naming the files it works with by path. -## Boot time +Files that can't hold comments (JSON, `subuid`, `machine-id`) are explained in +the file that uses them. -Measure it over ssh with `systemd-analyze`, `systemd-analyze blame` and `systemd-analyze critical-chain`. In `blame`, the `.device` units only show when udev announced the device, not time spent, so filter them out. +### Comments -The test VM always does a first boot (setup, growfs, enrolling the Secure Boot keys). To measure a normal boot, reboot it with `systemctl reboot` as root: the throwaway copy lasts until qemu exits. Compare a few boots, since timings vary. +- Explain why, not what. Leave out comments that only repeat the code. Try to + 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.` +- Wrap comments at 80 columns. -To see what systemd waits for, boot with debug logging without a rebuild: `mkosi --kernel-command-line-extra=systemd.log_level=debug vm`. +## Testing + +`mkosi vm` boots the image in qemu and needs systemd on the host. Without it, +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 \ + 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 +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. +`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 ''`. + +`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 +its version (from `mkosi.output/agent_.raw`): + +```sh +mkosi --image-version 202611171816 vm +``` diff --git a/README.md b/README.md index 7259d93..a2e02cb 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,28 @@ # agent-mkosi -A VM that runs [Claude Code](https://claude.com/claude-code) in the background. 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. +A VM that runs [Claude Code](https://claude.com/claude-code) in the background. +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:** Claude's home, `/tmp` and all containers are wiped at every boot. Only the setup answers 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. +- **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. ## 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 in Secure Boot setup mode. The console asks 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, then /login with your Claude account -``` - -The VM then shows up in claude.ai/code and the Claude app under its hostname. +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. ## Development @@ -27,13 +31,13 @@ mkosi genkey # local test only, CI uses the SECUREBOOT_KEY secret mkosi build ``` -`mkosi.output/` then holds the disk image (`agent_.raw`) and the UKI and root partitions that make up an update. +`mkosi.output/` then holds the disk image (`agent_.raw`) and the UKI +and root partitions that make up an update. -To build and boot a test VM (needs `qemu-kvm-core edk2-ovmf`): +To build and boot a test VM (needs `qemu-kvm-core edk2-ovmf openssh-clients +socat`): ```sh -mkosi -f vm # log in as root, password test -ssh -p 2222 claude@localhost # with your own key +mkosi -f vm # opens its console: log in as root, password root +mkosi ssh # in another terminal: ssh in as root ``` - -[AGENTS.md](AGENTS.md) has the details, such as testing from a dev container. diff --git a/STYLE.md b/STYLE.md deleted file mode 100644 index 2ec4e5d..0000000 --- a/STYLE.md +++ /dev/null @@ -1,30 +0,0 @@ -# Style guide - -- Keep it simple. Prefer fewer files, fewer lines and the defaults. Delete what isn't needed. -- Each fact lives in one place. Elsewhere, refer to it by path instead of repeating it. -- Write plain English in short sentences. - -## File header - -Every file that allows comments starts with a header. It goes after the shebang, if there is one: - -```sh -#!/bin/sh -# Image version -# -# Prints the build time in UTC (YYYYMMDDhhmm). Every build gets a higher version than the one -# before, which is how systemd-sysupdate tells that an update is newer. -``` - -- Line 1 is a title: a few words, in sentence case, with no full stop. -- Then a line with only the comment character. -- Then what the file does and why, naming the files it works with by path. - -Files that can't hold comments (JSON, `subuid`, `machine-id`) are explained in the file that uses -them or in the README. - -## Comments - -- Explain why, not what. Leave out comments that only repeat the code. -- Put a comment right above the line or block it is about. -- Wrap comments at 80 columns. diff --git a/mkosi.conf b/mkosi.conf index c8c71ef..2083e69 100644 --- a/mkosi.conf +++ b/mkosi.conf @@ -5,6 +5,9 @@ # 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. +# 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. [Distribution] Distribution=fedora @@ -15,30 +18,34 @@ Format=disk ImageId=agent OutputDirectory=mkosi.output SplitArtifacts=uki,partitions -# Lists the packages and versions in the image, to tell which builds a CVE -# affects. .gitea/workflows/os.yml publishes it with the image. +# ManifestFormat lists the packages and versions in the image, to tell which +# builds a CVE affects. .gitea/workflows/os.yml publishes it with the image. ManifestFormat=json [Build] -# Keeps downloaded packages between builds, also when mkosi runs in a -# throwaway container (AGENTS.md). dnf still installs the newest versions. +# 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 -# - kernel-core no drivers or firmware beyond what VMs need -# - systemd-container systemd-pull, which systemd-sysupdate downloads by -# - gnupg2 checks the signature of updates -# - util-linux, shadow-utils console login, setup, newuidmap for rootless podman -# - systemd-pam login sessions: Claude's user manager and its -# D-Bus, which rootless podman needs -# - openssh-server ssh login for the user (keys only) -# - zram-generator-defaults compressed swap in RAM -# - podman, podman-docker containers for Claude's toolchains (also as docker) -# - claude-code from mkosi.sandbox/etc/yum.repos.d -# - git-core, openssh-clients Claude Code uses git and pushes over ssh -# - the rest basic command-line tools Claude uses on the host +# Packages is systemd with its network services, plus: +# - kernel-core has no drivers or firmware beyond what VMs need. +# - systemd-container has systemd-pull, which systemd-sysupdate downloads with. +# - gnupg2 checks the signature of updates. +# - systemd-boot-unsigned is the boot loader, which mkosi signs. +# - util-linux and shadow-utils give the console login, the setup and +# newuidmap for rootless podman. +# - systemd-pam gives login sessions: Claude's user manager and its D-Bus, +# which rootless podman needs. +# - openssh-server lets you log in with ssh keys. +# - zram-generator-defaults sets up compressed swap in RAM. +# - podman and podman-docker run Claude's toolchains in containers. +# - claude-code comes from mkosi.sandbox/etc/yum.repos.d. +# - git-core and openssh-clients let Claude Code use git and push over ssh. +# - The rest are basic command-line tools Claude uses on the host. Packages= kernel-core systemd @@ -70,17 +77,18 @@ Packages= iproute WithDocs=no Locale=C.UTF-8 -# Name UKIs the way systemd-sysupdate names the ones it installs. +# UnifiedKernelImageFormat names UKIs the way systemd-sysupdate names the ones +# it installs. UnifiedKernelImageFormat=%i_%v -# - console= show the console on screen and on the serial port -# - systemd.machine_id= take 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 agent-setup asks its own questions -# - SYSTEMD_DEFAULT_MOUNT_ systemd handles mount changes at most 5 times per -# RATE_LIMIT_BURST= second and holds back mounts once it hits that. At -# boot that delays / and /var by up to a second each. -# The kernel passes this to systemd as environment. +# 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. +# - 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 +# as environment. KernelCommandLine= console=tty0 console=ttyS0,115200n8 @@ -88,7 +96,8 @@ KernelCommandLine= systemd.machine_id=firmware systemd.firstboot=no SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST=50 -# The initrd only needs to find and mount the disks of a VM. +# KernelInitrdModules are the modules the initrd needs to find and mount the +# disks of a VM. KernelInitrdModules= virtio_blk virtio_scsi @@ -97,10 +106,10 @@ KernelInitrdModules= dm-verity erofs ext4 -# 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 built into the -# kernel. +# 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 +# built into the kernel. KernelModules= /* -/drivers/* @@ -116,16 +125,16 @@ KernelModules= virtio-gpu veth tun -# - System.map only for debugging the kernel -# - udev hwdb hardware names and quirks, nothing a VM needs -# - homed, userdbd, unused services -# nsresourced -# - sshd-keygen sshd uses one host key, kept on /var -# (mkosi.extra/etc/ssh/sshd_config.d) -# - /afs mount point for the AFS network filesystem -# - /buildroot 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 drops what the image doesn't use: +# - 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 @@ -138,25 +147,25 @@ RemoveFiles= /buildroot [Validation] -# Sign systemd-boot and the UKI for Secure Boot, and put our keys on the ESP. +# SecureBoot signs systemd-boot and the UKI, and puts our keys on the ESP. # systemd-boot enrolls them at first boot if the firmware is in setup mode (no -# keys enrolled; in Proxmox, an EFI disk without pre-enrolled keys). -# mkosi.key is the SECUREBOOT_KEY secret, which .gitea/workflows/os.yml writes. +# keys enrolled; in Proxmox, an EFI disk without pre-enrolled keys). mkosi.key +# is the SECUREBOOT_KEY secret, which .gitea/workflows/os.yml writes. SecureBoot=yes SecureBootKey=mkosi.key SecureBootCertificate=mkosi.crt [Runtime] -# For testing with mkosi vm. It does the setup itself (the agent.* credentials, -# mkosi.extra/usr/libexec/agent-setup). Its console is hvc0, which the UKI -# doesn't use: with Secure Boot, systemd-stub ignores the console= mkosi -# passes. So the serial port joins that console, and replaces mkosi's network -# to forward ssh to port 2222. Ephemeral boots a copy, so test runs don't -# change the built image. +# 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. Ephemeral=yes CPUs=4 RAM=8G RuntimeSize=40G -RuntimeNetwork=none -QemuArgs=-serial chardev:console -nic user,model=virtio-net-pci,hostfwd=tcp::2222-:22 -Credentials=agent.password=test agent.hostname=agent-test mkosi.credentials/ +# QemuArgs joins the serial port to the console mkosi vm shows (hvc0). The UKI +# 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 diff --git a/mkosi.credentials/agent.ssh-key b/mkosi.credentials/agent.ssh-key deleted file mode 100755 index 0946e36..0000000 --- a/mkosi.credentials/agent.ssh-key +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -# SSH key for mkosi vm -# -# mkosi vm runs this and passes what it prints as the credential agent.ssh-key -# (mkosi.conf), so you can ssh into the test VM with your own keys. -cat ~/.ssh/*.pub 2>/dev/null || true diff --git a/mkosi.extra/etc/fstab b/mkosi.extra/etc/fstab index e7d9511..e05cac9 100644 --- a/mkosi.extra/etc/fstab +++ b/mkosi.extra/etc/fstab @@ -1,5 +1,11 @@ # 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. -PARTLABEL=var /var ext4 x-systemd.growfs 0 2 +# 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/ssh/sshd_config.d/10-agent.conf b/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf index 55f3472..adfdf0f 100644 --- a/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf +++ b/mkosi.extra/etc/ssh/sshd_config.d/10-agent.conf @@ -1,11 +1,22 @@ # ssh login # -# Only the user claude may log in, only with the key asked at setup. The host -# key lives on /var (agent-boot makes it), so it stays the same across reboots. -# The first value wins, and this file sorts before Fedora's 50-redhat.conf. +# 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 -AuthorizedKeysFile /var/lib/agent/authorized_keys -AllowUsers claude -PermitRootLogin no 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/usr/lib/systemd/network/80-dhcp.network b/mkosi.extra/usr/lib/systemd/network/80-dhcp.network index 18c9667..71bfcea 100644 --- a/mkosi.extra/usr/lib/systemd/network/80-dhcp.network +++ b/mkosi.extra/usr/lib/systemd/network/80-dhcp.network @@ -10,5 +10,6 @@ Kind=!* DHCP=yes [DHCPv4] -# The hostname is the one asked at setup (agent-boot), not the DHCP server's. +# 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 431fbe8..4b7379a 100644 --- a/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset +++ b/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset @@ -6,8 +6,8 @@ enable systemd-networkd.service enable systemd-resolved.service enable sshd.service -# Fedora disables the login on the screen (tty1, and tty2-6 through its -# autovt@ alias). +# 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 @@ -15,8 +15,8 @@ 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, and no system or config extensions. +# 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. disable authselect-apply-changes.service disable fips-crypto-policy-overlay.service disable systemd-sysext.service diff --git a/mkosi.extra/usr/lib/systemd/system/agent-setup@.service b/mkosi.extra/usr/lib/systemd/system/agent-setup@.service index 2dbc173..08cd9c8 100644 --- a/mkosi.extra/usr/lib/systemd/system/agent-setup@.service +++ b/mkosi.extra/usr/lib/systemd/system/agent-setup@.service @@ -18,7 +18,7 @@ StandardOutput=tty TTYPath=/dev/%I TTYReset=yes TTYVHangup=yes -# Being stopped by the other console is not a failure. +# SuccessExitStatus counts being stopped by the other console as success. SuccessExitStatus=SIGTERM [Install] diff --git a/mkosi.extra/usr/lib/systemd/system/claude-remote.service b/mkosi.extra/usr/lib/systemd/system/claude-remote.service index d1ef719..b709f32 100644 --- a/mkosi.extra/usr/lib/systemd/system/claude-remote.service +++ b/mkosi.extra/usr/lib/systemd/system/claude-remote.service @@ -11,7 +11,8 @@ After=network-online.target user@1000.service agent-boot.service [Service] User=claude WorkingDirectory=~ -Environment=CLAUDE_CONFIG_DIR=/var/lib/agent/claude XDG_RUNTIME_DIR=/run/user/1000 +EnvironmentFile=/etc/environment +Environment=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/tmpfiles.d/agent.conf b/mkosi.extra/usr/lib/tmpfiles.d/agent.conf index 81132bb..863212d 100644 --- a/mkosi.extra/usr/lib/tmpfiles.d/agent.conf +++ b/mkosi.extra/usr/lib/tmpfiles.d/agent.conf @@ -3,13 +3,14 @@ # What survives a reboot and what doesn't. Claude can only write to the wiped # directories, /tmp and its own config. -# Kept: the setup answers, the ssh host key and Claude's login. agent-boot -# wipes the rest of Claude's 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 - -# Wiped at every boot (D): Claude's home, with its podman storage, and -# temporary files. +# 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 - -# Start Claude's user manager at boot, for rootless podman and its own services. +# linger starts 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-setup b/mkosi.extra/usr/libexec/agent-setup index d1f2a4f..8d5b2ef 100755 --- a/mkosi.extra/usr/libexec/agent-setup +++ b/mkosi.extra/usr/libexec/agent-setup @@ -1,24 +1,25 @@ #!/bin/bash # First-boot setup # -# Asks for the root password, the hostname and the ssh key of the user claude -# (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. +# 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, agent.hostname and agent.ssh-key. mkosi -# vm passes them (mkosi.conf). +# 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 - # Both consoles run this script. Only one may do the setup. + # 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 - key=$(cred ssh-key) || true else echo "== Agent setup ==" while :; do @@ -28,18 +29,24 @@ else echo "Passwords do not match, try again." done read -rp "Hostname [agent]: " name - read -rp "SSH public key for the user claude: " key 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 -[[ -n $key ]] && install -m 0644 /dev/stdin /var/lib/agent/authorized_keys <<<"$key" -# Written last: its presence marks the setup as done. +# 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 -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. +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.repart/10-root.conf b/mkosi.repart/10-root.conf index 4cb3838..cf4907a 100644 --- a/mkosi.repart/10-root.conf +++ b/mkosi.repart/10-root.conf @@ -14,6 +14,6 @@ Verity=data VerityMatchKey=root SizeMinBytes=1G SizeMaxBytes=1G -# roothash= finds the root by its partition UUID, so the file name holds it -# and updates keep it (@u in mkosi.extra/usr/lib/sysupdate.d). +# 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