$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
// add translations
$_SERVER['REQUEST_URI'] = 'vms';
require_once "$docroot/webGui/include/Translations.php";
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/plugins/dynamix.vm.manager/include/libvirt_helpers.php";
$user_prefs = '/boot/config/plugins/dynamix.vm.manager/userprefs.cfg';
$vms = $lv->get_domains();
if (empty($vms)) {
echo '
| '._('No Virtual Machines installed').' |
';
return;
}
if (file_exists($user_prefs)) {
$prefs = parse_ini_file($user_prefs); $sort = [];
foreach ($vms as $vm) $sort[] = array_search($vm,$prefs) ?? 999;
array_multisort($sort,SORT_NUMERIC,$vms);
} else {
natcasesort($vms);
}
$i = 0;
$menu = [];
$kvm = ['var kvm=[];'];
$show = explode(',',$_GET['show']) ?? [];
foreach ($vms as $vm) {
$res = $lv->get_domain_by_name($vm);
$desc = $lv->domain_get_description($res);
$uuid = $lv->domain_get_uuid($res);
$dom = $lv->domain_get_info($res);
$id = $lv->domain_get_id($res) ?: '-';
$is_autostart = $lv->domain_get_autostart($res);
$state = $lv->domain_state_translate($dom['state']);
$icon = $lv->domain_get_icon_url($res);
$image = substr($icon,-4)=='.png' ? "
" : (substr($icon,0,5)=='icon-' ? "" : "");
$arrConfig = domain_to_config($uuid);
if ($state == 'running') {
$mem = $dom['memory'] / 1024;
} else {
$mem = $lv->domain_get_memory($res) / 1024;
}
$mem = round($mem).'M';
$vcpu = $dom['nrVirtCpu'];
$auto = $is_autostart ? 'checked':'';
$template = $lv->_get_single_xpath_result($res, '//domain/metadata/*[local-name()=\'vmtemplate\']/@name');
if (empty($template)) $template = 'Custom';
$log = (is_file("/var/log/libvirt/qemu/$vm.log") ? "libvirt/qemu/$vm.log" : '');
$disks = '-';
$diskdesc = '';
if (($diskcnt = $lv->get_disk_count($res)) > 0) {
$disks = $diskcnt.' / '.$lv->get_disk_capacity($res);
$diskdesc = 'Current physical size: '.$lv->get_disk_capacity($res, true);
}
$arrValidDiskBuses = getValidDiskBuses();
$vncport = $lv->domain_get_vnc_port($res);
$vnc = '';
$graphics = '';
if ($vncport > 0) {
$wsport = $lv->domain_get_ws_port($res);
$vnc = '/plugins/dynamix.vm.manager/vnc.html?autoconnect=true&host=' . $_SERVER['HTTP_HOST'] . '&port=&path=/wsproxy/' . $wsport . '/';
$graphics = 'VNC:'.$vncport;
} elseif ($vncport == -1) {
$graphics = 'VNC:auto';
} elseif (!empty($arrConfig['gpu'])) {
$arrValidGPUDevices = getValidGPUDevices();
foreach ($arrConfig['gpu'] as $arrGPU) foreach ($arrValidGPUDevices as $arrDev) {
if ($arrGPU['id'] == $arrDev['id']) $graphics .= $arrDev['name']."\n";
}
$graphics = str_replace("\n", "
", trim($graphics));
}
unset($dom);
$menu[] = sprintf("addVMContext('%s','%s','%s','%s','%s','%s');", addslashes($vm),addslashes($uuid),addslashes($template),$state,addslashes($vnc),addslashes($log));
$kvm[] = "kvm.push({id:'$uuid',state:'$state'});";
switch ($state) {
case 'running':
$shape = 'play';
$status = 'started';
$color = 'green-text';
break;
case 'paused':
case 'pmsuspended':
$shape = 'pause';
$status = 'paused';
$color = 'orange-text';
break;
default:
$shape = 'square';
$status = 'stopped';
$color = 'red-text';
break;
}
/* VM information */
echo "";
echo "$image$vm "._($status)." | ";
echo "$desc | ";
echo "$vcpu | ";
echo "$mem | ";
echo "$disks | ";
echo "$graphics | ";
echo " |
";
/* Disk device information */
echo "" : "' style='display:none'>");
echo "";
echo "";
echo "| "._('Disk devices')." | "._('Bus')." | "._('Capacity')." | "._('Allocation')." | ";
echo "";
/* Display VM disks */
foreach ($lv->get_disk_stats($res) as $arrDisk) {
$capacity = $lv->format_size($arrDisk['capacity'], 0);
$allocation = $lv->format_size($arrDisk['allocation'], 0);
$disk = $arrDisk['file'] ?? $arrDisk['partition'];
$dev = $arrDisk['device'];
$bus = $arrValidDiskBuses[$arrDisk['bus']] ?? 'VirtIO';
echo "| $disk | $bus | ";
if ($state == 'shutoff') {
echo "";
echo " | ";
} else {
echo "$capacity | ";
}
echo "$allocation | ";
}
/* Display VM cdroms */
foreach ($lv->get_cdrom_stats($res) as $arrCD) {
$capacity = $lv->format_size($arrCD['capacity'], 0);
$allocation = $lv->format_size($arrCD['allocation'], 0);
$disk = $arrCD['file'] ?? $arrCD['partition'];
$dev = $arrCD['device'];
$bus = $arrValidDiskBuses[$arrCD['bus']] ?? 'VirtIO';
echo "| $disk | $bus | $capacity | $allocation | ";
}
echo " ";
echo " |
";
}
echo "\0".implode($menu).implode($kvm);
?>