summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/vm/VboxMetaData.java
blob: f504c708be8563750dc383e0e0368f9a8c3730ed (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
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;

public class VboxMetaData extends VmMetaData
{

	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()
	{
		this.config.init();
		displayName = config.getDisplayName();
		setOs( "virtualbox", config.getOsName() );

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

		// hey debug code here
		try {
			addFloppy( 0, null, true );
			WriteToFile();
		} catch ( TransformerFactoryConfigurationError | TransformerException e ) {
			// WriteToFile exceptions here...not important for the the LOGGER
			e.printStackTrace();
		}
		// end here
	}

	// --TODO will be needed later again
	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 );
	}

	@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;
	}
}