summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/vm/VboxMetaData.java
blob: 7e814faeb8b05e827e98d90a47f7bb666e8f197b (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package org.openslx.util.vm;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;
import org.w3c.dom.Element;

class VBoxSoundCardMeta
{
	public final boolean isPresent;
	public final String value;

	public VBoxSoundCardMeta( boolean present, String val )
	{
		isPresent = present;
		value = val;
	}
}

class VBoxDDAccelMeta
{
	public final boolean isPresent;

	public VBoxDDAccelMeta( boolean present )
	{
		isPresent = present;
	}
}

class VBoxHWVersionMeta
{
	public final int version;

	public VBoxHWVersionMeta( int vers )
	{
		version = vers;
	}
}

class VBoxEthernetDevTypeMeta
{
	public final String value;

	public VBoxEthernetDevTypeMeta( String val )
	{
		value = val;
	}
}

public class VboxMetaData extends VmMetaData<VBoxSoundCardMeta, VBoxDDAccelMeta, VBoxHWVersionMeta, VBoxEthernetDevTypeMeta>
{
	private static final Logger LOGGER = Logger.getLogger( VboxMetaData.class );

	private static final Virtualizer virtualizer = new Virtualizer( "virtualbox", "VirtualBox" );

	private final VboxConfig config;

	public VboxMetaData( List<OperatingSystem> osList, File file ) throws IOException, UnsupportedVirtualizerFormatException
	{
		super( osList );
		this.config = new VboxConfig( file );
		init();
	}

	public VboxMetaData( List<OperatingSystem> osList, byte[] vmContent, int length ) throws IOException, UnsupportedVirtualizerFormatException
	{
		super( osList );
		this.config = new VboxConfig( vmContent, length );
		init();
	}

	private void init()
	{
		populateTheMaps();

		this.config.init();
		displayName = config.getDisplayName();
		setOs( "virtualbox", config.getOsName() );

		for ( HardDisk hardDisk : config.getHdds() ) {
			hdds.add( hardDisk );
		}
	}

	// TODO DELETE LATER WHEN NOT NEEDED ANYMORE
	private void WriteToFile() throws TransformerFactoryConfigurationError, TransformerException
	{
		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		StreamResult output = new StreamResult( new File( "output.xml" ) );
		Source input = new DOMSource( config.getConfigDoc() );
		transformer.transform( input, output );
	}
	// TODO END DELETE LATER WHEN NOT NEEDED ANYMORE

	@Override
	public Virtualizer getVirtualizer()
	{
		return virtualizer;
	}

	@Override
	public void enableUsb( boolean enabled )
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void applySettingsForLocalEdit()
	{
		// TODO Auto-generated method stub
	}

	@Override
	public byte[] getFilteredDefinitionArray()
	{
		return config.toString().getBytes( StandardCharsets.UTF_8 );
	}

	@Override
	public boolean addHddTemplate( String diskImage, String hddMode, String redoDir )
	{
		config.changeAttribute( "HardDisk", "location", diskImage );
		return true;
	}

	@Override
	public boolean addHddTemplate( File diskImage, String hddMode, String redoDir )
	{
		String diskImagePath = diskImage.getName();
		config.changeAttribute( "HardDisk", "location", diskImagePath );

		UUID newhdduuid = UUID.randomUUID();
		LOGGER.debug( newhdduuid );

		// patching the new uuid in the vbox config file here
		String vboxUUid = "{" + newhdduuid.toString() + "}";
		config.changeAttribute( "HardDisk", "uuid", vboxUUid );
		config.changeAttribute( "Image", "uuid", vboxUUid );

		// write the new hdd uuid in the vdi file
		byte[] bytesToWrite = new byte[ 16 ];

		int[] bytesOffset = { 32, 40, 48, 56, 16, 24, 0, 8, 56, 48, 40, 32, 24, 16, 8, 0 };

		int offset = 0;
		for ( int i = 0; i < 2; i++ ) {
			Long uuidlong = null;
			if ( i == 0 ) {
				uuidlong = newhdduuid.getMostSignificantBits();
			} else {
				uuidlong = newhdduuid.getLeastSignificantBits();
			}

			for ( int j = 0; j < 8; j++ ) {
				int index = j + offset;
				bytesToWrite[index] = (byte) ( uuidlong >>> bytesOffset[index] );
			}
			offset = 8;
		}
		try ( RandomAccessFile file = new RandomAccessFile( diskImage, "rw" ) ) {

			file.seek( 392 );
			file.write( bytesToWrite, 0, 16 );
		} catch ( Exception e ) {
			LOGGER.warn( "could not patch new uuid in the vdi", e );
		}

		// we need a new machine uuid
		UUID newMachineUuid = UUID.randomUUID();
		if ( newMachineUuid.equals( newhdduuid ) ) {
			LOGGER.warn( "The new Machine UUID is the same as the new HDD UUID; tying again...this vm might not start" );
			newMachineUuid = UUID.randomUUID();
		}
		String machineUUid = "{" + newMachineUuid.toString() + "}";
		config.changeAttribute( "Machine", "uuid", machineUUid );
		return true;
	}

	@Override
	public boolean addDefaultNat()
	{
		config.addNewNode( "Adapter", "NAT", false );
		return true;
	}

	public void reWrite()
	{
		try {
			WriteToFile();
		} catch ( TransformerFactoryConfigurationError | TransformerException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void setOs( String vendorOsId )
	{
		// TODO Auto-generated method stub

	}

	@Override
	public boolean addDisplayName( String name )
	{
		config.changeAttribute( "Machine", "name", name );
		return true;
	}

	@Override
	public boolean addRam( int mem )
	{
		config.changeAttribute( "Memory", "RAMSize", Integer.toString( mem ) );
		return true;
	}

	@Override
	public void addFloppy( int index, String image, boolean readOnly )
	{

		if ( image == null ) {
		} else {
		}

	}

	@Override
	public boolean addCdrom( String image )
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addCpuCoreCount( int nrOfCores )
	{
		config.changeAttribute( "CPU", "count", "1" );
		return true;
	}

	@Override
	public void setSoundCard( org.openslx.util.vm.VmMetaData.SoundCardType type )
	{
		// TODO Auto-generated method stub

	}

	@Override
	public VmMetaData.SoundCardType getSoundCard()
	{
		VmMetaData.SoundCardType returnsct = null;
		// TODO ask about controller and driver
		Element x = (Element) config.findANode( "AudioAdapter" ).item( 0 );
		returnsct = VmMetaData.SoundCardType.DEFAULT;
		// TODO End
		return returnsct;
	}

	@Override
	public void setDDAcceleration( VmMetaData.DDAcceleration type )
	{
		// TODO Auto-generated method stub

	}

	@Override
	public VmMetaData.DDAcceleration getDDAcceleration()
	{
		VmMetaData.DDAcceleration returndda = null;
		Element x = (Element)config.findANode( "Display" ).item( 0 );
		if ( x.hasAttribute( "accelerate3D" ) ) {
			if ( x.getAttribute( "accelerate3D" ).equals( "true" ) ) {
				returndda = VmMetaData.DDAcceleration.ON;
			} else {
				returndda = VmMetaData.DDAcceleration.OFF;
			}
		} else {
			returndda = VmMetaData.DDAcceleration.OFF;
		}
		return returndda;
	}

	@Override
	public void setHWVersion( HWVersion type )
	{
		// TODO Auto-generated method stub

	}

	@Override
	public HWVersion getHWVersion()
	{
		VmMetaData.HWVersion returnhwv = null;
		// TODO what do here? there is only one supported hwversion as far as I know
		returnhwv = VmMetaData.HWVersion.NONE;
		// TODO End
		return returnhwv;
	}

	@Override
	public void setEthernetDevType( int cardIndex, EthernetDevType type )
	{
		// TODO Auto-generated method stub

	}

	@Override
	public EthernetDevType getEthernetDevType( int cardIndex )
	{
		VmMetaData.EthernetDevType returnedt = null;
		// TODO what do here?
		// TODO need to search for the 1.st adapter of network tag
		return VmMetaData.EthernetDevType.AUTO;
	}

	@Override
	public byte[] getDefinitionArray()
	{
		return config.toString().getBytes( StandardCharsets.UTF_8 );
	}

	public void populateTheMaps()
	{
		/* add all from VBox supported sound cards here 
		soundCards.put( VmMetaData.SoundCardType.NONE, new VBoxSoundCardMeta( false, null ) );
		soundCards.put( VmMetaData.SoundCardType.HD_AUDIO, new VBoxSoundCardMeta( true, "hdaudio" ) );
		// end of supported sound cards
		// add all from VBox supported settings for the 3D acceleration
		ddacc.put( VmMetaData.DDAcceleration.ON, new VBoxDDAccelMeta( true ) );
		ddacc.put( VmMetaData.DDAcceleration.OFF, new VBoxDDAccelMeta( false ) );
		// end of all from VBox supported settings for the 3D acceleration
		// add all from VBox supported Hardware versions here 
		hwversion.put( VmMetaData.HWVersion.NONE, new VBoxHWVersionMeta( 0 ) );
		// end of all from VBox supported Hardware versions here 
		// add all from VBox supported Ethernet devices versions here
		networkCards.put( VmMetaData.EthernetDevType.E1000, new VBoxEthernetDevTypeMeta( "e1000" ) );
		// end of all from VBox supported Ethernet devices versions here */
		
	// add all from vmware supported sound cards here 
			soundCards.put( VmMetaData.SoundCardType.NONE, new VBoxSoundCardMeta( false, null ) );
			soundCards.put( VmMetaData.SoundCardType.DEFAULT, new VBoxSoundCardMeta( true, null ) );
			soundCards.put( VmMetaData.SoundCardType.SOUND_BLASTER, new VBoxSoundCardMeta( true, "sb16" ) );
			soundCards.put( VmMetaData.SoundCardType.ES, new VBoxSoundCardMeta( true, "es1371" ) );
			soundCards.put( VmMetaData.SoundCardType.HD_AUDIO, new VBoxSoundCardMeta( true, "hdaudio" ) );
			// end of supported sound cards
			// add all from vmware supported settings for the 3D acceleration
			ddacc.put( VmMetaData.DDAcceleration.OFF, new VBoxDDAccelMeta( false ) );
			ddacc.put( VmMetaData.DDAcceleration.ON, new VBoxDDAccelMeta( true ) );
			// end of all from vmware supported settings for the 3D acceleration
			// add all from vmware supported Hardware versions here 
			hwversion.put( VmMetaData.HWVersion.NONE, new VBoxHWVersionMeta( 0 ) );
			hwversion.put( VmMetaData.HWVersion.THREE, new VBoxHWVersionMeta( 3 ) );
			hwversion.put( VmMetaData.HWVersion.FOUR, new VBoxHWVersionMeta( 4 ) );

			// end of all from vmware supported Hardware versions here 
			// add all from vmware supported Ethernet devices versions here
			networkCards.put( VmMetaData.EthernetDevType.AUTO, new VBoxEthernetDevTypeMeta( null ) );
			networkCards.put( VmMetaData.EthernetDevType.PCNET32, new VBoxEthernetDevTypeMeta( "vlance" ) );
			networkCards.put( VmMetaData.EthernetDevType.E1000, new VBoxEthernetDevTypeMeta( "e1000" ) );
			// end of all from vmware supported Ethernet devices versions here
	}
}