Exemple #1
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";
        }
    }
}
Exemple #2
0
function main()
{
    global $poolname, $vmname, $htvmname;
    global $vm, $reason, $snapshot, $snapshots, $message;
    xvp_global_init();
    xvp_config_init();
    xvp_db_init();
    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, "snapshot")) {
        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);
        }
    }
    switch ($reason) {
        case 'init':
            $message = "";
            break;
        case 'create':
            if (xenapi_vm_snapshot($session, $ref, $snapshot) !== false) {
                $message = "Successfully created snapshot";
            } else {
                $message = "Error creating snapshot";
            }
            break;
        case 'revert':
            if (xenapi_vm_revert($session, $snapshot) !== false) {
                $message = "Successfully reverted to snapshot";
            } else {
                $message = "Error reverting to snapshot";
            }
            break;
        case 'delete':
            // destroying VM will destroy its VBDs but not the underlying VDIs
            $delete_vdis = array();
            if (($vbds = xenapi_vm_get_vbds($session, $snapshot)) !== false) {
                foreach ($vbds as $vbd) {
                    if (($vbd_rec = xenapi_vbd_get_record($session, $vbd)) === false) {
                        continue;
                    }
                    if ($vbd_rec["type"] != "Disk") {
                        continue;
                    }
                    $vdi = $vbd_rec["VDI"];
                    if (($vdi_rec = xenapi_vdi_get_record($session, $vdi)) === false) {
                        continue;
                    }
                    if ($vdi_rec["is_a_snapshot"] && count($vdi_rec["VBDs"]) == 1) {
                        $delete_vdis[] = $vdi;
                    }
                }
            }
            if ($ok = xenapi_vm_destroy($session, $snapshot)) {
                foreach ($delete_vdis as $vdi) {
                    $ok &= xenapi_vdi_destroy($session, $vdi);
                }
            }
            if ($ok) {
                $message = "Successfully deleted snapshot";
            } else {
                $message = "Error deleting snapshot";
            }
            break;
    }
    $snapshots = array();
    $snaprefs = xenapi_vm_get_snapshots($session, $ref);
    foreach ($snaprefs as $snapref) {
        $snapname = xenapi_vm_get_name_label($session, $snapref);
        if ($snapname !== false) {
            $snapshots[$snapref] = $snapname;
        }
    }
}