0.8.1: switch dropdown shows type, interface and IP; chosen switch is written into the new VM config
This commit is contained in:
1 parent
dd395dfe76
commit
81b4d6a63e
5 files changed
+54
-10
No files matched your search
@@ -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.
|
||||
|
||||
+19
-5
@@ -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');
|
||||
|
||||
Reference in new issue
Block a user