@@ -188,52 +188,131 @@ public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) {
188188 DataTO srcData = cmd .getSrcTO ();
189189 TemplateObjectTO template = (TemplateObjectTO )srcData ;
190190 DataStoreTO srcStore = srcData .getDataStore ();
191+
191192 if (!(srcStore instanceof NfsTO )) {
192193 return new CopyCmdAnswer ("unsupported protocol" );
193194 }
195+
194196 NfsTO nfsImageStore = (NfsTO )srcStore ;
195197 DataTO destData = cmd .getDestTO ();
196198 DataStoreTO destStore = destData .getDataStore ();
197199 DataStoreTO primaryStore = destStore ;
200+
198201 String secondaryStorageUrl = nfsImageStore .getUrl ();
202+
199203 assert (secondaryStorageUrl != null );
200204
205+ boolean managed = false ;
206+ String storageHost = null ;
207+ int storagePort = Integer .MIN_VALUE ;
208+ String managedStoragePoolName = null ;
209+ String managedStoragePoolRootVolumeName = null ;
210+ String chapInitiatorUsername = null ;
211+ String chapInitiatorSecret = null ;
212+ String chapTargetUsername = null ;
213+ String chapTargetSecret = null ;
214+
215+ if (destStore instanceof PrimaryDataStoreTO ) {
216+ PrimaryDataStoreTO destPrimaryDataStoreTo = (PrimaryDataStoreTO )destStore ;
217+
218+ Map <String , String > details = destPrimaryDataStoreTo .getDetails ();
219+
220+ if (details != null ) {
221+ managed = Boolean .parseBoolean (details .get (PrimaryDataStoreTO .MANAGED ));
222+
223+ if (managed ) {
224+ storageHost = details .get (PrimaryDataStoreTO .STORAGE_HOST );
225+
226+ try {
227+ storagePort = Integer .parseInt (details .get (PrimaryDataStoreTO .STORAGE_PORT ));
228+ }
229+ catch (Exception ex ) {
230+ storagePort = 3260 ;
231+ }
232+
233+ managedStoragePoolName = details .get (PrimaryDataStoreTO .MANAGED_STORE_TARGET );
234+ managedStoragePoolRootVolumeName = details .get (PrimaryDataStoreTO .MANAGED_STORE_TARGET_ROOT_VOLUME );
235+ chapInitiatorUsername = details .get (PrimaryDataStoreTO .CHAP_INITIATOR_USERNAME );
236+ chapInitiatorSecret = details .get (PrimaryDataStoreTO .CHAP_INITIATOR_SECRET );
237+ chapTargetUsername = details .get (PrimaryDataStoreTO .CHAP_TARGET_USERNAME );
238+ chapTargetSecret = details .get (PrimaryDataStoreTO .CHAP_TARGET_SECRET );
239+ }
240+ }
241+ }
242+
201243 String templateUrl = secondaryStorageUrl + "/" + srcData .getPath ();
202244
203245 Pair <String , String > templateInfo = VmwareStorageLayoutHelper .decodeTemplateRelativePathAndNameFromUrl (secondaryStorageUrl , templateUrl , template .getName ());
204246
205247 VmwareContext context = hostService .getServiceContext (cmd );
248+
206249 try {
207250 VmwareHypervisorHost hyperHost = hostService .getHyperHost (context , cmd );
208-
209- String templateUuidName = deriveTemplateUuidOnHost (hyperHost , primaryStore .getUuid (), templateInfo .second ());
210-
251+ String storageUuid = managed ? managedStoragePoolName : primaryStore .getUuid ();
252+ String templateUuidName = deriveTemplateUuidOnHost (hyperHost , storageUuid , templateInfo .second ());
211253 DatacenterMO dcMo = new DatacenterMO (context , hyperHost .getHyperHostDatacenter ());
212254 VirtualMachineMO templateMo = VmwareHelper .pickOneVmOnRunningHost (dcMo .findVmByNameAndLabel (templateUuidName ), true );
255+ DatastoreMO dsMo = null ;
213256
214257 if (templateMo == null ) {
215- if (s_logger .isInfoEnabled ())
216- s_logger .info ("Template " + templateInfo .second () + " is not setup yet, setup template from secondary storage with uuid name: " + templateUuidName );
217- ManagedObjectReference morDs = HypervisorHostHelper .findDatastoreWithBackwardsCompatibility (hyperHost , primaryStore .getUuid ());
258+ if (s_logger .isInfoEnabled ()) {
259+ s_logger .info ("Template " + templateInfo .second () + " is not setup yet. Set up template from secondary storage with uuid name: " + templateUuidName );
260+ }
261+
262+ final ManagedObjectReference morDs ;
263+
264+ if (managed ) {
265+ morDs = hostService .prepareManagedDatastore (hyperHost , managedStoragePoolName , storageHost , storagePort ,
266+ chapInitiatorUsername , chapInitiatorSecret , chapTargetUsername , chapTargetSecret );
267+ }
268+ else {
269+ morDs = HypervisorHostHelper .findDatastoreWithBackwardsCompatibility (hyperHost , storageUuid );
270+ }
271+
218272 assert (morDs != null );
219- DatastoreMO primaryStorageDatastoreMo = new DatastoreMO (context , morDs );
220273
221- copyTemplateFromSecondaryToPrimary (hyperHost , primaryStorageDatastoreMo , secondaryStorageUrl , templateInfo .first (), templateInfo .second (),
222- templateUuidName );
274+ dsMo = new DatastoreMO (context , morDs );
275+
276+ copyTemplateFromSecondaryToPrimary (hyperHost , dsMo , secondaryStorageUrl , templateInfo .first (), templateInfo .second (),
277+ managed ? managedStoragePoolRootVolumeName : templateUuidName );
278+
279+ if (managed ) {
280+ String [] vmwareLayoutFilePair = VmwareStorageLayoutHelper .getVmdkFilePairDatastorePath (dsMo , managedStoragePoolRootVolumeName , managedStoragePoolRootVolumeName ,
281+ VmwareStorageLayoutType .VMWARE , false );
282+ String [] legacyCloudStackLayoutFilePair = VmwareStorageLayoutHelper .getVmdkFilePairDatastorePath (dsMo , null , managedStoragePoolRootVolumeName ,
283+ VmwareStorageLayoutType .CLOUDSTACK_LEGACY , false );
284+
285+ dsMo .moveDatastoreFile (vmwareLayoutFilePair [0 ], dcMo .getMor (), dsMo .getMor (), legacyCloudStackLayoutFilePair [0 ], dcMo .getMor (), true );
286+ dsMo .moveDatastoreFile (vmwareLayoutFilePair [1 ], dcMo .getMor (), dsMo .getMor (), legacyCloudStackLayoutFilePair [1 ], dcMo .getMor (), true );
287+
288+ String folderToDelete = dsMo .getDatastorePath (managedStoragePoolRootVolumeName , true );
289+ dsMo .deleteFolder (folderToDelete , dcMo .getMor ());
290+ }
223291 } else {
224292 s_logger .info ("Template " + templateInfo .second () + " has already been setup, skip the template setup process in primary storage" );
225293 }
226294
227295 TemplateObjectTO newTemplate = new TemplateObjectTO ();
228- newTemplate .setPath (templateUuidName );
296+
297+ if (managed ) {
298+ String path = dsMo .getDatastorePath (managedStoragePoolRootVolumeName + ".vmdk" );
299+
300+ newTemplate .setPath (path );
301+ }
302+ else {
303+ newTemplate .setPath (templateUuidName );
304+ }
305+
229306 return new CopyCmdAnswer (newTemplate );
230307 } catch (Throwable e ) {
231308 if (e instanceof RemoteException ) {
232309 hostService .invalidateServiceContext (context );
233310 }
234311
235312 String msg = "Unable to copy template to primary storage due to exception:" + VmwareHelper .getExceptionMessage (e );
313+
236314 s_logger .error (msg , e );
315+
237316 return new CopyCmdAnswer (msg );
238317 }
239318 }
@@ -1176,9 +1255,9 @@ private Answer attachVolume(Command cmd, DiskTO disk, boolean isAttach, boolean
11761255 Map <String , String > details = disk .getDetails ();
11771256
11781257 morDs = hostService .prepareManagedStorage (hyperHost , iScsiName , storageHost , storagePort ,
1179- details .get (DiskTO .CHAP_INITIATOR_USERNAME ), details .get (DiskTO .CHAP_INITIATOR_SECRET ),
1180- details .get (DiskTO .CHAP_TARGET_USERNAME ), details .get (DiskTO .CHAP_TARGET_SECRET ),
1181- volumeTO .getSize (), cmd );
1258+ details .get (DiskTO .CHAP_INITIATOR_USERNAME ), details .get (DiskTO .CHAP_INITIATOR_SECRET ),
1259+ details .get (DiskTO .CHAP_TARGET_USERNAME ), details .get (DiskTO .CHAP_TARGET_SECRET ),
1260+ volumeTO .getSize (), cmd );
11821261 }
11831262 else {
11841263 morDs = HypervisorHostHelper .findDatastoreWithBackwardsCompatibility (hyperHost , isManaged ? VmwareResource .getDatastoreName (iScsiName ) : primaryStore .getUuid ());
@@ -1572,15 +1651,15 @@ private List<String> getManagedIqnsFromVirtualDisks(List<VirtualDisk> virtualDis
15721651 VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo )virtualDisk .getBacking ();
15731652 String path = backingInfo .getFileName ();
15741653
1575- path = new DatastoreFile (path ).getFileBaseName ();
1576-
1577- String search = "-" ;
1654+ String search = "[-" ;
15781655 int index = path .indexOf (search );
15791656
15801657 if (index > -1 ) {
15811658 path = path .substring (index + search .length ());
15821659
1583- index = path .lastIndexOf (search );
1660+ String search2 = "-0]" ;
1661+
1662+ index = path .lastIndexOf (search2 );
15841663
15851664 if (index > -1 ) {
15861665 path = path .substring (0 , index );
0 commit comments