2014-02-22 03:29:21 -05:00
|
|
|
package nsinit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-02-25 18:19:13 -05:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-22 03:29:21 -05:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2014-02-25 00:11:52 -05:00
|
|
|
// CommandFactory takes the container's configuration and options passed by the
|
|
|
|
// parent processes and creates an *exec.Cmd that will be used to fork/exec the
|
|
|
|
// namespaced init process
|
2014-02-22 03:29:21 -05:00
|
|
|
type CommandFactory interface {
|
2014-03-06 08:10:32 -05:00
|
|
|
Create(container *libcontainer.Container, console string, syncFd *os.File, args []string) *exec.Cmd
|
2014-02-22 03:29:21 -05:00
|
|
|
}
|
|
|
|
|
2014-02-25 18:19:13 -05:00
|
|
|
type DefaultCommandFactory struct {
|
|
|
|
Root string
|
|
|
|
}
|
2014-02-22 03:29:21 -05:00
|
|
|
|
|
|
|
// Create will return an exec.Cmd with the Cloneflags set to the proper namespaces
|
|
|
|
// defined on the container's configuration and use the current binary as the init with the
|
|
|
|
// args provided
|
2014-03-06 08:10:32 -05:00
|
|
|
func (c *DefaultCommandFactory) Create(container *libcontainer.Container, console string, pipe *os.File, args []string) *exec.Cmd {
|
2014-02-25 18:19:13 -05:00
|
|
|
// get our binary name from arg0 so we can always reexec ourself
|
|
|
|
command := exec.Command(os.Args[0], append([]string{
|
2014-02-22 03:29:21 -05:00
|
|
|
"-console", console,
|
2014-03-06 08:10:32 -05:00
|
|
|
"-pipe", "3",
|
2014-02-25 18:19:13 -05:00
|
|
|
"-root", c.Root,
|
2014-02-22 03:29:21 -05:00
|
|
|
"init"}, args...)...)
|
|
|
|
|
2014-02-25 18:19:13 -05:00
|
|
|
system.SetCloneFlags(command, uintptr(GetNamespaceFlags(container.Namespaces)))
|
2014-02-22 03:29:21 -05:00
|
|
|
command.Env = container.Env
|
2014-03-06 08:10:32 -05:00
|
|
|
command.ExtraFiles = []*os.File{pipe}
|
2014-02-22 03:29:21 -05:00
|
|
|
return command
|
|
|
|
}
|
2014-02-25 18:19:13 -05:00
|
|
|
|
|
|
|
// GetNamespaceFlags parses the container's Namespaces options to set the correct
|
|
|
|
// flags on clone, unshare, and setns
|
|
|
|
func GetNamespaceFlags(namespaces libcontainer.Namespaces) (flag int) {
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
flag |= ns.Value
|
|
|
|
}
|
|
|
|
return flag
|
|
|
|
}
|