From 706910e440527c22f176bcab0032c37c60357c25 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Fri, 29 Jan 2021 12:25:44 +0100 Subject: Add support for QEMU VMs (based on Libvirt domain XML documents) --- src/main/java/org/openslx/util/vm/DiskImage.java | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/main/java/org/openslx/util/vm/DiskImage.java') diff --git a/src/main/java/org/openslx/util/vm/DiskImage.java b/src/main/java/org/openslx/util/vm/DiskImage.java index 1602926..617fadd 100644 --- a/src/main/java/org/openslx/util/vm/DiskImage.java +++ b/src/main/java/org/openslx/util/vm/DiskImage.java @@ -5,6 +5,8 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Arrays; +import java.util.List; +import java.util.function.Predicate; import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.Virtualizer; @@ -318,7 +320,40 @@ public class DiskImage } throw new UnknownImageFormatException(); } + + /** + * Creates new disk image and checks if image is supported by hypervisor's image formats. + * + * @param disk file to a disk storing the virtual machine content. + * @param supportedImageTypes list of supported image types of a hypervisor's image format. + * @throws FileNotFoundException cannot find virtual machine image file. + * @throws IOException cannot access the virtual machine image file. + * @throws UnknownImageFormatException virtual machine image file has an unknown image format. + */ + public DiskImage( File disk, List supportedImageTypes ) + throws FileNotFoundException, IOException, UnknownImageFormatException + { + this( disk ); + if ( !this.isSupportedByHypervisor( supportedImageTypes ) ) { + throw new UnknownImageFormatException(); + } + } + + /** + * Checks if image format is supported by a list of supported hypervisor image formats. + * + * @param supportedImageTypes list of supported image types of a hypervisor. + * @return true if image type is supported by the hypervisor; otherwise + * false. + */ + public boolean isSupportedByHypervisor( List supportedImageTypes ) + { + Predicate matchDiskFormat = supportedImageType -> supportedImageType.toString() + .equalsIgnoreCase( this.getImageFormat().toString() ); + return supportedImageTypes.stream().anyMatch( matchDiskFormat ); + } + private int findNull( byte[] buffer ) { for ( int i = 0; i < buffer.length; ++i ) { -- cgit v1.2.3-55-g7522