diff --git a/bin/vmm-jobd b/bin/vmm-jobd index 28a1dd4..88b39af 100644 --- a/bin/vmm-jobd +++ b/bin/vmm-jobd @@ -200,6 +200,7 @@ joblog($log, 'state after action: ' . $stateAfter); if (in_array($verb, ['stop', 'poweroff', 'destroy'], true)) { vmm_console_stop($name); + vmm_novnc_stop($name); } $job['rc'] = $rc; diff --git a/bin/vmm-lib.php b/bin/vmm-lib.php index feda7a9..e192046 100644 --- a/bin/vmm-lib.php +++ b/bin/vmm-lib.php @@ -830,6 +830,135 @@ function vmm_delete_data_file(string $dir, string $name): array * Turn a bhyve VNC bind address ("0.0.0.0:5901") into something a human can * actually connect to. 0.0.0.0 is a listen address, not a destination. */ +function vmm_novnc_ready(): array +{ + $bin = '/usr/local/bin/websockify'; + $web = '/usr/local/libexec/novnc'; + return [ + 'binary' => $bin, + 'web' => $web, + 'ok' => is_executable($bin) && is_file($web . '/vnc.html'), + ]; +} + +function vmm_novnc_state_file(string $vm): string +{ + return VMM_ROOT . '/state/novnc/' . preg_replace('/[^A-Za-z0-9._-]/', '_', $vm) . '.json'; +} + +function vmm_novnc_status(string $vm): ?array +{ + $f = vmm_novnc_state_file($vm); + if (!is_file($f)) { + return null; + } + $st = json_decode((string)file_get_contents($f), true); + if (!is_array($st) || !vmm_pid_alive((int)($st['pid'] ?? 0))) { + @unlink($f); + return null; + } + return $st; +} + +function vmm_novnc_stop(string $vm): array +{ + $st = vmm_novnc_status($vm); + if ($st === null) { + return ['ok' => true, 'stopped' => false]; + } + exec('/bin/kill ' . (int)$st['pid'] . ' >/dev/null 2>&1'); + usleep(300000); + exec('/bin/kill -9 ' . (int)$st['pid'] . ' >/dev/null 2>&1'); + @unlink(vmm_novnc_state_file($vm)); + return ['ok' => true, 'stopped' => true]; +} + +/** + * Serve the VM's VNC screen through noVNC (websockify + static web root). + */ +function vmm_novnc_start(string $vm, string $bindHost = ''): array +{ + $ready = vmm_novnc_ready(); + if (!$ready['ok']) { + return ['ok' => false, 'error' => 'noVNC/websockify not installed (pkg install novnc py312-websockify)']; + } + if (vmm_vm_state($vm) !== 'running') { + return ['ok' => false, 'error' => 'vm is not running']; + } + $info = vmm_info($vm); + $vncPort = 0; + foreach (($info['sections']['console-ports'] ?? []) as $b) { + if (!empty($b['data']['vnc']) && preg_match('/:(\d+)$/', (string)$b['data']['vnc'], $m)) { + $vncPort = (int)$m[1]; + break; + } + } + if ($vncPort === 0) { + return ['ok' => false, 'error' => 'this vm has no VNC screen (graphics="no"?)']; + } + if ($bindHost === '') { + $bindHost = vmm_vnc_host_for_vm($vm); + } + $existing = vmm_novnc_status($vm); + if ($existing !== null) { + return ['ok' => true] + $existing; + } + + $port = 0; + for ($p = 6080; $p <= 6099; $p++) { + $fp = @fsockopen('127.0.0.1', $p, $errno, $errstr, 0.2); + if ($fp === false) { + $port = $p; + break; + } + fclose($fp); + } + if ($port === 0) { + return ['ok' => false, 'error' => 'no free port in 6080-6099']; + } + + $dir = VMM_ROOT . '/state/novnc'; + if (!is_dir($dir)) { + @mkdir($dir, 0750, true); + } + $log = $dir . '/' . $vm . '.log'; + $cmd = sprintf( + '/usr/bin/nohup %s --web %s %s %s > %s 2>&1 &', + escapeshellarg($ready['binary']), + escapeshellarg($ready['web']), + escapeshellarg($bindHost . ':' . $port), + escapeshellarg('127.0.0.1:' . $vncPort), + escapeshellarg($log) + ); + if (function_exists('exec')) { + exec($cmd); + } else { + @shell_exec($cmd); + } + usleep(700000); + + $pid = 0; + exec('/usr/bin/pgrep -f ' . escapeshellarg('websockify --web ' . $ready['web'] . ' ' . $bindHost . ':' . $port) . ' 2>/dev/null', $out, $rc); + if ($rc === 0 && !empty($out[0])) { + $pid = (int)$out[0]; + } + if ($pid === 0) { + return ['ok' => false, 'error' => 'websockify did not start, see ' . $log]; + } + + $st = [ + 'vm' => $vm, + 'pid' => $pid, + 'port' => $port, + 'host' => $bindHost, + 'vnc_port' => $vncPort, + 'started' => time(), + 'url' => sprintf('http://%s:%d/vnc.html?autoconnect=1&resize=scale&path=websockify', $bindHost, $port), + ]; + file_put_contents(vmm_novnc_state_file($vm), json_encode($st, JSON_PRETTY_PRINT)); + return ['ok' => true] + $st; +} + function vmm_console_state_file(string $vm): string { return VMM_ROOT . '/state/console/' . preg_replace('/[^A-Za-z0-9._-]/', '_', $vm) . '.json'; diff --git a/gui/ext/vmmngr/vmmngr.conf b/gui/ext/vmmngr/vmmngr.conf index 5238066..500b191 100644 --- a/gui/ext/vmmngr/vmmngr.conf +++ b/gui/ext/vmmngr/vmmngr.conf @@ -1,6 +1,6 @@ { "appname": "VM Manager", - "version": "0.5.1", + "version": "0.6.0", "rootfolder": "/mnt/Daten/extensions/vmmngr", "vm_source": "auto", "bridges": ["ix0bridge"], diff --git a/gui/vmmngr_vm.php b/gui/vmmngr_vm.php index e637d9b..6a123b6 100644 --- a/gui/vmmngr_vm.php +++ b/gui/vmmngr_vm.php @@ -24,6 +24,20 @@ if (strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? '')) === 'POST' && isset($ $vmName = $name; } + if ($verb === 'novnc_start' || $verb === 'novnc_stop') { + if ($verb === 'novnc_stop') { + vmm_novnc_stop($name); + $_SESSION['vmmngr_error'] = gettext('noVNC stopped.'); + } else { + $res = vmm_novnc_start($name); + $_SESSION['vmmngr_error'] = $res['ok'] + ? sprintf(gettext('noVNC on http://%s:%d'), $res['host'], $res['port']) + : (string)$res['error']; + } + header('Location: vmmngr_vm.php?name=' . urlencode($name)); + exit; + } + if ($verb === 'console_start') { $res = vmm_console_start($name); $_SESSION['vmmngr_error'] = $res['ok'] @@ -211,6 +225,37 @@ vmm_nav('dashboard'); + + + + + + + + + + + + + + + + +
+ + + pkg install novnc py312-websockify + + + +
+ + + +
+ +