storage_pool.go 12.8 KB
Newer Older
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
/*
 * 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.
 *
 */

27 28 29
package libvirt

/*
30
#cgo pkg-config: libvirt
31
#include <stdlib.h>
32
#include "storage_pool_wrapper.h"
33 34 35 36
*/
import "C"

import (
37
	"reflect"
38 39 40
	"unsafe"
)

41
type StoragePoolState int
42

43
const (
44 45 46 47 48
	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
49 50
)

51
type StoragePoolBuildFlags int
52

53
const (
54 55 56 57 58
	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
59 60
)

61
type StoragePoolCreateFlags int
62 63

const (
64 65 66 67
	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)
68 69
)

70
type StoragePoolDeleteFlags int
71 72

const (
73 74
	STORAGE_POOL_DELETE_NORMAL = StoragePoolDeleteFlags(C.VIR_STORAGE_POOL_DELETE_NORMAL)
	STORAGE_POOL_DELETE_ZEROED = StoragePoolDeleteFlags(C.VIR_STORAGE_POOL_DELETE_ZEROED)
75 76
)

77
type StoragePoolEventID int
78 79

const (
80 81
	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)
82 83
)

84
type StoragePoolEventLifecycleType int
85 86

const (
87 88 89 90
	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)
91 92
	STORAGE_POOL_EVENT_CREATED   = StoragePoolEventLifecycleType(C.VIR_STORAGE_POOL_EVENT_CREATED)
	STORAGE_POOL_EVENT_DELETED   = StoragePoolEventLifecycleType(C.VIR_STORAGE_POOL_EVENT_DELETED)
93 94
)

95
type StoragePool struct {
96 97 98
	ptr C.virStoragePoolPtr
}

99 100
type StoragePoolInfo struct {
	State      StoragePoolState
101 102 103
	Capacity   uint64
	Allocation uint64
	Available  uint64
104 105
}

106
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolBuild
107
func (p *StoragePool) Build(flags StoragePoolBuildFlags) error {
108 109
	var err C.virError
	result := C.virStoragePoolBuildWrapper(p.ptr, C.uint(flags), &err)
110
	if result == -1 {
111
		return makeError(&err)
112 113 114
	}
	return nil
}
115

116
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolCreate
117
func (p *StoragePool) Create(flags StoragePoolCreateFlags) error {
118 119
	var err C.virError
	result := C.virStoragePoolCreateWrapper(p.ptr, C.uint(flags), &err)
120
	if result == -1 {
121
		return makeError(&err)
122 123 124 125
	}
	return nil
}

126
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolDelete
127
func (p *StoragePool) Delete(flags StoragePoolDeleteFlags) error {
128 129
	var err C.virError
	result := C.virStoragePoolDeleteWrapper(p.ptr, C.uint(flags), &err)
130
	if result == -1 {
131
		return makeError(&err)
132 133 134 135
	}
	return nil
}

136
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolDestroy
137
func (p *StoragePool) Destroy() error {
138 139
	var err C.virError
	result := C.virStoragePoolDestroyWrapper(p.ptr, &err)
140
	if result == -1 {
141
		return makeError(&err)
142 143 144 145
	}
	return nil
}

146
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolFree
147
func (p *StoragePool) Free() error {
148 149
	var err C.virError
	ret := C.virStoragePoolFreeWrapper(p.ptr, &err)
150
	if ret == -1 {
151
		return makeError(&err)
152 153 154 155
	}
	return nil
}

156
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolRef
157
func (c *StoragePool) Ref() error {
158 159
	var err C.virError
	ret := C.virStoragePoolRefWrapper(c.ptr, &err)
160
	if ret == -1 {
161
		return makeError(&err)
162 163 164 165
	}
	return nil
}

166
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetAutostart
167
func (p *StoragePool) GetAutostart() (bool, error) {
168
	var out C.int
169 170
	var err C.virError
	result := C.virStoragePoolGetAutostartWrapper(p.ptr, (*C.int)(unsafe.Pointer(&out)), &err)
171
	if result == -1 {
172
		return false, makeError(&err)
173 174 175 176 177 178 179 180 181
	}
	switch out {
	case 1:
		return true, nil
	default:
		return false, nil
	}
}

182
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetInfo
183
func (p *StoragePool) GetInfo() (*StoragePoolInfo, error) {
184
	var cinfo C.virStoragePoolInfo
185 186
	var err C.virError
	result := C.virStoragePoolGetInfoWrapper(p.ptr, &cinfo, &err)
187
	if result == -1 {
188
		return nil, makeError(&err)
189
	}
190 191
	return &StoragePoolInfo{
		State:      StoragePoolState(cinfo.state),
192 193 194 195
		Capacity:   uint64(cinfo.capacity),
		Allocation: uint64(cinfo.allocation),
		Available:  uint64(cinfo.available),
	}, nil
196 197
}

198
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetName
199
func (p *StoragePool) GetName() (string, error) {
200 201
	var err C.virError
	name := C.virStoragePoolGetNameWrapper(p.ptr, &err)
202
	if name == nil {
203
		return "", makeError(&err)
204 205 206 207
	}
	return C.GoString(name), nil
}

208
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetUUID
209
func (p *StoragePool) GetUUID() ([]byte, error) {
210 211
	var cUuid [C.VIR_UUID_BUFLEN](byte)
	cuidPtr := unsafe.Pointer(&cUuid)
212 213
	var err C.virError
	result := C.virStoragePoolGetUUIDWrapper(p.ptr, (*C.uchar)(cuidPtr), &err)
214
	if result != 0 {
215
		return []byte{}, makeError(&err)
216 217 218 219
	}
	return C.GoBytes(cuidPtr, C.VIR_UUID_BUFLEN), nil
}

220
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetUUIDString
221
func (p *StoragePool) GetUUIDString() (string, error) {
222 223
	var cUuid [C.VIR_UUID_STRING_BUFLEN](C.char)
	cuidPtr := unsafe.Pointer(&cUuid)
224 225
	var err C.virError
	result := C.virStoragePoolGetUUIDStringWrapper(p.ptr, (*C.char)(cuidPtr), &err)
226
	if result != 0 {
227
		return "", makeError(&err)
228 229 230 231
	}
	return C.GoString((*C.char)(cuidPtr)), nil
}

232
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolGetXMLDesc
233
func (p *StoragePool) GetXMLDesc(flags StorageXMLFlags) (string, error) {
234 235
	var err C.virError
	result := C.virStoragePoolGetXMLDescWrapper(p.ptr, C.uint(flags), &err)
236
	if result == nil {
237
		return "", makeError(&err)
238 239 240 241 242 243
	}
	xml := C.GoString(result)
	C.free(unsafe.Pointer(result))
	return xml, nil
}

244
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolIsActive
245
func (p *StoragePool) IsActive() (bool, error) {
246 247
	var err C.virError
	result := C.virStoragePoolIsActiveWrapper(p.ptr, &err)
248
	if result == -1 {
249
		return false, makeError(&err)
250 251 252 253 254 255 256
	}
	if result == 1 {
		return true, nil
	}
	return false, nil
}

257
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolIsPersistent
258
func (p *StoragePool) IsPersistent() (bool, error) {
259 260
	var err C.virError
	result := C.virStoragePoolIsPersistentWrapper(p.ptr, &err)
261
	if result == -1 {
262
		return false, makeError(&err)
263 264 265 266 267 268 269
	}
	if result == 1 {
		return true, nil
	}
	return false, nil
}

270
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolSetAutostart
271
func (p *StoragePool) SetAutostart(autostart bool) error {
272 273 274 275 276 277 278
	var cAutostart C.int
	switch autostart {
	case true:
		cAutostart = 1
	default:
		cAutostart = 0
	}
279 280
	var err C.virError
	result := C.virStoragePoolSetAutostartWrapper(p.ptr, cAutostart, &err)
281
	if result == -1 {
282
		return makeError(&err)
283 284 285 286
	}
	return nil
}

287
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolRefresh
288
func (p *StoragePool) Refresh(flags uint32) error {
289 290
	var err C.virError
	result := C.virStoragePoolRefreshWrapper(p.ptr, C.uint(flags), &err)
291
	if result == -1 {
292
		return makeError(&err)
293 294 295 296
	}
	return nil
}

297
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolUndefine
298
func (p *StoragePool) Undefine() error {
299 300
	var err C.virError
	result := C.virStoragePoolUndefineWrapper(p.ptr, &err)
301
	if result == -1 {
302
		return makeError(&err)
303 304 305 306
	}
	return nil
}

307
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolCreateXML
308
func (p *StoragePool) StorageVolCreateXML(xmlConfig string, flags StorageVolCreateFlags) (*StorageVol, error) {
dorzheh's avatar
dorzheh committed
309 310
	cXml := C.CString(string(xmlConfig))
	defer C.free(unsafe.Pointer(cXml))
311 312
	var err C.virError
	ptr := C.virStorageVolCreateXMLWrapper(p.ptr, cXml, C.uint(flags), &err)
dorzheh's avatar
dorzheh committed
313
	if ptr == nil {
314
		return nil, makeError(&err)
dorzheh's avatar
dorzheh committed
315
	}
316
	return &StorageVol{ptr: ptr}, nil
dorzheh's avatar
dorzheh committed
317 318
}

319
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolCreateXMLFrom
320
func (p *StoragePool) StorageVolCreateXMLFrom(xmlConfig string, clonevol *StorageVol, flags StorageVolCreateFlags) (*StorageVol, error) {
321 322
	cXml := C.CString(string(xmlConfig))
	defer C.free(unsafe.Pointer(cXml))
323 324
	var err C.virError
	ptr := C.virStorageVolCreateXMLFromWrapper(p.ptr, cXml, clonevol.ptr, C.uint(flags), &err)
325
	if ptr == nil {
326
		return nil, makeError(&err)
327
	}
328
	return &StorageVol{ptr: ptr}, nil
329 330
}

331
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolLookupByName
332
func (p *StoragePool) LookupStorageVolByName(name string) (*StorageVol, error) {
dorzheh's avatar
dorzheh committed
333 334
	cName := C.CString(name)
	defer C.free(unsafe.Pointer(cName))
335 336
	var err C.virError
	ptr := C.virStorageVolLookupByNameWrapper(p.ptr, cName, &err)
dorzheh's avatar
dorzheh committed
337
	if ptr == nil {
338
		return nil, makeError(&err)
dorzheh's avatar
dorzheh committed
339
	}
340
	return &StorageVol{ptr: ptr}, nil
dorzheh's avatar
dorzheh committed
341
}
342

343
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolNumOfVolumes
344
func (p *StoragePool) NumOfStorageVolumes() (int, error) {
345 346
	var err C.virError
	result := int(C.virStoragePoolNumOfVolumesWrapper(p.ptr, &err))
347
	if result == -1 {
348
		return 0, makeError(&err)
349 350 351 352
	}
	return result, nil
}

353
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolListVolumes
354
func (p *StoragePool) ListStorageVolumes() ([]string, error) {
355 356 357
	const maxVols = 1024
	var names [maxVols](*C.char)
	namesPtr := unsafe.Pointer(&names)
358 359
	var err C.virError
	numStorageVols := C.virStoragePoolListVolumesWrapper(
360 361
		p.ptr,
		(**C.char)(namesPtr),
362
		maxVols, &err)
363
	if numStorageVols == -1 {
364
		return nil, makeError(&err)
365 366 367 368 369 370 371 372 373
	}
	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
}

374
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolListAllVolumes
375
func (p *StoragePool) ListAllStorageVolumes(flags uint32) ([]StorageVol, error) {
376
	var cList *C.virStorageVolPtr
377 378
	var err C.virError
	numVols := C.virStoragePoolListAllVolumesWrapper(p.ptr, (**C.virStorageVolPtr)(&cList), C.uint(flags), &err)
379
	if numVols == -1 {
380
		return nil, makeError(&err)
381 382 383 384 385 386
	}
	hdr := reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(cList)),
		Len:  int(numVols),
		Cap:  int(numVols),
	}
387
	var pools []StorageVol
388 389
	slice := *(*[]C.virStorageVolPtr)(unsafe.Pointer(&hdr))
	for _, ptr := range slice {
390
		pools = append(pools, StorageVol{ptr})
391 392 393 394
	}
	C.free(unsafe.Pointer(cList))
	return pools, nil
}