0.4.0: ISO install (attach CD devices incl. virtio-win driver ISO), eject, virtio-win download preset
This commit is contained in:
1 parent
e989df97a5
commit
c577f3c96f
5 files changed
+232
-2
No files matched your search
+117
@@ -826,6 +826,123 @@ function vmm_delete_data_file(string $dir, string $name): array
|
||||
: ['ok' => false, 'error' => 'cannot delete'];
|
||||
}
|
||||
|
||||
function vmm_vm_conf_path(string $name): string
|
||||
{
|
||||
$dir = vmm_data_dir('');
|
||||
$dir = rtrim($dir, '/');
|
||||
if ($dir === '' || !vmm_valid_name($name)) {
|
||||
return '';
|
||||
}
|
||||
return $dir . '/' . $name . '/' . $name . '.conf';
|
||||
}
|
||||
|
||||
function vmm_attached_isos(string $name): array
|
||||
{
|
||||
$conf = vmm_vm_conf_path($name);
|
||||
if ($conf === '' || !is_file($conf)) {
|
||||
return [];
|
||||
}
|
||||
$out = [];
|
||||
$cur = null;
|
||||
foreach (preg_split('/\R/', (string)file_get_contents($conf)) ?: [] as $line) {
|
||||
if (preg_match('/^disk(\d+)_type="(ahci-cd|ahci-hd)"/', trim($line), $m)) {
|
||||
$cur = (int)$m[1];
|
||||
$out[$cur] = ['slot' => $cur, 'path' => ''];
|
||||
} elseif ($cur !== null && preg_match('/^disk' . $cur . '_name="([^"]+)"/', trim($line), $m2)) {
|
||||
$out[$cur]['path'] = $m2[1];
|
||||
}
|
||||
}
|
||||
return array_values(array_filter($out, static fn($d) => ($d['path'] ?? '') !== ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach ISOs as read-only CD devices and drop any previously attached ones.
|
||||
* Returns ['added' => [...], 'removed' => [...]]
|
||||
*/
|
||||
function vmm_iso_attach(string $name, array $isos): array
|
||||
{
|
||||
$conf = vmm_vm_conf_path($name);
|
||||
if ($conf === '' || !is_file($conf)) {
|
||||
return ['error' => 'vm config not found'];
|
||||
}
|
||||
$isos = array_values(array_filter($isos, static fn($p) => $p !== ''));
|
||||
foreach ($isos as $iso) {
|
||||
if (!is_file($iso) || !str_ends_with(strtolower($iso), '.iso')) {
|
||||
return ['error' => 'iso not found: ' . basename($iso)];
|
||||
}
|
||||
}
|
||||
|
||||
$lines = preg_split('/\R/', (string)file_get_contents($conf)) ?: [];
|
||||
$kept = [];
|
||||
$removed = [];
|
||||
$skip = null;
|
||||
foreach ($lines as $line) {
|
||||
if (preg_match('/^disk(\d+)_type="ahci-cd"\s*$/', trim($line), $m)) {
|
||||
$skip = (int)$m[1];
|
||||
$removed[] = 'disk' . $skip;
|
||||
continue;
|
||||
}
|
||||
if ($skip !== null && preg_match('/^disk' . $skip . '_/', trim($line))) {
|
||||
continue;
|
||||
}
|
||||
$skip = null;
|
||||
$kept[] = $line;
|
||||
}
|
||||
|
||||
$max = -1;
|
||||
foreach ($kept as $line) {
|
||||
if (preg_match('/^disk(\d+)_/', trim($line), $m)) {
|
||||
$max = max($max, (int)$m[1]);
|
||||
}
|
||||
}
|
||||
$added = [];
|
||||
foreach ($isos as $iso) {
|
||||
$max++;
|
||||
$kept[] = 'disk' . $max . '_type="ahci-cd"';
|
||||
$kept[] = 'disk' . $max . '_dev="custom"';
|
||||
$kept[] = 'disk' . $max . '_name="' . $iso . '"';
|
||||
$added[] = 'disk' . $max . ' = ' . basename($iso);
|
||||
}
|
||||
|
||||
if (@copy($conf, $conf . '.vmmngr.bak') === false) {
|
||||
return ['error' => 'cannot back up vm config'];
|
||||
}
|
||||
if (@file_put_contents($conf, implode("\n", $kept) . "\n") === false) {
|
||||
return ['error' => 'cannot write vm config'];
|
||||
}
|
||||
return ['added' => $added, 'removed' => $removed];
|
||||
}
|
||||
|
||||
function vmm_iso_detach(string $name): array
|
||||
{
|
||||
$conf = vmm_vm_conf_path($name);
|
||||
if ($conf === '' || !is_file($conf)) {
|
||||
return ['error' => 'vm config not found'];
|
||||
}
|
||||
$lines = preg_split('/\R/', (string)file_get_contents($conf)) ?: [];
|
||||
$kept = [];
|
||||
$removed = [];
|
||||
$skip = null;
|
||||
foreach ($lines as $line) {
|
||||
if (preg_match('/^disk(\d+)_type="ahci-cd"\s*$/', trim($line), $m)) {
|
||||
$skip = (int)$m[1];
|
||||
$removed[] = 'disk' . $skip;
|
||||
continue;
|
||||
}
|
||||
if ($skip !== null && preg_match('/^disk' . $skip . '_/', trim($line))) {
|
||||
continue;
|
||||
}
|
||||
$skip = null;
|
||||
$kept[] = $line;
|
||||
}
|
||||
if ($removed === []) {
|
||||
return ['removed' => []];
|
||||
}
|
||||
@copy($conf, $conf . '.vmmngr.bak');
|
||||
@file_put_contents($conf, implode("\n", $kept) . "\n");
|
||||
return ['removed' => $removed];
|
||||
}
|
||||
|
||||
function vmm_valid_template_name(string $name): bool
|
||||
{
|
||||
return (bool)preg_match('/^[A-Za-z0-9][A-Za-z0-9 ._()+\-]{0,120}\.conf$/', $name);
|
||||
|
||||
Reference in new issue
Block a user