diff --git a/.gitignore b/.gitignore index 7bae99d..604bbc4 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ examples/create examples/create_snapshot examples/destroy examples/destroy_snapshots +examples/device_add_remove examples/freeze examples/interfaces examples/ipaddress diff --git a/container.go b/container.go index 9ddffcf..f8e0228 100644 --- a/container.go +++ b/container.go @@ -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 +} diff --git a/examples/device_add_remove.go b/examples/device_add_remove.go new file mode 100644 index 0000000..58eed3e --- /dev/null +++ b/examples/device_add_remove.go @@ -0,0 +1,59 @@ +/* + * device_add_remove.go + * + * Copyright © 2013, S.Çağlar Onur + * + * Authors: + * S.Çağlar Onur + * + * 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()) + } +} diff --git a/lxc.c b/lxc.c index a29c063..9062ba5 100644 --- a/lxc.c +++ b/lxc.c @@ -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); +} diff --git a/lxc.h b/lxc.h index 208f5c1..f9154c7 100644 --- a/lxc.h +++ b/lxc.h @@ -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 *);