$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/plugins/dynamix.vm.manager/include/libvirt_helpers.php";
$strXML = '';
$strUUID = '';
$boolRunning = false;
// If we are editing a existing VM load it's existing configuration details
if (!empty($_GET['uuid'])) {
$strUUID = $_GET['uuid'];
$res = $lv->domain_get_name_by_uuid($strUUID);
$dom = $lv->domain_get_info($res);
$strXML = $lv->domain_get_xml($res);
$boolRunning = ($lv->domain_state_translate($dom['state']) == 'running');
}
if (array_key_exists('createvm', $_POST)) {
$tmp = $lv->domain_define($_POST['xmldesc'], !empty($config['domain']['startnow']));
if (!$tmp){
$arrResponse = ['error' => $lv->get_last_error()];
} else {
$lv->domain_set_autostart($tmp, $_POST['domain']['autostart'] == 1);
$arrResponse = ['success' => true];
}
echo json_encode($arrResponse);
exit;
}
if (array_key_exists('updatevm', $_POST)) {
// Backup xml for existing domain in ram
$strOldXML = '';
$boolOldAutoStart = false;
$dom = $lv->domain_get_domain_by_uuid($_POST['domain']['uuid']);
if ($dom) {
$strOldXML = $lv->domain_get_xml($dom);
$boolOldAutoStart = $lv->domain_get_autostart($dom);
}
// Remove existing domain
$lv->nvram_backup($_POST['domain']['uuid']);
$lv->domain_undefine($dom);
$lv->nvram_restore($_POST['domain']['uuid']);
// Save new domain
$tmp = $lv->domain_define($_POST['xmldesc']);
if (!$tmp){
$strLastError = $lv->get_last_error();
// Failure -- try to restore existing domain
$tmp = $lv->domain_define($strOldXML);
if ($tmp) $lv->domain_set_autostart($tmp, $boolOldAutoStart);
$arrResponse = ['error' => $strLastError];
} else {
$lv->domain_set_autostart($tmp, $_POST['domain']['autostart'] == 1);
$arrResponse = ['success' => true];
}
echo json_encode($arrResponse);
exit;
}
?>
if (!$boolRunning) { ?>
if (!empty($strXML)) { ?>
} else { ?>
} ?>
Manual XML edits may be lost if you later edit with the GUI editor.
} else { ?>
} ?>