diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d6a97b..b756433 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to the VM Manager extension. +## 0.11.0 +- VM detail: configuration editor for vCPU, memory, loader, VNC, network switch and + autostart (stopped guests only); writes `.conf` with a `.vmmngr.bak` backup and + keeps `vm_list` in sync. + ## 0.10.2 - Create wizard: driver ISO (virtio-win) as second CD device, separate download row, hint when no virtio image is present. diff --git a/README.md b/README.md index 868eaa8..c713f58 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Status: **0.10.1** – feature-complete for daily use on a single node. |---|---| | Dashboard | all guests with state, loader, vCPU, RAM, datastore, autostart; start/stop/restart inline | | VM actions | start, stop, restart, reset, power-off, destroy (typed name required, ZFS holds released first) | +| VM settings | edit vCPU, memory, loader, graphics/VNC, network switch and autostart of a stopped VM | | Create wizard | template, datastore, vCPU/RAM/disk, switch, optional boot ISO, optional virtio-win driver ISO, optional start, optional cloud-init image | | Images | ISO/IMG inventory, HTTP(S) downloads as jobs, delete with typed confirmation, virtio-win preset | | Templates | list, edit, duplicate, delete, create from skeleton | diff --git a/bin/vmm-lib.php b/bin/vmm-lib.php index de33308..29338de 100644 --- a/bin/vmm-lib.php +++ b/bin/vmm-lib.php @@ -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' => [...]] diff --git a/gui/ext/vmmngr/vmmngr.conf b/gui/ext/vmmngr/vmmngr.conf index 82d960b..68f7721 100644 --- a/gui/ext/vmmngr/vmmngr.conf +++ b/gui/ext/vmmngr/vmmngr.conf @@ -1,6 +1,6 @@ { "appname": "VM Manager", - "version": "0.10.2", + "version": "0.11.0", "rootfolder": "/mnt/Daten/extensions/vmmngr", "vm_source": "auto", "bridges": ["ix0bridge"], diff --git a/gui/vmmngr-config-lib.inc b/gui/vmmngr-config-lib.inc index bcc9eff..b9b7060 100644 --- a/gui/vmmngr-config-lib.inc +++ b/gui/vmmngr-config-lib.inc @@ -147,6 +147,26 @@ if (!function_exists('vmm_register_boot')) { return ''; } + function vmm_autostart_names(): array + { + $list = trim(vmm_rc_param('vm_list')); + if ($list === '') { + return []; + } + return preg_split('/\s+/', $list) ?: []; + } + + function vmm_autostart_set(string $name, bool $on): bool + { + $names = vmm_autostart_names(); + $names = array_values(array_filter($names, static fn($n) => $n !== $name)); + if ($on) { + $names[] = $name; + } + vmm_set_rc_param('vm_list', implode(' ', $names)); + return true; + } + function vmm_boot_state(): array { global $config; diff --git a/gui/vmmngr_vm.php b/gui/vmmngr_vm.php index a0dd549..235d4d9 100644 --- a/gui/vmmngr_vm.php +++ b/gui/vmmngr_vm.php @@ -4,6 +4,7 @@ */ require_once 'vmmngr-boot.inc'; +require_once 'vmmngr-config-lib.inc'; $name = (string)($_GET['name'] ?? ''); // POSTs from this page carry the VM name in the act field; accept that too so @@ -24,6 +25,48 @@ if (strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? '')) === 'POST' && isset($ $vmName = $name; } + if ($verb === 'save_config') { + if (vmm_vm_state($name) === 'running') { + $_SESSION['vmmngr_error'] = gettext('Stop the VM before changing CPU, memory or loader.'); + header('Location: vmmngr_vm.php?name=' . urlencode($name)); + exit; + } + $cpu = trim((string)($_POST['cpu'] ?? '')); + $mem = strtoupper(trim((string)($_POST['memory'] ?? ''))); + $loader = (string)($_POST['loader'] ?? ''); + $graphics = (string)($_POST['graphics'] ?? 'no'); + $switch = trim((string)($_POST['switch'] ?? '')); + + if (!preg_match('/^[0-9]{1,2}$/', $cpu) || (int)$cpu < 1 || (int)$cpu > 64) { + $_SESSION['vmmngr_error'] = gettext('vCPU must be between 1 and 64.'); + } elseif (!preg_match('/^[0-9]{1,5}[MG]$/', $mem)) { + $_SESSION['vmmngr_error'] = gettext('Memory must look like 2G or 512M.'); + } elseif (!in_array($loader, ['uefi', 'uefi-csm', 'grub', 'bhyve'], true)) { + $_SESSION['vmmngr_error'] = gettext('Unknown loader.'); + } else { + $pairs = [ + 'cpu' => $cpu, + 'memory' => $mem, + 'loader' => $loader, + 'graphics' => $graphics === 'yes' ? 'yes' : 'no', + ]; + if ($switch !== '') { + $pairs['network0_switch'] = $switch; + } + $res = vmm_conf_set($name, $pairs); + if (!$res['ok']) { + $_SESSION['vmmngr_error'] = (string)$res['error']; + } else { + vmm_autostart_set($name, isset($_POST['autostart'])); + $_SESSION['vmmngr_error'] = $res['changed'] === [] + ? gettext('No changes.') + : sprintf(gettext('Updated: %s'), implode(', ', $res['changed'])); + } + } + header('Location: vmmngr_vm.php?name=' . urlencode($name)); + exit; + } + if ($verb === 'novnc_start' || $verb === 'novnc_stop') { if ($verb === 'novnc_stop') { vmm_novnc_stop($name); @@ -175,6 +218,70 @@ vmm_nav('', $name); + (string)($x['name'] ?? ''), vmm_switches()); + ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
/>
/>
+ +
+ +
+ +
+ +
  + + + + +
+