Debian Codegraph @

lxc-binding.go

raw symbols SCIP
1 // Copyright © 2013, 2014, The Go-LXC Authors. All rights reserved.
2 // Use of this source code is governed by a LGPLv2.1
3 // license that can be found in the LICENSE file.
4
5 //go:build linux && cgo
6 // +build linux,cgo
7
8 package lxc
9
10 // #include <lxc/lxccontainer.h>
11 // #include <lxc/version.h>
12 // #include "lxc-binding.h"
13 // #ifndef LXC_DEVEL
14 // #define LXC_DEVEL 0
15 // #endif
16 import "C"
17
18 import (
19 "fmt"
20 "strconv"
21 "strings"
22 "unsafe"
23 )
24
25 // NewContainer returns a new container struct.
26 // Caller needs to call Release() on the returned container to release its resources.
27 func NewContainer(name string, lxcpath ...string) (*Container, error) {
28 var container *C.struct_lxc_container
29
30 cname := C.CString(name)
31 defer C.free(unsafe.Pointer(cname))
32
33 if lxcpath != nil && len(lxcpath) == 1 {
34 clxcpath := C.CString(lxcpath[0])
35 defer C.free(unsafe.Pointer(clxcpath))
36
37 container = C.lxc_container_new(cname, clxcpath)
38 } else {
39 container = C.lxc_container_new(cname, nil)
40 }
41
42 if container == nil {
43 return nil, ErrNewFailed
44 }
45 c := &Container{container: container, verbosity: Quiet}
46
47 return c, nil
48 }
49
50 // Acquire increments the reference counter of the container object.
51 func Acquire(c *Container) bool {
52 c.mu.RLock()
53 defer c.mu.RUnlock()
54
55 return C.lxc_container_get(c.container) == 1
56 }
57
58 // Release decrements the reference counter of the container object.
59 func Release(c *Container) bool {
60 return c.Release() == nil
61 }
62
63 // Version returns the LXC version.
64 func Version() string {
65 version := C.GoString(C.lxc_get_version())
66
67 // New liblxc versions append "-devel" when LXC_DEVEL is set.
68 if strings.HasSuffix(version, "-devel") {
69 return fmt.Sprintf("%s (devel)", version[:(len(version)-len("-devel"))])
70 }
71
72 return version
73 }
74
75 // GlobalConfigItem returns the value of the given global config key.
76 func GlobalConfigItem(name string) string {
77 cname := C.CString(name)
78 defer C.free(unsafe.Pointer(cname))
79
80 return C.GoString(C.lxc_get_global_config_item(cname))
81 }
82
83 // DefaultConfigPath returns default config path.
84 func DefaultConfigPath() string {
85 return GlobalConfigItem("lxc.lxcpath")
86 }
87
88 // DefaultLvmVg returns the name of the default LVM volume group.
89 func DefaultLvmVg() string {
90 return GlobalConfigItem("lxc.bdev.lvm.vg")
91 }
92
93 // DefaultZfsRoot returns the name of the default ZFS root.
94 func DefaultZfsRoot() string {
95 return GlobalConfigItem("lxc.bdev.zfs.root")
96 }
97
98 // ContainerNames returns the names of defined and active containers on the system.
99 func ContainerNames(lxcpath ...string) []string {
100 var size int
101 var cnames **C.char
102
103 if lxcpath != nil && len(lxcpath) == 1 {
104 clxcpath := C.CString(lxcpath[0])
105 defer C.free(unsafe.Pointer(clxcpath))
106
107 size = int(C.list_all_containers(clxcpath, &cnames, nil))
108 } else {
109
110 size = int(C.list_all_containers(nil, &cnames, nil))
111 }
112
113 if size < 1 {
114 return nil
115 }
116 return convertNArgs(cnames, size)
117 }
118
119 // Containers returns the defined and active containers on the system. Only
120 // containers that could retrieved successfully are returned.
121 // Caller needs to call Release() on the returned containers to release resources.
122 func Containers(lxcpath ...string) []*Container {
123 var containers []*Container
124
125 for _, v := range ContainerNames(lxcpath...) {
126 if container, err := NewContainer(v, lxcpath...); err == nil {
127 containers = append(containers, container)
128 }
129 }
130
131 return containers
132 }
133
134 // DefinedContainerNames returns the names of the defined containers on the system.
135 func DefinedContainerNames(lxcpath ...string) []string {
136 var size int
137 var cnames **C.char
138
139 if lxcpath != nil && len(lxcpath) == 1 {
140 clxcpath := C.CString(lxcpath[0])
141 defer C.free(unsafe.Pointer(clxcpath))
142
143 size = int(C.list_defined_containers(clxcpath, &cnames, nil))
144 } else {
145
146 size = int(C.list_defined_containers(nil, &cnames, nil))
147 }
148
149 if size < 1 {
150 return nil
151 }
152 return convertNArgs(cnames, size)
153 }
154
155 // DefinedContainers returns the defined containers on the system. Only
156 // containers that could retrieved successfully are returned.
157 // Caller needs to call Release() on the returned containers to release resources.
158 func DefinedContainers(lxcpath ...string) []*Container {
159 var containers []*Container
160
161 for _, v := range DefinedContainerNames(lxcpath...) {
162 if container, err := NewContainer(v, lxcpath...); err == nil {
163 containers = append(containers, container)
164 }
165 }
166
167 return containers
168 }
169
170 // ActiveContainerNames returns the names of the active containers on the system.
171 func ActiveContainerNames(lxcpath ...string) []string {
172 var size int
173 var cnames **C.char
174
175 if lxcpath != nil && len(lxcpath) == 1 {
176 clxcpath := C.CString(lxcpath[0])
177 defer C.free(unsafe.Pointer(clxcpath))
178
179 size = int(C.list_active_containers(clxcpath, &cnames, nil))
180 } else {
181
182 size = int(C.list_active_containers(nil, &cnames, nil))
183 }
184
185 if size < 1 {
186 return nil
187 }
188 return convertNArgs(cnames, size)
189 }
190
191 // ActiveContainers returns the active containers on the system. Only
192 // containers that could retrieved successfully are returned.
193 // Caller needs to call Release() on the returned containers to release resources.
194 func ActiveContainers(lxcpath ...string) []*Container {
195 var containers []*Container
196
197 for _, v := range ActiveContainerNames(lxcpath...) {
198 if container, err := NewContainer(v, lxcpath...); err == nil {
199 containers = append(containers, container)
200 }
201 }
202
203 return containers
204 }
205
206 // VersionNumber returns the LXC version.
207 func VersionNumber() (major int, minor int) {
208 major = C.LXC_VERSION_MAJOR
209 minor = C.LXC_VERSION_MINOR
210
211 return
212 }
213
214 // VersionAtLeast returns true when the tested version >= current version.
215 func VersionAtLeast(major int, minor int, micro int) bool {
216 if C.LXC_DEVEL == 1 {
217 return true
218 }
219
220 if major > C.LXC_VERSION_MAJOR {
221 return false
222 }
223
224 if major == C.LXC_VERSION_MAJOR &&
225 minor > C.LXC_VERSION_MINOR {
226 return false
227 }
228
229 if major == C.LXC_VERSION_MAJOR &&
230 minor == C.LXC_VERSION_MINOR &&
231 micro > C.LXC_VERSION_MICRO {
232 return false
233 }
234
235 return true
236 }
237
238 // IsSupportedConfigItem returns true if the key belongs to a supported config item.
239 func IsSupportedConfigItem(key string) bool {
240 configItem := C.CString(key)
241 defer C.free(unsafe.Pointer(configItem))
242 return bool(C.go_lxc_config_item_is_supported(configItem))
243 }
244
245 // RuntimeLiblxcVersionAtLeast checks if the system's liblxc matches the
246 // provided version requirement
247 func RuntimeLiblxcVersionAtLeast(version string, major int, minor int, micro int) bool {
248 // Strip git versioning from pre-release snapshots.
249 version = strings.Split(version, "~")[0]
250
251 // Convert devel indicator into a valid version.
252 version = strings.Replace(version, " (devel)", "-devel", 1)
253
254 // Split the version into its major, minor and micro parts.
255 parts := strings.Split(version, ".")
256 partsLen := len(parts)
257 if partsLen == 0 {
258 return false
259 }
260
261 // If the last part includes -devel, assume everything is supported.
262 develParts := strings.Split(parts[partsLen-1], "-")
263 if len(develParts) == 2 && develParts[1] == "devel" {
264 return true
265 }
266
267 // Actually parse and compare the version string now.
268 maj := -1
269 min := -1
270 mic := -1
271
272 for i, v := range parts {
273 if i > 2 {
274 break
275 }
276
277 num, err := strconv.Atoi(v)
278 if err != nil {
279 return false
280 }
281
282 switch i {
283 case 0:
284 maj = num
285 case 1:
286 min = num
287 case 2:
288 mic = num
289 }
290 }
291
292 /* Major version is greater. */
293 if maj > major {
294 return true
295 }
296
297 if maj < major {
298 return false
299 }
300
301 /* Minor number is greater.*/
302 if min > minor {
303 return true
304 }
305
306 if min < minor {
307 return false
308 }
309
310 /* Patch number is greater. */
311 if mic > micro {
312 return true
313 }
314
315 if mic < micro {
316 return false
317 }
318
319 return true
320 }
321
322 // HasApiExtension returns true if the extension is supported.
323 // Deprecated: Please use HasAPIExtension instead.
324 func HasApiExtension(extension string) bool {
325 return HasAPIExtension(extension)
326 }
327
328 // HasAPIExtension returns true if the extension is supported.
329 func HasAPIExtension(extension string) bool {
330 if RuntimeLiblxcVersionAtLeast(Version(), 3, 1, 0) {
331 apiExtension := C.CString(extension)
332 defer C.free(unsafe.Pointer(apiExtension))
333 return bool(C.go_lxc_has_api_extension(apiExtension))
334 }
335 return false
336 }