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