Set<VM> vmSnapshots = VM.getByNameLabel(conn, cmd.getTarget().getSnapshotName());
if(vmSnapshots.size() > 0)
return new CreateVMSnapshotAnswer(cmd, cmd.getTarget(), cmd.getVolumeTOs());
// check if there is already a task for this VM snapshot
Task task = null;
Set<Task> tasks = Task.getByNameLabel(conn, "Async.VM.snapshot");
tasks.addAll(Task.getByNameLabel(conn, "Async.VM.checkpoint"));
for (Task taskItem : tasks) {
if(taskItem.getOtherConfig(conn).containsKey("CS_VM_SNAPSHOT_KEY")){
String vmSnapshotTaskName = taskItem.getOtherConfig(conn).get("CS_VM_SNAPSHOT_KEY");
if(vmSnapshotTaskName != null && vmSnapshotTaskName.equals(cmd.getTarget().getSnapshotName())){
task = taskItem;
}
}
}
// create a new task if there is no existing task for this VM snapshot
if(task == null){
try {
vm = getVM(conn, vmName);
} catch (Exception e) {
if (!snapshotMemory) {
vm = createWorkingVM(conn, vmName, guestOSType, listVolumeTo);
}
}
if (vm == null) {
return new CreateVMSnapshotAnswer(cmd, false,
"Creating VM Snapshot Failed due to can not find vm: "
+ vmName);
}
// call Xenserver API
if (!snapshotMemory) {
task = vm.snapshotAsync(conn, vmSnapshotName);
} else {
Set<VBD> vbds = vm.getVBDs(conn);
Pool pool = Pool.getByUuid(conn, _host.pool);
for (VBD vbd: vbds){
VBD.Record vbdr = vbd.getRecord(conn);
if (vbdr.userdevice.equals("0")){
VDI vdi = vbdr.VDI;
SR sr = vdi.getSR(conn);
// store memory image on the same SR with ROOT volume
pool.setSuspendImageSR(conn, sr);
}
}
task = vm.checkpointAsync(conn, vmSnapshotName);
}
task.addToOtherConfig(conn, "CS_VM_SNAPSHOT_KEY", vmSnapshotName);
}
waitForTask(conn, task, 1000, timeout * 1000);
checkForSuccess(conn, task);
String result = task.getResult(conn);
// extract VM snapshot ref from result
String ref = result.substring("<value>".length(), result.length() - "</value>".length());
vmSnapshot = Types.toVM(ref);
try {