Ejemplo n.º 1
0
Archivo: reset.php Proyecto: suzizi/xvp
function main()
{
    global $poolname, $vmname, $jsvmname, $reset;
    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 (($session = xenapi_login($pool, $master_ref)) === false) {
        return;
    }
    if (xvp_is_uuid($vmname)) {
        if (($ref = xenapi_vm_by_uuid($session, $vmname)) === false || ($vmname = xenapi_vm_get_name_label($session, $ref)) === false) {
            return;
        }
        $vm->uuid = $vm->vmname;
        $vm->vmname = $vmname;
        $jsvmname = addslashes($vmname);
    } else {
        if (($ref = xenapi_vm_by_name($session, $vmname)) === false) {
            return;
        }
    }
    if (!xvp_db_user_may_perform($vm, null, "reset")) {
        return;
    }
    if (xenapi_vm_hard_reboot($session, $ref)) {
        $reset = "true";
    }
}
Ejemplo n.º 2
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.º 3
0
function main()
{
    global $poolname, $vmname, $jsvmname, $shutdown;
    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 (($session = xenapi_login($pool, $master_ref)) === false) {
        return;
    }
    if (xvp_is_uuid($vmname)) {
        if (($ref = xenapi_vm_by_uuid($session, $vmname)) === false || ($vmname = xenapi_vm_get_name_label($session, $ref)) === false) {
            return;
        }
        $vm->uuid = $vm->vmname;
        $vm->vmname = $vmname;
        $jsvmname = addslashes($vmname);
    } else {
        if (($ref = xenapi_vm_by_name($session, $vmname)) === false) {
            return;
        }
    }
    if (!xvp_db_user_may_perform($vm, null, "shutdown")) {
        return;
    }
    $always = xenapi_vm_get_ha_always_run($session, $ref);
    if ($always === true) {
        // disable HA prior to shutdown
        xenapi_vm_set_ha_always_run($session, $ref, false);
    }
    if (xenapi_vm_shutdown($session, $ref)) {
        $shutdown = "true";
    } else {
        if ($always === true) {
            xenapi_vm_set_ha_always_run($session, $ref, true);
        }
    }
}
Ejemplo n.º 4
0
function main()
{
    global $poolname, $vmname, $jsvmname, $resumed;
    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 (($session = xenapi_login($pool, $master_ref)) === false) {
        return;
    }
    if (xvp_is_uuid($vmname)) {
        if (($ref = xenapi_vm_by_uuid($session, $vmname)) === false || ($vmname = xenapi_vm_get_name_label($session, $ref)) === false) {
            return;
        }
        $vm->uuid = $vm->vmname;
        $vm->vmname = $vmname;
        $jsvmname = addslashes($vmname);
    } else {
        if (($ref = xenapi_vm_by_name($session, $vmname)) === false) {
            return;
        }
    }
    if (!xvp_db_user_may_perform($vm, null, "resume")) {
        return;
    }
    $prio = xenapi_vm_get_restart_priority($session, $ref);
    $always = xenapi_vm_get_ha_always_run($session, $ref);
    if (xenapi_vm_resume($session, $ref)) {
        $resumed = "true";
        if (strlen($prio) > 0 && $always === false) {
            // looks like HA disabled for VM on suspend, so re-enable
            xenapi_vm_set_ha_always_run($session, $ref, true);
        }
    }
}
Ejemplo n.º 5
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";
        }
    }
}
Ejemplo n.º 6
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;
        }
    }
}
Ejemplo n.º 7
0
function vm_update($pool, $vm, $session)
{
    if (xvp_db_user_rights($vm, null) == "none") {
        $vm->vmname = false;
        return;
    }
    if (xvp_is_uuid($vm->vmname)) {
        if (($ref = xenapi_vm_by_uuid($session, $vm->vmname)) === false || ($vmname = xenapi_vm_get_name_label($session, $ref)) === false) {
            return;
        }
        $vm->uuid = $vm->vmname;
        $vm->vmname = $vmname;
    } else {
        if (($ref = xenapi_vm_by_name($session, $vm->vmname)) === false) {
            return;
        }
    }
    if (($vm->state = xenapi_vm_get_power_state($session, $ref)) != "Running") {
        return;
    }
    $vm->metrics = xenapi_vm_get_metrics($session, $ref);
    $vm->guest_metrics = xenapi_vm_get_guest_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 ($vm->state == "Running") {
            $vm->uptime = vm_uptime(xenapi_vm_get_start_time($session, $vm->metrics));
        }
    }
    if ($vm->guest_metrics !== false) {
        $osversion = xenapi_vm_get_os_version($session, $vm->guest_metrics);
        if ($osversion !== false) {
            $vm->os_version = $osversion;
        }
    }
}
Ejemplo n.º 8
0
Archivo: boot.php Proyecto: suzizi/xvp
function main()
{
    global $poolname, $vmname, $jsvmname, $booted, $recovery;
    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 (($session = xenapi_login($pool, $master_ref)) === false) {
        return;
    }
    if (xvp_is_uuid($vmname)) {
        if (($ref = xenapi_vm_by_uuid($session, $vmname)) === false || ($vmname = xenapi_vm_get_name_label($session, $ref)) === false) {
            return;
        }
        $vm->uuid = $vm->vmname;
        $vm->vmname = $vmname;
        $jsvmname = addslashes($vmname);
    } else {
        if (($ref = xenapi_vm_by_name($session, $vmname)) === false) {
            return;
        }
    }
    if (!xvp_db_user_may_perform($vm, null, "boot")) {
        return;
    }
    if ($recovery && !xvp_db_user_may_perform($vm, null, "bootrecovery")) {
        return;
    }
    if ($recovery) {
        /*
         * A recovery boot means using HVM, even if guest is PV, and
         * setting boot device order to CD/DVD drive and then network.
         * However, we must restore original settings immediately
         * after boot completes or fails.
         */
        $policy = xenapi_vm_get_hvm_boot_policy($session, $ref);
        $params = xenapi_vm_get_hvm_boot_params($session, $ref);
        if ($policy === false || $params === false) {
            return;
        }
        if (isset($params["order"])) {
            $order = $params["order"];
        } else {
            $order = "";
        }
        $params["order"] = "dn";
        if (!xenapi_vm_set_hvm_boot_params($session, $ref, $params)) {
            return;
        }
        if (!xenapi_vm_set_hvm_boot_policy($session, $ref, "BIOS order")) {
            $params["order"] = $order;
            xenapi_vm_set_hvm_boot_params($session, $ref, $params);
            return;
        }
    }
    $prio = xenapi_vm_get_restart_priority($session, $ref);
    $always = xenapi_vm_get_ha_always_run($session, $ref);
    if (xenapi_vm_start($session, $ref)) {
        $booted = "true";
        if (strlen($prio) > 0 && $always === false) {
            // looks like HA disabled for VM on shutdown, so re-enable
            xenapi_vm_set_ha_always_run($session, $ref, true);
        }
    }
    if ($recovery) {
        /*
         * Restore original HVM/PV and boot order settings.
         */
        xenapi_vm_set_hvm_boot_policy($session, $ref, $policy);
        $params["order"] = $order;
        xenapi_vm_set_hvm_boot_params($session, $ref, $params);
    }
}