diff options
author | Manuel Bentele | 2021-02-25 15:00:38 +0100 |
---|---|---|
committer | Manuel Bentele | 2021-03-10 15:05:56 +0100 |
commit | be40e979e03e41ddcd831d9c330902f76908ca64 (patch) | |
tree | 0b5d66d2e01bfb7b96c76170db788b5f36fd8b2d /src/main/java/org/openslx/vm/disk/DiskImageVdi.java | |
parent | [vmware] Stop creating 'null.present = "TRUE"' entries (diff) | |
download | master-sync-shared-be40e979e03e41ddcd831d9c330902f76908ca64.tar.gz master-sync-shared-be40e979e03e41ddcd831d9c330902f76908ca64.tar.xz master-sync-shared-be40e979e03e41ddcd831d9c330902f76908ca64.zip |
Refactor disk image representation and add unit tests
Diffstat (limited to 'src/main/java/org/openslx/vm/disk/DiskImageVdi.java')
-rw-r--r-- | src/main/java/org/openslx/vm/disk/DiskImageVdi.java | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/vm/disk/DiskImageVdi.java b/src/main/java/org/openslx/vm/disk/DiskImageVdi.java new file mode 100644 index 0000000..1c34c1d --- /dev/null +++ b/src/main/java/org/openslx/vm/disk/DiskImageVdi.java @@ -0,0 +1,119 @@ +package org.openslx.vm.disk; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * VDI disk image for virtual machines. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class DiskImageVdi extends DiskImage +{ + /** + * Big endian representation of the little endian VDI magic bytes (signature). + */ + private static final int VDI_MAGIC = 0x7f10dabe; + + /** + * Creates a new VDI disk image from an existing VDI image file. + * + * @param diskImage file to a VDI disk storing the image content. + * + * @throws FileNotFoundException cannot find specified VDI disk image file. + * @throws IOException cannot access the content of the VDI disk image file. + */ + public DiskImageVdi( File diskImage ) throws FileNotFoundException, IOException + { + super( diskImage ); + } + + /** + * Creates a new VDI disk image from an existing VDI image file. + * + * @param diskImage file to a VDI disk storing the image content. + */ + public DiskImageVdi( RandomAccessFile diskImage ) + { + super( diskImage ); + } + + /** + * Probe specified file with unknown format to be a VDI disk image file. + * + * @param diskImage file with unknown format that should be probed. + * @return state whether file is a VDI disk image or not. + * + * @throws DiskImageException cannot probe specified file with unknown format. + */ + public static boolean probe( RandomAccessFile diskImage ) throws DiskImageException + { + final boolean isVdiImageFormat; + + // goto the beginning of the disk image to read the magic bytes + // skip first 64 bytes (opening tag) + final int diskImageMagic = DiskImageUtils.readInt( diskImage, 64 ); + + // check if disk image's magic bytes can be found + if ( diskImageMagic == DiskImageVdi.VDI_MAGIC ) { + isVdiImageFormat = true; + } else { + isVdiImageFormat = false; + } + + return isVdiImageFormat; + } + + @Override + public boolean isStandalone() throws DiskImageException + { + // VDI does not seem to support split VDI files, so VDI files are always standalone + return true; + } + + @Override + public boolean isCompressed() throws DiskImageException + { + // compression is done by sparsifying the disk files, there is no flag for it + return false; + } + + @Override + public boolean isSnapshot() throws DiskImageException + { + final RandomAccessFile diskFile = this.getDiskImage(); + + // if parent UUID is set, the VDI file is a snapshot + final String parentUuid = DiskImageUtils.readBytesAsString( diskFile, 440, 16 ); + final String zeroUuid = new String( new byte[ 16 ] ); + + return !zeroUuid.equals( parentUuid ); + } + + @Override + public int getVersion() throws DiskImageException + { + final RandomAccessFile diskFile = this.getDiskImage(); + + final short vdiVersionMajor = Short.reverseBytes( DiskImageUtils.readShort( diskFile, 68 ) ); + final short vdiVersionMinor = Short.reverseBytes( DiskImageUtils.readShort( diskFile, 70 ) ); + + return DiskImageUtils.versionFromMajorMinor( vdiVersionMajor, vdiVersionMinor ); + } + + @Override + public String getDescription() throws DiskImageException + { + final RandomAccessFile diskFile = this.getDiskImage(); + return DiskImageUtils.readBytesAsString( diskFile, 84, 256 ); + } + + @Override + public ImageFormat getFormat() + { + return ImageFormat.VDI; + } +} |