-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDisk.java
More file actions
225 lines (199 loc) · 6.17 KB
/
Copy pathDisk.java
File metadata and controls
225 lines (199 loc) · 6.17 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
package javaforce.vm;
import javaforce.api.linux.VMAPI;
import java.io.*;
import javaforce.*;
import javaforce.api.*;
import javaforce.ffm.*;
/** Disk.
*
* @author pquiring
*/
public class Disk implements Serializable {
private static final long serialVersionUID = 1L;
public String pool; //storage pool
public String folder; //usually vm name
public String name; //filename
public int type;
public Size size;
public int boot_order; //0=none 1=first etc.
public String target_dev = "sd%c"; //see Hardware.validate()
public String target_bus = "auto";
public static final int TYPE_VMDK = 0;
public static final int TYPE_QCOW2 = 1;
public static final int TYPE_ISO = 2;
public static final int PROVISION_THICK = 0;
public static final int PROVISION_THIN = 1;
public String getType() {
switch (type) {
case TYPE_QCOW2: return "qcow2";
case TYPE_VMDK: return "vmdk";
case TYPE_ISO: return "iso";
}
return "";
}
public static int getType(String ext) {
switch (ext) {
case "qcow2": return TYPE_QCOW2;
case "vmdk": return TYPE_VMDK;
case "iso": return TYPE_ISO;
}
return -1;
}
public String getFolder() {
return "/volumes/" + pool + "/" + folder;
}
public String getFile() {
return name + '.' + getType();
}
public String getPath() {
return "/volumes/" + pool + "/" + folder + "/" + name + '.' + getType();
}
public String getSnapshotPath(String ssname) {
//NOTE : libvirt only supports qcow2 for overlay files, but the base file may be vmdk
return "/volumes/" + pool + "/" + folder + "/" + name + "-" + ssname + '.' + "qcow2";
}
/** Get path using a different pool. */
public String getPath(String pool) {
return "/volumes/" + pool + "/" + folder + "/" + name + '.' + getType();
}
public boolean exists() {
return new File(getPath()).exists();
}
public boolean exists(String pool) {
return new File(getPath(pool)).exists();
}
private String getPath2() {
if (type == TYPE_VMDK) {
String flat = "/volumes/" + pool + "/" + folder + "/" + name + "-flat." + getType();
if (new File(flat).exists()) {
return flat;
}
}
return "/volumes/" + pool + "/" + folder + "/" + name + '.' + getType();
}
/** Provision virtual disk for a VirtualMachine. */
public boolean create(int provision) {
new File(getFolder()).mkdirs();
if (false) {
//use libvirt (not working per docs)
String xml = getCreateXML(provision);
JFLog.log("Disk.xml=" + xml);
return VMAPI.getInstance().vmDiskCreate(pool, xml);
} else {
//use qemu-img
ShellProcess sp = new ShellProcess();
switch (type) {
case TYPE_VMDK: {
String subformat = "";
switch (provision) {
case PROVISION_THICK:
subformat = "monolithicFlat";
break;
default:
case PROVISION_THIN:
subformat = "monolithicSparse";
break;
}
sp.run(new String[] {"/usr/bin/qemu-img", "create", "-f", getType(), "-o", "subformat=" + subformat, getPath(), size.getSize()}, true);
break;
}
case TYPE_QCOW2: {
String preallocation = "";
switch (provision) {
case PROVISION_THICK:
preallocation = "metadata"; //or "full" which is slower
break;
default:
case PROVISION_THIN:
preallocation = "off";
break;
}
sp.run(new String[] {"/usr/bin/qemu-img", "create", "-f", getType(), "-o", "preallocation=" + preallocation, getPath(), size.getSize()}, true);
break;
}
}
return sp.getErrorLevel() == 0;
}
}
public boolean resize() {
return resize(null);
}
public boolean resize(Storage pool) {
if (false) {
//use libvirt (not working per docs)
String xml = getCreateXML(0);
JFLog.log("Disk.xml=" + xml);
return VMAPI.getInstance().vmDiskCreate(pool.name, xml);
} else {
//use qemu-img
ShellProcess sp = new ShellProcess();
switch (type) {
case TYPE_VMDK:
sp.run(new String[] {"/usr/bin/qemu-img", "resize", getPath(), size.getSize()}, true);
break;
case TYPE_QCOW2:
sp.run(new String[] {"/usr/bin/qemu-img", "resize", getPath(), size.getSize()}, true);
break;
}
return sp.getErrorLevel() == 0;
}
}
private String getDeviceType() {
switch (type) {
case TYPE_ISO: return "cdrom";
default: return "disk";
}
}
public String getHardwareXML(int os) {
StringBuilder xml = new StringBuilder();
String _dev = target_dev;
String _bus = target_bus;
if (target_bus == null || target_bus.equals("auto")) {
if (os == Hardware.OS_WINDOWS) {
_bus = "sata";
} else {
_bus = "scsi";
}
}
xml.append("<disk type='file' device='" + getDeviceType() + "'>");
xml.append("<source file='" + getPath2() + "'>");
xml.append("</source>");
xml.append("<target dev='" + _dev + "' bus='" + _bus + "'/>");
if (boot_order > 0) {
xml.append("<boot order='" + boot_order + "'/>");
}
xml.append("</disk>");
return xml.toString();
}
private String getCreateXML(int provision) {
StringBuilder xml = new StringBuilder();
xml.append("<volume type='file'>");
xml.append("<name>" + name + "</name>");
xml.append("<allocation unit='" + size.getUnitChar() + "iB'>");
switch (provision) {
case PROVISION_THICK:
xml.append(size.size);
break;
case PROVISION_THIN:
xml.append("0");
break;
}
xml.append("</allocation>");
xml.append("<capacity unit='" + size.getUnitChar() + "iB'>");
xml.append(size.size);
xml.append("</capacity>");
xml.append("<target>");
xml.append("<path>" + getPath() + "</path>");
xml.append("<format type='" + getType() + "'/>");
xml.append("</target>");
xml.append("</volume>");
return xml.toString();
}
public String toString() {
if (type == Disk.TYPE_ISO) {
return name + ":iso";
} else {
return name + ":" + size.toString();
}
}
}