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"
|
2014-03-05 19:59:31 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/apparmor"
|
2014-02-19 01:52:06 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/capabilities"
|
2014-02-19 23:33:44 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
2014-02-25 05:11:52 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/utils"
|
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-19 01:52:06 +00:00
|
|
|
"os"
|
|
|
|
"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-25 02:38:24 +00:00
|
|
|
func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, console string, syncPipe *SyncPipe, args []string) error {
|
2014-02-25 05:11:52 +00:00
|
|
|
rootfs, err := utils.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-20 22:12:08 +00:00
|
|
|
// We always read this as it is a way to sync with the parent as well
|
2014-03-13 17:43:15 +00:00
|
|
|
ns.logger.Printf("reading from sync pipe fd %d\n", syncPipe.child.Fd())
|
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-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("received context from parent")
|
2014-02-22 06:58:30 +00:00
|
|
|
syncPipe.Close()
|
|
|
|
|
2014-02-21 02:05:40 +00:00
|
|
|
if console != "" {
|
2014-03-13 17:35:16 +00:00
|
|
|
ns.logger.Printf("setting up %s as console\n", console)
|
2014-02-25 05:11:52 +00:00
|
|
|
slave, err := system.OpenTerminal(console, syscall.O_RDWR)
|
2014-02-21 02:05:40 +00:00
|
|
|
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-03-14 18:42:01 +00:00
|
|
|
// this is our best effort to let the process know that the parent has died and that it
|
|
|
|
// should it should act on it how it sees fit
|
|
|
|
if err := system.ParentDeathSignal(uintptr(syscall.SIGTERM)); err != nil {
|
2014-03-07 00:30:56 +00:00
|
|
|
return fmt.Errorf("parent death signal %s", err)
|
|
|
|
}
|
2014-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("setup mount namespace")
|
2014-03-03 15:15:29 +00:00
|
|
|
if err := setupNewMountNamespace(rootfs, container.Mounts, console, container.ReadonlyFs, container.NoPivotRoot); 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-26 22:19:39 +00:00
|
|
|
if err := setupNetwork(container, 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-03-03 20:15:47 +00:00
|
|
|
if err := finalizeNamespace(container); err != nil {
|
|
|
|
return fmt.Errorf("finalize namespace %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 17:35:16 +00:00
|
|
|
if profile := container.Context["apparmor_profile"]; profile != "" {
|
2014-03-13 17:43:15 +00:00
|
|
|
ns.logger.Printf("setting apparmor profile %s\n", profile)
|
2014-03-13 17:35:16 +00:00
|
|
|
if err := apparmor.ApplyProfile(os.Getpid(), profile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-06 19:10:58 +00:00
|
|
|
}
|
2014-03-13 17:35:16 +00:00
|
|
|
ns.logger.Printf("execing %s\n", args[0])
|
2014-03-05 19:59:31 +00:00
|
|
|
return system.Execv(args[0], args[0:], container.Env)
|
2014-02-22 06:58:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func setupUser(container *libcontainer.Container) error {
|
2014-02-25 05:11:52 +00:00
|
|
|
switch container.User {
|
|
|
|
case "root", "":
|
|
|
|
if err := system.Setgroups(nil); err != nil {
|
2014-02-24 21:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-25 05:11:52 +00:00
|
|
|
if err := system.Setresgid(0, 0, 0); err != nil {
|
2014-02-24 21:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-25 05:11:52 +00:00
|
|
|
if err := system.Setresuid(0, 0, 0); err != nil {
|
2014-02-24 21:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-25 05:11:52 +00:00
|
|
|
default:
|
|
|
|
uid, gid, suppGids, err := user.GetUserGroupSupplementary(container.User, syscall.Getuid(), syscall.Getgid())
|
|
|
|
if err != nil {
|
2014-02-24 21:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-25 05:11:52 +00:00
|
|
|
if err := system.Setgroups(suppGids); err != nil {
|
2014-02-24 21:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-25 05:11:52 +00:00
|
|
|
if err := system.Setgid(gid); err != nil {
|
2014-02-24 21:52:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-25 05:11:52 +00:00
|
|
|
if err := system.Setuid(uid); err != nil {
|
2014-02-24 21:52:56 +00:00
|
|
|
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 {
|
2014-03-05 19:59:31 +00:00
|
|
|
if err := system.Dup2(slave.Fd(), 0); err != nil {
|
|
|
|
return err
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
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
|
|
|
// 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-26 22:19:39 +00:00
|
|
|
func setupNetwork(container *libcontainer.Container, context libcontainer.Context) error {
|
|
|
|
for _, config := range container.Networks {
|
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
|
|
|
|
}
|
2014-03-03 20:15:47 +00:00
|
|
|
|
|
|
|
// finalizeNamespace drops the caps and sets the correct user
|
|
|
|
// and working dir before execing the command inside the namespace
|
|
|
|
func finalizeNamespace(container *libcontainer.Container) error {
|
|
|
|
if err := capabilities.DropCapabilities(container); err != nil {
|
|
|
|
return fmt.Errorf("drop capabilities %s", err)
|
|
|
|
}
|
|
|
|
if err := setupUser(container); err != nil {
|
|
|
|
return fmt.Errorf("setup user %s", err)
|
|
|
|
}
|
|
|
|
if container.WorkingDir != "" {
|
|
|
|
if err := system.Chdir(container.WorkingDir); err != nil {
|
|
|
|
return fmt.Errorf("chdir to %s %s", container.WorkingDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|