From e4a4af4bfed55871b80d67d8836f93b119b04a59 Mon Sep 17 00:00:00 2001 From: Saied Kazemi Date: Tue, 23 Sep 2014 14:04:55 -0700 Subject: [PATCH 1/2] Add RootFs field to configuration options in libcontainer's Config Since currently the container.json file does not include the pathname to a container's root filesystem, we need to parse /proc/mounts which is slow and error-prone. This patch addresses this issue by adding a new RootFs field. Signed-off-by: Saied Kazemi --- config.go | 3 +++ namespaces/exec.go | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 8fe95c24..1fb377dc 100644 --- a/config.go +++ b/config.go @@ -15,6 +15,9 @@ type Config struct { // Mount specific options. MountConfig *MountConfig `json:"mount_config,omitempty"` + // Pathname to container's root filesystem + RootFs string `json:"root_fs,omitempty"` + // Hostname optionally sets the container's hostname if provided Hostname string `json:"hostname,omitempty"` diff --git a/namespaces/exec.go b/namespaces/exec.go index 382abfbc..e07e1a95 100644 --- a/namespaces/exec.go +++ b/namespaces/exec.go @@ -21,7 +21,7 @@ import ( // Move this to libcontainer package. // Exec performs setup outside of a namespace so that a container can be // executed. Exec is a high level function for working with container namespaces. -func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Writer, console string, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) { +func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Writer, console, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) { var ( err error ) @@ -34,7 +34,7 @@ func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Wri } defer syncPipe.Close() - command := createCommand(container, console, rootfs, dataPath, os.Args[0], syncPipe.Child(), args) + command := createCommand(container, console, container.RootFs, dataPath, os.Args[0], syncPipe.Child(), args) // Note: these are only used in non-tty mode // if there is a tty for the container it will be opened within the namespace and the // fds will be duped to stdin, stdiout, and stderr From ae81ea069f58a443f477d15c69d37bfee16cfbab Mon Sep 17 00:00:00 2001 From: Saied Kazemi Date: Tue, 23 Sep 2014 14:04:55 -0700 Subject: [PATCH 2/2] Add RootFs field to configuration options in libcontainer's Config Since currently the container.json file does not include the pathname to a container's root filesystem, we need to parse /proc/mounts which is slow and error-prone. This patch addresses this issue by adding a new RootFs field. Signed-off-by: Saied Kazemi --- config.go | 3 +++ namespaces/create.go | 2 +- namespaces/exec.go | 8 ++++---- nsinit/exec.go | 6 +++--- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index 8fe95c24..1fb377dc 100644 --- a/config.go +++ b/config.go @@ -15,6 +15,9 @@ type Config struct { // Mount specific options. MountConfig *MountConfig `json:"mount_config,omitempty"` + // Pathname to container's root filesystem + RootFs string `json:"root_fs,omitempty"` + // Hostname optionally sets the container's hostname if provided Hostname string `json:"hostname,omitempty"` diff --git a/namespaces/create.go b/namespaces/create.go index 15a844bc..b6418b6e 100644 --- a/namespaces/create.go +++ b/namespaces/create.go @@ -7,4 +7,4 @@ import ( "github.com/docker/libcontainer" ) -type CreateCommand func(container *libcontainer.Config, console, rootfs, dataPath, init string, childPipe *os.File, args []string) *exec.Cmd +type CreateCommand func(container *libcontainer.Config, console, dataPath, init string, childPipe *os.File, args []string) *exec.Cmd diff --git a/namespaces/exec.go b/namespaces/exec.go index 382abfbc..4440ccd0 100644 --- a/namespaces/exec.go +++ b/namespaces/exec.go @@ -21,7 +21,7 @@ import ( // Move this to libcontainer package. // Exec performs setup outside of a namespace so that a container can be // executed. Exec is a high level function for working with container namespaces. -func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Writer, console string, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) { +func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Writer, console, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) { var ( err error ) @@ -34,7 +34,7 @@ func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Wri } defer syncPipe.Close() - command := createCommand(container, console, rootfs, dataPath, os.Args[0], syncPipe.Child(), args) + command := createCommand(container, console, dataPath, os.Args[0], syncPipe.Child(), args) // Note: these are only used in non-tty mode // if there is a tty for the container it will be opened within the namespace and the // fds will be duped to stdin, stdiout, and stderr @@ -121,7 +121,7 @@ func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Wri // root: the path to the container json file and information // pipe: sync pipe to synchronize the parent and child processes // args: the arguments to pass to the container to run as the user's program -func DefaultCreateCommand(container *libcontainer.Config, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd { +func DefaultCreateCommand(container *libcontainer.Config, console, dataPath, init string, pipe *os.File, args []string) *exec.Cmd { // get our binary name from arg0 so we can always reexec ourself env := []string{ "console=" + console, @@ -141,7 +141,7 @@ func DefaultCreateCommand(container *libcontainer.Config, console, rootfs, dataP command := exec.Command(init, append([]string{"init", "--"}, args...)...) // make sure the process is executed inside the context of the rootfs - command.Dir = rootfs + command.Dir = container.RootFs command.Env = append(os.Environ(), env...) if command.SysProcAttr == nil { diff --git a/nsinit/exec.go b/nsinit/exec.go index c46b1917..6fc553b8 100644 --- a/nsinit/exec.go +++ b/nsinit/exec.go @@ -135,8 +135,8 @@ func startContainer(container *libcontainer.Config, dataPath string, args []stri signal.Notify(sigc) - createCommand := func(container *libcontainer.Config, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd { - cmd = namespaces.DefaultCreateCommand(container, console, rootfs, dataPath, init, pipe, args) + createCommand := func(container *libcontainer.Config, console, dataPath, init string, pipe *os.File, args []string) *exec.Cmd { + cmd = namespaces.DefaultCreateCommand(container, console, dataPath, init, pipe, args) if logPath != "" { cmd.Env = append(cmd.Env, fmt.Sprintf("log=%s", logPath)) } @@ -189,7 +189,7 @@ func startContainer(container *libcontainer.Config, dataPath string, args []stri }() } - return namespaces.Exec(container, stdin, stdout, stderr, console, "", dataPath, args, createCommand, startCallback) + return namespaces.Exec(container, stdin, stdout, stderr, console, dataPath, args, createCommand, startCallback) } func resizeTty(master *os.File) {