From d23e731ec6fa703e0518dea070fb915a83c14f60 Mon Sep 17 00:00:00 2001
From: SimonFair <39065407+SimonFair@users.noreply.github.com>
Date: Sun, 18 Dec 2022 12:03:35 +0000
Subject: [PATCH 1/5] Update Memory Backup processing forVirtiofs.
---
.../dynamix.vm.manager/include/libvirt.php | 14 +-
.../include/libvirt_helpers.php | 162 +++++++++++++++++-
.../templates/Custom.form.php | 4 +-
3 files changed, 169 insertions(+), 11 deletions(-)
diff --git a/plugins/dynamix.vm.manager/include/libvirt.php b/plugins/dynamix.vm.manager/include/libvirt.php
index 1652ca6dc..dcdd1bd94 100644
--- a/plugins/dynamix.vm.manager/include/libvirt.php
+++ b/plugins/dynamix.vm.manager/include/libvirt.php
@@ -685,9 +685,7 @@
}
$sharestr = '';
- $memorybacking ="
-
- " ;
+ $memorybacking = json_decode($domain['memoryBacking'],true);
if (!empty($shares)) {
foreach ($shares as $i => $share) {
@@ -696,10 +694,8 @@
}
if ($share['mode'] == "virtiofs") {
- $memorybacking = "
-
-
- " ;
+ if (!isset($memorybacking['source'])) $memorybacking['source']["@attributes"]["type"] = "memfd" ;
+ if (!isset($memorybacking['access'])) $memorybacking['access']["@attributes"]["mode"] = "shared" ;
$sharestr .= "
@@ -867,6 +863,8 @@
";
}
#$osbootdev = "" ;
+ $memorybackingXML = Array2XML::createXML('memoryBacking', $memorybacking);
+ $memoryBackingXML = $memorybackingXML->saveXML($memorybackingXML->documentElement);
return "
$uuid
$name
@@ -875,7 +873,7 @@
$mem
$maxmem
$cpustr
- $memorybacking
+ $memoryBackingXML
$loader
hvm
diff --git a/plugins/dynamix.vm.manager/include/libvirt_helpers.php b/plugins/dynamix.vm.manager/include/libvirt_helpers.php
index 863f25344..5025e68b0 100644
--- a/plugins/dynamix.vm.manager/include/libvirt_helpers.php
+++ b/plugins/dynamix.vm.manager/include/libvirt_helpers.php
@@ -12,6 +12,155 @@
*/
?>
+/**
+ * Array2XML: A class to convert array in PHP to XML
+ * It also takes into account attributes names unlike SimpleXML in PHP
+ * It returns the XML in form of DOMDocument class for further manipulation.
+ * It throws exception if the tag name or attribute name has illegal chars.
+ *
+ * Author : Lalit Patel
+ * Website: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes
+ * License: Apache License 2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Version: 0.1 (10 July 2011)
+ * Version: 0.2 (16 August 2011)
+ * - replaced htmlentities() with htmlspecialchars() (Thanks to Liel Dulev)
+ * - fixed a edge case where root node has a false/null/0 value. (Thanks to Liel Dulev)
+ * Version: 0.3 (22 August 2011)
+ * - fixed tag sanitize regex which didn't allow tagnames with single character.
+ * Version: 0.4 (18 September 2011)
+ * - Added support for CDATA section using @cdata instead of @value.
+ * Version: 0.5 (07 December 2011)
+ * - Changed logic to check numeric array indices not starting from 0.
+ * Version: 0.6 (04 March 2012)
+ * - Code now doesn't @cdata to be placed in an empty array
+ * Version: 0.7 (24 March 2012)
+ * - Reverted to version 0.5
+ * Version: 0.8 (02 May 2012)
+ * - Removed htmlspecialchars() before adding to text node or attributes.
+ *
+ * Usage:
+ * $xml = Array2XML::createXML('root_node_name', $php_array);
+ * echo $xml->saveXML();
+ */
+class Array2XML {
+ private static $xml = null;
+private static $encoding = 'UTF-8';
+ /**
+ * Initialize the root XML node [optional]
+ * @param $version
+ * @param $encoding
+ * @param $format_output
+ */
+ public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
+ self::$xml = new DomDocument($version, $encoding);
+ self::$xml->formatOutput = $format_output;
+ self::$encoding = $encoding;
+ }
+ /**
+ * Convert an Array to XML
+ * @param string $node_name - name of the root node to be converted
+ * @param array $arr - aray to be converterd
+ * @return DomDocument
+ */
+ public static function &createXML($node_name, $arr=array()) {
+ $xml = self::getXMLRoot();
+ $xml->appendChild(self::convert($node_name, $arr));
+ self::$xml = null; // clear the xml node in the class for 2nd time use.
+ return $xml;
+ }
+ /**
+ * Convert an Array to XML
+ * @param string $node_name - name of the root node to be converted
+ * @param array $arr - aray to be converterd
+ * @return DOMNode
+ */
+ private static function &convert($node_name, $arr=array()) {
+ //print_arr($node_name);
+ $xml = self::getXMLRoot();
+ $node = $xml->createElement($node_name);
+ if(is_array($arr)){
+ // get the attributes first.;
+ if(isset($arr['@attributes'])) {
+ foreach($arr['@attributes'] as $key => $value) {
+ if(!self::isValidTagName($key)) {
+ throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name);
+ }
+ $node->setAttribute($key, self::bool2str($value));
+ }
+ unset($arr['@attributes']); //remove the key from the array once done.
+ }
+ // check if it has a value stored in @value, if yes store the value and return
+ // else check if its directly stored as string
+ if(isset($arr['@value'])) {
+ $node->appendChild($xml->createTextNode(self::bool2str($arr['@value'])));
+ unset($arr['@value']); //remove the key from the array once done.
+ //return from recursion, as a note with value cannot have child nodes.
+ return $node;
+ } else if(isset($arr['@cdata'])) {
+ $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
+ unset($arr['@cdata']); //remove the key from the array once done.
+ //return from recursion, as a note with cdata cannot have child nodes.
+ return $node;
+ }
+ }
+ //create subnodes using recursion
+ if(is_array($arr)){
+ // recurse to get the node for that key
+ foreach($arr as $key=>$value){
+ if(!self::isValidTagName($key)) {
+ throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name);
+ }
+ if(is_array($value) && is_numeric(key($value))) {
+ // MORE THAN ONE NODE OF ITS KIND;
+ // if the new array is numeric index, means it is array of nodes of the same kind
+ // it should follow the parent key name
+ foreach($value as $k=>$v){
+ $node->appendChild(self::convert($key, $v));
+ }
+ } else {
+ // ONLY ONE NODE OF ITS KIND
+ $node->appendChild(self::convert($key, $value));
+ }
+ unset($arr[$key]); //remove the key from the array once done.
+ }
+ }
+ // after we are done with all the keys in the array (if it is one)
+ // we check if it has any text value, if yes, append it.
+ if(!is_array($arr)) {
+ $node->appendChild($xml->createTextNode(self::bool2str($arr ?? "")));
+ }
+ return $node;
+ }
+ /*
+ * Get the root XML node, if there isn't one, create it.
+ */
+ private static function getXMLRoot(){
+ if(empty(self::$xml)) {
+ self::init();
+ }
+ return self::$xml;
+ }
+ /*
+ * Get string representation of boolean value
+ */
+ private static function bool2str($v){
+ //convert boolean to text value.
+ $v = $v === true ? 'true' : $v;
+ $v = $v === false ? 'false' : $v;
+ return $v;
+ }
+ /*
+ * Check if the tag name or attribute name contains illegal characters
+ * Ref: http://www.w3.org/TR/xml/#sec-common-syn
+ */
+ private static function isValidTagName($tag){
+ $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
+ return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;
+ }
+}
+
+
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "$docroot/plugins/dynamix.vm.manager/include/libvirt.php";
@@ -1117,7 +1266,8 @@
'state' => $lv->domain_state_translate($dom['state']),
'ovmf' => $strOVMF,
'usbboot' => $osbootdev,
- 'usbmode' => $strUSBMode
+ 'usbmode' => $strUSBMode,
+ 'memoryBacking' => getmemoryBacking($res)
],
'media' => [
'cdrom' => (!empty($medias) && !empty($medias[0]) && array_key_exists('file', $medias[0])) ? $medias[0]['file'] : '',
@@ -1181,7 +1331,7 @@
foreach ($old['devices']['disk'] as $k => $d) if ($source==$d['source']['@attributes']['file']) $new['devices']['disk'][$key]['driver']['@attributes'] = $d['driver']['@attributes'];
}
// settings not in the GUI, but maybe customized
- unset($new['memoryBacking'], $new['clock']);
+ unset($new['clock']);
// preserve vnc/spice port settings
// unset($new['devices']['graphics']['@attributes']['port'],$new['devices']['graphics']['@attributes']['autoport']);
if (!$new['devices']['graphics']) unset($old['devices']['graphics']);
@@ -1271,4 +1421,12 @@
$data = $data[0]->state ;
return $data ;
}
+
+ function getmemoryBacking($res) {
+ global $lv ;
+ $xml = $lv->domain_get_xml($res) ;
+ $memoryBacking = new SimpleXMLElement($xml);
+ $memorybacking = $memoryBacking->memoryBacking ;
+ return json_encode($memorybacking); ;
+ }
?>
diff --git a/plugins/dynamix.vm.manager/templates/Custom.form.php b/plugins/dynamix.vm.manager/templates/Custom.form.php
index 53ad7f90d..1b872d744 100644
--- a/plugins/dynamix.vm.manager/templates/Custom.form.php
+++ b/plugins/dynamix.vm.manager/templates/Custom.form.php
@@ -57,7 +57,8 @@
'vcpu' => [0],
'hyperv' => 1,
'ovmf' => 1,
- 'usbmode' => 'usb2'
+ 'usbmode' => 'usb2',
+ 'memoryBacking' => '{"nosharepages":{}}'
],
'media' => [
'cdrom' => '',
@@ -286,6 +287,7 @@
+
From ef5997906c0bd99897bf07c04d9035ac97a05993 Mon Sep 17 00:00:00 2001
From: Eric Schultz
Date: Mon, 19 Dec 2022 10:14:18 -0700
Subject: [PATCH 2/5] additional php8 fixes
---
plugins/dynamix.my.servers/include/myservers2.php | 4 ++--
plugins/dynamix.my.servers/include/state.php | 2 +-
plugins/dynamix/include/UpdateDNS.php | 8 ++++----
3 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/plugins/dynamix.my.servers/include/myservers2.php b/plugins/dynamix.my.servers/include/myservers2.php
index f8043ca32..e29326baa 100644
--- a/plugins/dynamix.my.servers/include/myservers2.php
+++ b/plugins/dynamix.my.servers/include/myservers2.php
@@ -611,12 +611,12 @@
banner="=$display['banner'] ?? ''?>"
bgcolor="=($backgnd) ? '#'.$backgnd : ''?>"
csrf="=$var['csrf_token']?>"
- displaydesc="=($display['headerdescription']!='no') ? 'true' : ''?>"
+ displaydesc="=($display['headerdescription']??''!='no') ? 'true' : ''?>"
expiretime="=1000*($var['regTy']=='Trial'||strstr($var['regTy'],'expired')?$var['regTm2']:0)?>"
hide-my-servers="=$plgInstalled ? '' : 'yes' ?>"
locale="=($_SESSION['locale']) ? $_SESSION['locale'] : 'en_US'?>"
locale-messages="=rawurlencode(json_encode($upc_translations, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE))?>"
- metacolor="=($display['headermetacolor']) ? '#'.$display['headermetacolor'] : ''?>"
+ metacolor="=($display['headermetacolor']??'') ? '#'.$display['headermetacolor'] : ''?>"
plg-path="dynamix.my.servers"
reg-wiz-time="=$remote['regWizTime'] ?? ''?>"
serverdesc="=$var['COMMENT']?>"
diff --git a/plugins/dynamix.my.servers/include/state.php b/plugins/dynamix.my.servers/include/state.php
index d6419609e..7b752c99e 100644
--- a/plugins/dynamix.my.servers/include/state.php
+++ b/plugins/dynamix.my.servers/include/state.php
@@ -57,7 +57,7 @@ $arr['internalip'] = $_SERVER['SERVER_ADDR'];
$arr['internalport'] = $_SERVER['SERVER_PORT'];
$arr['plgVersion'] = 'base-'.$var['version'];
$arr['protocol'] = $_SERVER['REQUEST_SCHEME'];
-$arr['locale'] = $_SESSION['locale'] ? $_SESSION['locale'] : 'en_US';
+$arr['locale'] = $_SESSION['locale'] ?? 'en_US';
$arr['expiretime']=1000*($var['regTy']=='Trial'||strstr($var['regTy'],'expired')?$var['regTm2']:0);
$arr['uptime']=1000*(time() - round(strtok(exec("cat /proc/uptime"),' ')));
$arr['config'] = [
diff --git a/plugins/dynamix/include/UpdateDNS.php b/plugins/dynamix/include/UpdateDNS.php
index e2197ae46..e5a9f696a 100644
--- a/plugins/dynamix/include/UpdateDNS.php
+++ b/plugins/dynamix/include/UpdateDNS.php
@@ -39,7 +39,7 @@ if (!function_exists('ipaddr')) {
function host_lookup_ip($host) {
$result = @dns_get_record($host, DNS_A);
- $ip = ($result) ? $result[0]['ip'] : '';
+ $ip = ($result) ? $result[0]['ip']??'' : '';
return($ip);
}
function rebindDisabled() {
@@ -191,11 +191,11 @@ function response_complete($httpcode, $result, $cli_success_msg='') {
$cli = php_sapi_name()=='cli';
$verbose = $anon = false;
-if ($cli && $argv[1] == "-v") {
+if ($cli && ($argc > 1) && $argv[1] == "-v") {
$verbose = true;
$anon = true;
}
-if ($cli && $argv[1] == "-vv") {
+if ($cli && ($argc > 1) && $argv[1] == "-vv") {
$verbose = true;
}
$var = parse_ini_file('/var/local/emhttp/var.ini');
@@ -343,7 +343,7 @@ if ($isRegistered) {
}
// if remoteaccess is enabled in 6.10.0-rc3+ and WANIP has changed since nginx started, reload nginx
-if ($post['_wanip'] && ($post['_wanip'] != $nginx['NGINX_WANIP']) && version_compare($var['version'],"6.10.0-rc2",">")) $reloadNginx = true;
+if (isset($post['_wanip']) && ($post['_wanip'] != $nginx['NGINX_WANIP']) && version_compare($var['version'],"6.10.0-rc2",">")) $reloadNginx = true;
// if remoteaccess is currently disabled (perhaps because a wanip was not available when nginx was started)
// BUT the system is configured to have it enabled AND a wanip is now available
// then reload nginx
From 72ed67487aba8b5fa27de9d7b7f9d8a1441857f9 Mon Sep 17 00:00:00 2001
From: Eric Schultz
Date: Mon, 19 Dec 2022 10:14:57 -0700
Subject: [PATCH 3/5] sync changes from My Servers plugin
---
plugins/dynamix.my.servers/include/state.php | 2 ++
plugins/dynamix/include/UpdateDNS.php | 5 +++--
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/plugins/dynamix.my.servers/include/state.php b/plugins/dynamix.my.servers/include/state.php
index 7b752c99e..a6df39a08 100644
--- a/plugins/dynamix.my.servers/include/state.php
+++ b/plugins/dynamix.my.servers/include/state.php
@@ -60,6 +60,8 @@ $arr['protocol'] = $_SERVER['REQUEST_SCHEME'];
$arr['locale'] = $_SESSION['locale'] ?? 'en_US';
$arr['expiretime']=1000*($var['regTy']=='Trial'||strstr($var['regTy'],'expired')?$var['regTm2']:0);
$arr['uptime']=1000*(time() - round(strtok(exec("cat /proc/uptime"),' ')));
+$arr['hasRemoteApikey'] = empty($remote['apikey']) ? 0 : 1;
+$arr['hideMyServers'] = (file_exists('/usr/local/sbin/unraid-api')) ? '' : 'yes';
$arr['config'] = [
'valid' => $var['configValid'] === 'yes',
'error' => $var['configValid'] !== 'yes'
diff --git a/plugins/dynamix/include/UpdateDNS.php b/plugins/dynamix/include/UpdateDNS.php
index e5a9f696a..712544d82 100644
--- a/plugins/dynamix/include/UpdateDNS.php
+++ b/plugins/dynamix/include/UpdateDNS.php
@@ -163,7 +163,8 @@ function verbose_output($httpcode, $result) {
}
if ($result) {
echo "Response (HTTP $httpcode):".PHP_EOL;
- echo @json_encode(@json_decode($result, true), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL;
+ $mutatedResult = is_array($result) ? json_encode($result) : $result;
+ echo @json_encode(@json_decode($mutatedResult, true), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL;
}
}
/**
@@ -293,7 +294,7 @@ $plgversion = file_exists("/var/log/plugins/dynamix.unraid.net.plg") ? trim(@exe
// only proceed when when signed in or when legacy unraid.net SSL certificate exists
if (!$isRegistered && !$isLegacyCert) {
- response_complete(406, array('error' => _('Nothing to do')));
+ response_complete(406, array('error' => _('Nothing to do')));
}
// keyfile
From 7ed6864efbf79543edacb1207aa0416618f5d3f7 Mon Sep 17 00:00:00 2001
From: SimonFair <39065407+SimonFair@users.noreply.github.com>
Date: Mon, 19 Dec 2022 17:22:14 +0000
Subject: [PATCH 4/5] Additional VM Manager PHP fixes
#$color = strpos($msg, "rror:")!==false ? 'red-text':'green-text'?> Still need to find a fix for this, but remarked out to get it working for now.
---
plugins/dynamix.vm.manager/VMMachines.page | 2 +-
plugins/dynamix.vm.manager/include/libvirt.php | 9 ++++++---
2 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/plugins/dynamix.vm.manager/VMMachines.page b/plugins/dynamix.vm.manager/VMMachines.page
index aa8f29c82..e89b40edb 100644
--- a/plugins/dynamix.vm.manager/VMMachines.page
+++ b/plugins/dynamix.vm.manager/VMMachines.page
@@ -272,7 +272,7 @@ function loadlist() {
}
$(function() {
- $color = strpos($msg, "rror:")!==false ? 'red-text':'green-text'?>
+ #$color = strpos($msg, "rror:")!==false ? 'red-text':'green-text'?>
$('#countdown').html("=_($msg)?>");
$('#btnAddVM').click(function AddVMEvent(){$('.tab>input#tab2').click();});
diff --git a/plugins/dynamix.vm.manager/include/libvirt.php b/plugins/dynamix.vm.manager/include/libvirt.php
index aeef31a8d..a3951728a 100644
--- a/plugins/dynamix.vm.manager/include/libvirt.php
+++ b/plugins/dynamix.vm.manager/include/libvirt.php
@@ -1215,7 +1215,7 @@
'capacity' => '-',
'allocation' => '-',
'physical' => '-',
- 'bus' => $disk->target->attributes()->bus,
+ 'bus' => $disk->target->attributes()->bus->__toString(),
'boot order' => $disk->boot->attributes()->order ,
'serial' => $disk->serial
];
@@ -1273,10 +1273,13 @@
$ret = 0;
for ($i = 0; $i < sizeof($tmp); $i++) {
if (($disk == '*') || ($tmp[$i]['device'] == $disk))
- if ($physical)
+ if ($physical) {
+ if($tmp[$i]['physical'] == "-") $tmp[$i]['physical'] = "0" ;
$ret += $tmp[$i]['physical'];
- else
+ } else {
+ if($tmp[$i]['capacity'] == "-") $tmp[$i]['capacity'] = "0" ;
$ret += $tmp[$i]['capacity'];
+ }
}
unset($tmp);
From 03061e26f426f4b9d3ec15249686f354a9ee7b89 Mon Sep 17 00:00:00 2001
From: SimonFair <39065407+SimonFair@users.noreply.github.com>
Date: Tue, 20 Dec 2022 12:41:32 +0000
Subject: [PATCH 5/5] Update VMMachines.page
---
plugins/dynamix.vm.manager/VMMachines.page | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/plugins/dynamix.vm.manager/VMMachines.page b/plugins/dynamix.vm.manager/VMMachines.page
index e89b40edb..2155b0964 100644
--- a/plugins/dynamix.vm.manager/VMMachines.page
+++ b/plugins/dynamix.vm.manager/VMMachines.page
@@ -272,8 +272,8 @@ function loadlist() {
}
$(function() {
- #$color = strpos($msg, "rror:")!==false ? 'red-text':'green-text'?>
- $('#countdown').html("=_($msg)?>");
+ $color = strpos($msg, "rror:")!==false ? 'red-text':'green-text'?>
+ $('#countdown').html("=_($msg)?>");
$('#btnAddVM').click(function AddVMEvent(){$('.tab>input#tab2').click();});
loadlist();