This repository has been archived by the owner on Aug 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices.go
211 lines (159 loc) · 5.3 KB
/
devices.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
package main
import (
"context"
"errors"
"strings"
"time"
"github.com/libvirt/libvirt-go"
"github.com/libvirt/libvirt-go-xml"
)
/*
func getNodeDeviceParent(ctx context.Context, c *libvirt.Connect, dev *libvirt.NodeDevice) (string, error) {
id := getReqIDFromContext(ctx)
parent, err := dev.GetParent()
if err != nil {
fail.Printf("%sfailed to get parent for node network device: %s\n", id, err.Error())
return "", err
}
info.Printf("%snode network device parent %s\n", id, parent)
return parent, nil
}
*/
/*
func getNodeDeviceName(ctx context.Context, c *libvirt.Connect, dev *libvirt.NodeDevice) (string, error) {
id := getReqIDFromContext(ctx)
name, err := dev.GetName()
if err != nil {
fail.Printf("%sfailed to get node network device name: %s\n", id, err.Error())
return "", err
}
info.Printf("%snode network device name: %s", id, name)
return name, nil
}
*/
func listNodeNetworkDevices(ctx context.Context, c *libvirt.Connect) ([]libvirt.NodeDevice, error) {
id := getReqIDFromContext(ctx)
devs, err := c.ListAllNodeDevices(libvirt.CONNECT_LIST_NODE_DEVICES_CAP_NET)
if err != nil {
fail.Printf("%sfailed to get list of node network devices: %s\n", id, err.Error())
return []libvirt.NodeDevice{}, err
}
if len(devs) == 0 {
fail.Printf("%sfailed to get list of node network devices: %s\n", id, errors.New("network array has zero size"))
return []libvirt.NodeDevice{}, errors.New("network array has zero size")
}
info.Printf("%sacquired list of node network devices\n", id)
return devs, nil
}
func detachNodeDeviceWithFlags(ctx context.Context, d *libvirt.Domain, xml string, flags libvirt.DomainDeviceModifyFlags) error {
id := getReqIDFromContext(ctx)
err := d.DetachDeviceFlags(xml, flags)
if err != nil {
fail.Printf("%sfailed to remove SR-IOV network device from domain config: %s\n", id, err.Error())
return err
}
time.Sleep(3 * time.Second)
info.Printf("%sremoved SR-IOV network device from domain config:\n", id)
return nil
}
func attachNodeDeviceWithFlags(ctx context.Context, d *libvirt.Domain, xml string, flags libvirt.DomainDeviceModifyFlags) error {
id := getReqIDFromContext(ctx)
err := d.AttachDeviceFlags(xml, flags)
if err != nil {
fail.Printf("%sfailed to re-attach SR-IOV network device from domain config: %s\n", id, err.Error())
return err
}
time.Sleep(3 * time.Second)
info.Printf("%sre-attached SR-IOV network device from domain config:\n", id)
return nil
}
func lookupNodeDeviceByName(ctx context.Context, c *libvirt.Connect, devID string) (*libvirt.NodeDevice, error) {
id := getReqIDFromContext(ctx)
dev, err := c.LookupDeviceByName(devID)
if err != nil || dev == nil {
fail.Printf("%sfailed to find device %s: %s\n", id, devID, err.Error())
return nil, err
}
info.Printf("%sfound device %s\n", id, devID)
return dev, nil
}
func freeNodeDevice(ctx context.Context, n *libvirt.NodeDevice) {
id := getReqIDFromContext(ctx)
err := n.Free()
if err != nil {
fail.Printf("%sfailed to free node device object: %s\n", id, err.Error())
}
info.Printf("%sfreed node device object\n", id)
}
func freeNodeDevices(ctx context.Context, devs []libvirt.NodeDevice) {
id := getReqIDFromContext(ctx)
for _, dev := range devs {
err := dev.Free()
if err != nil {
fail.Printf("%sfailed to free node device object: %s\n", id, err.Error())
}
info.Printf("%sfreed node device object\n", id)
}
}
func getNodePCIDevicePath(ctx context.Context, c *libvirt.Connect, devName string) string {
id := getReqIDFromContext(ctx)
var err error
dev, err := lookupNodeDeviceByName(ctx, c, devName)
defer freeNodeDevice(ctx, dev)
if err != nil || dev == nil {
return ""
}
xml, err := dev.GetXMLDesc(0)
if err != nil {
fail.Printf("%sfailed to get device %s XML: %s\n", id, devName, err.Error())
return ""
}
info.Printf("%sacquired device %s XML\n", id, devName)
nodeDev := &libvirtxml.NodeDevice{}
err = nodeDev.Unmarshal(xml)
if err != nil {
fail.Printf("%sfailed to unmarshal device %s XML: %s\n", id, devName, err.Error())
return ""
}
info.Printf("%sunmarshaled device %s XML\n", id, devName)
info.Printf("%sdevice %s PCI path %s\n", id, devName, nodeDev.Path)
return strings.TrimSpace(nodeDev.Path)
}
func getDomainBlockDeviceNamesOrPaths(ctx context.Context, d *libvirt.Domain, getPath bool) ([]string, error) {
id := getReqIDFromContext(ctx)
var disks, paths []string
xml, err := d.GetXMLDesc(0)
if err != nil {
fail.Printf("%sfailed to get Domain XML: %s\n", id, err.Error())
return []string{}, err
}
info.Printf("%sacquired Domain XML\n", id)
domCfg := &libvirtxml.Domain{}
err = domCfg.Unmarshal(xml)
if err != nil {
fail.Printf("%sfailed to parse Domain XML: %s\n", id, err.Error())
return []string{}, err
}
info.Printf("%sparsed Domain XML\n", id)
if domCfg.Devices != nil {
for _, dev := range domCfg.Devices.Disks {
if dev.Device == "disk" {
if dev.Target != nil {
disks = append(disks, dev.Target.Dev)
info.Printf("%sfound disk %s in Domain XML\n", id, dev.Target.Dev)
}
if dev.Source != nil {
if dev.Source.File != nil {
paths = append(paths, dev.Source.File.File)
info.Printf("%sfound path %s in Domain XML\n", id, dev.Source.File.File)
}
}
}
}
}
info.Printf("%sacquired block device list from domain XML\n", id)
if getPath {
return paths, nil
}
return disks, nil
}