forked from rgbkrk/libvirt-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_pool.go
210 lines (184 loc) · 4.66 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
package libvirt
/*
#cgo LDFLAGS: -lvirt -ldl
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <stdlib.h>
*/
import "C"
import (
"io/ioutil"
"unsafe"
)
type VirStoragePool struct {
ptr C.virStoragePoolPtr
}
type VirStoragePoolInfo struct {
ptr C.virStoragePoolInfo
}
func (p *VirStoragePool) Build(flags uint32) error {
result := C.virStoragePoolBuild(p.ptr, C.uint(flags))
if result == -1 {
return GetLastError()
}
return nil
}
func (p *VirStoragePool) Create(flags uint32) error {
result := C.virStoragePoolCreate(p.ptr, C.uint(flags))
if result == -1 {
return GetLastError()
}
return nil
}
func (p *VirStoragePool) Delete(flags uint32) error {
result := C.virStoragePoolDelete(p.ptr, C.uint(flags))
if result == -1 {
return GetLastError()
}
return nil
}
func (p *VirStoragePool) Destroy() error {
result := C.virStoragePoolDestroy(p.ptr)
if result == -1 {
return GetLastError()
}
return nil
}
func (p *VirStoragePool) Free() error {
if result := C.virStoragePoolFree(p.ptr); result != 0 {
return GetLastError()
}
return nil
}
func (p *VirStoragePool) GetAutostart() (bool, error) {
var out C.int
result := C.virStoragePoolGetAutostart(p.ptr, (*C.int)(unsafe.Pointer(&out)))
if result == -1 {
return false, GetLastError()
}
switch out {
case 1:
return true, nil
default:
return false, nil
}
}
func (p *VirStoragePool) GetInfo() (VirStoragePoolInfo, error) {
pi := VirStoragePoolInfo{}
var ptr C.virStoragePoolInfo
result := C.virStoragePoolGetInfo(p.ptr, (*C.virStoragePoolInfo)(unsafe.Pointer(&ptr)))
if result == -1 {
return pi, GetLastError()
}
pi.ptr = ptr
return pi, nil
}
func (p *VirStoragePool) GetName() (string, error) {
name := C.virStoragePoolGetName(p.ptr)
if name == nil {
return "", GetLastError()
}
return C.GoString(name), nil
}
func (p *VirStoragePool) GetUUID() ([]byte, error) {
var cUuid [C.VIR_UUID_BUFLEN](byte)
cuidPtr := unsafe.Pointer(&cUuid)
result := C.virStoragePoolGetUUID(p.ptr, (*C.uchar)(cuidPtr))
if result != 0 {
return []byte{}, GetLastError()
}
return C.GoBytes(cuidPtr, C.VIR_UUID_BUFLEN), nil
}
func (p *VirStoragePool) GetUUIDString() (string, error) {
var cUuid [C.VIR_UUID_STRING_BUFLEN](C.char)
cuidPtr := unsafe.Pointer(&cUuid)
result := C.virStoragePoolGetUUIDString(p.ptr, (*C.char)(cuidPtr))
if result != 0 {
return "", GetLastError()
}
return C.GoString((*C.char)(cuidPtr)), nil
}
func (p *VirStoragePool) GetXMLDesc(flags uint32) (string, error) {
result := C.virStoragePoolGetXMLDesc(p.ptr, C.uint(flags))
if result == nil {
return "", GetLastError()
}
xml := C.GoString(result)
C.free(unsafe.Pointer(result))
return xml, nil
}
func (p *VirStoragePool) IsActive() (bool, error) {
result := C.virStoragePoolIsActive(p.ptr)
if result == -1 {
return false, GetLastError()
}
if result == 1 {
return true, nil
}
return false, nil
}
func (p *VirStoragePool) SetAutostart(autostart bool) error {
var cAutostart C.int
switch autostart {
case true:
cAutostart = 1
default:
cAutostart = 0
}
result := C.virStoragePoolSetAutostart(p.ptr, cAutostart)
if result == -1 {
return GetLastError()
}
return nil
}
func (p *VirStoragePool) Refresh(flags uint32) error {
result := C.virStoragePoolRefresh(p.ptr, C.uint(flags))
if result == -1 {
return GetLastError()
}
return nil
}
func (p *VirStoragePool) Undefine() error {
result := C.virStoragePoolUndefine(p.ptr)
if result == -1 {
return GetLastError()
}
return nil
}
func (i *VirStoragePoolInfo) GetState() uint8 {
return uint8(i.ptr.state)
}
func (i *VirStoragePoolInfo) GetCapacityInBytes() uint64 {
return uint64(i.ptr.capacity)
}
func (i *VirStoragePoolInfo) GetAllocationInBytes() uint64 {
return uint64(i.ptr.allocation)
}
func (i *VirStoragePoolInfo) GetAvailableInBytes() uint64 {
return uint64(i.ptr.available)
}
func (p *VirStoragePool) StorageVolCreateXMLFromFile(xmlFile string, flags uint32) (VirStorageVol, error) {
xmlConfig, err := ioutil.ReadFile(xmlFile)
if err != nil {
return VirStorageVol{}, err
}
return p.StorageVolCreateXML(string(xmlConfig), flags)
}
func (p *VirStoragePool) StorageVolCreateXML(xmlConfig string, flags uint32) (VirStorageVol, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virStorageVolCreateXML(p.ptr, cXml, C.uint(flags))
if ptr == nil {
return VirStorageVol{}, GetLastError()
}
return VirStorageVol{ptr: ptr}, nil
}
func (p *VirStoragePool) LookupStorageVolByName(name string) (VirStorageVol, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
ptr := C.virStorageVolLookupByName(p.ptr, cName)
if ptr == nil {
return VirStorageVol{}, GetLastError()
}
return VirStorageVol{ptr: ptr}, nil
}