Search in sources :

Example 6 with VBD

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

the class CitrixResourceBase method startvmfailhandle.

protected void startvmfailhandle(final Connection conn, final VM vm, final List<Ternary<SR, VDI, VolumeVO>> mounts) {
    if (vm != null) {
        try {
            if (vm.getPowerState(conn) == VmPowerState.RUNNING) {
                try {
                    vm.hardShutdown(conn);
                } catch (final Exception e) {
                    final String msg = "VM hardshutdown failed due to " + e.toString();
                    s_logger.warn(msg, e);
                }
            }
            if (vm.getPowerState(conn) == VmPowerState.HALTED) {
                try {
                    vm.destroy(conn);
                } catch (final Exception e) {
                    final String msg = "VM destroy failed due to " + e.toString();
                    s_logger.warn(msg, e);
                }
            }
        } catch (final Exception e) {
            final String msg = "VM getPowerState failed due to " + e.toString();
            s_logger.warn(msg, e);
        }
    }
    if (mounts != null) {
        for (final Ternary<SR, VDI, VolumeVO> mount : mounts) {
            final VDI vdi = mount.second();
            Set<VBD> vbds = null;
            try {
                vbds = vdi.getVBDs(conn);
            } catch (final Exception e) {
                final String msg = "VDI getVBDS failed due to " + e.toString();
                s_logger.warn(msg, e);
                continue;
            }
            for (final VBD vbd : vbds) {
                try {
                    vbd.unplug(conn);
                    vbd.destroy(conn);
                } catch (final Exception e) {
                    final String msg = "VBD destroy failed due to " + e.toString();
                    s_logger.warn(msg, e);
                }
            }
        }
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) 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) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SR(com.xensource.xenapi.SR)

Example 7 with VBD

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

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<VolumeObjectTO>();
    try {
        // Volume paths would have changed. Return that information.
        final Set<VBD> vbds = migratedVm.getVBDs(connection);
        final Map<String, VDI> deviceIdToVdiMap = new HashMap<String, VDI>();
        // 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(org.apache.cloudstack.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 8 with VBD

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

the class XenServer56FP1FenceCommandWrapper method execute.

@Override
public Answer execute(final FenceCommand command, final XenServer56Resource xenServer56) {
    final Connection conn = xenServer56.getConnection();
    try {
        final Boolean alive = xenServer56.checkHeartbeat(command.getHostGuid());
        if (alive == null) {
            s_logger.debug("Failed to check heartbeat,  so unable to fence");
            return new FenceAnswer(command, false, "Failed to check heartbeat, so unable to fence");
        }
        if (alive) {
            s_logger.debug("Heart beat is still going so unable to fence");
            return new FenceAnswer(command, false, "Heartbeat is still going on unable to fence");
        }
        final Set<VM> vms = VM.getByNameLabel(conn, command.getVmName());
        for (final VM vm : vms) {
            final Set<VDI> vdis = new HashSet<VDI>();
            final Set<VBD> vbds = vm.getVBDs(conn);
            for (final VBD vbd : vbds) {
                final VDI vdi = vbd.getVDI(conn);
                if (!xenServer56.isRefNull(vdi)) {
                    vdis.add(vdi);
                }
            }
            s_logger.info("Fence command for VM " + command.getVmName());
            vm.powerStateReset(conn);
            vm.destroy(conn);
            for (final VDI vdi : vdis) {
                final Map<String, String> smConfig = vdi.getSmConfig(conn);
                for (final String key : smConfig.keySet()) {
                    if (key.startsWith("host_")) {
                        vdi.removeFromSmConfig(conn, key);
                        break;
                    }
                }
            }
        }
        return new FenceAnswer(command);
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    } catch (final Exception e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    }
}
Also used : Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) FenceAnswer(com.cloud.agent.api.FenceAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) HashSet(java.util.HashSet)

Example 9 with VBD

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

the class CitrixMigrateCommandWrapper method execute.

@Override
public Answer execute(final MigrateCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    final String dstHostIpAddr = command.getDestinationIp();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        final Set<Host> hosts = Host.getAll(conn);
        Host dsthost = null;
        if (hosts != null) {
            for (final Host host : hosts) {
                if (host.getAddress(conn).equals(dstHostIpAddr)) {
                    dsthost = host;
                    break;
                }
            }
        }
        if (dsthost == null) {
            final String msg = "Migration failed due to unable to find host " + dstHostIpAddr + " in XenServer pool " + citrixResourceBase.getHost().getPool();
            s_logger.warn(msg);
            return new MigrateAnswer(command, false, msg, null);
        }
        for (final VM vm : vms) {
            final Set<VBD> vbds = vm.getVBDs(conn);
            for (final VBD vbd : vbds) {
                final VBD.Record vbdRec = vbd.getRecord(conn);
                if (vbdRec.type.equals(Types.VbdType.CD) && !vbdRec.empty) {
                    vbd.eject(conn);
                    // for config drive vbd destroy the vbd.
                    if (!vbdRec.userdevice.equals(citrixResourceBase._attachIsoDeviceNum)) {
                        if (vbdRec.currentlyAttached) {
                            vbd.destroy(conn);
                        }
                    }
                    continue;
                }
            }
            citrixResourceBase.migrateVM(conn, dsthost, vm, vmName);
            vm.setAffinity(conn, dsthost);
        }
        // Attach the config drive iso device to VM
        if (!citrixResourceBase.attachConfigDriveToMigratedVm(conn, vmName, dstHostIpAddr)) {
            s_logger.debug("Config drive ISO attach failed after migration for vm " + vmName);
        }
        return new MigrateAnswer(command, true, "migration succeeded", null);
    } catch (final Exception e) {
        s_logger.warn(e.getMessage(), e);
        return new MigrateAnswer(command, false, e.getMessage(), null);
    }
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) VBD(com.xensource.xenapi.VBD) Host(com.xensource.xenapi.Host)

Example 10 with VBD

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

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();
    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)

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