diff --git a/bin/vmm-jobd b/bin/vmm-jobd index 88b39af..221f3c0 100755 --- a/bin/vmm-jobd +++ b/bin/vmm-jobd @@ -173,6 +173,23 @@ fclose($pipes[1]); fclose($pipes[2]); $rc = proc_close($proc); +// the wizard may pick a switch; vm-bhyve takes it from the template, so patch it in +if ($verb === 'create' && !empty($job['switch'])) { + $conf = vmm_vm_conf_path($name); + if ($conf !== '' && is_file($conf)) { + $text = (string)file_get_contents($conf); + $want = 'network0_switch="' . $job['switch'] . '"'; + if (preg_match('/^network0_switch="[^"]*"/m', $text)) { + $text = preg_replace('/^network0_switch="[^"]*"/m', $want, $text, 1); + } else { + $text = rtrim($text) . "\n" . $want . "\n"; + } + if (file_put_contents($conf, $text) !== false) { + joblog($log, 'network0_switch set to ' . $job['switch']); + } + } +} + // vm-bhyve often returns 0 even when it refused the operation, so the // resulting state is what counts. Some states are transitional (a guest that // just booted reports "bootloader" for a moment), so wait a little. diff --git a/bin/vmm-lib.php b/bin/vmm-lib.php index 2959ce6..de33308 100644 --- a/bin/vmm-lib.php +++ b/bin/vmm-lib.php @@ -693,7 +693,7 @@ function vmm_job_running(string $id): bool return $job !== null && in_array($job['state'], ['queued', 'running'], true); } -function vmm_job_new(string $verb, string $name, array $argv = [], int $timeout = 0): array +function vmm_job_new(string $verb, string $name, array $argv = [], int $timeout = 0, array $meta = []): array { vmm_ensure_state_dirs(); $id = date('Ymd-His') . '-' . bin2hex(random_bytes(4)); @@ -712,6 +712,11 @@ function vmm_job_new(string $verb, string $name, array $argv = [], int $timeout 'error' => '', 'by' => (string)($_SERVER['REMOTE_ADDR'] ?? 'cli'), ]; + foreach ($meta as $k => $v) { + if (is_scalar($v)) { + $job[$k] = $v; + } + } vmm_job_write($job); @touch(VMM_JOBDIR . '/' . $id . '.log'); @@ -1152,6 +1157,15 @@ function vmm_vnc_connect(string $bind, string $host = ''): string * IP of the bridge that carries this VM's traffic (the address a client on * that network actually reaches). Falls back to the host name. */ +function vmm_iface_ipv4(string $iface): string +{ + if ($iface === '' || $iface === 'n/a') { + return ''; + } + $out = trim((string)@shell_exec('/sbin/ifconfig ' . escapeshellarg($iface) . ' inet 2>/dev/null')); + return preg_match('/inet (\d+\.\d+\.\d+\.\d+)/', $out, $m) ? $m[1] : ''; +} + function vmm_vnc_host_for_vm(string $name): string { $switchName = ''; @@ -1171,10 +1185,10 @@ function vmm_vnc_host_for_vm(string $name): string } } } - if ($iface !== '' && $iface !== 'n/a') { - $out = trim((string)@shell_exec('/sbin/ifconfig ' . escapeshellarg($iface) . ' inet 2>/dev/null')); - if (preg_match('/inet (\d+\.\d+\.\d+\.\d+)/', $out, $m)) { - return $m[1]; + if ($iface !== '') { + $ip = vmm_iface_ipv4($iface); + if ($ip !== '') { + return $ip; } } return trim((string)@shell_exec('hostname -f 2>/dev/null')) ?: (gethostname() ?: 'localhost'); diff --git a/gui/ext/vmmngr/vmmngr.conf b/gui/ext/vmmngr/vmmngr.conf index f17ff83..7b2e6c3 100644 --- a/gui/ext/vmmngr/vmmngr.conf +++ b/gui/ext/vmmngr/vmmngr.conf @@ -1,6 +1,6 @@ { "appname": "VM Manager", - "version": "0.8.0", + "version": "0.8.1", "rootfolder": "/mnt/Daten/extensions/vmmngr", "vm_source": "auto", "bridges": ["ix0bridge"], diff --git a/gui/vmmngr_create.php b/gui/vmmngr_create.php index cef16cd..3f050b1 100644 --- a/gui/vmmngr_create.php +++ b/gui/vmmngr_create.php @@ -22,6 +22,7 @@ if ($post) { $size = (string)($_POST['size'] ?? '20G'); $cloud = isset($_POST['cloudinit']); $img = (string)($_POST['image'] ?? ''); + $switch = trim((string)($_POST['switch'] ?? '')); if (!vmm_valid_name($name)) { $err = gettext('Invalid name: letters, digits, dot, dash, underscore (max 32).'); @@ -57,7 +58,7 @@ if ($post) { $argv[] = $img; } $argv[] = $name; - $job = vmm_job_new('create', $name, $argv, 900); + $job = vmm_job_new('create', $name, $argv, 900, ['switch' => $switch]); header('Location: vmmngr_jobs.php?id=' . urlencode((string)$job['id'])); exit; } @@ -122,8 +123,20 @@ vmm_nav('create');