This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
lxc.go
155 lines (133 loc) · 4.66 KB
/
lxc.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
// +build !without_lxc
/*
* 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.
*
*/
package libvirt
/*
#cgo pkg-config: libvirt
// Can't rely on pkg-config for libvirt-lxc since it was not
// installed until 2.6.0 onwards
#cgo LDFLAGS: -lvirt-lxc
#include <stdlib.h>
#include <string.h>
#include "lxc_wrapper.h"
*/
import "C"
import (
"os"
"unsafe"
)
func (d *Domain) LxcOpenNamespace(flags uint32) ([]os.File, error) {
var cfdlist *C.int
var err C.virError
ret := C.virDomainLxcOpenNamespaceWrapper(d.ptr, &cfdlist, C.uint(flags), &err)
if ret == -1 {
return []os.File{}, makeError(&err)
}
fdlist := make([]os.File, ret)
for i := 0; i < int(ret); i++ {
var cfd C.int
cfd = *(*C.int)(unsafe.Pointer(uintptr(unsafe.Pointer(cfdlist)) + (unsafe.Sizeof(cfd) * uintptr(i))))
fdlist[i] = *os.NewFile(uintptr(cfd), "namespace")
}
defer C.free(unsafe.Pointer(cfdlist))
return fdlist, nil
}
func (d *Domain) LxcEnterNamespace(fdlist []os.File, flags uint32) ([]os.File, error) {
var coldfdlist *C.int
var ncoldfdlist C.uint
cfdlist := make([]C.int, len(fdlist))
for i := 0; i < len(fdlist); i++ {
cfdlist[i] = C.int(fdlist[i].Fd())
}
var err C.virError
ret := C.virDomainLxcEnterNamespaceWrapper(d.ptr, C.uint(len(fdlist)), &cfdlist[0], &ncoldfdlist, &coldfdlist, C.uint(flags), &err)
if ret == -1 {
return []os.File{}, makeError(&err)
}
oldfdlist := make([]os.File, ncoldfdlist)
for i := 0; i < int(ncoldfdlist); i++ {
var cfd C.int
cfd = *(*C.int)(unsafe.Pointer(uintptr(unsafe.Pointer(coldfdlist)) + (unsafe.Sizeof(cfd) * uintptr(i))))
oldfdlist[i] = *os.NewFile(uintptr(cfd), "namespace")
}
defer C.free(unsafe.Pointer(coldfdlist))
return oldfdlist, nil
}
func DomainLxcEnterSecurityLabel(model *NodeSecurityModel, label *SecurityLabel, flags uint32) (*SecurityLabel, error) {
var cmodel C.virSecurityModel
var clabel C.virSecurityLabel
var coldlabel C.virSecurityLabel
cmodelstrlen := len(model.Model)
if cmodelstrlen > (C.VIR_SECURITY_MODEL_BUFLEN - 1) {
cmodelstrlen = C.VIR_SECURITY_MODEL_BUFLEN - 1
}
cmodelstr := C.CString(model.Model)
defer C.free(unsafe.Pointer(cmodelstr))
cdoistrlen := len(model.Doi)
if cdoistrlen >= (C.VIR_SECURITY_DOI_BUFLEN - 1) {
cdoistrlen = C.VIR_SECURITY_DOI_BUFLEN - 1
}
cdoistr := C.CString(model.Doi)
defer C.free(unsafe.Pointer(cdoistr))
C.memcpy(unsafe.Pointer(&cmodel.model), unsafe.Pointer(cmodelstr), C.size_t(cmodelstrlen))
C.memcpy(unsafe.Pointer(&cmodel.doi), unsafe.Pointer(cdoistr), C.size_t(cdoistrlen))
clabelstrlen := len(label.Label)
if clabelstrlen > (C.VIR_SECURITY_LABEL_BUFLEN - 1) {
clabelstrlen = C.VIR_SECURITY_LABEL_BUFLEN - 1
}
clabelstr := C.CString(label.Label)
defer C.free(unsafe.Pointer(clabelstr))
C.memcpy(unsafe.Pointer(&clabel.label), unsafe.Pointer(clabelstr), C.size_t(clabelstrlen))
if label.Enforcing {
clabel.enforcing = 1
} else {
clabel.enforcing = 0
}
var err C.virError
ret := C.virDomainLxcEnterSecurityLabelWrapper(&cmodel, &clabel, &coldlabel, C.uint(flags), &err)
if ret == -1 {
return nil, makeError(&err)
}
var oldlabel SecurityLabel
oldlabel.Label = C.GoString((*C.char)(unsafe.Pointer(&coldlabel.label)))
if coldlabel.enforcing != 0 {
oldlabel.Enforcing = true
} else {
oldlabel.Enforcing = false
}
return &oldlabel, nil
}
func (d *Domain) DomainLxcEnterCGroup(flags uint32) error {
if C.LIBVIR_VERSION_NUMBER < 2000000 {
return makeNotImplementedError("virDomainLxcEnterCGroup")
}
var err C.virError
ret := C.virDomainLxcEnterCGroupWrapper(d.ptr, C.uint(flags), &err)
if ret == -1 {
return makeError(&err)
}
return nil
}