diff --git a/gui/ext/vmmngr/vmmngr.conf b/gui/ext/vmmngr/vmmngr.conf index 4e5011b..9d73304 100644 --- a/gui/ext/vmmngr/vmmngr.conf +++ b/gui/ext/vmmngr/vmmngr.conf @@ -1,6 +1,6 @@ { "appname": "VM Manager", - "version": "0.9.0", + "version": "0.10.0", "rootfolder": "/mnt/Daten/extensions/vmmngr", "vm_source": "auto", "bridges": ["ix0bridge"], diff --git a/gui/vmmngr-boot.inc b/gui/vmmngr-boot.inc index 35a97d0..cfde93d 100644 --- a/gui/vmmngr-boot.inc +++ b/gui/vmmngr-boot.inc @@ -59,6 +59,7 @@ if (!function_exists('vmm_nav')) { 'switches' => ['vmmngr_switches.php', gettext('Switches')], 'jobs' => ['vmmngr_jobs.php', gettext('Jobs')], 'config' => ['vmmngr_config.php', gettext('Configuration')], + 'setup' => ['vmmngr_setup.php', gettext('Setup')], ]; $label = $crumb !== '' ? $crumb : ($items[$current][1] ?? gettext('VM Manager')); ?> diff --git a/gui/vmmngr-config-lib.inc b/gui/vmmngr-config-lib.inc index 5c2e590..bcc9eff 100644 --- a/gui/vmmngr-config-lib.inc +++ b/gui/vmmngr-config-lib.inc @@ -101,6 +101,52 @@ if (!function_exists('vmm_register_boot')) { return $added; } + /** + * Set a plain rc.conf variable through config.xml (survives reboots) and + * apply it to the running system via sysrc. + */ + function vmm_set_rc_param(string $name, string $value): bool + { + global $config; + if (!isset($config['rc']['param']) || !is_array($config['rc']['param'])) { + $config['rc'] = $config['rc'] ?? []; + $config['rc']['param'] = []; + } + $found = false; + foreach ($config['rc']['param'] as $i => $p) { + if ((string)($p['name'] ?? '') === $name) { + $config['rc']['param'][$i]['value'] = $value; + $config['rc']['param'][$i]['enable'] = true; + $found = true; + break; + } + } + if (!$found) { + $config['rc']['param'][] = [ + 'uuid' => uuid(), + 'enable' => true, + 'name' => $name, + 'value' => $value, + 'comment' => '', + ]; + } + write_config(); + // apply now, the file is only regenerated at boot + @exec('/usr/sbin/sysrc ' . escapeshellarg($name . '=' . $value) . ' >/dev/null 2>&1'); + return true; + } + + function vmm_rc_param(string $name): string + { + global $config; + foreach (($config['rc']['param'] ?? []) as $p) { + if ((string)($p['name'] ?? '') === $name) { + return (string)($p['value'] ?? ''); + } + } + return ''; + } + function vmm_boot_state(): array { global $config; diff --git a/gui/vmmngr.php b/gui/vmmngr.php index 8fe7c8d..8e7702d 100644 --- a/gui/vmmngr.php +++ b/gui/vmmngr.php @@ -33,6 +33,8 @@ $vms = $vmprov['available'] ? vmm_list() : []; $ver = $vmprov['available'] ? vmm_vm_version() : ['version' => '', 'rev' => '']; $vm_dir = $vmprov['available'] ? vmm_vm_dir() : ''; $budget = vmm_ram_budget(); +$switchList = $vmprov['available'] ? vmm_switches() : []; +$needsSetup = ($vm_dir === '' || $switchList === []); $running = array_values(array_filter($vms, static fn(array $v): bool => ($v['state'] ?? '') === 'running')); $mem_used = 0; @@ -59,6 +61,22 @@ vmm_nav('dashboard'); + + ' . gettext('Open setup') . ''); + ?> + + diff --git a/gui/vmmngr_setup.php b/gui/vmmngr_setup.php new file mode 100644 index 0000000..d4e45d9 --- /dev/null +++ b/gui/vmmngr_setup.php @@ -0,0 +1,260 @@ + config.xml + vm init + * Step 3: first switch on an existing host bridge + */ + +require_once 'vmmngr-boot.inc'; +require_once 'vmmngr-config-lib.inc'; + +$vmDir = vmm_rc_param('vm_dir'); +if ($vmDir === '') { + $out = trim((string)@shell_exec('/usr/bin/grep -E "^vm_dir=" /etc/rc.conf 2>/dev/null')); + if (preg_match('/vm_dir="?([^"\n]+)"?/', $out, $m)) { + $vmDir = $m[1]; + } +} +$switchList = vmm_switches(); +$hostBridges = array_values(array_filter(array_map('trim', explode("\n", trim((string)@shell_exec('/sbin/ifconfig -g bridge 2>/dev/null')))))); + +$post = strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? '')) === 'POST'; +$act = $post ? (string)($_POST['act'] ?? '') : ''; + +if ($post && $act === 'save_datastore') { + $spec = trim((string)($_POST['datastore'] ?? '')); + $newDs = trim((string)($_POST['new_dataset'] ?? '')); + $create = isset($_POST['create_dataset']); + + if ($newDs !== '' && $create) { + if (!preg_match('#^[A-Za-z0-9][A-Za-z0-9._-]*/[A-Za-z0-9][A-Za-z0-9._-]*$#', $newDs)) { + $_SESSION['vmmngr_error'] = gettext('Dataset name must look like pool/name.'); + header('Location: vmmngr_setup.php?step=2'); + exit; + } + exec('/sbin/zfs list -H -o name ' . escapeshellarg($newDs) . ' 2>/dev/null', $o, $rc); + if ($rc !== 0) { + exec('/sbin/zfs create ' . escapeshellarg($newDs) . ' 2>&1', $out, $crc); + if ($crc !== 0) { + $_SESSION['vmmngr_error'] = 'zfs create failed: ' . implode(' ', $out); + header('Location: vmmngr_setup.php?step=2'); + exit; + } + } + $spec = 'zfs:' . $newDs; + } + + if (!preg_match('#^zfs:([A-Za-z0-9._:/-]+)$#', $spec, $m)) { + $_SESSION['vmmngr_error'] = gettext('Choose a ZFS dataset.'); + header('Location: vmmngr_setup.php?step=2'); + exit; + } + $ds = $m[1]; + exec('/sbin/zfs list -H -o mounted ' . escapeshellarg($ds) . ' 2>/dev/null', $o2, $rc2); + if ($rc2 !== 0 || trim((string)($o2[0] ?? '')) !== 'yes') { + $_SESSION['vmmngr_error'] = gettext('Dataset is not mounted - cannot use it as vm_dir.'); + header('Location: vmmngr_setup.php?step=2'); + exit; + } + + vmm_set_rc_param('vm_dir', 'zfs:' . $ds); + vmm_set_rc_param('vm_enable', 'YES'); + $res = vmm_run(['init'], 60); + $_SESSION['vmmngr_error'] = $res['rc'] === 0 + ? sprintf(gettext('Default datastore set to zfs:%s and initialised.'), $ds) + : trim($res['stderr'] ?: $res['stdout'] ?: 'vm init failed'); + header('Location: vmmngr_setup.php?step=3'); + exit; +} + +if ($post && $act === 'add_switch') { + $name = trim((string)($_POST['sw_name'] ?? '')); + $bridge = trim((string)($_POST['bridge'] ?? '')); + if (!preg_match('/^[A-Za-z0-9][A-Za-z0-9_-]{0,31}$/', $name)) { + $_SESSION['vmmngr_error'] = gettext('Invalid switch name.'); + } elseif (!in_array($bridge, $hostBridges, true)) { + $_SESSION['vmmngr_error'] = gettext('Choose an existing host bridge.'); + } else { + $res = vmm_run(['switch', 'create', '-t', 'manual', '-b', $bridge, $name], 30); + $_SESSION['vmmngr_error'] = $res['rc'] === 0 + ? sprintf(gettext('Switch %s created on %s.'), $name, $bridge) + : trim($res['stderr'] ?: $res['stdout'] ?: 'switch create failed'); + if ($res['rc'] === 0) { + header('Location: vmmngr_setup.php?step=4'); + exit; + } + } + header('Location: vmmngr_setup.php?step=3'); + exit; +} + +$step = (int)($_GET['step'] ?? 0); +if ($step < 1 || $step > 4) { + $step = $vmDir === '' ? 2 : ($switchList === [] ? 3 : 4); +} + +// dataset inventory for the picker +$datasets = []; +exec('/sbin/zfs list -H -o name,mountpoint -t filesystem 2>/dev/null', $zlines); +foreach ($zlines as $line) { + $parts = preg_split('/\t+/', trim($line)); + if (count($parts) < 2) { + continue; + } + [$dsName, $mp] = $parts; + if ($mp === '/' || $mp === 'none' || $mp === '-' || !str_starts_with($mp, '/mnt')) { + continue; + } + if (substr_count($dsName, '/') > 1) { + continue; + } + $datasets[$dsName] = $mp; +} + +$pgtitle = [gettext('Extensions'), gettext('VM Manager'), gettext('Setup')]; +include 'fbegin.inc'; +vmm_css(); +vmm_nav('setup'); +?> +
+ diff --git a/version.txt b/version.txt index ac39a10..78bc1ab 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.9.0 +0.10.0