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');
|
||||
|
||||
@@ -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"],
|
||||
|
||||
+16
-3
@@ -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');
|
||||
<td class="celltag"><?=gettext('Network switch');?></td>
|
||||
<td class="celldata">
|
||||
<select name="switch">
|
||||
<?php foreach ($switches as $s): ?>
|
||||
<option value="<?=vmm_h((string)($s['name'] ?? ''));?>"><?=vmm_h((string)($s['name'] ?? ''));?> (<?=vmm_h((string)($s['type'] ?? ''));?>)</option>
|
||||
<?php foreach ($switches as $s):
|
||||
$swName = (string)($s['name'] ?? '');
|
||||
$swType = (string)($s['type'] ?? '');
|
||||
$swIface = (string)($s['iface'] ?? '');
|
||||
$swIp = vmm_iface_ipv4($swIface);
|
||||
$label = $swName . ' — ' . $swType;
|
||||
if ($swIface !== '' && $swIface !== 'n/a') {
|
||||
$label .= ' → ' . $swIface;
|
||||
}
|
||||
if ($swIp !== '') {
|
||||
$label .= ' (' . $swIp . ')';
|
||||
}
|
||||
?>
|
||||
<option value="<?=vmm_h($swName);?>"><?=vmm_h($label);?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</td>
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
0.8.0
|
||||
0.8.1
|
||||
Reference in new issue
Block a user