forked from lstoll/libvirt-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoragePool.java
More file actions
377 lines (344 loc) · 11.9 KB
/
Copy pathStoragePool.java
File metadata and controls
377 lines (344 loc) · 11.9 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
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.libvirt;
import org.libvirt.jna.Libvirt;
import org.libvirt.jna.StoragePoolPointer;
import org.libvirt.jna.StorageVolPointer;
import org.libvirt.jna.virStoragePoolInfo;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
/**
* A collection of storage
*/
public class StoragePool {
static final class BuildFlags {
/**
* Regular build from scratch
*/
static final int VIR_STORAGE_POOL_BUILD_NEW = 0;
/**
* Repair / reinitialize
*/
static final int VIR_STORAGE_POOL_BUILD_REPAIR = 1;
/**
* Extend existing pool
*/
static final int VIR_STORAGE_POOL_BUILD_RESIZE = 2;
}
static final class DeleteFlags {
/**
* Delete metadata only (fast)
*/
static final int VIR_STORAGE_POOL_DELETE_NORMAL = 0;
/**
* Clear all data to zeros (slow)
*/
static final int VIR_STORAGE_POOL_DELETE_ZEROED = 1;
}
/**
* the native virStoragePoolPtr.
*/
protected StoragePoolPointer VSPP;
/**
* The VirConnect Object that represents the Hypervisor of this Domain
*/
protected Connect virConnect;
/**
* the libvirt instance
*/
protected Libvirt libvirt;
/**
* Constructs a VirStoragePool object from a known native virStoragePoolPtr,
* and a VirConnect object. For use when native libvirt returns a
* virStoragePoolPtr, i.e. error handling.
*
* @param virConnect
* the Domain's hypervisor
* @param VSPP
* the native virStoragePoolPtr
*/
StoragePool(Connect virConnect, StoragePoolPointer VSPP) {
this.virConnect = virConnect;
this.VSPP = VSPP;
libvirt = virConnect.libvirt;
}
/**
* Build the underlying storage pool
*
* @param flags
* future flags, use 0 for now
*/
public void build(int flags) throws LibvirtException {
int result = libvirt.virStoragePoolBuild(VSPP, flags);
ErrorHandler.processError(libvirt, result);
}
/**
* Starts this inactive storage pool
*
* @param flags
* future flags, use 0 for now
*/
public void create(int flags) throws LibvirtException {
int result = libvirt.virStoragePoolCreate(VSPP, flags);
ErrorHandler.processError(libvirt, result);
}
/**
* Delete the underlying pool resources. This is a non-recoverable
* operation. The virStoragePool object itself is not free'd.
*
* @param flags
* flags for obliteration process
*/
public void delete(int flags) throws LibvirtException {
int result = libvirt.virStoragePoolDelete(VSPP, flags);
ErrorHandler.processError(libvirt, result);
}
/**
* Destroy an active storage pool. This will deactivate the pool on the
* host, but keep any persistent config associated with it. If it has a
* persistent config it can later be restarted with virStoragePoolCreate().
* This does not free the associated virStoragePoolPtr object.
*/
public void destroy() throws LibvirtException {
int result = libvirt.virStoragePoolDestroy(VSPP);
ErrorHandler.processError(libvirt, result);
}
@Override
public void finalize() throws LibvirtException {
free();
}
/**
* Free a storage pool object, releasing all memory associated with it. Does
* not change the state of the pool on the host.
*
* @throws LibvirtException
* @return number of references left (>= 0) for success, -1 for failure.
*/
public int free() throws LibvirtException {
int success = 0;
if (VSPP != null) {
success = libvirt.virStoragePoolFree(VSPP);
ErrorHandler.processError(libvirt, success);
VSPP = null;
}
return success;
}
/**
* Fetches the value of the autostart flag, which determines whether the
* pool is automatically started at boot time
*
* @return the result
* @throws LibvirtException
*/
public boolean getAutostart() throws LibvirtException {
IntByReference autoStart = new IntByReference();
int result = libvirt.virStoragePoolGetAutostart(VSPP, autoStart);
ErrorHandler.processError(libvirt, result);
return autoStart.getValue() != 0 ? true : false;
}
/**
* Provides the connection pointer associated with a storage pool.
*
* @return the Connect object
*/
public Connect getConnect() {
return virConnect;
}
/**
* Get volatile information about the storage pool such as free space /
* usage summary
*
* @return a StoragePoolInfo object describing this storage pool
* @throws LibvirtException
*/
public StoragePoolInfo getInfo() throws LibvirtException {
virStoragePoolInfo vInfo = new virStoragePoolInfo();
int result = libvirt.virStoragePoolGetInfo(VSPP, vInfo);
ErrorHandler.processError(libvirt, result);
return new StoragePoolInfo(vInfo);
}
/**
* Fetch the locally unique name of the storage pool
*
* @return the name
* @throws LibvirtException
*/
public String getName() throws LibvirtException {
String returnValue = libvirt.virStoragePoolGetName(VSPP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Fetch the globally unique ID of this storage pool
*
* @return the UUID as an unpacked int array
* @throws LibvirtException
*/
public int[] getUUID() throws LibvirtException {
byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN];
int success = libvirt.virStoragePoolGetUUID(VSPP, bytes);
ErrorHandler.processError(libvirt, success);
int[] returnValue = new int[0];
if (success == 0) {
returnValue = Connect.convertUUIDBytes(bytes);
}
return returnValue;
}
/**
* Fetch the globally unique ID of the storage pool as a string
*
* @return the UUID in canonical String format
* @throws LibvirtException
*/
public String getUUIDString() throws LibvirtException {
byte[] bytes = new byte[Libvirt.VIR_UUID_STRING_BUFLEN];
int success = libvirt.virStoragePoolGetUUIDString(VSPP, bytes);
ErrorHandler.processError(libvirt, success);
String returnValue = null;
if (success == 0) {
returnValue = Native.toString(bytes);
}
return returnValue;
}
/**
* Fetch an XML document describing all aspects of the storage pool. This is
* suitable for later feeding back into the virStoragePoolCreateXML method.
*
* @param flags
* flags for XML format options (set of virDomainXMLFlags)
* @return a XML document -java @throws LibvirtException
*/
public String getXMLDesc(int flags) throws LibvirtException {
String returnValue = libvirt.virStoragePoolGetXMLDesc(VSPP, flags);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Determine if the storage pool is currently running
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virStoragePoolIsActive">Libvirt
* Documentation</a>
* @return 1 if running, 0 if inactive, -1 on error
* @throws LibvirtException
*/
public int isActive() throws LibvirtException {
int returnValue = libvirt.virStoragePoolIsActive(VSPP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Determine if the storage pool has a persistent configuration which means
* it will still exist after shutting down
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virStoragePoolIsPersistent">Libvirt
* Documentation</a>
* @return 1 if persistent, 0 if transient, -1 on error
* @throws LibvirtException
*/
public int isPersistent() throws LibvirtException {
int returnValue = libvirt.virStoragePoolIsPersistent(VSPP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Fetch list of storage volume names
*
* @return an Array of Strings that contains the names of the storage
* volumes
* @throws LibvirtException
*/
public String[] listVolumes() throws LibvirtException {
int num = numOfVolumes();
String[] returnValue = new String[num];
int result = libvirt.virStoragePoolListVolumes(VSPP, returnValue, num);
ErrorHandler.processError(libvirt, result);
return returnValue;
}
/**
* Fetch the number of storage volumes within a pool
*
* @return the number of storage pools
* @throws LibvirtException
*/
public int numOfVolumes() throws LibvirtException {
int returnValue = libvirt.virStoragePoolNumOfVolumes(VSPP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Request that the pool refresh its list of volumes. This may involve
* communicating with a remote server, and/or initializing new devices at
* the OS layer
*
* @param flags
* flags to control refresh behaviour (currently unused, use 0)
* @throws LibvirtException
*/
public void refresh(int flags) throws LibvirtException {
int result = libvirt.virStoragePoolRefresh(VSPP);
ErrorHandler.processError(libvirt, result);
}
/**
* Sets the autostart flag
*
* @param autostart
* new flag setting
* @throws LibvirtException
*/
public void setAutostart(int autostart) throws LibvirtException {
libvirt.virStoragePoolSetAutostart(VSPP, autostart);
}
/**
* Create a storage volume within a pool based on an XML description. Not
* all pools support creation of volumes
*
* @param xmlDesc
* description of volume to create
* @param flags
* flags for creation (unused, pass 0)
* @return the storage volume
* @throws LibvirtException
*/
public StorageVol storageVolCreateXML(String xmlDesc, int flags) throws LibvirtException {
StorageVolPointer sPtr = libvirt.virStorageVolCreateXML(VSPP, xmlDesc, flags);
ErrorHandler.processError(libvirt, sPtr);
return new StorageVol(virConnect, sPtr);
}
/**
* Create a storage volume in the parent pool, using the 'clonevol' volume
* as input. Information for the new volume (name, perms) are passed via a
* typical volume XML description.
*
* @return
* @throws LibvirtException
*/
public StorageVol storageVolCreateXMLFrom(String xmlDesc, StorageVol cloneVolume, int flags)
throws LibvirtException {
StorageVolPointer sPtr = libvirt.virStorageVolCreateXMLFrom(VSPP, xmlDesc, cloneVolume.VSVP, flags);
ErrorHandler.processError(libvirt, sPtr);
return new StorageVol(virConnect, sPtr);
}
/**
* Fetch an object representing to a storage volume based on its name within
* a pool
*
* @param name
* name of storage volume
* @return The StorageVol object found
* @throws LibvirtException
*/
public StorageVol storageVolLookupByName(String name) throws LibvirtException {
StorageVolPointer sPtr = libvirt.virStorageVolLookupByName(VSPP, name);
ErrorHandler.processError(libvirt, sPtr);
return new StorageVol(virConnect, sPtr);
}
/**
* Undefine an inactive storage pool
*
* @throws LibvirtException
*/
public void undefine() throws LibvirtException {
int result = libvirt.virStoragePoolUndefine(VSPP);
ErrorHandler.processError(libvirt, result);
}
}