diff --git a/emhttp/languages/en_US/helptext.txt b/emhttp/languages/en_US/helptext.txt index 16c31f19d9..94b6b91677 100644 --- a/emhttp/languages/en_US/helptext.txt +++ b/emhttp/languages/en_US/helptext.txt @@ -1711,7 +1711,11 @@ Stop VMs from Autostarting\Starting when VM Manager starts or open is run from t :end :vms_libvirt_volume_help: -This is the libvirt volume. +This is the libvirt volume/directory. +:end + +:vms_libvirt_secondary_volume_help: +This is a location for storing previous versions of XML and NVRAM when changes are made. :end :vms_libvirt_vdisk_size_help: @@ -1720,7 +1724,11 @@ To resize an existing image file, specify the new size here. Next time the Libvi :end :vms_libvirt_location_help: -You must specify an image file for Libvirt. The system will automatically create this file when the Libvirt service is first started. +You must specify an image file/directory for Libvirt. The system will automatically create this file/directory when the Libvirt service is first started. +:end + +:vms_libvirt_secondary_location_help: +This is a directory for storing previous versions of xml and nvram at change. Does not need to be specified. :end :vms_libvirt_storage_help: diff --git a/emhttp/plugins/dynamix.vm.manager/VMSettings.page b/emhttp/plugins/dynamix.vm.manager/VMSettings.page index e772324bb7..e9a96e1217 100644 --- a/emhttp/plugins/dynamix.vm.manager/VMSettings.page +++ b/emhttp/plugins/dynamix.vm.manager/VMSettings.page @@ -123,7 +123,7 @@ $libvirt_log = file_exists("/var/log/libvirt/libvirtd.log"); - + _(Enable VMs)_: : :vms_libvirt_vdisk_size_help: _(Libvirt storage location)_: -: +: @@ -179,6 +181,7 @@ _(Libvirt storage location)_: :vms_libvirt_location_help: + _(Default VM storage path)_: : @@ -286,7 +289,7 @@ _(Boot Parameter Settings)_: - +
_(Libvirt volume info)_
_(btrfs filesystem show)_: diff --git a/emhttp/plugins/dynamix.vm.manager/include/VMajax.php b/emhttp/plugins/dynamix.vm.manager/include/VMajax.php index 34acadd4d7..24391c4af5 100644 --- a/emhttp/plugins/dynamix.vm.manager/include/VMajax.php +++ b/emhttp/plugins/dynamix.vm.manager/include/VMajax.php @@ -282,7 +282,9 @@ function embed(&$bootcfg, $env, $key, $value) { case 'domain-delete': requireLibvirt(); - $arrResponse = $lv->domain_delete($domName) + $firstdisk = filter_var(_var($_REQUEST,'firstdisk', 'true'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + if ($firstdisk === null) $firstdisk = true; + $arrResponse = $lv->domain_delete($domName, $firstdisk) ? ['success' => true] : ['error' => $lv->get_last_error()]; break; @@ -484,7 +486,8 @@ function embed(&$bootcfg, $env, $key, $value) { $list = glob($pathinfo['dirname']."/*"); $uuid = $lv->domain_get_uuid($domName); - $list2 = glob("/etc/libvirt/qemu/nvram/*$uuid*"); + $vm_path = libvirt_get_vm_path($domName); + $list2 = glob(libvirt_get_nvram_dir($vm_path, $domName)."/*$uuid*"); $listnew = array(); $list=array_merge($list,$list2); foreach($list as $key => $listent) diff --git a/emhttp/plugins/dynamix.vm.manager/include/fs_helpers.php b/emhttp/plugins/dynamix.vm.manager/include/fs_helpers.php new file mode 100644 index 0000000000..1975e51999 --- /dev/null +++ b/emhttp/plugins/dynamix.vm.manager/include/fs_helpers.php @@ -0,0 +1,100 @@ + $src, + 'dst' => $dst, + 'would_copy' => false, + 'copied' => false, + 'error' => null + ]; + + if (!file_exists($src)) { + $result['error'] = 'source not found'; + return $result; + } + + $dst_dir = dirname($dst); + if (!is_dir($dst_dir)) { + if ($dry_run) { + $result['would_copy'] = true; + return $result; + } + if (!@mkdir($dst_dir, 0755, true)) { + $result['error'] = 'failed to create dest dir'; + return $result; + } + } + + if (file_exists($dst)) { + if (files_identical($src, $dst)) { + return $result; // identical, nothing to do + } + $result['would_copy'] = true; + } else { + $result['would_copy'] = true; + } + + if ($dry_run) return $result; + + if (@copy($src, $dst)) { + $result['copied'] = true; + } else { + $result['error'] = 'copy_failed'; + } + + return $result; +} + +function dir_copy($src, $dst) { + if (!is_dir($src)) return false; + if (!is_dir($dst)) { + if (!@mkdir($dst, 0755, true)) return false; + } + $items = scandir($src); + foreach ($items as $item) { + if ($item === '.' || $item === '..') continue; + $s = $src . DIRECTORY_SEPARATOR . $item; + $d = $dst . DIRECTORY_SEPARATOR . $item; + if (is_dir($s)) { + if (!dir_copy($s, $d)) return false; + } else { + if (file_exists($d)) { + if (files_identical($s, $d)) continue; + } + if (!@copy($s, $d)) return false; + } + } + return true; +} + +function dir_remove($dir) { + if (!is_dir($dir)) return false; + $items = scandir($dir); + if ($items === false) return false; + foreach ($items as $item) { + if ($item === '.' || $item === '..') continue; + $path = $dir . DIRECTORY_SEPARATOR . $item; + if (is_dir($path)) { + if (!dir_remove($path)) return false; + } else { + if (!@unlink($path)) return false; + } + } + return @rmdir($dir); +} diff --git a/emhttp/plugins/dynamix.vm.manager/include/libvirt.php b/emhttp/plugins/dynamix.vm.manager/include/libvirt.php index aaf7a951dd..ef986fb892 100644 --- a/emhttp/plugins/dynamix.vm.manager/include/libvirt.php +++ b/emhttp/plugins/dynamix.vm.manager/include/libvirt.php @@ -12,6 +12,7 @@ */ ?> /usr/share/qemu/ovmf-x64/OVMF_CODE-pure-efi.fd - /etc/libvirt/qemu/nvram/".$uuid."_VARS-pure-efi.fd"; + ".$nvram_dir."/".$uuid."_VARS-pure-efi.fd"; if ($domain['usbboot'] == 'Yes') $osbootdev = ""; } if ($domain['ovmf'] == 2) { - if (!is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd')) { + if (!$defer_write && !is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd')) { // Create a new copy of OVMF VARS for this VM - mkdir('/etc/libvirt/qemu/nvram/', 0777, true); - copy('/usr/share/qemu/ovmf-x64/OVMF_VARS-pure-efi-tpm.fd', '/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd'); + mkdir($nvram_dir.'/', 0777, true); + copy('/usr/share/qemu/ovmf-x64/OVMF_VARS-pure-efi-tpm.fd', $nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd'); } - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd')) { + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd')) { // Delete OVMF VARS for this VM if found - unlink('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd'); + unlink($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd'); } $loader = "/usr/share/qemu/ovmf-x64/OVMF_CODE-pure-efi-tpm.fd - /etc/libvirt/qemu/nvram/".$uuid."_VARS-pure-efi-tpm.fd"; + ".$nvram_dir."/".$uuid."_VARS-pure-efi-tpm.fd"; $swtpm = " "; @@ -1014,7 +1026,27 @@ function appendqemucmdline($xml, $cmdline) { return $newxml; } + function build_vm_paths($config) { + if (!is_vm_newmodel()) { + return $config; + } + if (empty($config['domain']['uuid'])) { + $config['domain']['uuid'] = $this->domain_generate_uuid(); + } + $vm_name = $config['domain']['name'] ?? null; + $storage = $config['template']['storage'] ?? 'default'; + $uuid = $config['domain']['uuid'] ?? null; + $entry = libvirt_build_vm_entry($vm_name, $storage, null, $uuid); + if ($entry !== null) { + libvirt_update_vms_json_entry($vm_name, $entry); + $config['domain']['path'] = $entry['path'] ?? null; + } + return $config; + } + function domain_new($config) { + # Build paths for VM and store in the vms.json + $config = $this->build_vm_paths($config); # Set storage for disks. foreach ($config['disk'] as $i => $disk) { $config['disk'][$i]['storage'] = $config['template']['storage'];} // attempt to create all disk images if needed @@ -1043,7 +1075,8 @@ function domain_new($config) { } // Define the VM to persist if ($config['domain']['persistent']) { - $tmp = libvirt_domain_define_xml($this->conn, $strXML); + #$tmp = libvirt_domain_define_xml($this->conn, $strXML); ## Update to use new function + $tmp = $this->domain_define($strXML); if (!$tmp) return $this->_set_last_error(); $this->domain_set_autostart($tmp, $config['domain']['autostart'] == 1); return $tmp; @@ -1472,9 +1505,9 @@ function domain_change_xml($domain, $xml) { if (!libvirt_domain_undefine($dom)) { return $this->_set_last_error(); } - if (!libvirt_domain_define_xml($this->conn, $xml)) { + if (!$this->domain_define($xml)) { ## Update to use new function $this->last_error = libvirt_get_last_error(); - libvirt_domain_define_xml($this->conn, $old_xml); + $this->domain_define($old_xml); return false; } return true; @@ -1637,6 +1670,67 @@ function domain_start($dom) { return $ret; } + function manage_domain_xml($domain, $xml = null, $save = true, $vm_path = null) { + // Save or delete XML in VM directory based on $save flag + // $domain is the domain name (already validated by caller) + $xml_dir = null; + $storage = "default"; + + if ($save) { + // Extract storage location from VM metadata if available + if ($xml && preg_match('/]*storage="([^"]*)"/', $xml, $matches)) { + $storage = $matches[1]; + } + + // Determine storage path + if ($storage === "default") { + // Read default storage location from domains.cfg + $domain_cfg = parse_ini_file('/boot/config/domain.cfg', true); + if (isset($domain_cfg['DOMAINDIR'])) { + $storage_path = rtrim($domain_cfg['DOMAINDIR'], '/'); + } else { + // Fallback to standard location + $storage_path = "/mnt/user/domains"; + } + } else { + // Storage is a pool name - construct pool path + $storage_path = "/mnt/$storage"; + } + + // Build full VM directory path + $xml_dir = "$storage_path/$domain"; + + // Verify directory exists + if (!is_dir($xml_dir)) { + return false; + } + + $xml_file = $xml_dir . '/' . $domain . '.xml'; + } + + if ($save === false) { + if (empty($vm_path)) { + return false; + } + $xml_file = rtrim($vm_path, '/') . '/' . $domain . '.xml'; + if (is_file($xml_file)) { + return unlink($xml_file); + } + return true; + } + + // Backup existing XML before writing new content + if (is_file($xml_file)) { + $backup_file = $xml_file . '.prev'; + @copy($xml_file, $backup_file); + } + + // Copy XML saved by libvirt + $libvirt_xml_file = '/etc/libvirt/qemu/' . $domain . '.xml'; + if (!is_file($libvirt_xml_file)) return false; + return @copy($libvirt_xml_file, $xml_file); + } + function domain_define($xml, $autostart=false) { if (strpos($xml,'') || strpos($xml,'')) { $tmp = explode("\n", $xml); @@ -1646,10 +1740,16 @@ function domain_define($xml, $autostart=false) { $xml = join("\n", $tmp); } if ($autostart) { - $tmp = libvirt_domain_create_xml($this->conn, $xml); + $tmp = libvirt_domain_create_xml($this->conn, $xml); if (!$tmp) return $this->_set_last_error(); } - $tmp = libvirt_domain_define_xml($this->conn, $xml); + $tmp = libvirt_domain_define_xml($this->conn, $xml); + if ($tmp) { + // Extract domain name from XML to save it + if (preg_match('/(.*?)<\/name>/s', $xml, $matches)) { + $this->manage_domain_xml($matches[1], $xml, true, null); + } + } return $tmp ?: $this->_set_last_error(); } @@ -1752,129 +1852,234 @@ function domain_undefine($domain) { $dom = $this->get_domain_object($domain); if (!$dom) return false; $uuid = $this->domain_get_uuid($dom); + $xml = libvirt_domain_get_xml_desc($dom, 0); + $domain_name = is_resource($dom) ? $this->domain_get_name($dom) : $domain; + $vm_path = libvirt_get_vm_path($domain_name); + if (empty($vm_path) && $xml) { + $storage = 'default'; + if (preg_match('/]*storage="([^"]*)"/', $xml, $matches)) { + $storage = $matches[1] ?: 'default'; + } + $entry = libvirt_build_vm_entry($domain_name, $storage, null, $uuid); + $vm_path = $entry['path'] ?? null; + } + $nvram_dir = libvirt_get_nvram_dir($vm_path, $domain_name); // remove OVMF VARS if this domain had them - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd')) { - unlink('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd')) { + unlink($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd'); } - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd')) { - unlink('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd')) { + unlink($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd'); + } + if ($xml) { + $this->manage_domain_xml($domain_name, $xml, false, $vm_path); } $tmp = libvirt_domain_undefine($dom); return $tmp ?: $this->_set_last_error(); } - function domain_delete($domain) { + function domain_delete($domain,$firstdiskonly=true) { $dom = $this->get_domain_object($domain); if (!$dom) return false; $disks = $this->get_disk_stats($dom); - $tmp = $this->domain_undefine($dom); + $tmp = $this->domain_undefine($domain); if (!$tmp) return $this->_set_last_error(); - // remove the first disk only - if (array_key_exists('file', $disks[0])) { - $disk = $disks[0]['file']; - $pathinfo = pathinfo($disk); - $dir = $pathinfo['dirname']; - // remove the vm config - $cfg_vm = $dir.'/'.$domain.'.cfg'; - if (is_file($cfg_vm)) unlink($cfg_vm); - $cfg = $dir.'/'.$pathinfo['filename'].'.cfg'; - $xml = $dir.'/'.$pathinfo['filename'].'.xml'; - if (is_file($disk)) unlink($disk); - if (is_file($cfg)) unlink($cfg); - if (is_file($xml)) unlink($xml); - if (is_dir($dir) && $this->is_dir_empty($dir)) { - $result= my_rmdir($dir); - if ($result['type'] == "zfs") { - qemu_log("$domain","delete empty zfs $dir {$result['rtncode']}"); - if (isset($result['dataset'])) qemu_log("$domain","dataset {$result['dataset']} "); - if (isset($result['cmd'])) qemu_log("$domain","Command {$result['cmd']} "); - if (isset($result['output'])) { - $outputlogs = implode(" ",$result['output']); - qemu_log("$domain","Output $outputlogs end"); + // Collect VM entry data before removal + $domain_name = is_resource($domain) ? $this->domain_get_name($domain) : $domain; + $vm_path = libvirt_get_vm_path($domain_name); + libvirt_remove_vms_json_entry($domain_name); + + // Remove NVRAM, snapshotdb, and related directories if empty + $nvram_dir = $vm_path . '/nvram'; + if (is_dir($nvram_dir) && count(scandir($nvram_dir)) === 2) { + my_rmdir($nvram_dir); + } + $snapshotdb_dir = $vm_path . '/snapshotdb'; + if (is_dir($snapshotdb_dir) && count(scandir($snapshotdb_dir)) === 2) { + my_rmdir($snapshotdb_dir); + } + $etc_nvram_dir = "/etc/libvirt/qemu/nvram/$domain"; + if (is_dir($etc_nvram_dir) && count(scandir($etc_nvram_dir)) === 2) { + my_rmdir($etc_nvram_dir); + } + $etc_snapshotdb_dir = "/etc/libvirt/qemu/snapshotdb/$domain"; + if (is_dir($etc_snapshotdb_dir) && count(scandir($etc_snapshotdb_dir)) === 2) { + my_rmdir($etc_snapshotdb_dir); + } + + if ($firstdiskonly) { + if (array_key_exists('file', $disks[0])) { + $disk = $disks[0]['file']; + $pathinfo = pathinfo($disk); + $dir = $pathinfo['dirname']; + // remove the vm config + $cfg_vm = $dir.'/'.$domain.'.cfg'; + if (is_file($cfg_vm)) unlink($cfg_vm); + $cfg = $dir.'/'.$pathinfo['filename'].'.cfg'; + $xml = $dir.'/'.$pathinfo['filename'].'.xml'; + if (is_file($disk)) unlink($disk); + if (is_file($cfg)) unlink($cfg); + if (is_file($xml)) unlink($xml); + # Remove NVRAM Dir/Snapshots DB + if (is_dir($dir) && $this->is_dir_empty($dir)) { + $result= my_rmdir($dir); + if ($result['type'] == "zfs") { + qemu_log("$domain","delete empty zfs $dir {$result['rtncode']}"); + if (isset($result['dataset'])) qemu_log("$domain","dataset {$result['dataset']} "); + if (isset($result['cmd'])) qemu_log("$domain","Command {$result['cmd']} "); + if (isset($result['output'])) { + $outputlogs = implode(" ",$result['output']); + qemu_log("$domain","Output $outputlogs end"); + } + } else { + qemu_log("$domain","delete empty $dir {$result['rtncode']}"); + } + } + } + } else { + #Check for files outside of the main VM directory + foreach ($disks as $disk) { + if (array_key_exists('file', $disk)) { + $disk_path = $disk['file']; + if (is_file($disk_path)) { + unlink($disk_path); + qemu_log("$domain","deleted disk $disk_path outside of VM directory"); + $disk_dir = dirname($disk_path); + if (is_dir($disk_dir) && count(scandir($disk_dir)) === 2) { + my_rmdir($disk_dir); + } + } + } + } + #REMOVE whole VM directory + if (is_dir($vm_path)) { + $files_deleted = delete_dir_contents($vm_path); + if ($files_deleted) { + $result= my_rmdir($vm_path); + if ($result['type'] == "zfs") { + qemu_log("$domain","delete empty zfs $vm_path {$result['rtncode']}"); + if (isset($result['dataset'])) qemu_log("$domain","dataset {$result['dataset']} "); + if (isset($result['cmd'])) qemu_log("$domain","Command {$result['cmd']} "); + if (isset($result['output'])) { + $outputlogs = implode(" ",$result['output']); + qemu_log("$domain","Output $outputlogs end"); + } + } else { + qemu_log("$domain","delete empty $vm_path {$result['rtncode']}"); } } else { - qemu_log("$domain","delete empty $dir {$result['rtncode']}"); + qemu_log("$domain","not deleting $vm_path not empty"); } } } return true; } - function nvram_backup($uuid) { + function nvram_backup($uuid, $vm_name = null) { + if (empty($vm_name) && !empty($uuid)) { + $vm_name = $this->domain_get_name_by_uuid($uuid) ?: null; + } + $vm_path = $vm_name ? libvirt_get_vm_path($vm_name) : null; + $nvram_dir = libvirt_get_nvram_dir($vm_path, $vm_name); // move OVMF VARS to a backup file if this domain has them - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd')) { - rename('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd', '/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd_backup'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd')) { + rename($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd', $nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd_backup'); return true; } - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd')) { - rename('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd', '/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd_backup'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd')) { + rename($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd', $nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd_backup'); return true; } return false; } - function nvram_restore($uuid) { + function nvram_restore($uuid, $vm_name = null) { + if (empty($vm_name) && !empty($uuid)) { + $vm_name = $this->domain_get_name_by_uuid($uuid) ?: null; + } + $vm_path = $vm_name ? libvirt_get_vm_path($vm_name) : null; + $nvram_dir = libvirt_get_nvram_dir($vm_path, $vm_name); // restore backup OVMF VARS if this domain had them - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd_backup')) { - rename('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd_backup', '/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd_backup')) { + rename($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd_backup', $nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd'); return true; } - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd_backup')) { - rename('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd_backup', '/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd_backup')) { + rename($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd_backup', $nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd'); return true; } return false; } - function nvram_rename($uuid, $newuuid) { + function nvram_rename($uuid, $newuuid, $vm_name = null) { + if (empty($vm_name) && !empty($uuid)) { + $vm_name = $this->domain_get_name_by_uuid($uuid) ?: null; + } + $vm_path = $vm_name ? libvirt_get_vm_path($vm_name) : null; + $nvram_dir = libvirt_get_nvram_dir($vm_path, $vm_name); // rename backup OVMF VARS if this domain had them - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd')) { - rename('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd_backup', '/etc/libvirt/qemu/nvram/'.$newuuid.'_VARS-pure-efi.fd'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd')) { + rename($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd', $nvram_dir.'/'.$newuuid.'_VARS-pure-efi.fd'); return true; } - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd')) { - rename('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd_backup', '/etc/libvirt/qemu/nvram/'.$newuuid.'_VARS-pure-efi-tpm.fd'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd')) { + rename($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd', $nvram_dir.'/'.$newuuid.'_VARS-pure-efi-tpm.fd'); return true; } return false; } - function nvram_create_snapshot($uuid, $snapshotname) { + function nvram_create_snapshot($uuid, $snapshotname, $vm_name = null) { + if (empty($vm_name) && !empty($uuid)) { + $vm_name = $this->domain_get_name_by_uuid($uuid) ?: null; + } + $vm_path = $vm_name ? libvirt_get_vm_path($vm_name) : null; + $nvram_dir = libvirt_get_nvram_dir($vm_path, $vm_name); // snapshot backup OVMF VARS if this domain had them - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd')) { - copy('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd', '/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi.fd'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd')) { + copy($nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd', $nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi.fd'); return true; } - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd')) { - copy('/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd', '/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd'); + if (is_file($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd')) { + copy($nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd', $nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd'); return true; } return false; } - function nvram_revert_snapshot($uuid, $snapshotname) { + function nvram_revert_snapshot($uuid, $snapshotname, $vm_name = null) { + if (empty($vm_name) && !empty($uuid)) { + $vm_name = $this->domain_get_name_by_uuid($uuid) ?: null; + } + $vm_path = $vm_name ? libvirt_get_vm_path($vm_name) : null; + $nvram_dir = libvirt_get_nvram_dir($vm_path, $vm_name); // snapshot backup OVMF VARS if this domain had them - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi.fd')) { - copy('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi.fd', '/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi.fd'); - unlink('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi.fd'); + if (is_file($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi.fd')) { + copy($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi.fd', $nvram_dir.'/'.$uuid.'_VARS-pure-efi.fd'); + unlink($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi.fd'); return true; } - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd')) { - copy('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd', '/etc/libvirt/qemu/nvram/'.$uuid.'_VARS-pure-efi-tpm.fd'); - unlink('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd'); + if (is_file($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd')) { + copy($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd', $nvram_dir.'/'.$uuid.'_VARS-pure-efi-tpm.fd'); + unlink($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd'); return true; } return false; } - function nvram_delete_snapshot($uuid, $snapshotname) { + function nvram_delete_snapshot($uuid, $snapshotname, $vm_name = null) { + if (empty($vm_name) && !empty($uuid)) { + $vm_name = $this->domain_get_name_by_uuid($uuid) ?: null; + } + $vm_path = $vm_name ? libvirt_get_vm_path($vm_name) : null; + $nvram_dir = libvirt_get_nvram_dir($vm_path, $vm_name); // snapshot backup OVMF VARS if this domain had them - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi.fd')) { - unlink('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi.fd'); + if (is_file($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi.fd')) { + unlink($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi.fd'); return true; } - if (is_file('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd')) { - unlink('/etc/libvirt/qemu/nvram/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd'); + if (is_file($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd')) { + unlink($nvram_dir.'/'.$uuid.$snapshotname.'_VARS-pure-efi-tpm.fd'); return true; } return false; @@ -2584,7 +2789,7 @@ function domain_change_cdrom($domain, $iso, $dev, $bus) { $tmp = libvirt_domain_update_device($domain, "", VIR_DOMAIN_DEVICE_MODIFY_CONFIG); if ($this->domain_is_active($domain)) { libvirt_domain_update_device($domain, "", VIR_DOMAIN_DEVICE_MODIFY_LIVE); - } + } ## Use new function? return $tmp ?: $this->_set_last_error(); } diff --git a/emhttp/plugins/dynamix.vm.manager/include/libvirt_helpers.php b/emhttp/plugins/dynamix.vm.manager/include/libvirt_helpers.php index 3a6a06fd08..aea0165960 100644 --- a/emhttp/plugins/dynamix.vm.manager/include/libvirt_helpers.php +++ b/emhttp/plugins/dynamix.vm.manager/include/libvirt_helpers.php @@ -12,6 +12,7 @@ */ ?> build_vm_paths($config); $xml = $lv->config_to_xml($config, true); $rtn = $lv->domain_define($xml); @@ -1872,7 +1874,10 @@ function compare_creationtimelt($a, $b) { function getvmsnapshots($vm) { $snaps=array(); - $dbpath = "/etc/libvirt/qemu/snapshotdb/$vm"; + $dbpath = libvirt_get_snapshotdb_dir(null, $vm); + if (!is_vm_newmodel()) { + $dbpath .= "/$vm"; + } $snaps_json = file_get_contents($dbpath."/snapshots.db"); $snaps = json_decode($snaps_json,true); if (is_array($snaps)) uasort($snaps,'compare_creationtime'); @@ -1881,12 +1886,25 @@ function getvmsnapshots($vm) { function write_snapshots_database($vm,$name,$state,$desc,$method="QEMU") { global $lv; - $dbpath = "/etc/libvirt/qemu/snapshotdb/$vm"; - if (!is_dir($dbpath)) mkdir($dbpath); + $dbpath = libvirt_get_snapshotdb_dir(null, $vm); + if (!is_vm_newmodel()) { + $dbpath .= "/$vm"; + } + if (!is_dir($dbpath)) { + if (!mkdir($dbpath, 0755, true) && !is_dir($dbpath)) { + // Log error and abort + error_log("Failed to create snapshotdb directory: $dbpath"); + return false; + } + } $noxml = ""; $snaps_json = file_get_contents($dbpath."/snapshots.db"); $snaps = json_decode($snaps_json,true); - $snapshot_res=$lv->domain_snapshot_lookup_by_name($vm,$name); + $snapshot_xml = @file_get_contents("/etc/libvirt/qemu/snapshot/{$vm}/{$name}.xml"); + if (empty($snapshot_xml)) { + $snapshot_xml = trim(shell_exec("virsh snapshot-dumpxml ".escapeshellarg($vm)." ".escapeshellarg($name)." 2>/dev/null")); + } + $snapshot_res = !empty($snapshot_xml); if (!$snapshot_res) { # Manual Snap no XML if ($state == "shutoff" && ($method == "ZFS" || $method == "BTRFS")) { @@ -1903,7 +1921,6 @@ function write_snapshots_database($vm,$name,$state,$desc,$method="QEMU") { $noxml = "noxml"; } } else { - $snapshot_xml=$lv->domain_snapshot_get_xml($snapshot_res); $a = simplexml_load_string($snapshot_xml); $a = json_encode($a); $b = json_decode($a, TRUE); @@ -1918,35 +1935,42 @@ function write_snapshots_database($vm,$name,$state,$desc,$method="QEMU") { } $disks =$lv->get_disk_stats($vm); - foreach($disks as $disk) { - $file = $disk["file"]; - if ($disk['device'] == "hdc" ) $primarypath = dirname(transpose_user_path($file)); - $output = array(); - exec("qemu-img info --backing-chain -U '$file' | grep image:",$output); #PHPS - foreach($output as $key => $line) { - $line=str_replace("image: ","",$line); - $output[$key] = $line; - } - - $snaps[$vmsnap]['backing'][$disk["device"]] = $output; - $rev = "r".$disk["device"]; - $reversed = array_reverse($output); - $snaps[$vmsnap]['backing'][$rev] = $reversed; + foreach($disks as $disk) { + $file = $disk["file"]; + if ($disk['device'] == "hdc" ) $primarypath = dirname(transpose_user_path($file)); + $output = array(); + exec("qemu-img info --backing-chain -U '$file' | grep image:",$output); #PHPS + foreach($output as $key => $line) { + $line=str_replace("image: ","",$line); + $output[$key] = $line; } - $snaps[$vmsnap]["primarypath"]= $primarypath; - $parentfind = $snaps[$vmsnap]['backing'][$disk["device"]]; - $parendfileinfo = pathinfo($parentfind[1]); - $snaps[$vmsnap]["parent"]= $parendfileinfo["extension"]; - $snaps[$vmsnap]["parent"] = str_replace("qcow2",'',$snaps[$vmsnap]["parent"]); - if (isset($parentfind[1]) && !isset($parentfind[2])) $snaps[$vmsnap]["parent"]="Base"; - if (isset($b)) if (array_key_exists(0 , $b["disks"]["disk"])) $snaps[$vmsnap]["disks"]= $b["disks"]["disk"]; else $snaps[$vmsnap]["disks"][0]= $b["disks"]["disk"]; + $snaps[$vmsnap]['backing'][$disk["device"]] = $output; + $rev = "r".$disk["device"]; + $reversed = array_reverse($output); + $snaps[$vmsnap]['backing'][$rev] = $reversed; + } + $snaps[$vmsnap]["primarypath"]= $primarypath; + $parentfind = $snaps[$vmsnap]['backing'][$disk["device"]]; + $parendfileinfo = pathinfo($parentfind[1]); + $snaps[$vmsnap]["parent"]= $parendfileinfo["extension"]; + $snaps[$vmsnap]["parent"] = str_replace("qcow2",'',$snaps[$vmsnap]["parent"]); + if (isset($parentfind[1]) && !isset($parentfind[2])) $snaps[$vmsnap]["parent"]="Base"; - $value = json_encode($snaps,JSON_PRETTY_PRINT); - file_put_contents($dbpath."/snapshots.db",$value); + if (isset($b)) if (array_key_exists(0 , $b["disks"]["disk"])) $snaps[$vmsnap]["disks"]= $b["disks"]["disk"]; else $snaps[$vmsnap]["disks"][0]= $b["disks"]["disk"]; + + $value = json_encode($snaps,JSON_PRETTY_PRINT); + if (!remove_empty_snapshots_db($dbpath, $snaps)) { + file_put_contents($dbpath . "/snapshots.db", $value); + } return $noxml; } - function purge_deleted_snapshots(array &$snaps){ + function purge_deleted_snapshots(array &$snaps, $vm = null) { + global $lv; + if ($vm === null) return; // Need VM name for NVRAM cleanup + $vmuuid = $lv->domain_get_uuid($vm); + $vm_path = libvirt_get_vm_path($vm); + $nvram_dir = libvirt_get_nvram_dir($vm_path, $vm); foreach ($snaps as $snapname => $snap) { $broken = false; foreach ($snap['disks'] as $disk) { @@ -1957,6 +1981,15 @@ function purge_deleted_snapshots(array &$snaps){ } } if ($broken) { + // Remove NVRAM snapshot files for this snapshot using correct path + $tpmfilename = rtrim($nvram_dir, '/') . '/' . $vmuuid . $snapname . "_VARS-pure-efi-tpm.fd"; + $nontpmfilename = rtrim($nvram_dir, '/') . '/' . $vmuuid . $snapname . "_VARS-pure-efi.fd"; + if (file_exists($tpmfilename)) { + unlink($tpmfilename); + } + if (file_exists($nontpmfilename)) { + unlink($nontpmfilename); + } unset($snaps[$snapname]); } } @@ -1964,14 +1997,23 @@ function purge_deleted_snapshots(array &$snaps){ function refresh_snapshots_database($vm,$delete_used=false) { global $lv; - $dbpath = "/etc/libvirt/qemu/snapshotdb/$vm"; - if (!is_dir($dbpath)) mkdir($dbpath); + $dbpath = libvirt_get_snapshotdb_dir(null, $vm); + if (!is_vm_newmodel()) { + $dbpath .= "/$vm"; + } + if (!is_dir($dbpath)) { + if (!mkdir($dbpath, 0755, true) && !is_dir($dbpath)) { + // Log error and abort + error_log("Failed to create snapshotdb directory: $dbpath"); + return false; + } + } $snaps_json = file_get_contents($dbpath."/snapshots.db"); $snaps = json_decode($snaps_json,true); // Only destructive operations may invalidate snapshots if ($delete_used) { - purge_deleted_snapshots($snaps); + purge_deleted_snapshots($snaps, $vm); } foreach($snaps as $vmsnap=>$snap) { @@ -1999,7 +2041,7 @@ function refresh_snapshots_database($vm,$delete_used=false) { } $value = json_encode($snaps,JSON_PRETTY_PRINT); $res = $lv->get_domain_by_name($vm); - #if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_create_snapshot($lv->domain_get_uuid($vm),$name); + #if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_create_snapshot($lv->domain_get_uuid($vm),$snap,$vm); #Remove any NVRAMs that are no longer valid. # Get uuid @@ -2018,17 +2060,38 @@ function refresh_snapshots_database($vm,$delete_used=false) { } foreach ($nvram_files as $nvram_file) unlink($nvram_file); - file_put_contents($dbpath."/snapshots.db",$value); + // Write or remove snapshots.db and directory if empty + if (!remove_empty_snapshots_db($dbpath, $snaps)) { + file_put_contents($dbpath . "/snapshots.db", $value); + } + } + + /** + * Remove snapshots.db and its directory if the database is empty. + */ + function remove_empty_snapshots_db($dbpath, $snaps) { + $dbfile = $dbpath . "/snapshots.db"; + if (empty($snaps)) { + if (file_exists($dbfile)) unlink($dbfile); + if (is_dir($dbpath) && count(scandir($dbpath)) === 2) rmdir($dbpath); + return true; + } + return false; } function delete_snapshots_database($vm,$name) { global $lv; - $dbpath = "/etc/libvirt/qemu/snapshotdb/$vm"; + $dbpath = libvirt_get_snapshotdb_dir(null, $vm); + if (!is_vm_newmodel()) { + $dbpath .= "/$vm"; + } $snaps_json = file_get_contents($dbpath."/snapshots.db"); $snaps = json_decode($snaps_json,true); unset($snaps[$name]); $value = json_encode($snaps,JSON_PRETTY_PRINT); - file_put_contents($dbpath."/snapshots.db",$value); + if (!remove_empty_snapshots_db($dbpath, $snaps)) { + file_put_contents($dbpath . "/snapshots.db", $value); + } return true; } @@ -2097,7 +2160,7 @@ function vm_snapshot($vm,$snapshotname, $snapshotdescinput, $free = "yes", $meth #Copy nvram if ($logging) qemu_log($vm,"Copy NVRAM"); - if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_create_snapshot($lv->domain_get_uuid($vm),$name); + if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_create_snapshot($lv->domain_get_uuid($vm), $name, $vm); $xmlfile = $dirpath."/".$name.".running"; if ($logging) qemu_log($vm,"Save XML if state is running current $state"); @@ -2133,7 +2196,14 @@ function vm_snapshot($vm,$snapshotname, $snapshotdescinput, $free = "yes", $meth if ($logging) qemu_log($vm,"Success write snap db"); $ret = write_snapshots_database("$vm","$name",$state,$snapshotdescinput,$method); #remove meta data - if ($ret != "noxml") $ret = $lv->domain_snapshot_delete($vm, "$name" ,2); + if ($ret != "noxml") { + exec("virsh snapshot-delete ".escapeshellarg($vm)." ".escapeshellarg($name)." --metadata 2>&1", $snapDelOut, $snapDelRtn); + // Remove snapshot dir if empty + $snapdir = "/etc/libvirt/qemu/snapshot/{$vm}"; + if (is_dir($snapdir) && count(scandir($snapdir)) === 2) { // only . and .. + rmdir($snapdir); + } + } } return $arrResponse; @@ -2250,7 +2320,7 @@ function vm_revert($vm, $snap="--current",$action="no",$actionmeta = 'yes',$dryr if (is_file($xmlfile) && $action == "yes") if (!$dryrun) unlink($xmlfile); else echo ("$xmlfile \n"); if ($logging) qemu_log($vm,"mem $memoryfile xml $xmlfile"); # Delete NVRAM - if (!empty($lv->domain_get_ovmf($res)) && $action == "yes") if (!$dryrun) if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_revert_snapshot($lv->domain_get_uuid($vm),$name); else echo "Remove old NV\n"; + if (!empty($lv->domain_get_ovmf($res)) && $action == "yes") if (!$dryrun) if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_revert_snapshot($lv->domain_get_uuid($vm), $name,$vm); else echo "Remove old NV\n"; if ($actionmeta == "yes") { if (!$dryrun) $ret = delete_snapshots_database("$vm","$name"); else echo "Old Delete snapshot meta\n"; if ($logging) qemu_log($vm,"Old Delete snapshot meta"); @@ -2305,7 +2375,7 @@ function vm_revert($vm, $snap="--current",$action="no",$actionmeta = 'yes',$dryr if ($logging) qemu_log($vm,"Delete Snapshot DB entry"); } - if (!$dryrun) if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_revert_snapshot($lv->domain_get_uuid($vm),$snap); else echo "Delete NV $vm,$snap\n"; + if (!$dryrun) if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_revert_snapshot($lv->domain_get_uuid($vm), $snap, $vm); else echo "Delete NV $vm,$snap\n"; $arrResponse = ['success' => true]; if ($dryrun) var_dump($arrResponse); @@ -2423,7 +2493,7 @@ function vm_snapremove($vm, $snap) { } # Delete NVRAM - if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_delete_snapshot($lv->domain_get_uuid($vm),$snap); + if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_delete_snapshot($lv->domain_get_uuid($vm), $snap, $vm); $ret = delete_snapshots_database("$vm","$snap") ; @@ -2518,13 +2588,13 @@ function vm_blockcommit($vm, $snap ,$path,$base,$top,$pivot,$action) { } # Delete NVRAM - #if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_delete_snapshot($lv->domain_get_uuid($vm),$snap); + if (!empty($lv->domain_get_ovmf($res))) $nvram = $lv->nvram_delete_snapshot($lv->domain_get_uuid($vm),$snap,$vm); if ($state == "shutoff") { $lv->domain_destroy($res); } refresh_snapshots_database($vm, $action=="yes" ? true : false); - $ret = $ret = delete_snapshots_database("$vm","$snap");; + $ret = delete_snapshots_database("$vm","$snap");; if($ret) $data = ["error" => "Unable to remove snap metadata $snap"]; else @@ -2585,7 +2655,6 @@ function vm_blockpull($vm, $snap ,$path,$base,$top,$pivot,$action) { $snaps_json=json_encode($snaps,JSON_PRETTY_PRINT); $pathinfo = pathinfo($file); $dirpath = $pathinfo["dirname"]; - #file_put_contents("$dirpath/image.tracker",$snaps_json); foreach($disks as $disk) { $path = $disk['file']; diff --git a/emhttp/plugins/dynamix.vm.manager/include/libvirt_paths.php b/emhttp/plugins/dynamix.vm.manager/include/libvirt_paths.php new file mode 100644 index 0000000000..fa88324396 --- /dev/null +++ b/emhttp/plugins/dynamix.vm.manager/include/libvirt_paths.php @@ -0,0 +1,159 @@ + + + $uuid, + 'storage' => $storage_name, + 'path' => $path, + 'path_shell' => $path ? escapeshellarg($path) : null, + 'exists' => $exists, + ]; +} + +function libvirt_update_vms_json_entry($vm_name, array $entry) { + $cfg = '/boot/config/plugins/dynamix.vm.manager/vms.json'; + $dir = dirname($cfg); + if (!is_dir($dir)) { + @mkdir($dir, 0755, true); + } + $vms = []; + if (file_exists($cfg)) { + $json = @json_decode(@file_get_contents($cfg), true); + if (is_array($json)) { + $vms = $json; + } + } + $vms[$vm_name] = array_filter($entry, fn($v) => $v !== null); + ksort($vms, SORT_NATURAL); + @file_put_contents($cfg, json_encode($vms, JSON_PRETTY_PRINT)); +} + +function libvirt_remove_vms_json_entry($vm_name) { + if (empty($vm_name)) { + return false; + } + $cfg = '/boot/config/plugins/dynamix.vm.manager/vms.json'; + if (!file_exists($cfg)) { + return false; + } + $json = @json_decode(@file_get_contents($cfg), true); + if (!is_array($json) || !array_key_exists($vm_name, $json)) { + return false; + } + unset($json[$vm_name]); + ksort($json, SORT_NATURAL); + @file_put_contents($cfg, json_encode($json, JSON_PRETTY_PRINT)); + return true; +} +/** + * Returns true if the new VM model is in use. + */ +function is_vm_newmodel() { + return file_exists('/boot/config/plugins/dynamix.vm.manager/vm_newmodel'); +} +?> diff --git a/emhttp/plugins/dynamix.vm.manager/javascript/vmmanager.js b/emhttp/plugins/dynamix.vm.manager/javascript/vmmanager.js index cfb750012c..46302f9a73 100644 --- a/emhttp/plugins/dynamix.vm.manager/javascript/vmmanager.js +++ b/emhttp/plugins/dynamix.vm.manager/javascript/vmmanager.js @@ -236,18 +236,32 @@ function addVMContext(name, uuid, template, state, vmrcurl, vmrcprotocol, log, f ajaxVMDispatch({action:"domain-undefine",uuid:uuid}, "loadlist"); }); }}); - opts.push({text:_("Remove VM")+" & "+_("Disks"), icon:"fa-trash", action:function(e) { + opts.push({text:_("Remove VM")+" & "+_("1st Disk only"), icon:"fa-trash", action:function(e) { e.preventDefault(); swal({ title:_("Are you sure?"), - text:_("Completely REMOVE")+" "+name+" "+_("disk image and definition"), + text:_("Completely REMOVE")+" "+name+" "+_("1st disk image and definition"), type:"warning", showCancelButton:true, confirmButtonText:_('Proceed'), cancelButtonText:_('Cancel') },function(){ $('#vm-'+uuid).find('i').removeClass('fa-play fa-square fa-pause').addClass('fa-refresh fa-spin'); - ajaxVMDispatch({action:"domain-delete",uuid:uuid}, "loadlist"); + ajaxVMDispatch({action:"domain-delete",uuid:uuid,firstdisk:true}, "loadlist"); + }); + }}); + opts.push({text:_("Remove VM")+" & "+_("All disks"), icon:"fa-trash", action:function(e) { + e.preventDefault(); + swal({ + title:_("Are you sure?"), + text:_("Completely REMOVE")+" "+name+" "+_("All disk images and definition"), + type:"warning", + showCancelButton:true, + confirmButtonText:_('Proceed'), + cancelButtonText:_('Cancel') + },function(){ + $('#vm-'+uuid).find('i').removeClass('fa-play fa-square fa-pause').addClass('fa-refresh fa-spin'); + ajaxVMDispatch({action:"domain-delete",uuid:uuid,firstdisk:false}, "loadlist"); }); }}); } diff --git a/emhttp/plugins/dynamix.vm.manager/scripts/codemirror/addon/hint/libvirt-schema.js b/emhttp/plugins/dynamix.vm.manager/scripts/codemirror/addon/hint/libvirt-schema.js index f0ae88b7a2..06221f9fc7 100644 --- a/emhttp/plugins/dynamix.vm.manager/scripts/codemirror/addon/hint/libvirt-schema.js +++ b/emhttp/plugins/dynamix.vm.manager/scripts/codemirror/addon/hint/libvirt-schema.js @@ -2,6 +2,10 @@ function getLibvirtSchema() { var root = {}; + var LIBVIRT_NVRAM_DIR = (typeof window !== "undefined" && window.LIBVIRT_NVRAM_DIR) + ? window.LIBVIRT_NVRAM_DIR + : "/etc/libvirt/qemu/nvram"; + root.domain = { "!attrs": { type: ["kvm"], @@ -167,7 +171,7 @@ function getLibvirtSchema() { "!value": "/usr/share/qemu/ovmf-x64/OVMF_CODE-pure-efi.fd" }; root.domain.os.nvram = { - "!value": "/etc/libvirt/qemu/nvram/{{UUID}}_VARS-pure-efi.fd" + "!value": LIBVIRT_NVRAM_DIR + "/{{UUID}}_VARS-pure-efi.fd" }; root.domain.features = {}; diff --git a/emhttp/plugins/dynamix.vm.manager/scripts/libvirt_init b/emhttp/plugins/dynamix.vm.manager/scripts/libvirt_init index 4b98d16f0c..32698ceb4f 100755 --- a/emhttp/plugins/dynamix.vm.manager/scripts/libvirt_init +++ b/emhttp/plugins/dynamix.vm.manager/scripts/libvirt_init @@ -5,6 +5,72 @@ # run & log functions . /etc/rc.d/rc.runlog + +# Sync domain data if IMAGE_FILE and OLD_IMAGE_FILE differ +DOMAIN_CFG=/boot/config/domain.cfg + +# Read values from domain.cfg safely (no eval) +IMAGE_FILE=$(grep -E '^IMAGE_FILE=' "$DOMAIN_CFG" | head -1 | cut -d= -f2-) +OLD_IMAGE_FILE=$(grep -E '^OLD_IMAGE_FILE=' "$DOMAIN_CFG" | head -1 | cut -d= -f2-) + +# Remove quotes +IMAGE_FILE="${IMAGE_FILE%\"}" +IMAGE_FILE="${IMAGE_FILE#\"}" +OLD_IMAGE_FILE="${OLD_IMAGE_FILE%\"}" +OLD_IMAGE_FILE="${OLD_IMAGE_FILE#\"}" + +# Proceed only if both variables are set and OLD_IMAGE_FILE exists +if [ -n "$IMAGE_FILE" ] && [ -n "$OLD_IMAGE_FILE" ] && [ "$IMAGE_FILE" != "$OLD_IMAGE_FILE" ]; then + if [ ! -e "$OLD_IMAGE_FILE" ]; then + log "OLD_IMAGE_FILE not found: $OLD_IMAGE_FILE — skipping sync" + else + log "IMAGE_FILE and OLD_IMAGE_FILE differ, syncing..." + + TMP_MNT=/etc/libvirt-sync + IMG_FILE_NAME=$(basename "$IMAGE_FILE") + OLD_IMG_FILE_NAME=$(basename "$OLD_IMAGE_FILE") + TIMESTAMP=$(date +%Y%m%d-%H%M%S) + + if [[ "$OLD_IMAGE_FILE" == *.img ]]; then + # Backup image before mounting + BACKUP_PATH="${OLD_IMAGE_FILE%.img}.bak-${TIMESTAMP}.img" + log "Creating backup of OLD_IMAGE_FILE: $BACKUP_PATH" + cp -p "$OLD_IMAGE_FILE" "$BACKUP_PATH" + + log "Mounting $OLD_IMAGE_FILE to $TMP_MNT" + mkdir -p "$TMP_MNT" + if ! mount "$OLD_IMAGE_FILE" "$TMP_MNT"; then + log "ERROR: Failed to mount $OLD_IMAGE_FILE" + rm -rf "$TMP_MNT" + exit 1 + fi + log "Copying full contents from image to directory $IMAGE_FILE" + if ! rsync -a --exclude="$OLD_IMG_FILE_NAME" "$TMP_MNT/" "$IMAGE_FILE/"; then + log "WARNING: rsync encountered errors" + fi + umount "$TMP_MNT" || log "WARNING: Failed to unmount $TMP_MNT" + rmdir "$TMP_MNT" 2>/dev/null + elif [[ "$IMAGE_FILE" == *.img ]]; then + log "Mounting $IMAGE_FILE to $TMP_MNT" + mkdir -p "$TMP_MNT" + mount "$IMAGE_FILE" "$TMP_MNT" + log "Copying full contents from directory $OLD_IMAGE_FILE to image" + rsync -a --exclude="$IMG_FILE_NAME" --exclude='*.bak-*.img' "$OLD_IMAGE_FILE/" "$TMP_MNT/" + umount "$TMP_MNT" + else + log "Both IMAGE_FILE and OLD_IMAGE_FILE are directories, copying full contents" + rsync -a --exclude="$IMG_FILE_NAME" "$OLD_IMAGE_FILE/" "$IMAGE_FILE/" + fi + + # Update OLD_IMAGE_FILE in domain.cfg + log "Updating OLD_IMAGE_FILE in $DOMAIN_CFG" + sed -i "s|^OLD_IMAGE_FILE=.*|OLD_IMAGE_FILE=\"$IMAGE_FILE\"|" "$DOMAIN_CFG" + fi +else + log "IMAGE_FILE and OLD_IMAGE_FILE match, or one is unset — skipping sync" +fi + + # missing qemu directory would indicate new libvirt image file created if [ ! -d /etc/libvirt/qemu ]; then log "initializing /etc/libvirt" @@ -36,3 +102,25 @@ if [ -s /var/log/vfio-pci-errors ]; then echo "vfio-pci bind error" > /run/libvirt/qemu/autostarted /usr/local/emhttp/webGui/scripts/notify -e "VM Autostart disabled" -s "vfio-pci-errors " -d "VM Autostart disabled due to vfio-bind error" -m "Please review /var/log/vfio-pci-errors" -i "alert" -l "/VMs" fi + +# Migrate configs to VM directories from QEMU directory +if [ ! -f /boot/config/plugins/dynamix.vm.manager/vm_newmodel ]; then + TIMESTAMP="${TIMESTAMP:-$(date +%Y%m%d-%H%M%S)}" + # Backup Image files before migration + if [[ "$IMAGE_FILE" == *.img ]] && [ -f "$IMAGE_FILE" ]; then + BACKUP_PATH="${IMAGE_FILE%.img}.bak-${TIMESTAMP}.img" + log "Creating backup of IMAGE_FILE: $BACKUP_PATH" + cp -p "$IMAGE_FILE" "$BACKUP_PATH" + else + log "Skipping IMAGE_FILE backup (not an .img file): $IMAGE_FILE" + fi + log "Disable libvirt autostart during migration" + mkdir -p /run/libvirt/qemu +fi + +# Copy XML from VM Directories to QEMU directory if new model is set. +if [ -f /boot/config/plugins/dynamix.vm.manager/vm_newmodel ]; then + log "Restoring VM configs to QEMU directory" + /usr/local/emhttp/plugins/dynamix.vm.manager/scripts/libvirtrestore +fi +# diff --git a/emhttp/plugins/dynamix.vm.manager/scripts/libvirtconfig b/emhttp/plugins/dynamix.vm.manager/scripts/libvirtconfig index 12ae126341..5d6e0f269e 100755 --- a/emhttp/plugins/dynamix.vm.manager/scripts/libvirtconfig +++ b/emhttp/plugins/dynamix.vm.manager/scripts/libvirtconfig @@ -15,7 +15,8 @@ $cfgfile = "/boot/config/domain.cfg"; $cfg_defaults = [ "SERVICE" => "disable", - "IMAGE_FILE" => "/mnt/user/system/libvirt/libvirt.img", + "IMAGE_FILE" => "/mnt/user/system/libvirt/", + "OLD_IMAGE_FILE" => "/mnt/user/system/libvirt/", "IMAGE_SIZE" => "1", "DEBUG" => "no", "DOMAINDIR" => "/mnt/user/domains/", diff --git a/emhttp/plugins/dynamix.vm.manager/scripts/libvirtcopy b/emhttp/plugins/dynamix.vm.manager/scripts/libvirtcopy new file mode 100755 index 0000000000..12304c5c86 --- /dev/null +++ b/emhttp/plugins/dynamix.vm.manager/scripts/libvirtcopy @@ -0,0 +1,133 @@ +#!/usr/bin/php + +name; + $uuid = (string)$sx->uuid; + + /* ----------------------------------------------------- + * Read storage metadata (Unraid vmtemplate) + * ----------------------------------------------------- */ + $metadata_storage = null; + + if (isset($sx->metadata)) { + // Try to get children in the 'http://unraid' namespace + $metaChildren = $sx->metadata->children('http://unraid'); + foreach ($metaChildren as $child) { + if ($child->getName() === 'vmtemplate') { + $metadata_storage = trim((string)$child['storage']); + break; + } + } + // Fallback: also check for vmtemplate in default namespace if not found + if ($metadata_storage === null) { + foreach ($sx->metadata->children() as $child) { + if ($child->getName() === 'vmtemplate') { + $metadata_storage = trim((string)$child['storage']); + break; + } + } + } + } + + /* ----------------------------------------------------- + * Store result + * ----------------------------------------------------- */ + $entry = libvirt_build_vm_entry($vm_name, $metadata_storage, $default_domain_dir, $uuid); + if ($entry !== null) { + $vms[$vm_name] = $entry; + } +} + +/* --------------------------------------------------------- + * Output + * --------------------------------------------------------- */ +#print_r($vms); +ksort($vms,SORT_NATURAL); +$json_path = "/boot/config/plugins/dynamix.vm.manager/vms.json"; +$json_dir = dirname($json_path); +if (!is_dir($json_dir)) { + if (!@mkdir($json_dir, 0755, true)) { + die("Failed to create directory: $json_dir\n"); + } +} +if (file_put_contents($json_path, json_encode($vms, JSON_PRETTY_PRINT)) === false) { + die("Failed to write vms.json\n"); +} + +file_put_contents("/tmp/Stopcopy",""); +foreach ($vms as $vm => $vmdetail) { + + $from_file = "/etc/libvirt/qemu/$vm.xml"; + $to_file = $vmdetail['path']."/$vm.xml"; + #echo " from:$from_file to:$to_file\n"; + if ($vmdetail['exists']) { + $res = copy_if_different($from_file, $to_file, false); + $msg = "$vm from:$from_file to:$to_file"; + if (!empty($res['error'])) { + $msg .= " ERROR:" . $res['error']; + } elseif (!empty($res['copied'])) { + $msg .= " COPIED"; + } elseif (!empty($res['would_copy'])) { + $msg .= " WOULD_COPY"; + } else { + $msg .= " SKIPPED_IDENTICAL"; + } + file_put_contents("/tmp/Stopcopy", $msg . "\n", FILE_APPEND); + } else file_put_contents("/tmp/Stopcopy","Nocpy $vm from:$from_file to:$to_file\n",FILE_APPEND); #echo " from:$from_file to:$to_file"; +} +?> diff --git a/emhttp/plugins/dynamix.vm.manager/scripts/libvirtmigrate b/emhttp/plugins/dynamix.vm.manager/scripts/libvirtmigrate new file mode 100755 index 0000000000..d5d56fce76 --- /dev/null +++ b/emhttp/plugins/dynamix.vm.manager/scripts/libvirtmigrate @@ -0,0 +1,568 @@ +#!/usr/bin/php -q + false, + 'error' => 'Failed to create nvram directory: ' . $nvram_dest_dir + ]; + } + } + + // Determine destination filename (preserve original) + $src_file = $valid_nvram['file']; + $dest_file = $nvram_dest_dir . '/' . basename($src_file); + + // Copy NVRAM file (compare first) + $would_copy = false; + $copied = false; + $is_snapshot = !empty($valid_nvram['is_snapshot']); + if (file_exists($dest_file)) { + $same = false; + if (filesize($src_file) === filesize($dest_file)) { + $hs = @md5_file($src_file); + $hd = @md5_file($dest_file); + if ($hs !== false && $hd !== false && $hs === $hd) { + $same = true; + $copied = true; // treat as handled for downstream cleanup + } + } + if (!$same) $would_copy = true; + } else { + $would_copy = true; + } + + if ($would_copy) { + if ($dry_run) { + // indicate would-copy in result, don't actually copy + } else { + if (!@copy($src_file, $dest_file)) { + return [ + 'success' => false, + 'error' => "Failed to copy NVRAM file from $src_file to $dest_file" + ]; + } + $copied = true; + } + } + + // For snapshot NVRAMs, only perform the file move/copy and cleanup, skip XML/define + if ($is_snapshot) { + if (!$dry_run && $copied && file_exists($src_file)) { + $deleted = @unlink($src_file); + } + return [ + 'success' => true, + 'nvram_src' => $src_file, + 'nvram_dest' => $dest_file, + 'is_snapshot' => true, + 'copied' => $copied, + 'deleted' => isset($deleted) ? $deleted : false + ]; + } + + // Update XML file (base NVRAM only) + $xml_old_path = $libvirt_location . "/qemu/$vm_name.xml"; + $xml_new_path = "$vm_path/$vm_name.xml"; + + // Read old XML + if (!file_exists($xml_old_path)) { + if ($copied) @unlink($dest_file); // Rollback only if we copied + return [ + 'success' => false, + 'error' => "XML file not found: $xml_old_path" + ]; + } + + $xml_content = file_get_contents($xml_old_path); + $xml = @simplexml_load_string($xml_content); + + if ($xml === false) { + if ($copied) @unlink($dest_file); // Rollback only if we copied + return [ + 'success' => false, + 'error' => "Failed to parse XML: $xml_old_path" + ]; + } + + // Update nvram path in XML + if (isset($xml->os->nvram)) { + $xml->os->nvram = $dest_file; + } + + // Write updated XML to new location + $xml_formatted = $xml->asXML(); + if (!$dry_run && !@file_put_contents($xml_new_path, $xml_formatted)) { + if ($copied) @unlink($dest_file); // Rollback only if we copied + return [ + 'success' => false, + 'error' => "Failed to write updated XML to: $xml_new_path" + ]; + } + + // Redefine the domain with the new XML so libvirt uses the new NVRAM path + $domain_defined = true; + if (!$dry_run) { + $domain_defined = false; + if ($xml_formatted) { + $output = null; + $retval = null; + exec("virsh define " . escapeshellarg($xml_new_path), $output, $retval); + if ($retval === 0) { + $domain_defined = true; + } else { + // Rollback: restore files if needed + if ($copied && file_exists($dest_file)) @unlink($dest_file); + // Optionally restore src_file if needed (not deleted yet) + return [ + 'success' => false, + 'error' => "Failed to redefine domain with new XML using virsh: " . implode("\n", $output) + ]; + } + } + } + + // Remove source NVRAM after successful copy, XML update, and domain redefine + if (!$dry_run && $copied && $domain_defined && file_exists($src_file)) { + $deleted = @unlink($src_file); + } + + return [ + 'success' => true, + 'nvram_src' => $src_file, + 'nvram_dest' => $dest_file, + 'xml_old_path' => $xml_old_path, + 'xml_new_path' => $xml_new_path, + 'dry_run' => $dry_run, + 'would_copy' => $would_copy, + 'copied' => $copied, + 'deleted' => $deleted + ]; +} + +/* --------------------------------------------------------- + * Perform NVRAM migration for valid files + * --------------------------------------------------------- */ +function perform_migration($valid_nvrams, $dry_run = false) { + if (empty($valid_nvrams)) { + return ['migrated' => 0, 'failed' => 0, 'errors' => []]; + } + + $vms_json = load_vms_json(); + if (empty($vms_json)) { + return [ + 'migrated' => 0, + 'failed' => count($valid_nvrams), + 'errors' => [['error' => 'vms.json not found or empty']] + ]; + } + + $migrated = 0; + $failed = 0; + $results = []; + global $libvirt_location; + $snapshot_moves = []; + $moved_snapshotdb = []; + + foreach ($valid_nvrams as $nvram_item) { + $vm_name = $nvram_item['vm_name']; + $vm_uuid = $nvram_item['uuid']; + + // Find VM in vms.json + if (!isset($vms_json[$vm_name])) { + $failed++; + $results[] = [ + 'vm_name' => $vm_name, + 'success' => false, + 'error' => "VM not found in vms.json" + ]; + continue; + } + + $vm_path = $vms_json[$vm_name]['path']; + if (empty($vm_path)) { + $failed++; + $results[] = [ + 'vm_name' => $vm_name, + 'success' => false, + 'error' => "VM path not found in vms.json" + ]; + continue; + } + + if ($dry_run) { + file_put_contents("/tmp/libvirtmigrate", "DRY RUN: migrate NVRAM for $vm_name at $vm_path\n", FILE_APPEND); + } + + // Ensure snapshotdb for this VM is moved once + if (!isset($moved_snapshotdb[$vm_name])) { + $moved_snapshotdb[$vm_name] = true; + $old_snap_dir = $libvirt_location . "/qemu/snapshotdb/" . $vm_name; + $new_snap_dir = rtrim($vm_path, '/') . "/snapshotdb"; + + // Only move snapshotdb if snapshots.db exists and is non-empty + $snap_db_file = $old_snap_dir . '/snapshots.db'; + $snap_contents = []; + if (file_exists($snap_db_file) && filesize($snap_db_file) > 0) { + $snap_contents = load_snapshot_db($vm_name); + } + + if (!empty($snap_contents)) { + if ($dry_run) { + $snapshot_moves[] = [ + 'vm_name' => $vm_name, + 'src' => $old_snap_dir, + 'dest' => $new_snap_dir, + 'would_move' => true, + 'dry_run' => true + ]; + file_put_contents("/tmp/libvirtmigrate", "DRY RUN: would move snapshotdb for $vm_name from $old_snap_dir to $new_snap_dir\n", FILE_APPEND); + } else { + // If destination exists, merge; otherwise attempt rename then fallback to copy + if (is_dir($new_snap_dir)) { + $ok = dir_copy($old_snap_dir, $new_snap_dir); + if ($ok) { + $removed = dir_remove($old_snap_dir); + $snapshot_moves[] = [ + 'vm_name' => $vm_name, + 'src' => $old_snap_dir, + 'dest' => $new_snap_dir, + 'success' => $ok && $removed, + 'action' => 'merge' + ]; + } else { + $snapshot_moves[] = [ + 'vm_name' => $vm_name, + 'src' => $old_snap_dir, + 'dest' => $new_snap_dir, + 'success' => false, + 'error' => 'Failed to merge snapshotdb into existing destination' + ]; + } + } else { + if (@rename($old_snap_dir, $new_snap_dir)) { + $snapshot_moves[] = [ + 'vm_name' => $vm_name, + 'src' => $old_snap_dir, + 'dest' => $new_snap_dir, + 'success' => true, + 'action' => 'rename' + ]; + } else { + // Fallback to copy + $ok = dir_copy($old_snap_dir, $new_snap_dir); + if ($ok) { + $removed = dir_remove($old_snap_dir); + $snapshot_moves[] = [ + 'vm_name' => $vm_name, + 'src' => $old_snap_dir, + 'dest' => $new_snap_dir, + 'success' => $ok && $removed, + 'action' => 'copy' + ]; + } else { + $snapshot_moves[] = [ + 'vm_name' => $vm_name, + 'src' => $old_snap_dir, + 'dest' => $new_snap_dir, + 'success' => false, + 'error' => 'Failed to move or copy snapshotdb' + ]; + } + } + } + } + } else { + // No snapshots present; skip moving/creating snapshotdb + $snapshot_moves[] = [ + 'vm_name' => $vm_name, + 'found' => false, + 'reason' => 'snapshots.db missing or empty' + ]; + } + } + + // Perform migration + $migration_result = migrate_nvram_file($nvram_item, $vm_path, $vm_uuid, $vm_name, $dry_run); + $migration_result['vm_name'] = $vm_name; + + if ($migration_result['success']) { + $migrated++; + } else { + $failed++; + } + + $results[] = $migration_result; + } + + return [ + 'migrated' => $migrated, + 'failed' => $failed, + 'results' => $results, + 'snapshotdb_moves' => $snapshot_moves + ]; +} + +/* --------------------------------------------------------- + * Load snapshot database for a VM + * --------------------------------------------------------- */ +function load_snapshot_db($vm_name) { + global $libvirt_location; + $snap_db = $libvirt_location . "/qemu/snapshotdb/" . $vm_name . "/snapshots.db"; + if (!file_exists($snap_db)) { + return []; + } + + $json = @json_decode(file_get_contents($snap_db), true); + return is_array($json) ? $json : []; +} + +/* --------------------------------------------------------- + * Validate NVRAM files against libvirt VM UUIDs and snapshots + * Returns array with 'valid' and 'orphaned' keys + * --------------------------------------------------------- */ +function validate_nvram_uuids() { + global $libvirt_location; + $log_file = "/tmp/libvirtmigrate"; + file_put_contents($log_file, "validate_nvram_uuids: start\n", FILE_APPEND); + // Connect to libvirt + $lv = libvirt_connect('qemu:///system', false); + if (!$lv) { + die("ERROR: Failed to connect to libvirt\n"); + } + + // Get all valid VM UUIDs + $domains = libvirt_list_domains($lv); + if ($domains === false) { + die("ERROR: Failed to list domains\n"); + } + + $valid_uuids = []; + $snapshot_dbs = []; + file_put_contents($log_file, "validate_nvram_uuids: domains=" . count($domains) . "\n", FILE_APPEND); + + foreach ($domains as $dom) { + $domget = libvirt_domain_lookup_by_name($lv, $dom); + if ($domget === false) continue; + + // Use the libvirt function to get UUID string directly + $uuid = @libvirt_domain_get_uuid_string($domget); + if ($uuid) { + $valid_uuids[$uuid] = $dom; + // Preload snapshot database for this VM + $snapshot_dbs[$dom] = load_snapshot_db($dom); + } + } + + // Scan NVRAM directory + $nvram_dir = $libvirt_location . "/qemu/nvram"; + if (!is_dir($nvram_dir)) { + file_put_contents($log_file, "validate_nvram_uuids: nvram dir missing: $nvram_dir\n", FILE_APPEND); + return ['valid' => [], 'orphaned' => []]; + } + + $nvram_files = glob("$nvram_dir/*"); + if ($nvram_files === false || count($nvram_files) === 0) { + file_put_contents($log_file, "validate_nvram_uuids: no nvram files found\n", FILE_APPEND); + return ['valid' => [], 'orphaned' => []]; + } + + $valid = []; + $orphaned = []; + $valid_count = 0; + $orphaned_count = 0; + + foreach ($nvram_files as $file) { + $basename = basename($file); + + // Extract UUID and optional snapshot name from filename + // Regular: {UUID}_VARS-pure-efi.fd + // Snapshot: {UUID}S{snapshot_name}_VARS-pure-efi.fd (snapshot name includes leading 'S') + if (preg_match('/^([a-f0-9\-]+)(S.+?)?_VARS/', $basename, $matches)) { + $uuid = $matches[1]; + $snapshot_name = isset($matches[2]) ? $matches[2] : null; + + if (isset($valid_uuids[$uuid])) { + $vm_name = $valid_uuids[$uuid]; + $is_snapshot = $snapshot_name !== null; + $snapshot_valid = true; + + // If it's a snapshot, validate against snapshots.db + if ($is_snapshot) { + $snapshots = $snapshot_dbs[$vm_name] ?? []; + $snapshot_valid = isset($snapshots[$snapshot_name]); + file_put_contents( + $log_file, + "validate_nvram_uuids: snapshot lookup vm=$vm_name name=$snapshot_name valid=" . ($snapshot_valid ? 'yes' : 'no') . "\n", + FILE_APPEND + ); + } + + if ($snapshot_valid) { + $valid[] = [ + 'file' => $file, + 'basename' => $basename, + 'uuid' => $uuid, + 'vm_name' => $vm_name, + 'snapshot_name' => $snapshot_name, + 'is_snapshot' => $is_snapshot, + 'size' => filesize($file) + ]; + $valid_count++; + } else { + $orphaned[] = [ + 'file' => $file, + 'basename' => $basename, + 'uuid' => $uuid, + 'vm_name' => $vm_name, + 'snapshot_name' => $snapshot_name, + 'is_snapshot' => true, + 'size' => filesize($file), + 'reason' => 'snapshot not found in snapshots.db' + ]; + $orphaned_count++; + } + } else { + $orphaned[] = [ + 'file' => $file, + 'basename' => $basename, + 'uuid' => $uuid, + 'snapshot_name' => $snapshot_name, + 'is_snapshot' => $snapshot_name !== null, + 'size' => filesize($file), + 'reason' => 'VM not found' + ]; + $orphaned_count++; + } + } + } + + file_put_contents($log_file, "validate_nvram_uuids: valid=$valid_count orphaned=$orphaned_count\n", FILE_APPEND); + + return ['valid' => $valid, 'orphaned' => $orphaned]; +} + +/* --------------------------------------------------------- + * Delete orphaned NVRAM files + * --------------------------------------------------------- */ +function delete_orphaned_files($orphaned_files, $dry_run = false) { + if (empty($orphaned_files)) { + return ['deleted' => 0, 'failed' => 0, 'errors' => []]; + } + + $deleted = 0; + $failed = 0; + $errors = []; + + foreach ($orphaned_files as $item) { + if (file_exists($item['file'])) { + if ($dry_run) { + // In dry-run mode, just count as would-be deleted + $deleted++; + file_put_contents("/tmp/libvirtmigrate", "DRY RUN: would delete orphaned NVRAM {$item['file']}\n", FILE_APPEND); + } elseif (@unlink($item['file'])) { + $deleted++; + } else { + $failed++; + $errors[] = [ + 'file' => $item['file'], + 'error' => 'Failed to delete' + ]; + } + } else { + $failed++; + $errors[] = [ + 'file' => $item['file'], + 'error' => 'File not found' + ]; + } + } + + return ['deleted' => $deleted, 'failed' => $failed, 'errors' => $errors, 'dry_run' => $dry_run]; +} + +// Parse command line arguments +$delete_flag = in_array('--delete', $argv) || in_array('-d', $argv); +$migrate_flag = in_array('--migrate', $argv) || in_array('-m', $argv); +$valid_only = in_array('--valid-only', $argv) || in_array('-v', $argv); +$orphaned_only = in_array('--orphaned-only', $argv) || in_array('-o', $argv); +$confirm = in_array('--confirm', $argv) || in_array('-y', $argv); +$dry_run = !$confirm; // Default to dry-run unless --confirm is set + +// Run validation and output results +$result = validate_nvram_uuids(); + +// Build output based on filters +if ($valid_only) { + $output = ['valid' => $result['valid']]; +} elseif ($orphaned_only) { + $output = ['orphaned' => $result['orphaned']]; +} else { + $output = [ + 'valid' => $result['valid'], + 'orphaned' => $result['orphaned'] + ]; +} +// Delete orphaned files if flag is set +if ($delete_flag && !empty($result['orphaned'])) { + $output['deletion_result'] = delete_orphaned_files($result['orphaned'], $dry_run); +} + +// Migrate valid NVRAM files if flag is set +if ($migrate_flag && !empty($result['valid'])) { + $output['migration_result'] = perform_migration($result['valid'], $dry_run); +} + +// Add dry-run flag to output if set +if ($dry_run) { + $output['dry_run'] = true; +} + +echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + +exit(count($result['orphaned']) === 0 ? 0 : 1); + +?> diff --git a/emhttp/plugins/dynamix.vm.manager/scripts/libvirtrestore b/emhttp/plugins/dynamix.vm.manager/scripts/libvirtrestore new file mode 100755 index 0000000000..d1727114df --- /dev/null +++ b/emhttp/plugins/dynamix.vm.manager/scripts/libvirtrestore @@ -0,0 +1,57 @@ +#!/usr/bin/php + + $vmdetail) { + $to_file = "/etc/libvirt/qemu/$vm.xml"; + $from_file = $vmdetail['path']."/$vm.xml"; + #echo " from:$from_file to:$to_file"; + if (file_exists($from_file)) { + $res = copy_if_different($from_file, $to_file, false); + $msg = "$vm from:$from_file to:$to_file"; + if (!empty($res['error'])) { + $msg .= " ERROR:" . $res['error']; + } elseif (!empty($res['copied'])) { + $msg .= " COPIED"; + } elseif (!empty($res['would_copy'])) { + $msg .= " WOULD_COPY"; + } else { + $msg .= " SKIPPED_IDENTICAL"; + } + file_put_contents("/tmp/libvirtrestore", $msg . "\n", FILE_APPEND); + } else { + file_put_contents("/tmp/libvirtrestore","Nocpy $vm from:$from_file to:$to_file\n",FILE_APPEND); + } +} +?> diff --git a/emhttp/plugins/dynamix.vm.manager/scripts/savehook.php b/emhttp/plugins/dynamix.vm.manager/scripts/savehook.php new file mode 100644 index 0000000000..8daa3d207b --- /dev/null +++ b/emhttp/plugins/dynamix.vm.manager/scripts/savehook.php @@ -0,0 +1,28 @@ +#!/usr/bin/env php + diff --git a/emhttp/plugins/dynamix.vm.manager/templates/Custom.form.php b/emhttp/plugins/dynamix.vm.manager/templates/Custom.form.php index 2b4db8e265..3c9330226f 100755 --- a/emhttp/plugins/dynamix.vm.manager/templates/Custom.form.php +++ b/emhttp/plugins/dynamix.vm.manager/templates/Custom.form.php @@ -154,7 +154,8 @@ } else { // form view #file_put_contents("/tmp/createpost",json_encode($_POST)); - if ($lv->domain_new($_POST)) { + $postConfig = $lv->build_vm_paths($_POST); + if ($lv->domain_new($postConfig)) { // Fire off the vnc/spice popup if available $dom = $lv->get_domain_by_name($_POST['domain']['name']); $vmrcport = $lv->domain_get_vnc_port($dom); @@ -258,10 +259,11 @@ // form view if (($error = create_vdisk($_POST)) === false) { $arrExistingConfig = custom::createArray('domain',$strXML); - $arrUpdatedConfig = custom::createArray('domain',$lv->config_to_xml($_POST)); + $postConfig = $lv->build_vm_paths($_POST); + $arrUpdatedConfig = custom::createArray('domain',$lv->config_to_xml($postConfig)); if ($debug) { file_put_contents("/tmp/vmdebug_exist",$strXML); - file_put_contents("/tmp/vmdebug_new",$lv->config_to_xml($_POST)); + file_put_contents("/tmp/vmdebug_new",$lv->config_to_xml($postConfig)); file_put_contents("/tmp/vmdebug_arrayN",json_encode($arrUpdatedConfig,JSON_PRETTY_PRINT)); file_put_contents("/tmp/vmdebug_arrayE",json_encode($arrExistingConfig,JSON_PRETTY_PRINT)); } @@ -309,6 +311,7 @@ $boolNew = true; $arrConfig = $arrConfigDefaults; $arrVMUSBs = getVMUSBs($strXML); + $arrConfig['domain']['defer_write'] = true; $strXML = $lv->config_to_xml($arrConfig); $domXML = new DOMDocument(); $domXML->preserveWhiteSpace = false; diff --git a/emhttp/plugins/dynamix/include/Helpers.php b/emhttp/plugins/dynamix/include/Helpers.php index 12844a4b9c..7a6240ca37 100644 --- a/emhttp/plugins/dynamix/include/Helpers.php +++ b/emhttp/plugins/dynamix/include/Helpers.php @@ -506,6 +506,29 @@ function my_rmdir($dirname) { return($return); } +/** + * Recursively delete all contents of a directory, but not the directory itself. + * Returns true on success, false on failure. + */ +function delete_dir_contents($dir) { + if (!is_dir($dir)) return false; + $success = true; + $items = scandir($dir); + if ($items === false) return false; + foreach ($items as $item) { + if ($item === '.' || $item === '..') continue; + $path = "$dir/$item"; + if (is_link($path)) { + $success = unlink($path) && $success; + } elseif (is_dir($path)) { + $success = delete_dir_contents($path) && rmdir($path) && $success; + } else { + $success = unlink($path) && $success; + } + } + return $success; +} + function get_realvolume($path) { if (strpos($path,"/mnt/user/",0) === 0) $reallocation = trim(shell_exec("getfattr --absolute-names --only-values -n system.LOCATION ".escapeshellarg($path)." 2>/dev/null")); diff --git a/etc/libvirt/paths.conf b/etc/libvirt/paths.conf new file mode 100644 index 0000000000..2f30576c38 --- /dev/null +++ b/etc/libvirt/paths.conf @@ -0,0 +1,4 @@ +# Libvirt path overrides +LIBVIRT_QEMU_DIR="/etc/libvirt/qemu" +LIBVIRT_NVRAM_DIR="/etc/libvirt/qemu/nvram" +LIBVIRT_SNAPSHOTDB_DIR="/etc/libvirt/qemu/snapshotdb" diff --git a/etc/rc.d/rc.libvirt b/etc/rc.d/rc.libvirt index ae1c88e7b7..94f3d8284d 100755 --- a/etc/rc.d/rc.libvirt +++ b/etc/rc.d/rc.libvirt @@ -34,6 +34,14 @@ VIRTLOGD_OPTS=${VIRTLOGD_OPTS:-" -f /etc/libvirt/virtlogd.conf -p $VIRTLOGD_PIDF VIRTLOCKD_PIDFILE="/var/run/libvirt/virtlockd.pid" VIRTLOCKD_OPTS=${VIRTLOCKD_OPTS:-" -f /etc/libvirt/virtlockd.conf -p $VIRTLOCKD_PIDFILE "} +# libvirt path configuration +LIBVIRT_QEMU_DIR="/etc/libvirt/qemu" +LIBVIRT_SNAPSHOTDB_DIR="$LIBVIRT_QEMU_DIR/snapshotdb" +if [[ -r /etc/rc.d/rc.libvirt.conf ]]; then + # shellcheck disable=SC1091 + . /etc/rc.d/rc.libvirt.conf +fi + BOOT_DOMAIN="/boot/config/domain.cfg" SYSTEM="/sys/class/net" VIRTLOG="virtlog daemon" @@ -233,7 +241,9 @@ libvirtd_start(){ mkdir -p /etc/libvirt/qemu/swtpm/tpm-states # setup snapshot persistance. mkdir -p /etc/libvirt/qemu/snapshot - mkdir -p /etc/libvirt/qemu/snapshotdb + if [[ ! -f /boot/config/plugins/dynamix.vm.manager/vm_newmodel ]]; then + mkdir -p "$LIBVIRT_SNAPSHOTDB_DIR" + fi rm -rf /var/lib/libvirt/qemu/snapshot ln -sf /etc/libvirt/qemu/snapshot /var/lib/libvirt/qemu/snapshot # create directory for pid file @@ -244,9 +254,21 @@ libvirtd_start(){ echo 0 > /sys/module/kvm/parameters/report_ignored_msrs libvirtd -d -l $LIBVIRTD_OPTS log "$DAEMON... Started." + + if [ ! -f /boot/config/plugins/dynamix.vm.manager/vm_newmodel ]; then + libvirt_migrate + fi } libvirtd_stop(){ + # Save VM locations + LIBVIRTCOPY="/usr/local/emhttp/plugins/dynamix.vm.manager/scripts/libvirtcopy" + if [[ -x "$LIBVIRTCOPY" ]]; then + "$LIBVIRTCOPY" || log "Warning: Failed to save VM locations" + else + log "Warning: libvirtcopy script not found or not executable" + fi + log "Stopping $DAEMON..." if [[ ! -f $LIBVIRTD_PIDFILE ]]; then log "$DAEMON... Already stopped." @@ -321,6 +343,17 @@ libvirtd_cleanup(){ sleep 1 } +libvirt_migrate(){ + log "Copying VM data to VM directories" + /usr/local/emhttp/plugins/dynamix.vm.manager/scripts/libvirtcopy + log "Migrating VM configs to new model" + /usr/local/emhttp/plugins/dynamix.vm.manager/scripts/libvirtmigrate --migrate --confirm > /boot/config/plugins/dynamix.vm.manager/libvirtmigrate.log 2>&1 + touch /boot/config/plugins/dynamix.vm.manager/vm_newmodel + rm -r /run/libvirt/qemu + libvirtd_stop + libvirtd_start +} + case "$1" in 'test') libvirtd_test diff --git a/etc/rc.d/rc.libvirt.conf b/etc/rc.d/rc.libvirt.conf new file mode 100644 index 0000000000..2f30576c38 --- /dev/null +++ b/etc/rc.d/rc.libvirt.conf @@ -0,0 +1,4 @@ +# Libvirt path overrides +LIBVIRT_QEMU_DIR="/etc/libvirt/qemu" +LIBVIRT_NVRAM_DIR="/etc/libvirt/qemu/nvram" +LIBVIRT_SNAPSHOTDB_DIR="/etc/libvirt/qemu/snapshotdb"