private void ensureDestinationImageMap() {
    if (MapUtils.isEmpty(getParameters().getDiskInfoDestinationMap())) {
      diskInfoDestinationMap = new HashMap<>();

      if (getVmTemplate() == null) {
        return;
      }

      if (!Guid.isNullOrEmpty(getParameters().getStorageDomainId())) {
        Guid storageId = getParameters().getStorageDomainId();
        ArrayList<Guid> storageIds = new ArrayList<>();
        storageIds.add(storageId);
        for (DiskImage image : getVmTemplate().getDiskTemplateMap().values()) {
          image.setStorageIds(storageIds);
          diskInfoDestinationMap.put(image.getId(), image);
        }
      } else {
        ImagesHandler.fillImagesMapBasedOnTemplate(
            getVmTemplate(), diskInfoDestinationMap, destStorages);
      }
    } else {
      diskInfoDestinationMap = getParameters().getDiskInfoDestinationMap();
    }

    storageToDisksMap =
        ImagesHandler.buildStorageToDiskMap(
            getVmTemplate().getDiskTemplateMap().values(), diskInfoDestinationMap);
  }
 /**
  * @param images The images to get the storage domain IDs for
  * @return A unique {@link Set} of all the storage domain IDs relevant to all the given images
  */
 public static Set<Guid> getAllStorageIdsForImageIds(Collection<DiskImage> images) {
   Set<Guid> domainsIds = new HashSet<>();
   for (DiskImage image : images) {
     domainsIds.addAll(image.getStorageIds());
   }
   return domainsIds;
 }
Exemple #3
0
  private void setDefaultBootDevice() {
    // In the time of disk creation the VM ID is an empty Guid, this is changed to the real ID only
    // after the reading
    // of the OS properties which comes after the disks creation so the disk VM elements are set to
    // the wrong VM ID
    // this part sets them to the correct VM ID
    for (DiskImage disk : _images) {
      disk.getDiskVmElements()
          .stream()
          .forEach(dve -> dve.setId(new VmDeviceId(disk.getId(), vmBase.getId())));
      disk.setDiskVmElements(disk.getDiskVmElements());
    }

    boolean hasBootDevice =
        vmBase
            .getManagedDeviceMap()
            .values()
            .stream()
            .anyMatch(device -> device.getBootOrder() > 0);
    if (hasBootDevice) {
      return;
    }

    AtomicInteger order =
        new AtomicInteger(1); // regular non-final variable cannot be used in lambda expression
    _images
        .stream()
        .filter(d -> d.getDiskVmElementForVm(vmBase.getId()).isBoot())
        .map(image -> vmBase.getManagedDeviceMap().get(image.getId()))
        .filter(Objects::nonNull)
        .forEachOrdered(device -> device.setBootOrder(order.getAndIncrement()));
  }
  private void onChangeQuota() {
    ChangeQuotaModel model = (ChangeQuotaModel) getWindow();
    ArrayList<VdcActionParametersBase> paramerterList = new ArrayList<VdcActionParametersBase>();

    for (Object item : model.getItems()) {
      ChangeQuotaItemModel itemModel = (ChangeQuotaItemModel) item;
      DiskImage disk = itemModel.getEntity();
      VdcActionParametersBase parameters =
          new ChangeQuotaParameters(
              itemModel.getQuota().getSelectedItem().getId(),
              disk.getId(),
              itemModel.getStorageDomainId(),
              disk.getStoragePoolId());
      paramerterList.add(parameters);
    }

    model.startProgress();

    Frontend.getInstance()
        .runMultipleAction(
            VdcActionType.ChangeQuotaForDisk,
            paramerterList,
            new IFrontendMultipleActionAsyncCallback() {
              @Override
              public void executed(FrontendMultipleActionAsyncResult result) {
                cancel();
              }
            },
            this);
  }
  private void canDoActionMakeDiskBootableOnOtherVm(boolean boot) {
    UpdateVmDiskParameters parameters = createParameters();
    Disk newDisk = parameters.getDiskInfo();
    newDisk.setBoot(true);

    Guid otherVmId = Guid.newGuid();
    VM otherVm = new VM();
    otherVm.setId(otherVmId);

    DiskImage otherDisk = new DiskImage();
    otherDisk.setId(Guid.newGuid());
    otherDisk.setActive(true);
    otherDisk.setBoot(boot);
    if (boot) {
      when(diskDao.getVmBootActiveDisk(otherVmId)).thenReturn(otherDisk);
    }
    when(diskDao.get(diskImageGuid)).thenReturn(createDiskImage());

    initializeCommand(parameters, Arrays.asList(createVmStatusDown(), otherVm));

    mockInterfaceList();

    // The command should only succeed if there is no other bootable disk
    assertEquals(!boot, command.canDoAction());
  }
 /** The following method will create a Shareable DiskImage with a specified format */
 private DiskImage createShareableDisk(VolumeFormat volumeFormat) {
   DiskImage disk = createDiskImage();
   disk.setvolumeFormat(volumeFormat);
   disk.setShareable(true);
   disk.setDiskInterface(DiskInterface.VirtIO);
   return disk;
 }
  private void onRemove() {
    RemoveDiskModel model = (RemoveDiskModel) getWindow();
    ArrayList<VdcActionParametersBase> paramerterList = new ArrayList<>();

    for (Object item : getSelectedItems()) {
      DiskImage disk = (DiskImage) item;
      VdcActionParametersBase parameters =
          new RemoveDiskParameters(disk.getId(), getEntity().getId());
      paramerterList.add(parameters);
    }

    model.startProgress();

    Frontend.getInstance()
        .runMultipleAction(
            VdcActionType.RemoveDisk,
            paramerterList,
            new IFrontendMultipleActionAsyncCallback() {
              @Override
              public void executed(FrontendMultipleActionAsyncResult result) {
                StorageDiskListModel localModel = (StorageDiskListModel) result.getState();
                localModel.stopProgress();
                cancel();
              }
            },
            this);
  }
  public boolean checkDestDomains() {
    List<Guid> validDomains = new ArrayList<>();
    for (DiskImage diskImage : diskInfoDestinationMap.values()) {
      Guid domainId = diskImage.getStorageIds().get(0);
      if (validDomains.contains(domainId)) {
        continue;
      }
      StorageDomain domain = destStorages.get(domainId);
      if (domain == null) {
        domain =
            getStorageDomainDao().getForStoragePool(domainId, getVmTemplate().getStoragePoolId());
        destStorages.put(domainId, domain);
      }
      if (storageToDisksMap.containsKey(domainId)) {
        int numOfDisksOnDomain = storageToDisksMap.get(domainId).size();
        if (numOfDisksOnDomain > 0
            && (domain.getStorageDomainType() == StorageDomainType.ImportExport)) {
          return failCanDoAction(EngineMessage.ACTION_TYPE_FAILED_STORAGE_DOMAIN_TYPE_ILLEGAL);
        }
      }
      validDomains.add(domainId);
    }

    return validateSpaceRequirements();
  }
 private void syncDbRecordsMergeFailure() {
   DiskImage curr = getDestinationDiskImage();
   while (!curr.getImageId().equals(getDiskImage().getImageId())) {
     curr = getDbFacade().getDiskImageDao().getSnapshotById(curr.getParentId());
     getImageDao().updateStatus(curr.getImageId(), ImageStatus.ILLEGAL);
   }
 }
 private static DiskImage createPreallocDiskImage() {
   DiskImage image = new DiskImage();
   image.setVolumeType(VolumeType.Preallocated);
   image.setDiskInterface(DiskInterface.IDE);
   image.setSizeInGigabytes(5);
   return image;
 }
Exemple #11
0
 private static DiskImage createDiskImageTemplate() {
   DiskImage i = new DiskImage();
   i.setSizeInGigabytes(USED_SPACE_GB + AVAILABLE_SPACE_GB);
   i.setActualSizeInBytes(REQUIRED_DISK_SIZE_GB * 1024L * 1024L * 1024L);
   i.setImageId(Guid.newGuid());
   i.setStorageIds(new ArrayList<>(Collections.singletonList(STORAGE_DOMAIN_ID_1)));
   return i;
 }
 private static DiskImage getDiskImageById(Guid id, Iterable<DiskImage> diskImages) {
   for (DiskImage diskImage : diskImages) {
     if (diskImage.getId().equals(id)) {
       return diskImage;
     }
   }
   return null;
 }
 public static void fillImagesBySnapshots(VM vm) {
   for (Disk disk : vm.getDiskMap().values()) {
     if (disk.getDiskStorageType().isInternal()) {
       DiskImage diskImage = (DiskImage) disk;
       diskImage.getSnapshots().addAll(getAllImageSnapshots(diskImage.getImageId()));
     }
   }
 }
 /**
  * Returns a list of image IDs for the specified DiskImages collection.
  *
  * @param diskImages collection of DiskImages
  * @return list of image IDs ordered by the order of the retrieved list.
  */
 public static List<Guid> getDiskImageIds(List<DiskImage> diskImages) {
   List<Guid> result = new ArrayList<>();
   if (diskImages != null) {
     for (DiskImage diskImage : diskImages) {
       result.add(diskImage.getImageId());
     }
   }
   return result;
 }
 /**
  * Gets a map of DiskImage IDs to DiskImage objects
  *
  * @param diskImages collection of DiskImage objects to create the map for
  * @return map object is the collection is not null
  */
 public static Map<Guid, DiskImage> getDiskImagesByIdMap(Collection<DiskImage> diskImages) {
   Map<Guid, DiskImage> result = new HashMap<>();
   if (diskImages != null) {
     for (DiskImage diskImage : diskImages) {
       result.put(diskImage.getImageId(), diskImage);
     }
   }
   return result;
 }
 /**
  * Adds a disk image (Adds image, disk, and relevant entities , but not VmDevice) This may be
  * useful for Clone VMs, where besides adding images it is required to copy all vm devices
  * (VmDeviceUtils.copyVmDevices) from the source VM.
  */
 public static void addDiskImageWithNoVmDevice(DiskImage image) {
   addDiskImageWithNoVmDevice(
       image,
       image.getActive(),
       new ImageStorageDomainMap(
           image.getImageId(),
           image.getStorageIds().get(0),
           image.getQuotaId(),
           image.getDiskProfileId()));
 }
 /**
  * Returns the subtraction set of the specified image lists (based on images' IDs)
  *
  * @param images full list
  * @param imagesToSubtract images to subtract list
  * @return the subtraction set
  */
 public static List<DiskImage> imagesSubtract(
     Iterable<DiskImage> images, Iterable<DiskImage> imagesToSubtract) {
   List<DiskImage> subtract = new ArrayList<>();
   for (DiskImage image : images) {
     if (getDiskImageById(image.getId(), imagesToSubtract) == null) {
       subtract.add(image);
     }
   }
   return subtract;
 }
 /**
  * Returns the intersection set of the specified image lists (based on images' IDs)
  *
  * @param images1 1st list
  * @param images2 2nd list
  * @return the intersection set
  */
 public static List<DiskImage> imagesIntersection(
     Iterable<DiskImage> images1, Iterable<DiskImage> images2) {
   List<DiskImage> intersection = new ArrayList<>();
   for (DiskImage image : images1) {
     if (getDiskImageById(image.getId(), images2) != null) {
       intersection.add(image);
     }
   }
   return intersection;
 }
 @Override
 public String getVolumeStringRepresentation() {
   return MemoryUtils.createMemoryStateString(
       storageDomainId,
       storagePool.getId(),
       memoryDisk.getId(),
       memoryDisk.getImageId(),
       metadataDisk.getId(),
       metadataDisk.getImageId());
 }
Exemple #20
0
  @Before
  public void setUp() {
    vmId = Guid.newGuid();
    diskId = Guid.newGuid();
    poolId = Guid.newGuid();
    storageDomainId = Guid.newGuid();
    groupId = Guid.newGuid();
    vdsId = Guid.newGuid();

    disk = new DiskImage();
    disk.setId(diskId);
    disk.setVmEntityType(VmEntityType.VM);
    disk.setImageStatus(ImageStatus.OK);
    disk.setStorageIds(new ArrayList<>(Arrays.asList(storageDomainId)));

    vm = new VM();
    vm.setId(vmId);
    vm.setStatus(VMStatus.Down);
    vm.setStoragePoolId(poolId);
    vm.setClusterId(groupId);

    VmDeviceId vmDeviceId = new VmDeviceId(diskId, vmId);
    vmDevice = new VmDevice();
    vmDevice.setId(vmDeviceId);

    cluster = new Cluster();
    cluster.setId(groupId);

    vds = new VDS();
    vds.setId(vdsId);

    storagePool = new StoragePool();
    storagePool.setStatus(StoragePoolStatus.Up);

    storageDomain = new StorageDomain();
    storageDomain.setStorageType(StorageType.ISCSI);

    when(vmDao.getVmsListForDisk(diskId, Boolean.FALSE)).thenReturn(Collections.singletonList(vm));
    when(vmDeviceDao.get(vmDeviceId)).thenReturn(vmDevice);
    when(vdsDao.getAllForClusterWithStatus(groupId, VDSStatus.Up))
        .thenReturn(Collections.singletonList(vds));
    when(spDao.get(poolId)).thenReturn(storagePool);
    when(clusterDao.get(groupId)).thenReturn(cluster);
    when(storageDomainStaticDao.get(storageDomainId))
        .thenReturn(storageDomain.getStorageStaticData());

    cmd = spy(new GetDiskAlignmentCommand<>(new GetDiskAlignmentParameters(diskId), null));

    doReturn(disk).when(cmd).getDisk();
    doReturn(vdsDao).when(cmd).getVdsDao();
    doReturn(vmDao).when(cmd).getVmDao();
    doReturn(spDao).when(cmd).getStoragePoolDao();
    doReturn(clusterDao).when(cmd).getClusterDao();
    doReturn(storageDomainStaticDao).when(cmd).getStorageDomainStaticDao();
  }
Exemple #21
0
  private void buildImageReference() {
    XmlNodeList list = selectNodes(_document, "//*/File", _xmlNS);
    for (XmlNode node : list) {
      // If the disk storage type is Cinder then override the disk image with Cinder object,
      // otherwise use the disk image.
      DiskImage disk = new DiskImage();

      // If the OVF is old and does not contain any storage type reference then we assume we can
      // only have disk image.
      if (node.attributes.get("ovf:disk_storage_type") != null) {
        String diskStorageType = node.attributes.get("ovf:disk_storage_type").getValue();
        if (diskStorageType != null && diskStorageType.equals(DiskStorageType.CINDER.name())) {
          disk = new CinderDisk();
          if (node.attributes.get("ovf:cinder_volume_type") != null) {
            String cinderVolumeType = node.attributes.get("ovf:cinder_volume_type").getValue();
            disk.setCinderVolumeType(cinderVolumeType);
          }
        }
      }
      disk.setImageId(new Guid(node.attributes.get("ovf:id").getValue()));
      disk.setId(
          OvfParser.getImageGroupIdFromImageFile(node.attributes.get("ovf:href").getValue()));
      // Default values:
      disk.setActive(true);
      disk.setImageStatus(ImageStatus.OK);
      disk.setDescription(node.attributes.get("ovf:description").getValue());

      disk.setDiskVmElements(
          Collections.singletonList(new DiskVmElement(disk.getId(), vmBase.getId())));

      _images.add(disk);
    }
  }
  private boolean isRemoveCommandAvailable(ArrayList<DiskImage> disks) {
    for (DiskImage disk : disks) {
      boolean isImageLocked = disk.getImageStatus() == ImageStatus.LOCKED;

      if (isImageLocked) {
        return false;
      }
    }

    return true;
  }
  @Test
  public void testResize() {
    DiskImage oldDisk = createDiskImage();
    when(diskDao.get(diskImageGuid)).thenReturn(oldDisk);

    UpdateVmDiskParameters parameters = createParameters();
    ((DiskImage) parameters.getDiskInfo()).setSize(oldDisk.getSize() * 2);
    initializeCommand(parameters);

    assertTrue(command.validateCanResizeDisk());
  }
  private void canDoActionUpdateDescription(VMStatus status) {
    DiskImage disk = createDiskImage();
    disk.setReadOnly(false);
    when(diskDao.get(diskImageGuid)).thenReturn(disk);
    UpdateVmDiskParameters parameters = createParameters();
    parameters.getDiskInfo().setReadOnly(false);
    disk.setDescription(RandomUtils.instance().nextString(10));
    initializeCommand(parameters, Collections.singletonList(createVm(status)));

    CanDoActionTestUtils.runAndAssertCanDoActionSuccess(command);
  }
 /**
  * This method is used for storage allocation validations, where the disks are the template's,
  * which could have another volume type/format than the target disk volume type/format, which is
  * not yet created. "Real" override for these values is done in CreateSnapshotCommand, when
  * creating the new DiskImages.
  */
 public static List<DiskImage> getDisksDummiesForStorageAllocations(
     Collection<DiskImage> originalDisks) {
   List<DiskImage> diskDummies = new ArrayList<>(originalDisks.size());
   for (DiskImage diskImage : originalDisks) {
     DiskImage clone = DiskImage.copyOf(diskImage);
     clone.setVolumeType(VolumeType.Sparse);
     clone.setvolumeFormat(VolumeFormat.COW);
     diskDummies.add(clone);
   }
   return diskDummies;
 }
 /**
  * Adds a disk image (Adds image with active flag according to the value in image, using the first
  * storage domain in the storage id as entry to the storage domain map)
  *
  * @param image DiskImage to add
  */
 public static void addDiskImage(DiskImage image, Guid vmId) {
   addDiskImage(
       image,
       image.getActive(),
       new ImageStorageDomainMap(
           image.getImageId(),
           image.getStorageIds().get(0),
           image.getQuotaId(),
           image.getDiskProfileId()),
       vmId);
 }
  // the last image in each list is the leaf
  public static Map<Guid, List<DiskImage>> getImagesLeaf(List<DiskImage> images) {
    Map<Guid, List<DiskImage>> retVal = new HashMap<>();
    for (DiskImage image : images) {
      MultiValueMapUtils.addToMap(image.getId(), image, retVal);
    }

    for (List<DiskImage> list : retVal.values()) {
      sortImageList(list);
    }
    return retVal;
  }
 private void updateDiskVmSnapshotId() {
   Guid snapshotId = getSnapshotDao().getId(getVmId(), SnapshotType.ACTIVE);
   if (disk.getDiskStorageType().isInternal()) {
     DiskImage diskImage = ((DiskImage) disk);
     getImageDao().updateImageVmSnapshotId(diskImage.getImageId(), snapshotId);
   } else {
     throw new EngineException(
         EngineError.StorageException,
         "update of snapshot id was initiated for unsupported disk type");
   }
 }
 /**
  * Add image and related entities to DB (Adds image, disk image dynamic and image storage domain
  * map)
  *
  * @param image the image to add
  * @param active if true the image will be active
  * @param imageStorageDomainMap entry of mapping between the storage domain and the image
  */
 public static void addImage(
     DiskImage image, boolean active, ImageStorageDomainMap imageStorageDomainMap) {
   image.setActive(active);
   DbFacade.getInstance().getImageDao().save(image.getImage());
   DiskImageDynamic diskDynamic = new DiskImageDynamic();
   diskDynamic.setId(image.getImageId());
   diskDynamic.setActualSize(image.getActualSizeInBytes());
   DbFacade.getInstance().getDiskImageDynamicDao().save(diskDynamic);
   if (imageStorageDomainMap != null) {
     DbFacade.getInstance().getImageStorageDomainMapDao().save(imageStorageDomainMap);
   }
 }
 protected boolean setAndValidateDiskProfiles() {
   if (diskInfoDestinationMap != null && !diskInfoDestinationMap.isEmpty()) {
     Map<DiskImage, Guid> map = new HashMap<>();
     for (DiskImage diskImage : diskInfoDestinationMap.values()) {
       map.put(diskImage, diskImage.getStorageIds().get(0));
     }
     return validate(
         DiskProfileHelper.setAndValidateDiskProfiles(
             map, getStoragePool().getCompatibilityVersion(), getCurrentUser()));
   }
   return true;
 }