[lxc-devel] [go-lxc/v2] container: add StartExecute()

brauner on Github lxc-bot at linuxcontainers.org
Sat Nov 11 11:07:27 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 425 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171111/253a4e5c/attachment.bin>
-------------- next part --------------
From 51ead75526160bea6c4528d1048965d8410af593 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 11 Nov 2017 12:05:16 +0100
Subject: [PATCH 1/2] container: add StartExecute()

This is an API-only version of Execute().

Closes #83.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 container.go | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/container.go b/container.go
index 8e9ba73..dafe840 100644
--- a/container.go
+++ b/container.go
@@ -490,6 +490,32 @@ func (c *Container) StartWithArgs(args []string) error {
 	return nil
 }
 
+// StartExecute starts a container. It runs a minimal init as PID 1 and the
+// requested program as the second process.
+func (c *Container) StartExecute(args []string) error {
+	c.mu.Lock()
+	defer c.mu.Unlock()
+
+	if err := c.makeSure(isDefined); err != nil {
+		// Deal with LXC 2.1's need of a defined container
+		if VersionAtLeast(2, 1, 0) {
+			os.MkdirAll(filepath.Join(c.configPath(), c.name()), 0700)
+			c.saveConfigFile(filepath.Join(c.configPath(), c.name(), "config"))
+			defer os.RemoveAll(filepath.Join(c.configPath(), c.name()))
+		}
+	}
+
+	if err := c.makeSure(isNotRunning); err != nil {
+		return err
+	}
+
+	if !bool(C.go_lxc_start(c.container, 1, makeNullTerminatedArgs(args))) {
+		return ErrStartFailed
+	}
+
+	return nil
+}
+
 // Execute executes the given command in a temporary container.
 func (c *Container) Execute(args ...string) ([]byte, error) {
 	c.mu.Lock()

From 004016bb61b9c6dcd5b6cb5d28812adc354633bb Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 11 Nov 2017 12:05:52 +0100
Subject: [PATCH 2/2] test: add StartExecute() test

Closes #83.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 lxc_test.go | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/lxc_test.go b/lxc_test.go
index 3e8d3b5..dab1406 100644
--- a/lxc_test.go
+++ b/lxc_test.go
@@ -135,6 +135,21 @@ func TestExecute(t *testing.T) {
 	}
 }
 
+func TestStartExecute(t *testing.T) {
+	if unprivileged() {
+		t.Skip("skipping test in unprivileged mode.")
+	}
+
+	c, err := NewContainer(ContainerName)
+	if err != nil {
+		t.Errorf(err.Error())
+	}
+
+	if err := c.StartExecute([]string{"/bin/true"}); err != nil {
+		t.Errorf(err.Error())
+	}
+}
+
 func TestSetVerbosity(t *testing.T) {
 	c, err := NewContainer(ContainerName)
 	if err != nil {


More information about the lxc-devel mailing list