-
Notifications
You must be signed in to change notification settings - Fork 49
/
domain_1.2.14.go
139 lines (115 loc) · 3.36 KB
/
domain_1.2.14.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
// +build libvirt.1.2.14
package libvirt
/*
#cgo LDFLAGS: -lvirt-qemu -lvirt
#include <libvirt/libvirt.h>
#include <libvirt/libvirt-qemu.h>
#include <libvirt/virterror.h>
#include <stdlib.h>
*/
import "C"
import (
"errors"
"reflect"
"unsafe"
)
type VirDomainIPAddress struct {
Type int
Addr string
Prefix uint
}
type VirDomainInterface struct {
Name string
Hwaddr string
Addrs []VirDomainIPAddress
}
func (d *VirDomain) ListAllInterfaceAddresses(src uint) ([]VirDomainInterface, error) {
var cList *C.virDomainInterfacePtr
numIfaces := int(C.virDomainInterfaceAddresses(d.ptr, (**C.virDomainInterfacePtr)(&cList), C.uint(src), 0))
if numIfaces == -1 {
return nil, GetLastError()
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numIfaces),
Cap: int(numIfaces),
}
ifaces := make([]VirDomainInterface, numIfaces)
ifaceSlice := *(*[]C.virDomainInterfacePtr)(unsafe.Pointer(&hdr))
for i := 0; i < numIfaces; i++ {
ifaces[i].Name = C.GoString(ifaceSlice[i].name)
ifaces[i].Hwaddr = C.GoString(ifaceSlice[i].hwaddr)
numAddr := int(ifaceSlice[i].naddrs)
addrHdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&ifaceSlice[i].addrs)),
Len: int(numAddr),
Cap: int(numAddr),
}
ifaces[i].Addrs = make([]VirDomainIPAddress, numAddr)
addrSlice := *(*[]C.virDomainIPAddressPtr)(unsafe.Pointer(&addrHdr))
for k := 0; k < numAddr; k++ {
ifaces[i].Addrs[k] = VirDomainIPAddress{}
ifaces[i].Addrs[k].Type = int(addrSlice[k]._type)
ifaces[i].Addrs[k].Addr = C.GoString(addrSlice[k].addr)
ifaces[i].Addrs[k].Prefix = uint(addrSlice[k].prefix)
}
C.virDomainInterfaceFree(ifaceSlice[i])
}
C.free(unsafe.Pointer(cList))
return ifaces, nil
}
func (dest *VirTypedParameters) loadToCPtr() (params C.virTypedParameterPtr, nParams C.int, err error) {
var maxParams C.int = 0
params = nil
for _, param := range *dest {
switch param.Name {
case VIR_DOMAIN_BLOCK_COPY_GRANULARITY:
value, ok := param.Value.(uint)
if !ok {
err = errors.New("Mismatched parameter value type")
return
}
cName := C.CString(param.Name)
defer C.free(unsafe.Pointer(cName))
result := C.virTypedParamsAddUInt(¶ms, &nParams, &maxParams, cName, C.uint(value))
if result == -1 {
err = GetLastError()
return
}
case VIR_DOMAIN_BLOCK_COPY_BANDWIDTH, VIR_DOMAIN_BLOCK_COPY_BUF_SIZE:
value, ok := param.Value.(uint64)
if !ok {
err = errors.New("Mismatched parameter value type")
return
}
cName := C.CString(param.Name)
defer C.free(unsafe.Pointer(cName))
result := C.virTypedParamsAddULLong(¶ms, &nParams, &maxParams, cName, C.ulonglong(value))
if result == -1 {
err = GetLastError()
return
}
default:
err = errors.New("Unknown parameter name: " + param.Name)
return
}
}
return
}
func (d *VirDomain) BlockCopy(disk string, destXML string, params VirTypedParameters, flags uint32) error {
cDisk := C.CString(disk)
defer C.free(unsafe.Pointer(cDisk))
cDestXML := C.CString(destXML)
defer C.free(unsafe.Pointer(cDestXML))
if cParams, cnParams, err := params.loadToCPtr(); err != nil {
C.virTypedParamsFree(cParams, cnParams)
return err
} else {
defer C.virTypedParamsFree(cParams, cnParams)
result := int(C.virDomainBlockCopy(d.ptr, cDisk, cDestXML, cParams, cnParams, C.uint(flags)))
if result == -1 {
return GetLastError()
}
}
return nil
}