Search in sources :

Example 31 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method handleVmStartFailure.

public String handleVmStartFailure(final Connection conn, final String vmName, final VM vm, final String message, final Throwable th) {
    final String msg = "Unable to start " + vmName + " due to " + message;
    s_logger.warn(msg, th);
    if (vm == null) {
        return msg;
    }
    try {
        final VM.Record vmr = vm.getRecord(conn);
        final List<Network> networks = new ArrayList<>();
        for (final VIF vif : vmr.VIFs) {
            try {
                final VIF.Record rec = vif.getRecord(conn);
                if (rec != null) {
                    networks.add(rec.network);
                } else {
                    s_logger.warn("Unable to cleanup VIF: " + vif.toWireString() + " As vif record is null");
                }
            } catch (final Exception e) {
                s_logger.warn("Unable to cleanup VIF", e);
            }
        }
        if (vmr.powerState == VmPowerState.RUNNING) {
            try {
                vm.hardShutdown(conn);
            } catch (final Exception e) {
                s_logger.warn("VM hardshutdown failed due to ", e);
            }
        }
        if (vm.getPowerState(conn) == VmPowerState.HALTED) {
            try {
                vm.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("VM destroy failed due to ", e);
            }
        }
        for (final VBD vbd : vmr.VBDs) {
            try {
                vbd.unplug(conn);
                vbd.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("Unable to clean up VBD due to ", e);
            }
        }
        for (final VIF vif : vmr.VIFs) {
            try {
                vif.unplug(conn);
                vif.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("Unable to cleanup VIF", e);
            }
        }
        for (final Network network : networks) {
            if (network.getNameLabel(conn).startsWith("VLAN")) {
                disableVlanNetwork(conn, network);
            }
        }
    } catch (final Exception e) {
        s_logger.warn("VM getRecord failed due to ", e);
    }
    return msg;
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) ArrayList(java.util.ArrayList) VBD(com.xensource.xenapi.VBD) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 32 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method attachConfigDriveIsoToVm.

public boolean attachConfigDriveIsoToVm(final Connection conn, final VM vm) throws XenAPIException, XmlRpcException {
    final String vmName = vm.getNameLabel(conn);
    final String isoURL = _configDriveIsopath + vmName + ".iso";
    final VDI srVdi;
    try {
        final Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + ".iso");
        if (vdis.isEmpty()) {
            throw new CloudRuntimeException("Could not find ISO with URL: " + isoURL);
        }
        srVdi = vdis.iterator().next();
    } catch (final XenAPIException e) {
        s_logger.debug("Unable to get config drive iso: " + isoURL + " due to " + e.toString());
        return false;
    } catch (final Exception e) {
        s_logger.debug("Unable to get config drive iso: " + isoURL + " due to " + e.toString());
        return false;
    }
    VBD isoVBD = null;
    // Find the VM's CD-ROM VBD
    final Set<VBD> vbds = vm.getVBDs(conn);
    for (final VBD vbd : vbds) {
        final Types.VbdType type = vbd.getType(conn);
        final VBD.Record vbdr = vbd.getRecord(conn);
        // if the device exists then attach it
        if (!vbdr.userdevice.equals(_attachIsoDeviceNum) && type == Types.VbdType.CD) {
            isoVBD = vbd;
            break;
        }
    }
    if (isoVBD == null) {
        // create vbd
        final 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;
        final VBD cfgDriveVBD = VBD.create(conn, cfgDriveVbdr);
        isoVBD = cfgDriveVBD;
        s_logger.debug("Created CD-ROM VBD for VM: " + vm);
    }
    if (isoVBD != null) {
        // If an ISO is already inserted, eject it
        if (isoVBD.getEmpty(conn) == false) {
            isoVBD.eject(conn);
        }
        try {
            // Insert the new ISO
            isoVBD.insert(conn, srVdi);
            s_logger.debug("Attached config drive iso to vm " + vmName);
        } catch (final XmlRpcException ex) {
            s_logger.debug("Failed to attach config drive iso to vm " + vmName);
            return false;
        }
    }
    return true;
}
Also used : Types(com.xensource.xenapi.Types) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 33 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class CitrixAttachIsoCommandWrapper method execute.

@Override
public Answer execute(final AttachIsoCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final boolean attach = command.isAttach();
    final String vmName = command.getVmName();
    final String isoURL = command.getIsoPath();
    final String errorMsg;
    if (attach) {
        errorMsg = "Failed to attach ISO";
    } else {
        errorMsg = "Failed to detach ISO";
    }
    try {
        if (attach) {
            VBD isoVBD = null;
            // Find the VM
            final VM vm = citrixResourceBase.getVM(conn, vmName);
            // Find the ISO VDI
            final VDI isoVDI = citrixResourceBase.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) == false) {
                    isoVBD.eject(conn);
                }
                // Insert the new ISO
                isoVBD.insert(conn, isoVDI);
            }
            return new Answer(command);
        } else {
            // Find the VM
            final VM vm = citrixResourceBase.getVM(conn, vmName);
            final String vmUUID = vm.getUuid(conn);
            // Find the ISO VDI
            final VDI isoVDI = citrixResourceBase.getIsoVDIByURL(conn, vmName, isoURL);
            final SR sr = isoVDI.getSR(conn);
            // Look up all VBDs for this VDI
            final Set<VBD> vbds = isoVDI.getVBDs(conn);
            // the ISO from it
            for (final VBD vbd : vbds) {
                final VM vbdVM = vbd.getVM(conn);
                final String vbdVmUUID = vbdVM.getUuid(conn);
                if (vbdVmUUID.equals(vmUUID)) {
                    // If an ISO is already inserted, eject it
                    if (!vbd.getEmpty(conn)) {
                        vbd.eject(conn);
                    }
                    break;
                }
            }
            if (!sr.getNameLabel(conn).startsWith("XenServer Tools")) {
                citrixResourceBase.removeSR(conn, sr);
            }
            return new Answer(command);
        }
    } catch (final XenAPIException e) {
        s_logger.warn(errorMsg + ": " + e.toString(), e);
        return new Answer(command, false, e.toString());
    } catch (final Exception e) {
        s_logger.warn(errorMsg + ": " + e.toString(), e);
        return new Answer(command, false, e.getMessage());
    }
}
Also used : Types(com.xensource.xenapi.Types) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Answer(com.cloud.agent.api.Answer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) SR(com.xensource.xenapi.SR)

Example 34 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class XenServer610Resource method getUpdatedVolumePathsOfMigratedVm.

public List<VolumeObjectTO> getUpdatedVolumePathsOfMigratedVm(final Connection connection, final VM migratedVm, final DiskTO[] volumes) throws CloudRuntimeException {
    final List<VolumeObjectTO> volumeToList = new ArrayList<>();
    try {
        // Volume paths would have changed. Return that information.
        final Set<VBD> vbds = migratedVm.getVBDs(connection);
        final Map<String, VDI> deviceIdToVdiMap = new HashMap<>();
        // get vdi:vbdr to a map
        for (final VBD vbd : vbds) {
            final VBD.Record vbdr = vbd.getRecord(connection);
            if (vbdr.type == Types.VbdType.DISK) {
                final VDI vdi = vbdr.VDI;
                deviceIdToVdiMap.put(vbdr.userdevice, vdi);
            }
        }
        for (final DiskTO volumeTo : volumes) {
            if (volumeTo.getType() != Volume.Type.ISO) {
                final VolumeObjectTO vol = (VolumeObjectTO) volumeTo.getData();
                final Long deviceId = volumeTo.getDiskSeq();
                final VDI vdi = deviceIdToVdiMap.get(deviceId.toString());
                final VolumeObjectTO newVol = new VolumeObjectTO();
                newVol.setPath(vdi.getUuid(connection));
                newVol.setId(vol.getId());
                volumeToList.add(newVol);
            }
        }
    } catch (final Exception e) {
        s_logger.error("Unable to get the updated VDI paths of the migrated vm " + e.toString(), e);
        throw new CloudRuntimeException("Unable to get the updated VDI paths of the migrated vm " + e.toString(), e);
    }
    return volumeToList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 35 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class CitrixRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    final List<VolumeObjectTO> listVolumeTo = command.getVolumeTOs();
    final VMSnapshot.Type vmSnapshotType = command.getTarget().getType();
    final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    final Connection conn = citrixResourceBase.getConnection();
    PowerState vmState = null;
    VM vm = null;
    try {
        final Set<VM> vmSnapshots = VM.getByNameLabel(conn, command.getTarget().getSnapshotName());
        if (vmSnapshots == null || vmSnapshots.size() == 0) {
            return new RevertToVMSnapshotAnswer(command, false, "Cannot find vmSnapshot with name: " + command.getTarget().getSnapshotName());
        }
        final VM vmSnapshot = vmSnapshots.iterator().next();
        // find target VM or creating a work VM
        try {
            vm = citrixResourceBase.getVM(conn, vmName);
        } catch (final Exception e) {
            vm = citrixResourceBase.createWorkingVM(conn, vmName, command.getGuestOSType(), command.getPlatformEmulator(), listVolumeTo);
        }
        if (vm == null) {
            return new RevertToVMSnapshotAnswer(command, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
        }
        // call plugin to execute revert
        citrixResourceBase.revertToSnapshot(conn, vmSnapshot, vmName, vm.getUuid(conn), snapshotMemory, citrixResourceBase.getHost().getUuid());
        vm = citrixResourceBase.getVM(conn, vmName);
        final Set<VBD> vbds = vm.getVBDs(conn);
        final Map<String, VDI> vdiMap = new HashMap<>();
        // get vdi:vbdr to a map
        for (final VBD vbd : vbds) {
            final VBD.Record vbdr = vbd.getRecord(conn);
            if (vbdr.type == Types.VbdType.DISK) {
                final VDI vdi = vbdr.VDI;
                vdiMap.put(vbdr.userdevice, vdi);
            }
        }
        if (!snapshotMemory) {
            vm.destroy(conn);
            vmState = PowerState.PowerOff;
        } else {
            vmState = PowerState.PowerOn;
        }
        // after revert, VM's volumes path have been changed, need to report to manager
        for (final VolumeObjectTO volumeTo : listVolumeTo) {
            final Long deviceId = volumeTo.getDeviceId();
            final VDI vdi = vdiMap.get(deviceId.toString());
            volumeTo.setPath(vdi.getUuid(conn));
        }
        return new RevertToVMSnapshotAnswer(command, listVolumeTo, vmState);
    } catch (final Exception e) {
        s_logger.error("revert vm " + vmName + " to snapshot " + command.getTarget().getSnapshotName() + " failed due to " + e.getMessage());
        return new RevertToVMSnapshotAnswer(command, false, e.getMessage());
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) VM(com.xensource.xenapi.VM) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Aggregations

VBD (com.xensource.xenapi.VBD)45 VDI (com.xensource.xenapi.VDI)34 VM (com.xensource.xenapi.VM)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)26 Connection (com.xensource.xenapi.Connection)26 XenAPIException (com.xensource.xenapi.Types.XenAPIException)25 XmlRpcException (org.apache.xmlrpc.XmlRpcException)25 SR (com.xensource.xenapi.SR)14 InternalErrorException (com.cloud.exception.InternalErrorException)13 DiskTO (com.cloud.agent.api.to.DiskTO)10 Types (com.xensource.xenapi.Types)10 DataTO (com.cloud.agent.api.to.DataTO)8 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 URISyntaxException (java.net.URISyntaxException)8 TimeoutException (java.util.concurrent.TimeoutException)8 ConfigurationException (javax.naming.ConfigurationException)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 SAXException (org.xml.sax.SAXException)8 Answer (com.cloud.agent.api.Answer)6