Exemple #1
0
  /**
   * copy disk.
   *
   * @param srcFile the source disk
   * @return a new disk object
   * @throws Exception
   */
  public VAFile copyDisk(VAFile srcFile) throws Exception {
    // check disk
    checkDiskType(srcFile);

    // if the file has parent file, it can't be copied
    VADisk srcDisk = new VADisk(srcFile);
    if (!srcDisk.getParent().equals(VAMConstants.NULL)) {
      throw new Exception("Can't copy the disk");
    }

    // create new VAFile object
    VAFile vafile =
        createFile(
            srcFile.getId(),
            null,
            VAMConstants.VAF_FILE_TYPE_DISK,
            srcFile.getFormat(),
            srcFile.getSize());

    VADisk vadisk = new VADisk(vafile);
    vadisk.setCapacity(srcDisk.getCapacity());
    vadisk.setParent(srcFile.getGuid());
    vadisk.setReplica(true);

    // get file data access object
    VAFileDao fileDao = DaoFactory.getVAFileDao();
    // add the file to the database
    vafile = fileDao.add(vadisk);
    if (vafile == null) {
      throw new Exception("Can't add the file to the database.");
    }

    try {
      // copy disk
      FileOperation.getInstance()
          .copyDisk(
              VAMConfig.getNfsHost(),
              VAMConfig.getNfsUser(),
              vafile,
              srcFile.getSavePath(),
              vafile.getSavePath());
    } catch (Exception e) {
      // remove file in the database
      DaoFactory.getVAFileDao().remove(vafile.getGuid());
      throw new Exception(e);
    }

    return vafile;
  }
Exemple #2
0
  /**
   * create a virtual disk.
   *
   * @param id disk id
   * @param format disk format
   * @param capacity disk capacity
   * @return file object
   * @throws Exception
   */
  public VAFile createDisk(VAFile vafile, long capacity) throws Exception {
    // check disk
    checkDiskType(vafile);

    // create a new disk
    VADisk vadisk = new VADisk(vafile);
    vadisk.setCapacity(capacity);

    // get file data access object
    VAFileDao fileDao = DaoFactory.getVAFileDao();
    // add the file to the database
    vafile = fileDao.add(vadisk);
    if (vafile == null) {
      throw new Exception("Can't add the file to the database.");
    }

    try {
      // create disk
      FileOperation.getInstance()
          .createDisk(
              VAMConfig.getNfsHost(),
              VAMConfig.getNfsUser(),
              vafile,
              vafile.getSavePath(),
              capacity);
    } catch (Exception e) {
      // remove file in the database
      DaoFactory.getVAFileDao().remove(vafile.getGuid());
      throw new Exception(e);
    }

    return vafile;
  }
Exemple #3
0
  /**
   * create snapshot of a disk.
   *
   * @param srcFile the parent disk file object
   * @param dstFile the snapshot file object
   * @param host the name or IP of host creating snapshot
   * @param user the user name or IP of host creating snapshot
   * @return snapshot file object
   * @throws Exception
   */
  public VAFile createSnapshot(VAFile srcFile, VAFile dstFile, String host, String user)
      throws Exception {
    // check file type
    checkDiskType(srcFile);

    // get file data access object
    VAFileDao fileDao = DaoFactory.getVAFileDao();

    VADisk instDisk = new VADisk(srcFile);

    // create new disk and set parent
    VADisk vadisk = new VADisk(dstFile);
    vadisk.setCapacity(instDisk.getCapacity());
    vadisk.setParent(srcFile.getGuid());

    // add the file to the database
    dstFile = fileDao.add(vadisk);
    if (dstFile == null) {
      throw new Exception("Can't add the file to the database.");
    }

    try {
      String path = null;
      String backingPath = null;
      if (host == null || user == null) {
        host = VAMConfig.getNfsHost();
        user = VAMConfig.getNfsUser();
      }

      path = dstFile.getSavePath();
      backingPath = srcFile.getSavePath();

      // create snapshot
      FileOperation.getInstance().createSnapshot(dstFile, host, user, path, backingPath);
    } catch (Exception e) {
      // remove file in the database
      fileDao.remove(dstFile.getGuid());
      throw new Exception(e);
    }

    return dstFile;
  }
Exemple #4
0
  /**
   * remove file by GUID.
   *
   * @param guid file GUID
   * @return whether remove the file successfully
   * @throws Exception
   */
  public boolean removeFile(String host, String user, String guid) throws Exception {
    // get file data access object
    VAFileDao fileDao = DaoFactory.getVAFileDao();

    // get the VAFile object with the GUID
    VAFile vafile = fileDao.query(guid);

    if (vafile == null) {
      // if the file is already deleted, should not throw exception
      return true;
      // throw new Exception("The file with guid \"" + guid
      //		+ "\" is not existed or available");
    }

    if (vafile.getParent().equals(VAMConstants.NULL)) {
      if (fileDao.getActiveReference(vafile.getGuid()) > 0) {
        throw new Exception("The file is in used");
      }
    } else {
      if (vafile.getRef() > 0) {
        throw new Exception("The file is in used");
      }
    }

    int oldState = vafile.getState();
    try {
      // remove file
      FileOperation.getInstance().removeFile(host, user, vafile, vafile.getSavePath());
    } catch (RuntimeException e) {
      // restore state
      vafile.setState(oldState);
      fileDao.update(vafile);
      throw new Exception(e);
    }

    return true;
  }
Exemple #5
0
  /**
   * clear incorrect file.
   *
   * @param clearFlag the clear flag, which supports VAMConstants.FILE_CLEAR_FLAG_ERROR,
   *     VAMConstants.FILE_CLEAR_FLAG_NOT_READY, VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_DATA,
   *     VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_FILE, VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_PARENT,
   *     it can use or operation,
   * @throws Exception
   */
  public void clearFile(int clearFlag) throws Exception {
    VAFileDao fileDao = DaoFactory.getVAFileDao();
    List<VAFile> fileList = getAllFile();

    // clear the file whose data can't be found in the database
    if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_DATA) != 0) {
      Map<String, String> pathMap = new HashMap<String, String>();
      for (int i = 0; i < fileList.size(); i++) {
        VAFile va = (VAFile) fileList.get(i);
        pathMap.put(va.getSavePath(), "ok");
      }

      File fileDir = new File(VAMConfig.getFileDirLocation());
      if (fileDir.exists() && fileDir.isDirectory()) {
        checkFile(fileDir, pathMap);
      }
    }

    // clear the file whose physical file can't be found
    if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_FILE) != 0) {
      File fileDir = new File(VAMConfig.getFileDirLocation());
      if (fileDir.exists() && fileDir.isDirectory()) {
        for (int i = 0; i < fileList.size(); i++) {
          VAFile va = (VAFile) fileList.get(i);
          File file = new File(va.getSavePath());
          if (!file.exists()) {
            fileDao.remove(va.getGuid());
          }
        }
      }
    }

    // clear the file whose state is error
    if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_ERROR) != 0) {
      List<VAObject> errorFiles = fileDao.getErrorFiles();
      for (int i = 0; i < errorFiles.size(); i++) {
        try {
          removeFile(null, null, errorFiles.get(i).getGuid());
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    // clear the file whose state is not ready
    if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_NOT_READY) != 0) {
      List<VAObject> notReadyFiles = fileDao.getNotReadyFiles();
      for (int i = 0; i < notReadyFiles.size(); i++) {
        VAFile va = new VAFile(notReadyFiles.get(i));
        if (VAMUtil.getTimestamp() - va.getTimestamp() > VAMConstants.HOUR / 2) {
          removeFile(null, null, va.getGuid());
        }
      }
    }

    // clear the file whose parent can't be found
    if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_PARENT) != 0) {
      fileList = getAllFile();
      Map<String, String> guidMap = new HashMap<String, String>();
      for (int i = 0; i < fileList.size(); i++) {
        VAFile va = (VAFile) fileList.get(i);
        guidMap.put(va.getGuid(), "ok");
      }
      for (int i = 0; i < fileList.size(); i++) {
        VAFile va = (VAFile) fileList.get(i);
        if (!va.getParent().equals(VAMConstants.NULL)
            && guidMap.get(va.getParent()) == null
            && va.getRef() == 0) {
          removeFile(null, null, va.getGuid());
        }
      }
    }
  }
Exemple #6
0
  /**
   * add file.
   *
   * @param vafile file object
   * @param storeLoc the file location
   * @param deleteSrcFile whether delete the source file after add file
   * @return file object
   * @throws Exception
   */
  public VAFile addFile(VAFile vafile, String storeLoc, boolean deleteSrcFile) throws Exception {
    // check whether the store file is existed
    String storePath = storeLoc;
    if (storePath != null) {
      File storeFile = new File(storePath);
      if (!storeFile.exists()) {
        throw new Exception("The file \"" + storePath + "\" is not existed!");
      }

      vafile.setSize(storeFile.length());
    }

    // check file path
    VAMUtil.checkFile(null, vafile.getLocation());

    // get file data access object
    VAFileDao fileDao = DaoFactory.getVAFileDao();
    // add the file to the database
    vafile = fileDao.add(vafile);
    if (vafile == null) {
      throw new Exception("Can't add the file to the database.");
    }

    try {
      // if the file type is disk, convert disk format
      if (vafile.getFileType().equals(VAMConstants.VAF_FILE_TYPE_DISK)) {
        vafile.setSrcPath(storeLoc);
        DiskInfo info = VAMUtil.getDiskInfo(storeLoc);
        String format = null;
        String backingFile = null;
        if (info != null) {
          format = info.getFormat();
          backingFile = info.getBackingFile();
        }
        if (format != null
            && format.equalsIgnoreCase(VAMConfig.getImageFormat())
            && backingFile == null) {
          VADisk disk = new VADisk(vafile);
          disk.setCapacity(info.getVirtualSize());
          disk.setSize(info.getDiskSize());

          FileOperation.getInstance()
              .moveFile(
                  VAMConfig.getNfsHost(),
                  VAMConfig.getNfsUser(),
                  disk,
                  storeLoc,
                  disk.getSavePath());
        } else {
          convertDiskFormat(vafile, storeLoc, VAMConfig.getImageFormat(), deleteSrcFile);
        }

      } else if (storeLoc != null) { // move file
        FileOperation.getInstance()
            .moveFile(
                VAMConfig.getNfsHost(),
                VAMConfig.getNfsUser(),
                vafile,
                storeLoc,
                vafile.getSavePath());
      }
    } catch (Exception e) {
      // remove the file in the database
      DaoFactory.getVAFileDao().remove(vafile.getGuid());
      throw new Exception(e);
    }

    return vafile;
  }