2014-07-16 16:39:15 -07:00
|
|
|
// +build linux,cgo
|
2014-07-14 12:49:50 -07:00
|
|
|
|
2014-03-21 08:10:07 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2015-03-05 09:55:14 -08:00
|
|
|
"errors"
|
2014-03-21 08:10:07 +00:00
|
|
|
"fmt"
|
2015-03-05 09:55:14 -08:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2014-04-01 07:07:42 +00:00
|
|
|
|
2014-07-24 22:25:29 +00:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-07-16 16:00:55 -07: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 08:10:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// createContainer populates and configures the container type with the
|
|
|
|
// data provided by the execdriver.Command
|
2015-07-28 08:43:22 +08:00
|
|
|
func (d *Driver) createContainer(c *execdriver.Command) (*configs.Config, error) {
|
2015-01-30 17:29:46 +00:00
|
|
|
container := execdriver.InitContainer(c)
|
2014-03-21 08:10:07 +00: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 15:32:36 -07:00
|
|
|
if err := d.createUTS(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-03-24 07:16:40 +00:00
|
|
|
if err := d.createNetwork(container, c); err != nil {
|
|
|
|
return nil, err
|
2014-03-21 08:10:07 +00:00
|
|
|
}
|
2014-06-23 16:43:43 -07:00
|
|
|
|
2014-08-26 22:05:37 +00: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 09:55:14 -08:00
|
|
|
}
|
2015-06-02 12:16:43 -04:00
|
|
|
container.ReadonlyPaths = nil
|
2015-03-05 09:55:14 -08:00
|
|
|
}
|
2015-06-02 12:16:43 -04:00
|
|
|
|
2015-07-14 15:00:41 +08: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 14:17:21 -07:00
|
|
|
container.MaskPaths = nil
|
2014-03-24 07:16:40 +00:00
|
|
|
if err := d.setPrivileged(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-10 18:41:11 +00:00
|
|
|
} else {
|
2014-07-10 23:38:11 +00:00
|
|
|
if err := d.setCapabilities(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-24 07:16:40 +00:00
|
|
|
}
|
2014-06-23 16:43:43 -07:00
|
|
|
|
2015-06-17 14:39:17 -04:00
|
|
|
container.AdditionalGroups = c.GroupAdd
|
|
|
|
|
2014-09-29 23:34:45 +00:00
|
|
|
if c.AppArmorProfile != "" {
|
|
|
|
container.AppArmorProfile = c.AppArmorProfile
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:29:46 +00:00
|
|
|
if err := execdriver.SetupCgroups(container, c); err != nil {
|
2014-03-24 07:16:40 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-23 16:43:43 -07: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 07:16:40 +00:00
|
|
|
if err := d.setupMounts(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-23 16:43:43 -07:00
|
|
|
|
2015-05-02 15:57:57 +02:00
|
|
|
d.setupLabels(container, c)
|
2015-02-11 14:21:38 -05:00
|
|
|
d.setupRlimits(container, c)
|
2015-03-05 09:55:14 -08:00
|
|
|
return container, nil
|
|
|
|
}
|
2015-02-11 14:21:38 -05:00
|
|
|
|
2015-03-05 09:55:14 -08: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 20:48:06 +00:00
|
|
|
}
|
2015-03-05 09:55:14 -08:00
|
|
|
return "", errors.New("Failed to find name for new interface")
|
2014-03-24 07:16:40 +00:00
|
|
|
}
|
2014-03-21 08:10:07 +00:00
|
|
|
|
2015-07-28 08:43:22 +08:00
|
|
|
func (d *Driver) createNetwork(container *configs.Config, c *execdriver.Command) error {
|
2015-05-24 08:26:56 -07:00
|
|
|
if c.Network == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-30 15:46:56 -07:00
|
|
|
if c.Network.ContainerID != "" {
|
2014-05-29 16:40:42 +04:00
|
|
|
d.Lock()
|
2014-05-21 20:48:06 +00:00
|
|
|
active := d.activeContainers[c.Network.ContainerID]
|
2014-05-29 16:40:42 +04:00
|
|
|
d.Unlock()
|
2014-06-23 16:43:43 -07:00
|
|
|
|
2015-03-05 09:55:14 -08:00
|
|
|
if active == nil {
|
2014-04-30 15:46:56 -07:00
|
|
|
return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
|
|
|
|
}
|
2014-05-21 20:48:06 +00:00
|
|
|
|
2015-03-05 09:55:14 -08:00
|
|
|
state, err := active.State()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container.Namespaces.Add(configs.NEWNET, state.NamespacePaths[configs.NEWNET])
|
2015-05-06 22:39:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Network.NamespacePath == "" {
|
|
|
|
return fmt.Errorf("network namespace path is empty")
|
2014-04-30 15:46:56 -07:00
|
|
|
}
|
2014-06-23 16:43:43 -07:00
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
container.Namespaces.Add(configs.NEWNET, c.Network.NamespacePath)
|
2014-03-24 07:16:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-03-21 08:10:07 +00:00
|
|
|
|
2015-07-28 08:43:22 +08: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 09:55:14 -08: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 09:55:14 -08: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 09:55:14 -08: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-07-28 08:43:22 +08: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 09:55:14 -08:00
|
|
|
container.Namespaces.Remove(configs.NEWPID)
|
2014-11-25 15:10:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 08:43:22 +08:00
|
|
|
func (d *Driver) createUTS(container *configs.Config, c *execdriver.Command) error {
|
2015-05-05 15:32:36 -07:00
|
|
|
if c.UTS.HostUTS {
|
|
|
|
container.Namespaces.Remove(configs.NEWUTS)
|
|
|
|
container.Hostname = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 08:43:22 +08:00
|
|
|
func (d *Driver) setPrivileged(container *configs.Config) (err error) {
|
2015-03-05 09:55:14 -08:00
|
|
|
container.Capabilities = execdriver.GetAllCapabilities()
|
2014-02-17 15:14:30 -08:00
|
|
|
container.Cgroups.AllowAllDevices = true
|
2014-04-11 11:45:39 +00:00
|
|
|
|
2015-03-05 09:55:14 -08:00
|
|
|
hostDevices, err := devices.HostDevices()
|
2014-05-30 18:30:27 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-05 09:55:14 -08:00
|
|
|
container.Devices = hostDevices
|
2014-05-20 00:13:00 +00:00
|
|
|
|
2014-04-09 10:22:17 +00:00
|
|
|
if apparmor.IsEnabled() {
|
2015-07-29 16:57:14 -04:00
|
|
|
container.AppArmorProfile = "docker-unconfined"
|
2014-04-09 10:22:17 +00:00
|
|
|
}
|
2014-06-23 16:43:43 -07:00
|
|
|
|
2014-03-24 07:16:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 08:43:22 +08:00
|
|
|
func (d *Driver) setCapabilities(container *configs.Config, c *execdriver.Command) (err error) {
|
2014-07-10 23:38:11 +00:00
|
|
|
container.Capabilities, err = execdriver.TweakCapabilities(container.Capabilities, c.CapAdd, c.CapDrop)
|
|
|
|
return err
|
2014-07-10 18:41:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 08:43:22 +08: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 09:55:14 -08: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-07-28 08:43:22 +08: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 08:10:07 +00:00
|
|
|
for _, m := range c.Mounts {
|
2015-03-05 09:55:14 -08: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 11:45:39 +00:00
|
|
|
Source: m.Source,
|
2015-04-21 17:31:05 -07:00
|
|
|
Destination: m.Destination,
|
2015-03-05 09:55:14 -08:00
|
|
|
Device: "bind",
|
|
|
|
Flags: flags,
|
2014-04-11 11:45:39 +00:00
|
|
|
})
|
2014-03-21 08:10:07 +00:00
|
|
|
}
|
2014-03-24 07:16:40 +00:00
|
|
|
return nil
|
2014-03-21 08:10:07 +00:00
|
|
|
}
|
2014-03-27 09:04:54 +00:00
|
|
|
|
2015-07-28 08:43:22 +08:00
|
|
|
func (d *Driver) setupLabels(container *configs.Config, c *execdriver.Command) {
|
2014-09-29 22:40:26 +00:00
|
|
|
container.ProcessLabel = c.ProcessLabel
|
2015-03-05 09:55:14 -08:00
|
|
|
container.MountLabel = c.MountLabel
|
2014-03-27 09:04:54 +00:00
|
|
|
}
|