summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/vm/DiskImage.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/util/vm/DiskImage.java')
-rw-r--r--src/main/java/org/openslx/util/vm/DiskImage.java86
1 files changed, 47 insertions, 39 deletions
diff --git a/src/main/java/org/openslx/util/vm/DiskImage.java b/src/main/java/org/openslx/util/vm/DiskImage.java
index ca016f2..30fea99 100644
--- a/src/main/java/org/openslx/util/vm/DiskImage.java
+++ b/src/main/java/org/openslx/util/vm/DiskImage.java
@@ -71,9 +71,14 @@ public class DiskImage
LOGGER.debug( "Validating disk file: " + disk.getAbsolutePath() );
try ( RandomAccessFile file = new RandomAccessFile( disk, "r" ) ) {
// vmdk
- if ( file.readInt() == VMDK_MAGIC ) {
- file.seek( 512 );
- byte[] buffer = new byte[ 2048 ];
+ boolean isVmdkMagic = ( file.readInt() == VMDK_MAGIC );
+ if ( isVmdkMagic || file.length() < 4096 ) {
+ if ( isVmdkMagic ) {
+ file.seek( 512 );
+ } else {
+ file.seek( 0 );
+ }
+ byte[] buffer = new byte[ (int)Math.min( 2048, file.length() ) ];
file.readFully( buffer );
VmwareConfig config;
try {
@@ -82,20 +87,23 @@ public class DiskImage
config = null;
}
if ( config != null ) {
- subFormat = config.get( "createType" );
+ String sf = config.get( "createType" );
String parent = config.get( "parentCID" );
- this.isStandalone = isStandaloneCreateType( subFormat, parent );
- this.isCompressed = subFormat != null && subFormat.equalsIgnoreCase( "streamOptimized" );
- this.isSnapshot = parent != null && !parent.equalsIgnoreCase( "ffffffff" );
- this.format = ImageFormat.VMDK;
- String hwv = config.get( "ddb.virtualHWVersion" );
- if ( hwv == null ) {
- this.hwVersion = 10;
- } else {
- this.hwVersion = Util.parseInt( hwv, 10 );
+ if ( sf != null || parent != null ) {
+ subFormat = sf;
+ this.isStandalone = isStandaloneCreateType( subFormat, parent );
+ this.isCompressed = subFormat != null && subFormat.equalsIgnoreCase( "streamOptimized" );
+ this.isSnapshot = parent != null && !parent.equalsIgnoreCase( "ffffffff" );
+ this.format = ImageFormat.VMDK;
+ String hwv = config.get( "ddb.virtualHWVersion" );
+ if ( hwv == null ) {
+ this.hwVersion = 10;
+ } else {
+ this.hwVersion = Util.parseInt( hwv, 10 );
+ }
+ this.diskDescription = null;
+ return;
}
- this.diskDescription = null;
- return;
}
}
// Format spec from: https://forums.virtualbox.org/viewtopic.php?t=8046
@@ -152,30 +160,30 @@ public class DiskImage
return;
}
- file.seek(0);
- //TODO: Standalone & Snapshot
- if (file.readInt() == QEMU_MAGIC) {
- //skip the next 14 ints as they don't interest us
- // - QFI version (4 bytes)
- // - backing file offset (8 bytes)
- // - backing (8 bytes)
- // - crypt method (4 bytes)
- // - l1 size (4 bytes)
- // - l1 table offset (8 bytes)
- // - refcount table offset (8 bytes)
- // - refcount cluster (4 bytes)
- file.skipBytes( 14 * 4 );
- this.isSnapshot = file.readInt() > 0;
- //skip the next 14 ints as they don't interest us
- file.seek(374);
- this.isCompressed = file.read() == 1;
- this.diskDescription = null;
- this.format = ImageFormat.QCOW2;
- this.isStandalone = true;
- this.subFormat = null;
- this.hwVersion = 0;
- return;
- }
+ file.seek(0);
+ //TODO: Standalone & Snapshot
+ if (file.readInt() == QEMU_MAGIC) {
+ //skip the next 14 ints as they don't interest us
+ // - QFI version (4 bytes)
+ // - backing file offset (8 bytes)
+ // - backing (8 bytes)
+ // - crypt method (4 bytes)
+ // - l1 size (4 bytes)
+ // - l1 table offset (8 bytes)
+ // - refcount table offset (8 bytes)
+ // - refcount cluster (4 bytes)
+ file.skipBytes( 14 * 4 );
+ this.isSnapshot = file.readInt() > 0;
+ //skip the next 14 ints as they don't interest us
+ file.seek(374);
+ this.isCompressed = file.read() == 1;
+ this.diskDescription = null;
+ this.format = ImageFormat.QCOW2;
+ this.isStandalone = true;
+ this.subFormat = null;
+ this.hwVersion = 0;
+ return;
+ }
}
throw new UnknownImageFormatException();
}