1
0
mirror of https://github.com/lxc/go-lxc.git synced 2026-02-05 06:46:38 +01:00

playing with ideas...

This commit is contained in:
S.Çağlar Onur
2013-04-24 19:32:25 -04:00
parent 76a9cf7b1f
commit 0dfb165a66
7 changed files with 101 additions and 2 deletions

View File

@@ -2,9 +2,10 @@ format:
gofmt -s -w *.go
test:
sudo `which go` test -v
test_clone:
sudo `which go` test -run TestClone -v
test_concurrent:
sudo `which go` test -run TestConcurrentDefined_Negative -v
sudo `which go` test -run TestConcurrentCreate -v
sudo `which go` test -run TestConcurrentDefined_Positive -v
sudo `which go` test -run TestConcurrentDestroy -v

46
backend.go Normal file
View File

@@ -0,0 +1,46 @@
/*
* backend.go: Go bindings for lxc
*
* Copyright © 2013, S.Çağlar Onur
*
* Authors:
* S.Çağlar Onur <caglar@10ur.org>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package lxc
type Backend int
const (
BTRFS Backend = iota
DIRECTORY
LVM
OVERLAYFS
)
// Backend as string
func (t Backend) String() string {
switch t {
case DIRECTORY:
return "Directory"
case BTRFS:
return "BtrFS"
case LVM:
return "LVM"
case OVERLAYFS:
return "OverlayFS"
}
return "<INVALID>"
}

View File

@@ -31,7 +31,7 @@ import (
"strconv"
"strings"
"time"
// "syscall"
// "syscall"
"sync"
"unsafe"
)
@@ -167,6 +167,22 @@ func (lxc *Container) Destroy() bool {
return bool(C.lxc_container_destroy(lxc.container))
}
// FIXME: consider other parameters
// FIXME: Clone or CloneToOverlayFS, CloneToLVM, SnapshotToLVM etc?
func (lxc *Container) Clone(name string, backend Backend) bool {
lxc.Lock()
defer lxc.Unlock()
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
if backend == OVERLAYFS {
return bool(C.lxc_container_clone(lxc.container, cname, C.int(LXC_CLONE_SNAPSHOT), C.CString("overlayfs")))
} else {
return bool(C.lxc_container_clone(lxc.container, cname, 0, nil))
}
}
// Waits till the container changes its state or timeouts
func (lxc *Container) Wait(state State, timeout int) bool {
lxc.Lock()

4
lxc.c
View File

@@ -148,3 +148,7 @@ bool lxc_container_load_config(struct lxc_container *c, char *alt_file) {
bool lxc_container_save_config(struct lxc_container *c, char *alt_file) {
return c->save_config(c, alt_file);
}
bool lxc_container_clone(struct lxc_container *c, const char *newname, int flags, const char *bdevtype) {
return c->clone(c, newname, NULL, flags, bdevtype, NULL, 0) != NULL;
}

15
lxc.go
View File

@@ -44,12 +44,27 @@ const (
LXC_NETWORK_KEY = "lxc.network"
)
const (
LXC_CLONE_KEEPNAME int = 1 << iota
LXC_CLONE_COPYHOOKS
LXC_CLONE_KEEPMACADDR
LXC_CLONE_SNAPSHOT
)
func NewContainer(name string) *Container {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return &Container{container: C.lxc_container_new(cname, nil)}
}
func GetContainer(lxc *Container) bool {
return C.lxc_container_get(lxc.container) == 1
}
func PutContainer(lxc *Container) bool {
return C.lxc_container_put(lxc.container) == 1
}
// Returns LXC version
func Version() string {
return C.GoString(C.lxc_get_version())

2
lxc.h
View File

@@ -44,3 +44,5 @@ extern const char* lxc_container_get_config_path(struct lxc_container *);
extern const char* lxc_container_state(struct lxc_container *);
extern pid_t lxc_container_init_pid(struct lxc_container *);
extern void lxc_container_want_daemonize(struct lxc_container *);
extern bool lxc_container_clone(struct lxc_container *, const char *, int, const char *);

View File

@@ -38,6 +38,21 @@ const (
CONFIG_FILE_NAME = "/var/lib/lxc/rubik/config"
)
func TestClone(t *testing.T) {
z := NewContainer("caglar")
z.Destroy()
PutContainer(z)
z = NewContainer("caglar-overlayfs")
z.Destroy()
PutContainer(z)
z = NewContainer("bleach")
t.Logf("Clone %v\n", z.Clone("caglar", DIRECTORY))
t.Logf("Clone %v\n", z.Clone("caglar-overlayfs", OVERLAYFS))
PutContainer(z)
}
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}