blob: 92138a1035527896f0b20a2d30f4e09bf3dea2e3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package org.openslx.util.vm;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.openslx.bwlp.thrift.iface.Virtualizer;
public class DiskImage
{
/**
* Big endian representation of the 4 bytes 'VMDK'
*/
private static final int VMDK_MAGIC = 0x4b444d56;
public enum ImageFormat
{
VMDK( "vmdk" ),
QCOW2( "qcow2" ),
VDI( "vdi" );
public final String extension;
private ImageFormat( String extension )
{
this.extension = extension;
}
public ImageFormat defaultForVirtualizer( Virtualizer virt )
{
if ( virt == null )
return null;
if ( virt.virtId.equals( "vmware" ) )
return VMDK;
if ( virt.virtId.equals( "virtualbox" ) )
return VDI;
return null;
}
}
public final boolean isStandalone;
public final boolean isCompressed;
public final ImageFormat format;
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 )
throw new UnknownImageFormatException();
file.seek( 512 );
byte[] buffer = new byte[ 2048 ];
file.readFully( buffer );
VmwareConfig config = new VmwareConfig( buffer, findNull( buffer ) );
String ct = config.get( "createType" );
this.isStandalone = isStandaloneCreateType( ct );
this.isCompressed = ct != null && ct.equalsIgnoreCase( "streamOptimized" );
this.format = ImageFormat.VMDK;
}
}
private int findNull( byte[] buffer )
{
for ( int i = 0; i < buffer.length; ++i ) {
if ( buffer[i] == 0 )
return i;
}
return buffer.length;
}
private boolean isStandaloneCreateType( String type )
{
if ( type == null )
return false;
return type.equalsIgnoreCase( "streamOptimized" ) || type.equalsIgnoreCase( "monolithicSparse" );
}
public static class UnknownImageFormatException extends Exception
{
private static final long serialVersionUID = -6647935235475007171L;
}
}
|