@@ -1,28 +1,29 @@
11 #!/usr/bin/env python3
2-"""Bakes conveyor's base VM image from the local CachyOS ISO, with QEMU run as the user.
3-conveyord runs every ticket in an overlay on this image. Run it again only to change the image:
4-delete images/base.qcow2 first.
2+"""Bakes conveyor's base VM image from Debian's cloud image for this host's architecture, with QEMU
3+run as the user. conveyord runs every ticket in an overlay on this image. Run it again only to change
4+the image: delete images/base.qcow2 first.
55
66 tools/bake.py bake
77
8-State lives in ~/.local/share/conveyor. Sockets live in /tmp/cv-*, because unix socket paths max out at 108 bytes.
8+State lives in ~/.local/share/conveyor. Sockets live in /tmp/cv-*, because unix socket paths max out at 104 bytes.
99 """
10-import base64, json, signal, socket, subprocess, sys, time
10+import http.server, json, os, platform, signal, socket, subprocess, sys, threading, time, urllib.request
1111 from pathlib import Path
1212
1313 DATA = Path.home() / ".local/share/conveyor"
1414 REPO = Path(__file__).resolve().parent.parent
1515 BASE = DATA / "images/base.qcow2"
16-VMS = DATA / "vms"
1716 KEY = DATA / "id_ed25519"
18-ISO = max(Path.home().glob("s/cachyos-desktop-linux*.iso"), default=None)
17+ARM = platform.machine() in ("arm64", "aarch64")
18+CLOUD = DATA / f"images/debian-13-{'arm64' if ARM else 'amd64'}.qcow2"
19+CLOUD_URL = f"https://cloud.debian.org/images/cloud/trixie/latest/{CLOUD.name.replace('debian-13', 'debian-13-genericcloud')}"
1920 USER, PASS = "sacenox", "zzzz"
20-# nodejs from extra, beside the simdjson it links: [cachyos] shadows it with a build that can lag simdjson's soname.
21-DEV_PKGS = "starship fzf tree-sitter-cli zig rustup extra/nodejs npm go python python-pip xdotool wmctrl maim xorg-xwininfo kitty nvim git openssh claude-code qemu-guest-agent vulkan-swrast ttf-jetbrains-mono ttf-jetbrains-mono-nerd".split()
22-XORG_PKGS = "libwnck3 mesa-utils xorg-xdpyinfo xorg-server xorg-xinit xorg-xinput xorg-xkill xorg-xrandr".split()
23-DE_PKGS = """blueman file-roller galculator gvfs lightdm lightdm-gtk-greeter network-manager-applet ristretto
24-thunar-archive-plugin xdg-user-dirs-gtk xed xfce4 xfce4-notifyd xfce4-pulseaudio-plugin xfce4-screenshooter
25-xfce4-taskmanager xfce4-whiskermenu-plugin""".split()
21+DEV_PKGS = """starship fzf tree-sitter-cli rustup nodejs npm golang-go python3 python3-pip xdotool wmctrl maim x11-utils
22+kitty neovim git curl build-essential libxkbcommon-dev qemu-guest-agent mesa-vulkan-drivers fonts-jetbrains-mono""".split()
23+XORG_PKGS = "libwnck-3-0 mesa-utils xserver-xorg xinit xinput x11-xserver-utils".split()
24+DE_PKGS = """blueman file-roller galculator gvfs lightdm lightdm-gtk-greeter ristretto thunar-archive-plugin
25+xdg-user-dirs-gtk mousepad xfce4 xfce4-notifyd xfce4-pulseaudio-plugin xfce4-screenshooter xfce4-taskmanager
26+xfce4-whiskermenu-plugin""".split()
2627
2728
2829 def sock(name, kind):
@@ -33,7 +34,7 @@ def log(*a):
3334 print(f"[{time.strftime('%H:%M:%S')}]", *a, flush=True)
3435
3536
36-# ---------- QMP and guest agent: JSON over a unix socket ----------
37+# ---------- QMP: JSON over a unix socket ----------
3738
3839 class Channel:
3940 def __init__(self, path, timeout=10):
@@ -72,86 +73,33 @@ def qmp(name):
7273 return c
7374
7475
75-def qga(name, timeout=5):
76- """Guest agent channel, synced: the agent answers guest-sync-delimited with 0xff then the reply."""
77- c = Channel(sock(name, "qga"), timeout)
78- c.s.sendall(b'{"execute":"guest-sync-delimited","arguments":{"id":4242}}\n')
79- data = b""
80- while True:
81- data += c.s.recv(65536)
82- if b"\xff" in data:
83- tail = data.split(b"\xff")[-1]
84- if b"\n" in tail:
85- c.buf = tail.split(b"\n", 1)[1]
86- return c
87-
88-
89-def wait_qga(name, secs):
90- end = time.monotonic() + secs
91- while time.monotonic() < end:
92- try:
93- c = qga(name, 3)
94- c.call("guest-ping")
95- return c
96- except (OSError, ConnectionError, RuntimeError):
97- time.sleep(2)
98- sys.exit(f"{name}: guest agent did not answer in {secs} s")
99-
100-
101-def guest_exec(name, cmd, timeout=3600):
102- c = wait_qga(name, 60)
103- pid = c.call("guest-exec", path="/bin/sh", arg=["-c", cmd], **{"capture-output": True})["pid"]
104- end = time.monotonic() + timeout
105- while True:
106- st = c.call("guest-exec-status", pid=pid)
107- if st.get("exited"):
108- break
109- if time.monotonic() > end:
110- sys.exit(f"guest command timed out: {cmd}")
111- time.sleep(2)
112- c.close()
113- out = base64.b64decode(st.get("out-data", "")).decode(errors="replace")
114- err = base64.b64decode(st.get("err-data", "")).decode(errors="replace")
115- return st.get("exitcode", 1), out, err
116-
117-
118-def guest_put(name, data: bytes, dst):
119- c = wait_qga(name, 60)
120- h = c.call("guest-file-open", path=dst, mode="w")
121- for i in range(0, len(data), 48 * 1024):
122- c.call("guest-file-write", handle=h, **{"buf-b64": base64.b64encode(data[i:i + 48 * 1024]).decode()})
123- c.call("guest-file-close", handle=h)
124- c.close()
125-
126-
12776 # ---------- QEMU ----------
12877
12978 def ssh_port(name):
13079 return 22000 + (sum(name.encode()) % 1000)
13180
13281
133-def boot(name, disk, mem_mb=8192, cpus=8, cdrom=None):
82+def boot(name, disk, mem_mb=8192, cpus=8, seed=None):
13483 for k in ("qmp", "qga", "vnc", "pid"):
13584 Path(sock(name, k)).unlink(missing_ok=True)
13685 args = [
137- "qemu-system-x86_64", "-name", name, "-enable-kvm", "-machine", "q35", "-cpu", "host",
138- "-smp", str(cpus), "-m", str(mem_mb),
139- "-drive", f"file={disk},if=virtio,format=qcow2,discard=unmap,cache=unsafe" if cdrom else
140- f"file={disk},if=virtio,format=qcow2,discard=unmap",
86+ f"qemu-system-{'aarch64' if ARM else 'x86_64'}", "-name", name, "-accel", "hvf" if sys.platform == "darwin" else "kvm",
87+ "-machine", "virt" if ARM else "q35", "-cpu", "host", "-smp", str(cpus), "-m", str(mem_mb),
88+ *(["-bios", "edk2-aarch64-code.fd"] if ARM else []),
89+ "-drive", f"file={disk},if=virtio,format=qcow2,discard=unmap",
14190 "-netdev", f"user,id=n0,hostfwd=tcp:127.0.0.1:{ssh_port(name)}-:22", "-device", "virtio-net-pci,netdev=n0",
14291 "-device", "virtio-balloon-pci,free-page-reporting=on",
14392 "-device", "virtio-serial-pci",
14493 "-chardev", f"socket,path={sock(name, 'qga')},server=on,wait=off,id=qga0",
14594 "-device", "virtserialport,chardev=qga0,name=org.qemu.guest_agent.0",
14695 "-qmp", f"unix:{sock(name, 'qmp')},server=on,wait=off",
147- "-vga", "virtio", "-display", "none", "-vnc", f"unix:{sock(name, 'vnc')}",
96+ *(["-device", "virtio-gpu-pci"] if ARM else ["-vga", "virtio"]), "-display", "none", "-vnc", f"unix:{sock(name, 'vnc')}",
14897 "-device", "qemu-xhci", "-device", "usb-tablet",
14998 "-pidfile", sock(name, "pid"), "-daemonize",
15099 ]
151- if cdrom:
152- args += ["-cdrom", str(cdrom), "-boot", "order=d"]
100+ if seed:
101+ args += ["-smbios", f"type=1,serial=ds=nocloud;s={seed}"]
153102 subprocess.run(args, check=True)
154- return int(Path(sock(name, "pid")).read_text())
155103
156104
157105 def ssh(name, *cmd, check=True, timeout=None):
@@ -175,10 +123,8 @@ def wait_ssh(name, secs=300):
175123
176124 def poweroff(name, secs=120):
177125 try:
178- c = qga(name, 5)
179- c.s.sendall(b'{"execute":"guest-shutdown","arguments":{"mode":"powerdown"}}\n')
180- c.close()
181- except OSError:
126+ qmp(name).call("system_powerdown")
127+ except (OSError, RuntimeError):
182128 pass
183129 pid_file = Path(sock(name, "pid"))
184130 end = time.monotonic() + secs
@@ -199,30 +145,20 @@ def poweroff(name, secs=120):
199145
200146 # ---------- bake ----------
201147
202-INSTALLER_SETTINGS = {
203- "install_type": "simple", "headless_mode": True, "device": "/dev/vda", "fs_name": "ext4",
204- "partitions": [{"name": "/dev/vda1", "mountpoint": "/", "size": "100%", "type": "root"}],
205- "hostname": "conveyor-base", "locale": "en_US.UTF-8", "xkbmap": "us", "timezone": "UTC",
206- "user_name": USER, "user_pass": PASS, "user_shell": "/bin/bash", "root_pass": PASS,
207- "kernel": "linux-cachyos", "desktop": "xfce", "bootloader": "grub", "hw_clock": "utc",
208- "carry_network": True, "os_prober": False, "hostcache": True, "autologin": True,
209- "net_profiles_path": "file:///root/net-profiles.toml",
210-}
211-
212148 PROVISION = f"""#!/bin/bash
213149 set -euo pipefail
214-pacman -Syyuu --noconfirm
215-pacman -S --needed --noconfirm {' '.join(DEV_PKGS + DE_PKGS + XORG_PKGS)}
216-systemctl enable sshd.service lightdm.service qemu-guest-agent.service
217-command -v ufw >/dev/null && ufw allow 22/tcp || true
218-groupadd -f autologin
219-gpasswd -a {USER} autologin
150+export DEBIAN_FRONTEND=noninteractive
151+apt-get update
152+apt-get -y full-upgrade
153+apt-get install -y {' '.join(DEV_PKGS + DE_PKGS + XORG_PKGS)}
154+# The cloud kernel has no GPU driver, and so no desktop: swap it for the generic one.
155+apt-get install -y linux-image-$(dpkg --print-architecture)
156+apt-get purge -y linux-image-cloud-$(dpkg --print-architecture) 'linux-image-*-cloud-*'
157+systemctl enable lightdm.service qemu-guest-agent.service
158+systemctl set-default graphical.target
220159 install -d /etc/lightdm/lightdm.conf.d
221160 printf '%s\\n' '[Seat:*]' 'autologin-user={USER}' 'autologin-user-timeout=0' 'autologin-session=xfce' > /etc/lightdm/lightdm.conf.d/50-autologin.conf
222-usermod -aG wheel {USER}
223161 systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
224-install -d -o {USER} -g {USER} -m 700 /home/{USER}/.ssh
225-install -o {USER} -g {USER} -m 600 /root/host_key.pub /home/{USER}/.ssh/authorized_keys
226162 git clone --depth 1 https://github.com/sacenox/dotfiles /tmp/dotfiles
227163 cp -a /tmp/dotfiles/. /home/{USER}/
228164 rm -rf /home/{USER}/.git /home/{USER}/README.md
@@ -238,64 +174,78 @@ cat > /home/{USER}/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-power-manager.
238174 XML
239175 chown -R {USER}:{USER} /home/{USER}
240176 runuser -u {USER} -- env HOME=/home/{USER} npm config set prefix /home/{USER}/.local
177+runuser -u {USER} -- env HOME=/home/{USER} bash -c 'curl -fsSL https://claude.ai/install.sh | bash'
241178 # A restored snapshot can leave a stale forwarded socket behind; let a new session replace it.
242-printf 'StreamLocalBindUnlink yes\n' > /etc/ssh/sshd_config.d/50-conveyor.conf
243-# No welcome app in a worker VM.
244-rm -f /etc/xdg/autostart/cachyos-hello.desktop /home/{USER}/.config/autostart/cachyos-hello.desktop
245-# Every overlay must get its own machine id and ssh host keys at first boot.
246-rm -f /etc/ssh/ssh_host_*
247-: > /etc/machine-id
179+printf 'StreamLocalBindUnlink yes\\n' > /etc/ssh/sshd_config.d/50-conveyor.conf
180+# Every overlay gets its own ssh host keys at first boot: Debian's sshd won't start without them.
181+install -d /etc/systemd/system/ssh.service.d
182+printf '%s\\n' '[Service]' 'ExecStartPre=' 'ExecStartPre=/usr/bin/ssh-keygen -A' 'ExecStartPre=/usr/sbin/sshd -t' > /etc/systemd/system/ssh.service.d/keygen.conf
183+# The seed is gone after the bake: cloud-init must not run again.
184+touch /etc/cloud/cloud-init.disabled
248185 """
249186
250187
188+def serve_seed():
189+ """cloud-init's NoCloud seed over HTTP: the guest reaches the host's loopback at 10.0.2.2."""
190+ files = {"/meta-data": b"instance-id: conveyor-base\nlocal-hostname: conveyor-base\n", "/vendor-data": b"",
191+ "/user-data": b"#cloud-config\n" + json.dumps({
192+ "users": [{"name": USER, "groups": ["sudo"], "shell": "/bin/bash", "lock_passwd": False,
193+ "plain_text_passwd": PASS, "ssh_authorized_keys": [KEY.with_suffix(".pub").read_text().strip()]}],
194+ "write_files": [{"path": "/root/provision.sh", "permissions": "0700", "content": PROVISION}],
195+ "runcmd": [["sh", "-c", "/root/provision.sh > /var/log/provision.log 2>&1 && touch /var/log/provision.ok"]],
196+ }).encode()}
197+
198+ class Seed(http.server.BaseHTTPRequestHandler):
199+ def do_GET(self):
200+ body = files.get(self.path)
201+ self.send_response(200 if body is not None else 404)
202+ self.end_headers()
203+ self.wfile.write(body or b"")
204+
205+ def log_message(self, *a):
206+ pass
207+
208+ srv = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Seed)
209+ threading.Thread(target=srv.serve_forever, daemon=True).start()
210+ return f"http://10.0.2.2:{srv.server_port}/"
211+
212+
251213 def bake():
252214 if BASE.exists():
253215 sys.exit(f"{BASE} exists. Delete it to bake again.")
254- if not ISO:
255- sys.exit("no ~/s/cachyos-desktop-linux*.iso")
256216 BASE.parent.mkdir(parents=True, exist_ok=True)
217+ if not CLOUD.exists():
218+ log(f"downloading {CLOUD_URL}")
219+ urllib.request.urlretrieve(CLOUD_URL, CLOUD.with_suffix(".part"))
220+ CLOUD.with_suffix(".part").rename(CLOUD)
257221 if not KEY.exists():
258- subprocess.run(["ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", "conveyor-spike", "-f", str(KEY)], check=True)
222+ subprocess.run(["ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", "conveyor", "-f", str(KEY)], check=True)
259223 tmp = BASE.with_suffix(".baking.qcow2")
260- tmp.unlink(missing_ok=True)
261- subprocess.run(["qemu-img", "create", "-q", "-f", "qcow2", str(tmp), "60G"], check=True)
224+ subprocess.run(["qemu-img", "convert", "-q", "-O", "qcow2", str(CLOUD), str(tmp)], check=True)
225+ subprocess.run(["qemu-img", "resize", "-q", str(tmp), "60G"], check=True)
262226 t0 = time.monotonic()
263- log(f"booting {ISO.name}")
264- boot("bake", tmp, 8192, 8, cdrom=ISO)
265- wait_qga("bake", 600).close()
266- log(f"live environment up after {time.monotonic() - t0:.0f} s")
267- guest_put("bake", json.dumps(INSTALLER_SETTINGS).encode(), "/root/settings.json")
268- profiles = "[desktop.xfce]\npackages = [\n" + ",\n".join(f' "{p}"' for p in DE_PKGS) + "\n]\n"
269- guest_put("bake", profiles.encode(), "/root/net-profiles.toml")
270- log("running cachyos-installer")
271- rc, out, err = guest_exec("bake", "cachyos-installer --config /root/settings.json > /root/install.log 2>&1; rc=$?; tail -n 30 /root/install.log; exit $rc")
272- if rc:
273- sys.exit(f"installer failed ({rc}):\n{out}{err}")
274- log(f"installed after {time.monotonic() - t0:.0f} s; provisioning")
275- guest_put("bake", PROVISION.encode(), "/root/provision.sh")
276- guest_put("bake", KEY.with_suffix(".pub").read_bytes(), "/root/host_key.pub")
277- # The host's repos: the installer leaves the v3 ones out, so the upgrade would downgrade it all to generic builds.
278- guest_put("bake", Path("/etc/pacman.conf").read_bytes(), "/root/pacman.conf")
279- rc, out, err = guest_exec("bake", "mount /dev/vda1 /mnt && install -m700 /root/provision.sh /root/host_key.pub /mnt/root/ "
280- "&& install -m644 /root/pacman.conf /mnt/etc/pacman.conf "
281- "&& cp --dereference /etc/resolv.conf /mnt/etc/resolv.conf "
282- "&& arch-chroot /mnt /root/provision.sh > /root/provision.log 2>&1; rc=$?; "
283- "tail -n 40 /root/provision.log; umount -R /mnt 2>/dev/null; sync; exit $rc")
284- if rc:
285- sys.exit(f"provisioning failed ({rc}):\n{out}{err}")
286- log(f"provisioned after {time.monotonic() - t0:.0f} s; powering off")
227+ log(f"booting {CLOUD.name}")
228+ boot("bake", tmp, seed=serve_seed())
229+ if not wait_ssh("bake", 300):
230+ sys.exit("the cloud image does not answer ssh")
231+ log(f"up after {time.monotonic() - t0:.0f} s; provisioning")
232+ r = ssh("bake", "cloud-init status --wait >/dev/null; tail -n 40 /var/log/provision.log; test -e /var/log/provision.ok", check=False)
233+ if r.returncode:
234+ sys.exit(f"provisioning failed:\n{r.stdout or ''}")
235+ log(f"provisioned after {time.monotonic() - t0:.0f} s; rebooting into the desktop")
287236 poweroff("bake")
288- log("first boot from disk, to check it")
289- boot("bake", tmp, 8192, 8)
237+ boot("bake", tmp)
290238 if not wait_ssh("bake", 300):
291239 sys.exit("baked image does not answer ssh")
292- r = ssh("bake", "systemctl is-system-running --wait; pacman -Q claude-code xfce4-session qemu-guest-agent; pgrep -u sacenox xfce4-session", check=False)
240+ r = ssh("bake", "systemctl is-system-running --wait; ~/.local/bin/claude --version; dpkg-query -W xfce4-session qemu-guest-agent; "
241+ "pgrep -u sacenox xfce4-session", check=False)
293242 log("verify:", (r.stdout or "").strip().replace("\n", " | "))
294243 # Rust and a warm cargo cache, so a widget ticket builds in seconds, not minutes.
295244 log("pre-building the widget library")
296245 # In ~/work, where agents build: cargo fingerprints workspace crates by path.
297- tar = subprocess.run(["tar", "-C", str(REPO / "templates"), "--exclude=library/target", "-cf", "-", "library"],
298- capture_output=True, check=True).stdout
246+ # Without macOS metadata: its ._* files would read as widgets.
247+ tar = subprocess.run(["tar", "--no-xattrs", "-C", str(REPO / "templates"), "--exclude=library/target", "-cf", "-", "library"],
248+ capture_output=True, check=True, env={**os.environ, "COPYFILE_DISABLE": "1"}).stdout
299249 subprocess.run(["ssh", "-i", str(KEY), "-p", str(ssh_port("bake")), "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no",
300250 "-o", "UserKnownHostsFile=/dev/null", "-o", "LogLevel=ERROR", f"{USER}@127.0.0.1", "tar -xf - -C /tmp && mv /tmp/library ~/work"], input=tar, check=True)
301251 r = ssh("bake", "rustup default 1.99.1 >/dev/null 2>&1 && cd ~/work && CARGO_TARGET_DIR=$HOME/.cache/conveyor-target "