| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package lxc |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import "C" |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"strconv" |
| 21 |
"strings" |
| 22 |
"unsafe" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 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 |
|
| 59 |
func Release(c *Container) bool { |
| 60 |
return c.Release() == nil |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func Version() string { |
| 65 |
version := C.GoString(C.lxc_get_version()) |
| 66 |
|
| 67 |
|
| 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 |
|
| 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 |
|
| 84 |
func DefaultConfigPath() string { |
| 85 |
return GlobalConfigItem("lxc.lxcpath") |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func DefaultLvmVg() string { |
| 90 |
return GlobalConfigItem("lxc.bdev.lvm.vg") |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
func DefaultZfsRoot() string { |
| 95 |
return GlobalConfigItem("lxc.bdev.zfs.root") |
| 96 |
} |
| 97 |
|
| 98 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 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 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 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 |
|
| 192 |
|
| 193 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 246 |
|
| 247 |
func RuntimeLiblxcVersionAtLeast(version string, major int, minor int, micro int) bool { |
| 248 |
|
| 249 |
version = strings.Split(version, "~")[0] |
| 250 |
|
| 251 |
|
| 252 |
version = strings.Replace(version, " (devel)", "-devel", 1) |
| 253 |
|
| 254 |
|
| 255 |
parts := strings.Split(version, ".") |
| 256 |
partsLen := len(parts) |
| 257 |
if partsLen == 0 { |
| 258 |
return false |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
develParts := strings.Split(parts[partsLen-1], "-") |
| 263 |
if len(develParts) == 2 && develParts[1] == "devel" { |
| 264 |
return true |
| 265 |
} |
| 266 |
|
| 267 |
|
| 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 |
|
| 293 |
if maj > major { |
| 294 |
return true |
| 295 |
} |
| 296 |
|
| 297 |
if maj < major { |
| 298 |
return false |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
if min > minor { |
| 303 |
return true |
| 304 |
} |
| 305 |
|
| 306 |
if min < minor { |
| 307 |
return false |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
if mic > micro { |
| 312 |
return true |
| 313 |
} |
| 314 |
|
| 315 |
if mic < micro { |
| 316 |
return false |
| 317 |
} |
| 318 |
|
| 319 |
return true |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
func HasApiExtension(extension string) bool { |
| 325 |
return HasAPIExtension(extension) |
| 326 |
} |
| 327 |
|
| 328 |
|
| 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 |
} |