2014-02-19 00:56:11 +00:00
|
|
|
/*
|
|
|
|
Higher level convience functions for setting up a container
|
|
|
|
*/
|
|
|
|
|
|
|
|
package namespaces
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-02-19 07:13:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-19 00:56:11 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-02-19 01:52:06 +00:00
|
|
|
"os/exec"
|
2014-02-19 02:15:41 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-02-19 00:56:11 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrExistingNetworkNamespace = errors.New("specified both CLONE_NEWNET and an existing network namespace")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Exec will spawn new namespaces with the specified Container configuration
|
|
|
|
// in the RootFs path and return the pid of the new containerized process.
|
|
|
|
//
|
|
|
|
// If an existing network namespace is specified the container
|
|
|
|
// will join that namespace. If an existing network namespace is not specified but CLONE_NEWNET is,
|
|
|
|
// the container will be spawned with a new network namespace with no configuration. Omiting an
|
|
|
|
// existing network namespace and the CLONE_NEWNET option in the container configuration will allow
|
|
|
|
// the container to the the host's networking options and configuration.
|
2014-02-19 01:52:06 +00:00
|
|
|
func ExecContainer(container *libcontainer.Container) (pid int, err error) {
|
2014-02-19 00:56:11 +00:00
|
|
|
// a user cannot pass CLONE_NEWNET and an existing net namespace fd to join
|
|
|
|
if container.NetNsFd > 0 && container.Namespaces.Contains(libcontainer.CLONE_NEWNET) {
|
|
|
|
return -1, ErrExistingNetworkNamespace
|
|
|
|
}
|
|
|
|
|
|
|
|
master, console, err := createMasterAndConsole()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-19 02:15:41 +00:00
|
|
|
nsinit := filepath.Join(container.RootFs, ".nsinit")
|
2014-02-19 00:56:11 +00:00
|
|
|
|
|
|
|
// we need CLONE_VFORK so we can wait on the child
|
2014-02-19 01:52:06 +00:00
|
|
|
flag := uintptr(getNamespaceFlags(container.Namespaces) | CLONE_VFORK)
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 07:13:36 +00:00
|
|
|
command := exec.Command(nsinit, "-master", strconv.Itoa(int(master.Fd())), "-console", console, "init", "container.json")
|
|
|
|
// command.Stdin = os.Stdin
|
|
|
|
// command.Stdout = os.Stdout
|
|
|
|
// command.Stderr = os.Stderr
|
2014-02-19 01:52:06 +00:00
|
|
|
command.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
command.SysProcAttr.Cloneflags = flag
|
2014-02-19 20:47:01 +00:00
|
|
|
|
|
|
|
command.ExtraFiles = []*os.File{master}
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 07:13:36 +00:00
|
|
|
println("vvvvvvvvv")
|
2014-02-19 01:52:06 +00:00
|
|
|
if err := command.Start(); err != nil {
|
|
|
|
return -1, err
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
pid = command.Process.Pid
|
2014-02-19 00:56:11 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
if _, err := io.Copy(os.Stdout, master); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
if _, err := io.Copy(master, os.Stdin); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return pid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMasterAndConsole() (*os.File, string, error) {
|
|
|
|
master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2014-02-19 07:13:36 +00:00
|
|
|
console, err := system.Ptsname(master)
|
2014-02-19 00:56:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Unlockpt(master); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return master, console, nil
|
|
|
|
}
|