0.5.0: web terminal per VM (ttyd on the serial console, basic auth, auto-stop with the guest)
This commit is contained in:
1 parent
b9ad09e717
commit
b2a12eb72a
5 files changed
+161
-2
No files matched your search
@@ -198,6 +198,10 @@ while ($accept !== [] && !in_array($stateAfter, $accept, true) && time() < $dead
|
|||||||
}
|
}
|
||||||
joblog($log, 'state after action: ' . $stateAfter);
|
joblog($log, 'state after action: ' . $stateAfter);
|
||||||
|
|
||||||
|
if (in_array($verb, ['stop', 'poweroff', 'destroy'], true)) {
|
||||||
|
vmm_console_stop($name);
|
||||||
|
}
|
||||||
|
|
||||||
$job['rc'] = $rc;
|
$job['rc'] = $rc;
|
||||||
$job['ended'] = time();
|
$job['ended'] = time();
|
||||||
$job['state_after'] = $stateAfter;
|
$job['state_after'] = $stateAfter;
|
||||||
|
|||||||
+126
@@ -830,6 +830,132 @@ 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
|
* 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.
|
* actually connect to. 0.0.0.0 is a listen address, not a destination.
|
||||||
*/
|
*/
|
||||||
|
function vmm_console_state_file(string $vm): string
|
||||||
|
{
|
||||||
|
return VMM_ROOT . '/state/console/' . preg_replace('/[^A-Za-z0-9._-]/', '_', $vm) . '.json';
|
||||||
|
}
|
||||||
|
|
||||||
|
function vmm_console_status(string $vm): ?array
|
||||||
|
{
|
||||||
|
$f = vmm_console_state_file($vm);
|
||||||
|
if (!is_file($f)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$st = json_decode((string)file_get_contents($f), true);
|
||||||
|
if (!is_array($st)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!vmm_pid_alive((int)($st['pid'] ?? 0))) {
|
||||||
|
@unlink($f);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return $st;
|
||||||
|
}
|
||||||
|
|
||||||
|
function vmm_console_stop(string $vm): array
|
||||||
|
{
|
||||||
|
$st = vmm_console_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_console_state_file($vm));
|
||||||
|
return ['ok' => true, 'stopped' => true];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start a ttyd instance that serves the VM's serial console (nmdm device).
|
||||||
|
*/
|
||||||
|
function vmm_console_start(string $vm, string $bindHost = ''): array
|
||||||
|
{
|
||||||
|
$ttyd = vmm_resolve('ttyd');
|
||||||
|
if (!$ttyd['available']) {
|
||||||
|
return ['ok' => false, 'error' => 'ttyd not found (install www/ttyd or bundle it)'];
|
||||||
|
}
|
||||||
|
if (vmm_vm_state($vm) !== 'running') {
|
||||||
|
return ['ok' => false, 'error' => 'vm is not running'];
|
||||||
|
}
|
||||||
|
$info = vmm_info($vm);
|
||||||
|
$dev = '';
|
||||||
|
foreach (($info['sections']['console-ports'] ?? []) as $b) {
|
||||||
|
if (!empty($b['data']['com1'])) {
|
||||||
|
$dev = (string)$b['data']['com1'];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($dev === '' || !file_exists($dev)) {
|
||||||
|
return ['ok' => false, 'error' => 'no serial console device (com1) for this vm'];
|
||||||
|
}
|
||||||
|
if ($bindHost === '') {
|
||||||
|
$bindHost = vmm_vnc_host_for_vm($vm);
|
||||||
|
}
|
||||||
|
|
||||||
|
$existing = vmm_console_status($vm);
|
||||||
|
if ($existing !== null) {
|
||||||
|
return ['ok' => true] + $existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
$port = 0;
|
||||||
|
for ($p = 7681; $p <= 7699; $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 7681-7699'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = 'vmm';
|
||||||
|
$pass = bin2hex(random_bytes(6));
|
||||||
|
$dir = VMM_ROOT . '/state/console';
|
||||||
|
if (!is_dir($dir)) {
|
||||||
|
@mkdir($dir, 0750, true);
|
||||||
|
}
|
||||||
|
$log = $dir . '/' . $vm . '.log';
|
||||||
|
$cmd = sprintf(
|
||||||
|
'/usr/bin/nohup %s -p %d -i %s -c %s -W /usr/bin/cu -l %s > %s 2>&1 &',
|
||||||
|
escapeshellarg($ttyd['path']),
|
||||||
|
$port,
|
||||||
|
escapeshellarg($bindHost),
|
||||||
|
escapeshellarg($user . ':' . $pass),
|
||||||
|
escapeshellarg($dev),
|
||||||
|
escapeshellarg($log)
|
||||||
|
);
|
||||||
|
if (function_exists('exec')) {
|
||||||
|
exec($cmd);
|
||||||
|
} else {
|
||||||
|
@shell_exec($cmd);
|
||||||
|
}
|
||||||
|
usleep(600000);
|
||||||
|
|
||||||
|
$pid = 0;
|
||||||
|
exec('/usr/bin/pgrep -f ' . escapeshellarg('ttyd -p ' . $port) . ' 2>/dev/null', $out, $rc);
|
||||||
|
if ($rc === 0 && !empty($out[0])) {
|
||||||
|
$pid = (int)$out[0];
|
||||||
|
}
|
||||||
|
if ($pid === 0) {
|
||||||
|
return ['ok' => false, 'error' => 'ttyd did not start, see ' . $log];
|
||||||
|
}
|
||||||
|
|
||||||
|
$st = [
|
||||||
|
'vm' => $vm,
|
||||||
|
'pid' => $pid,
|
||||||
|
'port' => $port,
|
||||||
|
'host' => $bindHost,
|
||||||
|
'user' => $user,
|
||||||
|
'pass' => $pass,
|
||||||
|
'device' => $dev,
|
||||||
|
'started' => time(),
|
||||||
|
];
|
||||||
|
file_put_contents(vmm_console_state_file($vm), json_encode($st, JSON_PRETTY_PRINT));
|
||||||
|
return ['ok' => true] + $st;
|
||||||
|
}
|
||||||
|
|
||||||
function vmm_vnc_connect(string $bind, string $host = ''): string
|
function vmm_vnc_connect(string $bind, string $host = ''): string
|
||||||
{
|
{
|
||||||
$bind = trim($bind);
|
$bind = trim($bind);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"appname": "VM Manager",
|
"appname": "VM Manager",
|
||||||
"version": "0.4.3",
|
"version": "0.5.0",
|
||||||
"rootfolder": "/mnt/Daten/extensions/vmmngr",
|
"rootfolder": "/mnt/Daten/extensions/vmmngr",
|
||||||
"vm_source": "auto",
|
"vm_source": "auto",
|
||||||
"bridges": ["ix0bridge"],
|
"bridges": ["ix0bridge"],
|
||||||
|
|||||||
@@ -24,6 +24,21 @@ if (strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? '')) === 'POST' && isset($
|
|||||||
$vmName = $name;
|
$vmName = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($verb === 'console_start') {
|
||||||
|
$res = vmm_console_start($name);
|
||||||
|
$_SESSION['vmmngr_error'] = $res['ok']
|
||||||
|
? sprintf(gettext('Web console on http://%s:%d'), $res['host'], $res['port'])
|
||||||
|
: (string)$res['error'];
|
||||||
|
header('Location: vmmngr_vm.php?name=' . urlencode($name));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
if ($verb === 'console_stop') {
|
||||||
|
vmm_console_stop($name);
|
||||||
|
$_SESSION['vmmngr_error'] = gettext('Web console stopped.');
|
||||||
|
header('Location: vmmngr_vm.php?name=' . urlencode($name));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
// media: attach ISOs (install) or eject them
|
// media: attach ISOs (install) or eject them
|
||||||
if ($verb === 'install' || $verb === 'eject') {
|
if ($verb === 'install' || $verb === 'eject') {
|
||||||
if ($verb === 'eject') {
|
if ($verb === 'eject') {
|
||||||
@@ -159,6 +174,20 @@ vmm_nav('dashboard');
|
|||||||
<td class="celltag"><?=gettext('Serial');?></td>
|
<td class="celltag"><?=gettext('Serial');?></td>
|
||||||
<td class="celldata vmm-mono">cu -l /dev/nmdm-<?=vmm_h($name);?>.1B</td>
|
<td class="celldata vmm-mono">cu -l /dev/nmdm-<?=vmm_h($name);?>.1B</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<?php $con = vmm_console_status($name); ?>
|
||||||
|
<tr>
|
||||||
|
<td class="celltag"><?=gettext('Web terminal');?></td>
|
||||||
|
<td class="celldata">
|
||||||
|
<?php if ($con !== null): ?>
|
||||||
|
<a class="formbtn" href="http://<?=vmm_h((string)$con['host']);?>:<?=(int)$con['port'];?>/" target="_blank">console öffnen</a>
|
||||||
|
<span class="vmm-mono" style="margin-left:8px;"><?=vmm_h((string)$con['user']);?> / <?=vmm_h((string)$con['pass']);?></span>
|
||||||
|
<button type="submit" class="formbtn" name="act" value="console_stop:<?=vmm_h($name);?>" style="margin-left:8px;"><?=gettext('Stop');?></button>
|
||||||
|
<?php else: ?>
|
||||||
|
<button type="submit" class="formbtn" name="act" value="console_start:<?=vmm_h($name);?>"><?=gettext('Start web console');?></button>
|
||||||
|
<span style="opacity:.75;margin-left:8px;"><?=gettext('ttyd serves the serial console (text only) with basic auth.');?></span>
|
||||||
|
<?php endif; ?>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
0.4.3
|
0.5.0
|
||||||
Reference in new issue
Block a user