| author | |
| committer | |
| log | 324d9364aab99a3828fd57085de6970258f52b0c |
| tree | 968bf29a5a50074329d5f31b913c08167cb9bc02 |
| parent | 809516b87e112259617b90675369cdb1f08bde27 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
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-astra22 files changed, 1180 insertions(+), 63 deletions(-)
config/mac-studio-sandbox.json+2-1| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | { |
| 2 | 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 | 299 | } |
| 300 | 300 | let allocated: f64 = array(&values[1]) |
| 301 | 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 | 303 | .map(|vm| number(&vm["balloon"])) |
| 304 | 304 | .sum(); |
| 305 | 305 | values[0]["availableMemory"] = |
| ... | ... | @@ -361,6 +361,9 @@ pub async fn route( |
| 361 | 361 | ["vms", name, tail @ ..] => { |
| 362 | 362 | vm_name(name)?; |
| 363 | 363 | match tail { |
| 364 | ["clipboard"] if method == Method::POST => { | |
| 365 | vm_call("clipboard", Some(json!({"name":name,"text":body["text"]}))).await?; | |
| 366 | } | |
| 364 | 367 | ["access"] if method == Method::GET => { |
| 365 | 368 | let mut response = |
| 366 | 369 | Document::new(vm_call("access", Some(json!({"name":name}))).await?) |
| ... | ... | @@ -410,7 +413,7 @@ pub async fn route( |
| 410 | 413 | } |
| 411 | 414 | [action] |
| 412 | 415 | if method == Method::POST |
| 413 | && ["start", "shutdown", "reboot", "destroy", "resume"] | |
| 416 | && ["start", "shutdown", "reboot", "destroy", "resume", "suspend"] | |
| 414 | 417 | .contains(action) => |
| 415 | 418 | { |
| 416 | 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 | 34 | Some("vm.create" | "vm.media" | "vm.preset") |
| 35 | 35 | ) { |
| 36 | 36 | 910 |
| 37 | } else if request["operation"] == "vm.act" { | |
| 38 | 240 | |
| 37 | 39 | } else if request["operation"] == "copyparty.manage" { |
| 38 | 40 | 310 |
| 39 | 41 | } else { |
dashboard/web/api.contract.ts+4-1| ... | ... | @@ -320,6 +320,9 @@ export type Api = Hono<{}, { |
| 320 | 320 | "/vms/:name/media": { |
| 321 | 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 | 326 | "/vms/:name/access": { |
| 324 | 327 | $get: Endpoint<{ username: string; password: string; hostname: string } | null, { param: { name: string } }>; |
| 325 | 328 | }; |
| ... | ... | @@ -327,7 +330,7 @@ export type Api = Hono<{}, { |
| 327 | 330 | $post: Endpoint<null, { param: { name: string }; json: { id: string; os: string; description: string } }, 204, "body">; |
| 328 | 331 | }; |
| 329 | 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 | 335 | "/account": { |
| 333 | 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 | 9 | import RemoteKeyboard from "@novnc/keyboard"; |
| 10 | 10 | import { VM_SETUP, type Domain } from "../types/vms.ts"; |
| 11 | 11 | import { StatusLabel } from "./Status.tsx"; |
| 12 | import { toast } from "./Toast.tsx"; | |
| 12 | 13 | |
| 13 | 14 | type WindowSurface = { |
| 14 | 15 | id: number; x: number; y: number; width: number; height: number; atlas_x: number; atlas_y: number; |
| ... | ... | @@ -16,7 +17,7 @@ type WindowSurface = { |
| 16 | 17 | }; |
| 17 | 18 | type WindowFrame = { windows: WindowSurface[]; rtpTimestamp: number }; |
| 18 | 19 | |
| 19 | export function VMConsole(props: { name: string; enabled: boolean; active: boolean; setup?: Domain["setup"]; toolbar: JSX.Element; content: (toolbar: HTMLDivElement) => JSX.Element }) { | |
| 20 | export 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 | 21 | const enabled = createMemo(() => props.enabled); |
| 21 | 22 | const preparing = () => !!props.setup && props.setup !== "ready"; |
| 22 | 23 | let reconnectTimer: number | undefined; |
| ... | ... | @@ -72,6 +73,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole |
| 72 | 73 | const connected = () => state() === "Connected"; |
| 73 | 74 | const [problem, setProblem] = createSignal(""); |
| 74 | 75 | const [clipboard, setClipboard] = createSignal(""); |
| 76 | const [sendingClipboard, setSendingClipboard] = createSignal(false); | |
| 75 | 77 | const [showClipboard, setShowClipboard] = createSignal(false); |
| 76 | 78 | const [windowsAvailable, setWindowsAvailable] = createSignal(false); |
| 77 | 79 | const [separateWindows, setSeparateWindows] = createSignal(false); |
| ... | ... | @@ -389,7 +391,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole |
| 389 | 391 | {props.toolbar} |
| 390 | 392 | <Show when={props.active}> |
| 391 | 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 | 395 | <button disabled={!connected()} onClick={() => setShowClipboard(!showClipboard())}><Clipboard size={14} aria-hidden="true" />Clipboard</button> |
| 394 | 396 | </VMMenu> |
| 395 | 397 | <VMMenu label="View"> |
| ... | ... | @@ -404,7 +406,7 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole |
| 404 | 406 | </VMMenu> |
| 405 | 407 | <span class="spacer" /> |
| 406 | 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 | 410 | </Show> |
| 409 | 411 | </div> |
| 410 | 412 | <Show when={props.active && showStats()}> |
| ... | ... | @@ -418,9 +420,15 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole |
| 418 | 420 | <Show when={props.active && showClipboard()}> |
| 419 | 421 | <div class="vm-clipboard"> |
| 420 | 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 | 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 | 432 | <button class="button small" onClick={() => navigator.clipboard.writeText(clipboard()).catch((failure) => setProblem(reason(failure)))}>copy text</button> |
| 425 | 433 | </div> |
| 426 | 434 | </div> |
| ... | ... | @@ -461,6 +469,6 @@ export function VMConsole(props: { name: string; enabled: boolean; active: boole |
| 461 | 469 | <img ref={cursorInvert} class="vm-cursor vm-cursor-invert" alt="" aria-hidden="true" /> |
| 462 | 470 | </div> |
| 463 | 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 | 473 | </div>; |
| 466 | 474 | } |
dashboard/web/pages/VMs.tsx+36-22| ... | ... | @@ -14,6 +14,7 @@ import RotateCw from "lucide-solid/icons/rotate-cw"; |
| 14 | 14 | import Save from "lucide-solid/icons/save"; |
| 15 | 15 | import Trash from "lucide-solid/icons/trash"; |
| 16 | 16 | import Square from "lucide-solid/icons/square"; |
| 17 | import Pause from "lucide-solid/icons/pause"; | |
| 17 | 18 | import KeyRound from "lucide-solid/icons/key-round"; |
| 18 | 19 | import Usb from "lucide-solid/icons/usb"; |
| 19 | 20 | import { 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 | 54 | const running = (vm: Domain) => vm.state === "running" || vm.state === "blocked"; |
| 54 | 55 | |
| 55 | 56 | function 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 | 59 | if (running(vm)) { |
| 57 | 60 | if (vm.setup && vm.setup !== "ready") return [vm.setup === "failed" ? "down" : vm.setup === "restarting" ? "restarting" : "starting", VM_SETUP[vm.setup]]; |
| 58 | 61 | if (vm.startedAt === null) return ["healthy", "up"]; |
| ... | ... | @@ -87,7 +90,7 @@ const threads = (list: number[]) => list |
| 87 | 90 | .join(", "); |
| 88 | 91 | |
| 89 | 92 | /** Current and max memory, like "16 GiB", or "8–12 GiB" while the balloon holds some back. */ |
| 90 | const allocated = (vm: Domain) => (vm.balloon < vm.memory ? `${+(vm.balloon / GiB).toFixed(1)}–${bytes(vm.memory)}` : bytes(vm.memory)); | |
| 93 | const allocated = (vm: Domain) => vm.memory === null ? "—" : vm.balloon !== null && vm.balloon < vm.memory ? `${+(vm.balloon / GiB).toFixed(1)}–${bytes(vm.memory)}` : bytes(vm.memory); | |
| 91 | 94 | |
| 92 | 95 | /** Remote desktop for Windows, the web UI for Home Assistant, ssh for the rest. */ |
| 93 | 96 | function remote(os: string, address: string): [href: string, label: string] { |
| ... | ... | @@ -350,22 +353,22 @@ function Hardware(props: { vm: Domain; refetch: () => unknown; username?: string |
| 350 | 353 | }; |
| 351 | 354 | return ( |
| 352 | 355 | <div class="vm-detail"> |
| 353 | <div class="vm-media"> | |
| 356 | <Show when={props.vm.capabilities.includes("media")}><div class="vm-media"> | |
| 354 | 357 | <span class="muted">CD drive</span> |
| 355 | 358 | <span class="source">{props.vm.media.find((item) => item.source)?.source ?? "No ISO attached"}</span> |
| 356 | 359 | <span class="spacer" /><span class="muted">{props.vm.firmware === "uefi" ? "UEFI" : "Legacy BIOS"}</span> |
| 357 | </div> | |
| 360 | </div></Show> | |
| 358 | 361 | <Loaded data={history} what="usage history" retry={refetch} skeleton={ |
| 359 | 362 | <div class="vm-charts"><div class="skeleton" /><div class="skeleton" /></div> |
| 360 | 363 | }> |
| 361 | 364 | {(latest) => { |
| 362 | 365 | const samples = () => latest().domains[props.vm.name]; |
| 363 | 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 | 368 | <div class="vm-charts"> |
| 366 | 369 | <TimeChart series={[{ name: "cpu", t: latest().t, v: samples()!.cpu }]} format={percent} max={100} |
| 367 | 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 | 372 | colors={["var(--series-2)"]} height={104} inline sync={`vm-${props.vm.name}`} /> |
| 370 | 373 | </div> |
| 371 | 374 | </Show> |
| ... | ... | @@ -375,19 +378,25 @@ function Hardware(props: { vm: Domain; refetch: () => unknown; username?: string |
| 375 | 378 | <div class="facts"> |
| 376 | 379 | <div class="settings"> |
| 377 | 380 | <dl class="kv"> |
| 381 | <dt>host</dt><dd>{props.vm.host}</dd> | |
| 382 | <dt>architecture</dt><dd>{props.vm.arch}</dd> | |
| 378 | 383 | <Show when={props.username}><dt>guest user</dt><dd>{props.username}</dd></Show> |
| 379 | 384 | <Show when={props.hostname}>{(hostname) => <><dt>hostname</dt><dd><Copy value={hostname()} /></dd></>}</Show> |
| 380 | 385 | <dt>for</dt> |
| 381 | 386 | <dd> |
| 387 | <Show when={props.vm.capabilities.includes("edit")} fallback={props.vm.description}> | |
| 382 | 388 | <button class="describe" classList={{ empty: !props.vm.description }} data-tip="edit" |
| 383 | 389 | onClick={() => describe(props.vm, props.refetch)}> |
| 384 | 390 | {props.vm.description || "add a description"} |
| 385 | 391 | </button> |
| 392 | </Show> | |
| 386 | 393 | </dd> |
| 394 | <Show when={props.vm.capabilities.includes("edit")}> | |
| 387 | 395 | <dt>autostart</dt> |
| 388 | 396 | <dd> |
| 389 | 397 | <Checkbox checked={props.vm.autostart} disabled={saving()} onChange={setAutostart}>start with the host</Checkbox> |
| 390 | 398 | </dd> |
| 399 | </Show> | |
| 391 | 400 | <dt>cpu</dt> |
| 392 | 401 | <dd>{props.vm.pinned ? `pinned to threads ${threads(props.vm.pinned)}` : "floats over all threads"}</dd> |
| 393 | 402 | </dl> |
| ... | ... | @@ -462,17 +471,17 @@ function PowerControls(props: { vm: Domain; refetch: () => unknown }) { |
| 462 | 471 | } |
| 463 | 472 | }; |
| 464 | 473 | return <> |
| 465 | <Show when={isOff(props.vm)}> | |
| 466 | <button class="button small" disabled={pending()} aria-busy={pending()} onClick={() => run("start")}><Play size={14} aria-hidden="true" />start</button> | |
| 474 | <Show when={isOff(props.vm) && props.vm.capabilities.includes("start")}> | |
| 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 | 476 | </Show> |
| 468 | <Show when={props.vm.state === "paused" || props.vm.state === "pmsuspended"}> | |
| 469 | <button class="button small" disabled={pending()} aria-busy={pending()} onClick={() => run("resume")}><Play size={14} aria-hidden="true" />resume</button> | |
| 477 | <Show when={(props.vm.state === "paused" || props.vm.state === "pmsuspended") && props.vm.capabilities.includes("resume")}> | |
| 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 | 479 | </Show> |
| 471 | <Show when={props.vm.state === "shutdown"}> | |
| 480 | <Show when={props.vm.state === "shutdown" && props.vm.capabilities.includes("destroy")}> | |
| 472 | 481 | <button class="button small danger" onClick={() => forceOff(props.vm, props.refetch)}><Square size={14} aria-hidden="true" />force off</button> |
| 473 | 482 | </Show> |
| 474 | <Show when={running(props.vm)}> | |
| 475 | <button class="button small" onClick={() => stop(props.vm, props.refetch)}><Power size={14} aria-hidden="true" />stop</button> | |
| 483 | <Show when={running(props.vm) && props.vm.capabilities.includes("shutdown")}> | |
| 484 | <button class="button small" disabled={!!props.vm.pendingAction} onClick={() => stop(props.vm, props.refetch)}><Power size={14} aria-hidden="true" />stop</button> | |
| 476 | 485 | </Show> |
| 477 | 486 | </>; |
| 478 | 487 | } |
| ... | ... | @@ -486,13 +495,14 @@ function Row(props: { |
| 486 | 495 | const state = () => status(props.vm); |
| 487 | 496 | const pinned = () => props.vm.pinned && `pinned to threads ${threads(props.vm.pinned)}`; |
| 488 | 497 | const cpuTip = () => { |
| 489 | if (!props.vm.usage) return pinned() || undefined; | |
| 498 | if (!props.vm.usage || props.vm.vcpus === null) return pinned() || undefined; | |
| 490 | 499 | const busy = (props.vm.usage.cpu / 100) * props.vm.vcpus; |
| 491 | 500 | return [`${busy.toFixed(1)} of ${props.vm.vcpus} vcpus busy, ${percent((busy / props.hostCpus) * 100)} of the host`, pinned()] |
| 492 | 501 | .filter(Boolean).join(", "); |
| 493 | 502 | }; |
| 494 | 503 | const memoryTip = () => { |
| 495 | 504 | const used = props.vm.usage?.memory; |
| 505 | if (props.vm.balloon === null || props.vm.memory === null) return undefined; | |
| 496 | 506 | const grows = props.vm.balloon < props.vm.memory && `can grow to ${bytes(props.vm.memory)}`; |
| 497 | 507 | if (used === undefined) return grows ? `boots with ${bytes(props.vm.balloon)}, ${grows}` : undefined; |
| 498 | 508 | return [`${bytes(used)} / ${bytes(props.vm.balloon)} (${percent((used / props.vm.balloon) * 100)})`, grows] |
| ... | ... | @@ -514,7 +524,7 @@ function Row(props: { |
| 514 | 524 | <td class="num" data-tip={memoryTip()}>{props.vm.usage && bytes(props.vm.usage.memory)}</td> |
| 515 | 525 | <td class="actions" rowspan="2" onClick={(event) => event.stopPropagation()}> |
| 516 | 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 | 528 | <Show when={running(props.vm) && props.vm.interfaces.flatMap((nic) => nic.addresses)[0]}> |
| 519 | 529 | {(value) => { |
| 520 | 530 | const target = () => remote(props.vm.os, value()); |
| ... | ... | @@ -529,6 +539,7 @@ function Row(props: { |
| 529 | 539 | <td class="specs" colspan="2"> |
| 530 | 540 | <div onClick={(event) => event.stopPropagation()}> |
| 531 | 541 | <span>{props.vm.os}</span> |
| 542 | <span>{props.vm.host}</span> | |
| 532 | 543 | <For each={props.vm.interfaces}> |
| 533 | 544 | {(nic) => ( |
| 534 | 545 | <span> |
| ... | ... | @@ -561,7 +572,7 @@ function Row(props: { |
| 561 | 572 | <Show when={props.vm.autostart}><span><Power size={12} aria-hidden="true" />autostart</span></Show> |
| 562 | 573 | </div> |
| 563 | 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 | 576 | <td class="num" tabindex={memoryTip() ? 0 : undefined} data-tip={memoryTip()}>{allocated(props.vm)}</td> |
| 566 | 577 | </tr> |
| 567 | 578 | |
| ... | ... | @@ -628,7 +639,9 @@ export function VMs() { |
| 628 | 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 | 640 | }>{(vm) => <section class="vm-workspace" aria-label={`${name} workspace`}> |
| 630 | 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 | 645 | content={(toolbar) => <> |
| 633 | 646 | <Show when={consoleOpened()}><VMSerial name={name} enabled={running(vm()) && vm().platform === "linux" && vm().serial} active={activeTab() === "console"} toolbar={toolbar} /></Show> |
| 634 | 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 | 654 | </TabBar> |
| 642 | 655 | <VMMenu label="Machine"> |
| 643 | 656 | <PowerControls vm={vm()} refetch={refetch} /> |
| 644 | <button disabled={!running(vm())} onClick={() => restart(vm(), refetch)}><RotateCw size={14} aria-hidden="true" />Restart</button> | |
| 645 | <Show when={vm().state !== "shutdown"}><button disabled={isOff(vm())} onClick={() => forceOff(vm(), refetch)}><Square size={14} aria-hidden="true" />Force off</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> | |
| 647 | <button class="danger" onClick={() => remove(vm(), refetch)}><Trash size={14} aria-hidden="true" />Delete</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> | |
| 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> | |
| 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> | |
| 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 | 662 | </VMMenu> |
| 649 | <VMMenu label="Media"> | |
| 663 | <Show when={vm().capabilities.includes("media")}><VMMenu label="Media"> | |
| 650 | 664 | <button onClick={() => attachIso(vm(), () => latest().library, refetch)}><Disc3 size={14} aria-hidden="true" />Attach ISO</button> |
| 651 | 665 | <button disabled={!vm().media.some((item) => item.source)} onClick={async () => { |
| 652 | 666 | try { await parseResponse(api.vms[":name"].media.$put({ param: { name }, json: { image: "" } })); await refetch(); } |
| 653 | 667 | catch (failure) { toast(`Unable to eject ISO. ${reason(failure)}`); } |
| 654 | 668 | }}><Disc2 size={14} aria-hidden="true" />Eject ISO</button> |
| 655 | 669 | <span class="muted vm-menu-info">{vm().media.find((item) => item.source)?.source ?? "No ISO attached"}</span> |
| 656 | </VMMenu> | |
| 670 | </VMMenu></Show> | |
| 657 | 671 | <Show when={access()}>{(login) => <VMMenu label="Password"> |
| 658 | 672 | <label class="vm-password">Guest password<input aria-label="Guest password" value={login().password} readOnly spellcheck={false} /></label> |
| 659 | 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 | /** libvirt's virDomainState, minus "nostate". "shutdown" means a shutdown is under way. */ | |
| 2 | export type DomainState = "running" | "blocked" | "paused" | "shutdown" | "shutoff" | "crashed" | "pmsuspended"; | |
| 1 | /** "shutdown" means a shutdown is under way; "unreachable" means the host did not answer. */ | |
| 2 | export type DomainState = "running" | "blocked" | "paused" | "shutdown" | "shutoff" | "crashed" | "pmsuspended" | "unreachable"; | |
| 3 | 3 | |
| 4 | 4 | export interface Disk { |
| 5 | 5 | /** Guest device, like "vda". */ |
| ... | ... | @@ -34,19 +34,23 @@ export const VM_SETUP = { |
| 34 | 34 | export interface Domain { |
| 35 | 35 | name: string; |
| 36 | 36 | owner: string | null; |
| 37 | host: string; | |
| 38 | arch: string; | |
| 39 | capabilities: string[]; | |
| 40 | pendingAction: string | null; | |
| 37 | 41 | /** What it's for, from the domain's `<description>`; empty when unset. */ |
| 38 | 42 | description: string; |
| 39 | 43 | state: DomainState; |
| 40 | 44 | /** libvirt's reason for a paused or crashed state, like "user", "ioerror" or "panicked"; null otherwise. */ |
| 41 | 45 | reason: string | null; |
| 42 | 46 | os: string; |
| 43 | vcpus: number; | |
| 47 | vcpus: number | null; | |
| 44 | 48 | /** Host threads the vCPUs are pinned to; null when they float over all of them. */ |
| 45 | 49 | pinned: number[] | null; |
| 46 | 50 | /** The most memory the guest can have. */ |
| 47 | memory: number; | |
| 51 | memory: number | null; | |
| 48 | 52 | /** What the balloon currently gives the guest, at most `memory`. */ |
| 49 | balloon: number; | |
| 53 | balloon: number | null; | |
| 50 | 54 | autostart: boolean; |
| 51 | 55 | /** Unix seconds the QEMU process started; null while shut off. */ |
| 52 | 56 | startedAt: number | null; |
| ... | ... | @@ -80,7 +84,7 @@ export interface History { |
| 80 | 84 | domains: Record<string, { cpu: (number | null)[]; memory: (number | null)[] }>; |
| 81 | 85 | } |
| 82 | 86 | |
| 83 | export const DOMAIN_ACTIONS = ["start", "shutdown", "reboot", "destroy", "resume"] as const; | |
| 87 | export const DOMAIN_ACTIONS = ["start", "shutdown", "reboot", "destroy", "resume", "suspend"] as const; | |
| 84 | 88 | export interface Preset { |
| 85 | 89 | id: string; |
| 86 | 90 | os: string; |
nixos/configuration.nix+1-1| ... | ... | @@ -6,7 +6,7 @@ let |
| 6 | 6 | proxyToken = "/var/lib/snowglobe/dashboard-proxy.token"; |
| 7 | 7 | hostTools = lib.fileset.toSource { |
| 8 | 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 | 11 | nativePkl = pkgs.callPackage ./pkl.nix { }; |
| 12 | 12 | screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]); |
nixos/mac-studio-sandbox.nix+1-1| ... | ... | @@ -50,7 +50,7 @@ in |
| 50 | 50 | enable = true; |
| 51 | 51 | hostName = "10.0.0.1"; |
| 52 | 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 | 56 | services.nfs.settings.nfsd = { |
nixos/mac-vm.nix created+17| ... | ... | @@ -0,0 +1,17 @@ |
| 1 | { ... }: | |
| 2 | let | |
| 3 | workspace = builtins.fromJSON (builtins.readFile ../config/mac-studio-sandbox.json); | |
| 4 | in { | |
| 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 | 16 | }; |
| 17 | 17 | in |
| 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 ]; | |
| 20 | 20 | |
| 21 | 21 | networking.hostName = "snowglobe"; |
| 22 | 22 | environment.etc."machine-info".text = '' |
| ... | ... | @@ -30,7 +30,7 @@ in |
| 30 | 30 | networking.firewall.extraCommands = lib.concatMapStrings ({ address, prefixLength, ... }: '' |
| 31 | 31 | iptables -A nixos-fw -i enp4s0 -s ${address}/${toString prefixLength} -p tcp --dport 445 -j nixos-fw-accept |
| 32 | 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-accept | |
| 33 | 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 | 34 | ip6tables -A nixos-fw -i enp4s0 -s fe80::/10 -p tcp --dport 445 -j nixos-fw-accept |
| 35 | 35 | 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 | |
| 2 | import os | |
| 3 | from pathlib import Path | |
| 4 | import subprocess | |
| 5 | import sys | |
| 6 | ||
| 7 | ||
| 8 | helper = str(Path(__file__).resolve().with_name("mac-agent-vm.py")) | |
| 9 | if 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.") | |
| 21 | else: | |
| 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 | 183 | vms.validate(action, payload) |
| 184 | 184 | except ValueError as error: |
| 185 | 185 | 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 | 193 | args = ["python3", str(Path(__file__).with_name("vms.py")), action] |
| 187 | 194 | if payload is not None: |
| 188 | 195 | args.append(json.dumps(payload)) |
| ... | ... | @@ -194,7 +201,7 @@ class Host: |
| 194 | 201 | return json.loads(command(*args, timeout=900)) |
| 195 | 202 | finally: |
| 196 | 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 | 205 | except subprocess.CalledProcessError as error: |
| 199 | 206 | if error.returncode == 2: |
| 200 | 207 | raise Rejected(error.stderr) from error |
| ... | ... | @@ -344,6 +351,9 @@ def stream_console(connection, host, request): |
| 344 | 351 | screen.settimeout(10) |
| 345 | 352 | screen.connect(details["path"]) |
| 346 | 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 | 357 | else: |
| 348 | 358 | identity = details["uuid"] |
| 349 | 359 | deadline = time.monotonic() + 5 |
| ... | ... | @@ -359,7 +369,7 @@ def stream_console(connection, host, request): |
| 359 | 369 | if details["uuid"] != identity: |
| 360 | 370 | raise Rejected("The VM changed while its screen was opening. Open it again.") |
| 361 | 371 | screen.setblocking(False) |
| 362 | send_response(connection, {"value": None}) | |
| 372 | send_response(connection, {"value": {"guestDisplay": False} if details.get("remote") else None}) | |
| 363 | 373 | try: |
| 364 | 374 | connection.setblocking(False) |
| 365 | 375 | with selectors.DefaultSelector() as selector: |
tools/mac-agent-gui.swift created+164| ... | ... | @@ -0,0 +1,164 @@ |
| 1 | import AppKit | |
| 2 | import ApplicationServices | |
| 3 | import ImageIO | |
| 4 | import ScreenCaptureKit | |
| 5 | import UniformTypeIdentifiers | |
| 6 | ||
| 7 | func output(_ value: Any) { | |
| 8 | let data = try! JSONSerialization.data(withJSONObject: value, options: [.sortedKeys]) | |
| 9 | print(String(data: data, encoding: .utf8)!) | |
| 10 | } | |
| 11 | ||
| 12 | func fail(_ message: String) -> Never { | |
| 13 | output(["error": message]) | |
| 14 | exit(1) | |
| 15 | } | |
| 16 | ||
| 17 | func attribute(_ element: AXUIElement, _ name: String) -> CFTypeRef? { | |
| 18 | var value: CFTypeRef? | |
| 19 | return AXUIElementCopyAttributeValue(element, name as CFString, &value) == .success ? value : nil | |
| 20 | } | |
| 21 | ||
| 22 | func 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 | ||
| 57 | let arguments = Array(CommandLine.arguments.dropFirst()) | |
| 58 | guard let command = arguments.first else { fail("Use status, request-permissions, screenshot, tree, press, click, key, or type.") } | |
| 59 | guard let eventSource = CGEventSource(stateID: .privateState) else { fail("Could not create a private input event source.") } | |
| 60 | switch command { | |
| 61 | case "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()]) | |
| 68 | case "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]]) | |
| 85 | case "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 | } | |
| 110 | case "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]]) | |
| 121 | case "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]) | |
| 141 | case "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]) | |
| 162 | default: | |
| 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, | |
| 4 | image 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 | |
| 6 | root. Missing storage fails explicitly; the tool does not recreate it on the | |
| 7 | startup disk. | |
| 8 | ||
| 9 | Documents' external volume has ownership disabled. The VM root uses | |
| 10 | an explicit named-Clover allow ACL before an everyone-deny ACL, with file and | |
| 11 | directory inheritance; mode bits alone would not protect it. This order and | |
| 12 | identity matching were tested on disposable files, including denied access | |
| 13 | despite permissive POSIX modes. The signed Tart app is protected by its | |
| 14 | ancestor directory ACL because macOS App Management rejects modifying the | |
| 15 | bundle's own ACL. Volume ownership and host security settings are unchanged. | |
| 16 | ||
| 17 | ```sh | |
| 18 | python3 tools/mac-agent-vm.py start | |
| 19 | python3 tools/mac-agent-vm.py status | |
| 20 | python3 tools/mac-agent-vm.py exec /bin/zsh -lc 'rustc --version; node --version' | |
| 21 | python3 tools/mac-agent-vm.py ssh | |
| 22 | python3 tools/mac-agent-vm.py console | |
| 23 | python3 tools/mac-agent-vm.py stop | |
| 24 | ``` | |
| 25 | ||
| 26 | `start` resumes a suspended VM or boots a stopped VM, then waits for commands | |
| 27 | to work through the logged-in guest agent. `stop` requests a graceful | |
| 28 | guest shutdown and waits for it to finish. `suspend` waits for Tart's RAM | |
| 29 | snapshot; it writes memory to disk and is not an instant CPU pause. A running | |
| 30 | VM holds a process-scoped idle-sleep assertion, so locking the host does not | |
| 31 | cause idle sleep. Host login, FileVault, and explicit sleep remain host policy. | |
| 32 | ||
| 33 | The guest has 28 vCPUs, 48 GiB RAM, and a 1440×900 display. vCPUs do not reserve | |
| 34 | physical cores. CPU count and RAM changes require a cold boot. No automatic | |
| 35 | lock/unlock controller or CPU affinity is installed. The guest's 48 GiB budget | |
| 36 | is fixed for a run. Apple [allocates physical memory on demand](https://developer.apple.com/documentation/virtualization/vzvirtualmachineconfiguration/memorysize), | |
| 37 | so it need not occupy 48 GiB immediately. Stock Tart has no verified balloon or | |
| 38 | guest-aware reclaim path; sizing assumes previously used pages may remain | |
| 39 | backed until the VM stops. This follows Clover's 48 GiB fallback. | |
| 40 | ||
| 41 | The 128 GB sparse disk contains a system APFS container of about 122 GB, | |
| 42 | a 5.4 GB recovery partition, and the original boot container. The system | |
| 43 | volume no longer has a separate 44 GB ceiling. Cache placement is managed by | |
| 44 | the guest's setup configuration under `~/.local/share/agents/vm`; projects and | |
| 45 | bulk 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 | |
| 48 | cache references were updated. A normal unmount and deletion targeted only | |
| 49 | volume UUID `D7650D6C-F7CC-444B-ACA3-EA472ADEB3B8`; the Code workspace and its | |
| 50 | replacement cache image were verified mounted afterward. Recovery archives | |
| 51 | are 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 | |
| 53 | matched 16,121 entries by content, mode, and symlink target, and all five | |
| 54 | restored SQLite databases passed integrity checks. | |
| 55 | ||
| 56 | The 2026-10-20 storage rebuild preserved the configured guest, accounts, tools, | |
| 57 | and automation approvals. It copied the system bytes into a sparse disk on | |
| 58 | Documents, moved and hash-verified the unchanged recovery partition, expanded | |
| 59 | the system container, and restored Scratch from the stopped original disk | |
| 60 | through a host read-only mount with explicit ownership. Full retained-content | |
| 61 | and metadata comparison, a detached-source cold boot, pinned SSH, actual screen | |
| 62 | capture, and browser keyboard input passed before removing the old Project | |
| 63 | installation. Documents is the only VM home; private rebuild evidence remains | |
| 64 | in `private/rebuild-20261020`. The temporary public Chromium-trial Xcode copy | |
| 65 | was omitted during the move; its pinned host source was verified before the | |
| 66 | old installation was removed. | |
| 67 | ||
| 68 | ## Dashboard access | |
| 69 | ||
| 70 | The existing `gui-agent` is registered as `mac-studio-gui-agent` in Snow Globe's | |
| 71 | VM dashboard. Its owner is the Snow account declared in `nixos/mac-vm.nix`. | |
| 72 | The dashboard retains its row when Mac Studio is unreachable; hardware in that | |
| 73 | state is the last successful observation, not live telemetry. Mac resources do | |
| 74 | not count against Snow Globe's Linux VM capacity. | |
| 75 | ||
| 76 | Snow 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` | |
| 79 | entry uses `restrict,command=` to invoke `mac-vm-bridge.py`; it grants no shell, | |
| 80 | forwarding, PTY, arbitrary VM selection, or arbitrary guest command. Deploy the | |
| 81 | bridge and `mac-agent-vm.py` together under the VM home's `runtime/control` and | |
| 82 | keep that forced command pointed there, independent of an agent session. | |
| 83 | ||
| 84 | The existing host sshd provides supervision. Each console connection creates | |
| 85 | a fresh, pinned guest SSH tunnel, authenticates Screen Sharing on the Mac, | |
| 86 | and relays RFB into the dashboard's existing WebRTC viewer. No public VNC or | |
| 87 | HTTP control port is required. Dashboard ownership checks cover the screen, | |
| 88 | power operations, and clipboard. Unicode clipboard text travels over stdin | |
| 89 | to the logged-in guest's `pbcopy`, never through command arguments or logs. | |
| 90 | ||
| 91 | Supported controls are start/resume, graceful shutdown/restart, and suspend. | |
| 92 | Power operations are serialized; a timeout kills unfinished lifecycle commands | |
| 93 | while preserving the detached VM runner. Hardware edits, media attachment, | |
| 94 | force-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 | |
| 101 | code-vm # Interactive SSH shell | |
| 102 | code-vm 'uname -a' # Run a guest command | |
| 103 | code-vm --clipboard # Copy the host clipboard into the guest | |
| 104 | ``` | |
| 105 | ||
| 106 | After copying the clipboard, focus the guest and press Command-V. Text travels | |
| 107 | through the guest agent into the logged-in session's pasteboard; no automatic | |
| 108 | keystroke or continuous clipboard sharing is enabled. SSH resolves the current | |
| 109 | VM address each time and reuses the existing pinned key configuration. | |
| 110 | ||
| 111 | The 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 | |
| 113 | file is not inherited. The previous guest file is `/etc/hosts.before-code-nas`. | |
| 114 | ||
| 115 | SSH uses a new private key in `private/ssh_key`, unique guest host keys pinned | |
| 116 | in `private/known_hosts`, and disables password authentication. No host SSH | |
| 117 | configuration, agent, accounts, or project directories are forwarded. The | |
| 118 | guest's randomized login password and auto-login credential are private to | |
| 119 | this VM. `admin` has passwordless sudo. | |
| 120 | ||
| 121 | The 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 | |
| 123 | commands: its Accessibility, Screen Recording, and Full Disk Access permissions | |
| 124 | are approved in the guest's System Settings. SIP remains enabled. Permission | |
| 125 | grants belong to the responsible guest agent; commands launched through another | |
| 126 | process may need separate guest approval. | |
| 127 | ||
| 128 | The first ScreenCaptureKit capture also requested permission to bypass the | |
| 129 | private-window picker; that guest prompt was approved separately. Future OS | |
| 130 | changes or capture-authorization renewals can prompt again: a successful | |
| 131 | preflight status is not proof that every future operation is prompt-free. | |
| 132 | Verify actual capture and input after upgrades. | |
| 133 | ||
| 134 | ```sh | |
| 135 | python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui status | |
| 136 | python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui screenshot /tmp/screen.png | |
| 137 | python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui tree | |
| 138 | python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui click 400 300 | |
| 139 | python3 tools/mac-agent-vm.py exec /usr/local/bin/agent-gui key 1 command | |
| 140 | python3 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 | |
| 144 | roles, 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 | |
| 146 | depth and node limits. Rebuild `mac-agent-gui.swift` with `swiftc -O`, install | |
| 147 | the binary at `/usr/local/bin/agent-gui` inside the guest, and recheck actual | |
| 148 | permissions and capture after changing its executable. | |
| 149 | ||
| 150 | The optional local recovery console is `http://127.0.0.1:17891/vnc_lite.html?autoconnect=true`. | |
| 151 | It reaches the guest's built-in Screen Sharing through pinned SSH, forwarding | |
| 152 | guest `127.0.0.1:5900` into a temporary private Unix socket. The relay handles | |
| 153 | the guest account authentication from `private/password` server-side. Screen | |
| 154 | Sharing listens on all interfaces inside the private NAT guest; the host relay's | |
| 155 | TCP listeners default to loopback and check the exact HTTP Host and WebSocket | |
| 156 | Origin. Tart's experimental host VNC server is disabled. The viewer trusts | |
| 157 | every client that can reach its listener; Origin checking protects against foreign | |
| 158 | websites, not other clients. For remote access, bind a separate console to this | |
| 159 | Mac's Tailscale IPv4 address: | |
| 160 | ||
| 161 | ```sh | |
| 162 | python3 tools/mac-agent-vm.py console --listen "$(tailscale ip -4)" | |
| 163 | ``` | |
| 164 | ||
| 165 | Use the printed IP-based URL; hostname aliases fail the exact Host check. | |
| 166 | This exposes desktop control to reachable tailnet peers without opening the LAN | |
| 167 | interface. Keep the authenticated RFB relay itself on loopback. Reconnect the | |
| 168 | viewer after a VM restart; each connection resolves the guest address again. | |
| 169 | Use the authenticated dashboard for ordinary remote access. | |
| 170 | Console shutdown terminates its SSH child and removes the private socket. | |
| 171 | If the page says “connection is closed” while SSH still works, restart the | |
| 172 | console relay; the VM does not need restarting. Run `console` in a host Terminal | |
| 173 | to keep its lifetime independent of an agent thread. Its SSH tunnel has a | |
| 174 | separate session so terminal hangups do not leave the HTTP listener stranded. | |
| 175 | Apple ARD authentication attaches the guest user's session; legacy VNC-only | |
| 176 | authentication is disabled. SSH encrypts its transport. Credentials never appear | |
| 177 | in arguments or browser URLs. | |
| 178 | ||
| 179 | ## Guest policy and tools | |
| 180 | ||
| 181 | Spotlight indexing, Siri, idle sleep, screen saver, screen lock, automatic OS | |
| 182 | update checks/downloads/installations, crash dialogs, and automatic diagnostic | |
| 183 | submission are disabled in the guest. Gatekeeper assessments are disabled as | |
| 184 | requested. Developer mode is enabled with `DevToolsSecurity`. On 2026-10-20, | |
| 185 | installed applications, including Xcode and Device Hub, received Full Disk | |
| 186 | Access, device control, screen recording, input monitoring, developer-tool | |
| 187 | access, and app-management approvals through System Settings. Existing apps | |
| 188 | may need a normal relaunch to use new grants. These are per-app permissions; | |
| 189 | future apps and request-only categories can still prompt. SIP remains enabled. | |
| 190 | FSEvents and durable disk synchronization remain enabled. Apply OS updates | |
| 191 | deliberately between test runs, then recheck GUI permissions and boot. | |
| 192 | ||
| 193 | Initial provisioning installed Rust/Cargo 1.99.1, Node 24.22.0 LTS, npm 11.19.1, | |
| 194 | pnpm 12.14.0, Jujutsu 0.42.0, and Apple's CLT with Clang 17 and the macOS SDK. | |
| 195 | Subsequent guest setup manages toolchain versions and paths. Login shells load | |
| 196 | their paths from `~/.zprofile` or `~/.profile`; use | |
| 197 | `exec /bin/zsh -lc ...` for development commands. | |
| 198 | ||
| 199 | ## Provenance | |
| 200 | ||
| 201 | Provisioned on 2026-10-19 with the signed [Tart 2.41.1 release](https://github.com/openai/tart/releases/tag/2.41.1), | |
| 202 | archive SHA-256 `e680a734febccfce7d4e910c1f7f13fc3b00f19a181c945e2d746de3214c32c2`. | |
| 203 | The Cirrus Sequoia vanilla image is pinned to | |
| 204 | `ghcr.io/cirruslabs/macos-sequoia-vanilla@sha256:57a83b1b52f928f5b11b0963927178fd6c4cac52c315279f227c27cc5ab81587` | |
| 205 | and boots macOS 15.7.7. Its default credentials were rotated after cloning. | |
| 206 | The [published template](https://github.com/cirruslabs/macos-image-templates/blob/master/templates/vanilla-sequoia.pkr.hcl) | |
| 207 | documents its original auto-login, CLT, SSH, sudo, and Gatekeeper setup. | |
| 208 | ||
| 209 | The VM agent is [Tart guest agent 0.15.0](https://github.com/openai/tart-guest-agent/releases/tag/v0.15.0), | |
| 210 | verified against release archive SHA-256 | |
| 211 | `eb47f402f18e742a8ea96344115ca776179f9c4c67494bc0c46a10a68db280b7`. | |
| 212 | The local browser relay uses noVNC 1.7.0, websockify 0.13.0, and cryptography | |
| 213 | for Apple ARD authentication, following [noVNC's protocol implementation](https://github.com/novnc/noVNC/blob/v1.7.0/core/rfb.js). | |
| 214 | Their runtime environment stays outside the checkout. No production service | |
| 215 | configuration is changed. |
tools/mac-agent-vm.py created+292| ... | ... | @@ -0,0 +1,292 @@ |
| 1 | #!/usr/bin/env python3 | |
| 2 | import argparse | |
| 3 | import contextlib | |
| 4 | import fcntl | |
| 5 | import hashlib | |
| 6 | import json | |
| 7 | import logging | |
| 8 | import multiprocessing | |
| 9 | import os | |
| 10 | from pathlib import Path | |
| 11 | import select | |
| 12 | import secrets | |
| 13 | import signal | |
| 14 | import socket | |
| 15 | import socketserver | |
| 16 | import struct | |
| 17 | import subprocess | |
| 18 | import sys | |
| 19 | import tempfile | |
| 20 | import threading | |
| 21 | import time | |
| 22 | ||
| 23 | ||
| 24 | ROOT = Path(os.environ.get("MAC_AGENT_VM_HOME", "/Volumes/Documents/VM/macOS Code Sandbox")) | |
| 25 | TART = ROOT / "runtime/tart.app/Contents/MacOS/tart" | |
| 26 | NAME = "gui-agent" | |
| 27 | PRIVATE = ROOT / "private" | |
| 28 | ||
| 29 | ||
| 30 | def tart(*args, **kwargs): | |
| 31 | return subprocess.run([str(TART), *args], env={**os.environ, "TART_HOME": str(ROOT / "state")}, | |
| 32 | check=True, **kwargs) | |
| 33 | ||
| 34 | ||
| 35 | def 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 | ||
| 43 | def 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 | ||
| 53 | def 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 | |
| 63 | def 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 | ||
| 96 | def 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 | ||
| 128 | def 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 | ||
| 162 | def 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 | ||
| 202 | def 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 | ||
| 287 | if __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.""" | |
| 3 | import fcntl | |
| 4 | from importlib import import_module | |
| 5 | import json | |
| 6 | import os | |
| 7 | import signal | |
| 8 | import struct | |
| 9 | import subprocess | |
| 10 | import sys | |
| 11 | ||
| 12 | vm = import_module("mac-agent-vm") | |
| 13 | ||
| 14 | ||
| 15 | def 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 | ||
| 38 | def 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 | ||
| 44 | def 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 | ||
| 59 | def 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 | ||
| 90 | if __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.""" | |
| 3 | import contextlib | |
| 4 | import fcntl | |
| 5 | from importlib import import_module | |
| 6 | import io | |
| 7 | import json | |
| 8 | import os | |
| 9 | from pathlib import Path | |
| 10 | import signal | |
| 11 | import subprocess | |
| 12 | import sys | |
| 13 | import tempfile | |
| 14 | import time | |
| 15 | import unittest | |
| 16 | from unittest.mock import patch | |
| 17 | ||
| 18 | bridge = import_module("mac-vm-bridge") | |
| 19 | backend = import_module("mac-vms") | |
| 20 | ||
| 21 | ||
| 22 | class 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 | |
| 87 | root = pathlib.Path(__file__).parent | |
| 88 | lock = (root / "lock").open("w") | |
| 89 | fcntl.flock(lock, fcntl.LOCK_EX) | |
| 90 | child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"]) | |
| 91 | runner = 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)) | |
| 93 | time.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 | ||
| 126 | if __name__ == "__main__": | |
| 127 | unittest.main() |
tools/mac-vms.py created+119| ... | ... | @@ -0,0 +1,119 @@ |
| 1 | import contextlib | |
| 2 | import json | |
| 3 | import os | |
| 4 | from pathlib import Path | |
| 5 | import socket | |
| 6 | import struct | |
| 7 | import subprocess | |
| 8 | import tempfile | |
| 9 | ||
| 10 | ||
| 11 | CONFIG = json.loads(os.environ.get("SNOWGLOBE_MAC_VM", "null")) | |
| 12 | CAPABILITIES = ["start", "shutdown", "reboot", "suspend", "resume", "clipboard"] | |
| 13 | CACHE = Path(os.environ.get("SNOWGLOBE_MAC_VM_CACHE", "/var/lib/snowglobe/host/mac-vm.json")) | |
| 14 | ||
| 15 | ||
| 16 | @contextlib.contextmanager | |
| 17 | def 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 | ||
| 67 | def 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 | ||
| 105 | def 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 | 63 | digest = hashlib.sha256() |
| 64 | 64 | paths = sorted(path for path, relative in files(root) |
| 65 | 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 | 67 | for path in paths: |
| 68 | 68 | digest.update(str(path.relative_to(root)).encode()) |
| 69 | 69 | _hash_contents(digest, path) |
tools/vm-screen.py+15-14| ... | ... | @@ -815,20 +815,21 @@ async def serve(reader, writer): |
| 815 | 815 | raise ValueError(result["error"]) |
| 816 | 816 | await asyncio.wait_for(desktop.start(), 10) |
| 817 | 817 | screen = Screen(writer, desktop) |
| 818 | guest = GUESTS.get(name) | |
| 819 | if guest and guest.stopping: | |
| 820 | await asyncio.shield(guest.task) | |
| 821 | if name not in GUESTS: | |
| 822 | GUESTS[name] = GuestDesktop(name) | |
| 823 | GUESTS[name].task = asyncio.create_task(GUESTS[name].run()) | |
| 824 | screen.guest = GUESTS[name] | |
| 825 | screen.guest.screens.add(screen) | |
| 826 | if screen.guest.pixels: | |
| 827 | screen.resize() | |
| 828 | if screen.guest.pointer: | |
| 829 | screen.send(screen.guest.pointer) | |
| 830 | for cursor in screen.guest.cursor: | |
| 831 | screen.send(cursor) | |
| 818 | if (result.get("value") or {}).get("guestDisplay", True): | |
| 819 | guest = GUESTS.get(name) | |
| 820 | if guest and guest.stopping: | |
| 821 | await asyncio.shield(guest.task) | |
| 822 | if name not in GUESTS: | |
| 823 | GUESTS[name] = GuestDesktop(name) | |
| 824 | GUESTS[name].task = asyncio.create_task(GUESTS[name].run()) | |
| 825 | screen.guest = GUESTS[name] | |
| 826 | screen.guest.screens.add(screen) | |
| 827 | if screen.guest.pixels: | |
| 828 | screen.resize() | |
| 829 | if screen.guest.pointer: | |
| 830 | screen.send(screen.guest.pointer) | |
| 831 | for cursor in screen.guest.cursor: | |
| 832 | screen.send(cursor) | |
| 832 | 833 | screen.send({"type": "ready", "iceServers": [{"urls": STUN.replace("stun://", "stun:")}] if STUN else []}) |
| 833 | 834 | task = asyncio.create_task(desktop.read(lambda: screen.present() if screen.source is desktop else None, |
| 834 | 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 | 20 | PRESETS = Path(os.environ.get("SNOWGLOBE_VM_PRESETS_ROOT", str(IMAGES / "Presets"))) |
| 21 | 21 | UPLOADS = Path(os.environ.get("SNOWGLOBE_VM_UPLOADS_ROOT", str(IMAGES / "Install Disks"))) |
| 22 | 22 | transition = import_module("namespace-transition") |
| 23 | mac_vms = import_module("mac-vms") | |
| 23 | 24 | NS = "{https://paperclover.net/snowglobe}" |
| 24 | 25 | NAME = re.compile(r"[a-z0-9][a-z0-9-]{0,62}\Z") |
| 25 | 26 | GUEST_CHANNEL = json.loads((Path(__file__).resolve().parent.parent / "guest/protocol.json").read_text())["channel"] |
| ... | ... | @@ -76,6 +77,7 @@ ACTION_FIELDS = { |
| 76 | 77 | "remove": {"name", "disks"}, |
| 77 | 78 | "library": None, "media": {"name", "image"}, |
| 78 | 79 | "preset": {"name", "id", "os", "description"}, |
| 80 | "clipboard": {"name", "text"}, | |
| 79 | 81 | "console": {"name"}, "serial": {"name"}, "guest": {"name"}, "owner": {"name"}, "access": {"name"}, "upload": {"volume", "size"}, |
| 80 | 82 | } |
| 81 | 83 | |
| ... | ... | @@ -137,7 +139,7 @@ def checked_profile(name, platform, firmware, arch): |
| 137 | 139 | |
| 138 | 140 | def node(): |
| 139 | 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} | |
| 141 | 143 | |
| 142 | 144 | |
| 143 | 145 | def validate(action, payload): |
| ... | ... | @@ -168,8 +170,10 @@ def validate(action, payload): |
| 168 | 170 | for field in ("autostart", "start", "disks"): |
| 169 | 171 | if field in payload and not isinstance(payload[field], bool): |
| 170 | 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 | 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 | 177 | if action == "preset": |
| 174 | 178 | ensure_name(payload["id"]) |
| 175 | 179 | if not isinstance(payload["os"], str) or not 1 <= len(payload["os"]) <= 100: |
| ... | ... | @@ -193,6 +197,8 @@ def validate(action, payload): |
| 193 | 197 | for field, minimum, maximum in [("vcpus", 1, host["cpus"]), ("memory", 2**29, host["memory"]), ("disk", 2**30, 2**63 - 1)]: |
| 194 | 198 | if type(payload[field]) is not int or not minimum <= payload[field] <= maximum: |
| 195 | 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 | 202 | if DISKS.is_symlink() or (DISKS / payload["name"]).is_symlink(): |
| 197 | 203 | raise ValueError("The VM disk directory is a symbolic link. Remove the link before continuing.") |
| 198 | 204 | |
| ... | ... | @@ -291,6 +297,10 @@ def domains(): |
| 291 | 297 | setup = root.findtext(f"metadata/{NS}vm/{NS}setup") |
| 292 | 298 | result.append({ |
| 293 | 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 | 304 | "owner": root.findtext(f"metadata/{NS}vm/{NS}owner"), |
| 295 | 305 | "description": root.findtext("description", ""), |
| 296 | 306 | "state": state, |
| ... | ... | @@ -937,7 +947,7 @@ def main(): |
| 937 | 947 | if action == "node": |
| 938 | 948 | return node() |
| 939 | 949 | if action == "domains": |
| 940 | return domains() | |
| 950 | return domains() + mac_vms.domain() | |
| 941 | 951 | if action == "stats": |
| 942 | 952 | result = {} |
| 943 | 953 | for name in virsh("list", "--name").splitlines(): |
| ... | ... | @@ -952,6 +962,8 @@ def main(): |
| 952 | 962 | return result |
| 953 | 963 | if action == "library": |
| 954 | 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 | 967 | if action == "console": |
| 956 | 968 | return console_target(payload["name"]) |
| 957 | 969 | if action == "guest": |
| ... | ... | @@ -993,7 +1005,7 @@ def main(): |
| 993 | 1005 | root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive"))) |
| 994 | 1006 | if root.find("./devices/interface/source[@network='default']") is not None: |
| 995 | 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 | 1009 | elif action == "update": |
| 998 | 1010 | name = payload["name"] |
| 999 | 1011 | ensure_name(name) |