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

introduce AddDeviceNode/RemoveDeviceNode methods

This commit is contained in:
S.Çağlar Onur
2013-11-13 16:10:05 -05:00
parent eefb977d26
commit 9e443b86e0
5 changed files with 127 additions and 0 deletions

1
.gitignore vendored
View File

@@ -11,6 +11,7 @@ examples/create
examples/create_snapshot
examples/destroy
examples/destroy_snapshots
examples/device_add_remove
examples/freeze
examples/interfaces
examples/ipaddress

View File

@@ -977,3 +977,60 @@ func (lxc *Container) SetLogLevel(level LogLevel) error {
}
return nil
}
// AddDeviceNode adds the device node into given container
func (lxc *Container) AddDeviceNode(source string, destination ...string) error {
if err := lxc.ensureDefinedAndRunning(); err != nil {
return err
}
lxc.Lock()
defer lxc.Unlock()
csource := C.CString(source)
defer C.free(unsafe.Pointer(csource))
if destination != nil && len(destination) == 1 {
cdestination := C.CString(destination[0])
defer C.free(unsafe.Pointer(cdestination))
if !bool(C.lxc_container_add_device_node(lxc.container, csource, cdestination)) {
return fmt.Errorf("adding device %s to container %q failed", source, C.GoString(lxc.container.name))
}
return nil
}
if !bool(C.lxc_container_add_device_node(lxc.container, csource, nil)) {
return fmt.Errorf("adding device %s to container %q failed", source, C.GoString(lxc.container.name))
}
return nil
}
// RemoveDeviceNode removes the device node from given container
func (lxc *Container) RemoveDeviceNode(source string, destination ...string) error {
if err := lxc.ensureDefinedAndRunning(); err != nil {
return err
}
lxc.Lock()
defer lxc.Unlock()
csource := C.CString(source)
defer C.free(unsafe.Pointer(csource))
if destination != nil && len(destination) == 1 {
cdestination := C.CString(destination[0])
defer C.free(unsafe.Pointer(cdestination))
if !bool(C.lxc_container_remove_device_node(lxc.container, csource, cdestination)) {
return fmt.Errorf("adding device %s to container %q failed", source, C.GoString(lxc.container.name))
}
return nil
}
if !bool(C.lxc_container_remove_device_node(lxc.container, csource, nil)) {
return fmt.Errorf("adding device %s to container %q failed", source, C.GoString(lxc.container.name))
}
return nil
}

View File

@@ -0,0 +1,59 @@
/*
* device_add_remove.go
*
* 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 main
import (
"flag"
"github.com/caglar10ur/lxc"
"log"
"time"
)
var (
lxcpath string
name string
)
func init() {
flag.StringVar(&lxcpath, "lxcpath", lxc.DefaultConfigPath(), "Use specified container path")
flag.StringVar(&name, "name", "rubik", "Name of the container")
flag.Parse()
}
func main() {
c, err := lxc.NewContainer(name, lxcpath)
if err != nil {
log.Fatalf("ERROR: %s\n", err.Error())
}
defer lxc.PutContainer(c)
if err := c.AddDeviceNode("/dev/network_latency"); err != nil {
log.Fatalf("ERROR: %s\n", err.Error())
}
time.Sleep(10000 * time.Millisecond)
if err := c.RemoveDeviceNode("/dev/network_latency"); err != nil {
log.Fatalf("ERROR: %s\n", err.Error())
}
}

8
lxc.c
View File

@@ -245,3 +245,11 @@ bool lxc_container_snapshot_restore(struct lxc_container *c, char *snapname, cha
bool lxc_container_snapshot_destroy(struct lxc_container *c, char *snapname) {
return c->snapshot_destroy(c, snapname);
}
bool lxc_container_add_device_node(struct lxc_container *c, char *src_path, char *dest_path) {
return c->add_device_node(c, src_path, dest_path);
}
bool lxc_container_remove_device_node(struct lxc_container *c, char *src_path, char *dest_path) {
return c->remove_device_node(c, src_path, dest_path);
}

2
lxc.h
View File

@@ -43,3 +43,5 @@ extern int lxc_container_snapshot_list(struct lxc_container *, struct lxc_snapsh
extern int lxc_container_snapshot(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_add_device_node(struct lxc_container *, char *, char *);
extern bool lxc_container_remove_device_node(struct lxc_container *, char *, char *);