3535
3636import javax .naming .ConfigurationException ;
3737
38+ import com .cloud .agent .api .storage .CopyVolumeAnswer ;
39+ import com .cloud .agent .api .to .DataObjectType ;
40+ import com .cloud .agent .api .to .S3TO ;
41+ import com .cloud .agent .api .to .StorageFilerTO ;
42+ import com .cloud .utils .S3Utils ;
3843import org .apache .cloudstack .storage .command .AttachAnswer ;
3944import org .apache .cloudstack .storage .command .AttachCommand ;
4045import org .apache .cloudstack .storage .command .CopyCmdAnswer ;
9297import com .ceph .rbd .RbdImage ;
9398import com .ceph .rbd .RbdException ;
9499
100+ import static com .cloud .utils .S3Utils .putFile ;
101+
95102public class KVMStorageProcessor implements StorageProcessor {
96103 private static final Logger s_logger = Logger .getLogger (KVMStorageProcessor .class );
97104 private KVMStoragePoolManager storagePoolMgr ;
@@ -293,13 +300,114 @@ public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) {
293300
294301 @ Override
295302 public Answer copyVolumeFromImageCacheToPrimary (CopyCommand cmd ) {
296- // TODO Auto-generated method stub
297- return null ;
303+ DataTO srcData = cmd .getSrcTO ();
304+ DataTO destData = cmd .getDestTO ();
305+ DataStoreTO srcStore = srcData .getDataStore ();
306+ DataStoreTO destStore = destData .getDataStore ();
307+ PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO ) destStore ;
308+ if (!(srcStore instanceof NfsTO )) {
309+ return new CopyCmdAnswer ("can only handle nfs storage" );
310+ }
311+ NfsTO nfsStore = (NfsTO )srcStore ;
312+ String srcVolumePath = srcData .getPath ();
313+ String secondaryStorageUrl = nfsStore .getUrl ();
314+ KVMStoragePool secondaryStoragePool = null ;
315+ KVMStoragePool primaryPool = null ;
316+ try {
317+ try {
318+ primaryPool = storagePoolMgr .getStoragePool (
319+ primaryStore .getPoolType (),
320+ primaryStore .getUuid ());
321+ } catch (CloudRuntimeException e ) {
322+ if (e .getMessage ().contains ("not found" )) {
323+ primaryPool = storagePoolMgr .createStoragePool (primaryStore .getUuid (),
324+ primaryStore .getHost (), primaryStore .getPort (),
325+ primaryStore .getPath (), null ,
326+ primaryStore .getPoolType ());
327+ } else {
328+ return new CopyCmdAnswer (e .getMessage ());
329+ }
330+ }
331+
332+ String volumeName = UUID .randomUUID ().toString ();
333+
334+ int index = srcVolumePath .lastIndexOf (File .separator );
335+ String volumeDir = srcVolumePath .substring (0 , index );
336+ String srcVolumeName = srcVolumePath .substring (index + 1 );
337+ secondaryStoragePool = storagePoolMgr .getStoragePoolByURI (
338+ secondaryStorageUrl + File .separator + volumeDir
339+ );
340+ KVMPhysicalDisk volume = secondaryStoragePool
341+ .getPhysicalDisk (srcVolumeName + ".qcow2" );
342+ storagePoolMgr .copyPhysicalDisk (volume , volumeName ,
343+ primaryPool );
344+ VolumeObjectTO newVol = new VolumeObjectTO ();
345+ newVol .setPath (volumeName );
346+ return new CopyCmdAnswer (newVol );
347+ } catch (CloudRuntimeException e ) {
348+ return new CopyCmdAnswer (e .toString ());
349+ } finally {
350+ if (secondaryStoragePool != null ) {
351+ storagePoolMgr .deleteStoragePool (secondaryStoragePool .getType (),secondaryStoragePool .getUuid ());
352+ }
353+ }
298354 }
299355
300356 @ Override
301357 public Answer copyVolumeFromPrimaryToSecondary (CopyCommand cmd ) {
302- return null ;
358+ DataTO srcData = cmd .getSrcTO ();
359+ DataTO destData = cmd .getDestTO ();
360+ DataStoreTO srcStore = srcData .getDataStore ();
361+ DataStoreTO destStore = destData .getDataStore ();
362+ PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO ) srcStore ;
363+ if (!(destStore instanceof NfsTO )) {
364+ return new CopyCmdAnswer ("can only handle nfs storage" );
365+ }
366+ NfsTO nfsStore = (NfsTO )destStore ;
367+ String srcVolumePath = srcData .getPath ();
368+ String destVolumePath = destData .getPath ();
369+ String secondaryStorageUrl = nfsStore .getUrl ();
370+ KVMStoragePool secondaryStoragePool = null ;
371+ KVMStoragePool primaryPool = null ;
372+ try {
373+ try {
374+ primaryPool = storagePoolMgr .getStoragePool (
375+ primaryStore .getPoolType (),
376+ primaryStore .getUuid ());
377+ } catch (CloudRuntimeException e ) {
378+ if (e .getMessage ().contains ("not found" )) {
379+ primaryPool = storagePoolMgr .createStoragePool (primaryStore .getUuid (),
380+ primaryStore .getHost (), primaryStore .getPort (),
381+ primaryStore .getPath (), null ,
382+ primaryStore .getPoolType ());
383+ } else {
384+ return new CopyCmdAnswer (e .getMessage ());
385+ }
386+ }
387+
388+ String volumeName = UUID .randomUUID ().toString ();
389+
390+
391+ String destVolumeName = volumeName + ".qcow2" ;
392+ KVMPhysicalDisk volume = primaryPool .getPhysicalDisk (srcVolumePath );
393+ secondaryStoragePool = storagePoolMgr .getStoragePoolByURI (
394+ secondaryStorageUrl );
395+ secondaryStoragePool .createFolder (destVolumePath );
396+ storagePoolMgr .deleteStoragePool (secondaryStoragePool .getType (),secondaryStoragePool .getUuid ());
397+ secondaryStoragePool = storagePoolMgr .getStoragePoolByURI (
398+ secondaryStorageUrl + File .separator + destVolumePath );
399+ storagePoolMgr .copyPhysicalDisk (volume ,
400+ destVolumeName ,secondaryStoragePool );
401+ VolumeObjectTO newVol = new VolumeObjectTO ();
402+ newVol .setPath (destVolumePath + File .separator + volumeName );
403+ return new CopyCmdAnswer (newVol );
404+ } catch (CloudRuntimeException e ) {
405+ return new CopyCmdAnswer (e .toString ());
406+ } finally {
407+ if (secondaryStoragePool != null ) {
408+ storagePoolMgr .deleteStoragePool (secondaryStoragePool .getType (),secondaryStoragePool .getUuid ());
409+ }
410+ }
303411 }
304412
305413 @ Override
@@ -419,7 +527,70 @@ public Answer createTemplateFromVolume(CopyCommand cmd) {
419527 public Answer createTemplateFromSnapshot (CopyCommand cmd ) {
420528 return null ; //To change body of implemented methods use File | Settings | File Templates.
421529 }
530+ protected String copyToS3 (File srcFile , S3TO destStore , String destPath ) {
531+ final String bucket = destStore .getBucketName ();
422532
533+ String key = destPath + S3Utils .SEPARATOR + srcFile .getName ();
534+ putFile (destStore , srcFile , bucket , key );
535+ return key ;
536+ }
537+ protected Answer copyToObjectStore (CopyCommand cmd ) {
538+ DataTO srcData = cmd .getSrcTO ();
539+ DataTO destData = cmd .getDestTO ();
540+ SnapshotObjectTO snapshot = (SnapshotObjectTO ) srcData ;
541+ DataStoreTO imageStore = destData .getDataStore ();
542+ NfsTO srcStore = (NfsTO )srcData .getDataStore ();
543+ String srcPath = srcData .getPath ();
544+ int index = srcPath .lastIndexOf (File .separator );
545+ String srcSnapshotDir = srcPath .substring (0 , index );
546+ String srcFileName = srcPath .substring (index + 1 );
547+ KVMStoragePool srcStorePool = null ;
548+ try {
549+ srcStorePool = storagePoolMgr .getStoragePoolByURI (srcStore .getUrl () + File .separator + srcSnapshotDir );
550+ if (srcStorePool == null ) {
551+ return new CopyCmdAnswer ("Can't get store:" + srcStore .getUrl ());
552+ }
553+ File srcFile = new File (srcStorePool .getLocalPath () + File .separator + srcFileName );
554+ if (!srcFile .exists ()) {
555+ return new CopyCmdAnswer ("Can't find src file: " + srcPath );
556+ }
557+ String destPath = null ;
558+ if (imageStore instanceof S3TO ) {
559+ destPath = copyToS3 (srcFile , (S3TO )imageStore , destData .getPath ());
560+ } else {
561+ return new CopyCmdAnswer ("Unsupported protocol" );
562+ }
563+ SnapshotObjectTO newSnapshot = new SnapshotObjectTO ();
564+ newSnapshot .setPath (destPath );
565+ return new CopyCmdAnswer (newSnapshot );
566+ } finally {
567+ if (srcStorePool != null ) {
568+ srcStorePool .delete ();
569+ }
570+ }
571+ }
572+
573+ protected Answer backupSnapshotForObjectStore (CopyCommand cmd ) {
574+ DataTO srcData = cmd .getSrcTO ();
575+ DataTO destData = cmd .getDestTO ();
576+ SnapshotObjectTO snapshot = (SnapshotObjectTO ) srcData ;
577+ DataStoreTO imageStore = destData .getDataStore ();
578+ DataTO cacheData = cmd .getCacheTO ();
579+ if (cacheData == null ) {
580+ return new CopyCmdAnswer ("Failed to copy to object store without cache store" );
581+ }
582+ DataStoreTO cacheStore = cacheData .getDataStore ();
583+ ((SnapshotObjectTO ) destData ).setDataStore (cacheStore );
584+ CopyCmdAnswer answer = (CopyCmdAnswer )backupSnapshot (cmd );
585+ if (!answer .getResult ()) {
586+ return answer ;
587+ }
588+ SnapshotObjectTO snapshotOnCacheStore = (SnapshotObjectTO )answer .getNewData ();
589+ snapshotOnCacheStore .setDataStore (cacheStore );
590+ ((SnapshotObjectTO ) destData ).setDataStore (imageStore );
591+ CopyCommand newCpyCmd = new CopyCommand (snapshotOnCacheStore , destData , cmd .getWait (), cmd .executeInSequence ());
592+ return copyToObjectStore (newCpyCmd );
593+ }
423594 @ Override
424595 public Answer backupSnapshot (CopyCommand cmd ) {
425596 DataTO srcData = cmd .getSrcTO ();
@@ -430,7 +601,7 @@ public Answer backupSnapshot(CopyCommand cmd) {
430601 DataStoreTO imageStore = destData .getDataStore ();
431602
432603 if (!(imageStore instanceof NfsTO )) {
433- return new CopyCmdAnswer ( "unsupported protocol" );
604+ return backupSnapshotForObjectStore ( cmd );
434605 }
435606 NfsTO nfsImageStore = (NfsTO ) imageStore ;
436607
0 commit comments