2014-03-21 04:10:07 -04:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-04-01 03:07:42 -04:00
|
|
|
"os"
|
2014-04-30 18:46:56 -04:00
|
|
|
"path/filepath"
|
2014-04-01 03:07:42 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
"github.com/dotcloud/docker/daemon/execdriver"
|
|
|
|
"github.com/dotcloud/docker/daemon/execdriver/native/configuration"
|
|
|
|
"github.com/dotcloud/docker/daemon/execdriver/native/template"
|
2014-04-13 19:33:25 -04:00
|
|
|
"github.com/dotcloud/docker/pkg/apparmor"
|
2014-03-21 04:10:07 -04:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// createContainer populates and configures the container type with the
|
|
|
|
// data provided by the execdriver.Command
|
|
|
|
func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container, error) {
|
2014-03-24 03:16:40 -04:00
|
|
|
container := template.New()
|
2014-03-21 04:10:07 -04:00
|
|
|
|
|
|
|
container.Hostname = getEnv("HOSTNAME", c.Env)
|
|
|
|
container.Tty = c.Tty
|
|
|
|
container.User = c.User
|
|
|
|
container.WorkingDir = c.WorkingDir
|
|
|
|
container.Env = c.Env
|
2014-03-24 03:16:40 -04:00
|
|
|
container.Cgroups.Name = c.ID
|
|
|
|
// check to see if we are running in ramdisk to disable pivot root
|
|
|
|
container.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
|
2014-05-01 13:08:18 -04:00
|
|
|
container.Context["restrictions"] = "true"
|
2014-03-21 04:10:07 -04:00
|
|
|
|
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-03-24 03:16:40 -04:00
|
|
|
if c.Privileged {
|
|
|
|
if err := d.setPrivileged(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-11 10:30:09 -04:00
|
|
|
} else {
|
|
|
|
container.Mounts = append(container.Mounts, libcontainer.Mount{Type: "devtmpfs"})
|
2014-03-24 03:16:40 -04:00
|
|
|
}
|
|
|
|
if err := d.setupCgroups(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := d.setupMounts(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-27 05:04:54 -04:00
|
|
|
if err := d.setupLabels(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-24 03:16:40 -04:00
|
|
|
if err := configuration.ParseConfiguration(container, d.activeContainers, c.Config["native"]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return container, nil
|
|
|
|
}
|
2014-03-21 04:10:07 -04:00
|
|
|
|
2014-03-24 03:16:40 -04:00
|
|
|
func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.Command) error {
|
2014-05-02 17:17:31 -04:00
|
|
|
if c.Network.HostNetworking {
|
2014-05-05 16:54:37 -04:00
|
|
|
container.Namespaces["NEWNET"] = false
|
2014-05-02 17:17:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-03-21 04:10:07 -04:00
|
|
|
container.Networks = []*libcontainer.Network{
|
2014-03-24 03:16:40 -04:00
|
|
|
{
|
|
|
|
Mtu: c.Network.Mtu,
|
|
|
|
Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
|
|
|
|
Gateway: "localhost",
|
|
|
|
Type: "loopback",
|
|
|
|
Context: libcontainer.Context{},
|
|
|
|
},
|
2014-03-21 04:10:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Network.Interface != nil {
|
|
|
|
vethNetwork := libcontainer.Network{
|
|
|
|
Mtu: c.Network.Mtu,
|
|
|
|
Address: fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
|
|
|
|
Gateway: c.Network.Interface.Gateway,
|
|
|
|
Type: "veth",
|
|
|
|
Context: libcontainer.Context{
|
|
|
|
"prefix": "veth",
|
|
|
|
"bridge": c.Network.Interface.Bridge,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
container.Networks = append(container.Networks, &vethNetwork)
|
|
|
|
}
|
2014-04-30 18:46:56 -04:00
|
|
|
|
|
|
|
if c.Network.ContainerID != "" {
|
|
|
|
cmd := d.activeContainers[c.Network.ContainerID]
|
|
|
|
if cmd == nil || cmd.Process == nil {
|
|
|
|
return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
|
|
|
|
}
|
|
|
|
nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
|
|
|
|
container.Networks = append(container.Networks, &libcontainer.Network{
|
|
|
|
Type: "netns",
|
|
|
|
Context: libcontainer.Context{
|
|
|
|
"nspath": nspath,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2014-03-24 03:16:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-03-21 04:10:07 -04:00
|
|
|
|
2014-03-24 03:16:40 -04:00
|
|
|
func (d *driver) setPrivileged(container *libcontainer.Container) error {
|
2014-05-05 15:34:21 -04:00
|
|
|
for key := range container.CapabilitiesMask {
|
|
|
|
container.CapabilitiesMask[key] = true
|
2014-03-21 04:10:07 -04:00
|
|
|
}
|
2014-03-24 03:16:40 -04:00
|
|
|
container.Cgroups.DeviceAccess = true
|
2014-04-11 07:45:39 -04:00
|
|
|
|
2014-05-01 13:08:18 -04:00
|
|
|
delete(container.Context, "restrictions")
|
2014-04-10 19:03:52 -04:00
|
|
|
|
2014-04-09 06:22:17 -04:00
|
|
|
if apparmor.IsEnabled() {
|
|
|
|
container.Context["apparmor_profile"] = "unconfined"
|
|
|
|
}
|
2014-03-24 03:16:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) setupCgroups(container *libcontainer.Container, c *execdriver.Command) error {
|
2014-03-21 04:10:07 -04:00
|
|
|
if c.Resources != nil {
|
|
|
|
container.Cgroups.CpuShares = c.Resources.CpuShares
|
|
|
|
container.Cgroups.Memory = c.Resources.Memory
|
2014-04-24 01:11:43 -04:00
|
|
|
container.Cgroups.MemoryReservation = c.Resources.Memory
|
2014-03-21 04:10:07 -04:00
|
|
|
container.Cgroups.MemorySwap = c.Resources.MemorySwap
|
|
|
|
}
|
2014-03-24 03:16:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-03-21 04:10:07 -04:00
|
|
|
|
2014-03-24 03:16:40 -04:00
|
|
|
func (d *driver) setupMounts(container *libcontainer.Container, c *execdriver.Command) error {
|
2014-03-21 04:10:07 -04:00
|
|
|
for _, m := range c.Mounts {
|
2014-04-11 07:45:39 -04:00
|
|
|
container.Mounts = append(container.Mounts, libcontainer.Mount{
|
|
|
|
Type: "bind",
|
|
|
|
Source: m.Source,
|
|
|
|
Destination: m.Destination,
|
|
|
|
Writable: m.Writable,
|
|
|
|
Private: m.Private,
|
|
|
|
})
|
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
|
|
|
|
|
|
|
func (d *driver) setupLabels(container *libcontainer.Container, c *execdriver.Command) error {
|
2014-04-17 19:47:27 -04:00
|
|
|
container.Context["process_label"] = c.Config["process_label"][0]
|
|
|
|
container.Context["mount_label"] = c.Config["mount_label"][0]
|
2014-03-27 05:04:54 -04:00
|
|
|
return nil
|
|
|
|
}
|