Skip to content

Commit ca474d0

Browse files
committed
CLOUDSTACK-2304: fix migrate volume for vmware
1 parent 4759492 commit ca474d0

2 files changed

Lines changed: 179 additions & 11 deletions

File tree

‎plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java‎

Lines changed: 178 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import java.util.Map;
2828
import java.util.UUID;
2929

30+
import com.cloud.agent.api.storage.CopyVolumeAnswer;
31+
import com.cloud.agent.api.storage.CopyVolumeCommand;
32+
import com.cloud.agent.api.to.*;
3033
import org.apache.cloudstack.storage.command.AttachAnswer;
3134
import org.apache.cloudstack.storage.command.AttachCommand;
3235
import org.apache.cloudstack.storage.command.CopyCmdAnswer;
@@ -51,11 +54,6 @@
5154
import com.cloud.agent.api.ManageSnapshotCommand;
5255
import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer;
5356
import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer;
54-
import com.cloud.agent.api.to.DataStoreTO;
55-
import com.cloud.agent.api.to.DataTO;
56-
import com.cloud.agent.api.to.DiskTO;
57-
import com.cloud.agent.api.to.NfsTO;
58-
import com.cloud.agent.api.to.VolumeTO;
5957
import com.cloud.hypervisor.vmware.manager.VmwareHostService;
6058
import com.cloud.hypervisor.vmware.manager.VmwareStorageMount;
6159
import com.cloud.hypervisor.vmware.mo.ClusterMO;
@@ -421,18 +419,187 @@ public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) {
421419
return new CopyCmdAnswer(e.toString());
422420
}
423421
}
424-
422+
423+
private Pair<String, String> copyVolumeFromSecStorage(VmwareHypervisorHost hyperHost, String srcVolumePath,
424+
DatastoreMO dsMo, String secStorageUrl) throws Exception {
425+
//srcVolumePath has volumes/dc/id/uuid
426+
int index = srcVolumePath.lastIndexOf(File.separator);
427+
String volumeFolder = srcVolumePath;
428+
String volumeName = srcVolumePath.substring(index + 1);
429+
430+
String newVolume = UUID.randomUUID().toString().replaceAll("-", "");
431+
restoreVolumeFromSecStorage(hyperHost, dsMo, newVolume, secStorageUrl, volumeFolder, volumeName);
432+
433+
return new Pair<String, String>(volumeFolder, newVolume);
434+
}
435+
436+
private String deleteVolumeDirOnSecondaryStorage(String volumeDir, String secStorageUrl) throws Exception {
437+
String secondaryMountPoint = mountService.getMountPoint(secStorageUrl);
438+
String volumeMountRoot = secondaryMountPoint + File.separator + volumeDir;
439+
440+
return deleteDir(volumeMountRoot);
441+
}
442+
443+
private String deleteDir(String dir) {
444+
synchronized(dir.intern()) {
445+
Script command = new Script(false, "rm", _timeout, s_logger);
446+
command.add("-rf");
447+
command.add(dir);
448+
return command.execute();
449+
}
450+
}
425451

426452
@Override
427453
public Answer copyVolumeFromImageCacheToPrimary(CopyCommand cmd) {
428-
// TODO Auto-generated method stub
429-
return null;
430-
}
454+
VolumeObjectTO srcVolume = (VolumeObjectTO)cmd.getSrcTO();
455+
VolumeObjectTO destVolume = (VolumeObjectTO)cmd.getDestTO();
456+
VmwareContext context = hostService.getServiceContext(cmd);
457+
try {
458+
459+
NfsTO srcStore = (NfsTO)srcVolume.getDataStore();
460+
PrimaryDataStoreTO destStore = (PrimaryDataStoreTO)destVolume.getDataStore();
461+
462+
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
463+
String uuid = destStore.getUuid();
464+
465+
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, uuid);
466+
if (morDatastore == null) {
467+
morDatastore = hyperHost.mountDatastore(
468+
false,
469+
destStore.getHost(), 0, destStore.getPath(),
470+
destStore.getUuid().replace("-", ""));
471+
472+
if (morDatastore == null) {
473+
throw new Exception("Unable to mount storage pool on host. storeUrl: " + destStore.getHost() + ":/" + destStore.getPath());
474+
}
475+
}
476+
477+
Pair<String, String> result = copyVolumeFromSecStorage(
478+
hyperHost, srcVolume.getPath(),
479+
new DatastoreMO(context, morDatastore),
480+
srcStore.getUrl());
481+
deleteVolumeDirOnSecondaryStorage(result.first(), srcStore.getUrl());
482+
VolumeObjectTO newVolume = new VolumeObjectTO();
483+
newVolume.setPath(result.second());
484+
return new CopyCmdAnswer(newVolume);
485+
} catch (Throwable t) {
486+
if (t instanceof RemoteException) {
487+
hostService.invalidateServiceContext(context);
488+
}
489+
490+
String msg = "Unable to execute CopyVolumeCommand due to exception";
491+
s_logger.error(msg, t);
492+
return new CopyCmdAnswer("CopyVolumeCommand failed due to exception: " + t.toString());
493+
}
494+
495+
}
496+
497+
private String getVolumePathInDatastore(DatastoreMO dsMo, String volumeFileName) throws Exception {
498+
String datastoreVolumePath = dsMo.searchFileInSubFolders(volumeFileName, true);
499+
assert (datastoreVolumePath != null) : "Virtual disk file missing from datastore.";
500+
return datastoreVolumePath;
501+
}
502+
503+
private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostService, VmwareHypervisorHost hyperHost, CopyCommand cmd,
504+
String vmName, String poolId, String volumePath, String destVolumePath,
505+
String secStorageUrl, String workerVmName) throws Exception {
506+
VirtualMachineMO workerVm=null;
507+
VirtualMachineMO vmMo=null;
508+
String exportName = UUID.randomUUID().toString();
509+
510+
try {
511+
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
512+
513+
if (morDs == null) {
514+
String msg = "Unable to find volumes's storage pool for copy volume operation";
515+
s_logger.error(msg);
516+
throw new Exception(msg);
517+
}
518+
519+
vmMo = hyperHost.findVmOnHyperHost(vmName);
520+
if (vmMo == null) {
521+
// create a dummy worker vm for attaching the volume
522+
DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
523+
//restrict VM name to 32 chars, (else snapshot descriptor file name will be truncated to 32 chars of vm name)
524+
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
525+
vmConfig.setName(workerVmName);
526+
vmConfig.setMemoryMB((long) 4);
527+
vmConfig.setNumCPUs(1);
528+
vmConfig.setGuestId(VirtualMachineGuestOsIdentifier.OTHER_GUEST.value());
529+
VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
530+
fileInfo.setVmPathName(String.format("[%s]", dsMo.getName()));
531+
vmConfig.setFiles(fileInfo);
532+
533+
// Scsi controller
534+
VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
535+
scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
536+
scsiController.setBusNumber(0);
537+
scsiController.setKey(1);
538+
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
539+
scsiControllerSpec.setDevice(scsiController);
540+
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
541+
vmConfig.getDeviceChange().add(scsiControllerSpec);
542+
543+
hyperHost.createVm(vmConfig);
544+
workerVm = hyperHost.findVmOnHyperHost(workerVmName);
545+
if (workerVm == null) {
546+
String msg = "Unable to create worker VM to execute CopyVolumeCommand";
547+
s_logger.error(msg);
548+
throw new Exception(msg);
549+
}
550+
551+
//attach volume to worker VM
552+
String datastoreVolumePath = getVolumePathInDatastore(dsMo, volumePath + ".vmdk");
553+
workerVm.attachDisk(new String[] { datastoreVolumePath }, morDs);
554+
vmMo = workerVm;
555+
}
556+
557+
vmMo.createSnapshot(exportName, "Temporary snapshot for copy-volume command", false, false);
558+
559+
exportVolumeToSecondaryStroage(vmMo, volumePath, secStorageUrl, destVolumePath, exportName,
560+
hostService.getWorkerName(hyperHost.getContext(), cmd, 1));
561+
return new Pair<String, String>(destVolumePath, exportName);
562+
563+
} finally {
564+
vmMo.removeSnapshot(exportName, false);
565+
if (workerVm != null) {
566+
//detach volume and destroy worker vm
567+
workerVm.detachAllDisks();
568+
workerVm.destroy();
569+
}
570+
}
571+
}
431572

432573
@Override
433574
public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) {
434-
// TODO Auto-generated method stub
435-
return null;
575+
VolumeObjectTO srcVolume = (VolumeObjectTO)cmd.getSrcTO();
576+
VolumeObjectTO destVolume = (VolumeObjectTO)cmd.getDestTO();
577+
String vmName = srcVolume.getVmName();
578+
579+
VmwareContext context = hostService.getServiceContext(cmd);
580+
try {
581+
PrimaryDataStoreTO primaryStorage = (PrimaryDataStoreTO)srcVolume.getDataStore();
582+
NfsTO destStore = (NfsTO)destVolume.getDataStore();
583+
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
584+
585+
Pair<String, String> result;
586+
587+
result = copyVolumeToSecStorage(hostService,
588+
hyperHost, cmd, vmName, primaryStorage.getUuid(), srcVolume.getPath(),destVolume.getPath(),
589+
destStore.getUrl(),
590+
hostService.getWorkerName(context, cmd, 0));
591+
VolumeObjectTO newVolume = new VolumeObjectTO();
592+
newVolume.setPath(result.first() + File.separator + result.second());
593+
return new CopyCmdAnswer(newVolume);
594+
} catch (Throwable e) {
595+
if (e instanceof RemoteException) {
596+
hostService.invalidateServiceContext(context);
597+
}
598+
599+
String msg = "Unable to execute CopyVolumeCommand due to exception";
600+
s_logger.error(msg, e);
601+
return new CopyCmdAnswer("CopyVolumeCommand failed due to exception: " + e.toString());
602+
}
436603
}
437604

438605
private void postCreatePrivateTemplate(String installFullPath, long templateId,

‎services/secondary-storage/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ protected Answer copySnapshotToTemplateFromNfsToNfs(CopyCommand cmd, SnapshotObj
445445

446446
processor.configure("qcow2 processor", params);
447447
String destPath = destFile.getAbsolutePath();
448+
448449
FormatInfo info = processor.process(destPath, null, templateName);
449450
TemplateLocation loc = new TemplateLocation(_storage, destPath);
450451
loc.create(1, true, srcFile.getName());

0 commit comments

Comments
 (0)