storage_volume.go 10.3 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.
 *
 */

dorzheh's avatar
dorzheh committed
27 28 29
package libvirt

/*
30
#cgo pkg-config: libvirt
dorzheh's avatar
dorzheh committed
31
#include <stdlib.h>
32
#include "storage_volume_wrapper.h"
dorzheh's avatar
dorzheh committed
33 34 35 36 37 38 39
*/
import "C"

import (
	"unsafe"
)

40
type StorageVolCreateFlags int
41

42
const (
43 44
	STORAGE_VOL_CREATE_PREALLOC_METADATA = StorageVolCreateFlags(C.VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA)
	STORAGE_VOL_CREATE_REFLINK           = StorageVolCreateFlags(C.VIR_STORAGE_VOL_CREATE_REFLINK)
45 46
)

47
type StorageVolDeleteFlags int
48

49
const (
50 51 52
	STORAGE_VOL_DELETE_NORMAL         = StorageVolDeleteFlags(C.VIR_STORAGE_VOL_DELETE_NORMAL)         // Delete metadata only (fast)
	STORAGE_VOL_DELETE_ZEROED         = StorageVolDeleteFlags(C.VIR_STORAGE_VOL_DELETE_ZEROED)         // Clear all data to zeros (slow)
	STORAGE_VOL_DELETE_WITH_SNAPSHOTS = StorageVolDeleteFlags(C.VIR_STORAGE_VOL_DELETE_WITH_SNAPSHOTS) // Force removal of volume, even if in use
53 54
)

55
type StorageVolResizeFlags int
56

57
const (
58 59 60
	STORAGE_VOL_RESIZE_ALLOCATE = StorageVolResizeFlags(C.VIR_STORAGE_VOL_RESIZE_ALLOCATE) // force allocation of new size
	STORAGE_VOL_RESIZE_DELTA    = StorageVolResizeFlags(C.VIR_STORAGE_VOL_RESIZE_DELTA)    // size is relative to current
	STORAGE_VOL_RESIZE_SHRINK   = StorageVolResizeFlags(C.VIR_STORAGE_VOL_RESIZE_SHRINK)   // allow decrease in capacity
61 62
)

63
type StorageVolType int
64

65
const (
66 67 68 69 70 71
	STORAGE_VOL_FILE    = StorageVolType(C.VIR_STORAGE_VOL_FILE)    // Regular file based volumes
	STORAGE_VOL_BLOCK   = StorageVolType(C.VIR_STORAGE_VOL_BLOCK)   // Block based volumes
	STORAGE_VOL_DIR     = StorageVolType(C.VIR_STORAGE_VOL_DIR)     // Directory-passthrough based volume
	STORAGE_VOL_NETWORK = StorageVolType(C.VIR_STORAGE_VOL_NETWORK) //Network volumes like RBD (RADOS Block Device)
	STORAGE_VOL_NETDIR  = StorageVolType(C.VIR_STORAGE_VOL_NETDIR)  // Network accessible directory that can contain other network volumes
	STORAGE_VOL_PLOOP   = StorageVolType(C.VIR_STORAGE_VOL_PLOOP)   // Ploop directory based volumes
72 73
)

74
type StorageVolWipeAlgorithm int
75

76
const (
77 78 79 80 81 82 83 84 85 86
	STORAGE_VOL_WIPE_ALG_ZERO       = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_ZERO)       // 1-pass, all zeroes
	STORAGE_VOL_WIPE_ALG_NNSA       = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_NNSA)       // 4-pass NNSA Policy Letter NAP-14.1-C (XVI-8)
	STORAGE_VOL_WIPE_ALG_DOD        = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_DOD)        // 4-pass DoD 5220.22-M section 8-306 procedure
	STORAGE_VOL_WIPE_ALG_BSI        = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_BSI)        // 9-pass method recommended by the German Center of Security in Information Technologies
	STORAGE_VOL_WIPE_ALG_GUTMANN    = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_GUTMANN)    // The canonical 35-pass sequence
	STORAGE_VOL_WIPE_ALG_SCHNEIER   = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_SCHNEIER)   // 7-pass method described by Bruce Schneier in "Applied Cryptography" (1996)
	STORAGE_VOL_WIPE_ALG_PFITZNER7  = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_PFITZNER7)  // 7-pass random
	STORAGE_VOL_WIPE_ALG_PFITZNER33 = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_PFITZNER33) // 33-pass random
	STORAGE_VOL_WIPE_ALG_RANDOM     = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_RANDOM)     // 1-pass random
	STORAGE_VOL_WIPE_ALG_TRIM       = StorageVolWipeAlgorithm(C.VIR_STORAGE_VOL_WIPE_ALG_TRIM)       // Trim the underlying storage
87 88
)

89
type StorageXMLFlags int
90 91

const (
92
	STORAGE_XML_INACTIVE = StorageXMLFlags(C.VIR_STORAGE_XML_INACTIVE)
93 94
)

95 96 97 98 99 100 101
type StorageVolInfoFlags int

const (
	STORAGE_VOL_USE_ALLOCATION = StorageVolInfoFlags(C.VIR_STORAGE_VOL_USE_ALLOCATION)
	STORAGE_VOL_GET_PHYSICAL   = StorageVolInfoFlags(C.VIR_STORAGE_VOL_GET_PHYSICAL)
)

102 103 104 105 106 107 108 109 110 111 112 113
type StorageVolUploadFlags int

const (
	STORAGE_VOL_UPLOAD_SPARSE_STREAM = StorageVolUploadFlags(C.VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM)
)

type StorageVolDownloadFlags int

const (
	STORAGE_VOL_DOWNLOAD_SPARSE_STREAM = StorageVolDownloadFlags(C.VIR_STORAGE_VOL_DOWNLOAD_SPARSE_STREAM)
)

114
type StorageVol struct {
dorzheh's avatar
dorzheh committed
115 116 117
	ptr C.virStorageVolPtr
}

118 119
type StorageVolInfo struct {
	Type       StorageVolType
120 121
	Capacity   uint64
	Allocation uint64
dorzheh's avatar
dorzheh committed
122 123
}

124
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolDelete
125
func (v *StorageVol) Delete(flags StorageVolDeleteFlags) error {
126 127
	var err C.virError
	result := C.virStorageVolDeleteWrapper(v.ptr, C.uint(flags), &err)
dorzheh's avatar
dorzheh committed
128
	if result == -1 {
129
		return makeError(&err)
dorzheh's avatar
dorzheh committed
130 131 132 133
	}
	return nil
}

134
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolFree
135
func (v *StorageVol) Free() error {
136 137
	var err C.virError
	ret := C.virStorageVolFreeWrapper(v.ptr, &err)
138
	if ret == -1 {
139
		return makeError(&err)
dorzheh's avatar
dorzheh committed
140 141 142 143
	}
	return nil
}

144
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolRef
145
func (c *StorageVol) Ref() error {
146 147
	var err C.virError
	ret := C.virStorageVolRefWrapper(c.ptr, &err)
148
	if ret == -1 {
149
		return makeError(&err)
150 151 152 153
	}
	return nil
}

154
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolGetInfo
155
func (v *StorageVol) GetInfo() (*StorageVolInfo, error) {
156
	var cinfo C.virStorageVolInfo
157 158
	var err C.virError
	result := C.virStorageVolGetInfoWrapper(v.ptr, &cinfo, &err)
dorzheh's avatar
dorzheh committed
159
	if result == -1 {
160
		return nil, makeError(&err)
dorzheh's avatar
dorzheh committed
161
	}
162 163
	return &StorageVolInfo{
		Type:       StorageVolType(cinfo._type),
164 165 166
		Capacity:   uint64(cinfo.capacity),
		Allocation: uint64(cinfo.allocation),
	}, nil
dorzheh's avatar
dorzheh committed
167 168
}

169
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolGetInfoFlags
170 171
func (v *StorageVol) GetInfoFlags(flags StorageVolInfoFlags) (*StorageVolInfo, error) {
	if C.LIBVIR_VERSION_NUMBER < 3000000 {
172
		return nil, makeNotImplementedError("virStorageVolGetInfoFlags")
173 174 175
	}

	var cinfo C.virStorageVolInfo
176 177
	var err C.virError
	result := C.virStorageVolGetInfoFlagsWrapper(v.ptr, &cinfo, C.uint(flags), &err)
178
	if result == -1 {
179
		return nil, makeError(&err)
180 181 182 183 184 185 186 187
	}
	return &StorageVolInfo{
		Type:       StorageVolType(cinfo._type),
		Capacity:   uint64(cinfo.capacity),
		Allocation: uint64(cinfo.allocation),
	}, nil
}

188
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolGetKey
189
func (v *StorageVol) GetKey() (string, error) {
190 191
	var err C.virError
	key := C.virStorageVolGetKeyWrapper(v.ptr, &err)
dorzheh's avatar
dorzheh committed
192
	if key == nil {
193
		return "", makeError(&err)
dorzheh's avatar
dorzheh committed
194 195 196 197
	}
	return C.GoString(key), nil
}

198
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolGetName
199
func (v *StorageVol) GetName() (string, error) {
200 201
	var err C.virError
	name := C.virStorageVolGetNameWrapper(v.ptr, &err)
dorzheh's avatar
dorzheh committed
202
	if name == nil {
203
		return "", makeError(&err)
dorzheh's avatar
dorzheh committed
204 205 206 207
	}
	return C.GoString(name), nil
}

208
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolGetPath
209
func (v *StorageVol) GetPath() (string, error) {
210 211
	var err C.virError
	result := C.virStorageVolGetPathWrapper(v.ptr, &err)
dorzheh's avatar
dorzheh committed
212
	if result == nil {
213
		return "", makeError(&err)
dorzheh's avatar
dorzheh committed
214 215 216 217 218 219
	}
	path := C.GoString(result)
	C.free(unsafe.Pointer(result))
	return path, nil
}

220
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolGetXMLDesc
221
func (v *StorageVol) GetXMLDesc(flags uint32) (string, error) {
222 223
	var err C.virError
	result := C.virStorageVolGetXMLDescWrapper(v.ptr, C.uint(flags), &err)
dorzheh's avatar
dorzheh committed
224
	if result == nil {
225
		return "", makeError(&err)
dorzheh's avatar
dorzheh committed
226 227 228 229 230 231
	}
	xml := C.GoString(result)
	C.free(unsafe.Pointer(result))
	return xml, nil
}

232
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolResize
233
func (v *StorageVol) Resize(capacity uint64, flags StorageVolResizeFlags) error {
234 235
	var err C.virError
	result := C.virStorageVolResizeWrapper(v.ptr, C.ulonglong(capacity), C.uint(flags), &err)
dorzheh's avatar
dorzheh committed
236
	if result == -1 {
237
		return makeError(&err)
dorzheh's avatar
dorzheh committed
238 239 240 241
	}
	return nil
}

242
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolWipe
243
func (v *StorageVol) Wipe(flags uint32) error {
244 245
	var err C.virError
	result := C.virStorageVolWipeWrapper(v.ptr, C.uint(flags), &err)
dorzheh's avatar
dorzheh committed
246
	if result == -1 {
247
		return makeError(&err)
dorzheh's avatar
dorzheh committed
248 249 250
	}
	return nil
}
251

252
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolWipePattern
253
func (v *StorageVol) WipePattern(algorithm StorageVolWipeAlgorithm, flags uint32) error {
254 255
	var err C.virError
	result := C.virStorageVolWipePatternWrapper(v.ptr, C.uint(algorithm), C.uint(flags), &err)
dorzheh's avatar
dorzheh committed
256
	if result == -1 {
257
		return makeError(&err)
dorzheh's avatar
dorzheh committed
258 259 260
	}
	return nil
}
261

262
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolUpload
263
func (v *StorageVol) Upload(stream *Stream, offset, length uint64, flags StorageVolUploadFlags) error {
264 265 266 267
	var err C.virError
	if C.virStorageVolUploadWrapper(v.ptr, stream.ptr, C.ulonglong(offset),
		C.ulonglong(length), C.uint(flags), &err) == -1 {
		return makeError(&err)
268 269 270 271
	}
	return nil
}

272
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolDownload
273
func (v *StorageVol) Download(stream *Stream, offset, length uint64, flags StorageVolDownloadFlags) error {
274 275 276 277
	var err C.virError
	if C.virStorageVolDownloadWrapper(v.ptr, stream.ptr, C.ulonglong(offset),
		C.ulonglong(length), C.uint(flags), &err) == -1 {
		return makeError(&err)
278 279 280
	}
	return nil
}
281

282
// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStoragePoolLookupByVolume
283
func (v *StorageVol) LookupPoolByVolume() (*StoragePool, error) {
284 285
	var err C.virError
	poolPtr := C.virStoragePoolLookupByVolumeWrapper(v.ptr, &err)
286
	if poolPtr == nil {
287
		return nil, makeError(&err)
288
	}
289
	return &StoragePool{ptr: poolPtr}, nil
290
}