From c577f3c96f77fe9c787de1ed355f45ec2dd7f945 Mon Sep 17 00:00:00 2001 From: Claw Date: Tue, 17 Nov 2026 20:37:21 +0100 Subject: [PATCH] 0.4.0: ISO install (attach CD devices incl. virtio-win driver ISO), eject, virtio-win download preset --- bin/vmm-lib.php | 117 +++++++++++++++++++++++++++++++++++++ gui/ext/vmmngr/vmmngr.conf | 2 +- gui/vmmngr_images.php | 19 ++++++ gui/vmmngr_vm.php | 94 +++++++++++++++++++++++++++++ version.txt | 2 +- 5 files changed, 232 insertions(+), 2 deletions(-) diff --git a/bin/vmm-lib.php b/bin/vmm-lib.php index 8678da8..7fdd587 100644 --- a/bin/vmm-lib.php +++ b/bin/vmm-lib.php @@ -826,6 +826,123 @@ function vmm_delete_data_file(string $dir, string $name): array : ['ok' => false, 'error' => 'cannot delete']; } +function vmm_vm_conf_path(string $name): string +{ + $dir = vmm_data_dir(''); + $dir = rtrim($dir, '/'); + if ($dir === '' || !vmm_valid_name($name)) { + return ''; + } + return $dir . '/' . $name . '/' . $name . '.conf'; +} + +function vmm_attached_isos(string $name): array +{ + $conf = vmm_vm_conf_path($name); + if ($conf === '' || !is_file($conf)) { + return []; + } + $out = []; + $cur = null; + foreach (preg_split('/\R/', (string)file_get_contents($conf)) ?: [] as $line) { + if (preg_match('/^disk(\d+)_type="(ahci-cd|ahci-hd)"/', trim($line), $m)) { + $cur = (int)$m[1]; + $out[$cur] = ['slot' => $cur, 'path' => '']; + } elseif ($cur !== null && preg_match('/^disk' . $cur . '_name="([^"]+)"/', trim($line), $m2)) { + $out[$cur]['path'] = $m2[1]; + } + } + return array_values(array_filter($out, static fn($d) => ($d['path'] ?? '') !== '')); +} + +/** + * Attach ISOs as read-only CD devices and drop any previously attached ones. + * Returns ['added' => [...], 'removed' => [...]] + */ +function vmm_iso_attach(string $name, array $isos): array +{ + $conf = vmm_vm_conf_path($name); + if ($conf === '' || !is_file($conf)) { + return ['error' => 'vm config not found']; + } + $isos = array_values(array_filter($isos, static fn($p) => $p !== '')); + foreach ($isos as $iso) { + if (!is_file($iso) || !str_ends_with(strtolower($iso), '.iso')) { + return ['error' => 'iso not found: ' . basename($iso)]; + } + } + + $lines = preg_split('/\R/', (string)file_get_contents($conf)) ?: []; + $kept = []; + $removed = []; + $skip = null; + foreach ($lines as $line) { + if (preg_match('/^disk(\d+)_type="ahci-cd"\s*$/', trim($line), $m)) { + $skip = (int)$m[1]; + $removed[] = 'disk' . $skip; + continue; + } + if ($skip !== null && preg_match('/^disk' . $skip . '_/', trim($line))) { + continue; + } + $skip = null; + $kept[] = $line; + } + + $max = -1; + foreach ($kept as $line) { + if (preg_match('/^disk(\d+)_/', trim($line), $m)) { + $max = max($max, (int)$m[1]); + } + } + $added = []; + foreach ($isos as $iso) { + $max++; + $kept[] = 'disk' . $max . '_type="ahci-cd"'; + $kept[] = 'disk' . $max . '_dev="custom"'; + $kept[] = 'disk' . $max . '_name="' . $iso . '"'; + $added[] = 'disk' . $max . ' = ' . basename($iso); + } + + if (@copy($conf, $conf . '.vmmngr.bak') === false) { + return ['error' => 'cannot back up vm config']; + } + if (@file_put_contents($conf, implode("\n", $kept) . "\n") === false) { + return ['error' => 'cannot write vm config']; + } + return ['added' => $added, 'removed' => $removed]; +} + +function vmm_iso_detach(string $name): array +{ + $conf = vmm_vm_conf_path($name); + if ($conf === '' || !is_file($conf)) { + return ['error' => 'vm config not found']; + } + $lines = preg_split('/\R/', (string)file_get_contents($conf)) ?: []; + $kept = []; + $removed = []; + $skip = null; + foreach ($lines as $line) { + if (preg_match('/^disk(\d+)_type="ahci-cd"\s*$/', trim($line), $m)) { + $skip = (int)$m[1]; + $removed[] = 'disk' . $skip; + continue; + } + if ($skip !== null && preg_match('/^disk' . $skip . '_/', trim($line))) { + continue; + } + $skip = null; + $kept[] = $line; + } + if ($removed === []) { + return ['removed' => []]; + } + @copy($conf, $conf . '.vmmngr.bak'); + @file_put_contents($conf, implode("\n", $kept) . "\n"); + return ['removed' => $removed]; +} + function vmm_valid_template_name(string $name): bool { return (bool)preg_match('/^[A-Za-z0-9][A-Za-z0-9 ._()+\-]{0,120}\.conf$/', $name); diff --git a/gui/ext/vmmngr/vmmngr.conf b/gui/ext/vmmngr/vmmngr.conf index d8f515a..31aac2d 100644 --- a/gui/ext/vmmngr/vmmngr.conf +++ b/gui/ext/vmmngr/vmmngr.conf @@ -1,6 +1,6 @@ { "appname": "VM Manager", - "version": "0.3.3", + "version": "0.4.0", "rootfolder": "/mnt/Daten/extensions/vmmngr", "vm_source": "auto", "bridges": ["ix0bridge"], diff --git a/gui/vmmngr_images.php b/gui/vmmngr_images.php index 29c8f5f..9b977cb 100644 --- a/gui/vmmngr_images.php +++ b/gui/vmmngr_images.php @@ -12,7 +12,19 @@ $post = strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? '')) === 'POST'; $act = $post ? (string)($_POST['act'] ?? '') : ''; $confirm = $post ? (string)($_POST['confirm'] ?? '') : ''; +$presets = [ + 'virtio' => ['kind' => 'iso', 'url' => 'https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso'], +]; + if ($post && $act !== '') { + if (str_starts_with($act, 'preset:')) { + $key = substr($act, 7); + if (isset($presets[$key])) { + $job = vmm_job_new($presets[$key]['kind'] . '_download', $key, [$presets[$key]['kind'], $presets[$key]['url']], 21600); + header('Location: vmmngr_jobs.php?id=' . urlencode((string)$job['id'])); + exit; + } + } if ($act === 'download') { $kind = (string)($_POST['kind'] ?? 'iso'); $url = trim((string)($_POST['url'] ?? '')); @@ -97,6 +109,13 @@ function vmmAskDelete(name) { + + + + + + + diff --git a/gui/vmmngr_vm.php b/gui/vmmngr_vm.php index 8d9f621..2d88f2f 100644 --- a/gui/vmmngr_vm.php +++ b/gui/vmmngr_vm.php @@ -17,6 +17,40 @@ if (strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? '')) === 'POST' && isset($ if ($vmName !== $name) { $vmName = $name; } + + // media: attach ISOs (install) or eject them + if ($verb === 'install' || $verb === 'eject') { + if ($verb === 'eject') { + $res = vmm_iso_detach($name); + $_SESSION['vmmngr_error'] = isset($res['error']) + ? (string)$res['error'] + : ($res['removed'] === [] ? gettext('No ISO attached.') : sprintf(gettext('Ejected: %s'), implode(', ', $res['removed']))); + header('Location: vmmngr_vm.php?name=' . urlencode($name)); + exit; + } + $isoDir = vmm_data_dir('.iso'); + $pick = []; + foreach (['iso', 'driver_iso'] as $field) { + $f = vmm_safe_basename((string)($_POST[$field] ?? '')); + if ($f !== '' && str_ends_with(strtolower($f), '.iso')) { + $pick[] = $isoDir . '/' . $f; + } + } + if ($pick === []) { + $_SESSION['vmmngr_error'] = gettext('Choose an ISO first.'); + header('Location: vmmngr_vm.php?name=' . urlencode($name)); + exit; + } + $res = vmm_iso_attach($name, $pick); + if (isset($res['error'])) { + $_SESSION['vmmngr_error'] = (string)$res['error']; + header('Location: vmmngr_vm.php?name=' . urlencode($name)); + exit; + } + $job = vmm_job_new('start', $name); + header('Location: vmmngr_jobs.php?id=' . urlencode((string)$job['id'])); + exit; + } $check = vmm_action_check($verb, $vmName, (string)($_POST['confirm'] ?? '')); if (!$check['ok']) { $_SESSION['vmmngr_error'] = $check['error']; @@ -93,6 +127,66 @@ vmm_nav('dashboard'); + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
disk →
+ + +
+ + +
+ + +
+ + + +
+
$blocks): ?> diff --git a/version.txt b/version.txt index 1c09c74..1d0ba9e 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.3.3 +0.4.0