2014-02-20 00:50:10 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-02-21 02:27:42 +00:00
|
|
|
package nsinit
|
2014-02-19 01:52:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/capabilities"
|
2014-02-19 23:33:44 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
2014-02-19 07:13:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-24 21:52:56 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/user"
|
2014-02-21 22:49:55 +00:00
|
|
|
"log"
|
2014-02-19 01:52:06 +00:00
|
|
|
"os"
|
2014-02-22 00:17:18 +00:00
|
|
|
"os/exec"
|
2014-02-19 01:52:06 +00:00
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-02-21 02:27:42 +00:00
|
|
|
// Init is the init process that first runs inside a new namespace to setup mounts, users, networking,
|
|
|
|
// and other options required for the new container.
|
2014-02-22 06:58:30 +00:00
|
|
|
func Init(container *libcontainer.Container, uncleanRootfs, console string, syncPipe *SyncPipe, args []string) error {
|
2014-02-21 22:49:55 +00:00
|
|
|
rootfs, err := resolveRootfs(uncleanRootfs)
|
2014-02-19 22:33:25 +00:00
|
|
|
if err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return err
|
2014-02-19 22:33:25 +00:00
|
|
|
}
|
2014-02-21 22:49:55 +00:00
|
|
|
log.Printf("initializing namespace at %s", rootfs)
|
2014-02-19 22:33:25 +00:00
|
|
|
|
2014-02-20 22:12:08 +00:00
|
|
|
// We always read this as it is a way to sync with the parent as well
|
2014-02-22 06:58:30 +00:00
|
|
|
context, err := syncPipe.ReadFromParent()
|
2014-02-20 22:12:08 +00:00
|
|
|
if err != nil {
|
2014-02-22 06:58:30 +00:00
|
|
|
syncPipe.Close()
|
2014-02-20 22:12:08 +00:00
|
|
|
return err
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
2014-02-22 06:58:30 +00:00
|
|
|
syncPipe.Close()
|
|
|
|
log.Printf("received context from parent %v", context)
|
|
|
|
|
2014-02-21 02:05:40 +00:00
|
|
|
if console != "" {
|
2014-02-21 22:49:55 +00:00
|
|
|
log.Printf("setting up console for %s", console)
|
2014-02-21 02:05:40 +00:00
|
|
|
// close pipes so that we can replace it with the pty
|
2014-02-22 06:58:30 +00:00
|
|
|
closeStdPipes()
|
2014-02-21 02:05:40 +00:00
|
|
|
slave, err := openTerminal(console, syscall.O_RDWR)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open terminal %s", err)
|
|
|
|
}
|
|
|
|
if err := dupSlave(slave); err != nil {
|
|
|
|
return fmt.Errorf("dup2 slave %s", err)
|
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if _, err := system.Setsid(); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return fmt.Errorf("setsid %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
2014-02-21 02:05:40 +00:00
|
|
|
if console != "" {
|
|
|
|
if err := system.Setctty(); err != nil {
|
|
|
|
return fmt.Errorf("setctty %s", err)
|
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.ParentDeathSignal(); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return fmt.Errorf("parent deth signal %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
2014-02-19 22:33:25 +00:00
|
|
|
if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return fmt.Errorf("setup mount namespace %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
2014-02-22 06:20:15 +00:00
|
|
|
if err := setupNetwork(container.Network, context); err != nil {
|
2014-02-20 03:14:31 +00:00
|
|
|
return fmt.Errorf("setup networking %s", err)
|
2014-02-19 22:33:25 +00:00
|
|
|
}
|
2014-02-20 03:14:31 +00:00
|
|
|
if err := system.Sethostname(container.Hostname); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return fmt.Errorf("sethostname %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
2014-02-21 22:49:55 +00:00
|
|
|
log.Printf("dropping capabilities")
|
2014-02-19 01:52:06 +00:00
|
|
|
if err := capabilities.DropCapabilities(container); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return fmt.Errorf("drop capabilities %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
2014-02-21 22:49:55 +00:00
|
|
|
log.Printf("setting user in namespace")
|
2014-02-19 01:52:06 +00:00
|
|
|
if err := setupUser(container); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return fmt.Errorf("setup user %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
if container.WorkingDir != "" {
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Chdir(container.WorkingDir); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return fmt.Errorf("chdir to %s %s", container.WorkingDir, err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-22 06:58:30 +00:00
|
|
|
return execArgs(args, container.Env)
|
|
|
|
}
|
|
|
|
|
|
|
|
func execArgs(args []string, env []string) error {
|
2014-02-22 00:17:18 +00:00
|
|
|
name, err := exec.LookPath(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("execing %s goodbye", name)
|
2014-02-22 06:58:30 +00:00
|
|
|
if err := system.Exec(name, args[0:], env); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return fmt.Errorf("exec %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2014-02-22 06:58:30 +00:00
|
|
|
func closeStdPipes() {
|
|
|
|
os.Stdin.Close()
|
|
|
|
os.Stdout.Close()
|
|
|
|
os.Stderr.Close()
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// resolveRootfs ensures that the current working directory is
|
|
|
|
// not a symlink and returns the absolute path to the rootfs
|
2014-02-21 22:49:55 +00:00
|
|
|
func resolveRootfs(uncleanRootfs string) (string, error) {
|
|
|
|
rootfs, err := filepath.Abs(uncleanRootfs)
|
2014-02-19 01:52:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.EvalSymlinks(rootfs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupUser(container *libcontainer.Container) error {
|
2014-02-24 21:52:56 +00:00
|
|
|
if container.User != "" {
|
|
|
|
uid, gid, suppGids, err := user.GetUserGroupSupplementary(container.User, syscall.Getuid(), syscall.Getgid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := system.Setgroups(suppGids); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := system.Setgid(gid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := system.Setuid(uid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := system.Setgroups(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := system.Setresgid(0, 0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := system.Setresuid(0, 0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// dupSlave dup2 the pty slave's fd into stdout and stdin and ensures that
|
|
|
|
// the slave's fd is 0, or stdin
|
2014-02-19 01:52:06 +00:00
|
|
|
func dupSlave(slave *os.File) error {
|
|
|
|
if slave.Fd() != 0 {
|
|
|
|
return fmt.Errorf("slave fd not 0 %d", slave.Fd())
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Dup2(slave.Fd(), 1); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Dup2(slave.Fd(), 2); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// openTerminal is a clone of os.OpenFile without the O_CLOEXEC
|
|
|
|
// used to open the pty slave inside the container namespace
|
2014-02-19 01:52:06 +00:00
|
|
|
func openTerminal(name string, flag int) (*os.File, error) {
|
|
|
|
r, e := syscall.Open(name, flag, 0)
|
|
|
|
if e != nil {
|
|
|
|
return nil, &os.PathError{"open", name, e}
|
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(r), name), nil
|
|
|
|
}
|
2014-02-19 18:44:29 +00:00
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// setupVethNetwork uses the Network config if it is not nil to initialize
|
|
|
|
// the new veth interface inside the container for use by changing the name to eth0
|
|
|
|
// setting the MTU and IP address along with the default gateway
|
2014-02-22 06:20:15 +00:00
|
|
|
func setupNetwork(config *libcontainer.Network, context libcontainer.Context) error {
|
2014-02-20 03:14:31 +00:00
|
|
|
if config != nil {
|
2014-02-22 06:20:15 +00:00
|
|
|
strategy, err := network.GetStrategy(config.Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-02-19 22:55:34 +00:00
|
|
|
}
|
2014-02-22 06:20:15 +00:00
|
|
|
return strategy.Initialize(config, context)
|
2014-02-19 18:44:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|