diff options
Diffstat (limited to 'src/main/java/org/openslx/util/vm/DiskImage.java')
-rw-r--r-- | src/main/java/org/openslx/util/vm/DiskImage.java | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/main/java/org/openslx/util/vm/DiskImage.java b/src/main/java/org/openslx/util/vm/DiskImage.java index 92138a1..4165183 100644 --- a/src/main/java/org/openslx/util/vm/DiskImage.java +++ b/src/main/java/org/openslx/util/vm/DiskImage.java @@ -11,15 +11,13 @@ public class DiskImage { /** - * Big endian representation of the 4 bytes 'VMDK' + * Big endian representation of the 4 bytes 'KDMV' */ private static final int VMDK_MAGIC = 0x4b444d56; public enum ImageFormat { - VMDK( "vmdk" ), - QCOW2( "qcow2" ), - VDI( "vdi" ); + VMDK( "vmdk" ), QCOW2( "qcow2" ), VDI( "vdi" ); public final String extension; @@ -28,13 +26,20 @@ public class DiskImage this.extension = extension; } - public ImageFormat defaultForVirtualizer( Virtualizer virt ) + public static ImageFormat defaultForVirtualizer( Virtualizer virt ) { if ( virt == null ) return null; - if ( virt.virtId.equals( "vmware" ) ) + return defaultForVirtualizer( virt.virtId ); + } + + public static ImageFormat defaultForVirtualizer( String virtId ) + { + if ( virtId == null ) + return null; + if ( virtId.equals( "vmware" ) ) return VMDK; - if ( virt.virtId.equals( "virtualbox" ) ) + if ( virtId.equals( "virtualbox" ) ) return VDI; return null; } @@ -44,11 +49,12 @@ public class DiskImage public final boolean isCompressed; public final ImageFormat format; - public DiskImage( File disk ) throws FileNotFoundException, IOException, UnknownImageFormatException + public DiskImage( File disk ) throws FileNotFoundException, IOException, + UnknownImageFormatException { // For now we only support VMDK... try ( RandomAccessFile file = new RandomAccessFile( disk, "r" ) ) { - if ( file.read() != VMDK_MAGIC ) + if ( file.readInt() != VMDK_MAGIC ) throw new UnknownImageFormatException(); file.seek( 512 ); byte[] buffer = new byte[ 2048 ]; @@ -56,7 +62,8 @@ public class DiskImage VmwareConfig config = new VmwareConfig( buffer, findNull( buffer ) ); String ct = config.get( "createType" ); this.isStandalone = isStandaloneCreateType( ct ); - this.isCompressed = ct != null && ct.equalsIgnoreCase( "streamOptimized" ); + this.isCompressed = ct != null + && ct.equalsIgnoreCase( "streamOptimized" ); this.format = ImageFormat.VMDK; } } @@ -74,7 +81,8 @@ public class DiskImage { if ( type == null ) return false; - return type.equalsIgnoreCase( "streamOptimized" ) || type.equalsIgnoreCase( "monolithicSparse" ); + return type.equalsIgnoreCase( "streamOptimized" ) + || type.equalsIgnoreCase( "monolithicSparse" ); } public static class UnknownImageFormatException extends Exception |