0.11.0: VM configuration editor (cpu, memory, loader, graphics, switch, autostart)

This commit is contained in:
Claw committed 2026-11-17 21:27:19 +01:00
1 parent f4160054ab
commit 02cb6b1079
7 files changed
+201 -2

No files matched your search

+66
View File
@@ -1223,6 +1223,72 @@ function vmm_attached_isos(string $name): array
return array_values(array_filter($out, static fn($d) => ($d['path'] ?? '') !== ''));
}
/**
* Read a VM's config file into a key => value map (quoted values, comments kept
* out of the map).
*/
function vmm_conf_get(string $name): array
{
$conf = vmm_vm_conf_path($name);
if ($conf === '' || !is_file($conf)) {
return [];
}
$out = [];
foreach (preg_split('/\R/', (string)file_get_contents($conf)) ?: [] as $line) {
if (preg_match('/^([A-Za-z0-9_]+)="([^"]*)"\s*$/', trim($line), $m)) {
$out[$m[1]] = $m[2];
} elseif (preg_match('/^([A-Za-z0-9_]+)=([^"\s].*)\s*$/', trim($line), $m)) {
$out[$m[1]] = $m[2];
}
}
return $out;
}
/**
* Update or insert config keys, keeping every other line untouched.
* Returns ['ok'=>bool, 'changed'=>[...]].
*/
function vmm_conf_set(string $name, array $pairs): array
{
$conf = vmm_vm_conf_path($name);
if ($conf === '' || !is_file($conf)) {
return ['ok' => false, 'error' => 'vm config not found'];
}
$lines = preg_split('/\R/', (string)file_get_contents($conf)) ?: [];
$changed = [];
foreach ($pairs as $key => $value) {
if (!preg_match('/^[A-Za-z0-9_]+$/', (string)$key)) {
continue;
}
$value = (string)$value;
$isNumeric = preg_match('/^[0-9]+[A-Za-z]?$/', $value) === 1 && preg_match('/^(cpu|memory)$/', (string)$key) === 1;
$line = $isNumeric ? $key . '=' . $value : $key . '="' . $value . '"';
$found = false;
foreach ($lines as $i => $existing) {
if (preg_match('/^' . preg_quote((string)$key, '/') . '=/', trim($existing))) {
if (trim($existing) !== $line) {
$lines[$i] = $line;
$changed[] = (string)$key;
}
$found = true;
break;
}
}
if (!$found) {
$lines[] = $line;
$changed[] = (string)$key;
}
}
if ($changed === []) {
return ['ok' => true, 'changed' => []];
}
@copy($conf, $conf . '.vmmngr.bak');
if (@file_put_contents($conf, implode("\n", $lines) . "\n") === false) {
return ['ok' => false, 'error' => 'cannot write vm config'];
}
return ['ok' => true, 'changed' => $changed];
}
/**
* Attach ISOs as read-only CD devices and drop any previously attached ones.
* Returns ['added' => [...], 'removed' => [...]]