0.2.1: fix job spawn (CLI php), transitional states, zfs hold release before destroy, API error logging

This commit is contained in:
Claw committed 2026-11-17 19:31:36 +01:00
1 parent 485a267ffe
commit 364ab42ffe
6 files changed
+233 -69

No files matched your search

+35 -13
View File
@@ -82,6 +82,21 @@ if ($grub['source'] === 'bundle') {
$env['PATH'] = VMM_ROOT . '/vendor/grub2-bhyve:' . ($env['PATH'] ?? '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin');
}
// destroying a VM fails when backup tools (znapzend/zrepl) hold its snapshots
if ($verb === 'destroy') {
$ds = vmm_dataset_for_vm($name);
if ($ds !== '') {
joblog($log, 'checking zfs holds on ' . $ds);
$released = vmm_release_holds($ds);
foreach ($released as $r) {
joblog($log, 'released hold: ' . $r);
}
if ($released === []) {
joblog($log, 'no holds to release');
}
}
}
$cmd = array_merge([$vm['path']], $argvVm);
$spec = [
0 => ['pipe', 'r'],
@@ -152,18 +167,25 @@ fclose($pipes[1]);
fclose($pipes[2]);
$rc = proc_close($proc);
$stateAfter = vmm_vm_state($name);
// vm-bhyve often returns 0 even when it refused the operation, so the
// resulting state is what counts.
$expected = [
'start' => 'running',
'stop' => 'stopped',
'restart' => 'running',
'reset' => 'running',
'poweroff' => 'stopped',
'destroy' => 'absent',
][$verb] ?? '';
// resulting state is what counts. Some states are transitional (a guest that
// just booted reports "bootloader" for a moment), so wait a little.
$accept = [
'start' => ['running', 'bootloader', 'starting'],
'stop' => ['stopped'],
'restart' => ['running', 'bootloader', 'starting'],
'reset' => ['running', 'bootloader'],
'poweroff' => ['stopped'],
'destroy' => ['absent'],
][$verb] ?? [];
$stateAfter = vmm_vm_state($name);
$deadline = time() + 30;
while ($accept !== [] && !in_array($stateAfter, $accept, true) && time() < $deadline) {
usleep(500000);
$stateAfter = vmm_vm_state($name);
}
joblog($log, 'state after action: ' . $stateAfter);
$job['rc'] = $rc;
$job['ended'] = time();
@@ -174,9 +196,9 @@ if ($timedOut) {
} elseif ($rc !== 0) {
$job['state'] = 'failed';
$job['error'] = 'vm exited with code ' . $rc;
} elseif ($expected !== '' && $stateAfter !== $expected) {
} elseif ($accept !== [] && !in_array($stateAfter, $accept, true)) {
$job['state'] = 'failed';
$job['error'] = sprintf('unexpected state after %s: %s (expected %s)', $verb, $stateAfter, $expected);
$job['error'] = sprintf('unexpected state after %s: %s (expected %s)', $verb, $stateAfter, implode('/', $accept));
} else {
$job['state'] = 'done';
}
+62 -2
View File
@@ -666,14 +666,20 @@ function vmm_job_new(string $verb, string $name): array
vmm_job_write($job);
@touch(VMM_JOBDIR . '/' . $id . '.log');
$php = PHP_BINARY !== '' ? PHP_BINARY : '/usr/local/bin/php';
// the web SAPI is php-cgi; jobs must be started with the CLI binary
$php = is_file('/usr/local/bin/php') ? '/usr/local/bin/php' : PHP_BINARY;
$cmd = sprintf(
'/usr/bin/nohup %s %s %s > /dev/null 2>&1 &',
escapeshellarg($php),
escapeshellarg(VMM_ROOT . '/bin/vmm-jobd'),
escapeshellarg($id)
);
exec($cmd);
if (function_exists('exec')) {
exec($cmd);
} else {
// exec() can be disabled in hardened PHP builds; shell_exec is enough here
@shell_exec($cmd);
}
return $job;
}
@@ -697,6 +703,60 @@ function vmm_job_recover(): int
return $fixed;
}
function vmm_dataset_for_vm(string $name): string
{
$vmDir = vmm_vm_dir();
if ($vmDir === '') {
return '';
}
$base = preg_replace('/^zfs:/', '', $vmDir);
$base = rtrim($base, '/');
return $base === '' ? '' : $base . '/' . $name;
}
/**
* Release ZFS holds that would block destroying a VM's datasets.
* Returns the list of released "snapshot tag" pairs.
*/
function vmm_release_holds(string $dataset): array
{
$released = [];
if ($dataset === '' || !preg_match('/^[A-Za-z0-9._:\/-]+$/', $dataset)) {
return $released;
}
// "zfs holds" only works on snapshots, so walk the snapshot tree first.
exec('/sbin/zfs list -H -o name -t snapshot -r ' . escapeshellarg($dataset) . ' 2>/dev/null', $snaps, $rc);
if ($rc !== 0) {
return $released;
}
$snaps = array_slice(array_filter(array_map('trim', $snaps)), 0, 2000);
foreach ($snaps as $snap) {
exec('/sbin/zfs holds ' . escapeshellarg($snap) . ' 2>/dev/null', $lines, $hrc);
if ($hrc !== 0 || $lines === []) {
continue;
}
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || stripos($line, 'NAME') === 0) {
continue;
}
$parts = preg_split('/\s+/', $line);
if (count($parts) < 2) {
continue;
}
[$holdSnap, $tag] = $parts;
exec('/sbin/zfs release ' . escapeshellarg($tag) . ' ' . escapeshellarg($holdSnap) . ' 2>&1', $out, $r);
if ($r === 0) {
$released[] = $holdSnap . ' ' . $tag;
}
}
$lines = [];
}
return $released;
}
function vmm_pid_alive(int $pid): bool
{
if ($pid <= 0) {