config.xml + vm init * Step 3: first switch on an existing host bridge */ require_once 'vmmngr-boot.inc'; require_once 'vmmngr-config-lib.inc'; $vmDir = vmm_rc_param('vm_dir'); if ($vmDir === '') { $out = trim((string)@shell_exec('/usr/bin/grep -E "^vm_dir=" /etc/rc.conf 2>/dev/null')); if (preg_match('/vm_dir="?([^"\n]+)"?/', $out, $m)) { $vmDir = $m[1]; } } $switchList = vmm_switches(); $hostBridges = array_values(array_filter(array_map('trim', explode("\n", trim((string)@shell_exec('/sbin/ifconfig -g bridge 2>/dev/null')))))); $post = strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? '')) === 'POST'; $act = $post ? (string)($_POST['act'] ?? '') : ''; if ($post && $act === 'save_datastore') { $spec = trim((string)($_POST['datastore'] ?? '')); $newDs = trim((string)($_POST['new_dataset'] ?? '')); $create = isset($_POST['create_dataset']); if ($newDs !== '' && $create) { if (!preg_match('#^[A-Za-z0-9][A-Za-z0-9._-]*/[A-Za-z0-9][A-Za-z0-9._-]*$#', $newDs)) { $_SESSION['vmmngr_error'] = gettext('Dataset name must look like pool/name.'); header('Location: vmmngr_setup.php?step=2'); exit; } exec('/sbin/zfs list -H -o name ' . escapeshellarg($newDs) . ' 2>/dev/null', $o, $rc); if ($rc !== 0) { exec('/sbin/zfs create ' . escapeshellarg($newDs) . ' 2>&1', $out, $crc); if ($crc !== 0) { $_SESSION['vmmngr_error'] = 'zfs create failed: ' . implode(' ', $out); header('Location: vmmngr_setup.php?step=2'); exit; } } $spec = 'zfs:' . $newDs; } if (!preg_match('#^zfs:([A-Za-z0-9._:/-]+)$#', $spec, $m)) { $_SESSION['vmmngr_error'] = gettext('Choose a ZFS dataset.'); header('Location: vmmngr_setup.php?step=2'); exit; } $ds = $m[1]; exec('/sbin/zfs list -H -o mounted ' . escapeshellarg($ds) . ' 2>/dev/null', $o2, $rc2); if ($rc2 !== 0 || trim((string)($o2[0] ?? '')) !== 'yes') { $_SESSION['vmmngr_error'] = gettext('Dataset is not mounted - cannot use it as vm_dir.'); header('Location: vmmngr_setup.php?step=2'); exit; } vmm_set_rc_param('vm_dir', 'zfs:' . $ds); vmm_set_rc_param('vm_enable', 'YES'); $res = vmm_run(['init'], 60); $_SESSION['vmmngr_error'] = $res['rc'] === 0 ? sprintf(gettext('Default datastore set to zfs:%s and initialised.'), $ds) : trim($res['stderr'] ?: $res['stdout'] ?: 'vm init failed'); header('Location: vmmngr_setup.php?step=3'); exit; } if ($post && $act === 'add_switch') { $name = trim((string)($_POST['sw_name'] ?? '')); $bridge = trim((string)($_POST['bridge'] ?? '')); if (!preg_match('/^[A-Za-z0-9][A-Za-z0-9_-]{0,31}$/', $name)) { $_SESSION['vmmngr_error'] = gettext('Invalid switch name.'); } elseif (!in_array($bridge, $hostBridges, true)) { $_SESSION['vmmngr_error'] = gettext('Choose an existing host bridge.'); } else { $res = vmm_run(['switch', 'create', '-t', 'manual', '-b', $bridge, $name], 30); $_SESSION['vmmngr_error'] = $res['rc'] === 0 ? sprintf(gettext('Switch %s created on %s.'), $name, $bridge) : trim($res['stderr'] ?: $res['stdout'] ?: 'switch create failed'); if ($res['rc'] === 0) { header('Location: vmmngr_setup.php?step=4'); exit; } } header('Location: vmmngr_setup.php?step=3'); exit; } $step = (int)($_GET['step'] ?? 0); if ($step < 1 || $step > 4) { $step = $vmDir === '' ? 2 : ($switchList === [] ? 3 : 4); } // dataset inventory for the picker $datasets = []; exec('/sbin/zfs list -H -o name,mountpoint -t filesystem 2>/dev/null', $zlines); foreach ($zlines as $line) { $parts = preg_split('/\t+/', trim($line)); if (count($parts) < 2) { continue; } [$dsName, $mp] = $parts; if ($mp === '/' || $mp === 'none' || $mp === '-' || !str_starts_with($mp, '/mnt')) { continue; } if (substr_count($dsName, '/') > 1) { continue; } $datasets[$dsName] = $mp; } $pgtitle = [gettext('Extensions'), gettext('VM Manager'), gettext('Setup')]; include 'fbegin.inc'; vmm_css(); vmm_nav('setup'); ?>
1 · 2 · 3 · 4 ·
 
 
(string)($s['name'] ?? ''), $switchList)));?>
 
vm_dir
(string)($s['name'] ?? ''), $switchList)));?>