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-05-01 00:55:15 +00:00
|
|
|
"strings"
|
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-05-01 01:00:42 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/security/restrict"
|
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-05-01 00:55:15 +00:00
|
|
|
func 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-05-01 00:55:15 +00:00
|
|
|
// clear the current processes env and replace it with the environment
|
|
|
|
// defined on the container
|
|
|
|
if err := LoadContainerEnvironment(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2014-04-11 15:15:28 +00:00
|
|
|
if 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-05-01 00:18:07 +00:00
|
|
|
|
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-05-05 18:12:25 +00:00
|
|
|
if container.Hostname != "" {
|
|
|
|
if err := system.Sethostname(container.Hostname); err != nil {
|
|
|
|
return fmt.Errorf("sethostname %s", err)
|
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 20:49:16 +00:00
|
|
|
runtime.LockOSThread()
|
2014-04-17 23:47:27 +00:00
|
|
|
|
2014-05-02 02:09:12 +00:00
|
|
|
if err := apparmor.ApplyProfile(container.Context["apparmor_profile"]); err != nil {
|
|
|
|
return fmt.Errorf("set apparmor profile %s: %s", container.Context["apparmor_profile"], err)
|
|
|
|
}
|
|
|
|
if err := label.SetProcessLabel(container.Context["process_label"]); err != nil {
|
|
|
|
return fmt.Errorf("set process label %s", err)
|
|
|
|
}
|
2014-05-01 17:08:18 +00:00
|
|
|
if container.Context["restrictions"] != "" {
|
2014-05-02 18:14:24 +00:00
|
|
|
if err := restrict.Restrict("proc", "sys"); err != nil {
|
2014-05-01 01:00:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := FinalizeNamespace(container); err != nil {
|
|
|
|
return fmt.Errorf("finalize namespace %s", err)
|
|
|
|
}
|
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-04-30 22:27:59 +00:00
|
|
|
// SetupUser changes the groups, gid, and uid for the user inside the container
|
|
|
|
func SetupUser(u string) error {
|
|
|
|
uid, gid, suppGids, err := user.GetUserGroupSupplementary(u, syscall.Getuid(), syscall.Getgid())
|
2014-04-28 22:46:03 +00:00
|
|
|
if err != nil {
|
2014-04-30 22:27:59 +00:00
|
|
|
return fmt.Errorf("get supplementary groups %s", err)
|
2014-04-28 22:46:03 +00:00
|
|
|
}
|
|
|
|
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-05-01 00:18:07 +00:00
|
|
|
// FinalizeNamespace drops the caps, sets the correct user
|
2014-04-29 05:22:54 +00:00
|
|
|
// and working dir, and closes any leaky file descriptors
|
|
|
|
// before execing the command inside the namespace
|
2014-05-01 00:18:07 +00:00
|
|
|
func FinalizeNamespace(container *libcontainer.Container) error {
|
2014-03-03 20:15:47 +00:00
|
|
|
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-04-30 22:27:59 +00:00
|
|
|
if err := SetupUser(container.User); err != nil {
|
2014-03-03 20:15:47 +00:00
|
|
|
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
|
|
|
|
}
|
2014-05-01 00:55:15 +00:00
|
|
|
|
|
|
|
func LoadContainerEnvironment(container *libcontainer.Container) error {
|
|
|
|
os.Clearenv()
|
|
|
|
for _, pair := range container.Env {
|
|
|
|
p := strings.SplitN(pair, "=", 2)
|
2014-05-16 04:36:15 +00:00
|
|
|
if len(p) < 2 {
|
|
|
|
return fmt.Errorf("invalid environment '%v'", pair)
|
|
|
|
}
|
2014-05-01 00:55:15 +00:00
|
|
|
if err := os.Setenv(p[0], p[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|