forked from rgbkrk/libvirt-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvirt.go
878 lines (786 loc) · 24.2 KB
/
libvirt.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
package libvirt
import (
"io/ioutil"
"reflect"
"unsafe"
)
/*
#cgo LDFLAGS: -lvirt -ldl
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <stdlib.h>
void virErrorFuncDummy(void *userData, virErrorPtr error);
int domainEventLifecycleCallback_cgo(virConnectPtr c, virDomainPtr d,
int event, int detail, void* data);
int domainEventGenericCallback_cgo(virConnectPtr c, virDomainPtr d, void* data);
int domainEventRTCChangeCallback_cgo(virConnectPtr c, virDomainPtr d,
long long utcoffset, void* data);
int domainEventWatchdogCallback_cgo(virConnectPtr c, virDomainPtr d,
int action, void* data);
int domainEventIOErrorCallback_cgo(virConnectPtr c, virDomainPtr d,
const char *srcPath, const char *devAlias,
int action, void* data);
int domainEventGraphicsCallback_cgo(virConnectPtr c, virDomainPtr d,
int phase, const virDomainEventGraphicsAddress *local,
const virDomainEventGraphicsAddress *remote,
const char *authScheme,
const virDomainEventGraphicsSubject *subject, void* data);
int domainEventIOErrorReasonCallback_cgo(virConnectPtr c, virDomainPtr d,
const char *srcPath, const char *devAlias,
int action, const char *reason, void* data);
int domainEventBlockJobCallback_cgo(virConnectPtr c, virDomainPtr d,
const char *disk, int type, int status, void* data);
int domainEventDiskChangeCallback_cgo(virConnectPtr c, virDomainPtr d,
const char *oldSrcPath, const char *newSrcPath,
const char *devAlias, int reason, void* data);
int domainEventTrayChangeCallback_cgo(virConnectPtr c, virDomainPtr d,
const char *devAlias, int reason, void* data);
int domainEventReasonCallback_cgo(virConnectPtr c, virDomainPtr d,
int reason, void* data);
int domainEventBalloonChangeCallback_cgo(virConnectPtr c, virDomainPtr d,
unsigned long long actual, void* data);
int domainEventDeviceRemovedCallback_cgo(virConnectPtr c, virDomainPtr d,
const char *devAlias, void* data);
*/
import "C"
func init() {
// libvirt won't print to stderr
C.virSetErrorFunc(nil, C.virErrorFunc(unsafe.Pointer(C.virErrorFuncDummy)))
}
type VirConnection struct {
ptr C.virConnectPtr
}
func NewVirConnection(uri string) (VirConnection, error) {
var cUri *C.char
if uri != "" {
cUri = C.CString(uri)
defer C.free(unsafe.Pointer(cUri))
}
ptr := C.virConnectOpen(cUri)
if ptr == nil {
return VirConnection{}, GetLastError()
}
obj := VirConnection{ptr: ptr}
return obj, nil
}
func NewVirConnectionReadOnly(uri string) (VirConnection, error) {
var cUri *C.char
if uri != "" {
cUri = C.CString(uri)
defer C.free(unsafe.Pointer(cUri))
}
ptr := C.virConnectOpenReadOnly(cUri)
if ptr == nil {
return VirConnection{}, GetLastError()
}
obj := VirConnection{ptr: ptr}
return obj, nil
}
func GetLastError() VirError {
var virErr VirError
err := C.virGetLastError()
virErr.Code = int(err.code)
virErr.Domain = int(err.domain)
virErr.Message = C.GoString(err.message)
virErr.Level = int(err.level)
C.virResetError(err)
return virErr
}
func (c *VirConnection) CloseConnection() (int, error) {
result := int(C.virConnectClose(c.ptr))
if result == -1 {
return result, GetLastError()
}
return result, nil
}
func (c *VirConnection) UnrefAndCloseConnection() error {
closeRes := 1
var err error
for closeRes > 0 {
closeRes, err = c.CloseConnection()
if err != nil {
return err
}
}
return nil
}
func (c *VirConnection) GetCapabilities() (string, error) {
str := C.virConnectGetCapabilities(c.ptr)
if str == nil {
return "", GetLastError()
}
capabilities := C.GoString(str)
C.free(unsafe.Pointer(str))
return capabilities, nil
}
func (c *VirConnection) GetNodeInfo() (VirNodeInfo, error) {
ni := VirNodeInfo{}
var ptr C.virNodeInfo
result := C.virNodeGetInfo(c.ptr, (*C.virNodeInfo)(unsafe.Pointer(&ptr)))
if result == -1 {
return ni, GetLastError()
}
ni.ptr = ptr
return ni, nil
}
func (c *VirConnection) GetHostname() (string, error) {
str := C.virConnectGetHostname(c.ptr)
if str == nil {
return "", GetLastError()
}
hostname := C.GoString(str)
C.free(unsafe.Pointer(str))
return hostname, nil
}
func (c *VirConnection) GetLibVersion() (uint32, error) {
var version C.ulong
if err := C.virConnectGetLibVersion(c.ptr, &version); err < 0 {
return 0, GetLastError()
}
return uint32(version), nil
}
func (c *VirConnection) GetType() (string, error) {
str := C.virConnectGetType(c.ptr)
if str == nil {
return "", GetLastError()
}
hypDriver := C.GoString(str)
return hypDriver, nil
}
func (c *VirConnection) IsAlive() (bool, error) {
result := C.virConnectIsAlive(c.ptr)
if result == -1 {
return false, GetLastError()
}
if result == 1 {
return true, nil
}
return false, nil
}
func (c *VirConnection) IsEncrypted() (bool, error) {
result := C.virConnectIsEncrypted(c.ptr)
if result == -1 {
return false, GetLastError()
}
if result == 1 {
return true, nil
}
return false, nil
}
func (c *VirConnection) IsSecure() (bool, error) {
result := C.virConnectIsSecure(c.ptr)
if result == -1 {
return false, GetLastError()
}
if result == 1 {
return true, nil
}
return false, nil
}
func (c *VirConnection) ListDefinedDomains() ([]string, error) {
var names [1024](*C.char)
namesPtr := unsafe.Pointer(&names)
numDomains := C.virConnectListDefinedDomains(
c.ptr,
(**C.char)(namesPtr),
1024)
if numDomains == -1 {
return nil, GetLastError()
}
goNames := make([]string, numDomains)
for k := 0; k < int(numDomains); k++ {
goNames[k] = C.GoString(names[k])
C.free(unsafe.Pointer(names[k]))
}
return goNames, nil
}
func (c *VirConnection) ListDomains() ([]uint32, error) {
var cDomainsIds [512](uint32)
cDomainsPointer := unsafe.Pointer(&cDomainsIds)
numDomains := C.virConnectListDomains(c.ptr, (*C.int)(cDomainsPointer), 512)
if numDomains == -1 {
return nil, GetLastError()
}
return cDomainsIds[:numDomains], nil
}
func (c *VirConnection) ListInterfaces() ([]string, error) {
const maxIfaces = 1024
var names [maxIfaces](*C.char)
namesPtr := unsafe.Pointer(&names)
numIfaces := C.virConnectListInterfaces(
c.ptr,
(**C.char)(namesPtr),
maxIfaces)
if numIfaces == -1 {
return nil, GetLastError()
}
goNames := make([]string, numIfaces)
for k := 0; k < int(numIfaces); k++ {
goNames[k] = C.GoString(names[k])
C.free(unsafe.Pointer(names[k]))
}
return goNames, nil
}
func (c *VirConnection) ListNetworks() ([]string, error) {
const maxNets = 1024
var names [maxNets](*C.char)
namesPtr := unsafe.Pointer(&names)
numNetworks := C.virConnectListNetworks(
c.ptr,
(**C.char)(namesPtr),
maxNets)
if numNetworks == -1 {
return nil, GetLastError()
}
goNames := make([]string, numNetworks)
for k := 0; k < int(numNetworks); k++ {
goNames[k] = C.GoString(names[k])
C.free(unsafe.Pointer(names[k]))
}
return goNames, nil
}
func (c *VirConnection) ListStoragePools() ([]string, error) {
const maxPools = 1024
var names [maxPools](*C.char)
namesPtr := unsafe.Pointer(&names)
numStoragePools := C.virConnectListStoragePools(
c.ptr,
(**C.char)(namesPtr),
maxPools)
if numStoragePools == -1 {
return nil, GetLastError()
}
goNames := make([]string, numStoragePools)
for k := 0; k < int(numStoragePools); k++ {
goNames[k] = C.GoString(names[k])
C.free(unsafe.Pointer(names[k]))
}
return goNames, nil
}
func (c *VirConnection) LookupDomainById(id uint32) (VirDomain, error) {
ptr := C.virDomainLookupByID(c.ptr, C.int(id))
if ptr == nil {
return VirDomain{}, GetLastError()
}
return VirDomain{ptr: ptr}, nil
}
func (c *VirConnection) LookupDomainByName(id string) (VirDomain, error) {
cName := C.CString(id)
defer C.free(unsafe.Pointer(cName))
ptr := C.virDomainLookupByName(c.ptr, cName)
if ptr == nil {
return VirDomain{}, GetLastError()
}
return VirDomain{ptr: ptr}, nil
}
func (c *VirConnection) DomainCreateXMLFromFile(xmlFile string, flags uint32) (VirDomain, error) {
xmlConfig, err := ioutil.ReadFile(xmlFile)
if err != nil {
return VirDomain{}, err
}
return c.DomainCreateXML(string(xmlConfig), flags)
}
func (c *VirConnection) DomainCreateXML(xmlConfig string, flags uint32) (VirDomain, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virDomainCreateXML(c.ptr, cXml, C.uint(flags))
if ptr == nil {
return VirDomain{}, GetLastError()
}
return VirDomain{ptr: ptr}, nil
}
func (c *VirConnection) DomainDefineXMLFromFile(xmlFile string) (VirDomain, error) {
xmlConfig, err := ioutil.ReadFile(xmlFile)
if err != nil {
return VirDomain{}, err
}
return c.DomainDefineXML(string(xmlConfig))
}
func (c *VirConnection) DomainDefineXML(xmlConfig string) (VirDomain, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virDomainDefineXML(c.ptr, cXml)
if ptr == nil {
return VirDomain{}, GetLastError()
}
return VirDomain{ptr: ptr}, nil
}
func (c *VirConnection) ListDefinedInterfaces() ([]string, error) {
const maxIfaces = 1024
var names [maxIfaces](*C.char)
namesPtr := unsafe.Pointer(&names)
numIfaces := C.virConnectListDefinedInterfaces(
c.ptr,
(**C.char)(namesPtr),
maxIfaces)
if numIfaces == -1 {
return nil, GetLastError()
}
goNames := make([]string, numIfaces)
for k := 0; k < int(numIfaces); k++ {
goNames[k] = C.GoString(names[k])
C.free(unsafe.Pointer(names[k]))
}
return goNames, nil
}
func (c *VirConnection) ListDefinedNetworks() ([]string, error) {
const maxNets = 1024
var names [maxNets](*C.char)
namesPtr := unsafe.Pointer(&names)
numNetworks := C.virConnectListDefinedNetworks(
c.ptr,
(**C.char)(namesPtr),
maxNets)
if numNetworks == -1 {
return nil, GetLastError()
}
goNames := make([]string, numNetworks)
for k := 0; k < int(numNetworks); k++ {
goNames[k] = C.GoString(names[k])
C.free(unsafe.Pointer(names[k]))
}
return goNames, nil
}
func (c *VirConnection) ListDefinedStoragePools() ([]string, error) {
const maxPools = 1024
var names [maxPools](*C.char)
namesPtr := unsafe.Pointer(&names)
numStoragePools := C.virConnectListDefinedStoragePools(
c.ptr,
(**C.char)(namesPtr),
maxPools)
if numStoragePools == -1 {
return nil, GetLastError()
}
goNames := make([]string, numStoragePools)
for k := 0; k < int(numStoragePools); k++ {
goNames[k] = C.GoString(names[k])
C.free(unsafe.Pointer(names[k]))
}
return goNames, nil
}
func (c *VirConnection) NumOfDefinedInterfaces() (int, error) {
result := int(C.virConnectNumOfDefinedInterfaces(c.ptr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) NumOfDefinedNetworks() (int, error) {
result := int(C.virConnectNumOfDefinedNetworks(c.ptr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) NumOfDefinedStoragePools() (int, error) {
result := int(C.virConnectNumOfDefinedStoragePools(c.ptr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) NumOfDomains() (int, error) {
result := int(C.virConnectNumOfDomains(c.ptr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) NumOfInterfaces() (int, error) {
result := int(C.virConnectNumOfInterfaces(c.ptr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) NumOfNetworks() (int, error) {
result := int(C.virConnectNumOfNetworks(c.ptr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) NumOfNWFilters() (int, error) {
result := int(C.virConnectNumOfNWFilters(c.ptr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) NumOfSecrets() (int, error) {
result := int(C.virConnectNumOfSecrets(c.ptr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) NetworkDefineXMLFromFile(xmlFile string) (VirNetwork, error) {
xmlConfig, err := ioutil.ReadFile(xmlFile)
if err != nil {
return VirNetwork{}, err
}
return c.NetworkDefineXML(string(xmlConfig))
}
func (c *VirConnection) NetworkDefineXML(xmlConfig string) (VirNetwork, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virNetworkDefineXML(c.ptr, cXml)
if ptr == nil {
return VirNetwork{}, GetLastError()
}
return VirNetwork{ptr: ptr}, nil
}
func (c *VirConnection) LookupNetworkByName(name string) (VirNetwork, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
ptr := C.virNetworkLookupByName(c.ptr, cName)
if ptr == nil {
return VirNetwork{}, GetLastError()
}
return VirNetwork{ptr: ptr}, nil
}
func (c *VirConnection) GetSysinfo(flags uint) (string, error) {
cStr := C.virConnectGetSysinfo(c.ptr, C.uint(flags))
if cStr == nil {
return "", GetLastError()
}
info := C.GoString(cStr)
C.free(unsafe.Pointer(cStr))
return info, nil
}
func (c *VirConnection) GetURI() (string, error) {
cStr := C.virConnectGetURI(c.ptr)
if cStr == nil {
return "", GetLastError()
}
uri := C.GoString(cStr)
C.free(unsafe.Pointer(cStr))
return uri, nil
}
func (c *VirConnection) GetMaxVcpus(typeAttr string) (int, error) {
var cTypeAttr *C.char
if typeAttr != "" {
cTypeAttr = C.CString(typeAttr)
defer C.free(unsafe.Pointer(cTypeAttr))
}
result := int(C.virConnectGetMaxVcpus(c.ptr, cTypeAttr))
if result == -1 {
return 0, GetLastError()
}
return result, nil
}
func (c *VirConnection) InterfaceDefineXMLFromFile(xmlFile string) (VirInterface, error) {
xmlConfig, err := ioutil.ReadFile(xmlFile)
if err != nil {
return VirInterface{}, err
}
return c.InterfaceDefineXML(string(xmlConfig), 0)
}
func (c *VirConnection) InterfaceDefineXML(xmlConfig string, flags uint32) (VirInterface, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virInterfaceDefineXML(c.ptr, cXml, C.uint(flags))
if ptr == nil {
return VirInterface{}, GetLastError()
}
return VirInterface{ptr: ptr}, nil
}
func (c *VirConnection) LookupInterfaceByName(name string) (VirInterface, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
ptr := C.virInterfaceLookupByName(c.ptr, cName)
if ptr == nil {
return VirInterface{}, GetLastError()
}
return VirInterface{ptr: ptr}, nil
}
func (c *VirConnection) LookupInterfaceByMACString(mac string) (VirInterface, error) {
cName := C.CString(mac)
defer C.free(unsafe.Pointer(cName))
ptr := C.virInterfaceLookupByMACString(c.ptr, cName)
if ptr == nil {
return VirInterface{}, GetLastError()
}
return VirInterface{ptr: ptr}, nil
}
func (c *VirConnection) StoragePoolDefineXMLFromFile(xmlFile string) (VirStoragePool, error) {
xmlConfig, err := ioutil.ReadFile(xmlFile)
if err != nil {
return VirStoragePool{}, err
}
return c.StoragePoolDefineXML(string(xmlConfig), 0)
}
func (c *VirConnection) StoragePoolDefineXML(xmlConfig string, flags uint32) (VirStoragePool, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virStoragePoolDefineXML(c.ptr, cXml, C.uint(flags))
if ptr == nil {
return VirStoragePool{}, GetLastError()
}
return VirStoragePool{ptr: ptr}, nil
}
func (c *VirConnection) LookupStoragePoolByName(name string) (VirStoragePool, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
ptr := C.virStoragePoolLookupByName(c.ptr, cName)
if ptr == nil {
return VirStoragePool{}, GetLastError()
}
return VirStoragePool{ptr: ptr}, nil
}
func (c *VirConnection) LookupStoragePoolByUUIDString(uuid string) (VirStoragePool, error) {
cUuid := C.CString(uuid)
defer C.free(unsafe.Pointer(cUuid))
ptr := C.virStoragePoolLookupByUUIDString(c.ptr, cUuid)
if ptr == nil {
return VirStoragePool{}, GetLastError()
}
return VirStoragePool{ptr: ptr}, nil
}
func (c *VirConnection) NWFilterDefineXMLFromFile(xmlFile string) (VirNWFilter, error) {
xmlConfig, err := ioutil.ReadFile(xmlFile)
if err != nil {
return VirNWFilter{}, err
}
return c.NWFilterDefineXML(string(xmlConfig))
}
func (c *VirConnection) NWFilterDefineXML(xmlConfig string) (VirNWFilter, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virNWFilterDefineXML(c.ptr, cXml)
if ptr == nil {
return VirNWFilter{}, GetLastError()
}
return VirNWFilter{ptr: ptr}, nil
}
func (c *VirConnection) LookupNWFilterByName(name string) (VirNWFilter, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
ptr := C.virNWFilterLookupByName(c.ptr, cName)
if ptr == nil {
return VirNWFilter{}, GetLastError()
}
return VirNWFilter{ptr: ptr}, nil
}
func (c *VirConnection) LookupNWFilterByUUIDString(uuid string) (VirNWFilter, error) {
cUuid := C.CString(uuid)
defer C.free(unsafe.Pointer(cUuid))
ptr := C.virNWFilterLookupByUUIDString(c.ptr, cUuid)
if ptr == nil {
return VirNWFilter{}, GetLastError()
}
return VirNWFilter{ptr: ptr}, nil
}
func (c *VirConnection) LookupStorageVolByKey(key string) (VirStorageVol, error) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
ptr := C.virStorageVolLookupByKey(c.ptr, cKey)
if ptr == nil {
return VirStorageVol{}, GetLastError()
}
return VirStorageVol{ptr: ptr}, nil
}
func (c *VirConnection) LookupStorageVolByPath(path string) (VirStorageVol, error) {
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
ptr := C.virStorageVolLookupByPath(c.ptr, cPath)
if ptr == nil {
return VirStorageVol{}, GetLastError()
}
return VirStorageVol{ptr: ptr}, nil
}
func (c *VirConnection) SecretDefineXMLFromFile(xmlFile string) (VirSecret, error) {
xmlConfig, err := ioutil.ReadFile(xmlFile)
if err != nil {
return VirSecret{}, err
}
return c.SecretDefineXML(string(xmlConfig), 0)
}
func (c *VirConnection) SecretDefineXML(xmlConfig string, flags uint32) (VirSecret, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virSecretDefineXML(c.ptr, cXml, C.uint(flags))
if ptr == nil {
return VirSecret{}, GetLastError()
}
return VirSecret{ptr: ptr}, nil
}
func (c *VirConnection) LookupSecretByUUIDString(uuid string) (VirSecret, error) {
cUuid := C.CString(uuid)
defer C.free(unsafe.Pointer(cUuid))
ptr := C.virSecretLookupByUUIDString(c.ptr, cUuid)
if ptr == nil {
return VirSecret{}, GetLastError()
}
return VirSecret{ptr: ptr}, nil
}
func (c *VirConnection) LookupSecretByUsage(usageType int, usageID string) (VirSecret, error) {
cUsageID := C.CString(usageID)
defer C.free(unsafe.Pointer(cUsageID))
ptr := C.virSecretLookupByUsage(c.ptr, C.int(usageType), cUsageID)
if ptr == nil {
return VirSecret{}, GetLastError()
}
return VirSecret{ptr: ptr}, nil
}
func (c *VirConnection) ListAllInterfaces(flags uint32) ([]VirInterface, error) {
var cList *C.virInterfacePtr
numIfaces := C.virConnectListAllInterfaces(c.ptr, (**C.virInterfacePtr)(&cList), C.uint(flags))
if numIfaces == -1 {
return nil, GetLastError()
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numIfaces),
Cap: int(numIfaces),
}
var ifaces []VirInterface
slice := *(*[]C.virInterfacePtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
ifaces = append(ifaces, VirInterface{ptr})
}
C.free(unsafe.Pointer(cList))
return ifaces, nil
}
func (c *VirConnection) ListAllNetworks(flags uint32) ([]VirNetwork, error) {
var cList *C.virNetworkPtr
numNets := C.virConnectListAllNetworks(c.ptr, (**C.virNetworkPtr)(&cList), C.uint(flags))
if numNets == -1 {
return nil, GetLastError()
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numNets),
Cap: int(numNets),
}
var nets []VirNetwork
slice := *(*[]C.virNetworkPtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
nets = append(nets, VirNetwork{ptr})
}
C.free(unsafe.Pointer(cList))
return nets, nil
}
func (c *VirConnection) ListAllDomains(flags uint32) ([]VirDomain, error) {
var cList *C.virDomainPtr
numDomains := C.virConnectListAllDomains(c.ptr, (**C.virDomainPtr)(&cList), C.uint(flags))
if numDomains == -1 {
return nil, GetLastError()
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numDomains),
Cap: int(numDomains),
}
var domains []VirDomain
slice := *(*[]C.virDomainPtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
domains = append(domains, VirDomain{ptr})
}
C.free(unsafe.Pointer(cList))
return domains, nil
}
func (c *VirConnection) ListAllNWFilters(flags uint32) ([]VirNWFilter, error) {
var cList *C.virNWFilterPtr
numNWFilters := C.virConnectListAllNWFilters(c.ptr, (**C.virNWFilterPtr)(&cList), C.uint(flags))
if numNWFilters == -1 {
return nil, GetLastError()
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numNWFilters),
Cap: int(numNWFilters),
}
var filters []VirNWFilter
slice := *(*[]C.virNWFilterPtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
filters = append(filters, VirNWFilter{ptr})
}
C.free(unsafe.Pointer(cList))
return filters, nil
}
func (c *VirConnection) ListAllStoragePools(flags uint32) ([]VirStoragePool, error) {
var cList *C.virStoragePoolPtr
numPools := C.virConnectListAllStoragePools(c.ptr, (**C.virStoragePoolPtr)(&cList), C.uint(flags))
if numPools == -1 {
return nil, GetLastError()
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numPools),
Cap: int(numPools),
}
var pools []VirStoragePool
slice := *(*[]C.virStoragePoolPtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
pools = append(pools, VirStoragePool{ptr})
}
C.free(unsafe.Pointer(cList))
return pools, nil
}
type DomainEventCallback func(c *VirConnection, d *VirDomain,
event interface{}, f func()) int
type domainCallbackContext struct {
cb *DomainEventCallback
f func()
}
func (c *VirConnection) DomainEventRegister(dom VirDomain,
eventId int,
callback *DomainEventCallback,
opaque func()) int {
var callbackPtr unsafe.Pointer
context := domainCallbackContext{
cb: callback,
f: opaque,
}
switch eventId {
case VIR_DOMAIN_EVENT_ID_LIFECYCLE:
callbackPtr = unsafe.Pointer(C.domainEventLifecycleCallback_cgo)
case VIR_DOMAIN_EVENT_ID_REBOOT:
case VIR_DOMAIN_EVENT_ID_CONTROL_ERROR:
callbackPtr = unsafe.Pointer(C.domainEventGenericCallback_cgo)
case VIR_DOMAIN_EVENT_ID_RTC_CHANGE:
callbackPtr = unsafe.Pointer(C.domainEventRTCChangeCallback_cgo)
case VIR_DOMAIN_EVENT_ID_WATCHDOG:
callbackPtr = unsafe.Pointer(C.domainEventWatchdogCallback_cgo)
case VIR_DOMAIN_EVENT_ID_IO_ERROR:
callbackPtr = unsafe.Pointer(C.domainEventIOErrorCallback_cgo)
case VIR_DOMAIN_EVENT_ID_GRAPHICS:
callbackPtr = unsafe.Pointer(C.domainEventGraphicsCallback_cgo)
case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON:
callbackPtr = unsafe.Pointer(C.domainEventIOErrorReasonCallback_cgo)
case VIR_DOMAIN_EVENT_ID_BLOCK_JOB:
// TODO Post 1.2.4, uncomment later
// case VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2:
callbackPtr = unsafe.Pointer(C.domainEventBlockJobCallback_cgo)
case VIR_DOMAIN_EVENT_ID_DISK_CHANGE:
callbackPtr = unsafe.Pointer(C.domainEventDiskChangeCallback_cgo)
case VIR_DOMAIN_EVENT_ID_TRAY_CHANGE:
callbackPtr = unsafe.Pointer(C.domainEventTrayChangeCallback_cgo)
case VIR_DOMAIN_EVENT_ID_PMWAKEUP:
case VIR_DOMAIN_EVENT_ID_PMSUSPEND:
case VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK:
callbackPtr = unsafe.Pointer(C.domainEventReasonCallback_cgo)
case VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE:
callbackPtr = unsafe.Pointer(C.domainEventBalloonChangeCallback_cgo)
case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED:
callbackPtr = unsafe.Pointer(C.domainEventDeviceRemovedCallback_cgo)
default:
}
ret := C.virConnectDomainEventRegisterAny(c.ptr, dom.ptr, C.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
C.virConnectDomainEventGenericCallback(callbackPtr),
unsafe.Pointer(&context),
nil)
return int(ret)
}
func (c *VirConnection) DomainEventDeregister(callbackId int) int {
// Deregister the callback
return int(C.virConnectDomainEventDeregisterAny(c.ptr, C.int(callbackId)))
}
func EventRegisterDefaultImpl() int {
return int(C.virEventRegisterDefaultImpl())
}
func EventRunDefaultImpl() int {
return int(C.virEventRunDefaultImpl())
}