Use nsinit as app
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
d62cc1cc66
commit
c1f8606d50
7 changed files with 82 additions and 119 deletions
|
@ -2,18 +2,14 @@ package libcontainer
|
||||||
|
|
||||||
type Container struct {
|
type Container struct {
|
||||||
ID string `json:"id,omitempty"`
|
ID string `json:"id,omitempty"`
|
||||||
NsPid int `json:"namespace_pid,omitempty"`
|
|
||||||
Command *Command `json:"command,omitempty"`
|
Command *Command `json:"command,omitempty"`
|
||||||
RootFs string `json:"rootfs,omitempty"`
|
|
||||||
ReadonlyFs bool `json:"readonly_fs,omitempty"`
|
ReadonlyFs bool `json:"readonly_fs,omitempty"`
|
||||||
NetNsFd uintptr `json:"network_namespace_fd,omitempty"`
|
|
||||||
User string `json:"user,omitempty"`
|
User string `json:"user,omitempty"`
|
||||||
WorkingDir string `json:"working_dir,omitempty"`
|
WorkingDir string `json:"working_dir,omitempty"`
|
||||||
Namespaces Namespaces `json:"namespaces,omitempty"`
|
Namespaces Namespaces `json:"namespaces,omitempty"`
|
||||||
Capabilities Capabilities `json:"capabilities,omitempty"`
|
Capabilities Capabilities `json:"capabilities,omitempty"`
|
||||||
Master uintptr `json:"master"`
|
LogFile string `json:"log_file,omitempty"`
|
||||||
Console string `json:"console"`
|
Network *Network `json:"network,omitempty"`
|
||||||
LogFile string `json:"log_file"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
|
@ -22,9 +18,9 @@ type Command struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Network struct {
|
type Network struct {
|
||||||
TempVethName string `json:"temp_veth,omitempty"`
|
|
||||||
IP string `json:"ip,omitempty"`
|
IP string `json:"ip,omitempty"`
|
||||||
Gateway string `json:"gateway,omitempty"`
|
Gateway string `json:"gateway,omitempty"`
|
||||||
Bridge string `json:"bridge,omitempty"`
|
Bridge string `json:"bridge,omitempty"`
|
||||||
Mtu int `json:"mtu,omitempty"`
|
Mtu int `json:"mtu,omitempty"`
|
||||||
|
TempVethName string `json:"temp_veth,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,17 @@
|
||||||
/*
|
|
||||||
Higher level convience functions for setting up a container
|
|
||||||
*/
|
|
||||||
|
|
||||||
package namespaces
|
package namespaces
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"github.com/dotcloud/docker/pkg/libcontainer"
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
||||||
"github.com/dotcloud/docker/pkg/system"
|
"github.com/dotcloud/docker/pkg/system"
|
||||||
|
"github.com/dotcloud/docker/pkg/term"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// ExecContainer will spawn new namespaces with the specified Container configuration
|
||||||
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.
|
// in the RootFs path and return the pid of the new containerized process.
|
||||||
//
|
//
|
||||||
// If an existing network namespace is specified the container
|
// If an existing network namespace is specified the container
|
||||||
|
@ -30,30 +20,19 @@ var (
|
||||||
// existing network namespace and the CLONE_NEWNET option in the container configuration will allow
|
// 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.
|
// the container to the the host's networking options and configuration.
|
||||||
func ExecContainer(container *libcontainer.Container) (pid int, err error) {
|
func ExecContainer(container *libcontainer.Container) (pid int, err error) {
|
||||||
// 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()
|
master, console, err := createMasterAndConsole()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
nsinit := filepath.Join(container.RootFs, ".nsinit")
|
|
||||||
|
|
||||||
// we need CLONE_VFORK so we can wait on the child
|
// we need CLONE_VFORK so we can wait on the child
|
||||||
flag := uintptr(getNamespaceFlags(container.Namespaces) | CLONE_VFORK)
|
flag := uintptr(getNamespaceFlags(container.Namespaces) | CLONE_VFORK)
|
||||||
|
|
||||||
command := exec.Command(nsinit, "-master", strconv.Itoa(int(master.Fd())), "-console", console, "init", "container.json")
|
command := exec.Command("nsinit", console)
|
||||||
// command.Stdin = os.Stdin
|
command.SysProcAttr = &syscall.SysProcAttr{
|
||||||
// command.Stdout = os.Stdout
|
Cloneflags: flag,
|
||||||
// command.Stderr = os.Stderr
|
}
|
||||||
command.SysProcAttr = &syscall.SysProcAttr{}
|
|
||||||
command.SysProcAttr.Cloneflags = flag
|
|
||||||
|
|
||||||
command.ExtraFiles = []*os.File{master}
|
|
||||||
|
|
||||||
println("vvvvvvvvv")
|
|
||||||
if err := command.Start(); err != nil {
|
if err := command.Start(); err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
|
@ -64,11 +43,18 @@ func ExecContainer(container *libcontainer.Container) (pid int, err error) {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if _, err := io.Copy(master, os.Stdin); err != nil {
|
if _, err := io.Copy(master, os.Stdin); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
term.SetRawTerminal(os.Stdin.Fd())
|
||||||
|
|
||||||
|
if err := command.Wait(); err != nil {
|
||||||
|
return pid, err
|
||||||
|
}
|
||||||
return pid, nil
|
return pid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
// +build linux,x86_64
|
|
||||||
package namespaces
|
|
||||||
|
|
||||||
// Via http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=7b21fddd087678a70ad64afc0f632e0f1071b092
|
|
||||||
const (
|
|
||||||
SYS_SETNS = 308
|
|
||||||
)
|
|
|
@ -40,5 +40,5 @@ func getNamespaceFlags(namespaces libcontainer.Namespaces) (flag int) {
|
||||||
for _, ns := range namespaces {
|
for _, ns := range namespaces {
|
||||||
flag |= namespaceMap[ns]
|
flag |= namespaceMap[ns]
|
||||||
}
|
}
|
||||||
return
|
return flag
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package nsinit
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/pkg/libcontainer"
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
||||||
"github.com/dotcloud/docker/pkg/libcontainer/capabilities"
|
"github.com/dotcloud/docker/pkg/libcontainer/capabilities"
|
||||||
|
@ -12,103 +13,112 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitNamespace should be run inside an existing namespace to setup
|
func loadContainer() (*libcontainer.Container, error) {
|
||||||
// common mounts, drop capabilities, and setup network interfaces
|
f, err := os.Open("container.json")
|
||||||
func InitNamespace(container *libcontainer.Container) error {
|
|
||||||
println("|||||||||||||")
|
|
||||||
if err := setLogFile(container); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
println(container.LogFile)
|
|
||||||
log.Printf("--------->")
|
|
||||||
rootfs, err := resolveRootfs(container)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
var container *libcontainer.Container
|
||||||
|
if err := json.NewDecoder(f).Decode(&container); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return container, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
container, err := loadContainer()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// any errors encoutered inside the namespace we should write
|
if os.Args[1] == "exec" {
|
||||||
// out to a log or a pipe to our parent and exit(1)
|
_, err := namespaces.ExecContainer(container)
|
||||||
// because writing to stderr will not work after we close
|
if err != nil {
|
||||||
if err := closeMasterAndStd(os.NewFile(container.Master, "/dev/ptmx")); err != nil {
|
log.Fatal(err)
|
||||||
log.Fatalf("close master and std %s", err)
|
}
|
||||||
return err
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
console := os.Args[1]
|
||||||
|
|
||||||
|
if err := setLogFile(container); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
slave, err := openTerminal(container.Console, syscall.O_RDWR)
|
rootfs, err := resolveRootfs()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// close pipes so that we can replace it with the pty
|
||||||
|
os.Stdin.Close()
|
||||||
|
os.Stdout.Close()
|
||||||
|
os.Stderr.Close()
|
||||||
|
|
||||||
|
slave, err := openTerminal(console, syscall.O_RDWR)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("open terminal %s", err)
|
log.Fatalf("open terminal %s", err)
|
||||||
return err
|
}
|
||||||
|
if slave.Fd() != 0 {
|
||||||
|
log.Fatalf("slave fd should be 0")
|
||||||
}
|
}
|
||||||
if err := dupSlave(slave); err != nil {
|
if err := dupSlave(slave); err != nil {
|
||||||
log.Fatalf("dup2 slave %s", err)
|
log.Fatalf("dup2 slave %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
if container.NetNsFd > 0 {
|
|
||||||
if err := joinExistingNamespace(container.NetNsFd, libcontainer.CLONE_NEWNET); err != nil {
|
|
||||||
log.Fatalf("join existing net namespace %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
if _, err := system.Setsid(); err != nil {
|
if _, err := system.Setsid(); err != nil {
|
||||||
log.Fatalf("setsid %s", err)
|
log.Fatalf("setsid %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if err := system.Setctty(); err != nil {
|
if err := system.Setctty(); err != nil {
|
||||||
log.Fatalf("setctty %s", err)
|
log.Fatalf("setctty %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if err := system.ParentDeathSignal(); err != nil {
|
if err := system.ParentDeathSignal(); err != nil {
|
||||||
log.Fatalf("parent deth signal %s", err)
|
log.Fatalf("parent deth signal %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if err := namespaces.SetupNewMountNamespace(rootfs, container.Console, container.ReadonlyFs); err != nil {
|
|
||||||
|
if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
|
||||||
log.Fatalf("setup mount namespace %s", err)
|
log.Fatalf("setup mount namespace %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if container.Network != nil {
|
||||||
|
if err := setupNetworking(container); err != nil {
|
||||||
|
log.Fatalf("setup networking %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := system.Sethostname(container.ID); err != nil {
|
if err := system.Sethostname(container.ID); err != nil {
|
||||||
log.Fatalf("sethostname %s", err)
|
log.Fatalf("sethostname %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if err := capabilities.DropCapabilities(container); err != nil {
|
if err := capabilities.DropCapabilities(container); err != nil {
|
||||||
log.Fatalf("drop capabilities %s", err)
|
log.Fatalf("drop capabilities %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if err := setupUser(container); err != nil {
|
if err := setupUser(container); err != nil {
|
||||||
log.Fatalf("setup user %s", err)
|
log.Fatalf("setup user %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if container.WorkingDir != "" {
|
if container.WorkingDir != "" {
|
||||||
if err := system.Chdir(container.WorkingDir); err != nil {
|
if err := system.Chdir(container.WorkingDir); err != nil {
|
||||||
log.Fatalf("chdir to %s %s", container.WorkingDir, err)
|
log.Fatalf("chdir to %s %s", container.WorkingDir, err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := system.Exec(container.Command.Args[0], container.Command.Args[0:], container.Command.Env); err != nil {
|
if err := system.Exec(container.Command.Args[0], container.Command.Args[0:], container.Command.Env); err != nil {
|
||||||
log.Fatalf("exec %s", err)
|
log.Fatalf("exec %s", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveRootfs(container *libcontainer.Container) (string, error) {
|
func resolveRootfs() (string, error) {
|
||||||
rootfs, err := filepath.Abs(container.RootFs)
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
rootfs, err := filepath.Abs(cwd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return filepath.EvalSymlinks(rootfs)
|
return filepath.EvalSymlinks(rootfs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeMasterAndStd(master *os.File) error {
|
|
||||||
master.Close()
|
|
||||||
os.Stdin.Close()
|
|
||||||
os.Stdout.Close()
|
|
||||||
os.Stderr.Close()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupUser(container *libcontainer.Container) error {
|
func setupUser(container *libcontainer.Container) error {
|
||||||
// TODO: honor user passed on container
|
// TODO: honor user passed on container
|
||||||
if err := system.Setgroups(nil); err != nil {
|
if err := system.Setgroups(nil); err != nil {
|
||||||
|
@ -154,3 +164,7 @@ func setLogFile(container *libcontainer.Container) error {
|
||||||
log.SetOutput(f)
|
log.SetOutput(f)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupNetworking(conatiner *libcontainer.Container) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package namespaces
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -14,7 +14,7 @@ var (
|
||||||
defaults = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
|
defaults = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupNewMountNamespace(rootfs, console string, readonly bool) error {
|
func setupNewMountNamespace(rootfs, console string, readonly bool) error {
|
||||||
if err := system.Mount("", "/", "", syscall.MS_SLAVE|syscall.MS_REC, ""); err != nil {
|
if err := system.Mount("", "/", "", syscall.MS_SLAVE|syscall.MS_REC, ""); err != nil {
|
||||||
return fmt.Errorf("mounting / as slave %s", err)
|
return fmt.Errorf("mounting / as slave %s", err)
|
||||||
}
|
}
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func addEnvIfNotSet(container *libcontainer.Container, key, value string) {
|
func addEnvIfNotSet(container *libcontainer.Container, key, value string) {
|
||||||
|
@ -26,31 +25,6 @@ func addEnvIfNotSet(container *libcontainer.Container, key, value string) {
|
||||||
container.Command.Env = append(container.Command.Env, jv)
|
container.Command.Env = append(container.Command.Env, jv)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getNsFds inspects the container's namespace configuration and opens the fds to
|
|
||||||
// each of the namespaces.
|
|
||||||
func getNsFds(container *libcontainer.Container) ([]uintptr, error) {
|
|
||||||
var (
|
|
||||||
namespaces = []string{}
|
|
||||||
fds = []uintptr{}
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, ns := range container.Namespaces {
|
|
||||||
namespaces = append(namespaces, namespaceFileMap[ns])
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, ns := range namespaces {
|
|
||||||
fd, err := getNsFd(container.NsPid, ns)
|
|
||||||
if err != nil {
|
|
||||||
for _, fd = range fds {
|
|
||||||
syscall.Close(int(fd))
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
fds = append(fds, fd)
|
|
||||||
}
|
|
||||||
return fds, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// getNsFd returns the fd for a specific pid and namespace option
|
// getNsFd returns the fd for a specific pid and namespace option
|
||||||
func getNsFd(pid int, ns string) (uintptr, error) {
|
func getNsFd(pid int, ns string) (uintptr, error) {
|
||||||
nspath := filepath.Join("/proc", strconv.Itoa(pid), "ns", ns)
|
nspath := filepath.Join("/proc", strconv.Itoa(pid), "ns", ns)
|
||||||
|
|
Loading…
Reference in a new issue