0.4.0: ISO install (attach CD devices incl. virtio-win driver ISO), eject, virtio-win download preset

This commit is contained in:
Claw committed 2026-11-17 20:37:21 +01:00
1 parent e989df97a5
commit c577f3c96f
5 files changed
+232 -2

No files matched your search

+117
View File
@@ -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);
+1 -1
View File
@@ -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"],
+19
View File
@@ -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) {
<span style="opacity:.75;margin-left:8px;"><?=gettext('Downloads run as a job in the background.');?></span>
</td>
</tr>
<tr>
<td class="celltag"><?=gettext('Presets');?></td>
<td class="celldata">
<button type="submit" class="formbtn" name="act" value="preset:virtio"><?=gettext('virtio-win drivers ISO');?></button>
<span style="opacity:.75;margin-left:8px;"><?=gettext('Official stable build - needed by Windows guests for disk/network drivers.');?></span>
</td>
</tr>
</tbody>
</table>
+94
View File
@@ -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');
</tbody>
</table>
<?php
$attached = vmm_attached_isos($name);
$isos = vmm_file_list(vmm_data_dir('.iso'), ['iso']);
?>
<table class="area_data_settings" style="width:100%;border-collapse:collapse;">
<colgroup><col class="area_data_settings_col_tag"><col class="area_data_settings_col_data"></colgroup>
<thead>
<?php html_separator2(); html_titleline2(gettext('Install / media'), 2); ?>
</thead>
<tbody>
<tr>
<td class="celltag"><?=gettext('Attached');?></td>
<td class="celldata vmm-mono">
<?php if ($attached === []): ?>
<?=gettext('none');?>
<?php else: ?>
<?php foreach ($attached as $a): ?>
<div>disk<?=(int)$a['slot'];?> → <?=vmm_h((string)$a['path']);?></div>
<?php endforeach; ?>
<?php endif; ?>
</td>
</tr>
<?php if (!$isRunning): ?>
<tr>
<td class="celltag"><?=gettext('Boot ISO');?></td>
<td class="celldata">
<select name="iso">
<option value=""><?=gettext('none');?></option>
<?php foreach ($isos as $i): ?>
<option value="<?=vmm_h((string)$i['name']);?>"><?=vmm_h((string)$i['name']);?></option>
<?php endforeach; ?>
</select>
<span style="opacity:.75;margin-left:8px;"><?=gettext('Windows: boot from the installation ISO, drivers from virtio-win below.');?></span>
</td>
</tr>
<tr>
<td class="celltag"><?=gettext('Driver ISO');?></td>
<td class="celldata">
<select name="driver_iso">
<option value=""><?=gettext('none');?></option>
<?php foreach ($isos as $i): ?>
<?php if (stripos((string)$i['name'], 'virtio') === false) { continue; } ?>
<option value="<?=vmm_h((string)$i['name']);?>"><?=vmm_h((string)$i['name']);?></option>
<?php endforeach; ?>
</select>
<span style="opacity:.75;margin-left:8px;"><?=gettext('Only virtio-* images are listed here.');?></span>
</td>
</tr>
<tr>
<td class="celltag"><?=gettext('Actions');?></td>
<td class="celldata">
<button type="submit" class="formbtn" name="act" value="install:<?=vmm_h($name);?>"><?=gettext('Attach and start');?></button>
<button type="submit" class="formbtn" name="act" value="eject:<?=vmm_h($name);?>" onclick="return confirm('Eject all attached ISOs?')"><?=gettext('Eject all');?></button>
<span style="opacity:.75;margin-left:8px;"><?=gettext('Attaching rewrites the VM config (backup as .vmmngr.bak).');?></span>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<div id="vmm-job" class="vmm-job"></div>
<?php foreach (($info['sections'] ?? []) as $type => $blocks): ?>
+1 -1
View File
@@ -1 +1 @@
0.3.3
0.4.0