Skip to content

Commit 68ffe1c

Browse files
committed
fix bug in create volume from snapshot
1 parent 564d3d6 commit 68ffe1c

8 files changed

Lines changed: 266 additions & 22 deletions

File tree

‎core/src/com/cloud/storage/resource/StorageProcessor.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ public interface StorageProcessor {
4242
public Answer createVolume(CreateObjectCommand cmd);
4343
public Answer createSnapshot(CreateObjectCommand cmd);
4444
public Answer deleteVolume(DeleteCommand cmd);
45+
public Answer createVolumeFromSnapshot(CopyCommand cmd);
4546
}

‎core/src/com/cloud/storage/resource/StorageSubsystemCommandHandlerBase.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ protected Answer execute(CopyCommand cmd) {
8282
}
8383
} else if (srcData.getObjectType() == DataObjectType.SNAPSHOT && srcData.getDataStore().getRole() == DataStoreRole.Primary) {
8484
return processor.backupSnasphot(cmd);
85+
} else if (srcData.getObjectType() == DataObjectType.SNAPSHOT && destData.getObjectType() == DataObjectType.VOLUME) {
86+
return processor.createVolumeFromSnapshot(cmd);
8587
}
8688

8789
return new Answer(cmd, false, "not implemented yet");

‎engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/AncientDataMotionStrategy.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,11 @@ protected Answer copyVolumeFromSnapshot(DataObject snapObj, DataObject volObj) {
231231
.getDefaultValue()));
232232

233233
CopyCommand cmd = new CopyCommand(srcData.getTO(), volObj.getTO(), _createVolumeFromSnapshotWait);
234-
235-
236-
Answer answer = storageMgr
237-
.sendToPool(pool, cmd);
234+
EndPoint ep = selector.select(snapObj, volObj);
235+
Answer answer = ep.sendMessage(cmd);
236+
238237
return answer;
239-
} catch (StorageUnavailableException e) {
238+
} catch (Exception e) {
240239
s_logger.error(basicErrMsg, e);
241240
throw new CloudRuntimeException(basicErrMsg);
242241
} finally {
@@ -300,7 +299,7 @@ public Void copyAsync(DataObject srcData, DataObject destData,
300299
} else if (srcData.getType() == DataObjectType.SNAPSHOT &&
301300
destData.getType() == DataObjectType.SNAPSHOT) {
302301
answer = copySnapshot(srcData, destData);
303-
}
302+
}
304303

305304
if (answer != null && !answer.getResult()) {
306305
errMsg = answer.getDetails();

‎engine/storage/integration-test/test/org/apache/cloudstack/storage/test/SnapshotTest.java‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public VolumeInfo createCopyBaseImage() {
360360
return null;
361361
}
362362

363-
@Test
363+
//@Test
364364
public void createSnapshot() {
365365
VolumeInfo vol = createCopyBaseImage();
366366
SnapshotVO snapshotVO = createSnapshotInDb(vol);
@@ -394,7 +394,7 @@ private VMTemplateVO createTemplateInDb() {
394394
return image;
395395
}
396396

397-
@Test
397+
//@Test
398398
public void createTemplateFromSnapshot() {
399399
VolumeInfo vol = createCopyBaseImage();
400400
SnapshotVO snapshotVO = createSnapshotInDb(vol);
@@ -416,4 +416,24 @@ public void createTemplateFromSnapshot() {
416416
DataStore imageStore = this.dataStoreMgr.getImageStore(this.dcId);
417417
this.imageService.createTemplateFromSnapshotAsync(snapshot, tmpl, imageStore);
418418
}
419+
420+
@Test
421+
public void createVolumeFromSnapshot() {
422+
VolumeInfo vol = createCopyBaseImage();
423+
SnapshotVO snapshotVO = createSnapshotInDb(vol);
424+
SnapshotInfo snapshot = this.snapshotFactory.getSnapshot(snapshotVO.getId(), vol.getDataStore());
425+
boolean result = false;
426+
for (SnapshotStrategy strategy : this.snapshotStrategies) {
427+
if (strategy.canHandle(snapshot)) {
428+
snapshot = strategy.takeSnapshot(snapshot);
429+
result = true;
430+
}
431+
}
432+
433+
AssertJUnit.assertTrue(result);
434+
435+
VolumeVO volVO = createVolume(vol.getTemplateId(), vol.getPoolId());
436+
VolumeInfo newVol = this.volFactory.getVolume(volVO.getId());
437+
this.volumeService.createVolumeFromSnapshot(newVol, newVol.getDataStore(), snapshot);
438+
}
419439
}

‎engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,11 @@ protected Void createVolumeFromSnapshotCallback(AsyncCallbackDispatcher<VolumeSe
539539
}
540540

541541
try {
542-
volume.processEvent(event);
542+
if (result.isSuccess()) {
543+
volume.processEvent(event, result.getAnswer());
544+
} else {
545+
volume.processEvent(event);
546+
}
543547
snapshot.processEvent(event);
544548
} catch (Exception e) {
545549
s_logger.debug("create volume from snapshot failed", e);

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java‎

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.File;
2222
import java.io.FileOutputStream;
23-
import java.io.IOException;
2423
import java.net.URISyntaxException;
2524
import java.text.DateFormat;
2625
import java.text.MessageFormat;
@@ -47,9 +46,9 @@
4746
import org.apache.cloudstack.storage.to.TemplateObjectTO;
4847
import org.apache.cloudstack.storage.to.VolumeObjectTO;
4948
import org.apache.cloudstack.utils.qemu.QemuImg;
49+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
5050
import org.apache.cloudstack.utils.qemu.QemuImgException;
5151
import org.apache.cloudstack.utils.qemu.QemuImgFile;
52-
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
5352
import org.apache.log4j.Logger;
5453
import org.libvirt.Connect;
5554
import org.libvirt.Domain;
@@ -58,37 +57,28 @@
5857
import org.libvirt.LibvirtException;
5958

6059
import com.cloud.agent.api.Answer;
61-
import com.cloud.agent.api.AttachVolumeAnswer;
62-
import com.cloud.agent.api.BackupSnapshotAnswer;
63-
import com.cloud.agent.api.ManageSnapshotAnswer;
64-
import com.cloud.agent.api.ManageSnapshotCommand;
65-
import com.cloud.agent.api.storage.CreateAnswer;
66-
import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer;
6760
import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer;
6861
import com.cloud.agent.api.to.DataStoreTO;
6962
import com.cloud.agent.api.to.DataTO;
7063
import com.cloud.agent.api.to.DiskTO;
7164
import com.cloud.agent.api.to.NfsTO;
72-
import com.cloud.agent.api.to.StorageFilerTO;
73-
import com.cloud.agent.api.to.VolumeTO;
7465
import com.cloud.exception.InternalErrorException;
7566
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
7667
import com.cloud.hypervisor.kvm.resource.LibvirtConnection;
7768
import com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser;
7869
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
7970
import com.cloud.storage.JavaStorageLayer;
80-
import com.cloud.storage.StorageLayer;
8171
import com.cloud.storage.Storage.ImageFormat;
8272
import com.cloud.storage.Storage.StoragePoolType;
73+
import com.cloud.storage.StorageLayer;
8374
import com.cloud.storage.resource.StorageProcessor;
8475
import com.cloud.storage.template.Processor;
76+
import com.cloud.storage.template.Processor.FormatInfo;
8577
import com.cloud.storage.template.QCOW2Processor;
8678
import com.cloud.storage.template.TemplateLocation;
87-
import com.cloud.storage.template.Processor.FormatInfo;
8879
import com.cloud.utils.NumbersUtil;
8980
import com.cloud.utils.exception.CloudRuntimeException;
9081
import com.cloud.utils.script.Script;
91-
import com.cloud.vm.DiskProfile;
9282

9383
public class KVMStorageProcessor implements StorageProcessor {
9484
private static final Logger s_logger = Logger.getLogger(KVMStorageProcessor.class);
@@ -870,4 +860,46 @@ public Answer deleteVolume(DeleteCommand cmd) {
870860
}
871861
}
872862

863+
@Override
864+
public Answer createVolumeFromSnapshot(CopyCommand cmd) {
865+
try {
866+
DataTO srcData = cmd.getSrcTO();
867+
SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData;
868+
DataTO destData = cmd.getDestTO();
869+
PrimaryDataStoreTO pool = (PrimaryDataStoreTO)destData.getDataStore();
870+
DataStoreTO imageStore = srcData.getDataStore();
871+
872+
873+
if (!(imageStore instanceof NfsTO)) {
874+
return new CopyCmdAnswer("unsupported protocol");
875+
}
876+
877+
NfsTO nfsImageStore = (NfsTO)imageStore;
878+
879+
String snapshotPath = snapshot.getPath();
880+
int index = snapshotPath.lastIndexOf("/");
881+
snapshotPath = snapshotPath.substring(0, index);
882+
String snapshotName = snapshotPath.substring(index + 1);
883+
KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(
884+
nfsImageStore.getUrl() + File.separator + snapshotPath);
885+
KVMPhysicalDisk snapshotDisk = secondaryPool.getPhysicalDisk(snapshotName);
886+
887+
String primaryUuid = pool.getUuid();
888+
KVMStoragePool primaryPool = storagePoolMgr
889+
.getStoragePool(pool.getPoolType(),
890+
primaryUuid);
891+
String volUuid = UUID.randomUUID().toString();
892+
KVMPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshotDisk,
893+
volUuid, primaryPool);
894+
VolumeObjectTO newVol = new VolumeObjectTO();
895+
newVol.setPath(disk.getName());
896+
newVol.setSize(disk.getVirtualSize());
897+
return new CopyCmdAnswer(
898+
newVol);
899+
} catch (CloudRuntimeException e) {
900+
return new CopyCmdAnswer(e.toString()
901+
);
902+
}
903+
}
904+
873905
}

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.cloud.agent.api.Answer;
4545
import com.cloud.agent.api.BackupSnapshotAnswer;
4646
import com.cloud.agent.api.Command;
47+
import com.cloud.agent.api.CreateVolumeFromSnapshotAnswer;
4748
import com.cloud.agent.api.ManageSnapshotAnswer;
4849
import com.cloud.agent.api.ManageSnapshotCommand;
4950
import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer;
@@ -980,6 +981,7 @@ public Answer createVolume(CreateObjectCommand cmd) {
980981

981982
VolumeObjectTO newVol = new VolumeObjectTO();
982983
newVol.setPath(volumeUuid);
984+
newVol.setSize(volume.getSize() / (1024L * 1024L));
983985
return new CreateObjectAnswer(newVol);
984986
} finally {
985987
s_logger.info("Destroy dummy VM after volume creation");
@@ -1154,4 +1156,120 @@ public Answer deleteVolume(DeleteCommand cmd) {
11541156
return new Answer(cmd, false, msg);
11551157
}
11561158
}
1159+
1160+
private Long restoreVolumeFromSecStorage(VmwareHypervisorHost hyperHost, DatastoreMO primaryDsMo, String newVolumeName,
1161+
String secStorageUrl, String secStorageDir, String backupName) throws Exception {
1162+
1163+
String secondaryMountPoint = mountService.getMountPoint(secStorageUrl);
1164+
String srcOVAFileName = secondaryMountPoint + "/" + secStorageDir + "/"
1165+
+ backupName + "." + ImageFormat.OVA.getFileExtension();
1166+
String snapshotDir = "";
1167+
if (backupName.contains("/")){
1168+
snapshotDir = backupName.split("/")[0];
1169+
}
1170+
1171+
File ovafile = new File(srcOVAFileName);
1172+
String srcOVFFileName = secondaryMountPoint + "/" + secStorageDir + "/"
1173+
+ backupName + ".ovf";
1174+
File ovfFile = new File(srcOVFFileName);
1175+
// String srcFileName = getOVFFilePath(srcOVAFileName);
1176+
if (!ovfFile.exists()) {
1177+
srcOVFFileName = getOVFFilePath(srcOVAFileName);
1178+
if(srcOVFFileName == null && ovafile.exists() ) { // volss: ova file exists; o/w can't do tar
1179+
Script command = new Script("tar", 0, s_logger);
1180+
command.add("--no-same-owner");
1181+
command.add("-xf", srcOVAFileName);
1182+
command.setWorkDir(secondaryMountPoint + "/" + secStorageDir + "/" + snapshotDir);
1183+
s_logger.info("Executing command: " + command.toString());
1184+
String result = command.execute();
1185+
if(result != null) {
1186+
String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
1187+
s_logger.error(msg);
1188+
throw new Exception(msg);
1189+
}
1190+
} else {
1191+
String msg = "Unable to find snapshot OVA file at: " + srcOVAFileName;
1192+
s_logger.error(msg);
1193+
throw new Exception(msg);
1194+
}
1195+
1196+
srcOVFFileName = getOVFFilePath(srcOVAFileName);
1197+
}
1198+
if(srcOVFFileName == null) {
1199+
String msg = "Unable to locate OVF file in template package directory: " + srcOVAFileName;
1200+
s_logger.error(msg);
1201+
throw new Exception(msg);
1202+
}
1203+
1204+
VirtualMachineMO clonedVm = null;
1205+
try {
1206+
hyperHost.importVmFromOVF(srcOVFFileName, newVolumeName, primaryDsMo, "thin");
1207+
clonedVm = hyperHost.findVmOnHyperHost(newVolumeName);
1208+
if(clonedVm == null)
1209+
throw new Exception("Unable to create container VM for volume creation");
1210+
1211+
clonedVm.moveAllVmDiskFiles(primaryDsMo, "", false);
1212+
clonedVm.detachAllDisks();
1213+
return _storage.getSize(srcOVFFileName);
1214+
} finally {
1215+
if(clonedVm != null) {
1216+
clonedVm.detachAllDisks();
1217+
clonedVm.destroy();
1218+
}
1219+
}
1220+
}
1221+
1222+
@Override
1223+
public Answer createVolumeFromSnapshot(CopyCommand cmd) {
1224+
DataTO srcData = cmd.getSrcTO();
1225+
SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData;
1226+
DataTO destData = cmd.getDestTO();
1227+
PrimaryDataStoreTO pool = (PrimaryDataStoreTO)destData.getDataStore();
1228+
DataStoreTO imageStore = srcData.getDataStore();
1229+
1230+
1231+
if (!(imageStore instanceof NfsTO)) {
1232+
return new CopyCmdAnswer("unsupported protocol");
1233+
}
1234+
1235+
NfsTO nfsImageStore = (NfsTO)imageStore;
1236+
String primaryStorageNameLabel = pool.getUuid();
1237+
1238+
String secondaryStorageUrl = nfsImageStore.getUrl();
1239+
String backedUpSnapshotUuid = snapshot.getPath();
1240+
int index = backedUpSnapshotUuid.lastIndexOf(File.separator);
1241+
String backupPath = backedUpSnapshotUuid.substring(0, index);
1242+
backedUpSnapshotUuid = backedUpSnapshotUuid.substring(index + 1);
1243+
String details = null;
1244+
String newVolumeName = UUID.randomUUID().toString().replaceAll("-", "");
1245+
1246+
VmwareContext context = hostService.getServiceContext(cmd);
1247+
try {
1248+
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
1249+
ManagedObjectReference morPrimaryDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost,
1250+
primaryStorageNameLabel);
1251+
if (morPrimaryDs == null) {
1252+
String msg = "Unable to find datastore: " + primaryStorageNameLabel;
1253+
s_logger.error(msg);
1254+
throw new Exception(msg);
1255+
}
1256+
1257+
DatastoreMO primaryDsMo = new DatastoreMO(hyperHost.getContext(), morPrimaryDs);
1258+
Long size = restoreVolumeFromSecStorage(hyperHost, primaryDsMo,
1259+
newVolumeName, secondaryStorageUrl, backupPath, backedUpSnapshotUuid);
1260+
1261+
VolumeObjectTO newVol = new VolumeObjectTO();
1262+
newVol.setPath(newVolumeName);
1263+
newVol.setSize(size);
1264+
return new CopyCmdAnswer(newVol);
1265+
} catch (Throwable e) {
1266+
if (e instanceof RemoteException) {
1267+
hostService.invalidateServiceContext(context);
1268+
}
1269+
1270+
s_logger.error("Unexpecpted exception ", e);
1271+
details = "CreateVolumeFromSnapshotCommand exception: " + StringUtils.getExceptionStackInfo(e);
1272+
}
1273+
return new CopyCmdAnswer(details);
1274+
}
11571275
}

0 commit comments

Comments
 (0)