utm-mcp 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +124 -2
- package/agentdist.json +68 -0
- package/bin/utm-mcp.mjs +30 -0
- package/package.json +49 -4
- package/skills/utm/SKILL.md +119 -0
- package/skills/utm/references/configuration.md +131 -0
- package/skills/utm/references/linux-guests.md +87 -0
- package/skills/utm/references/utmctl.md +51 -0
- package/skills/utm/references/windows-guests.md +66 -0
- package/src/applescript.mjs +247 -0
- package/src/exec.mjs +81 -0
- package/src/feedback.mjs +105 -0
- package/src/media.mjs +194 -0
- package/src/policy.mjs +94 -0
- package/src/server.mjs +460 -0
- package/src/utm.mjs +162 -0
package/src/media.mjs
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// Builds the small ISO images that make an operating system install itself: a cloud-init seed for
|
|
2
|
+
// Linux, and an answer file plus UTM's guest tools for Windows.
|
|
3
|
+
import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { homedir, tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { HDIUTIL, run } from "./exec.mjs";
|
|
7
|
+
import { MEDIA_DIR } from "./policy.mjs";
|
|
8
|
+
|
|
9
|
+
async function makeIso(sourceDir, isoPath, volumeName) {
|
|
10
|
+
rmSync(isoPath, { force: true });
|
|
11
|
+
const result = await run(HDIUTIL, ["makehybrid", "-quiet", "-iso", "-joliet", "-default-volume-name", volumeName, "-o", isoPath, sourceDir], { timeoutMs: 300_000 });
|
|
12
|
+
if (result.code !== 0) throw new Error(`Could not build ${isoPath}: ${result.stderr.trim()}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function mediaDir(vmName) {
|
|
16
|
+
const dir = join(MEDIA_DIR, vmName);
|
|
17
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
18
|
+
return dir;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function removeMedia(vmName) {
|
|
22
|
+
rmSync(join(MEDIA_DIR, vmName), { recursive: true, force: true });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A cloud-init NoCloud seed: a volume labelled "cidata" holding meta-data and user-data.
|
|
27
|
+
* UTM copies it into the VM, so the caller deletes the returned directory afterwards.
|
|
28
|
+
*/
|
|
29
|
+
export async function buildLinuxSeed({ name, user, sshPublicKey }) {
|
|
30
|
+
const work = mkdtempSync(join(tmpdir(), "utm-mcp-seed-"));
|
|
31
|
+
const seed = join(work, "seed");
|
|
32
|
+
mkdirSync(seed);
|
|
33
|
+
writeFileSync(join(seed, "meta-data"), `instance-id: ${name}-${Date.now()}\nlocal-hostname: ${name}\n`);
|
|
34
|
+
const userData = [
|
|
35
|
+
"#cloud-config",
|
|
36
|
+
"users:",
|
|
37
|
+
` - name: ${user}`,
|
|
38
|
+
" shell: /bin/bash",
|
|
39
|
+
" sudo: ALL=(ALL) NOPASSWD:ALL",
|
|
40
|
+
...(sshPublicKey ? [" ssh_authorized_keys:", ` - ${sshPublicKey}`] : []),
|
|
41
|
+
"package_update: true",
|
|
42
|
+
"packages:",
|
|
43
|
+
" - qemu-guest-agent",
|
|
44
|
+
"runcmd:",
|
|
45
|
+
" - systemctl enable --now qemu-guest-agent",
|
|
46
|
+
"",
|
|
47
|
+
];
|
|
48
|
+
writeFileSync(join(seed, "user-data"), userData.join("\n"));
|
|
49
|
+
const iso = join(work, "seed.iso");
|
|
50
|
+
await makeIso(seed, iso, "cidata");
|
|
51
|
+
return { iso, cleanup: () => rmSync(work, { recursive: true, force: true }) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const GUEST_TOOLS_DIR = join(homedir(), "Library/Containers/com.utmapp.UTM/Data/Library/Application Support/GuestSupportTools");
|
|
55
|
+
|
|
56
|
+
function findGuestToolsIso() {
|
|
57
|
+
if (!existsSync(GUEST_TOOLS_DIR)) return null;
|
|
58
|
+
const iso = readdirSync(GUEST_TOOLS_DIR).filter((file) => /^utm-guest-tools.*\.iso$/.test(file)).sort().pop();
|
|
59
|
+
return iso ? join(GUEST_TOOLS_DIR, iso) : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const xml = (value) => String(value).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
63
|
+
|
|
64
|
+
const component = (name, body) =>
|
|
65
|
+
` <component name="${name}" processorArchitecture="arm64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">\n${body}\n </component>`;
|
|
66
|
+
|
|
67
|
+
const command = (tag, order, line) =>
|
|
68
|
+
` <${tag} wcm:action="add"><Order>${order}</Order><${tag === "SynchronousCommand" ? "CommandLine" : "Path"}>${xml(line)}</${tag === "SynchronousCommand" ? "CommandLine" : "Path"}></${tag}>`;
|
|
69
|
+
|
|
70
|
+
/** An answer file that takes Windows 11 on ARM from the installer to a logged-in desktop with no questions. */
|
|
71
|
+
export function windowsAnswerFile({ computerName, username, password, edition, productKey }) {
|
|
72
|
+
const locale = "<InputLocale>en-US</InputLocale><SystemLocale>en-US</SystemLocale><UILanguage>en-US</UILanguage><UserLocale>en-US</UserLocale>";
|
|
73
|
+
const bypass = ["BypassCPUCheck", "BypassRAMCheck", "BypassSecureBootCheck", "BypassTPMCheck"].map((check, index) =>
|
|
74
|
+
command("RunSynchronousCommand", index + 1, `reg add HKLM\\System\\Setup\\LabConfig /v ${check} /t REG_DWORD /d 1 /f`),
|
|
75
|
+
);
|
|
76
|
+
// The support disc's drive letter is not known in advance, so look for it.
|
|
77
|
+
const tools = 'cmd /c "for %d in (D E F G H I J K) do if exist %d:\\utm-mcp\\utm-guest-tools.exe start /wait %d:\\utm-mcp\\utm-guest-tools.exe /S"';
|
|
78
|
+
return `<?xml version="1.0" encoding="utf-8"?>
|
|
79
|
+
<unattend xmlns="urn:schemas-microsoft-com:unattend" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State">
|
|
80
|
+
<settings pass="windowsPE">
|
|
81
|
+
${component("Microsoft-Windows-International-Core-WinPE", ` <SetupUILanguage><UILanguage>en-US</UILanguage></SetupUILanguage>${locale}`)}
|
|
82
|
+
${component(
|
|
83
|
+
"Microsoft-Windows-Setup",
|
|
84
|
+
` <RunSynchronous>
|
|
85
|
+
${bypass.join("\n")}
|
|
86
|
+
</RunSynchronous>
|
|
87
|
+
<DiskConfiguration>
|
|
88
|
+
<Disk wcm:action="add">
|
|
89
|
+
<DiskID>0</DiskID>
|
|
90
|
+
<WillWipeDisk>true</WillWipeDisk>
|
|
91
|
+
<CreatePartitions>
|
|
92
|
+
<CreatePartition wcm:action="add"><Order>1</Order><Type>EFI</Type><Size>300</Size></CreatePartition>
|
|
93
|
+
<CreatePartition wcm:action="add"><Order>2</Order><Type>MSR</Type><Size>16</Size></CreatePartition>
|
|
94
|
+
<CreatePartition wcm:action="add"><Order>3</Order><Type>Primary</Type><Extend>true</Extend></CreatePartition>
|
|
95
|
+
</CreatePartitions>
|
|
96
|
+
<ModifyPartitions>
|
|
97
|
+
<ModifyPartition wcm:action="add"><Order>1</Order><PartitionID>1</PartitionID><Format>FAT32</Format><Label>System</Label></ModifyPartition>
|
|
98
|
+
<ModifyPartition wcm:action="add"><Order>2</Order><PartitionID>3</PartitionID><Format>NTFS</Format><Label>Windows</Label><Letter>C</Letter></ModifyPartition>
|
|
99
|
+
</ModifyPartitions>
|
|
100
|
+
</Disk>
|
|
101
|
+
</DiskConfiguration>
|
|
102
|
+
<ImageInstall>
|
|
103
|
+
<OSImage>
|
|
104
|
+
<InstallTo><DiskID>0</DiskID><PartitionID>3</PartitionID></InstallTo>
|
|
105
|
+
<InstallFrom><MetaData wcm:action="add"><Key>/IMAGE/NAME</Key><Value>${xml(edition)}</Value></MetaData></InstallFrom>
|
|
106
|
+
</OSImage>
|
|
107
|
+
</ImageInstall>
|
|
108
|
+
<UserData>
|
|
109
|
+
<AcceptEula>true</AcceptEula>
|
|
110
|
+
<ProductKey><Key>${xml(productKey)}</Key><WillShowUI>OnError</WillShowUI></ProductKey>
|
|
111
|
+
</UserData>`,
|
|
112
|
+
)}
|
|
113
|
+
</settings>
|
|
114
|
+
<settings pass="specialize">
|
|
115
|
+
${component("Microsoft-Windows-Shell-Setup", ` <ComputerName>${xml(computerName)}</ComputerName><TimeZone>UTC</TimeZone>`)}
|
|
116
|
+
${component(
|
|
117
|
+
"Microsoft-Windows-Deployment",
|
|
118
|
+
` <RunSynchronous>
|
|
119
|
+
${command("RunSynchronousCommand", 1, "reg add HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\OOBE /v BypassNRO /t REG_DWORD /d 1 /f")}
|
|
120
|
+
</RunSynchronous>`,
|
|
121
|
+
)}
|
|
122
|
+
</settings>
|
|
123
|
+
<settings pass="oobeSystem">
|
|
124
|
+
${component("Microsoft-Windows-International-Core", ` ${locale}`)}
|
|
125
|
+
${component(
|
|
126
|
+
"Microsoft-Windows-Shell-Setup",
|
|
127
|
+
` <OOBE>
|
|
128
|
+
<HideEULAPage>true</HideEULAPage>
|
|
129
|
+
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
|
|
130
|
+
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
|
|
131
|
+
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
|
|
132
|
+
<HideLocalAccountScreen>true</HideLocalAccountScreen>
|
|
133
|
+
<ProtectYourPC>3</ProtectYourPC>
|
|
134
|
+
</OOBE>
|
|
135
|
+
<UserAccounts>
|
|
136
|
+
<LocalAccounts>
|
|
137
|
+
<LocalAccount wcm:action="add">
|
|
138
|
+
<Name>${xml(username)}</Name>
|
|
139
|
+
<Group>Administrators</Group>
|
|
140
|
+
<Password><Value>${xml(password)}</Value><PlainText>true</PlainText></Password>
|
|
141
|
+
</LocalAccount>
|
|
142
|
+
</LocalAccounts>
|
|
143
|
+
</UserAccounts>
|
|
144
|
+
<AutoLogon>
|
|
145
|
+
<Enabled>true</Enabled>
|
|
146
|
+
<Username>${xml(username)}</Username>
|
|
147
|
+
<Password><Value>${xml(password)}</Value><PlainText>true</PlainText></Password>
|
|
148
|
+
<LogonCount>3</LogonCount>
|
|
149
|
+
</AutoLogon>
|
|
150
|
+
<FirstLogonCommands>
|
|
151
|
+
${command("SynchronousCommand", 1, tools)}
|
|
152
|
+
${command("SynchronousCommand", 2, 'cmd /c "echo done > C:\\utm-mcp-setup-complete.txt"')}
|
|
153
|
+
</FirstLogonCommands>`,
|
|
154
|
+
)}
|
|
155
|
+
</settings>
|
|
156
|
+
</unattend>
|
|
157
|
+
`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The disc Windows Setup reads its answers from, with UTM's guest tools (drivers and the QEMU guest
|
|
162
|
+
* agent) beside them. It stays attached through the install, so it lives in the broker's media
|
|
163
|
+
* directory rather than a temporary one. It contains the account password in plain text until
|
|
164
|
+
* detach_media removes it.
|
|
165
|
+
*/
|
|
166
|
+
export async function buildWindowsSupport(vmName, answers) {
|
|
167
|
+
const toolsIso = findGuestToolsIso();
|
|
168
|
+
if (!toolsIso) {
|
|
169
|
+
throw new Error(
|
|
170
|
+
"UTM's Windows guest tools have not been downloaded on this Mac. In UTM, open any Windows VM's drive menu and choose " +
|
|
171
|
+
'"Install Windows Guest Tools…" once, then try again.',
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
const dir = mediaDir(vmName);
|
|
175
|
+
const stage = join(dir, "stage");
|
|
176
|
+
rmSync(stage, { recursive: true, force: true });
|
|
177
|
+
mkdirSync(join(stage, "utm-mcp"), { recursive: true });
|
|
178
|
+
writeFileSync(join(stage, "autounattend.xml"), windowsAnswerFile(answers));
|
|
179
|
+
|
|
180
|
+
const mount = mkdtempSync(join(tmpdir(), "utm-mcp-tools-"));
|
|
181
|
+
const attached = await run(HDIUTIL, ["attach", "-readonly", "-nobrowse", "-mountpoint", mount, toolsIso], { timeoutMs: 60_000 });
|
|
182
|
+
if (attached.code !== 0) throw new Error(`Could not open UTM's guest tools image: ${attached.stderr.trim()}`);
|
|
183
|
+
try {
|
|
184
|
+
cpSync(join(mount, "utm-guest-tools.exe"), join(stage, "utm-mcp", "utm-guest-tools.exe"));
|
|
185
|
+
} finally {
|
|
186
|
+
await run(HDIUTIL, ["detach", mount, "-force"], { timeoutMs: 60_000 });
|
|
187
|
+
rmSync(mount, { recursive: true, force: true });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const iso = join(dir, "support.iso");
|
|
191
|
+
await makeIso(stage, iso, "UTMMCP");
|
|
192
|
+
rmSync(stage, { recursive: true, force: true });
|
|
193
|
+
return iso;
|
|
194
|
+
}
|
package/src/policy.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// What the broker will and will not do, decided by whoever configures the server, not by the agent
|
|
2
|
+
// calling it. Read once from the environment at startup.
|
|
3
|
+
import { appendFileSync, mkdirSync, realpathSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path";
|
|
6
|
+
|
|
7
|
+
const expand = (path) => (path.startsWith("~/") ? join(homedir(), path.slice(2)) : path);
|
|
8
|
+
const list = (value, separator) => (value ?? "").split(separator).map((item) => item.trim()).filter(Boolean);
|
|
9
|
+
|
|
10
|
+
/** The broker's own directory: generated install media, the exchange directory, the audit log. */
|
|
11
|
+
export const HOME = resolve(expand(process.env.UTM_MCP_HOME ?? "~/.utm-mcp"));
|
|
12
|
+
export const MEDIA_DIR = join(HOME, "media");
|
|
13
|
+
export const FILES_DIR = join(HOME, "files");
|
|
14
|
+
const AUDIT_LOG = join(HOME, "audit.log");
|
|
15
|
+
|
|
16
|
+
mkdirSync(MEDIA_DIR, { recursive: true, mode: 0o700 });
|
|
17
|
+
mkdirSync(FILES_DIR, { recursive: true, mode: 0o700 });
|
|
18
|
+
|
|
19
|
+
const real = (path) => {
|
|
20
|
+
try {
|
|
21
|
+
return realpathSync(path);
|
|
22
|
+
} catch {
|
|
23
|
+
return resolve(path);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** Host directories the agent may name: images and ISOs to install from, files to push or pull, folders to share. */
|
|
28
|
+
export const HOST_DIRS = [FILES_DIR, ...list(process.env.UTM_MCP_HOST_DIRS, ":").map((dir) => resolve(expand(dir)))].map(real);
|
|
29
|
+
|
|
30
|
+
/** VMs the broker did not create but has been told it may operate, by name or UUID. */
|
|
31
|
+
export const ALLOWED_VMS = list(process.env.UTM_MCP_ALLOW_VMS, ",");
|
|
32
|
+
|
|
33
|
+
export function describePolicy() {
|
|
34
|
+
return {
|
|
35
|
+
managed_vms: "VMs this server created (marked in their UTM notes)" + (ALLOWED_VMS.length ? `, plus: ${ALLOWED_VMS.join(", ")}` : ""),
|
|
36
|
+
host_directories: HOST_DIRS,
|
|
37
|
+
exchange_directory: FILES_DIR,
|
|
38
|
+
audit_log: AUDIT_LOG,
|
|
39
|
+
configured_by: "UTM_MCP_HOST_DIRS (colon-separated directories) and UTM_MCP_ALLOW_VMS (comma-separated names or UUIDs) in the server's environment",
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Resolve a host path the agent supplied and require it to be inside an allowed directory.
|
|
45
|
+
* Symlinks are resolved first, so a link inside an allowed directory cannot point out of it.
|
|
46
|
+
* A file that does not exist yet is judged by its parent directory.
|
|
47
|
+
*/
|
|
48
|
+
export function hostPath(path, { mustExist = true } = {}) {
|
|
49
|
+
if (typeof path !== "string" || !isAbsolute(expand(path))) throw new Error("Host paths must be absolute.");
|
|
50
|
+
const wanted = resolve(expand(path));
|
|
51
|
+
let resolved;
|
|
52
|
+
try {
|
|
53
|
+
resolved = realpathSync(wanted);
|
|
54
|
+
} catch {
|
|
55
|
+
if (mustExist) throw new Error(`No such file or directory on the host: ${wanted}`);
|
|
56
|
+
try {
|
|
57
|
+
resolved = join(realpathSync(dirname(wanted)), basename(wanted));
|
|
58
|
+
} catch {
|
|
59
|
+
throw new Error(`The directory for ${wanted} does not exist on the host.`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (!HOST_DIRS.some((dir) => resolved === dir || resolved.startsWith(dir + sep))) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`${wanted} is outside the directories this server may use (${HOST_DIRS.join(", ")}). ` +
|
|
65
|
+
"The person running the server can add one with UTM_MCP_HOST_DIRS.",
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return resolved;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function isManaged(vm) {
|
|
72
|
+
return vm.managed || ALLOWED_VMS.includes(vm.name) || ALLOWED_VMS.some((ref) => ref.toUpperCase() === vm.id);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function requireManaged(vm) {
|
|
76
|
+
if (!isManaged(vm)) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`"${vm.name}" was not created by this server, so it is read-only here. ` +
|
|
79
|
+
"The person running the server can allow it with UTM_MCP_ALLOW_VMS.",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const SECRET_KEYS = new Set(["password", "stdin", "product_key"]);
|
|
85
|
+
|
|
86
|
+
/** One line per tool call. Secrets and bulk input are left out. */
|
|
87
|
+
export function audit(tool, args, outcome) {
|
|
88
|
+
const safe = Object.fromEntries(Object.entries(args ?? {}).map(([key, value]) => [key, SECRET_KEYS.has(key) ? "[redacted]" : value]));
|
|
89
|
+
try {
|
|
90
|
+
appendFileSync(AUDIT_LOG, `${JSON.stringify({ time: new Date().toISOString(), tool, args: safe, ...outcome })}\n`, { mode: 0o600 });
|
|
91
|
+
} catch {
|
|
92
|
+
// The log is a record, not a gate: a full disk should not stop a VM from being shut down.
|
|
93
|
+
}
|
|
94
|
+
}
|