2014-07-16 19:39:15 -04:00
|
|
|
// +build linux,cgo
|
2014-07-14 15:49:50 -04:00
|
|
|
|
2014-03-21 04:10:07 -04:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2015-03-05 12:55:14 -05:00
|
|
|
"errors"
|
2014-03-21 04:10:07 -04:00
|
|
|
"fmt"
|
2015-03-05 12:55:14 -05:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2014-04-01 03:07:42 -04:00
|
|
|
|
2014-07-24 18:25:29 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/apparmor"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/devices"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2014-03-21 04:10:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// createContainer populates and configures the container type with the
|
|
|
|
// data provided by the execdriver.Command
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) createContainer(c *execdriver.Command) (*configs.Config, error) {
|
2015-01-30 12:29:46 -05:00
|
|
|
container := execdriver.InitContainer(c)
|
2014-03-21 04:10:07 -04:00
|
|
|
|
2014-11-10 16:14:17 -05:00
|
|
|
if err := d.createIpc(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-25 15:10:53 -05:00
|
|
|
if err := d.createPid(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-05 18:32:36 -04:00
|
|
|
if err := d.createUTS(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-03-24 03:16:40 -04:00
|
|
|
if err := d.createNetwork(container, c); err != nil {
|
|
|
|
return nil, err
|
2014-03-21 04:10:07 -04:00
|
|
|
}
|
2014-06-23 19:43:43 -04:00
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
if c.ProcessConfig.Privileged {
|
2015-06-02 12:16:43 -04:00
|
|
|
if !container.Readonlyfs {
|
|
|
|
// clear readonly for /sys
|
|
|
|
for i := range container.Mounts {
|
|
|
|
if container.Mounts[i].Destination == "/sys" {
|
|
|
|
container.Mounts[i].Flags &= ^syscall.MS_RDONLY
|
|
|
|
}
|
2015-03-05 12:55:14 -05:00
|
|
|
}
|
2015-06-02 12:16:43 -04:00
|
|
|
container.ReadonlyPaths = nil
|
2015-03-05 12:55:14 -05:00
|
|
|
}
|
2015-06-02 12:16:43 -04:00
|
|
|
|
2015-07-14 03:00:41 -04:00
|
|
|
// clear readonly for cgroup
|
|
|
|
for i := range container.Mounts {
|
|
|
|
if container.Mounts[i].Device == "cgroup" {
|
|
|
|
container.Mounts[i].Flags &= ^syscall.MS_RDONLY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 17:17:21 -04:00
|
|
|
container.MaskPaths = nil
|
2014-03-24 03:16:40 -04:00
|
|
|
if err := d.setPrivileged(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-10 14:41:11 -04:00
|
|
|
} else {
|
2014-07-10 19:38:11 -04:00
|
|
|
if err := d.setCapabilities(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-24 03:16:40 -04:00
|
|
|
}
|
2014-06-23 19:43:43 -04:00
|
|
|
|
2015-06-17 14:39:17 -04:00
|
|
|
container.AdditionalGroups = c.GroupAdd
|
|
|
|
|
2014-09-29 19:34:45 -04:00
|
|
|
if c.AppArmorProfile != "" {
|
|
|
|
container.AppArmorProfile = c.AppArmorProfile
|
|
|
|
}
|
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
if err := execdriver.SetupCgroups(container, c); err != nil {
|
2014-03-24 03:16:40 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-23 19:43:43 -04:00
|
|
|
|
2015-06-02 12:16:43 -04:00
|
|
|
if container.Readonlyfs {
|
|
|
|
for i := range container.Mounts {
|
|
|
|
switch container.Mounts[i].Destination {
|
|
|
|
case "/proc", "/dev", "/dev/pts":
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
container.Mounts[i].Flags |= syscall.MS_RDONLY
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These paths must be remounted as r/o */
|
|
|
|
container.ReadonlyPaths = append(container.ReadonlyPaths, "/proc", "/dev")
|
|
|
|
}
|
|
|
|
|
2014-03-24 03:16:40 -04:00
|
|
|
if err := d.setupMounts(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-23 19:43:43 -04:00
|
|
|
|
2015-05-02 09:57:57 -04:00
|
|
|
d.setupLabels(container, c)
|
2015-02-11 14:21:38 -05:00
|
|
|
d.setupRlimits(container, c)
|
2015-03-05 12:55:14 -05:00
|
|
|
return container, nil
|
|
|
|
}
|
2015-02-11 14:21:38 -05:00
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
func generateIfaceName() (string, error) {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
name, err := utils.GenerateRandomName("veth", 7)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := net.InterfaceByName(name); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "no such") {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
2014-05-21 16:48:06 -04:00
|
|
|
}
|
2015-03-05 12:55:14 -05:00
|
|
|
return "", errors.New("Failed to find name for new interface")
|
2014-03-24 03:16:40 -04:00
|
|
|
}
|
2014-03-21 04:10:07 -04:00
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) createNetwork(container *configs.Config, c *execdriver.Command) error {
|
2015-05-24 11:26:56 -04:00
|
|
|
if c.Network == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-30 18:46:56 -04:00
|
|
|
if c.Network.ContainerID != "" {
|
2014-05-29 08:40:42 -04:00
|
|
|
d.Lock()
|
2014-05-21 16:48:06 -04:00
|
|
|
active := d.activeContainers[c.Network.ContainerID]
|
2014-05-29 08:40:42 -04:00
|
|
|
d.Unlock()
|
2014-06-23 19:43:43 -04:00
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
if active == nil {
|
2014-04-30 18:46:56 -04:00
|
|
|
return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
|
|
|
|
}
|
2014-05-21 16:48:06 -04:00
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
state, err := active.State()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container.Namespaces.Add(configs.NEWNET, state.NamespacePaths[configs.NEWNET])
|
2015-05-06 18:39:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Network.NamespacePath == "" {
|
|
|
|
return fmt.Errorf("network namespace path is empty")
|
2014-04-30 18:46:56 -04:00
|
|
|
}
|
2014-06-23 19:43:43 -04:00
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
container.Namespaces.Add(configs.NEWNET, c.Network.NamespacePath)
|
2014-03-24 03:16:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-03-21 04:10:07 -04:00
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) createIpc(container *configs.Config, c *execdriver.Command) error {
|
2014-11-10 16:14:17 -05:00
|
|
|
if c.Ipc.HostIpc {
|
2015-03-05 12:55:14 -05:00
|
|
|
container.Namespaces.Remove(configs.NEWIPC)
|
2014-11-10 16:14:17 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Ipc.ContainerID != "" {
|
|
|
|
d.Lock()
|
|
|
|
active := d.activeContainers[c.Ipc.ContainerID]
|
|
|
|
d.Unlock()
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
if active == nil {
|
2014-11-10 16:14:17 -05:00
|
|
|
return fmt.Errorf("%s is not a valid running container to join", c.Ipc.ContainerID)
|
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
state, err := active.State()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.Namespaces.Add(configs.NEWIPC, state.NamespacePaths[configs.NEWIPC])
|
2014-11-10 16:14:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) createPid(container *configs.Config, c *execdriver.Command) error {
|
2014-11-25 15:10:53 -05:00
|
|
|
if c.Pid.HostPid {
|
2015-03-05 12:55:14 -05:00
|
|
|
container.Namespaces.Remove(configs.NEWPID)
|
2014-11-25 15:10:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-05 18:32:36 -04:00
|
|
|
func (d *driver) createUTS(container *configs.Config, c *execdriver.Command) error {
|
|
|
|
if c.UTS.HostUTS {
|
|
|
|
container.Namespaces.Remove(configs.NEWUTS)
|
|
|
|
container.Hostname = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) setPrivileged(container *configs.Config) (err error) {
|
|
|
|
container.Capabilities = execdriver.GetAllCapabilities()
|
2014-02-17 18:14:30 -05:00
|
|
|
container.Cgroups.AllowAllDevices = true
|
2014-04-11 07:45:39 -04:00
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
hostDevices, err := devices.HostDevices()
|
2014-05-30 21:30:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-05 12:55:14 -05:00
|
|
|
container.Devices = hostDevices
|
2014-05-19 20:13:00 -04:00
|
|
|
|
2014-04-09 06:22:17 -04:00
|
|
|
if apparmor.IsEnabled() {
|
2014-06-26 15:23:53 -04:00
|
|
|
container.AppArmorProfile = "unconfined"
|
2014-04-09 06:22:17 -04:00
|
|
|
}
|
2014-06-23 19:43:43 -04:00
|
|
|
|
2014-03-24 03:16:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) setCapabilities(container *configs.Config, c *execdriver.Command) (err error) {
|
2014-07-10 19:38:11 -04:00
|
|
|
container.Capabilities, err = execdriver.TweakCapabilities(container.Capabilities, c.CapAdd, c.CapDrop)
|
|
|
|
return err
|
2014-07-10 14:41:11 -04:00
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) setupRlimits(container *configs.Config, c *execdriver.Command) {
|
2015-02-11 14:21:38 -05:00
|
|
|
if c.Resources == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rlimit := range c.Resources.Rlimits {
|
2015-03-05 12:55:14 -05:00
|
|
|
container.Rlimits = append(container.Rlimits, configs.Rlimit{
|
|
|
|
Type: rlimit.Type,
|
|
|
|
Hard: rlimit.Hard,
|
|
|
|
Soft: rlimit.Soft,
|
|
|
|
})
|
2015-02-11 14:21:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) setupMounts(container *configs.Config, c *execdriver.Command) error {
|
2015-03-12 17:59:57 -04:00
|
|
|
userMounts := make(map[string]struct{})
|
|
|
|
for _, m := range c.Mounts {
|
|
|
|
userMounts[m.Destination] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out mounts that are overriden by user supplied mounts
|
|
|
|
var defaultMounts []*configs.Mount
|
2015-03-18 13:56:47 -04:00
|
|
|
_, mountDev := userMounts["/dev"]
|
2015-03-12 17:59:57 -04:00
|
|
|
for _, m := range container.Mounts {
|
|
|
|
if _, ok := userMounts[m.Destination]; !ok {
|
2015-03-18 13:56:47 -04:00
|
|
|
if mountDev && strings.HasPrefix(m.Destination, "/dev/") {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-12 17:59:57 -04:00
|
|
|
defaultMounts = append(defaultMounts, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
container.Mounts = defaultMounts
|
|
|
|
|
2014-03-21 04:10:07 -04:00
|
|
|
for _, m := range c.Mounts {
|
2015-03-05 12:55:14 -05:00
|
|
|
flags := syscall.MS_BIND | syscall.MS_REC
|
|
|
|
if !m.Writable {
|
|
|
|
flags |= syscall.MS_RDONLY
|
|
|
|
}
|
|
|
|
if m.Slave {
|
|
|
|
flags |= syscall.MS_SLAVE
|
|
|
|
}
|
|
|
|
container.Mounts = append(container.Mounts, &configs.Mount{
|
2014-04-11 07:45:39 -04:00
|
|
|
Source: m.Source,
|
2015-04-21 20:31:05 -04:00
|
|
|
Destination: m.Destination,
|
2015-03-05 12:55:14 -05:00
|
|
|
Device: "bind",
|
|
|
|
Flags: flags,
|
2014-04-11 07:45:39 -04:00
|
|
|
})
|
2014-03-21 04:10:07 -04:00
|
|
|
}
|
2014-03-24 03:16:40 -04:00
|
|
|
return nil
|
2014-03-21 04:10:07 -04:00
|
|
|
}
|
2014-03-27 05:04:54 -04:00
|
|
|
|
2015-05-02 09:57:57 -04:00
|
|
|
func (d *driver) setupLabels(container *configs.Config, c *execdriver.Command) {
|
2014-09-29 18:40:26 -04:00
|
|
|
container.ProcessLabel = c.ProcessLabel
|
2015-03-05 12:55:14 -05:00
|
|
|
container.MountLabel = c.MountLabel
|
2014-03-27 05:04:54 -04:00
|
|
|
}
|