This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
storage_pool.go
394 lines (354 loc) · 12.8 KB
/
storage_pool.go
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
/*
* This file is part of the libvirt-go project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Copyright (c) 2013 Alex Zorin
* Copyright (C) 2016 Red Hat, Inc.
*
*/
package libvirt
/*
#cgo pkg-config: libvirt
#include <stdlib.h>
#include "storage_pool_wrapper.h"
*/
import "C"
import (
"reflect"
"unsafe"
)
type StoragePoolState int
const (
STORAGE_POOL_INACTIVE = StoragePoolState(C.VIR_STORAGE_POOL_INACTIVE) // Not running
STORAGE_POOL_BUILDING = StoragePoolState(C.VIR_STORAGE_POOL_BUILDING) // Initializing pool,not available
STORAGE_POOL_RUNNING = StoragePoolState(C.VIR_STORAGE_POOL_RUNNING) // Running normally
STORAGE_POOL_DEGRADED = StoragePoolState(C.VIR_STORAGE_POOL_DEGRADED) // Running degraded
STORAGE_POOL_INACCESSIBLE = StoragePoolState(C.VIR_STORAGE_POOL_INACCESSIBLE) // Running,but not accessible
)
type StoragePoolBuildFlags uint
const (
STORAGE_POOL_BUILD_NEW = StoragePoolBuildFlags(C.VIR_STORAGE_POOL_BUILD_NEW) // Regular build from scratch
STORAGE_POOL_BUILD_REPAIR = StoragePoolBuildFlags(C.VIR_STORAGE_POOL_BUILD_REPAIR) // Repair / reinitialize
STORAGE_POOL_BUILD_RESIZE = StoragePoolBuildFlags(C.VIR_STORAGE_POOL_BUILD_RESIZE) // Extend existing pool
STORAGE_POOL_BUILD_NO_OVERWRITE = StoragePoolBuildFlags(C.VIR_STORAGE_POOL_BUILD_NO_OVERWRITE) // Do not overwrite existing pool
STORAGE_POOL_BUILD_OVERWRITE = StoragePoolBuildFlags(C.VIR_STORAGE_POOL_BUILD_OVERWRITE) // Overwrite data
)
type StoragePoolCreateFlags uint
const (
STORAGE_POOL_CREATE_NORMAL = StoragePoolCreateFlags(C.VIR_STORAGE_POOL_CREATE_NORMAL)
STORAGE_POOL_CREATE_WITH_BUILD = StoragePoolCreateFlags(C.VIR_STORAGE_POOL_CREATE_WITH_BUILD)
STORAGE_POOL_CREATE_WITH_BUILD_OVERWRITE = StoragePoolCreateFlags(C.VIR_STORAGE_POOL_CREATE_WITH_BUILD_OVERWRITE)
STORAGE_POOL_CREATE_WITH_BUILD_NO_OVERWRITE = StoragePoolCreateFlags(C.VIR_STORAGE_POOL_CREATE_WITH_BUILD_NO_OVERWRITE)
)
type StoragePoolDeleteFlags uint
const (
STORAGE_POOL_DELETE_NORMAL = StoragePoolDeleteFlags(C.VIR_STORAGE_POOL_DELETE_NORMAL)
STORAGE_POOL_DELETE_ZEROED = StoragePoolDeleteFlags(C.VIR_STORAGE_POOL_DELETE_ZEROED)
)
type StoragePoolEventID int
const (
STORAGE_POOL_EVENT_ID_LIFECYCLE = StoragePoolEventID(C.VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE)
STORAGE_POOL_EVENT_ID_REFRESH = StoragePoolEventID(C.VIR_STORAGE_POOL_EVENT_ID_REFRESH)
)
type StoragePoolEventLifecycleType int
const (
STORAGE_POOL_EVENT_DEFINED = StoragePoolEventLifecycleType(C.VIR_STORAGE_POOL_EVENT_DEFINED)
STORAGE_POOL_EVENT_UNDEFINED = StoragePoolEventLifecycleType(C.VIR_STORAGE_POOL_EVENT_UNDEFINED)
STORAGE_POOL_EVENT_STARTED = StoragePoolEventLifecycleType(C.VIR_STORAGE_POOL_EVENT_STARTED)
STORAGE_POOL_EVENT_STOPPED = StoragePoolEventLifecycleType(C.VIR_STORAGE_POOL_EVENT_STOPPED)
STORAGE_POOL_EVENT_CREATED = StoragePoolEventLifecycleType(C.VIR_STORAGE_POOL_EVENT_CREATED)
STORAGE_POOL_EVENT_DELETED = StoragePoolEventLifecycleType(C.VIR_STORAGE_POOL_EVENT_DELETED)
)
type StoragePool struct {
ptr C.virStoragePoolPtr
}
type StoragePoolInfo struct {
State StoragePoolState
Capacity uint64
Allocation uint64
Available uint64
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolBuild
func (p *StoragePool) Build(flags StoragePoolBuildFlags) error {
var err C.virError
result := C.virStoragePoolBuildWrapper(p.ptr, C.uint(flags), &err)
if result == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolCreate
func (p *StoragePool) Create(flags StoragePoolCreateFlags) error {
var err C.virError
result := C.virStoragePoolCreateWrapper(p.ptr, C.uint(flags), &err)
if result == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolDelete
func (p *StoragePool) Delete(flags StoragePoolDeleteFlags) error {
var err C.virError
result := C.virStoragePoolDeleteWrapper(p.ptr, C.uint(flags), &err)
if result == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolDestroy
func (p *StoragePool) Destroy() error {
var err C.virError
result := C.virStoragePoolDestroyWrapper(p.ptr, &err)
if result == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolFree
func (p *StoragePool) Free() error {
var err C.virError
ret := C.virStoragePoolFreeWrapper(p.ptr, &err)
if ret == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolRef
func (c *StoragePool) Ref() error {
var err C.virError
ret := C.virStoragePoolRefWrapper(c.ptr, &err)
if ret == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetAutostart
func (p *StoragePool) GetAutostart() (bool, error) {
var out C.int
var err C.virError
result := C.virStoragePoolGetAutostartWrapper(p.ptr, (*C.int)(unsafe.Pointer(&out)), &err)
if result == -1 {
return false, makeError(&err)
}
switch out {
case 1:
return true, nil
default:
return false, nil
}
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetInfo
func (p *StoragePool) GetInfo() (*StoragePoolInfo, error) {
var cinfo C.virStoragePoolInfo
var err C.virError
result := C.virStoragePoolGetInfoWrapper(p.ptr, &cinfo, &err)
if result == -1 {
return nil, makeError(&err)
}
return &StoragePoolInfo{
State: StoragePoolState(cinfo.state),
Capacity: uint64(cinfo.capacity),
Allocation: uint64(cinfo.allocation),
Available: uint64(cinfo.available),
}, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetName
func (p *StoragePool) GetName() (string, error) {
var err C.virError
name := C.virStoragePoolGetNameWrapper(p.ptr, &err)
if name == nil {
return "", makeError(&err)
}
return C.GoString(name), nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetUUID
func (p *StoragePool) GetUUID() ([]byte, error) {
var cUuid [C.VIR_UUID_BUFLEN](byte)
cuidPtr := unsafe.Pointer(&cUuid)
var err C.virError
result := C.virStoragePoolGetUUIDWrapper(p.ptr, (*C.uchar)(cuidPtr), &err)
if result != 0 {
return []byte{}, makeError(&err)
}
return C.GoBytes(cuidPtr, C.VIR_UUID_BUFLEN), nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetUUIDString
func (p *StoragePool) GetUUIDString() (string, error) {
var cUuid [C.VIR_UUID_STRING_BUFLEN](C.char)
cuidPtr := unsafe.Pointer(&cUuid)
var err C.virError
result := C.virStoragePoolGetUUIDStringWrapper(p.ptr, (*C.char)(cuidPtr), &err)
if result != 0 {
return "", makeError(&err)
}
return C.GoString((*C.char)(cuidPtr)), nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetXMLDesc
func (p *StoragePool) GetXMLDesc(flags StorageXMLFlags) (string, error) {
var err C.virError
result := C.virStoragePoolGetXMLDescWrapper(p.ptr, C.uint(flags), &err)
if result == nil {
return "", makeError(&err)
}
xml := C.GoString(result)
C.free(unsafe.Pointer(result))
return xml, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolIsActive
func (p *StoragePool) IsActive() (bool, error) {
var err C.virError
result := C.virStoragePoolIsActiveWrapper(p.ptr, &err)
if result == -1 {
return false, makeError(&err)
}
if result == 1 {
return true, nil
}
return false, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolIsPersistent
func (p *StoragePool) IsPersistent() (bool, error) {
var err C.virError
result := C.virStoragePoolIsPersistentWrapper(p.ptr, &err)
if result == -1 {
return false, makeError(&err)
}
if result == 1 {
return true, nil
}
return false, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolSetAutostart
func (p *StoragePool) SetAutostart(autostart bool) error {
var cAutostart C.int
switch autostart {
case true:
cAutostart = 1
default:
cAutostart = 0
}
var err C.virError
result := C.virStoragePoolSetAutostartWrapper(p.ptr, cAutostart, &err)
if result == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolRefresh
func (p *StoragePool) Refresh(flags uint32) error {
var err C.virError
result := C.virStoragePoolRefreshWrapper(p.ptr, C.uint(flags), &err)
if result == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolUndefine
func (p *StoragePool) Undefine() error {
var err C.virError
result := C.virStoragePoolUndefineWrapper(p.ptr, &err)
if result == -1 {
return makeError(&err)
}
return nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolCreateXML
func (p *StoragePool) StorageVolCreateXML(xmlConfig string, flags StorageVolCreateFlags) (*StorageVol, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
var err C.virError
ptr := C.virStorageVolCreateXMLWrapper(p.ptr, cXml, C.uint(flags), &err)
if ptr == nil {
return nil, makeError(&err)
}
return &StorageVol{ptr: ptr}, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolCreateXMLFrom
func (p *StoragePool) StorageVolCreateXMLFrom(xmlConfig string, clonevol *StorageVol, flags StorageVolCreateFlags) (*StorageVol, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
var err C.virError
ptr := C.virStorageVolCreateXMLFromWrapper(p.ptr, cXml, clonevol.ptr, C.uint(flags), &err)
if ptr == nil {
return nil, makeError(&err)
}
return &StorageVol{ptr: ptr}, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolLookupByName
func (p *StoragePool) LookupStorageVolByName(name string) (*StorageVol, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
var err C.virError
ptr := C.virStorageVolLookupByNameWrapper(p.ptr, cName, &err)
if ptr == nil {
return nil, makeError(&err)
}
return &StorageVol{ptr: ptr}, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolNumOfVolumes
func (p *StoragePool) NumOfStorageVolumes() (int, error) {
var err C.virError
result := int(C.virStoragePoolNumOfVolumesWrapper(p.ptr, &err))
if result == -1 {
return 0, makeError(&err)
}
return result, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolListVolumes
func (p *StoragePool) ListStorageVolumes() ([]string, error) {
const maxVols = 1024
var names [maxVols](*C.char)
namesPtr := unsafe.Pointer(&names)
var err C.virError
numStorageVols := C.virStoragePoolListVolumesWrapper(
p.ptr,
(**C.char)(namesPtr),
maxVols, &err)
if numStorageVols == -1 {
return nil, makeError(&err)
}
goNames := make([]string, numStorageVols)
for k := 0; k < int(numStorageVols); k++ {
goNames[k] = C.GoString(names[k])
C.free(unsafe.Pointer(names[k]))
}
return goNames, nil
}
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolListAllVolumes
func (p *StoragePool) ListAllStorageVolumes(flags uint32) ([]StorageVol, error) {
var cList *C.virStorageVolPtr
var err C.virError
numVols := C.virStoragePoolListAllVolumesWrapper(p.ptr, (**C.virStorageVolPtr)(&cList), C.uint(flags), &err)
if numVols == -1 {
return nil, makeError(&err)
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numVols),
Cap: int(numVols),
}
var pools []StorageVol
slice := *(*[]C.virStorageVolPtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
pools = append(pools, StorageVol{ptr})
}
C.free(unsafe.Pointer(cList))
return pools, nil
}