public boolean save() { for (FormatInfo info : _formats) { String formatExtension = info.format.getFileExtension(); _props.setProperty(formatExtension, "true"); _props.setProperty(formatExtension + ".filename", info.filename); _props.setProperty(formatExtension + ".size", Long.toString(info.size)); _props.setProperty(formatExtension + ".virtualsize", Long.toString(info.virtualSize)); } try (FileOutputStream strm = new FileOutputStream(_file);) { _props.store(strm, ""); } catch (IOException e) { s_logger.warn("Unable to save the template properties ", e); return false; } return true; }
protected FormatInfo deleteFormat(ImageFormat format) { Iterator<FormatInfo> it = _formats.iterator(); while (it.hasNext()) { FormatInfo info = it.next(); if (info.format == format) { it.remove(); String formatExtension = format.getFileExtension(); _props.remove(formatExtension); for(String propertySuffix : Arrays.asList("filename","size","virtualsize")) { _props.remove(formatExtension + "." + propertySuffix); } return info; } } return null; } }
public long getTemplateVirtualSize(String templatePath, String templateName) throws InternalErrorException { long virtualSize = 0; String templateFileFullPath = templatePath.endsWith(File.separator) ? templatePath : templatePath + File.separator; templateFileFullPath += templateName.endsWith(ImageFormat.VMDK.getFileExtension()) ? templateName : templateName + "." + ImageFormat.VMDK.getFileExtension(); try ( FileReader fileReader = new FileReader(templateFileFullPath); BufferedReader bufferedReader = new BufferedReader(fileReader); ) { Pattern regex = Pattern.compile("(RW|RDONLY|NOACCESS) (\\d+) (FLAT|SPARSE|ZERO|VMFS|VMFSSPARSE|VMFSDRM|VMFSRAW)"); String line = null; while((line = bufferedReader.readLine()) != null) { Matcher m = regex.matcher(line); if (m.find( )) { long sectors = Long.parseLong(m.group(2)); virtualSize = sectors * 512; break; } } } catch(FileNotFoundException ex) { String msg = "Unable to open file '" + templateFileFullPath + "' " + ex.toString(); s_logger.error(msg); throw new InternalErrorException(msg); } catch(IOException ex) { String msg = "Unable read open file '" + templateFileFullPath + "' " + ex.toString(); s_logger.error(msg); throw new InternalErrorException(msg); } s_logger.debug("vmdk file had size="+virtualSize); return virtualSize; }
s_logger.info("MDOVE Secondary storage mount point: " + secondaryMountPoint); String srcOVAFileName = getTemplateOnSecStorageFilePath(secondaryMountPoint, templateRelativeFolderPath, templateInfo.second(), ImageFormat.OVA.getFileExtension());
String templateFilePath = templatePath + File.separator + templateName + "." + ImageFormat.OVA.getFileExtension(); if (!_storage.exists(templateFilePath)) { if (s_logger.isInfoEnabled()) { String templateFileFullPath = templatePath + File.separator + templateName + "." + ImageFormat.OVA.getFileExtension(); File templateFile = new File(templateFileFullPath); Script command = new Script("tar", processTimeout, s_logger); info.filename = templateName + "." + ImageFormat.OVA.getFileExtension(); info.size = _storage.getSize(templateFilePath); info.virtualSize = getTemplateVirtualSize(templatePath, info.filename);
String currentExtension = format.getFileExtension(); String ext = _props.getProperty(currentExtension); if (ext != null) {
final String volumeName = UUID.randomUUID().toString(); final String destVolumeName = volumeName + "." + destFormat.getFileExtension(); final KVMPhysicalDisk volume = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), srcVolumePath); volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString()));
try { NfsTO nfsCacheStore = (NfsTO)cacheStore; String fileName = cmd.getName() + "." + cmd.getFormat().getFileExtension(); file = downloadFromUrlToNfs(cmd.getUrl(), nfsCacheStore, path, fileName); String container = "T-" + cmd.getId();
if ((vInfo.getSize() == vInfo.getPhysicalSize()) && (vInfo.getInstallPath().endsWith(ImageFormat.OVA.getFileExtension()))) { try { Processor processor = _processors.get("OVA Processor");
if ((tInfo.getSize() == tInfo.getPhysicalSize()) && (tInfo.getInstallPath().endsWith(ImageFormat.OVA.getFileExtension()))) { try { Processor processor = _processors.get("OVA Processor");
@Override public FormatInfo process(String templatePath, ImageFormat format, String templateName, long processTimeout) throws InternalErrorException { if (format != null) { s_logger.debug("We currently don't handle conversion from " + format + " to VHD."); return null; } String vhdPath = templatePath + File.separator + templateName + "." + ImageFormat.VHD.getFileExtension(); if (!_storage.exists(vhdPath)) { s_logger.debug("Unable to find the vhd file: " + vhdPath); return null; } File vhdFile = _storage.getFile(vhdPath); FormatInfo info = new FormatInfo(); info.format = ImageFormat.VHD; info.filename = templateName + "." + ImageFormat.VHD.getFileExtension(); info.size = _storage.getSize(vhdPath); try { info.virtualSize = getTemplateVirtualSize(vhdFile); } catch (IOException e) { s_logger.error("Unable to get the virtual size for " + vhdPath); throw new InternalErrorException("unable to get virtual size from vhd file"); } return info; }
@Override public String createEntityExtractUrl(DataStore store, String installPath, ImageFormat format, DataObject dataObject) { // find an endpoint to send command EndPoint ep = _epSelector.select(store); // Create Symlink at ssvm String path = installPath; String uuid = UUID.randomUUID().toString() + "." + format.getFileExtension(); CreateEntityDownloadURLCommand cmd = new CreateEntityDownloadURLCommand(((ImageStoreEntity)store).getMountPoint(), path, uuid, dataObject.getTO()); Answer ans = null; if (ep == null) { String errMsg = "No remote endpoint to send command, check if host or ssvm is down?"; s_logger.error(errMsg); ans = new Answer(cmd, false, errMsg); } else { ans = ep.sendMessage(cmd); } if (ans == null || !ans.getResult()) { String errorString = "Unable to create a link for entity at " + installPath + " on ssvm," + ans.getDetails(); s_logger.error(errorString); throw new CloudRuntimeException(errorString); } // Construct actual URL locally now that the symlink exists at SSVM return generateCopyUrl(ep.getPublicAddr(), uuid); }
public long getTemplateVirtualSize(String templatePath, String templateName) throws InternalErrorException { // get the virtual size from the OVF file meta data long virtualSize = 0; String templateFileFullPath = templatePath.endsWith(File.separator) ? templatePath : templatePath + File.separator; templateFileFullPath += templateName.endsWith(ImageFormat.OVA.getFileExtension()) ? templateName : templateName + "." + ImageFormat.OVA.getFileExtension(); String ovfFileName = getOVFFilePath(templateFileFullPath); if (ovfFileName == null) { String msg = "Unable to locate OVF file in template package directory: " + templatePath; s_logger.error(msg); throw new InternalErrorException(msg); } try { Document ovfDoc = null; ovfDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(ovfFileName)); Element disk = (Element)ovfDoc.getElementsByTagName("Disk").item(0); virtualSize = Long.parseLong(disk.getAttribute("ovf:capacity")); String allocationUnits = disk.getAttribute("ovf:capacityAllocationUnits"); virtualSize = OVFHelper.getDiskVirtualSize(virtualSize, allocationUnits, ovfFileName); return virtualSize; } catch (Exception e) { String msg = "getTemplateVirtualSize: Unable to parse OVF XML document " + templatePath + " to get the virtual disk " + templateName + " size due to " + e; s_logger.error(msg); throw new InternalErrorException(msg); } }
@Override public FormatInfo process(String templatePath, ImageFormat format, String templateName, long processTimeout) throws InternalErrorException { if (format != null) { s_logger.debug("We currently don't handle conversion from " + format + " to QCOW2."); return null; } String qcow2Path = templatePath + File.separator + templateName + "." + ImageFormat.QCOW2.getFileExtension(); if (!_storage.exists(qcow2Path)) { s_logger.debug("Unable to find the qcow2 file: " + qcow2Path); return null; } FormatInfo info = new FormatInfo(); info.format = ImageFormat.QCOW2; info.filename = templateName + "." + ImageFormat.QCOW2.getFileExtension(); File qcow2File = _storage.getFile(qcow2Path); info.size = _storage.getSize(qcow2Path); try { info.virtualSize = getTemplateVirtualSize(qcow2File); } catch (IOException e) { s_logger.error("Unable to get virtual size from " + qcow2File.getName()); throw new InternalErrorException("unable to get virtual size from qcow2 file"); } return info; }
@Override public FormatInfo process(String templatePath, ImageFormat format, String templateName, long processTimeout) throws InternalErrorException { if (format != null) { if (s_logger.isInfoEnabled()) { s_logger.info("We currently don't handle conversion from " + format + " to VMDK."); } return null; } s_logger.info("Template processing. templatePath: " + templatePath + ", templateName: " + templateName); String templateFilePath = templatePath + File.separator + templateName + "." + ImageFormat.VMDK.getFileExtension(); if (!_storage.exists(templateFilePath)) { if (s_logger.isInfoEnabled()) { s_logger.info("Unable to find the vmware template file: " + templateFilePath); } return null; } FormatInfo info = new FormatInfo(); info.format = ImageFormat.VMDK; info.filename = templateName + "." + ImageFormat.VMDK.getFileExtension(); info.size = _storage.getSize(templateFilePath); info.virtualSize = getTemplateVirtualSize(templatePath, info.filename); return info; }
@Override public FormatInfo process(String templatePath, ImageFormat format, String templateName, long processTimeout) { if (format != null) { s_logger.debug("We currently don't handle conversion from " + format + " to TAR."); return null; } String tarPath = templatePath + File.separator + templateName + "." + ImageFormat.TAR.getFileExtension(); if (!_storage.exists(tarPath)) { s_logger.debug("Unable to find the tar file: " + tarPath); return null; } FormatInfo info = new FormatInfo(); info.format = ImageFormat.TAR; info.filename = templateName + "." + ImageFormat.TAR.getFileExtension(); File tarFile = _storage.getFile(tarPath); info.size = _storage.getSize(tarPath); info.virtualSize = getVirtualSize(tarFile); return info; }
@Override public FormatInfo process(String templatePath, ImageFormat format, String templateName, long processTimeout) { if (format != null) { s_logger.debug("We don't handle conversion from " + format + " to ISO."); return null; } String isoPath = templatePath + File.separator + templateName + "." + ImageFormat.ISO.getFileExtension(); if (!_storage.exists(isoPath)) { s_logger.debug("Unable to find the iso file: " + isoPath); return null; } FormatInfo info = new FormatInfo(); info.format = ImageFormat.ISO; info.filename = templateName + "." + ImageFormat.ISO.getFileExtension(); info.size = _storage.getSize(isoPath); info.virtualSize = info.size; return info; }
@Override public FormatInfo process(String templatePath, ImageFormat format, String templateName, long processTimeout) throws InternalErrorException { if (format != null) { s_logger.debug("We currently don't handle conversion from " + format + " to raw image."); return null; } String imgPath = templatePath + File.separator + templateName + "." + ImageFormat.RAW.getFileExtension(); if (!_storage.exists(imgPath)) { s_logger.debug("Unable to find raw image:" + imgPath); return null; } FormatInfo info = new FormatInfo(); info.format = ImageFormat.RAW; info.filename = templateName + "." + ImageFormat.RAW.getFileExtension(); info.size = _storage.getSize(imgPath); info.virtualSize = info.size; s_logger.debug("Process raw image " + info.filename + " successfully"); return info; }
@Override public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException { TemplateProfile profile = super.prepare(cmd); String url = profile.getUrl(); UriUtils.validateUrl(ImageFormat.ISO.getFileExtension(), url); if (cmd.isDirectDownload()) { DigestHelper.validateChecksumString(cmd.getChecksum()); Long templateSize = performDirectDownloadUrlValidation(url); profile.setSize(templateSize); } profile.setUrl(url); // Check that the resource limit for secondary storage won't be exceeded _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(cmd.getEntityOwnerId()), ResourceType.secondary_storage, UriUtils.getRemoteSize(url)); return profile; }
private VolumeInfo copyVolume(StoragePool rootDiskPool, VolumeInfo volume, VirtualMachine vm, VirtualMachineTemplate rootDiskTmplt, DataCenter dcVO, Pod pod, DiskOffering diskVO, ServiceOffering svo, HypervisorType rootDiskHyperType) throws NoTransitionException { if (!isSupportedImageFormatForCluster(volume, rootDiskHyperType)) { throw new InvalidParameterValueException("Failed to attach volume to VM since volumes format " + volume.getFormat().getFileExtension() + " is not compatible with the vm hypervisor type"); } VolumeInfo volumeOnPrimary = copyVolumeFromSecToPrimary(volume, vm, rootDiskTmplt, dcVO, pod, rootDiskPool.getClusterId(), svo, diskVO, new ArrayList<StoragePool>(), volume.getSize(), rootDiskHyperType); return volumeOnPrimary; }