authoravatarclover caruso<git@paperclover.net>2026-10-20 13:16:29-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-21 02:52:29-07:00
log324d9364aab99a3828fd57085de6970258f52b0c
tree968bf29a5a50074329d5f31b913c08167cb9bc02
parent809516b87e112259617b90675369cdb1f08bde27
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Integrate the Mac development VM into the authenticated dashboard

Register the existing Tart guest through a pinned, restricted SSH bridge. Keep its identity visible when Mac Studio is unreachable; expose supported power controls, reconnectable screen streaming, and Unicode clipboard transfer without a public VNC listener. Preserve local VM behavior and exclude remote memory from Snow Globe capacity. Bound lifecycle cleanup, retain observed hardware separately from live availability, and document the persistent host entry point. Verified: Rust 57 tests; host/VM Python checks; five Mac protocol/failure tests; 25 Linux stream tests; TypeScript and web build; NixOS system build; preview sign-in and inventory; isolated real lifecycle, ISO, preset, clone and path-boundary checks; repeated Mac RFB stream connections. Personal Mac guest remained running. Assisted-by: gpt-6-astra

22 files changed, 1180 insertions(+), 63 deletions(-)

config/mac-studio-sandbox.json+2-1
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1{1{
2 "dataset": "globe/clover/mac-studio-sandbox",2 "dataset": "globe/clover/mac-studio-sandbox",
3 "mountPoint": "/srv/mac-studio-vm/sandbox"3 "mountPoint": "/srv/mac-studio-vm/sandbox",
4 "clientAddress": "10.0.0.222"
4}5}
dashboard/src/apps.rs+5-2
...@@ -299,7 +299,7 @@ pub async fn route(...@@ -299,7 +299,7 @@ pub async fn route(
299 }299 }
300 let allocated: f64 = array(&values[1])300 let allocated: f64 = array(&values[1])
301 .iter()301 .iter()
302 .filter(|vm| !["shutoff", "crashed"].contains(&string(&vm["state"])))302 .filter(|vm| vm["host"] == values[0]["name"] && !["shutoff", "crashed"].contains(&string(&vm["state"])))
303 .map(|vm| number(&vm["balloon"]))303 .map(|vm| number(&vm["balloon"]))
304 .sum();304 .sum();
305 values[0]["availableMemory"] =305 values[0]["availableMemory"] =
...@@ -361,6 +361,9 @@ pub async fn route(...@@ -361,6 +361,9 @@ pub async fn route(
361 ["vms", name, tail @ ..] => {361 ["vms", name, tail @ ..] => {
362 vm_name(name)?;362 vm_name(name)?;
363 match tail {363 match tail {
364 ["clipboard"] if method == Method::POST => {
365 vm_call("clipboard", Some(json!({"name":name,"text":body["text"]}))).await?;
366 }
364 ["access"] if method == Method::GET => {367 ["access"] if method == Method::GET => {
365 let mut response =368 let mut response =
366 Document::new(vm_call("access", Some(json!({"name":name}))).await?)369 Document::new(vm_call("access", Some(json!({"name":name}))).await?)
...@@ -410,7 +413,7 @@ pub async fn route(...@@ -410,7 +413,7 @@ pub async fn route(
410 }413 }
411 [action]414 [action]
412 if method == Method::POST415 if method == Method::POST
413 && ["start", "shutdown", "reboot", "destroy", "resume"]416 && ["start", "shutdown", "reboot", "destroy", "resume", "suspend"]
414 .contains(action) =>417 .contains(action) =>
415 {418 {
416 vm_call("act", Some(json!({"name":name,"action":action}))).await?;419 vm_call("act", Some(json!({"name":name,"action":action}))).await?;
dashboard/src/host.rs+2
...@@ -34,6 +34,8 @@ pub async fn open(request: Value) -> Result<(Value, tokio::net::UnixStream)> {...@@ -34,6 +34,8 @@ pub async fn open(request: Value) -> Result<(Value, tokio::net::UnixStream)> {
34 Some("vm.create" | "vm.media" | "vm.preset")34 Some("vm.create" | "vm.media" | "vm.preset")
35 ) {35 ) {
36 91036 910
37 } else if request["operation"] == "vm.act" {
38 240
37 } else if request["operation"] == "copyparty.manage" {39 } else if request["operation"] == "copyparty.manage" {
38 31040 310
39 } else {41 } else {
dashboard/web/api.contract.ts+4-1
...@@ -320,6 +320,9 @@ export type Api = Hono<{}, {...@@ -320,6 +320,9 @@ export type Api = Hono<{}, {
320 "/vms/:name/media": {320 "/vms/:name/media": {
321 $put: Endpoint<null, { param: { name: string }; json: { image: string } }, 204, "body">;321 $put: Endpoint<null, { param: { name: string }; json: { image: string } }, 204, "body">;
322 };322 };
323 "/vms/:name/clipboard": {
324 $post: Endpoint<null, { param: { name: string }; json: { text: string } }>;
325 };
323 "/vms/:name/access": {326 "/vms/:name/access": {
324 $get: Endpoint<{ username: string; password: string; hostname: string } | null, { param: { name: string } }>;327 $get: Endpoint<{ username: string; password: string; hostname: string } | null, { param: { name: string } }>;
325 };328 };
...@@ -327,7 +330,7 @@ export type Api = Hono<{}, {...@@ -327,7 +330,7 @@ export type Api = Hono<{}, {
327 $post: Endpoint<null, { param: { name: string }; json: { id: string; os: string; description: string } }, 204, "body">;330 $post: Endpoint<null, { param: { name: string }; json: { id: string; os: string; description: string } }, 204, "body">;
328 };331 };
329 "/vms/:name/:action": {332 "/vms/:name/:action": {
330 $post: Endpoint<null, { param: { name: string; action: "start" | "destroy" | "shutdown" | "reboot" | "resume"; }; }, 204, "body">;333 $post: Endpoint<null, { param: { name: string; action: "start" | "destroy" | "shutdown" | "reboot" | "resume" | "suspend"; }; }, 204, "body">;
331 };334 };
332 "/account": {335 "/account": {
333 $get: Endpoint<User & {picture: string | null; credentials:Credential[]; commitIdentity: import("./types/users.ts").CommitIdentity}>;336 $get: Endpoint<User & {picture: string | null; credentials:Credential[]; commitIdentity: import("./types/users.ts").CommitIdentity}>;
dashboard/web/components/VMConsole.tsx+14-6
...@@ -9,6 +9,7 @@ import Check from "lucide-solid/icons/check";...@@ -9,6 +9,7 @@ import Check from "lucide-solid/icons/check";
9import RemoteKeyboard from "@novnc/keyboard";9import RemoteKeyboard from "@novnc/keyboard";
10import { VM_SETUP, type Domain } from "../types/vms.ts";10import { VM_SETUP, type Domain } from "../types/vms.ts";
11import { StatusLabel } from "./Status.tsx";11import { StatusLabel } from "./Status.tsx";
12import { toast } from "./Toast.tsx";
1213
13type WindowSurface = {14type WindowSurface = {
14 id: number; x: number; y: number; width: number; height: number; atlas_x: number; atlas_y: number;15 id: number; x: number; y: number; width: number; height: number; atlas_x: number; atlas_y: number;
...@@ -16,7 +17,7 @@ type WindowSurface = {...@@ -16,7 +17,7 @@ type WindowSurface = {
16};17};
17type WindowFrame = { windows: WindowSurface[]; rtpTimestamp: number };18type WindowFrame = { windows: WindowSurface[]; rtpTimestamp: number };
1819
19export function VMConsole(props: { name: string; enabled: boolean; active: boolean; setup?: Domain["setup"]; toolbar: JSX.Element; content: (toolbar: HTMLDivElement) => JSX.Element }) {20export function VMConsole(props: { name: string; platform: Domain["platform"]; enabled: boolean; active: boolean; setup?: Domain["setup"]; unavailable?: string; clipboard?: (text: string) => Promise<void>; toolbar: JSX.Element; content: (toolbar: HTMLDivElement) => JSX.Element }) {
20 const enabled = createMemo(() => props.enabled);21 const enabled = createMemo(() => props.enabled);
21 const preparing = () => !!props.setup && props.setup !== "ready";22 const preparing = () => !!props.setup && props.setup !== "ready";
22 let reconnectTimer: number | undefined;23 let reconnectTimer: number | undefined;
...@@ -72,6 +73,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole...@@ -72,6 +73,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole
72 const connected = () => state() === "Connected";73 const connected = () => state() === "Connected";
73 const [problem, setProblem] = createSignal("");74 const [problem, setProblem] = createSignal("");
74 const [clipboard, setClipboard] = createSignal("");75 const [clipboard, setClipboard] = createSignal("");
76 const [sendingClipboard, setSendingClipboard] = createSignal(false);
75 const [showClipboard, setShowClipboard] = createSignal(false);77 const [showClipboard, setShowClipboard] = createSignal(false);
76 const [windowsAvailable, setWindowsAvailable] = createSignal(false);78 const [windowsAvailable, setWindowsAvailable] = createSignal(false);
77 const [separateWindows, setSeparateWindows] = createSignal(false);79 const [separateWindows, setSeparateWindows] = createSignal(false);
...@@ -389,7 +391,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole...@@ -389,7 +391,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole
389 {props.toolbar}391 {props.toolbar}
390 <Show when={props.active}>392 <Show when={props.active}>
391 <VMMenu label="Input">393 <VMMenu label="Input">
392 <button disabled={!connected()} onClick={ctrlAltDel}><Keyboard size={14} aria-hidden="true" />Ctrl+Alt+Del</button>394 <Show when={props.platform !== "macos"}><button disabled={!connected()} onClick={ctrlAltDel}><Keyboard size={14} aria-hidden="true" />Ctrl+Alt+Del</button></Show>
393 <button disabled={!connected()} onClick={() => setShowClipboard(!showClipboard())}><Clipboard size={14} aria-hidden="true" />Clipboard</button>395 <button disabled={!connected()} onClick={() => setShowClipboard(!showClipboard())}><Clipboard size={14} aria-hidden="true" />Clipboard</button>
394 </VMMenu>396 </VMMenu>
395 <VMMenu label="View">397 <VMMenu label="View">
...@@ -404,7 +406,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole...@@ -404,7 +406,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole
404 </VMMenu>406 </VMMenu>
405 <span class="spacer" />407 <span class="spacer" />
406 <Show when={connected() && !preparing()}><span class="vm-console-status" id={`keyboard-release-${props.name}`}>Ctrl+Alt+Esc releases keyboard</span></Show>408 <Show when={connected() && !preparing()}><span class="vm-console-status" id={`keyboard-release-${props.name}`}>Ctrl+Alt+Esc releases keyboard</span></Show>
407 <span class="vm-console-status" role="status">{preparing() ? "" : props.enabled ? state() : "Off"}</span>409 <span class="vm-console-status" role="status">{preparing() ? "" : props.enabled ? state() : props.unavailable ? "Unavailable" : "Off"}</span>
408 </Show>410 </Show>
409 </div>411 </div>
410 <Show when={props.active && showStats()}>412 <Show when={props.active && showStats()}>
...@@ -418,9 +420,15 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole...@@ -418,9 +420,15 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole
418 <Show when={props.active && showClipboard()}>420 <Show when={props.active && showClipboard()}>
419 <div class="vm-clipboard">421 <div class="vm-clipboard">
420 <label class="field">clipboard<textarea class="search" rows="3" value={clipboard()} onInput={(event) => setClipboard(event.currentTarget.value)} /></label>422 <label class="field">clipboard<textarea class="search" rows="3" value={clipboard()} onInput={(event) => setClipboard(event.currentTarget.value)} /></label>
421 <span class="hint">Clipboard sharing requires support in the guest</span>423 <span class="hint">{props.clipboard ? "Send text, then paste in the VM with Command+V." : "Clipboard sharing requires support in the guest"}</span>
422 <div class="row">424 <div class="row">
423 <button class="button small" disabled={!connected()} onClick={() => send({ type: "clipboard", text: clipboard() })}>send text</button>425 <button class="button small" disabled={!connected() || sendingClipboard()} aria-busy={sendingClipboard()} onClick={async () => {
426 if (!props.clipboard) { send({ type: "clipboard", text: clipboard() }); return; }
427 setSendingClipboard(true);
428 try { await props.clipboard(clipboard()); setProblem(""); toast("Text sent to the VM clipboard."); }
429 catch (error) { setProblem(reason(error)); }
430 finally { setSendingClipboard(false); }
431 }}>send text</button>
424 <button class="button small" onClick={() => navigator.clipboard.writeText(clipboard()).catch((failure) => setProblem(reason(failure)))}>copy text</button>432 <button class="button small" onClick={() => navigator.clipboard.writeText(clipboard()).catch((failure) => setProblem(reason(failure)))}>copy text</button>
425 </div>433 </div>
426 </div>434 </div>
...@@ -461,6 +469,6 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole...@@ -461,6 +469,6 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole
461 <img ref={cursorInvert} class="vm-cursor vm-cursor-invert" alt="" aria-hidden="true" />469 <img ref={cursorInvert} class="vm-cursor vm-cursor-invert" alt="" aria-hidden="true" />
462 </div>470 </div>
463 <Show when={tools()}>{(toolbar) => props.content(toolbar())}</Show>471 <Show when={tools()}>{(toolbar) => props.content(toolbar())}</Show>
464 <Show when={props.active && !props.enabled}><div class="empty"><p>The VM is off. Start it from the Machine menu.</p></div></Show>472 <Show when={props.active && !props.enabled}><div class="empty"><p>{props.unavailable ?? "The VM is off. Start it from the Machine menu."}</p></div></Show>
465 </div>;473 </div>;
466}474}
dashboard/web/pages/VMs.tsx+36-22
...@@ -14,6 +14,7 @@ import RotateCw from "lucide-solid/icons/rotate-cw";...@@ -14,6 +14,7 @@ import RotateCw from "lucide-solid/icons/rotate-cw";
14import Save from "lucide-solid/icons/save";14import Save from "lucide-solid/icons/save";
15import Trash from "lucide-solid/icons/trash";15import Trash from "lucide-solid/icons/trash";
16import Square from "lucide-solid/icons/square";16import Square from "lucide-solid/icons/square";
17import Pause from "lucide-solid/icons/pause";
17import KeyRound from "lucide-solid/icons/key-round";18import KeyRound from "lucide-solid/icons/key-round";
18import Usb from "lucide-solid/icons/usb";19import Usb from "lucide-solid/icons/usb";
19import { createEffect, createMemo, createResource, createSignal, For, lazy, onCleanup, Show } from "solid-js";20import { createEffect, createMemo, createResource, createSignal, For, lazy, onCleanup, Show } from "solid-js";
...@@ -53,6 +54,8 @@ const isOff = (vm: Domain) => vm.state === "shutoff" || vm.state === "crashed";...@@ -53,6 +54,8 @@ const isOff = (vm: Domain) => vm.state === "shutoff" || vm.state === "crashed";
53const running = (vm: Domain) => vm.state === "running" || vm.state === "blocked";54const running = (vm: Domain) => vm.state === "running" || vm.state === "blocked";
5455
55function status(vm: Domain): [StatusKind, string, tip?: string] {56function status(vm: Domain): [StatusKind, string, tip?: string] {
57 if (vm.state === "unreachable") return ["down", "unreachable", `${vm.host} is not answering`];
58 if (vm.pendingAction) return ["restarting", vm.pendingAction === "start" ? "starting" : vm.pendingAction === "suspend" ? "suspending" : vm.pendingAction === "reboot" ? "restarting" : "stopping"];
56 if (running(vm)) {59 if (running(vm)) {
57 if (vm.setup && vm.setup !== "ready") return [vm.setup === "failed" ? "down" : vm.setup === "restarting" ? "restarting" : "starting", VM_SETUP[vm.setup]];60 if (vm.setup && vm.setup !== "ready") return [vm.setup === "failed" ? "down" : vm.setup === "restarting" ? "restarting" : "starting", VM_SETUP[vm.setup]];
58 if (vm.startedAt === null) return ["healthy", "up"];61 if (vm.startedAt === null) return ["healthy", "up"];
...@@ -87,7 +90,7 @@ const threads = (list: number[]) => list...@@ -87,7 +90,7 @@ const threads = (list: number[]) => list
87 .join(", ");90 .join(", ");
8891
89/** Current and max memory, like "16 GiB", or "8–12 GiB" while the balloon holds some back. */92/** Current and max memory, like "16 GiB", or "8–12 GiB" while the balloon holds some back. */
90const allocated = (vm: Domain) => (vm.balloon < vm.memory ? `${+(vm.balloon / GiB).toFixed(1)}–${bytes(vm.memory)}` : bytes(vm.memory));93const allocated = (vm: Domain) => vm.memory === null ? "—" : vm.balloon !== null && vm.balloon < vm.memory ? `${+(vm.balloon / GiB).toFixed(1)}–${bytes(vm.memory)}` : bytes(vm.memory);
9194
92/** Remote desktop for Windows, the web UI for Home Assistant, ssh for the rest. */95/** Remote desktop for Windows, the web UI for Home Assistant, ssh for the rest. */
93function remote(os: string, address: string): [href: string, label: string] {96function remote(os: string, address: string): [href: string, label: string] {
...@@ -350,22 +353,22 @@ function Hardware(props: { vm: Domain; refetch: () => unknown; username?: string...@@ -350,22 +353,22 @@ function Hardware(props: { vm: Domain; refetch: () => unknown; username?: string
350 };353 };
351 return (354 return (
352 <div class="vm-detail">355 <div class="vm-detail">
353 <div class="vm-media">356 <Show when={props.vm.capabilities.includes("media")}><div class="vm-media">
354 <span class="muted">CD drive</span>357 <span class="muted">CD drive</span>
355 <span class="source">{props.vm.media.find((item) => item.source)?.source ?? "No ISO attached"}</span>358 <span class="source">{props.vm.media.find((item) => item.source)?.source ?? "No ISO attached"}</span>
356 <span class="spacer" /><span class="muted">{props.vm.firmware === "uefi" ? "UEFI" : "Legacy BIOS"}</span>359 <span class="spacer" /><span class="muted">{props.vm.firmware === "uefi" ? "UEFI" : "Legacy BIOS"}</span>
357 </div>360 </div></Show>
358 <Loaded data={history} what="usage history" retry={refetch} skeleton={361 <Loaded data={history} what="usage history" retry={refetch} skeleton={
359 <div class="vm-charts"><div class="skeleton" /><div class="skeleton" /></div>362 <div class="vm-charts"><div class="skeleton" /><div class="skeleton" /></div>
360 }>363 }>
361 {(latest) => {364 {(latest) => {
362 const samples = () => latest().domains[props.vm.name];365 const samples = () => latest().domains[props.vm.name];
363 return (366 return (
364 <Show when={samples()?.cpu.some((v) => v !== null)} fallback={<p class="off-hour">Off for the last hour</p>}>367 <Show when={samples()?.cpu.some((v) => v !== null)} fallback={<p class="off-hour">No usage samples in the last hour</p>}>
365 <div class="vm-charts">368 <div class="vm-charts">
366 <TimeChart series={[{ name: "cpu", t: latest().t, v: samples()!.cpu }]} format={percent} max={100}369 <TimeChart series={[{ name: "cpu", t: latest().t, v: samples()!.cpu }]} format={percent} max={100}
367 colors={["var(--series-1)"]} height={104} inline sync={`vm-${props.vm.name}`} />370 colors={["var(--series-1)"]} height={104} inline sync={`vm-${props.vm.name}`} />
368 <TimeChart series={[{ name: "memory", t: latest().t, v: samples()!.memory }]} format={bytes} max={props.vm.memory}371 <TimeChart series={[{ name: "memory", t: latest().t, v: samples()!.memory }]} format={bytes} max={props.vm.memory ?? undefined}
369 colors={["var(--series-2)"]} height={104} inline sync={`vm-${props.vm.name}`} />372 colors={["var(--series-2)"]} height={104} inline sync={`vm-${props.vm.name}`} />
370 </div>373 </div>
371 </Show>374 </Show>
...@@ -375,19 +378,25 @@ function Hardware(props: { vm: Domain; refetch: () => unknown; username?: string...@@ -375,19 +378,25 @@ function Hardware(props: { vm: Domain; refetch: () => unknown; username?: string
375 <div class="facts">378 <div class="facts">
376 <div class="settings">379 <div class="settings">
377 <dl class="kv">380 <dl class="kv">
381 <dt>host</dt><dd>{props.vm.host}</dd>
382 <dt>architecture</dt><dd>{props.vm.arch}</dd>
378 <Show when={props.username}><dt>guest user</dt><dd>{props.username}</dd></Show>383 <Show when={props.username}><dt>guest user</dt><dd>{props.username}</dd></Show>
379 <Show when={props.hostname}>{(hostname) => <><dt>hostname</dt><dd><Copy value={hostname()} /></dd></>}</Show>384 <Show when={props.hostname}>{(hostname) => <><dt>hostname</dt><dd><Copy value={hostname()} /></dd></>}</Show>
380 <dt>for</dt>385 <dt>for</dt>
381 <dd>386 <dd>
387 <Show when={props.vm.capabilities.includes("edit")} fallback={props.vm.description}>
382 <button class="describe" classList={{ empty: !props.vm.description }} data-tip="edit"388 <button class="describe" classList={{ empty: !props.vm.description }} data-tip="edit"
383 onClick={() => describe(props.vm, props.refetch)}>389 onClick={() => describe(props.vm, props.refetch)}>
384 {props.vm.description || "add a description"}390 {props.vm.description || "add a description"}
385 </button>391 </button>
392 </Show>
386 </dd>393 </dd>
394 <Show when={props.vm.capabilities.includes("edit")}>
387 <dt>autostart</dt>395 <dt>autostart</dt>
388 <dd>396 <dd>
389 <Checkbox checked={props.vm.autostart} disabled={saving()} onChange={setAutostart}>start with the host</Checkbox>397 <Checkbox checked={props.vm.autostart} disabled={saving()} onChange={setAutostart}>start with the host</Checkbox>
390 </dd>398 </dd>
399 </Show>
391 <dt>cpu</dt>400 <dt>cpu</dt>
392 <dd>{props.vm.pinned ? `pinned to threads ${threads(props.vm.pinned)}` : "floats over all threads"}</dd>401 <dd>{props.vm.pinned ? `pinned to threads ${threads(props.vm.pinned)}` : "floats over all threads"}</dd>
393 </dl>402 </dl>
...@@ -462,17 +471,17 @@ function PowerControls(props: { vm: Domain; refetch: () => unknown }) {...@@ -462,17 +471,17 @@ function PowerControls(props: { vm: Domain; refetch: () => unknown }) {
462 }471 }
463 };472 };
464 return <>473 return <>
465 <Show when={isOff(props.vm)}>474 <Show when={isOff(props.vm) && props.vm.capabilities.includes("start")}>
466 <button class="button small" disabled={pending()} aria-busy={pending()} onClick={() => run("start")}><Play size={14} aria-hidden="true" />start</button>475 <button class="button small" disabled={pending() || !!props.vm.pendingAction} aria-busy={pending()} onClick={() => run("start")}><Play size={14} aria-hidden="true" />start</button>
467 </Show>476 </Show>
468 <Show when={props.vm.state === "paused" || props.vm.state === "pmsuspended"}>477 <Show when={(props.vm.state === "paused" || props.vm.state === "pmsuspended") && props.vm.capabilities.includes("resume")}>
469 <button class="button small" disabled={pending()} aria-busy={pending()} onClick={() => run("resume")}><Play size={14} aria-hidden="true" />resume</button>478 <button class="button small" disabled={pending() || !!props.vm.pendingAction} aria-busy={pending()} onClick={() => run("resume")}><Play size={14} aria-hidden="true" />resume</button>
470 </Show>479 </Show>
471 <Show when={props.vm.state === "shutdown"}>480 <Show when={props.vm.state === "shutdown" && props.vm.capabilities.includes("destroy")}>
472 <button class="button small danger" onClick={() => forceOff(props.vm, props.refetch)}><Square size={14} aria-hidden="true" />force off</button>481 <button class="button small danger" onClick={() => forceOff(props.vm, props.refetch)}><Square size={14} aria-hidden="true" />force off</button>
473 </Show>482 </Show>
474 <Show when={running(props.vm)}>483 <Show when={running(props.vm) && props.vm.capabilities.includes("shutdown")}>
475 <button class="button small" onClick={() => stop(props.vm, props.refetch)}><Power size={14} aria-hidden="true" />stop</button>484 <button class="button small" disabled={!!props.vm.pendingAction} onClick={() => stop(props.vm, props.refetch)}><Power size={14} aria-hidden="true" />stop</button>
476 </Show>485 </Show>
477 </>;486 </>;
478}487}
...@@ -486,13 +495,14 @@ function Row(props: {...@@ -486,13 +495,14 @@ function Row(props: {
486 const state = () => status(props.vm);495 const state = () => status(props.vm);
487 const pinned = () => props.vm.pinned && `pinned to threads ${threads(props.vm.pinned)}`;496 const pinned = () => props.vm.pinned && `pinned to threads ${threads(props.vm.pinned)}`;
488 const cpuTip = () => {497 const cpuTip = () => {
489 if (!props.vm.usage) return pinned() || undefined;498 if (!props.vm.usage || props.vm.vcpus === null) return pinned() || undefined;
490 const busy = (props.vm.usage.cpu / 100) * props.vm.vcpus;499 const busy = (props.vm.usage.cpu / 100) * props.vm.vcpus;
491 return [`${busy.toFixed(1)} of ${props.vm.vcpus} vcpus busy, ${percent((busy / props.hostCpus) * 100)} of the host`, pinned()]500 return [`${busy.toFixed(1)} of ${props.vm.vcpus} vcpus busy, ${percent((busy / props.hostCpus) * 100)} of the host`, pinned()]
492 .filter(Boolean).join(", ");501 .filter(Boolean).join(", ");
493 };502 };
494 const memoryTip = () => {503 const memoryTip = () => {
495 const used = props.vm.usage?.memory;504 const used = props.vm.usage?.memory;
505 if (props.vm.balloon === null || props.vm.memory === null) return undefined;
496 const grows = props.vm.balloon < props.vm.memory && `can grow to ${bytes(props.vm.memory)}`;506 const grows = props.vm.balloon < props.vm.memory && `can grow to ${bytes(props.vm.memory)}`;
497 if (used === undefined) return grows ? `boots with ${bytes(props.vm.balloon)}, ${grows}` : undefined;507 if (used === undefined) return grows ? `boots with ${bytes(props.vm.balloon)}, ${grows}` : undefined;
498 return [`${bytes(used)} / ${bytes(props.vm.balloon)} (${percent((used / props.vm.balloon) * 100)})`, grows]508 return [`${bytes(used)} / ${bytes(props.vm.balloon)} (${percent((used / props.vm.balloon) * 100)})`, grows]
...@@ -514,7 +524,7 @@ function Row(props: {...@@ -514,7 +524,7 @@ function Row(props: {
514 <td class="num" data-tip={memoryTip()}>{props.vm.usage && bytes(props.vm.usage.memory)}</td>524 <td class="num" data-tip={memoryTip()}>{props.vm.usage && bytes(props.vm.usage.memory)}</td>
515 <td class="actions" rowspan="2" onClick={(event) => event.stopPropagation()}>525 <td class="actions" rowspan="2" onClick={(event) => event.stopPropagation()}>
516 <div>526 <div>
517 <Show when={!isOff(props.vm) && props.vm.console}><button onClick={props.onOpen}>screen</button></Show>527 <Show when={running(props.vm) && props.vm.console}><button onClick={props.onOpen}>screen</button></Show>
518 <Show when={running(props.vm) && props.vm.interfaces.flatMap((nic) => nic.addresses)[0]}>528 <Show when={running(props.vm) && props.vm.interfaces.flatMap((nic) => nic.addresses)[0]}>
519 {(value) => {529 {(value) => {
520 const target = () => remote(props.vm.os, value());530 const target = () => remote(props.vm.os, value());
...@@ -529,6 +539,7 @@ function Row(props: {...@@ -529,6 +539,7 @@ function Row(props: {
529 <td class="specs" colspan="2">539 <td class="specs" colspan="2">
530 <div onClick={(event) => event.stopPropagation()}>540 <div onClick={(event) => event.stopPropagation()}>
531 <span>{props.vm.os}</span>541 <span>{props.vm.os}</span>
542 <span>{props.vm.host}</span>
532 <For each={props.vm.interfaces}>543 <For each={props.vm.interfaces}>
533 {(nic) => (544 {(nic) => (
534 <span>545 <span>
...@@ -561,7 +572,7 @@ function Row(props: {...@@ -561,7 +572,7 @@ function Row(props: {
561 <Show when={props.vm.autostart}><span><Power size={12} aria-hidden="true" />autostart</span></Show>572 <Show when={props.vm.autostart}><span><Power size={12} aria-hidden="true" />autostart</span></Show>
562 </div>573 </div>
563 </td>574 </td>
564 <td class="num" tabindex={cpuTip() ? 0 : undefined} data-tip={cpuTip()}>{plural(props.vm.vcpus, "vcpu")}</td>575 <td class="num" tabindex={cpuTip() ? 0 : undefined} data-tip={cpuTip()}>{props.vm.vcpus === null ? "—" : plural(props.vm.vcpus, "vcpu")}</td>
565 <td class="num" tabindex={memoryTip() ? 0 : undefined} data-tip={memoryTip()}>{allocated(props.vm)}</td>576 <td class="num" tabindex={memoryTip() ? 0 : undefined} data-tip={memoryTip()}>{allocated(props.vm)}</td>
566 </tr>577 </tr>
567578
...@@ -628,7 +639,9 @@ export function VMs() {...@@ -628,7 +639,9 @@ export function VMs() {
628 <div class="empty vms-empty"><p>This VM isn't in your account.</p><button class="button" onClick={() => setParams({ vm: undefined, tab: undefined })}>VM home</button></div>639 <div class="empty vms-empty"><p>This VM isn't in your account.</p><button class="button" onClick={() => setParams({ vm: undefined, tab: undefined })}>VM home</button></div>
629 }>{(vm) => <section class="vm-workspace" aria-label={`${name} workspace`}>640 }>{(vm) => <section class="vm-workspace" aria-label={`${name} workspace`}>
630 <h1 class="sr-only">{name}</h1>641 <h1 class="sr-only">{name}</h1>
631 <VMConsole name={name} enabled={!isOff(vm())} active={activeTab() === "screen"} setup={vm().setup}642 <VMConsole name={name} platform={vm().platform} enabled={running(vm()) || vm().state === "paused"} active={activeTab() === "screen"} setup={vm().setup}
643 unavailable={vm().state === "unreachable" ? `${vm().host} is unreachable. Check its power and network.` : vm().state === "pmsuspended" ? "The VM is suspended. Resume it from the Machine menu." : undefined}
644 clipboard={vm().capabilities.includes("clipboard") ? async (text) => { await parseResponse(api.vms[":name"].clipboard.$post({ param: { name }, json: { text } })); } : undefined}
632 content={(toolbar) => <>645 content={(toolbar) => <>
633 <Show when={consoleOpened()}><VMSerial name={name} enabled={running(vm()) && vm().platform === "linux" && vm().serial} active={activeTab() === "console"} toolbar={toolbar} /></Show>646 <Show when={consoleOpened()}><VMSerial name={name} enabled={running(vm()) && vm().platform === "linux" && vm().serial} active={activeTab() === "console"} toolbar={toolbar} /></Show>
634 <Show when={activeTab() === "hardware"}><Hardware vm={vm()} refetch={refetch} username={access()?.username} hostname={access()?.hostname} /></Show>647 <Show when={activeTab() === "hardware"}><Hardware vm={vm()} refetch={refetch} username={access()?.username} hostname={access()?.hostname} /></Show>
...@@ -641,19 +654,20 @@ export function VMs() {...@@ -641,19 +654,20 @@ export function VMs() {
641 </TabBar>654 </TabBar>
642 <VMMenu label="Machine">655 <VMMenu label="Machine">
643 <PowerControls vm={vm()} refetch={refetch} />656 <PowerControls vm={vm()} refetch={refetch} />
644 <button disabled={!running(vm())} onClick={() => restart(vm(), refetch)}><RotateCw size={14} aria-hidden="true" />Restart</button>657 <Show when={vm().capabilities.includes("reboot")}><button disabled={!running(vm()) || !!vm().pendingAction} onClick={() => restart(vm(), refetch)}><RotateCw size={14} aria-hidden="true" />Restart</button></Show>
645 <Show when={vm().state !== "shutdown"}><button disabled={isOff(vm())} onClick={() => forceOff(vm(), refetch)}><Square size={14} aria-hidden="true" />Force off</button></Show>658 <Show when={vm().capabilities.includes("suspend")}><button disabled={!running(vm()) || !!vm().pendingAction} onClick={() => showConfirmDialog({ title: `Suspend ${name}?`, description: "The VM pauses until you resume it.", confirmLabel: "suspend", onConfirm: () => act(vm(), "suspend", refetch) })}><Pause size={14} aria-hidden="true" />Suspend</button></Show>
646 <Show when={latest().canPublish && vm().platform !== "macos"}><button disabled={vm().state !== "shutoff"} title={vm().state !== "shutoff" ? "Shut down before saving a preset" : undefined} onClick={() => savePreset(vm(), refetch)}><Save size={14} aria-hidden="true" />Save preset</button></Show>659 <Show when={vm().capabilities.includes("destroy") && vm().state !== "shutdown"}><button disabled={isOff(vm())} onClick={() => forceOff(vm(), refetch)}><Square size={14} aria-hidden="true" />Force off</button></Show>
647 <button class="danger" onClick={() => remove(vm(), refetch)}><Trash size={14} aria-hidden="true" />Delete</button>660 <Show when={latest().canPublish && vm().capabilities.includes("preset") && vm().platform !== "macos"}><button disabled={vm().state !== "shutoff"} title={vm().state !== "shutoff" ? "Shut down before saving a preset" : undefined} onClick={() => savePreset(vm(), refetch)}><Save size={14} aria-hidden="true" />Save preset</button></Show>
661 <Show when={vm().capabilities.includes("delete")}><button class="danger" onClick={() => remove(vm(), refetch)}><Trash size={14} aria-hidden="true" />Delete</button></Show>
648 </VMMenu>662 </VMMenu>
649 <VMMenu label="Media">663 <Show when={vm().capabilities.includes("media")}><VMMenu label="Media">
650 <button onClick={() => attachIso(vm(), () => latest().library, refetch)}><Disc3 size={14} aria-hidden="true" />Attach ISO</button>664 <button onClick={() => attachIso(vm(), () => latest().library, refetch)}><Disc3 size={14} aria-hidden="true" />Attach ISO</button>
651 <button disabled={!vm().media.some((item) => item.source)} onClick={async () => {665 <button disabled={!vm().media.some((item) => item.source)} onClick={async () => {
652 try { await parseResponse(api.vms[":name"].media.$put({ param: { name }, json: { image: "" } })); await refetch(); }666 try { await parseResponse(api.vms[":name"].media.$put({ param: { name }, json: { image: "" } })); await refetch(); }
653 catch (failure) { toast(`Unable to eject ISO. ${reason(failure)}`); }667 catch (failure) { toast(`Unable to eject ISO. ${reason(failure)}`); }
654 }}><Disc2 size={14} aria-hidden="true" />Eject ISO</button>668 }}><Disc2 size={14} aria-hidden="true" />Eject ISO</button>
655 <span class="muted vm-menu-info">{vm().media.find((item) => item.source)?.source ?? "No ISO attached"}</span>669 <span class="muted vm-menu-info">{vm().media.find((item) => item.source)?.source ?? "No ISO attached"}</span>
656 </VMMenu>670 </VMMenu></Show>
657 <Show when={access()}>{(login) => <VMMenu label="Password">671 <Show when={access()}>{(login) => <VMMenu label="Password">
658 <label class="vm-password">Guest password<input aria-label="Guest password" value={login().password} readOnly spellcheck={false} /></label>672 <label class="vm-password">Guest password<input aria-label="Guest password" value={login().password} readOnly spellcheck={false} /></label>
659 <Copy value={login().password} label="guest password"><KeyRound size={14} aria-hidden="true" />Copy password</Copy>673 <Copy value={login().password} label="guest password"><KeyRound size={14} aria-hidden="true" />Copy password</Copy>
dashboard/web/types/vms.ts+10-6
...@@ -1,5 +1,5 @@...@@ -1,5 +1,5 @@
1/** libvirt's virDomainState, minus "nostate". "shutdown" means a shutdown is under way. */1/** "shutdown" means a shutdown is under way; "unreachable" means the host did not answer. */
2export type DomainState = "running" | "blocked" | "paused" | "shutdown" | "shutoff" | "crashed" | "pmsuspended";2export type DomainState = "running" | "blocked" | "paused" | "shutdown" | "shutoff" | "crashed" | "pmsuspended" | "unreachable";
33
4export interface Disk {4export interface Disk {
5 /** Guest device, like "vda". */5 /** Guest device, like "vda". */
...@@ -34,19 +34,23 @@ export const VM_SETUP = {...@@ -34,19 +34,23 @@ export const VM_SETUP = {
34export interface Domain {34export interface Domain {
35 name: string;35 name: string;
36 owner: string | null;36 owner: string | null;
37 host: string;
38 arch: string;
39 capabilities: string[];
40 pendingAction: string | null;
37 /** What it's for, from the domain's `<description>`; empty when unset. */41 /** What it's for, from the domain's `<description>`; empty when unset. */
38 description: string;42 description: string;
39 state: DomainState;43 state: DomainState;
40 /** libvirt's reason for a paused or crashed state, like "user", "ioerror" or "panicked"; null otherwise. */44 /** libvirt's reason for a paused or crashed state, like "user", "ioerror" or "panicked"; null otherwise. */
41 reason: string | null;45 reason: string | null;
42 os: string;46 os: string;
43 vcpus: number;47 vcpus: number | null;
44 /** Host threads the vCPUs are pinned to; null when they float over all of them. */48 /** Host threads the vCPUs are pinned to; null when they float over all of them. */
45 pinned: number[] | null;49 pinned: number[] | null;
46 /** The most memory the guest can have. */50 /** The most memory the guest can have. */
47 memory: number;51 memory: number | null;
48 /** What the balloon currently gives the guest, at most `memory`. */52 /** What the balloon currently gives the guest, at most `memory`. */
49 balloon: number;53 balloon: number | null;
50 autostart: boolean;54 autostart: boolean;
51 /** Unix seconds the QEMU process started; null while shut off. */55 /** Unix seconds the QEMU process started; null while shut off. */
52 startedAt: number | null;56 startedAt: number | null;
...@@ -80,7 +84,7 @@ export interface History {...@@ -80,7 +84,7 @@ export interface History {
80 domains: Record<string, { cpu: (number | null)[]; memory: (number | null)[] }>;84 domains: Record<string, { cpu: (number | null)[]; memory: (number | null)[] }>;
81}85}
8286
83export const DOMAIN_ACTIONS = ["start", "shutdown", "reboot", "destroy", "resume"] as const;87export const DOMAIN_ACTIONS = ["start", "shutdown", "reboot", "destroy", "resume", "suspend"] as const;
84export interface Preset {88export interface Preset {
85 id: string;89 id: string;
86 os: string;90 os: string;
nixos/configuration.nix+1-1
...@@ -6,7 +6,7 @@ let...@@ -6,7 +6,7 @@ let
6 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";6 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";
7 hostTools = lib.fileset.toSource {7 hostTools = lib.fileset.toSource {
8 root = ../.;8 root = ../.;
9 fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../config/shale.json ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../tools/namespace-transition.py ../dashboard/src/namespace-transition.json ../guest/protocol.json ];9 fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../config/shale.json ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../tools/namespace-transition.py ../dashboard/src/namespace-transition.json ../guest/protocol.json ];
10 };10 };
11 nativePkl = pkgs.callPackage ./pkl.nix { };11 nativePkl = pkgs.callPackage ./pkl.nix { };
12 screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);12 screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);
nixos/mac-studio-sandbox.nix+1-1
...@@ -50,7 +50,7 @@ in...@@ -50,7 +50,7 @@ in
50 enable = true;50 enable = true;
51 hostName = "10.0.0.1";51 hostName = "10.0.0.1";
52 exports = ''52 exports = ''
53 ${mountPoint} 10.0.0.222(rw,sync,fsid=0,no_subtree_check,all_squash,anonuid=${toString config.users.users.clo.uid},anongid=${toString config.users.groups.chloe.gid},insecure)53 ${mountPoint} ${workspace.clientAddress}(rw,sync,fsid=0,no_subtree_check,all_squash,anonuid=${toString config.users.users.clo.uid},anongid=${toString config.users.groups.chloe.gid},insecure)
54 '';54 '';
55 };55 };
56 services.nfs.settings.nfsd = {56 services.nfs.settings.nfsd = {
nixos/mac-vm.nix created+17
...@@ -0,0 +1,17 @@
1{ ... }:
2let
3 workspace = builtins.fromJSON (builtins.readFile ../config/mac-studio-sandbox.json);
4in {
5 systemd.services.snowglobe-host = {
6 environment.SNOWGLOBE_MAC_VM = builtins.toJSON {
7 name = "mac-studio-gui-agent";
8 owner = "8c909fab-98b0-4472-9171-9606ccebe9d5";
9 address = workspace.clientAddress;
10 port = 22;
11 user = "clo";
12 identity = "/var/lib/snowglobe/mac-vm/ssh_key";
13 knownHosts = "/var/lib/snowglobe/mac-vm/known_hosts";
14 };
15 serviceConfig.IPAddressAllow = [ "${workspace.clientAddress}/32" ];
16 };
17}
nixos/snowglobe.nix+2-2
...@@ -16,7 +16,7 @@ let...@@ -16,7 +16,7 @@ let
16 };16 };
17in17in
18{18{
19 imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./zfs-read-cache.nix ];19 imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./mac-vm.nix ./zfs-read-cache.nix ];
2020
21 networking.hostName = "snowglobe";21 networking.hostName = "snowglobe";
22 environment.etc."machine-info".text = ''22 environment.etc."machine-info".text = ''
...@@ -30,7 +30,7 @@ in...@@ -30,7 +30,7 @@ in
30 networking.firewall.extraCommands = lib.concatMapStrings ({ address, prefixLength, ... }: ''30 networking.firewall.extraCommands = lib.concatMapStrings ({ address, prefixLength, ... }: ''
31 iptables -A nixos-fw -i enp4s0 -s ${address}/${toString prefixLength} -p tcp --dport 445 -j nixos-fw-accept31 iptables -A nixos-fw -i enp4s0 -s ${address}/${toString prefixLength} -p tcp --dport 445 -j nixos-fw-accept
32 '') config.networking.interfaces.enp4s0.ipv4.addresses + ''32 '') config.networking.interfaces.enp4s0.ipv4.addresses + ''
33 iptables -A nixos-fw -i enp4s0 -s 10.0.0.222/32 -p tcp --dport 2049 -j nixos-fw-accept33 iptables -A nixos-fw -i enp4s0 -s ${(builtins.fromJSON (builtins.readFile ../config/mac-studio-sandbox.json)).clientAddress}/32 -p tcp --dport 2049 -j nixos-fw-accept
34 ip6tables -A nixos-fw -i enp4s0 -s fe80::/10 -p tcp --dport 445 -j nixos-fw-accept34 ip6tables -A nixos-fw -i enp4s0 -s fe80::/10 -p tcp --dport 445 -j nixos-fw-accept
35 ip6tables -A nixos-fw -i enp4s0 -s fc00::/7 -p tcp --dport 445 -j nixos-fw-accept35 ip6tables -A nixos-fw -i enp4s0 -s fc00::/7 -p tcp --dport 445 -j nixos-fw-accept
36 '';36 '';
tools/code-vm created+22
...@@ -0,0 +1,22 @@
1#!/usr/bin/env python3
2import os
3from pathlib import Path
4import subprocess
5import sys
6
7
8helper = str(Path(__file__).resolve().with_name("mac-agent-vm.py"))
9if sys.argv[1:] == ["--clipboard"]:
10 with subprocess.Popen(["/usr/bin/pbpaste"], stdout=subprocess.PIPE) as clipboard:
11 try:
12 result = subprocess.run(
13 [sys.executable, helper, "exec", "/usr/bin/pbcopy"],
14 stdin=clipboard.stdout,
15 )
16 finally:
17 clipboard.stdout.close()
18 if clipboard.wait() or result.returncode:
19 sys.exit(1)
20 print("VM clipboard updated. Press Command-V in the VM.")
21else:
22 os.execv(sys.executable, [sys.executable, helper, "ssh", *sys.argv[1:]])
tools/dashboard-host.py+12-2
...@@ -183,6 +183,13 @@ class Host:...@@ -183,6 +183,13 @@ class Host:
183 vms.validate(action, payload)183 vms.validate(action, payload)
184 except ValueError as error:184 except ValueError as error:
185 raise Rejected(str(error)) from error185 raise Rejected(str(error)) from error
186 if action == "clipboard":
187 if not vms.mac_vms.CONFIG or payload["name"] != vms.mac_vms.CONFIG["name"]:
188 raise Rejected("Use this VM's screen clipboard.")
189 try:
190 return vms.mac_vms.handle(action, payload)
191 except ValueError as error:
192 raise Rejected(str(error)) from error
186 args = ["python3", str(Path(__file__).with_name("vms.py")), action]193 args = ["python3", str(Path(__file__).with_name("vms.py")), action]
187 if payload is not None:194 if payload is not None:
188 args.append(json.dumps(payload))195 args.append(json.dumps(payload))
...@@ -194,7 +201,7 @@ class Host:...@@ -194,7 +201,7 @@ class Host:
194 return json.loads(command(*args, timeout=900))201 return json.loads(command(*args, timeout=900))
195 finally:202 finally:
196 VM_PREPARATION_SLOT.release()203 VM_PREPARATION_SLOT.release()
197 return json.loads(command(*args))204 return json.loads(command(*args, timeout=230 if action == "act" else 60))
198 except subprocess.CalledProcessError as error:205 except subprocess.CalledProcessError as error:
199 if error.returncode == 2:206 if error.returncode == 2:
200 raise Rejected(error.stderr) from error207 raise Rejected(error.stderr) from error
...@@ -344,6 +351,9 @@ def stream_console(connection, host, request):...@@ -344,6 +351,9 @@ def stream_console(connection, host, request):
344 screen.settimeout(10)351 screen.settimeout(10)
345 screen.connect(details["path"])352 screen.connect(details["path"])
346 screen.setblocking(False)353 screen.setblocking(False)
354 elif details.get("remote"):
355 _, screen = resources.enter_context(vms.mac_vms.connect({"action": "console"}, timeout=20))
356 screen.setblocking(False)
347 else:357 else:
348 identity = details["uuid"]358 identity = details["uuid"]
349 deadline = time.monotonic() + 5359 deadline = time.monotonic() + 5
...@@ -359,7 +369,7 @@ def stream_console(connection, host, request):...@@ -359,7 +369,7 @@ def stream_console(connection, host, request):
359 if details["uuid"] != identity:369 if details["uuid"] != identity:
360 raise Rejected("The VM changed while its screen was opening. Open it again.")370 raise Rejected("The VM changed while its screen was opening. Open it again.")
361 screen.setblocking(False)371 screen.setblocking(False)
362 send_response(connection, {"value": None})372 send_response(connection, {"value": {"guestDisplay": False} if details.get("remote") else None})
363 try:373 try:
364 connection.setblocking(False)374 connection.setblocking(False)
365 with selectors.DefaultSelector() as selector:375 with selectors.DefaultSelector() as selector:
tools/mac-agent-gui.swift created+164
...@@ -0,0 +1,164 @@
1import AppKit
2import ApplicationServices
3import ImageIO
4import ScreenCaptureKit
5import UniformTypeIdentifiers
6
7func output(_ value: Any) {
8 let data = try! JSONSerialization.data(withJSONObject: value, options: [.sortedKeys])
9 print(String(data: data, encoding: .utf8)!)
10}
11
12func fail(_ message: String) -> Never {
13 output(["error": message])
14 exit(1)
15}
16
17func attribute(_ element: AXUIElement, _ name: String) -> CFTypeRef? {
18 var value: CFTypeRef?
19 return AXUIElementCopyAttributeValue(element, name as CFString, &value) == .success ? value : nil
20}
21
22func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String: Any] {
23 remaining -= 1
24 var result: [String: Any] = ["path": path]
25 for name in ["AXRole", "AXSubrole", "AXTitle", "AXDescription", "AXValue", "AXEnabled", "AXFocused"] {
26 if let value = attribute(element, name), value is String || value is NSNumber {
27 result[name] = value
28 }
29 }
30 for name in ["AXPosition", "AXSize"] {
31 if let value = attribute(element, name), CFGetTypeID(value) == AXValueGetTypeID() {
32 let axValue = value as! AXValue
33 if AXValueGetType(axValue) == .cgPoint {
34 var point = CGPoint.zero
35 AXValueGetValue(axValue, .cgPoint, &point)
36 result[name] = [point.x, point.y]
37 } else if AXValueGetType(axValue) == .cgSize {
38 var size = CGSize.zero
39 AXValueGetValue(axValue, .cgSize, &size)
40 result[name] = [size.width, size.height]
41 }
42 }
43 }
44 var actions: CFArray?
45 if AXUIElementCopyActionNames(element, &actions) == .success { result["actions"] = actions }
46 if path.count < 16, let children = attribute(element, "AXChildren") as? [AXUIElement] {
47 var nodes: [[String: Any]] = []
48 for (index, child) in children.enumerated() where remaining > 0 {
49 nodes.append(tree(child, path: path + [index], remaining: &remaining))
50 }
51 result["children"] = nodes
52 if nodes.count < children.count { result["truncated"] = true }
53 }
54 return result
55}
56
57let arguments = Array(CommandLine.arguments.dropFirst())
58guard let command = arguments.first else { fail("Use status, request-permissions, screenshot, tree, press, click, key, or type.") }
59guard let eventSource = CGEventSource(stateID: .privateState) else { fail("Could not create a private input event source.") }
60switch command {
61case "status", "request-permissions":
62 if command == "request-permissions" {
63 _ = AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary)
64 _ = CGRequestScreenCaptureAccess()
65 _ = CGRequestPostEventAccess()
66 }
67 output(["accessibility": AXIsProcessTrusted(), "screenCapture": CGPreflightScreenCaptureAccess(), "postEvent": CGPreflightPostEventAccess()])
68case "screenshot":
69 guard arguments.count == 2 else { fail("Provide an output PNG path.") }
70 let image: CGImage
71 do {
72 let content = try await SCShareableContent.excludingDesktopWindows(false, onScreenWindowsOnly: true)
73 guard let display = content.displays.first(where: { $0.displayID == CGMainDisplayID() }) else { fail("No active display.") }
74 let configuration = SCStreamConfiguration()
75 configuration.width = display.width
76 configuration.height = display.height
77 configuration.showsCursor = false
78 image = try await SCScreenshotManager.captureImage(contentFilter: SCContentFilter(display: display, excludingWindows: []), configuration: configuration)
79 } catch { fail("Screen capture failed: \(error.localizedDescription)") }
80 guard let destination = CGImageDestinationCreateWithURL(URL(fileURLWithPath: arguments[1]) as CFURL, UTType.png.identifier as CFString, 1, nil)
81 else { fail("Could not create the output PNG.") }
82 CGImageDestinationAddImage(destination, image, nil)
83 guard CGImageDestinationFinalize(destination) else { fail("Could not write the output PNG.") }
84 output(["width": image.width, "height": image.height, "path": arguments[1]])
85case "tree", "press":
86 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }
87 let app: AXUIElement
88 if arguments.count > 1 {
89 guard let pid = Int32(arguments[1]), pid > 0 else { fail("Provide a positive application PID.") }
90 app = AXUIElementCreateApplication(pid)
91 } else {
92 guard let front = NSWorkspace.shared.frontmostApplication else { fail("No active application.") }
93 app = AXUIElementCreateApplication(front.processIdentifier)
94 }
95 if command == "tree" {
96 var remaining = 5000
97 output(tree(app, path: [], remaining: &remaining))
98 } else {
99 guard arguments.count == 3 else { fail("Provide a PID and a dot-separated AX child path.") }
100 var element = app
101 for item in arguments[2].split(separator: ".") {
102 guard let index = Int(item), let children = attribute(element, "AXChildren") as? [AXUIElement], children.indices.contains(index)
103 else { fail("The AX path no longer exists; read the tree again.") }
104 element = children[index]
105 }
106 let result = AXUIElementPerformAction(element, kAXPressAction as CFString)
107 guard result == .success else { fail("AXPress failed: \(result.rawValue)") }
108 output(["pressed": true])
109 }
110case "click":
111 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }
112 guard arguments.count == 3, let x = Double(arguments[1]), let y = Double(arguments[2]) else { fail("Provide x and y coordinates.") }
113 let point = CGPoint(x: x, y: y)
114 for type in [CGEventType.mouseMoved, .leftMouseDown, .leftMouseUp] {
115 let event = CGEvent(mouseEventSource: eventSource, mouseType: type, mouseCursorPosition: point, mouseButton: .left)
116 event?.flags = []
117 event?.post(tap: .cghidEventTap)
118 usleep(10000)
119 }
120 output(["clicked": [x, y]])
121case "key":
122 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }
123 guard arguments.count >= 2, let code = UInt16(arguments[1]) else { fail("Provide a macOS virtual key code, optionally followed by command, shift, option, or control.") }
124 var flags: CGEventFlags = []
125 for modifier in arguments.dropFirst(2) {
126 switch modifier {
127 case "command": flags.insert(.maskCommand)
128 case "shift": flags.insert(.maskShift)
129 case "option": flags.insert(.maskAlternate)
130 case "control": flags.insert(.maskControl)
131 default: fail("Unknown key modifier.")
132 }
133 }
134 for down in [true, false] {
135 let event = CGEvent(keyboardEventSource: eventSource, virtualKey: code, keyDown: down)
136 event?.flags = down ? flags : []
137 event?.post(tap: .cghidEventTap)
138 usleep(10000)
139 }
140 output(["key": code])
141case "type":
142 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }
143 guard arguments.count == 2 else { fail("Provide text, or a dash to read UTF-8 from stdin.") }
144 let text = arguments[1] == "-" ? String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)! : arguments[1]
145 let units = Array(text.utf16)
146 var start = 0
147 while start < units.count {
148 var end = min(start + 20, units.count)
149 if end < units.count && (0xD800...0xDBFF).contains(units[end - 1]) { end -= 1 }
150 let chunk = Array(units[start..<end])
151 for down in [true, false] {
152 let event = CGEvent(keyboardEventSource: eventSource, virtualKey: 0, keyDown: down)
153 event?.flags = []
154 event?.keyboardSetUnicodeString(stringLength: chunk.count, unicodeString: chunk)
155 event?.post(tap: .cghidEventTap)
156 usleep(10000)
157 }
158 start = end
159 }
160 usleep(50000)
161 output(["typed": true])
162default:
163 fail("Unknown command.")
164}
tools/mac-agent-vm.md created+215
...@@ -0,0 +1,215 @@
1# Local macOS agent VM
2
3`mac-agent-vm.py` controls the `gui-agent` Tart VM on this Mac. Its runtime,
4image cache, private credentials, and VM disks live under
5`/Volumes/Documents/VM/macOS Code Sandbox` on the local SSD. `MAC_AGENT_VM_HOME` overrides that
6root. Missing storage fails explicitly; the tool does not recreate it on the
7startup disk.
8
9Documents' external volume has ownership disabled. The VM root uses
10an explicit named-Clover allow ACL before an everyone-deny ACL, with file and
11directory inheritance; mode bits alone would not protect it. This order and
12identity matching were tested on disposable files, including denied access
13despite permissive POSIX modes. The signed Tart app is protected by its
14ancestor directory ACL because macOS App Management rejects modifying the
15bundle's own ACL. Volume ownership and host security settings are unchanged.
16
17```sh
18python3 tools/mac-agent-vm.py start
19python3 tools/mac-agent-vm.py status
20python3 tools/mac-agent-vm.py exec /bin/zsh -lc 'rustc --version; node --version'
21python3 tools/mac-agent-vm.py ssh
22python3 tools/mac-agent-vm.py console
23python3 tools/mac-agent-vm.py stop
24```
25
26`start` resumes a suspended VM or boots a stopped VM, then waits for commands
27to work through the logged-in guest agent. `stop` requests a graceful
28guest shutdown and waits for it to finish. `suspend` waits for Tart's RAM
29snapshot; it writes memory to disk and is not an instant CPU pause. A running
30VM holds a process-scoped idle-sleep assertion, so locking the host does not
31cause idle sleep. Host login, FileVault, and explicit sleep remain host policy.
32
33The guest has 28 vCPUs, 48 GiB RAM, and a 1440×900 display. vCPUs do not reserve
34physical cores. CPU count and RAM changes require a cold boot. No automatic
35lock/unlock controller or CPU affinity is installed. The guest's 48 GiB budget
36is fixed for a run. Apple [allocates physical memory on demand](https://developer.apple.com/documentation/virtualization/vzvirtualmachineconfiguration/memorysize),
37so it need not occupy 48 GiB immediately. Stock Tart has no verified balloon or
38guest-aware reclaim path; sizing assumes previously used pages may remain
39backed until the VM stops. This follows Clover's 48 GiB fallback.
40
41The 128 GB sparse disk contains a system APFS container of about 122 GB,
42a 5.4 GB recovery partition, and the original boot container. The system
43volume no longer has a separate 44 GB ceiling. Cache placement is managed by
44the guest's setup configuration under `~/.local/share/agents/vm`; projects and
45bulk caches use `/Volumes/Code`. SIP and the recovery partition remain intact.
46
47`AgentScratch` was removed on 2026-10-20 after its users stopped and legacy
48cache references were updated. A normal unmount and deletion targeted only
49volume UUID `D7650D6C-F7CC-444B-ACA3-EA472ADEB3B8`; the Code workspace and its
50replacement cache image were verified mounted afterward. Recovery archives
51are retained in the host VM home's `private/agentscratch-retirement-20261020` and the guest's
52`/Volumes/Code/scratch/setup/AgentScratch-retirement.tar`. The host restore
53matched 16,121 entries by content, mode, and symlink target, and all five
54restored SQLite databases passed integrity checks.
55
56The 2026-10-20 storage rebuild preserved the configured guest, accounts, tools,
57and automation approvals. It copied the system bytes into a sparse disk on
58Documents, moved and hash-verified the unchanged recovery partition, expanded
59the system container, and restored Scratch from the stopped original disk
60through a host read-only mount with explicit ownership. Full retained-content
61and metadata comparison, a detached-source cold boot, pinned SSH, actual screen
62capture, and browser keyboard input passed before removing the old Project
63installation. Documents is the only VM home; private rebuild evidence remains
64in `private/rebuild-20261020`. The temporary public Chromium-trial Xcode copy
65was omitted during the move; its pinned host source was verified before the
66old installation was removed.
67
68## Dashboard access
69
70The existing `gui-agent` is registered as `mac-studio-gui-agent` in Snow Globe's
71VM dashboard. Its owner is the Snow account declared in `nixos/mac-vm.nix`.
72The dashboard retains its row when Mac Studio is unreachable; hardware in that
73state is the last successful observation, not live telemetry. Mac resources do
74not count against Snow Globe's Linux VM capacity.
75
76Snow Globe connects to the Mac's existing SSH service with a dedicated key at
77`/var/lib/snowglobe/mac-vm/ssh_key` and a pinned host key in the adjacent
78`known_hosts`. Both are root-only. The corresponding Mac `authorized_keys`
79entry uses `restrict,command=` to invoke `mac-vm-bridge.py`; it grants no shell,
80forwarding, PTY, arbitrary VM selection, or arbitrary guest command. Deploy the
81bridge and `mac-agent-vm.py` together under the VM home's `runtime/control` and
82keep that forced command pointed there, independent of an agent session.
83
84The existing host sshd provides supervision. Each console connection creates
85a fresh, pinned guest SSH tunnel, authenticates Screen Sharing on the Mac,
86and relays RFB into the dashboard's existing WebRTC viewer. No public VNC or
87HTTP control port is required. Dashboard ownership checks cover the screen,
88power operations, and clipboard. Unicode clipboard text travels over stdin
89to the logged-in guest's `pbcopy`, never through command arguments or logs.
90
91Supported controls are start/resume, graceful shutdown/restart, and suspend.
92Power operations are serialized; a timeout kills unfinished lifecycle commands
93while preserving the detached VM runner. Hardware edits, media attachment,
94force-off, deletion, and preset creation are unavailable for this managed VM.
95
96## Guest access
97
98`~/.local/bin/code-vm` links to `tools/code-vm`:
99
100```sh
101code-vm # Interactive SSH shell
102code-vm 'uname -a' # Run a guest command
103code-vm --clipboard # Copy the host clipboard into the guest
104```
105
106After copying the clipboard, focus the guest and press Command-V. Text travels
107through the guest agent into the logged-in session's pasteboard; no automatic
108keystroke or continuous clipboard sharing is enabled. SSH resolves the current
109VM address each time and reuses the existing pinned key configuration.
110
111The guest's `/etc/hosts` maps `file.paperclover.net` to the NAS LAN address
112`10.0.0.1`. Traffic uses the VM's NAT gateway `192.168.64.1`; the host's hosts
113file is not inherited. The previous guest file is `/etc/hosts.before-code-nas`.
114
115SSH uses a new private key in `private/ssh_key`, unique guest host keys pinned
116in `private/known_hosts`, and disables password authentication. No host SSH
117configuration, agent, accounts, or project directories are forwarded. The
118guest's randomized login password and auto-login credential are private to
119this VM. `admin` has passwordless sudo.
120
121The Tart guest agent runs in the logged-in guest session and supplies
122`exec` over Virtualization.framework's VM channel. Prefer that path for GUI
123commands: its Accessibility, Screen Recording, and Full Disk Access permissions
124are approved in the guest's System Settings. SIP remains enabled. Permission
125grants belong to the responsible guest agent; commands launched through another
126process may need separate guest approval.
127
128The first ScreenCaptureKit capture also requested permission to bypass the
129private-window picker; that guest prompt was approved separately. Future OS
130changes or capture-authorization renewals can prompt again: a successful
131preflight status is not proof that every future operation is prompt-free.
132Verify actual capture and input after upgrades.
133
134```sh
135python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui status
136python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui screenshot /tmp/screen.png
137python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui tree
138python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui click 400 300
139python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui key 1 command
140python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui type 'Synthetic test text'
141```
142
143`type -` reads UTF-8 from stdin, including newlines. `tree [PID]` returns
144roles, names, values, states, bounds, available actions, and child paths;
145`press PID 0.1.2` invokes AXPress at a freshly inspected path. Its tree has
146depth and node limits. Rebuild `mac-agent-gui.swift` with `swiftc -O`, install
147the binary at `/usr/local/bin/agent-gui` inside the guest, and recheck actual
148permissions and capture after changing its executable.
149
150The optional local recovery console is `http://127.0.0.1:17891/vnc_lite.html?autoconnect=true`.
151It reaches the guest's built-in Screen Sharing through pinned SSH, forwarding
152guest `127.0.0.1:5900` into a temporary private Unix socket. The relay handles
153the guest account authentication from `private/password` server-side. Screen
154Sharing listens on all interfaces inside the private NAT guest; the host relay's
155TCP listeners default to loopback and check the exact HTTP Host and WebSocket
156Origin. Tart's experimental host VNC server is disabled. The viewer trusts
157every client that can reach its listener; Origin checking protects against foreign
158websites, not other clients. For remote access, bind a separate console to this
159Mac's Tailscale IPv4 address:
160
161```sh
162python3 tools/mac-agent-vm.py console --listen "$(tailscale ip -4)"
163```
164
165Use the printed IP-based URL; hostname aliases fail the exact Host check.
166This exposes desktop control to reachable tailnet peers without opening the LAN
167interface. Keep the authenticated RFB relay itself on loopback. Reconnect the
168viewer after a VM restart; each connection resolves the guest address again.
169Use the authenticated dashboard for ordinary remote access.
170Console shutdown terminates its SSH child and removes the private socket.
171If the page says “connection is closed” while SSH still works, restart the
172console relay; the VM does not need restarting. Run `console` in a host Terminal
173to keep its lifetime independent of an agent thread. Its SSH tunnel has a
174separate session so terminal hangups do not leave the HTTP listener stranded.
175Apple ARD authentication attaches the guest user's session; legacy VNC-only
176authentication is disabled. SSH encrypts its transport. Credentials never appear
177in arguments or browser URLs.
178
179## Guest policy and tools
180
181Spotlight indexing, Siri, idle sleep, screen saver, screen lock, automatic OS
182update checks/downloads/installations, crash dialogs, and automatic diagnostic
183submission are disabled in the guest. Gatekeeper assessments are disabled as
184requested. Developer mode is enabled with `DevToolsSecurity`. On 2026-10-20,
185installed applications, including Xcode and Device Hub, received Full Disk
186Access, device control, screen recording, input monitoring, developer-tool
187access, and app-management approvals through System Settings. Existing apps
188may need a normal relaunch to use new grants. These are per-app permissions;
189future apps and request-only categories can still prompt. SIP remains enabled.
190FSEvents and durable disk synchronization remain enabled. Apply OS updates
191deliberately between test runs, then recheck GUI permissions and boot.
192
193Initial provisioning installed Rust/Cargo 1.99.1, Node 24.22.0 LTS, npm 11.19.1,
194pnpm 12.14.0, Jujutsu 0.42.0, and Apple's CLT with Clang 17 and the macOS SDK.
195Subsequent guest setup manages toolchain versions and paths. Login shells load
196their paths from `~/.zprofile` or `~/.profile`; use
197`exec /bin/zsh -lc ...` for development commands.
198
199## Provenance
200
201Provisioned on 2026-10-19 with the signed [Tart 2.41.1 release](https://github.com/openai/tart/releases/tag/2.41.1),
202archive SHA-256 `e680a734febccfce7d4e910c1f7f13fc3b00f19a181c945e2d746de3214c32c2`.
203The Cirrus Sequoia vanilla image is pinned to
204`ghcr.io/cirruslabs/macos-sequoia-vanilla@sha256:57a83b1b52f928f5b11b0963927178fd6c4cac52c315279f227c27cc5ab81587`
205and boots macOS 15.7.7. Its default credentials were rotated after cloning.
206The [published template](https://github.com/cirruslabs/macos-image-templates/blob/master/templates/vanilla-sequoia.pkr.hcl)
207documents its original auto-login, CLT, SSH, sudo, and Gatekeeper setup.
208
209The VM agent is [Tart guest agent 0.15.0](https://github.com/openai/tart-guest-agent/releases/tag/v0.15.0),
210verified against release archive SHA-256
211`eb47f402f18e742a8ea96344115ca776179f9c4c67494bc0c46a10a68db280b7`.
212The local browser relay uses noVNC 1.7.0, websockify 0.13.0, and cryptography
213for Apple ARD authentication, following [noVNC's protocol implementation](https://github.com/novnc/noVNC/blob/v1.7.0/core/rfb.js).
214Their runtime environment stays outside the checkout. No production service
215configuration is changed.
tools/mac-agent-vm.py created+292
...@@ -0,0 +1,292 @@
1#!/usr/bin/env python3
2import argparse
3import contextlib
4import fcntl
5import hashlib
6import json
7import logging
8import multiprocessing
9import os
10from pathlib import Path
11import select
12import secrets
13import signal
14import socket
15import socketserver
16import struct
17import subprocess
18import sys
19import tempfile
20import threading
21import time
22
23
24ROOT = Path(os.environ.get("MAC_AGENT_VM_HOME", "/Volumes/Documents/VM/macOS Code Sandbox"))
25TART = ROOT / "runtime/tart.app/Contents/MacOS/tart"
26NAME = "gui-agent"
27PRIVATE = ROOT / "private"
28
29
30def tart(*args, **kwargs):
31 return subprocess.run([str(TART), *args], env={**os.environ, "TART_HOME": str(ROOT / "state")},
32 check=True, **kwargs)
33
34
35def ssh_args():
36 return ["ssh", "-F", "/dev/null", "-i", str(PRIVATE / "ssh_key"),
37 "-o", "IdentitiesOnly=yes", "-o", "BatchMode=yes", "-o", "ForwardAgent=no",
38 "-o", "StrictHostKeyChecking=yes", "-o", "HostKeyAlias=" + NAME,
39 "-o", f'UserKnownHostsFile="{PRIVATE / "known_hosts"}"',
40 "-o", "GlobalKnownHostsFile=/dev/null", "-o", "ConnectTimeout=10"]
41
42
43def exact(stream, size):
44 data = bytearray()
45 while len(data) < size:
46 chunk = stream.recv(size - len(data))
47 if not chunk:
48 raise ConnectionError("VNC connection closed")
49 data.extend(chunk)
50 return bytes(data)
51
52
53def wait_for_state(state):
54 for _ in range(90):
55 rows = json.loads(tart("list", "--format", "json", capture_output=True, text=True).stdout)
56 if next(row for row in rows if row["Name"] == NAME and row["Source"] == "local")["State"] == state:
57 return
58 time.sleep(1)
59 raise ValueError(f"The guest has not reached {state}; inspect status before another lifecycle action.")
60
61
62@contextlib.contextmanager
63def guest_screen():
64 ip = tart("ip", NAME, capture_output=True, text=True, timeout=5).stdout.strip()
65 with tempfile.TemporaryDirectory(prefix="vnc-", dir=PRIVATE) as directory:
66 path = Path(directory) / "console.sock"
67 tunnel = subprocess.Popen([*ssh_args(), "-N", "-o", "ExitOnForwardFailure=yes",
68 "-o", "ServerAliveInterval=5", "-o", "ServerAliveCountMax=2",
69 "-o", "StreamLocalBindMask=0177", "-L",
70 f"{path}:127.0.0.1:5900", "admin@" + ip],
71 stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
72 stderr=subprocess.DEVNULL)
73 try:
74 for _ in range(100):
75 if tunnel.poll() is not None:
76 raise ValueError("The guest console is unavailable. Wait for the VM to start, then reconnect.")
77 if path.exists():
78 break
79 time.sleep(0.1)
80 else:
81 raise ValueError("The guest console tunnel did not start. Reconnect to try again.")
82 with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as upstream:
83 upstream.settimeout(10)
84 upstream.connect(str(path))
85 authenticate_screen(upstream)
86 yield upstream
87 finally:
88 tunnel.terminate()
89 try:
90 tunnel.wait(timeout=5)
91 except subprocess.TimeoutExpired:
92 tunnel.kill()
93 tunnel.wait()
94
95
96def authenticate_screen(upstream):
97 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
98 password = (PRIVATE / "password").read_bytes()
99 if len(password) > 63:
100 raise ValueError("Apple Screen Sharing supports at most 63 UTF-8 password bytes.")
101 if not exact(upstream, 12).startswith(b"RFB 003."):
102 raise ValueError("Unsupported VNC server version")
103 upstream.sendall(b"RFB 003.008\n")
104 kinds = exact(upstream, exact(upstream, 1)[0])
105 if 30 not in kinds:
106 raise ValueError("The guest does not offer Apple Screen Sharing authentication.")
107 upstream.sendall(b"\x1e")
108 generator, size = struct.unpack(">HH", exact(upstream, 4))
109 if not 16 <= size <= 512:
110 raise ValueError("The guest sent an unsupported Screen Sharing key size.")
111 prime = int.from_bytes(exact(upstream, size), "big")
112 public = int.from_bytes(exact(upstream, size), "big")
113 if not 1 < generator < prime or not 1 < public < prime - 1:
114 raise ValueError("The guest sent invalid Screen Sharing key parameters.")
115 private = secrets.randbelow(prime - 3) + 2
116 client_public = pow(generator, private, prime).to_bytes(size, "big")
117 shared = pow(public, private, prime).to_bytes(size, "big")
118 credentials = bytearray(secrets.token_bytes(128))
119 credentials[:6] = b"admin\0"
120 credentials[64:65 + len(password)] = password + b"\0"
121 # MD5 and AES-ECB are Apple RFB type 30's wire protocol, carried inside pinned SSH.
122 encryptor = Cipher(algorithms.AES(hashlib.md5(shared).digest()), modes.ECB()).encryptor()
123 upstream.sendall(encryptor.update(bytes(credentials)) + encryptor.finalize() + client_public)
124 if exact(upstream, 4) != b"\0\0\0\0":
125 raise ValueError("VNC authentication failed")
126
127
128def relay_screen(upstream, incoming, outgoing):
129 def read(size):
130 data = bytearray()
131 while len(data) < size:
132 chunk = os.read(incoming, size - len(data))
133 if not chunk:
134 raise ConnectionError("The viewer disconnected.")
135 data.extend(chunk)
136 return bytes(data)
137
138 def write(data):
139 while data:
140 data = data[os.write(outgoing, data):]
141
142 write(b"RFB 003.008\n")
143 if not read(12).startswith(b"RFB 003."):
144 raise ValueError("Unsupported viewer protocol.")
145 write(b"\x01\x01")
146 if read(1) != b"\x01":
147 return
148 write(b"\0\0\0\0")
149 upstream.settimeout(None)
150 while True:
151 ready, _, _ = select.select([upstream, incoming], [], [], 300)
152 for source in ready:
153 data = os.read(source if isinstance(source, int) else source.fileno(), 65536)
154 if not data:
155 return
156 if source is upstream:
157 write(data)
158 else:
159 upstream.sendall(data)
160
161
162def console(host, port):
163 from websockify.auth_plugins import AuthenticationError
164 from websockify.websocketproxy import ProxyRequestHandler, WebSocketProxy
165
166 def terminate(signum, frame):
167 raise SystemExit(0)
168
169 signal.signal(signal.SIGTERM, terminate)
170 logging.basicConfig(level=logging.INFO, format="%(message)s")
171 multiprocessing.set_start_method("fork")
172
173 class SameOrigin:
174 def authenticate(self, headers, target_host, target_port):
175 if headers.get("Host") != f"{host}:{port}" or headers.get("Origin") != f"http://{host}:{port}":
176 raise AuthenticationError(response_msg="This console accepts only its configured origin.")
177
178 class ConsoleHandler(ProxyRequestHandler):
179 def do_GET(self):
180 if self.headers.get("Host") != f"{host}:{port}":
181 self.send_error(403)
182 return
183 super().do_GET()
184
185 class Relay(socketserver.BaseRequestHandler):
186 def handle(self):
187 with guest_screen() as upstream:
188 relay_screen(upstream, self.request.fileno(), self.request.fileno())
189
190 with socketserver.ThreadingTCPServer(("127.0.0.1", 0), Relay) as server:
191 server.daemon_threads = True
192 threading.Thread(target=server.serve_forever, daemon=True).start()
193 print(f"http://{host}:{port}/vnc_lite.html?autoconnect=true&resize=scale", flush=True)
194 try:
195 WebSocketProxy(RequestHandlerClass=ConsoleHandler, listen_host=host, listen_port=port,
196 web=str(ROOT / "runtime/novnc"), file_only=True, auth_plugin=SameOrigin(),
197 target_host="127.0.0.1", target_port=server.server_address[1]).start_server()
198 finally:
199 server.shutdown()
200
201
202def main():
203 parser = argparse.ArgumentParser(description="Manage this Mac's isolated GUI-agent VM.")
204 sub = parser.add_subparsers(dest="action", required=True)
205 sub.add_parser("start")
206 sub.add_parser("status")
207 sub.add_parser("stop")
208 sub.add_parser("suspend")
209 sub.add_parser("reboot")
210 for action in ["exec", "ssh"]:
211 command = sub.add_parser(action)
212 command.add_argument("command", nargs=argparse.REMAINDER)
213 viewer = sub.add_parser("console")
214 viewer.add_argument("--listen", default="127.0.0.1")
215 viewer.add_argument("--port", type=int, default=17891)
216 args = parser.parse_args()
217 if args.action in {"start", "stop", "suspend", "reboot"}:
218 lock = (PRIVATE / "lifecycle.lock").open("a")
219 os.chmod(PRIVATE / "lifecycle.lock", 0o600)
220 try:
221 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
222 except BlockingIOError:
223 raise ValueError("Another VM power operation is running. Wait for it to finish.") from None
224 lock.seek(0)
225 lock.truncate()
226 lock.write(args.action)
227 lock.flush()
228 if args.action == "reboot":
229 tart("exec", NAME, "sudo", "-n", "shutdown", "-h", "now", timeout=15)
230 wait_for_state("stopped")
231 args.action = "start"
232 if args.action == "start":
233 rows = json.loads(tart("list", "--format", "json", capture_output=True, text=True).stdout)
234 if next(row for row in rows if row["Name"] == NAME and row["Source"] == "local")["Running"]:
235 print("GUI-agent VM is already running.")
236 return
237 with (PRIVATE / "run.log").open("w") as log:
238 os.chmod(PRIVATE / "run.log", 0o600)
239 process = subprocess.Popen([str(TART), "run", NAME, "--no-graphics", "--no-audio",
240 "--no-usb-accessories", "--no-clipboard", "--suspendable",
241 "--root-disk-opts=sync=full"],
242 env={**os.environ, "TART_HOME": str(ROOT / "state")},
243 stdin=subprocess.DEVNULL, stdout=log, stderr=log,
244 start_new_session=True)
245 subprocess.Popen(["/usr/bin/caffeinate", "-i", "-w", str(process.pid)],
246 stdin=subprocess.DEVNULL, stdout=log, stderr=log,
247 start_new_session=True)
248 deadline = time.monotonic() + 90
249 while time.monotonic() < deadline:
250 if process.poll() is not None:
251 raise ValueError("VM startup failed; inspect the private run log locally.")
252 try:
253 tart("exec", NAME, "/usr/bin/true", capture_output=True, timeout=5)
254 except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
255 time.sleep(1)
256 else:
257 print("GUI-agent VM is ready in its logged-in guest session.")
258 return
259 raise ValueError("VM startup is still pending; inspect status before retrying.")
260 elif args.action == "status":
261 tart("get", NAME)
262 elif args.action == "exec":
263 if not args.command:
264 parser.error("exec requires a guest command")
265 tart("exec", "-i", NAME, *args.command)
266 elif args.action == "ssh":
267 ip = tart("ip", NAME, capture_output=True, text=True).stdout.strip()
268 command = ssh_args()
269 if not args.command:
270 command.append("-t")
271 os.execvp("ssh", [*command, "admin@" + ip, *args.command])
272 elif args.action == "stop":
273 tart("exec", NAME, "sudo", "-n", "shutdown", "-h", "now")
274 wait_for_state("stopped")
275 print("GUI-agent VM shut down.")
276 elif args.action == "suspend":
277 tart("suspend", NAME)
278 wait_for_state("suspended")
279 print("GUI-agent VM suspended.")
280 elif args.action == "console":
281 python = ROOT / "runtime/venv/bin/python"
282 if Path(sys.prefix) != python.parent.parent:
283 os.execv(str(python), [str(python), __file__, *sys.argv[1:]])
284 console(args.listen, args.port)
285
286
287if __name__ == "__main__":
288 try:
289 main()
290 except (ValueError, OSError, subprocess.CalledProcessError) as error:
291 print(str(error), file=sys.stderr)
292 sys.exit(1)
tools/mac-vm-bridge.py created+103
...@@ -0,0 +1,103 @@
1#!/usr/bin/env python3
2"""Restricted SSH entry point for the existing Mac development VM."""
3import fcntl
4from importlib import import_module
5import json
6import os
7import signal
8import struct
9import subprocess
10import sys
11
12vm = import_module("mac-agent-vm")
13
14
15def status():
16 current = json.loads(vm.tart("get", vm.NAME, "--format", "json", capture_output=True, text=True, timeout=5).stdout)
17 disk = vm.ROOT / "state/vms" / vm.NAME / "disk.img"
18 pending = None
19 with (vm.PRIVATE / "lifecycle.lock").open("a+") as lock:
20 try:
21 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
22 except BlockingIOError:
23 lock.seek(0)
24 pending = lock.read(32) or "start"
25 agent = None
26 if current["Running"]:
27 try:
28 vm.tart("exec", vm.NAME, "/usr/bin/true", capture_output=True, timeout=2)
29 agent = "connected"
30 except subprocess.SubprocessError:
31 agent = "disconnected"
32 return {"state": {"running": "running", "stopped": "shutoff", "suspended": "pmsuspended"}[current["State"]],
33 "vcpus": current["CPU"], "memory": current["Memory"] * 2**20,
34 "disk": {"capacity": disk.stat().st_size, "allocation": disk.stat().st_blocks * 512},
35 "agent": agent, "pendingAction": pending, "mac": current["MACAddress"]}
36
37
38def respond(value):
39 data = json.dumps(value).encode()
40 sys.stdout.buffer.write(struct.pack("!I", len(data)) + data)
41 sys.stdout.buffer.flush()
42
43
44def lifecycle(action):
45 process = subprocess.Popen([sys.executable, vm.__file__, action], stdout=subprocess.DEVNULL,
46 start_new_session=True)
47 try:
48 if process.wait(timeout=210):
49 raise ValueError("The power operation failed. Check the VM status before retrying.")
50 finally:
51 # Tart's detached VM runner survives; unfinished lifecycle commands do not.
52 try:
53 os.killpg(process.pid, signal.SIGKILL)
54 except ProcessLookupError:
55 pass
56 process.wait()
57
58
59def main():
60 line = sys.stdin.buffer.readline(1048577)
61 if len(line) > 1048576 or not line.endswith(b"\n"):
62 raise ValueError("The VM request is too large or incomplete.")
63 request = json.loads(line)
64 if not isinstance(request, dict):
65 raise ValueError("Choose a supported VM operation.")
66 action = request.get("action")
67 if action == "status" and set(request) == {"action"}:
68 respond({"value": status()})
69 elif action in {"start", "shutdown", "reboot", "suspend", "resume"} and set(request) == {"action"}:
70 action = {"shutdown": "stop", "resume": "start"}.get(action, action)
71 lifecycle(action)
72 respond({"value": None})
73 elif action == "clipboard" and set(request) == {"action", "text"}:
74 value = request["text"]
75 if not isinstance(value, str) or len(value.encode()) > 262144:
76 raise ValueError("Shorten the clipboard text, then send it again.")
77 vm.tart("exec", "-i", vm.NAME, "/usr/bin/pbcopy", input=value.encode(), capture_output=True, timeout=15)
78 respond({"value": None})
79 elif action == "console" and set(request) == {"action"}:
80 with vm.guest_screen() as upstream:
81 respond({"value": None})
82 try:
83 vm.relay_screen(upstream, sys.stdin.fileno(), sys.stdout.fileno())
84 except OSError:
85 pass
86 else:
87 raise ValueError("This operation is unavailable for this Mac VM.")
88
89
90if __name__ == "__main__":
91 python = vm.ROOT / "runtime/venv/bin/python"
92 if sys.prefix != str(python.parent.parent):
93 os.execv(str(python), [str(python), __file__])
94 def disconnected(signum, frame):
95 raise SystemExit(0)
96 signal.signal(signal.SIGHUP, disconnected)
97 signal.signal(signal.SIGTERM, disconnected)
98 try:
99 main()
100 except (ValueError, OSError, subprocess.SubprocessError) as error:
101 print(type(error).__name__, file=sys.stderr)
102 respond({"error": str(error) if isinstance(error, ValueError) else "The Mac VM did not answer. Check its host, then retry."})
103 sys.exit(1)
tools/mac-vm-test.py created+127
...@@ -0,0 +1,127 @@
1#!/usr/bin/env python3
2"""Mac VM protocol and failure tests; no real guest or credentials required."""
3import contextlib
4import fcntl
5from importlib import import_module
6import io
7import json
8import os
9from pathlib import Path
10import signal
11import subprocess
12import sys
13import tempfile
14import time
15import unittest
16from unittest.mock import patch
17
18bridge = import_module("mac-vm-bridge")
19backend = import_module("mac-vms")
20
21
22class MacVMTests(unittest.TestCase):
23 def test_protocol_rejects_arbitrary_commands_and_unbounded_clipboards(self):
24 for request in [{"action": "exec", "command": "id"}, {"action": "status", "name": "other"},
25 {"action": "clipboard", "text": "a" * 262145}, ["status"],
26 {"action": "clipboard", "text": 42}]:
27 with self.subTest(request=str(request)[:80]), \
28 patch.object(sys, "stdin", io.TextIOWrapper(io.BytesIO(json.dumps(request).encode() + b"\n"))), \
29 patch.object(bridge.vm, "tart") as tart, patch.object(bridge, "lifecycle") as lifecycle:
30 with self.assertRaises(ValueError):
31 bridge.main()
32 tart.assert_not_called()
33 lifecycle.assert_not_called()
34
35 def test_unicode_clipboard_is_stdin_and_accepts_full_byte_limit(self):
36 value = "🦊" * (262144 // 4)
37 request = json.dumps({"action": "clipboard", "text": value}).encode() + b"\n"
38 with patch.object(sys, "stdin", io.TextIOWrapper(io.BytesIO(request))), \
39 patch.object(bridge.vm, "tart") as tart, patch.object(bridge, "respond") as respond:
40 bridge.main()
41 self.assertEqual(tart.call_args.args, ("exec", "-i", "gui-agent", "/usr/bin/pbcopy"))
42 self.assertEqual(tart.call_args.kwargs["input"], value.encode())
43 respond.assert_called_once_with({"value": None})
44
45 def test_offline_inventory_keeps_identity_and_last_observed_hardware(self):
46 @contextlib.contextmanager
47 def connected(*args, **kwargs):
48 yield {"state": "running", "vcpus": 28, "memory": 48 * 2**30, "agent": "connected",
49 "pendingAction": None, "disk": {"capacity": 128 * 2**30, "allocation": 42 * 2**30},
50 "mac": "00:11:22:33:44:55"}, None
51 with tempfile.TemporaryDirectory() as directory, \
52 patch.object(backend, "CONFIG", {"name": "mac-fixture", "owner": "fixture-owner"}), \
53 patch.object(backend, "CACHE", Path(directory) / "cache.json"):
54 with patch.object(backend, "connect", side_effect=ValueError("offline")):
55 first = backend.domain()[0]
56 self.assertIsNone(first["memory"])
57 self.assertEqual(first["state"], "unreachable")
58 with patch.object(backend, "connect", connected):
59 live = backend.domain()[0]
60 self.assertEqual(live["state"], "running")
61 self.assertEqual(backend.CACHE.stat().st_mode & 0o777, 0o600)
62 with patch.object(backend.os, "replace", side_effect=OSError("disk full")):
63 self.assertEqual(backend.domain()[0]["state"], "running")
64 with patch.object(backend, "connect", side_effect=ValueError("offline")):
65 offline = backend.domain()[0]
66 self.assertEqual(offline["name"], "mac-fixture")
67 self.assertEqual(offline["owner"], "fixture-owner")
68 self.assertEqual(offline["memory"], live["memory"])
69 self.assertEqual(offline["state"], "unreachable")
70 self.assertIsNone(offline["agent"])
71 self.assertIsNone(offline["pendingAction"])
72
73 def test_unsupported_operations_never_contact_mac(self):
74 with patch.object(backend, "connect") as connect:
75 for action in ["delete", "update", "preset", "media"]:
76 with self.subTest(action=action), self.assertRaises(ValueError):
77 backend.handle(action, {"name": "mac-fixture"})
78 with self.assertRaises(ValueError):
79 backend.handle("act", {"action": "destroy"})
80 connect.assert_not_called()
81
82 def test_lifecycle_timeout_reaps_commands_but_preserves_detached_runner(self):
83 with tempfile.TemporaryDirectory() as directory:
84 root = Path(directory)
85 fixture = root / "lifecycle.py"
86 fixture.write_text('''import fcntl, os, pathlib, subprocess, sys, time
87root = pathlib.Path(__file__).parent
88lock = (root / "lock").open("w")
89fcntl.flock(lock, fcntl.LOCK_EX)
90child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"])
91runner = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"], start_new_session=True)
92(root / "pids").write_text(str(child.pid) + " " + str(runner.pid))
93time.sleep(60)
94''')
95 original = subprocess.Popen
96 def start(*args, **kwargs):
97 process = original(*args, **kwargs)
98 wait = process.wait
99 process.wait = lambda timeout=None: wait(timeout=0.5 if timeout == 210 else timeout)
100 return process
101 try:
102 with patch.object(bridge.vm, "__file__", str(fixture)), patch.object(bridge.subprocess, "Popen", start):
103 with self.assertRaises(subprocess.TimeoutExpired):
104 bridge.lifecycle("suspend")
105 child, runner = map(int, (root / "pids").read_text().split())
106 os.kill(runner, 0)
107 deadline = time.monotonic() + 5
108 while time.monotonic() < deadline:
109 state = subprocess.run(["ps", "-p", str(child), "-o", "stat="], capture_output=True, text=True).stdout.strip()
110 if not state or state.startswith("Z"):
111 break
112 time.sleep(0.02)
113 else:
114 self.fail("unfinished lifecycle command survived its helper")
115 with (root / "lock").open("r") as lock:
116 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
117 finally:
118 if (root / "pids").exists():
119 for pid in map(int, (root / "pids").read_text().split()):
120 try:
121 os.kill(pid, signal.SIGKILL)
122 except ProcessLookupError:
123 pass
124
125
126if __name__ == "__main__":
127 unittest.main()
tools/mac-vms.py created+119
...@@ -0,0 +1,119 @@
1import contextlib
2import json
3import os
4from pathlib import Path
5import socket
6import struct
7import subprocess
8import tempfile
9
10
11CONFIG = json.loads(os.environ.get("SNOWGLOBE_MAC_VM", "null"))
12CAPABILITIES = ["start", "shutdown", "reboot", "suspend", "resume", "clipboard"]
13CACHE = Path(os.environ.get("SNOWGLOBE_MAC_VM_CACHE", "/var/lib/snowglobe/host/mac-vm.json"))
14
15
16@contextlib.contextmanager
17def connect(request, timeout=10):
18 if not CONFIG:
19 raise ValueError("The Mac VM is not configured on this host.")
20 parent, child = socket.socketpair()
21 process = None
22 try:
23 process = subprocess.Popen([
24 "ssh", "-T", "-F", "/dev/null", "-o", "BatchMode=yes", "-o", "IdentitiesOnly=yes",
25 "-o", "ForwardAgent=no", "-o", "ConnectTimeout=3", "-o", "ConnectionAttempts=1",
26 "-o", "ServerAliveInterval=5", "-o", "ServerAliveCountMax=2",
27 "-o", "StrictHostKeyChecking=yes", "-o", "HostKeyAlias=mac-studio",
28 "-o", "UserKnownHostsFile=" + CONFIG["knownHosts"], "-o", "GlobalKnownHostsFile=/dev/null",
29 "-i", CONFIG["identity"], "-p", str(CONFIG["port"]), CONFIG["user"] + "@" + CONFIG["address"],
30 ], stdin=child, stdout=child, stderr=subprocess.DEVNULL)
31 child.close()
32 parent.settimeout(timeout)
33 parent.sendall(json.dumps(request).encode() + b"\n")
34
35 def read(size):
36 data = bytearray()
37 while len(data) < size:
38 chunk = parent.recv(size - len(data))
39 if not chunk:
40 raise ConnectionError("The Mac connection closed.")
41 data.extend(chunk)
42 return data
43
44 size, = struct.unpack("!I", read(4))
45 if size > 65536:
46 raise ValueError("The Mac response is too large.")
47 reply = json.loads(read(size))
48 if "error" in reply:
49 raise ValueError(reply["error"])
50 if "value" not in reply:
51 raise ValueError("The Mac response is incomplete.")
52 yield reply["value"], parent
53 except OSError as error:
54 raise ValueError("Mac Studio is unreachable. Check its power and network, then retry.") from error
55 finally:
56 parent.close()
57 child.close()
58 if process:
59 process.terminate()
60 try:
61 process.wait(timeout=5)
62 except subprocess.TimeoutExpired:
63 process.kill()
64 process.wait()
65
66
67def domain():
68 if not CONFIG:
69 return []
70 value = {"name": CONFIG["name"], "owner": CONFIG["owner"], "host": "Mac Studio", "arch": "aarch64",
71 "description": "macOS development", "state": "unreachable", "reason": None,
72 "os": "macOS", "vcpus": None, "memory": None, "balloon": None, "pinned": None,
73 "autostart": False, "startedAt": None, "agent": None, "setup": None, "pendingAction": None,
74 "disks": [], "media": [], "firmware": "uefi", "platform": "macos", "console": True,
75 "serial": False, "interfaces": [], "hostdevs": [], "capabilities": CAPABILITIES}
76 try:
77 with connect({"action": "status"}, timeout=8) as (status, _):
78 value.update({key: status[key] for key in ("state", "vcpus", "memory", "agent", "pendingAction")})
79 value["balloon"] = value["memory"]
80 value["disks"] = [{"target": "disk", "pool": "Mac Studio", "source": "gui-agent", **status["disk"]}]
81 value["interfaces"] = [{"source": "Mac Studio NAT", "mac": status["mac"], "addresses": []}]
82 except (ValueError, OSError, KeyError, TypeError):
83 try:
84 previous = json.loads(CACHE.read_text())
85 for key in ("vcpus", "memory", "balloon", "disks", "interfaces"):
86 value[key] = previous[key]
87 except (ValueError, OSError, KeyError, TypeError):
88 pass
89 value.update(state="unreachable", agent=None, pendingAction=None)
90 else:
91 try:
92 CACHE.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
93 descriptor, temporary = tempfile.mkstemp(dir=CACHE.parent, prefix=".mac-vm-")
94 try:
95 with os.fdopen(descriptor, "w") as outgoing:
96 json.dump(value, outgoing)
97 os.replace(temporary, CACHE)
98 finally:
99 Path(temporary).unlink(missing_ok=True)
100 except OSError:
101 pass
102 return [value]
103
104
105def handle(action, payload):
106 if action == "owner":
107 return CONFIG["owner"]
108 if action == "access":
109 return None
110 if action == "console":
111 return {"remote": True}
112 if action == "act" and payload["action"] in CAPABILITIES:
113 request = {"action": payload["action"]}
114 elif action == "clipboard":
115 request = {"action": "clipboard", "text": payload["text"]}
116 else:
117 raise ValueError("This operation is unavailable for this Mac VM.")
118 with connect(request, timeout=220) as (value, _):
119 return value
tools/release.py+1-1
...@@ -63,7 +63,7 @@ def host_digest(root):...@@ -63,7 +63,7 @@ def host_digest(root):
63 digest = hashlib.sha256()63 digest = hashlib.sha256()
64 paths = sorted(path for path, relative in files(root)64 paths = sorted(path for path, relative in files(root)
65 if relative.parts[0] in {"nixos", "dashboard", "config", "service"}65 if relative.parts[0] in {"nixos", "dashboard", "config", "service"}
66 or str(relative) in {"flake.nix", "flake.lock", "tools/router.py", "tools/dashboard-host.py", "tools/dashboard-run.py", "tools/release.py", "tools/namespace-transition.py", "tools/vms.py", "tools/vm-screen.py", "guest/protocol.json"})66 or str(relative) in {"flake.nix", "flake.lock", "tools/router.py", "tools/dashboard-host.py", "tools/dashboard-run.py", "tools/release.py", "tools/namespace-transition.py", "tools/vms.py", "tools/mac-vms.py", "tools/vm-screen.py", "guest/protocol.json"})
67 for path in paths:67 for path in paths:
68 digest.update(str(path.relative_to(root)).encode())68 digest.update(str(path.relative_to(root)).encode())
69 _hash_contents(digest, path)69 _hash_contents(digest, path)
tools/vm-screen.py+15-14
...@@ -815,20 +815,21 @@ async def serve(reader, writer):...@@ -815,20 +815,21 @@ async def serve(reader, writer):
815 raise ValueError(result["error"])815 raise ValueError(result["error"])
816 await asyncio.wait_for(desktop.start(), 10)816 await asyncio.wait_for(desktop.start(), 10)
817 screen = Screen(writer, desktop)817 screen = Screen(writer, desktop)
818 guest = GUESTS.get(name)818 if (result.get("value") or {}).get("guestDisplay", True):
819 if guest and guest.stopping:819 guest = GUESTS.get(name)
820 await asyncio.shield(guest.task)820 if guest and guest.stopping:
821 if name not in GUESTS:821 await asyncio.shield(guest.task)
822 GUESTS[name] = GuestDesktop(name)822 if name not in GUESTS:
823 GUESTS[name].task = asyncio.create_task(GUESTS[name].run())823 GUESTS[name] = GuestDesktop(name)
824 screen.guest = GUESTS[name]824 GUESTS[name].task = asyncio.create_task(GUESTS[name].run())
825 screen.guest.screens.add(screen)825 screen.guest = GUESTS[name]
826 if screen.guest.pixels:826 screen.guest.screens.add(screen)
827 screen.resize()827 if screen.guest.pixels:
828 if screen.guest.pointer:828 screen.resize()
829 screen.send(screen.guest.pointer)829 if screen.guest.pointer:
830 for cursor in screen.guest.cursor:830 screen.send(screen.guest.pointer)
831 screen.send(cursor)831 for cursor in screen.guest.cursor:
832 screen.send(cursor)
832 screen.send({"type": "ready", "iceServers": [{"urls": STUN.replace("stun://", "stun:")}] if STUN else []})833 screen.send({"type": "ready", "iceServers": [{"urls": STUN.replace("stun://", "stun:")}] if STUN else []})
833 task = asyncio.create_task(desktop.read(lambda: screen.present() if screen.source is desktop else None,834 task = asyncio.create_task(desktop.read(lambda: screen.present() if screen.source is desktop else None,
834 lambda: screen.resize() if screen.source is desktop else None))835 lambda: screen.resize() if screen.source is desktop else None))
tools/vms.py+16-4
...@@ -20,6 +20,7 @@ IMAGES = Path(os.environ.get("SNOWGLOBE_VM_IMAGES_ROOT", "/srv/clover/Media/vm")...@@ -20,6 +20,7 @@ IMAGES = Path(os.environ.get("SNOWGLOBE_VM_IMAGES_ROOT", "/srv/clover/Media/vm")
20PRESETS = Path(os.environ.get("SNOWGLOBE_VM_PRESETS_ROOT", str(IMAGES / "Presets")))20PRESETS = Path(os.environ.get("SNOWGLOBE_VM_PRESETS_ROOT", str(IMAGES / "Presets")))
21UPLOADS = Path(os.environ.get("SNOWGLOBE_VM_UPLOADS_ROOT", str(IMAGES / "Install Disks")))21UPLOADS = Path(os.environ.get("SNOWGLOBE_VM_UPLOADS_ROOT", str(IMAGES / "Install Disks")))
22transition = import_module("namespace-transition")22transition = import_module("namespace-transition")
23mac_vms = import_module("mac-vms")
23NS = "{https://paperclover.net/snowglobe}"24NS = "{https://paperclover.net/snowglobe}"
24NAME = re.compile(r"[a-z0-9][a-z0-9-]{0,62}\Z")25NAME = re.compile(r"[a-z0-9][a-z0-9-]{0,62}\Z")
25GUEST_CHANNEL = json.loads((Path(__file__).resolve().parent.parent / "guest/protocol.json").read_text())["channel"]26GUEST_CHANNEL = json.loads((Path(__file__).resolve().parent.parent / "guest/protocol.json").read_text())["channel"]
...@@ -76,6 +77,7 @@ ACTION_FIELDS = {...@@ -76,6 +77,7 @@ ACTION_FIELDS = {
76 "remove": {"name", "disks"},77 "remove": {"name", "disks"},
77 "library": None, "media": {"name", "image"},78 "library": None, "media": {"name", "image"},
78 "preset": {"name", "id", "os", "description"},79 "preset": {"name", "id", "os", "description"},
80 "clipboard": {"name", "text"},
79 "console": {"name"}, "serial": {"name"}, "guest": {"name"}, "owner": {"name"}, "access": {"name"}, "upload": {"volume", "size"},81 "console": {"name"}, "serial": {"name"}, "guest": {"name"}, "owner": {"name"}, "access": {"name"}, "upload": {"volume", "size"},
80}82}
8183
...@@ -137,7 +139,7 @@ def checked_profile(name, platform, firmware, arch):...@@ -137,7 +139,7 @@ def checked_profile(name, platform, firmware, arch):
137139
138def node():140def node():
139 memory = next(int(line.split()[1]) * 1024 for line in Path("/proc/meminfo").read_text().splitlines() if line.startswith("MemTotal:"))141 memory = next(int(line.split()[1]) * 1024 for line in Path("/proc/meminfo").read_text().splitlines() if line.startswith("MemTotal:"))
140 return {"cpus": os.cpu_count(), "memory": memory}142 return {"name": os.uname().nodename, "cpus": os.cpu_count(), "memory": memory}
141143
142144
143def validate(action, payload):145def validate(action, payload):
...@@ -168,8 +170,10 @@ def validate(action, payload):...@@ -168,8 +170,10 @@ def validate(action, payload):
168 for field in ("autostart", "start", "disks"):170 for field in ("autostart", "start", "disks"):
169 if field in payload and not isinstance(payload[field], bool):171 if field in payload and not isinstance(payload[field], bool):
170 raise ValueError(f"Choose whether to enable {field}.")172 raise ValueError(f"Choose whether to enable {field}.")
171 if action == "act" and (not isinstance(payload["action"], str) or payload["action"] not in {"start", "shutdown", "reboot", "destroy", "resume"}):173 if action == "act" and (not isinstance(payload["action"], str) or payload["action"] not in {"start", "shutdown", "reboot", "destroy", "resume", "suspend"}):
172 raise ValueError("Choose a supported VM action.")174 raise ValueError("Choose a supported VM action.")
175 if action == "clipboard" and (not isinstance(payload["text"], str) or len(payload["text"].encode()) > 262144):
176 raise ValueError("Shorten the clipboard text, then send it again.")
173 if action == "preset":177 if action == "preset":
174 ensure_name(payload["id"])178 ensure_name(payload["id"])
175 if not isinstance(payload["os"], str) or not 1 <= len(payload["os"]) <= 100:179 if not isinstance(payload["os"], str) or not 1 <= len(payload["os"]) <= 100:
...@@ -193,6 +197,8 @@ def validate(action, payload):...@@ -193,6 +197,8 @@ def validate(action, payload):
193 for field, minimum, maximum in [("vcpus", 1, host["cpus"]), ("memory", 2**29, host["memory"]), ("disk", 2**30, 2**63 - 1)]:197 for field, minimum, maximum in [("vcpus", 1, host["cpus"]), ("memory", 2**29, host["memory"]), ("disk", 2**30, 2**63 - 1)]:
194 if type(payload[field]) is not int or not minimum <= payload[field] <= maximum:198 if type(payload[field]) is not int or not minimum <= payload[field] <= maximum:
195 raise ValueError(f"Choose {field} within the host's supported range.")199 raise ValueError(f"Choose {field} within the host's supported range.")
200 if action == "create" and mac_vms.CONFIG and payload["name"] == mac_vms.CONFIG["name"]:
201 raise ValueError("A VM already has this name. Choose another name.")
196 if DISKS.is_symlink() or (DISKS / payload["name"]).is_symlink():202 if DISKS.is_symlink() or (DISKS / payload["name"]).is_symlink():
197 raise ValueError("The VM disk directory is a symbolic link. Remove the link before continuing.")203 raise ValueError("The VM disk directory is a symbolic link. Remove the link before continuing.")
198204
...@@ -291,6 +297,10 @@ def domains():...@@ -291,6 +297,10 @@ def domains():
291 setup = root.findtext(f"metadata/{NS}vm/{NS}setup")297 setup = root.findtext(f"metadata/{NS}vm/{NS}setup")
292 result.append({298 result.append({
293 "name": name,299 "name": name,
300 "host": os.uname().nodename,
301 "arch": root.find("./os/type").get("arch"),
302 "pendingAction": None,
303 "capabilities": ["start", "shutdown", "reboot", "destroy", "resume", "suspend", "edit", "delete", "media", "preset"],
294 "owner": root.findtext(f"metadata/{NS}vm/{NS}owner"),304 "owner": root.findtext(f"metadata/{NS}vm/{NS}owner"),
295 "description": root.findtext("description", ""),305 "description": root.findtext("description", ""),
296 "state": state,306 "state": state,
...@@ -937,7 +947,7 @@ def main():...@@ -937,7 +947,7 @@ def main():
937 if action == "node":947 if action == "node":
938 return node()948 return node()
939 if action == "domains":949 if action == "domains":
940 return domains()950 return domains() + mac_vms.domain()
941 if action == "stats":951 if action == "stats":
942 result = {}952 result = {}
943 for name in virsh("list", "--name").splitlines():953 for name in virsh("list", "--name").splitlines():
...@@ -952,6 +962,8 @@ def main():...@@ -952,6 +962,8 @@ def main():
952 return result962 return result
953 if action == "library":963 if action == "library":
954 return library()964 return library()
965 if mac_vms.CONFIG and isinstance(payload, dict) and payload.get("name") == mac_vms.CONFIG["name"]:
966 return mac_vms.handle(action, payload)
955 if action == "console":967 if action == "console":
956 return console_target(payload["name"])968 return console_target(payload["name"])
957 if action == "guest":969 if action == "guest":
...@@ -993,7 +1005,7 @@ def main():...@@ -993,7 +1005,7 @@ def main():
993 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive")))1005 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive")))
994 if root.find("./devices/interface/source[@network='default']") is not None:1006 if root.find("./devices/interface/source[@network='default']") is not None:
995 ensure_network()1007 ensure_network()
996 virsh({"start": "start", "shutdown": "shutdown", "reboot": "reboot", "destroy": "destroy", "resume": "resume"}[payload["action"]], name)1008 virsh({"start": "start", "shutdown": "shutdown", "reboot": "reboot", "destroy": "destroy", "resume": "resume", "suspend": "suspend"}[payload["action"]], name)
997 elif action == "update":1009 elif action == "update":
998 name = payload["name"]1010 name = payload["name"]
999 ensure_name(name)1011 ensure_name(name)