-
Notifications
You must be signed in to change notification settings - Fork 11
/
StoragePool.cs
439 lines (412 loc) · 19.1 KB
/
StoragePool.cs
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Copyright (C)
* Arnaud Champion <[email protected]>
* Jaromír Červenka <[email protected]>
*
* See COPYING.LIB for the License of this software
*/
using System;
using System.Runtime.InteropServices;
namespace Libvirt
{
/// <summary>
/// The StoragePool class expose all libvirt storage pool related functions
/// </summary>
public class StoragePool
{
private const int MaxStringLength = 1024;
/// <summary>
/// Build the underlying storage pool.
/// </summary>
/// <param name="pool">
/// A pointer to storage pool.
/// </param>
/// <param name="flags">
/// Future flags, use 0 for now.
/// </param>
/// <returns>
/// 0 on success, or -1 upon failure
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolBuild")]
public static extern int Build(IntPtr pool, StoragePoolBuildFlags flags);
/// <summary>
/// Starts an inactive storage pool.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="flags">
/// Future flags, use 0 for now.
/// </param>
/// <returns>
/// 0 on success, or -1 if it could not be started.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolCreate")]
public static extern int Create(IntPtr pool, uint flags);
/// <summary>
/// Create a new storage based on its XML description. The pool is not persistent,
/// so its definition will disappear when it is destroyed, or if the host is restarted
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to hypervisor connection.
/// </param>
/// <param name="xmlDesc">
/// A <see cref="System.String"/>XML description for new pool.
/// </param>
/// <param name="flags">
/// A <see cref="System.UInt32"/>future flags, use 0 for now.
/// </param>
/// <returns>
/// A virStoragePoolPtr object, or NULL if creation failed.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolCreateXML")]
public static extern IntPtr CreateXML(IntPtr conn, string xmlDesc, uint flags);
/// <summary>
/// Define a new inactive storage pool based on its XML description. The pool is persistent,
/// until explicitly undefined.
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to hypervisor connection.
/// </param>
/// <param name="xml">
/// A <see cref="System.String"/>XML description for new pool.
/// </param>
/// <param name="flags">
/// A <see cref="System.UInt32"/>future flags, use 0 for now.
/// </param>
/// <returns>
/// A virStoragePoolPtr object, or NULL if creation failed.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolDefineXML")]
public static extern IntPtr DefineXML(IntPtr conn, string xml, uint flags);
/// <summary>
/// Delete the underlying pool resources. This is a non-recoverable operation. The virStoragePoolPtr object itself is not free'd.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="flags">
/// A <see cref="StoragePoolDeleteFlags"/>flags for obliteration process.
/// </param>
/// <returns>
/// 0 on success, or -1 if it could not be obliterate.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolDelete")]
public static extern int Delete(IntPtr pool, StoragePoolDeleteFlags flags);
/// <summary>
/// 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.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <returns>
/// 0 on success, or -1 if it could not be destroyed.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolDestroy")]
public static extern int Destroy(IntPtr pool);
/// <summary>
/// Free a storage pool object, releasing all memory associated with it. Does not change the state of the pool on the host.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <returns>
/// 0 on success, or -1 if it could not be free'd.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolFree")]
public static extern int Free(IntPtr pool);
/// <summary>
/// Fetches the value of the autostart flag, which determines whether the pool is automatically started at boot time.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="autotart"></param>
/// <returns>
/// 0 on success, -1 on failure
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolGetAutostart")]
public static extern int GetAutostart(IntPtr pool, out int autotart);
/// <summary>
/// Provides the connection pointer associated with a storage pool. The reference counter on the connection is not increased by this call.
/// WARNING: When writing libvirt bindings in other languages, do not use this function. Instead, store the connection and the pool object together.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to a pool.
/// </param>
/// <returns>
/// A <see cref="IntPtr"/>the virConnectPtr or NULL in case of failure.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolGetConnect")]
public static extern IntPtr GetConnect(IntPtr pool);
/// <summary>
/// Get volatile information about the storage pool such as free space / usage summary.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="info">
/// Pointer at which to store info.
/// </param>
/// <returns>
/// 0 on success, or -1 on failure.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolGetInfo")]
public static extern int GetInfo(IntPtr pool, ref StoragePoolInfo info);
/// <summary>
/// Fetch the locally unique name of the storage pool.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <returns>
/// The name of the pool, or NULL on error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolGetName")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StringWithoutNativeCleanUpMarshaler))]
public static extern string GetName(IntPtr pool);
/// <summary>
/// Fetch the globally unique ID of the storage pool.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>Pointer to storage pool.
/// </param>
/// <param name="uuid">
/// Buffer of VIR_UUID_BUFLEN bytes in size.
/// </param>
/// <returns>
/// 0 on success, or -1 on error
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolGetUUID")]
public static extern int GetUUID(IntPtr pool, [Out] char[] uuid);
/// <summary>
/// Fetch the globally unique ID of the storage pool as a string.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="uuid">
/// A <see cref="IntPtr"/>buffer of VIR_UUID_STRING_BUFLEN bytes in size.
/// </param>
/// <returns>
/// 0 on success, or -1 on error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolGetUUIDString")]
private static extern int GetUUIDString(IntPtr pool, [Out] char[] uuid);
///<summary>
/// Fetch the globally unique ID of the storage pool as a string.
///</summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
///<returns>the storage pool UUID</returns>
public static string GetUUIDString(IntPtr pool)
{
char[] uuidArray = new char[36];
GetUUIDString(pool, uuidArray);
return new string(uuidArray);
}
/// <summary>
/// Fetch an XML document describing all aspects of the storage pool.
/// This is suitable for later feeding back into the virStoragePoolCreateXML method.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="flags">
/// Flags for XML format options (set of virDomainXMLFlags).
/// </param>
/// <returns>
/// A XML document, or NULL on error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolGetXMLDesc")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StringWithoutNativeCleanUpMarshaler))]
public static extern string GetXMLDesc(IntPtr pool, DomainXMLFlags flags);
/// <summary>
/// Determine if the storage pool is currently running.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to the storage pool object.
/// </param>
/// <returns>
/// 1 if running, 0 if inactive, -1 on error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolIsActive")]
public static extern int IsActive(IntPtr pool);
/// <summary>
/// Determine if the storage pool has a persistent configuration which means it will still exist after shutting down.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to the storage pool object.
/// </param>
/// <returns>
/// 1 if persistent, 0 if transient, -1 on error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolIsPersistent")]
public static extern int IsPersistent(IntPtr pool);
/// <summary>
/// Fetch list of storage volume names, limiting to at most maxnames.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="names">
/// A <see cref="IntPtr"/>array in which to storage volume names.
/// </param>
/// <param name="maxnames">
/// A <see cref="System.Int32"/>size of names array.
/// </param>
/// <returns>
/// The number of names fetched, or -1 on error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolListVolumes")]
private static extern int ListVolumes(IntPtr pool, IntPtr names, int maxnames);
/// <summary>
/// Fetch list of storage volume names, limiting to at most maxnames.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="names">
/// A <see cref="IntPtr"/>array in which to storage volume names.
/// </param>
/// <param name="maxnames">
/// A <see cref="System.Int32"/>size of names array.
/// </param>
/// <returns>
/// The number of names fetched, or -1 on error.
/// </returns>
public static int ListVolumes(IntPtr pool, ref string[] names, int maxnames)
{
IntPtr namesPtr = Marshal.AllocHGlobal(MaxStringLength);
int count = ListVolumes(pool, namesPtr, maxnames);
if (count > 0)
names = MarshalHelper.ptrToStringArray(namesPtr, count);
Marshal.FreeHGlobal(namesPtr);
return count;
}
/// <summary>
/// Fetch a storage pool based on its unique name.
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to hypervisor connection.
/// </param>
/// <param name="name">
/// Name of pool to fetch.
/// </param>
/// <returns>
/// A <see cref="IntPtr"/>virStoragePoolPtr object, or NULL if no matching pool is found.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolLookupByName")]
public static extern IntPtr LookupByName(IntPtr conn, string name);
/// <summary>
/// Fetch a storage pool based on its globally unique id.
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to hypervisor connection.
/// </param>
/// <param name="uuid">
/// Globally unique id of pool to fetch.
/// </param>
/// <returns>
/// A virStoragePoolPtr object, or NULL if no matching pool is found
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolLookupByUUID")]
public static extern IntPtr LookupByUUID(IntPtr conn, char[] uuid);
/// <summary>
/// Fetch a storage pool based on its globally unique id.
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to hypervisor connection.
/// </param>
/// <param name="uuidstr">
/// A <see cref="System.String"/>globally unique id of pool to fetch.
/// </param>
/// <returns>
/// A <see cref="IntPtr"/>object, or NULL if no matching pool is found.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolLookupByUUIDString")]
public static extern IntPtr LookupByUUIDString(IntPtr conn, string uuidstr);
/// <summary>
/// Fetch a storage pool which contains a particular volume.
/// </summary>
/// <param name="vol">
/// A <see cref="IntPtr"/>pointer to storage volume.
/// </param>
/// <returns>
/// A <see cref="IntPtr"/>object, or NULL if no matching pool is found.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolLookupByVolume")]
public static extern IntPtr LookupByVolume(IntPtr vol);
/// <summary>
/// Fetch the number of storage volumes within a pool.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <returns>
/// A <see cref="System.Int32"/>the number of storage pools, or -1 on failure.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolNumOfVolumes")]
public static extern int NumOfVolumes(IntPtr pool);
/// <summary>
/// Increment the reference count on the pool. For each additional call to this method,
/// there shall be a corresponding call to virStoragePoolFree to release the reference count,
/// once the caller no longer needs the reference to this object.
/// This method is typically useful for applications where multiple threads are using a connection,
/// and it is required that the connection remain open until all threads have finished using it. ie,
/// each new thread using a pool would increment the reference count.
/// </summary>
/// <param name="pool">
/// The pool to hold a reference on.
/// </param>
/// <returns>
/// 0 in case of success, -1 in case of failure.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolRef")]
public static extern int Ref(IntPtr pool);
/// <summary>
/// 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.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="flags">
/// A <see cref="System.UInt32"/>flags to control refresh behaviour (currently unused, use 0).
/// </param>
/// <returns>
/// 0 if the volume list was refreshed, -1 on failure.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolRefresh")]
public static extern int Refresh(IntPtr pool, uint flags);
/// <summary>
/// Sets the autostart flag.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <param name="autostart">
/// A <see cref="System.Int32"/>new flag setting.
/// </param>
/// <returns>
/// 0 on success, -1 on failure.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolSetAutostart")]
public static extern int SetAutostart(IntPtr pool, int autostart);
/// <summary>
/// Undefine an inactive storage pool.
/// </summary>
/// <param name="pool">
/// A <see cref="IntPtr"/>pointer to storage pool.
/// </param>
/// <returns>
/// 0 on success, -1 on failure.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virStoragePoolUndefine")]
public static extern int Undefine(IntPtr pool);
}
}