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"
|
2014-03-04 05:46:49 +00:00
|
|
|
"os"
|
2014-03-27 08:00:18 +00:00
|
|
|
"runtime"
|
2014-03-04 05:46:49 +00:00
|
|
|
"syscall"
|
|
|
|
|
2014-04-13 23:33:25 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/apparmor"
|
2014-03-18 20:49:16 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/label"
|
2014-02-19 01:52:06 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-04-11 15:15:28 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/console"
|
2014-04-11 15:06:56 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/mount"
|
2014-02-19 23:33:44 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
2014-04-11 15:28:56 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/security/capabilities"
|
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
|
|
|
)
|
|
|
|
|
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-04-11 15:15:28 +00:00
|
|
|
func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consolePath 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-04-11 15:15:28 +00:00
|
|
|
if consolePath != "" {
|
|
|
|
ns.logger.Printf("setting up %s as console\n", consolePath)
|
|
|
|
if err := console.OpenAndDup(consolePath); err != nil {
|
|
|
|
return err
|
2014-02-21 02:05:40 +00:00
|
|
|
}
|
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-04-11 15:15:28 +00:00
|
|
|
if consolePath != "" {
|
2014-02-21 02:05:40 +00:00
|
|
|
if err := system.Setctty(); err != nil {
|
|
|
|
return fmt.Errorf("setctty %s", err)
|
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
2014-03-04 05:46:49 +00:00
|
|
|
if err := setupNetwork(container, context); err != nil {
|
|
|
|
return fmt.Errorf("setup networking %s", err)
|
|
|
|
}
|
2014-04-01 14:03:29 +00:00
|
|
|
|
|
|
|
label.Init()
|
2014-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("setup mount namespace")
|
2014-04-11 15:15:28 +00:00
|
|
|
if err := mount.InitializeMountNamespace(rootfs, consolePath, container); 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-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-18 20:49:16 +00:00
|
|
|
runtime.LockOSThread()
|
|
|
|
if err := label.SetProcessLabel(container.Context["process_label"]); err != nil {
|
|
|
|
return fmt.Errorf("SetProcessLabel label %s", err)
|
|
|
|
}
|
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-04-28 22:46:03 +00:00
|
|
|
uid, gid, suppGids, err := user.GetUserGroupSupplementary(container.User, syscall.Getuid(), syscall.Getgid())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetUserGroupSupplementary %s", err)
|
|
|
|
}
|
|
|
|
if err := system.Setgroups(suppGids); err != nil {
|
|
|
|
return fmt.Errorf("setgroups %s", err)
|
|
|
|
}
|
|
|
|
if err := system.Setgid(gid); err != nil {
|
|
|
|
return fmt.Errorf("setgid %s", err)
|
|
|
|
}
|
|
|
|
if err := system.Setuid(uid); err != nil {
|
|
|
|
return fmt.Errorf("setuid %s", err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
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-03-16 00:01:31 +00:00
|
|
|
|
|
|
|
err1 := strategy.Initialize(config, context)
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2014-02-19 18:44:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-03-03 20:15:47 +00:00
|
|
|
|
2014-04-29 05:22:54 +00:00
|
|
|
// finalizeNamespace drops the caps, sets the correct user
|
|
|
|
// and working dir, and closes any leaky file descriptors
|
|
|
|
// before execing the command inside the namespace
|
2014-03-03 20:15:47 +00:00
|
|
|
func finalizeNamespace(container *libcontainer.Container) error {
|
|
|
|
if err := capabilities.DropCapabilities(container); err != nil {
|
|
|
|
return fmt.Errorf("drop capabilities %s", err)
|
|
|
|
}
|
2014-04-29 05:22:54 +00:00
|
|
|
if err := system.CloseFdsFrom(3); err != nil {
|
|
|
|
return fmt.Errorf("close open file descriptors %s", err)
|
|
|
|
}
|
2014-03-03 20:15:47 +00:00
|
|
|
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
|
|
|
|
}
|