use of com.xensource.xenapi.SR in project cloudstack by apache.
the class XenServerStorageProcessor method snapshotAndCopy.
// if the source SR needs to be attached to, do so
// take a snapshot of the source VDI (on the source SR)
// create an iSCSI SR based on the new back-end volume
// copy the snapshot to the new SR
// delete the snapshot
// detach the new SR
// if we needed to perform an attach to the source SR, detach from it
@Override
public SnapshotAndCopyAnswer snapshotAndCopy(final SnapshotAndCopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
try {
SR sourceSr = null;
final Map<String, String> sourceDetails = cmd.getSourceDetails();
if (sourceDetails != null && sourceDetails.keySet().size() > 0) {
final String iScsiName = sourceDetails.get(DiskTO.IQN);
final String storageHost = sourceDetails.get(DiskTO.STORAGE_HOST);
final String chapInitiatorUsername = sourceDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
final String chapInitiatorSecret = sourceDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
sourceSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false);
}
final VDI vdiToSnapshot = VDI.getByUuid(conn, cmd.getUuidOfSourceVdi());
final VDI vdiSnapshot = vdiToSnapshot.snapshot(conn, new HashMap<String, String>());
final Map<String, String> destDetails = cmd.getDestDetails();
final String iScsiName = destDetails.get(DiskTO.IQN);
final String storageHost = destDetails.get(DiskTO.STORAGE_HOST);
final String chapInitiatorUsername = destDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
final String chapInitiatorSecret = destDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
final SR newSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false);
final VDI vdiCopy = vdiSnapshot.copy(conn, newSr);
final String vdiUuid = vdiCopy.getUuid(conn);
vdiSnapshot.destroy(conn);
if (sourceSr != null) {
hypervisorResource.removeSR(conn, sourceSr);
}
hypervisorResource.removeSR(conn, newSr);
final SnapshotAndCopyAnswer snapshotAndCopyAnswer = new SnapshotAndCopyAnswer();
snapshotAndCopyAnswer.setPath(vdiUuid);
return snapshotAndCopyAnswer;
} catch (final Exception ex) {
s_logger.warn("Failed to take and copy snapshot: " + ex.toString(), ex);
return new SnapshotAndCopyAnswer(ex.getMessage());
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class XenServerStorageProcessor method dettachIso.
@Override
public Answer dettachIso(final DettachCommand cmd) {
final DiskTO disk = cmd.getDisk();
final DataTO data = disk.getData();
final DataStoreTO store = data.getDataStore();
String isoURL = null;
if (store == null) {
final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
isoURL = iso.getName();
} else {
if (!(store instanceof NfsTO)) {
s_logger.debug("Can't detach a iso which is not created on nfs: ");
return new AttachAnswer("Can't detach a iso which is not created on nfs: ");
}
final NfsTO nfsStore = (NfsTO) store;
isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
}
try {
final Connection conn = hypervisorResource.getConnection();
// Find the VM
final VM vm = hypervisorResource.getVM(conn, cmd.getVmName());
final String vmUUID = vm.getUuid(conn);
// Find the ISO VDI
final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), 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 (!XenServerUtilitiesHelper.isXenServerToolsSR(sr.getNameLabel(conn))) {
hypervisorResource.removeSR(conn, sr);
}
return new DettachAnswer(disk);
} catch (final XenAPIException e) {
final String msg = "Failed to detach volume" + " for uuid: " + data.getPath() + " due to " + e.toString();
s_logger.warn(msg, e);
return new DettachAnswer(msg);
} catch (final Exception e) {
final String msg = "Failed to detach volume" + " for uuid: " + data.getPath() + " due to " + e.getMessage();
s_logger.warn(msg, e);
return new DettachAnswer(msg);
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class XenServerStorageProcessor method createSnapshot.
@Override
public Answer createSnapshot(final CreateObjectCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) cmd.getData();
final long snapshotId = snapshotTO.getId();
final String snapshotName = snapshotTO.getName();
String details = "create snapshot operation Failed for snapshotId: " + snapshotId;
String snapshotUUID = null;
try {
final String volumeUUID = snapshotTO.getVolume().getPath();
final VDI volume = VDI.getByUuid(conn, volumeUUID);
final VDI snapshot = volume.snapshot(conn, new HashMap<String, String>());
if (snapshotName != null) {
snapshot.setNameLabel(conn, snapshotName);
}
snapshotUUID = snapshot.getUuid(conn);
final String preSnapshotUUID = snapshotTO.getParentSnapshotPath();
// check if it is a empty snapshot
if (preSnapshotUUID != null) {
final SR sr = volume.getSR(conn);
final String srUUID = sr.getUuid(conn);
final String type = sr.getType(conn);
final Boolean isISCSI = IsISCSI(type);
final String snapshotParentUUID = getVhdParent(conn, srUUID, snapshotUUID, isISCSI);
try {
final String preSnapshotParentUUID = getVhdParent(conn, srUUID, preSnapshotUUID, isISCSI);
if (snapshotParentUUID != null && snapshotParentUUID.equals(preSnapshotParentUUID)) {
// this is empty snapshot, remove it
snapshot.destroy(conn);
snapshotUUID = preSnapshotUUID;
}
} catch (final Exception e) {
s_logger.debug("Failed to get parent snapshot", e);
}
}
final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
newSnapshot.setPath(snapshotUUID);
return new CreateObjectAnswer(newSnapshot);
} catch (final XenAPIException e) {
details += ", reason: " + e.toString();
s_logger.warn(details, e);
} catch (final Exception e) {
details += ", reason: " + e.toString();
s_logger.warn(details, e);
}
return new CreateObjectAnswer(details);
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixResourceBase method cleanupTemplateSR.
public void cleanupTemplateSR(final Connection conn) {
Set<PBD> pbds = null;
try {
final Host host = Host.getByUuid(conn, _host.getUuid());
pbds = host.getPBDs(conn);
} catch (final XenAPIException e) {
s_logger.warn("Unable to get the SRs " + e.toString(), e);
throw new CloudRuntimeException("Unable to get SRs " + e.toString(), e);
} catch (final Exception e) {
throw new CloudRuntimeException("Unable to get SRs " + e.getMessage(), e);
}
for (final PBD pbd : pbds) {
SR sr = null;
SR.Record srRec = null;
try {
sr = pbd.getSR(conn);
srRec = sr.getRecord(conn);
} catch (final Exception e) {
s_logger.warn("pbd.getSR get Exception due to ", e);
continue;
}
final String type = srRec.type;
if (srRec.shared) {
continue;
}
if (SRType.NFS.equals(type) || SRType.ISO.equals(type) && srRec.nameDescription.contains("template")) {
try {
pbd.unplug(conn);
pbd.destroy(conn);
sr.forget(conn);
} catch (final Exception e) {
s_logger.warn("forget SR catch Exception due to ", e);
}
}
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixResourceBase method setupHeartbeatSr.
public String setupHeartbeatSr(final Connection conn, final SR sr, final boolean force) throws XenAPIException, XmlRpcException {
final SR.Record srRec = sr.getRecord(conn);
final String srUuid = srRec.uuid;
if (!srRec.shared || !SRType.LVMOHBA.equals(srRec.type) && !SRType.LVMOISCSI.equals(srRec.type) && !SRType.NFS.equals(srRec.type)) {
return srUuid;
}
String result = null;
final Host host = Host.getByUuid(conn, _host.getUuid());
final Set<String> tags = host.getTags(conn);
if (force || !tags.contains("cloud-heartbeat-" + srUuid)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Setting up the heartbeat sr for host " + _host.getIp() + " and sr " + srUuid);
}
final Set<PBD> pbds = sr.getPBDs(conn);
for (final PBD pbd : pbds) {
final PBD.Record pbdr = pbd.getRecord(conn);
if (!pbdr.currentlyAttached && pbdr.host.getUuid(conn).equals(_host.getUuid())) {
pbd.plug(conn);
break;
}
}
result = callHostPluginThroughMaster(conn, "vmopspremium", "setup_heartbeat_sr", "host", _host.getUuid(), "sr", srUuid);
if (result == null || !result.split("#")[1].equals("0")) {
throw new CloudRuntimeException("Unable to setup heartbeat sr on SR " + srUuid + " due to " + result);
}
if (!tags.contains("cloud-heartbeat-" + srUuid)) {
tags.add("cloud-heartbeat-" + srUuid);
host.setTags(conn, tags);
}
}
result = callHostPluginPremium(conn, "setup_heartbeat_file", "host", _host.getUuid(), "sr", srUuid, "add", "true");
if (result == null || !result.split("#")[1].equals("0")) {
throw new CloudRuntimeException("Unable to setup heartbeat file entry on SR " + srUuid + " due to " + result);
}
return srUuid;
}
Aggregations