@@ -579,35 +579,7 @@ public Volume attachVolumeToVM(AttachVolumeCmd command) {
579579 rootVolumeOfVm = rootVolumesOfVm .get (0 );
580580 }
581581
582- HypervisorType rootDiskHyperType = _volsDao .getHypervisorType (rootVolumeOfVm .getId ());
583-
584- /*if (volume.getState().equals(Volume.State.Allocated) || volume.getState().equals(Volume.State.UploadOp)) {
585- // Need to create the volume
586- VMTemplateVO rootDiskTmplt = _templateDao.findById(vm.getTemplateId());
587- DataCenterVO dcVO = _dcDao.findById(vm.getDataCenterIdToDeployIn());
588- HostPodVO pod = _podDao.findById(vm.getPodIdToDeployIn());
589- StoragePoolVO rootDiskPool = _storagePoolDao.findById(rootVolumeOfVm.getPoolId());
590- ServiceOfferingVO svo = _serviceOfferingDao.findById(vm.getServiceOfferingId());
591- DiskOfferingVO diskVO = _diskOfferingDao.findById(volume.getDiskOfferingId());
592- if (!volumeOnSec){
593- volume = _storageMgr.createVolume(volume, vm, rootDiskTmplt, dcVO, pod, rootDiskPool.getClusterId(), svo, diskVO, new ArrayList<StoragePoolVO>(), volume.getSize(), rootDiskHyperType);
594- }else {
595- try {
596- // Format of data disk should be the same as root disk
597- if( ! volHostVO.getFormat().getFileExtension().equals(_storageMgr.getSupportedImageFormatForCluster(rootDiskPool.getClusterId())) ){
598- throw new InvalidParameterValueException("Failed to attach volume to VM since volumes format " +volHostVO.getFormat().getFileExtension()+
599- " is not compatible with the vm hypervisor type" );
600- }
601- volume = _storageMgr.copyVolumeFromSecToPrimary(volume, vm, rootDiskTmplt, dcVO, pod, rootDiskPool.getClusterId(), svo, diskVO, new ArrayList<StoragePoolVO>(), volume.getSize(), rootDiskHyperType);
602- } catch (NoTransitionException e) {
603- e.printStackTrace();
604- }
605- }
606-
607- if (volume == null) {
608- throw new CloudRuntimeException("Failed to create volume when attaching it to VM: " + vm.getHostName());
609- }
610- }*/
582+ HypervisorType rootDiskHyperType = vm .getHypervisorType ();
611583
612584 HypervisorType dataDiskHyperType = _volsDao .getHypervisorType (volume .getId ());
613585 if (dataDiskHyperType != HypervisorType .None && rootDiskHyperType != dataDiskHyperType ) {
@@ -657,7 +629,7 @@ public Volume attachVolumeToVM(AttachVolumeCmd command) {
657629 ServiceOfferingVO svo = _serviceOfferingDao .findById (vm .getServiceOfferingId ());
658630 DiskOfferingVO diskVO = _diskOfferingDao .findById (volume .getDiskOfferingId ());
659631 Long clusterId = (rootDiskPool == null ? null : rootDiskPool .getClusterId ());
660-
632+
661633 if (!isVolumeOnSec ){
662634 volume = _storageMgr .createVolume (volume , vm , rootDiskTmplt , dcVO , pod , clusterId , svo , diskVO , new ArrayList <StoragePoolVO >(), volume .getSize (), rootDiskHyperType );
663635 }else {
@@ -666,7 +638,14 @@ public Volume attachVolumeToVM(AttachVolumeCmd command) {
666638 if ( ! volHostVO .getFormat ().getFileExtension ().equals (_storageMgr .getSupportedImageFormatForCluster (rootDiskPool .getClusterId ())) ){
667639 throw new InvalidParameterValueException ("Failed to attach volume to VM since volumes format " +volHostVO .getFormat ().getFileExtension () + " is not compatible with the vm hypervisor type" );
668640 }
669-
641+
642+ // Check that there is some shared storage.
643+ StoragePoolVO vmRootVolumePool = _storagePoolDao .findById (rootVolumeOfVm .getPoolId ());
644+ List <StoragePoolVO > sharedVMPools = _storagePoolDao .findPoolsByTags (vmRootVolumePool .getDataCenterId (), vmRootVolumePool .getPodId (), vmRootVolumePool .getClusterId (), null , true );
645+ if (sharedVMPools .size () == 0 ) {
646+ throw new CloudRuntimeException ("Cannot attach volume since there are no shared storage pools in the VM's cluster to copy the uploaded volume to." );
647+ }
648+
670649 volume = _storageMgr .copyVolumeFromSecToPrimary (volume , vm , rootDiskTmplt , dcVO , pod , rootDiskPool .getClusterId (), svo , diskVO , new ArrayList <StoragePoolVO >(), volume .getSize (), rootDiskHyperType );
671650 } catch (NoTransitionException e ) {
672651 throw new CloudRuntimeException ("Unable to transition the volume " ,e );
0 commit comments