/**
   * Validate whether a disk can be shareable. Disk can be shareable if it is not based on qcow FS,
   * which means it should not be based on a template image with thin provisioning, it also should
   * not contain snapshots and it is not bootable.
   *
   * @return Indication whether the disk can be shared or not.
   */
  protected boolean validateShareableDisk() {
    if (DiskStorageType.LUN == _oldDisk.getDiskStorageType()) {
      return true;
    }
    boolean isDiskUpdatedToShareable = getParameters().getDiskInfo().isShareable();
    boolean isDiskShareable = _oldDisk.isShareable();

    // Check if VM is not during snapshot.
    if (getSnapshotDao().exists(getVmId(), SnapshotStatus.IN_PREVIEW)) {
      addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_VM_IN_PREVIEW);
      return false;
    }

    // If user want to update the disk to be shareable then update the vm snapshot id to be null.
    if (!isDiskShareable && isDiskUpdatedToShareable) {
      List<DiskImage> diskImageList =
          getDiskImageDao().getAllSnapshotsForImageGroup(_oldDisk.getId());

      // If disk image list is more then one then we assume that it has a snapshot, since one image
      // is the active
      // disk and all the other images are the snapshots.
      if ((diskImageList.size() > 1) || !Guid.Empty.equals(((DiskImage) _oldDisk).getit_guid())) {
        addCanDoActionMessage(VdcBllMessages.SHAREABLE_DISK_IS_NOT_SUPPORTED_FOR_DISK);
        return false;
      }
      if (!isVersionSupportedForShareable(
          _oldDisk,
          getStoragePoolDAO()
              .get(getVm().getstorage_pool_id())
              .getcompatibility_version()
              .getValue())) {
        addCanDoActionMessage(VdcBllMessages.ACTION_NOT_SUPPORTED_FOR_CLUSTER_POOL_LEVEL);
        return false;
      }

      DiskImage diskImage = (DiskImage) getParameters().getDiskInfo();
      if (!isVolumeFormatSupportedForShareable(diskImage.getvolume_format())) {
        addCanDoActionMessage(VdcBllMessages.SHAREABLE_DISK_IS_NOT_SUPPORTED_BY_VOLUME_FORMAT);
        return false;
      }

      diskImage.setvm_snapshot_id(null);
    } else if (isDiskShareable && !isDiskUpdatedToShareable) {
      if (getVmDAO().getVmsListForDisk(_oldDisk.getId()).size() > 1) {
        addCanDoActionMessage(VdcBllMessages.DISK_IS_ALREADY_SHARED_BETWEEN_VMS);
        return false;
      }

      // If disk is not floating, then update its vm snapshot id to the active VM snapshot.
      ((DiskImage) _oldDisk)
          .setvm_snapshot_id(
              DbFacade.getInstance()
                  .getSnapshotDao()
                  .getId(getVmId(), SnapshotType.ACTIVE)
                  .getValue());
    }
    return true;
  }
 @Override
 public int compare(Disk o1, Disk o2) {
   Boolean boot1 = o1.isBoot();
   Boolean boot2 = o2.isBoot();
   int bootResult = boot1.compareTo(boot2);
   if (bootResult == 0 && o1.isBoot()) {
     return Boolean.compare(o2.isDiskSnapshot(), o1.isDiskSnapshot());
   }
   return bootResult;
 }
  @SuppressWarnings("rawtypes")
  @Override
  public void setItems(Iterable value) {
    ArrayList<Disk> disks = value != null ? Linq.<Disk>Cast(value) : new ArrayList<Disk>();
    ArrayList<Disk> filteredDisks = new ArrayList<Disk>();
    DiskStorageType diskStorageType = (DiskStorageType) getDiskViewType().getEntity();

    for (Disk disk : disks) {
      if (diskStorageType == null || diskStorageType == disk.getDiskStorageType()) {
        filteredDisks.add(disk);
      }
    }

    super.setItems(filteredDisks);
  }
Exemplo n.º 4
0
    @Override
    public int compare(Disk x, Disk y) {
      String xAlias = x.getDiskAlias();
      String yAlias = y.getDiskAlias();

      if (xAlias == null) {
        return 1;
      }

      if (yAlias == null) {
        return -1;
      }

      return xAlias.compareTo(yAlias);
    }
  @Override
  protected boolean canDoAction() {
    boolean retValue = isVmExist();
    if (retValue) {
      if (!isDiskExist(_oldDisk)) {
        return false;
      }

      List<VM> listVms = getVmDAO().getForDisk(_oldDisk.getId()).get(Boolean.TRUE);
      buidSharedLockMap(listVms);
      buidExclusiveLockMap(listVms);
      acquireLockInternal();

      // Check if all VMs are in status down.
      if (listVms != null && !listVms.isEmpty()) {
        for (VM vm : listVms) {
          if (vm.getstatus() != VMStatus.Down) {
            addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_VM_IS_NOT_DOWN);
            return false;
          }
        }
      }
      retValue = checkCanPerformRegularUpdate();
    }
    return retValue;
  }
Exemplo n.º 6
0
  @Test
  public void testReadOnlyMapping() {
    Disk model = new Disk();
    model.setReadOnly(true);

    org.ovirt.engine.core.common.businessentities.Disk entity = DiskMapper.map(model, null);
    assertTrue(entity.getReadOnly());

    model.setReadOnly(false);
    entity = DiskMapper.map(model, null);
    assertFalse(entity.getReadOnly());

    model.setReadOnly(null);
    entity = DiskMapper.map(model, null);
    assertNull(entity.getReadOnly());
  }
Exemplo n.º 7
0
 @Test
 public void testSizeMapping() throws Exception {
   Disk model = new Disk();
   // only <size>
   model.setSize((long) 576576);
   org.ovirt.engine.core.common.businessentities.Disk entity = DiskMapper.map(model, null);
   assertEquals(entity.getSize(), 576576);
   // <size> and <provisioned_size> - the latter should be dominant
   model.setProvisionedSize((long) 888888);
   entity = DiskMapper.map(model, null);
   assertEquals(entity.getSize(), 888888);
   // only <provisioned_size>
   model.setSize(null);
   entity = DiskMapper.map(model, null);
   assertEquals(entity.getSize(), 888888);
 }
 private boolean canPerformQuotaValidation() {
   if (DiskStorageType.IMAGE == _oldDisk.getDiskStorageType()) {
     if (!((DiskImage) _oldDisk).getQuotaId().equals(getQuotaId())) {
       return true;
     }
   }
   return false;
 }
  @Override
  public List<PermissionSubject> getPermissionCheckSubjects() {
    if (listPermissionSubjects == null) {
      listPermissionSubjects = new ArrayList<PermissionSubject>();

      Guid diskId = _oldDisk == null ? null : _oldDisk.getId();
      listPermissionSubjects.add(
          new PermissionSubject(diskId, VdcObjectType.Disk, ActionGroup.EDIT_DISK_PROPERTIES));
    }
    return listPermissionSubjects;
  }
  /**
   * Synchronize the VM's Disks with the images from the snapshot:<br>
   *
   * <ul>
   *   <li>Existing disks are updated.
   *   <li>Disks that don't exist anymore get re-added.
   *       <ul>
   *         <li>If the image is still in the DB, the disk is linked to it.
   *         <li>If the image is not in the DB anymore, the disk will be marked as "broken"
   *       </ul>
   * </ul>
   *
   * @param vmId The VM ID is needed to re-add disks.
   * @param snapshotId The snapshot ID is used to find only the VM disks at the time.
   * @param disksFromSnapshot The disks that existed in the snapshot.
   */
  protected void synchronizeDisksFromSnapshot(
      Guid vmId, Guid snapshotId, Guid activeSnapshotId, List<DiskImage> disksFromSnapshot) {
    List<Guid> diskIdsFromSnapshot = new ArrayList<Guid>();

    // Sync disks that exist or existed in the snapshot.
    for (DiskImage diskImage : disksFromSnapshot) {
      diskIdsFromSnapshot.add(diskImage.getimage_group_id());
      Disk disk = diskImage.getDisk();
      if (getDiskDao().exists(disk.getId())) {
        getDiskDao().update(disk);
      } else {

        // If can't find the image, insert it as illegal so that it can't be used and make the
        // device unplugged.
        if (getDiskImageDao().getSnapshotById(diskImage.getId()) == null) {
          diskImage.setimageStatus(ImageStatus.ILLEGAL);
          diskImage.setvm_snapshot_id(activeSnapshotId);

          ImagesHandler.addImage(
              diskImage,
              true,
              new image_storage_domain_map(diskImage.getId(), diskImage.getstorage_ids().get(0)));
        }

        ImagesHandler.addDiskToVm(disk, vmId);
      }
    }

    // Remove all disks that didn't exist in the snapshot.
    for (VmDevice vmDevice :
        getVmDeviceDao()
            .getVmDeviceByVmIdTypeAndDevice(
                vmId, VmDeviceType.DISK.getName(), VmDeviceType.DISK.getName())) {
      if (!diskIdsFromSnapshot.contains(vmDevice.getDeviceId())) {
        getDiskDao().remove(vmDevice.getDeviceId());
        getVmDeviceDao().remove(vmDevice.getId());
      }
    }
  }
Exemplo n.º 11
0
  public static DiskModel DiskToModel(Disk disk) {
    DiskModel diskModel = new DiskModel();
    diskModel.setIsNew(true);
    diskModel.getAlias().setEntity(disk.getDiskAlias());

    if (disk.getDiskStorageType() == DiskStorageType.IMAGE) {
      DiskImage diskImage = (DiskImage) disk;
      EntityModel sizeEntity = new EntityModel();
      sizeEntity.setEntity(diskImage.getSizeInGigabytes());
      diskModel.setSize(sizeEntity);
      ListModel volumeList = new ListModel();
      volumeList.setItems(
          (diskImage.getvolume_type() == VolumeType.Preallocated
              ? new ArrayList<VolumeType>(Arrays.asList(new VolumeType[] {VolumeType.Preallocated}))
              : DataProvider.GetVolumeTypeList()));
      volumeList.setSelectedItem(diskImage.getvolume_type());
      diskModel.setVolumeType(volumeList);
    }

    diskModel.setDisk(disk);

    return diskModel;
  }
Exemplo n.º 12
0
  private boolean checkCanPerformRegularUpdate() {
    boolean retValue = true;
    if (_oldDisk.getDiskInterface() != getParameters().getDiskInfo().getDiskInterface()) {
      List<VmNetworkInterface> allVmInterfaces =
          DbFacade.getInstance().getVmNetworkInterfaceDao().getAllForVm(getVmId());

      List<Disk> allVmDisks = getDiskDao().getAllForVm(getVmId());
      allVmDisks.removeAll(
          LinqUtils.filter(
              allVmDisks,
              new Predicate<Disk>() {
                @Override
                public boolean eval(Disk o) {
                  return o.getId().equals(_oldDisk.getId());
                }
              }));
      allVmDisks.add(getParameters().getDiskInfo());
      if (!checkPciAndIdeLimit(
          getVm().getnum_of_monitors(),
          allVmInterfaces,
          allVmDisks,
          getReturnValue().getCanDoActionMessages())) {
        retValue = false;
      }
    }

    // Validate update boot disk.
    if (retValue && getParameters().getDiskInfo().isBoot()) {
      VmHandler.updateDisksFromDb(getVm());
      for (Disk disk : getVm().getDiskMap().values()) {
        if (disk.isBoot() && !disk.getId().equals(_oldDisk.getId())) {
          retValue = false;
          addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_DISK_BOOT_IN_USE);
          getReturnValue()
              .getCanDoActionMessages()
              .add(String.format("$DiskName %1$s", disk.getDiskAlias()));
          break;
        }
      }
    }

    // Set disk alias name in the disk retrieved from the parameters.
    ImagesHandler.setDiskAlias(getParameters().getDiskInfo(), getVm());
    return retValue && validateShareableDisk();
  }
Exemplo n.º 13
0
 @Override
 public String getDiskAlias() {
   return _oldDisk.getDiskAlias();
 }
 @Override
 protected <T> boolean matchEntity(
     org.ovirt.engine.core.common.businessentities.Disk entity, T id) {
   return id != null && (id.equals(entity.getId()));
 }
 @Override
 public VdcActionParametersBase getParameters(
     Disk incoming, org.ovirt.engine.core.common.businessentities.Disk entity) {
   return new UpdateVmDiskParameters(parentId, entity.getId(), map(incoming, entity));
 }
 @Override
 public String toString() {
   return String.format("%s, volumeId = %s", super.toString(), disk.getId());
 }