-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathHardware.java
More file actions
238 lines (210 loc) · 5.29 KB
/
Copy pathHardware.java
File metadata and controls
238 lines (210 loc) · 5.29 KB
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
package javaforce.vm;
import java.io.*;
import java.util.*;
import javaforce.*;
/** Hardware setup for a Virtual Machine.
*
* Supported hardware:
* machine : kvm -machine ?
* pc alias = pc-i440fx-7.2
* q35 alias = pc-q35-7.2
* usb : kvm -device ? | grep usb
* piix3-uhci
* piix4-uhci
* ich9-uhci6
*
*
* @author pquiring
*/
public class Hardware implements Serializable {
private static final long serialVersionUID = 1L;
public transient String pool;
public transient String folder;
public transient String name;
public String genid;
public int os;
public int cores;
public int tpm;
public Size memory;
public String machine = "pc";
public String video = "vga";
public int vram = 16384;
public ArrayList<Disk> disks;
public ArrayList<Network> networks;
public ArrayList<Device> devices;
public ArrayList<Controller> controllers;
public boolean bios_efi;
public boolean bios_secure;
public boolean video_3d_accel;
public static final int OS_LINUX = 0;
public static final int OS_WINDOWS = 1;
public static final int TPM_NONE = 0;
public static final int TPM_1_2 = 1;
public static final int TPM_2_0 = 2;
public Hardware() {
pool = "default";
folder = "default";
name = "default";
genid = JF.generateUUID();
os = OS_LINUX;
cores = 2;
memory = new Size(1, Size.GB);
disks = new ArrayList<>();
networks = new ArrayList<>();
devices = new ArrayList<>();
controllers = new ArrayList<>();
}
public Hardware(String pool, String folder, String name, int os, int cores, Size memory) {
this.pool = pool;
this.folder = folder;
this.name = name;
this.genid = JF.generateUUID();
this.os = os;
this.cores = cores;
this.memory = memory;
disks = new ArrayList<>();
networks = new ArrayList<>();
devices = new ArrayList<>();
controllers = new ArrayList<>();
}
/** Gets VM Folder Location.
*/
public Location getLocation() {
return new Location(pool, folder, name);
}
/** Sets VM Location.
*
* These values are transient and need to be set if loaded indirectly.
*/
public void setLocation(Location loc) {
pool = loc.pool;
folder = loc.folder;
name = loc.folder;
}
public static String getName(String cfg) {
//remove .jfvm
int idx = cfg.indexOf(".jfvm");
return cfg.substring(0, idx);
}
public static Hardware load(String pool, String folder, String cfg) {
String path = "/volumes/" + pool + "/" + folder + "/" + cfg;
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
Hardware hardware = (Hardware)Compression.deserialize(fis, file.length());
fis.close();
//populate transient fields
hardware.pool = pool;
hardware.folder = folder;
hardware.name = getName(cfg);
return hardware;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public synchronized boolean save() {
String path = "/volumes/" + pool + "/" + folder + "/" + name + ".jfvm";
try {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
boolean res = Compression.serialize(fos, this);
fos.close();
return res;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public String getPath() {
return "/volumes/" + pool + "/" + folder;
}
public void addNetwork(Network network) {
networks.add(network);
}
public void removeNetwork(Network network) {
networks.remove(network);
}
public void addDisk(Disk disk) {
disks.add(disk);
}
public void removeDisk(Disk disk) {
disks.remove(disk);
}
public String getNextDiskName() {
int num = disks.size();
do {
String name = "disk" + num;
boolean ok = true;
for(Disk disk : disks) {
if (disk.name.equals(name)) {
ok = false;
}
}
if (ok) return name;
num++;
} while (true);
}
public boolean hasDisk(String name) {
for(Disk disk : disks) {
if (disk.name.equals(name)) return true;
}
return false;
}
public void addDevice(Device device) {
devices.add(device);
}
public void removeDevice(Device device) {
devices.remove(device);
}
public void addController(Controller ctrl) {
controllers.add(ctrl);
}
public void removeController(Controller ctrl) {
controllers.remove(ctrl);
}
public String getTPMVersion() {
switch (tpm) {
case TPM_1_2: return "1.2";
case TPM_2_0: return "2.0";
}
return "0.0";
}
public void validate() {
if (genid == null) {
genid = JF.generateUUID();
}
if (cores == 0) {
cores = 2;
}
if (machine == null) {
machine = "pc";
}
if (video == null) {
video = "vga";
}
if (vram <= 0) {
vram = 16384;
}
if (memory == null) {
memory = new Size(1, Size.GB);
}
if (disks == null) {
disks = new ArrayList<>();
}
if (networks == null) {
networks = new ArrayList<>();
}
if (devices == null) {
devices = new ArrayList<>();
}
if (controllers == null) {
controllers = new ArrayList<>();
}
int idx = 0;
for(Disk disk : disks) {
disk.target_dev = String.format("sd%c", 'a' + idx);
idx++;
}
}
}