Search in sources :

Example 16 with VBD

use of com.xensource.xenapi.VBD in project cloudstack by apache.

the class CitrixResourceBase method createPatchVbd.

public VBD createPatchVbd(final Connection conn, final String vmName, final VM vm) throws XmlRpcException, XenAPIException {
    if (_host.getSystemvmisouuid() == null) {
        final Set<SR> srs = SR.getByNameLabel(conn, "XenServer Tools");
        if (srs.size() != 1) {
            throw new CloudRuntimeException("There are " + srs.size() + " SRs with name XenServer Tools");
        }
        final SR sr = srs.iterator().next();
        sr.scan(conn);
        final SR.Record srr = sr.getRecord(conn);
        if (_host.getSystemvmisouuid() == null) {
            for (final VDI vdi : srr.VDIs) {
                final VDI.Record vdir = vdi.getRecord(conn);
                if (vdir.nameLabel.contains("systemvm.iso")) {
                    _host.setSystemvmisouuid(vdir.uuid);
                    break;
                }
            }
        }
        if (_host.getSystemvmisouuid() == null) {
            throw new CloudRuntimeException("can not find systemvmiso");
        }
    }
    final VBD.Record cdromVBDR = new VBD.Record();
    cdromVBDR.VM = vm;
    cdromVBDR.empty = true;
    cdromVBDR.bootable = false;
    cdromVBDR.userdevice = "3";
    cdromVBDR.mode = Types.VbdMode.RO;
    cdromVBDR.type = Types.VbdType.CD;
    final VBD cdromVBD = VBD.create(conn, cdromVBDR);
    cdromVBD.insert(conn, VDI.getByUuid(conn, _host.getSystemvmisouuid()));
    return cdromVBD;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) SR(com.xensource.xenapi.SR)

Example 17 with VBD

use of com.xensource.xenapi.VBD in project cloudstack by apache.

the class CitrixResourceBase method attachConfigDriveToMigratedVm.

public boolean attachConfigDriveToMigratedVm(Connection conn, String vmName, String ipAddr) {
    try {
        s_logger.debug("Attaching config drive iso device for the VM " + vmName + " In host " + ipAddr);
        Set<VM> vms = VM.getByNameLabel(conn, vmName);
        SR sr = getSRByNameLabel(conn, _configDriveSRName + ipAddr);
        //Here you will find only two vdis with the <vmname>.iso.
        //one is from source host and second from dest host
        Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + ".iso");
        if (vdis.isEmpty()) {
            s_logger.debug("Could not find config drive ISO: " + vmName);
            return false;
        }
        VDI configdriveVdi = null;
        for (VDI vdi : vdis) {
            SR vdiSr = vdi.getSR(conn);
            if (vdiSr.getUuid(conn).equals(sr.getUuid(conn))) {
                //get this vdi to attach to vbd
                configdriveVdi = vdi;
                s_logger.debug("VDI for the config drive ISO  " + vdi);
            } else {
                // delete the vdi in source host so that the <vmname>.iso file is get removed
                s_logger.debug("Removing the source host VDI for the config drive ISO  " + vdi);
                vdi.destroy(conn);
            }
        }
        if (configdriveVdi == null) {
            s_logger.debug("Config drive ISO VDI is not found ");
            return false;
        }
        for (VM vm : vms) {
            //create vbd
            VBD.Record cfgDriveVbdr = new VBD.Record();
            cfgDriveVbdr.VM = vm;
            cfgDriveVbdr.empty = true;
            cfgDriveVbdr.bootable = false;
            cfgDriveVbdr.userdevice = "autodetect";
            cfgDriveVbdr.mode = Types.VbdMode.RO;
            cfgDriveVbdr.type = Types.VbdType.CD;
            VBD cfgDriveVBD = VBD.create(conn, cfgDriveVbdr);
            s_logger.debug("Inserting vbd " + configdriveVdi);
            cfgDriveVBD.insert(conn, configdriveVdi);
            break;
        }
        return true;
    } catch (BadServerResponse e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a bad server response.", e);
        return false;
    } catch (XenAPIException e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a xapi problem.", e);
        return false;
    } catch (XmlRpcException e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a problem in a remote call.", e);
        return false;
    }
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 18 with VBD

use of com.xensource.xenapi.VBD in project cloudstack by apache.

the class XenServerStorageProcessor method attachIso.

@Override
public AttachAnswer attachIso(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final DataTO data = disk.getData();
    final DataStoreTO store = data.getDataStore();
    String isoURL = null;
    if (store == null) {
        final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        isoURL = iso.getName();
    } else {
        if (!(store instanceof NfsTO)) {
            s_logger.debug("Can't attach a iso which is not created on nfs: ");
            return new AttachAnswer("Can't attach a iso which is not created on nfs: ");
        }
        final NfsTO nfsStore = (NfsTO) store;
        isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
    }
    final String vmName = cmd.getVmName();
    try {
        final Connection conn = hypervisorResource.getConnection();
        VBD isoVBD = null;
        // Find the VM
        final VM vm = hypervisorResource.getVM(conn, vmName);
        // Find the ISO VDI
        final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, vmName, isoURL);
        // Find the VM's CD-ROM VBD
        final Set<VBD> vbds = vm.getVBDs(conn);
        for (final VBD vbd : vbds) {
            final String userDevice = vbd.getUserdevice(conn);
            final Types.VbdType type = vbd.getType(conn);
            if (userDevice.equals("3") && type == Types.VbdType.CD) {
                isoVBD = vbd;
                break;
            }
        }
        if (isoVBD == null) {
            throw new CloudRuntimeException("Unable to find CD-ROM VBD for VM: " + vmName);
        } else {
            // If an ISO is already inserted, eject it
            if (!isoVBD.getEmpty(conn)) {
                isoVBD.eject(conn);
            }
            // Insert the new ISO
            isoVBD.insert(conn, isoVDI);
        }
        return new AttachAnswer(disk);
    } catch (final XenAPIException e) {
        s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
        return new AttachAnswer(e.toString());
    } catch (final Exception e) {
        s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
        return new AttachAnswer(e.toString());
    }
}
Also used : Types(com.xensource.xenapi.Types) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.agent.api.to.NfsTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 19 with VBD

use of com.xensource.xenapi.VBD in project cloudstack by apache.

the class CitrixAttachOrDettachConfigDriveCommandWrapper method execute.

@Override
public Answer execute(final AttachOrDettachConfigDriveCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    String vmName = command.getVmName();
    List<String[]> vmData = command.getVmData();
    String label = command.getConfigDriveLabel();
    Boolean isAttach = command.isAttach();
    try {
        Set<VM> vms = VM.getByNameLabel(conn, vmName);
        for (VM vm : vms) {
            if (isAttach) {
                if (!citrixResourceBase.createAndAttachConfigDriveIsoForVM(conn, vm, vmData, label)) {
                    s_logger.debug("Failed to attach config drive iso to VM " + vmName);
                }
            } else {
                // delete the config drive iso attached to VM
                Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + ".iso");
                if (vdis != null && !vdis.isEmpty()) {
                    s_logger.debug("Deleting config drive for the VM " + vmName);
                    VDI vdi = vdis.iterator().next();
                    // Find the VM's CD-ROM VBD
                    Set<VBD> vbds = vdi.getVBDs(conn);
                    for (VBD vbd : vbds) {
                        VBD.Record vbdRec = vbd.getRecord(conn);
                        if (vbdRec.type.equals(Types.VbdType.CD) && !vbdRec.empty && !vbdRec.userdevice.equals(citrixResourceBase._attachIsoDeviceNum)) {
                            if (vbdRec.currentlyAttached) {
                                vbd.eject(conn);
                            }
                            vbd.destroy(conn);
                        }
                    }
                    vdi.destroy(conn);
                }
                s_logger.debug("Successfully dettached config drive iso from the VM " + vmName);
            }
        }
    } catch (Types.XenAPIException ex) {
        s_logger.debug("Failed to attach config drive iso to VM " + vmName + " " + ex.getMessage());
    } catch (XmlRpcException ex) {
        s_logger.debug("Failed to attach config drive iso to VM " + vmName + " " + ex.getMessage());
    }
    return new Answer(command, true, "success");
}
Also used : Types(com.xensource.xenapi.Types) Connection(com.xensource.xenapi.Connection) Answer(com.cloud.agent.api.Answer) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 20 with VBD

use of com.xensource.xenapi.VBD in project cloudstack by apache.

the class CitrixCreateVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreateVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    final String vmSnapshotName = command.getTarget().getSnapshotName();
    final List<VolumeObjectTO> listVolumeTo = command.getVolumeTOs();
    VmPowerState vmState = VmPowerState.HALTED;
    final String guestOSType = command.getGuestOSType();
    final String platformEmulator = command.getPlatformEmulator();
    final boolean snapshotMemory = command.getTarget().getType() == VMSnapshot.Type.DiskAndMemory;
    final long timeout = command.getWait();
    final Connection conn = citrixResourceBase.getConnection();
    VM vm = null;
    VM vmSnapshot = null;
    boolean success = false;
    try {
        // check if VM snapshot already exists
        final Set<VM> vmSnapshots = VM.getByNameLabel(conn, command.getTarget().getSnapshotName());
        if (vmSnapshots == null || vmSnapshots.size() > 0) {
            return new CreateVMSnapshotAnswer(command, command.getTarget(), command.getVolumeTOs());
        }
        // check if there is already a task for this VM snapshot
        Task task = null;
        Set<Task> tasks = Task.getByNameLabel(conn, "Async.VM.snapshot");
        if (tasks == null) {
            tasks = new LinkedHashSet<>();
        }
        final Set<Task> tasksByName = Task.getByNameLabel(conn, "Async.VM.checkpoint");
        if (tasksByName != null) {
            tasks.addAll(tasksByName);
        }
        for (final Task taskItem : tasks) {
            if (taskItem.getOtherConfig(conn).containsKey("CS_VM_SNAPSHOT_KEY")) {
                final String vmSnapshotTaskName = taskItem.getOtherConfig(conn).get("CS_VM_SNAPSHOT_KEY");
                if (vmSnapshotTaskName != null && vmSnapshotTaskName.equals(command.getTarget().getSnapshotName())) {
                    task = taskItem;
                }
            }
        }
        // create a new task if there is no existing task for this VM snapshot
        if (task == null) {
            try {
                vm = citrixResourceBase.getVM(conn, vmName);
                vmState = vm.getPowerState(conn);
            } catch (final Exception e) {
                if (!snapshotMemory) {
                    vm = citrixResourceBase.createWorkingVM(conn, vmName, guestOSType, platformEmulator, listVolumeTo);
                }
            }
            if (vm == null) {
                return new CreateVMSnapshotAnswer(command, false, "Creating VM Snapshot Failed due to can not find vm: " + vmName);
            }
            // call Xenserver API
            if (!snapshotMemory) {
                task = vm.snapshotAsync(conn, vmSnapshotName);
            } else {
                final Set<VBD> vbds = vm.getVBDs(conn);
                final Pool pool = Pool.getByUuid(conn, citrixResourceBase.getHost().getPool());
                for (final VBD vbd : vbds) {
                    final VBD.Record vbdr = vbd.getRecord(conn);
                    if (vbdr.userdevice.equals("0")) {
                        final VDI vdi = vbdr.VDI;
                        final SR sr = vdi.getSR(conn);
                        // store memory image on the same SR with ROOT volume
                        pool.setSuspendImageSR(conn, sr);
                    }
                }
                task = vm.checkpointAsync(conn, vmSnapshotName);
            }
            task.addToOtherConfig(conn, "CS_VM_SNAPSHOT_KEY", vmSnapshotName);
        }
        citrixResourceBase.waitForTask(conn, task, 1000, timeout * 1000);
        citrixResourceBase.checkForSuccess(conn, task);
        final String result = task.getResult(conn);
        // extract VM snapshot ref from result
        final String ref = result.substring("<value>".length(), result.length() - "</value>".length());
        vmSnapshot = Types.toVM(ref);
        try {
            Thread.sleep(5000);
        } catch (final InterruptedException ex) {
        }
        // calculate used capacity for this VM snapshot
        for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) {
            final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName());
            volumeTo.setSize(size);
        }
        success = true;
        return new CreateVMSnapshotAnswer(command, command.getTarget(), command.getVolumeTOs());
    } catch (final Exception e) {
        String msg = "";
        if (e instanceof Types.BadAsyncResult) {
            final String licenseKeyWord = "LICENCE_RESTRICTION";
            final Types.BadAsyncResult errorResult = (Types.BadAsyncResult) e;
            if (errorResult.shortDescription != null && errorResult.shortDescription.contains(licenseKeyWord)) {
                msg = licenseKeyWord;
            }
        } else {
            msg = e.toString();
        }
        s_logger.warn("Creating VM Snapshot " + command.getTarget().getSnapshotName() + " failed due to: " + msg, e);
        return new CreateVMSnapshotAnswer(command, false, msg);
    } finally {
        try {
            if (!success) {
                if (vmSnapshot != null) {
                    s_logger.debug("Delete exsisting VM Snapshot " + vmSnapshotName + " after making VolumeTO failed");
                    final Set<VBD> vbds = vmSnapshot.getVBDs(conn);
                    for (final VBD vbd : vbds) {
                        final VBD.Record vbdr = vbd.getRecord(conn);
                        if (vbdr.type == Types.VbdType.DISK) {
                            final VDI vdi = vbdr.VDI;
                            vdi.destroy(conn);
                        }
                    }
                    vmSnapshot.destroy(conn);
                }
            }
            if (vmState == VmPowerState.HALTED) {
                if (vm != null) {
                    vm.destroy(conn);
                }
            }
        } catch (final Exception e2) {
            s_logger.error("delete snapshot error due to " + e2.getMessage());
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) Connection(com.xensource.xenapi.Connection) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) VM(com.xensource.xenapi.VM) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) Pool(com.xensource.xenapi.Pool) VDI(com.xensource.xenapi.VDI) VmPowerState(com.xensource.xenapi.Types.VmPowerState) SR(com.xensource.xenapi.SR)

Aggregations

VBD (com.xensource.xenapi.VBD)21 VDI (com.xensource.xenapi.VDI)17 VM (com.xensource.xenapi.VM)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 Connection (com.xensource.xenapi.Connection)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)12 XmlRpcException (org.apache.xmlrpc.XmlRpcException)12 InternalErrorException (com.cloud.exception.InternalErrorException)8 SR (com.xensource.xenapi.SR)7 DiskTO (com.cloud.agent.api.to.DiskTO)5 Types (com.xensource.xenapi.Types)5 DataTO (com.cloud.agent.api.to.DataTO)4 IOException (java.io.IOException)4 MalformedURLException (java.net.MalformedURLException)4 URISyntaxException (java.net.URISyntaxException)4 TimeoutException (java.util.concurrent.TimeoutException)4 ConfigurationException (javax.naming.ConfigurationException)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)4 Answer (com.cloud.agent.api.Answer)3