Ejemplo n.º 1
0
function setup()
{
    global $poolname, $vmname, $jsvmname;
    global $stage, $hostname, $htvmname, $home, $hosts, $bootrecovery;
    global $session, $ref, $href;
    xvp_global_init();
    xvp_config_init();
    xvp_db_init();
    $hosts = $ref = $href = false;
    if (!($pool = xvp_config_pool_by_name($poolname)) || !($vm = xvp_config_vm_by_name($pool, $vmname))) {
        return;
    }
    if (($session = xenapi_login($pool, $master_ref)) === false) {
        return;
    }
    if (xvp_is_uuid($vmname)) {
        if (($ref = xenapi_vm_by_uuid($session, $vmname)) === false || ($label = xenapi_vm_get_name_label($session, $ref)) === false) {
            return;
        }
        $vm->uuid = $vm->vmname;
        $htvmname = htmlspecialchars($label);
        if ($stage == 2) {
            $jsvmname = addslashes($label);
        }
    } else {
        if (($ref = xenapi_vm_by_name($session, $vmname)) === false) {
            return;
        } else {
            $htvmname = htmlspecialchars($vmname);
        }
    }
    if (!xvp_db_user_may_perform($vm, null, "booton")) {
        return;
    }
    $bootrecovery = xvp_db_user_may_perform($vm, null, "bootrecovery");
    switch ($stage) {
        case 1:
            if (($aff = xenapi_vm_get_affinity($session, $ref)) !== false) {
                $home = xenapi_host_get_name_label($session, $aff);
            } else {
                $home = "";
            }
            if (($ph = xenapi_vm_get_possible_hosts($session, $ref)) === false) {
                return;
            }
            $hosts = array();
            foreach ($ph as $href) {
                $name = xenapi_host_get_name_label($session, $href);
                if ($name !== false) {
                    $hosts[$href] = $name;
                }
            }
            asort($hosts);
            break;
        case 2:
            $href = xenapi_host_by_name($session, $hostname);
            break;
    }
}
Ejemplo n.º 2
0
function main()
{
    global $poolname, $vmname, $htvmname, $description;
    global $ha, $vm, $state, $platform, $home, $where, $cpus;
    global $current_dvd_uuid, $current_dvd_name, $possible_dvds;
    global $dvd_change, $dvd_iso, $dvd_host, $dvd_uuid, $dvd_error;
    xvp_global_init();
    xvp_config_init();
    xvp_db_init();
    $platform = "blank";
    if (!($pool = xvp_config_pool_by_name($poolname)) || !($vm = xvp_config_vm_by_name($pool, $vmname))) {
        return;
    }
    if (!xvp_db_user_may_perform($vm, null, "properties")) {
        exit;
    }
    if (($session = xenapi_login($pool, $master_ref)) === false) {
        return;
    }
    if (xvp_is_uuid($vmname)) {
        if (($ref = xenapi_vm_by_uuid($session, $vmname)) === false || ($label = xenapi_vm_get_name_label($session, $ref)) === false) {
            return;
        }
        $vm->uuid = $vm->vmname;
        $htvmname = htmlspecialchars($label);
    } else {
        if (($ref = xenapi_vm_by_name($session, $vmname)) === false) {
            return;
        } else {
            $htvmname = htmlspecialchars($vmname);
        }
    }
    $description = xenapi_vm_get_name_description($session, $ref);
    if ($description === false) {
        $description = "";
    } else {
        $description = htmlspecialchars($description);
    }
    $dvd_iso = xvp_db_user_may_perform($vm, null, "dvdiso");
    $dvd_host = xvp_db_user_may_perform($vm, null, "dvdhost");
    $dvd_change = $dvd_iso || $dvd_host;
    $prio = xenapi_vm_get_restart_priority($session, $ref);
    // 1 in older versions of XenServer seems to have become "restart" in
    // newer versions.  Based on Christian Scheele's patch of 23/07/2012.
    switch ($prio) {
        case 1:
        case "restart":
            $ha = "protected";
            break;
        case "best-effort":
            $ha = "restart if possible";
            break;
        default:
            $ha = "do not restart";
            break;
    }
    if (($aff = xenapi_vm_get_affinity($session, $ref)) !== false) {
        $home = xenapi_host_get_name_label($session, $aff);
    }
    if (($where = xenapi_vm_get_resident_on($session, $ref)) !== false) {
        $where = xenapi_host_get_name_label($session, $where);
    }
    $cpus = xenapi_vm_get_vcpus_max($session, $ref);
    // only handle 1st usable DVD drive found
    $current_dvd_name = null;
    $current_dvd_uuid = null;
    if (($vbds = xenapi_vm_get_vbds($session, $ref)) !== false) {
        foreach ($vbds as $vbd) {
            if (($vbd_rec = xenapi_vbd_get_record($session, $vbd)) === false || $vbd_rec["type"] != "CD") {
                continue;
            }
            if ($vbd_rec["empty"]) {
                $current_dvd_name = "<empty>";
                $current_dvd_uuid = "empty";
            } else {
                $vdi = $vbd_rec["VDI"];
                if (($vdi_rec = xenapi_vdi_get_record($session, $vdi)) === false) {
                    continue;
                }
                $current_dvd_name = $vdi_rec["name_label"];
                $current_dvd_uuid = $vdi_rec["uuid"];
            }
            break;
        }
    }
    $dvd_error = "";
    if ($dvd_change && isset($dvd_uuid) && $dvd_uuid != $current_dvd_uuid) {
        if ($dvd_uuid == "empty") {
            $changed = xenapi_vbd_eject($session, $vbd);
        } else {
            if ($dvd_uuid != $current_dvd_uuid) {
                if ($current_dvd_uuid != "empty") {
                    xenapi_vbd_eject($session, $vbd);
                }
                if ($vdi = xenapi_vdi_by_uuid($session, $dvd_uuid)) {
                    $changed = xenapi_vbd_insert($session, $vbd, $vdi);
                } else {
                    $changed = false;
                }
            }
        }
        if ($changed) {
            if (($current_dvd_uuid = $dvd_uuid) == "empty") {
                $current_dvd_name = "<empty>";
            } else {
                $current_dvd_name = xenapi_vdi_get_name_label($session, $vdi);
            }
        } else {
            $dvd_error = " !";
        }
    }
    $possible_dvds = array();
    $possible_dvds["empty"] = "<empty>";
    if (($srs = xenapi_sr_get_all($session)) !== false) {
        foreach ($srs as $sr) {
            // SR needs to be an ISO Library or a physical DVD drive
            if (($sr_rec = xenapi_sr_get_record($session, $sr)) === false || !isset($sr_rec["content_type"]) || $sr_rec["content_type"] != "iso") {
                continue;
            }
            if (($sr_host = sr_get_host_name($session, $sr_rec)) !== false) {
                // If SR on one host and VM running on another, we can't use SR
                if ($where !== false && $where != $sr_host) {
                    continue;
                }
                $sr_rec["name_label"] = $sr_host;
            }
            $vdicount = 0;
            $sr_dvds = array();
            foreach ($sr_rec["VDIs"] as $vdi) {
                $vdi = $vdi->scalarval();
                if (($vdi_rec = xenapi_vdi_get_record($session, $vdi)) === false) {
                    continue;
                }
                // nasty hack for XenServer Tools, which may have unwanted VDIs
                if ($sr_rec["name_label"] == "XenServer Tools" && $vdi_rec["name_label"] != "xs-tools.iso") {
                    continue;
                }
                // Bodge physical drive naming
                $vdi_display = $sr_host === false ? $vdi_rec["name_label"] : "DVD drive {$vdicount}";
                $sr_dvds[$vdi_rec["uuid"]] = $sr_rec["name_label"] . ": {$vdi_display}";
                $vdicount++;
            }
            if ($vdicount > 0) {
                natcasesort($sr_dvds);
                $possible_dvds = array_merge($possible_dvds, $sr_dvds);
            }
        }
    }
    $state = strtolower(xenapi_vm_get_power_state($session, $ref));
    $vm->metrics = xenapi_vm_get_metrics($session, $ref);
    if ($vm->metrics !== false) {
        if (($mem = xenapi_vm_get_memory_actual($session, $vm->metrics)) > 0) {
            $mem = $mem / (1024 * 1024);
            if ($mem >= 1024) {
                $vm->memory_total = sprintf("%.1f GB", $mem / 1024);
            } else {
                $vm->memory_total = sprintf("%d MB", $mem);
            }
        }
        if ($state == "running") {
            $vm->uptime = vm_uptime(xenapi_vm_get_start_time($session, $vm->metrics));
        }
    }
    if ($state != "running") {
        return;
    }
    $vm->guest_metrics = xenapi_vm_get_guest_metrics($session, $ref);
    if ($vm->guest_metrics !== false) {
        $osversion = xenapi_vm_get_os_version($session, $vm->guest_metrics);
        if ($osversion !== false) {
            $vm->os_version = $osversion;
        }
        $toolsversion = xenapi_vm_get_pv_drivers_version($session, $vm->guest_metrics);
        if ($toolsversion == false) {
            $vm->tools_info = "not installed";
        } else {
            $toolsuptodate = xenapi_vm_get_pv_drivers_up_to_date($session, $vm->guest_metrics);
            $vm->tools_info = sprintf("%s (%sup to date)", $toolsversion, $toolsuptodate ? "" : "not ");
        }
    }
    if (vm_os_windows($vm->os_version)) {
        $platform = "windows";
    } else {
        if (vm_os_linux($vm->os_version)) {
            $platform = "linux";
        }
    }
}