Search in sources :

Example 11 with VBD

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

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";
    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) InternalErrorException(com.cloud.exception.InternalErrorException) 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 12 with VBD

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

the class CitrixResourceBase method createVbd.

public VBD createVbd(final Connection conn, final DiskTO volume, final String vmName, final VM vm, final BootloaderType bootLoaderType, VDI vdi) throws XmlRpcException, XenAPIException {
    final Volume.Type type = volume.getType();
    if (vdi == null) {
        vdi = mount(conn, vmName, volume);
    }
    if (vdi != null) {
        if ("detached".equals(vdi.getNameLabel(conn))) {
            vdi.setNameLabel(conn, vmName + "-DATA");
        }
        final Map<String, String> smConfig = vdi.getSmConfig(conn);
        for (final String key : smConfig.keySet()) {
            if (key.startsWith("host_")) {
                vdi.removeFromSmConfig(conn, key);
                break;
            }
        }
    }
    final VBD.Record vbdr = new VBD.Record();
    vbdr.VM = vm;
    if (vdi != null) {
        vbdr.VDI = vdi;
    } else {
        vbdr.empty = true;
    }
    if (type == Volume.Type.ROOT && bootLoaderType == BootloaderType.PyGrub) {
        vbdr.bootable = true;
    } else if (type == Volume.Type.ISO && bootLoaderType == BootloaderType.CD) {
        vbdr.bootable = true;
    }
    if (volume.getType() == Volume.Type.ISO) {
        vbdr.mode = Types.VbdMode.RO;
        vbdr.type = Types.VbdType.CD;
        vbdr.userdevice = "3";
    } else {
        vbdr.mode = Types.VbdMode.RW;
        vbdr.type = Types.VbdType.DISK;
        vbdr.unpluggable = (volume.getType() == Volume.Type.ROOT) ? false : true;
        vbdr.userdevice = "autodetect";
        final Long deviceId = volume.getDiskSeq();
        if (deviceId != null && (!isDeviceUsed(conn, vm, deviceId) || deviceId > 3)) {
            vbdr.userdevice = deviceId.toString();
        }
    }
    final VBD vbd = VBD.create(conn, vbdr);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("VBD " + vbd.getUuid(conn) + " created for " + volume);
    }
    return vbd;
}
Also used : Volume(com.cloud.storage.Volume) VBD(com.xensource.xenapi.VBD)

Example 13 with VBD

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

the class CitrixResourceBase method prepareISO.

public void prepareISO(final Connection conn, final String vmName, List<String[]> vmDataList, String configDriveLabel) throws XmlRpcException, XenAPIException {
    final Set<VM> vms = VM.getByNameLabel(conn, vmName);
    if (vms == null || vms.size() != 1) {
        throw new CloudRuntimeException("There are " + (vms == null ? "0" : vms.size()) + " VMs named " + vmName);
    }
    final VM vm = vms.iterator().next();
    if (vmDataList != null) {
        // create SR
        SR sr = createLocalIsoSR(conn, _configDriveSRName + getHost().getIp());
        // 1. create vm data files
        createVmdataFiles(vmName, vmDataList, configDriveLabel);
        // 2. copy config drive iso to host
        copyConfigDriveIsoToHost(conn, sr, vmName);
    }
    final Set<VBD> vbds = vm.getVBDs(conn);
    for (final VBD vbd : vbds) {
        final VBD.Record vbdr = vbd.getRecord(conn);
        if (vbdr.type == Types.VbdType.CD && vbdr.empty == false && vbdr.userdevice.equals(_attachIsoDeviceNum)) {
            final VDI vdi = vbdr.VDI;
            final SR sr = vdi.getSR(conn);
            final Set<PBD> pbds = sr.getPBDs(conn);
            if (pbds == null) {
                throw new CloudRuntimeException("There is no pbd for sr " + sr);
            }
            for (final PBD pbd : pbds) {
                final PBD.Record pbdr = pbd.getRecord(conn);
                if (pbdr.host.getUuid(conn).equals(_host.getUuid())) {
                    return;
                }
            }
            sr.setShared(conn, true);
            final Host host = Host.getByUuid(conn, _host.getUuid());
            final PBD.Record pbdr = pbds.iterator().next().getRecord(conn);
            pbdr.host = host;
            pbdr.uuid = "";
            final PBD pbd = PBD.create(conn, pbdr);
            pbdPlug(conn, pbd, pbd.getUuid(conn));
            break;
        }
    }
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) Host(com.xensource.xenapi.Host) SR(com.xensource.xenapi.SR)

Example 14 with VBD

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

the class CitrixDestroyCommandWrapper method execute.

@Override
public Answer execute(final DestroyCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final VolumeTO vol = command.getVolume();
    // Look up the VDI
    final String volumeUUID = vol.getPath();
    VDI vdi = null;
    try {
        vdi = citrixResourceBase.getVDIbyUuid(conn, volumeUUID);
    } catch (final Exception e) {
        return new Answer(command, true, "Success");
    }
    Set<VBD> vbds = null;
    try {
        vbds = vdi.getVBDs(conn);
    } catch (final Exception e) {
        final String msg = "VDI getVBDS for " + volumeUUID + " failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new Answer(command, false, msg);
    }
    for (final VBD vbd : vbds) {
        try {
            vbd.unplug(conn);
            vbd.destroy(conn);
        } catch (final Exception e) {
            final String msg = "VM destroy for " + volumeUUID + "  failed due to " + e.toString();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        }
    }
    try {
        final Set<VDI> snapshots = vdi.getSnapshots(conn);
        for (final VDI snapshot : snapshots) {
            snapshot.destroy(conn);
        }
        vdi.destroy(conn);
    } catch (final Exception e) {
        final String msg = "VDI destroy for " + volumeUUID + " failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new Answer(command, false, msg);
    }
    return new Answer(command, true, "Success");
}
Also used : Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) Connection(com.xensource.xenapi.Connection) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI)

Example 15 with VBD

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

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<String, VDI>();
        // 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(org.apache.cloudstack.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)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