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;
}
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;
}
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;
}
}
}
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");
}
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());
}
}
Aggregations