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

+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) {