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
|
package org.openslx.dozmod.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.dozmod.thrift.cache.MetaDataCache;
import org.openslx.util.vm.VmwareMetaData;
public class VmWrapper {
private static final Logger LOGGER = Logger.getLogger(VmWrapper.class);
public static void wrapVm(File diskFile, String imageName, byte[] machineDescription,
String virtualizerId, int osId) throws MetaDataMissingException, IOException {
// TODO This is very vmware specific, but should support others too some day
if (!diskFile.exists())
throw new FileNotFoundException("Disk file " + diskFile.getAbsolutePath() + " does not exist");
// Handle machine description to generate *.vmx
VmwareMetaData vmwareConfig = null;
if (machineDescription == null) {
// TODO: Generate fallback vmx
return;
} else {
vmwareConfig = new VmwareMetaData(MetaDataCache.getOperatingSystems(), machineDescription,
machineDescription.length);
}
// Append disk & NAT
if (!vmwareConfig.addEthernetNat() || !vmwareConfig.addHddTemplate(diskFile.getName())) {
throw new MetaDataMissingException();
}
// The guestOS should be in the vmx, but the user could have changed it by editing
// the image via the GUI. Those changes are not written back to the stored vmx
if (virtualizerId != null && osId != 0) {
OperatingSystem os = MetaDataCache.getOsById(osId);
if (os != null && os.virtualizerOsId != null) {
String virtOsId = os.virtualizerOsId.get(virtualizerId);
if (virtOsId != null) {
vmwareConfig.setOs(virtOsId);
}
}
}
File vmxFile = new File(diskFile.getAbsolutePath() + ".vmx");
vmxFile.delete();
FileUtils.writeByteArrayToFile(vmxFile, vmwareConfig.getFilteredDefinitionArray());
}
/**
* Generates a filename based on the given imageName and with the proper
* extension depending on the virtualizer
*
* @param imageName
* @param virtualizerId
* @return the generated name as String
*/
public static String generateFilename(String imageName, String virtualizerId) {
String fileName = imageName.replaceAll("[^a-zA-Z0-9_\\.\\-]+", "_");
if (fileName.length() > 50) {
fileName = fileName.substring(0, 50);
}
if (virtualizerId == null) {
fileName += ".img";
} else if ("vmware".equals(virtualizerId)) {
fileName += ".vmdk";
} else if ("virtualbox".equals(virtualizerId)) {
fileName += ".vdi";
} else {
fileName += "." + virtualizerId;
}
return fileName;
}
public static class MetaDataMissingException extends Exception {
private static final long serialVersionUID = -7842986428831219758L;
}
}
|