2015-07-29 18:25:56 +00:00
|
|
|
// +build linux freebsd
|
2015-04-29 22:53:35 +00:00
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-05-06 22:39:29 +00:00
|
|
|
"net"
|
2015-04-29 22:53:35 +00:00
|
|
|
"os"
|
2015-05-06 22:39:29 +00:00
|
|
|
"path"
|
2015-04-29 22:53:35 +00:00
|
|
|
"path/filepath"
|
2015-05-06 22:39:29 +00:00
|
|
|
"strconv"
|
2015-04-29 22:53:35 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-07-20 22:19:05 +00:00
|
|
|
"github.com/docker/docker/daemon/links"
|
2015-04-29 22:53:35 +00:00
|
|
|
"github.com/docker/docker/daemon/network"
|
2015-09-17 18:54:14 +00:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-04-29 22:53:35 +00:00
|
|
|
"github.com/docker/docker/pkg/directory"
|
2015-06-30 16:41:01 +00:00
|
|
|
"github.com/docker/docker/pkg/nat"
|
2015-04-29 22:53:35 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-06-11 18:29:29 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-04-29 22:53:35 +00:00
|
|
|
"github.com/docker/docker/pkg/ulimit"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/docker/utils"
|
2015-07-16 21:14:58 +00:00
|
|
|
"github.com/docker/docker/volume"
|
2015-09-18 23:58:05 +00:00
|
|
|
"github.com/docker/docker/volume/store"
|
2015-05-06 22:39:29 +00:00
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
2015-05-23 06:00:03 +00:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-07-16 23:00:55 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/devices"
|
2015-07-16 21:14:58 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2015-04-29 22:53:35 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// DefaultPathEnv is unix style list of directories to search for
|
|
|
|
// executables. Each directory is separated from the next by a colon
|
|
|
|
// ':' character .
|
2015-04-29 22:53:35 +00:00
|
|
|
const DefaultPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Container holds the fields specific to unixen implementations. See
|
|
|
|
// CommonContainer for standard fields common to all containers.
|
2015-04-29 22:53:35 +00:00
|
|
|
type Container struct {
|
|
|
|
CommonContainer
|
|
|
|
|
|
|
|
// Fields below here are platform specific.
|
2015-05-19 20:05:25 +00:00
|
|
|
activeLinks map[string]*links.Link
|
2015-07-16 21:14:58 +00:00
|
|
|
AppArmorProfile string
|
|
|
|
HostnamePath string
|
|
|
|
HostsPath string
|
2015-08-03 22:05:34 +00:00
|
|
|
ShmPath string
|
|
|
|
MqueuePath string
|
2015-07-16 21:14:58 +00:00
|
|
|
MountPoints map[string]*mountPoint
|
|
|
|
ResolvConfPath string
|
2015-07-30 21:01:53 +00:00
|
|
|
|
|
|
|
Volumes map[string]string // Deprecated since 1.7, kept for backwards compatibility
|
|
|
|
VolumesRW map[string]bool // Deprecated since 1.7, kept for backwards compatibility
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func killProcessDirectly(container *Container) error {
|
|
|
|
if _, err := container.WaitStop(10 * time.Second); err != nil {
|
|
|
|
// Ensure that we don't kill ourselves
|
2015-07-30 21:01:53 +00:00
|
|
|
if pid := container.getPID(); pid != 0 {
|
2015-04-29 22:53:35 +00:00
|
|
|
logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
|
|
|
|
if err := syscall.Kill(pid, 9); err != nil {
|
|
|
|
if err != syscall.ESRCH {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Cannot kill process (pid=%d) with signal 9: no such process.", pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) setupLinkedContainers() ([]string, error) {
|
2015-04-29 22:53:35 +00:00
|
|
|
var (
|
|
|
|
env []string
|
|
|
|
daemon = container.daemon
|
|
|
|
)
|
2015-09-29 17:51:40 +00:00
|
|
|
children, err := daemon.children(container.Name)
|
2015-04-29 22:53:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(children) > 0 {
|
|
|
|
for linkAlias, child := range children {
|
|
|
|
if !child.IsRunning() {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeLinkNotRunning.WithArgs(child.Name, linkAlias)
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 22:19:05 +00:00
|
|
|
link := links.NewLink(
|
2015-04-29 22:53:35 +00:00
|
|
|
container.NetworkSettings.IPAddress,
|
|
|
|
child.NetworkSettings.IPAddress,
|
|
|
|
linkAlias,
|
|
|
|
child.Config.Env,
|
|
|
|
child.Config.ExposedPorts,
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, envVar := range link.ToEnv() {
|
|
|
|
env = append(env, envVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return env, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) createDaemonEnvironment(linkedEnv []string) []string {
|
|
|
|
// if a domain name was specified, append it to the hostname (see #7851)
|
|
|
|
fullHostname := container.Config.Hostname
|
|
|
|
if container.Config.Domainname != "" {
|
|
|
|
fullHostname = fmt.Sprintf("%s.%s", fullHostname, container.Config.Domainname)
|
|
|
|
}
|
|
|
|
// Setup environment
|
|
|
|
env := []string{
|
|
|
|
"PATH=" + DefaultPathEnv,
|
|
|
|
"HOSTNAME=" + fullHostname,
|
|
|
|
// Note: we don't set HOME here because it'll get autoset intelligently
|
|
|
|
// based on the value of USER inside dockerinit, but only if it isn't
|
|
|
|
// set already (ie, that can be overridden by setting HOME via -e or ENV
|
|
|
|
// in a Dockerfile).
|
|
|
|
}
|
|
|
|
if container.Config.Tty {
|
|
|
|
env = append(env, "TERM=xterm")
|
|
|
|
}
|
|
|
|
env = append(env, linkedEnv...)
|
|
|
|
// because the env on the container can override certain default values
|
|
|
|
// we need to replace the 'env' keys where they match and append anything
|
|
|
|
// else.
|
|
|
|
env = utils.ReplaceOrAppendEnvValues(env, container.Config.Env)
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDevicesFromPath(deviceMapping runconfig.DeviceMapping) (devs []*configs.Device, err error) {
|
|
|
|
device, err := devices.DeviceFromPath(deviceMapping.PathOnHost, deviceMapping.CgroupPermissions)
|
|
|
|
// if there was no error, return the device
|
|
|
|
if err == nil {
|
|
|
|
device.Path = deviceMapping.PathInContainer
|
|
|
|
return append(devs, device), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the device is not a device node
|
|
|
|
// try to see if it's a directory holding many devices
|
|
|
|
if err == devices.ErrNotADevice {
|
|
|
|
|
|
|
|
// check if it is a directory
|
|
|
|
if src, e := os.Stat(deviceMapping.PathOnHost); e == nil && src.IsDir() {
|
|
|
|
|
|
|
|
// mount the internal devices recursively
|
|
|
|
filepath.Walk(deviceMapping.PathOnHost, func(dpath string, f os.FileInfo, e error) error {
|
|
|
|
childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
|
|
|
|
if e != nil {
|
|
|
|
// ignore the device
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the device to userSpecified devices
|
|
|
|
childDevice.Path = strings.Replace(dpath, deviceMapping.PathOnHost, deviceMapping.PathInContainer, 1)
|
|
|
|
devs = append(devs, childDevice)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(devs) > 0 {
|
|
|
|
return devs, nil
|
|
|
|
}
|
|
|
|
|
2015-09-16 18:56:26 +00:00
|
|
|
return devs, derr.ErrorCodeDeviceInfo.WithArgs(deviceMapping.PathOnHost, err)
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func populateCommand(c *Container, env []string) error {
|
2015-05-24 15:26:56 +00:00
|
|
|
var en *execdriver.Network
|
2015-06-04 00:31:22 +00:00
|
|
|
if !c.Config.NetworkDisabled {
|
2015-09-11 19:05:57 +00:00
|
|
|
en = &execdriver.Network{}
|
|
|
|
if !c.daemon.execDriver.SupportsHooks() || c.hostConfig.NetworkMode.IsHost() {
|
|
|
|
en.NamespacePath = c.NetworkSettings.SandboxKey
|
2015-05-24 15:26:56 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-05-24 15:26:56 +00:00
|
|
|
parts := strings.SplitN(string(c.hostConfig.NetworkMode), ":", 2)
|
|
|
|
if parts[0] == "container" {
|
2015-09-29 17:51:40 +00:00
|
|
|
nc, err := c.getNetworkedContainer()
|
2015-05-24 15:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
en.ContainerID = nc.ID
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc := &execdriver.Ipc{}
|
2015-08-03 22:05:34 +00:00
|
|
|
var err error
|
|
|
|
c.ShmPath, err = c.shmPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.MqueuePath, err = c.mqueuePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
|
|
|
if c.hostConfig.IpcMode.IsContainer() {
|
2015-09-29 17:51:40 +00:00
|
|
|
ic, err := c.getIpcContainer()
|
2015-04-29 22:53:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ipc.ContainerID = ic.ID
|
2015-08-03 22:05:34 +00:00
|
|
|
c.ShmPath = ic.ShmPath
|
|
|
|
c.MqueuePath = ic.MqueuePath
|
2015-04-29 22:53:35 +00:00
|
|
|
} else {
|
|
|
|
ipc.HostIpc = c.hostConfig.IpcMode.IsHost()
|
2015-08-03 22:05:34 +00:00
|
|
|
if ipc.HostIpc {
|
|
|
|
c.ShmPath = "/dev/shm"
|
|
|
|
c.MqueuePath = "/dev/mqueue"
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pid := &execdriver.Pid{}
|
|
|
|
pid.HostPid = c.hostConfig.PidMode.IsHost()
|
|
|
|
|
|
|
|
uts := &execdriver.UTS{
|
|
|
|
HostUTS: c.hostConfig.UTSMode.IsHost(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build lists of devices allowed and created within the container.
|
|
|
|
var userSpecifiedDevices []*configs.Device
|
|
|
|
for _, deviceMapping := range c.hostConfig.Devices {
|
|
|
|
devs, err := getDevicesFromPath(deviceMapping)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userSpecifiedDevices = append(userSpecifiedDevices, devs...)
|
|
|
|
}
|
|
|
|
|
2015-05-29 15:29:56 +00:00
|
|
|
allowedDevices := mergeDevices(configs.DefaultAllowedDevices, userSpecifiedDevices)
|
|
|
|
|
|
|
|
autoCreatedDevices := mergeDevices(configs.DefaultAutoCreatedDevices, userSpecifiedDevices)
|
2015-04-29 22:53:35 +00:00
|
|
|
|
|
|
|
// TODO: this can be removed after lxc-conf is fully deprecated
|
|
|
|
lxcConfig, err := mergeLxcConfIntoOptions(c.hostConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rlimits []*ulimit.Rlimit
|
|
|
|
ulimits := c.hostConfig.Ulimits
|
|
|
|
|
|
|
|
// Merge ulimits with daemon defaults
|
|
|
|
ulIdx := make(map[string]*ulimit.Ulimit)
|
|
|
|
for _, ul := range ulimits {
|
|
|
|
ulIdx[ul.Name] = ul
|
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
for name, ul := range c.daemon.configStore.Ulimits {
|
2015-04-29 22:53:35 +00:00
|
|
|
if _, exists := ulIdx[name]; !exists {
|
|
|
|
ulimits = append(ulimits, ul)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, limit := range ulimits {
|
|
|
|
rl, err := limit.GetRlimit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rlimits = append(rlimits, rl)
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := &execdriver.Resources{
|
2015-09-23 06:02:45 +00:00
|
|
|
Memory: c.hostConfig.Memory,
|
|
|
|
MemorySwap: c.hostConfig.MemorySwap,
|
|
|
|
MemoryReservation: c.hostConfig.MemoryReservation,
|
|
|
|
KernelMemory: c.hostConfig.KernelMemory,
|
|
|
|
CPUShares: c.hostConfig.CPUShares,
|
|
|
|
CpusetCpus: c.hostConfig.CpusetCpus,
|
|
|
|
CpusetMems: c.hostConfig.CpusetMems,
|
|
|
|
CPUPeriod: c.hostConfig.CPUPeriod,
|
|
|
|
CPUQuota: c.hostConfig.CPUQuota,
|
|
|
|
BlkioWeight: c.hostConfig.BlkioWeight,
|
|
|
|
Rlimits: rlimits,
|
|
|
|
OomKillDisable: c.hostConfig.OomKillDisable,
|
|
|
|
MemorySwappiness: -1,
|
2015-07-29 20:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.hostConfig.MemorySwappiness != nil {
|
|
|
|
resources.MemorySwappiness = *c.hostConfig.MemorySwappiness
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
processConfig := execdriver.ProcessConfig{
|
|
|
|
Privileged: c.hostConfig.Privileged,
|
|
|
|
Entrypoint: c.Path,
|
|
|
|
Arguments: c.Args,
|
|
|
|
Tty: c.Config.Tty,
|
|
|
|
User: c.Config.User,
|
|
|
|
}
|
|
|
|
|
|
|
|
processConfig.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
|
|
|
processConfig.Env = env
|
|
|
|
|
|
|
|
c.command = &execdriver.Command{
|
|
|
|
ID: c.ID,
|
2015-07-30 21:01:53 +00:00
|
|
|
Rootfs: c.rootfsPath(),
|
2015-04-29 22:53:35 +00:00
|
|
|
ReadonlyRootfs: c.hostConfig.ReadonlyRootfs,
|
|
|
|
InitPath: "/.dockerinit",
|
|
|
|
WorkingDir: c.Config.WorkingDir,
|
|
|
|
Network: en,
|
|
|
|
Ipc: ipc,
|
|
|
|
Pid: pid,
|
|
|
|
UTS: uts,
|
|
|
|
Resources: resources,
|
|
|
|
AllowedDevices: allowedDevices,
|
|
|
|
AutoCreatedDevices: autoCreatedDevices,
|
2015-07-01 17:59:18 +00:00
|
|
|
CapAdd: c.hostConfig.CapAdd.Slice(),
|
|
|
|
CapDrop: c.hostConfig.CapDrop.Slice(),
|
2015-06-17 18:39:17 +00:00
|
|
|
GroupAdd: c.hostConfig.GroupAdd,
|
2015-04-29 22:53:35 +00:00
|
|
|
ProcessConfig: processConfig,
|
2015-07-30 21:01:53 +00:00
|
|
|
ProcessLabel: c.getProcessLabel(),
|
|
|
|
MountLabel: c.getMountLabel(),
|
2015-04-29 22:53:35 +00:00
|
|
|
LxcConfig: lxcConfig,
|
|
|
|
AppArmorProfile: c.AppArmorProfile,
|
|
|
|
CgroupParent: c.hostConfig.CgroupParent,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-29 15:29:56 +00:00
|
|
|
func mergeDevices(defaultDevices, userDevices []*configs.Device) []*configs.Device {
|
|
|
|
if len(userDevices) == 0 {
|
|
|
|
return defaultDevices
|
|
|
|
}
|
|
|
|
|
|
|
|
paths := map[string]*configs.Device{}
|
|
|
|
for _, d := range userDevices {
|
|
|
|
paths[d.Path] = d
|
|
|
|
}
|
|
|
|
|
|
|
|
var devs []*configs.Device
|
|
|
|
for _, d := range defaultDevices {
|
|
|
|
if _, defined := paths[d.Path]; !defined {
|
|
|
|
devs = append(devs, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(devs, userDevices...)
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// GetSize returns the real size & virtual size of the container.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) getSize() (int64, int64) {
|
2015-04-29 22:53:35 +00:00
|
|
|
var (
|
|
|
|
sizeRw, sizeRootfs int64
|
|
|
|
err error
|
|
|
|
driver = container.daemon.driver
|
|
|
|
)
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.Mount(); err != nil {
|
2015-04-29 22:53:35 +00:00
|
|
|
logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
|
|
|
|
return sizeRw, sizeRootfs
|
|
|
|
}
|
2015-09-29 17:51:40 +00:00
|
|
|
defer container.Unmount()
|
2015-04-29 22:53:35 +00:00
|
|
|
|
|
|
|
initID := fmt.Sprintf("%s-init", container.ID)
|
|
|
|
sizeRw, err = driver.DiffSize(container.ID, initID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Driver %s couldn't return diff size of container %s: %s", driver, container.ID, err)
|
|
|
|
// FIXME: GetSize should return an error. Not changing it now in case
|
|
|
|
// there is a side-effect.
|
|
|
|
sizeRw = -1
|
|
|
|
}
|
|
|
|
|
2015-05-19 05:48:44 +00:00
|
|
|
if _, err = os.Stat(container.basefs); err == nil {
|
2015-04-29 22:53:35 +00:00
|
|
|
if sizeRootfs, err = directory.Size(container.basefs); err != nil {
|
|
|
|
sizeRootfs = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sizeRw, sizeRootfs
|
|
|
|
}
|
|
|
|
|
2015-07-14 00:54:00 +00:00
|
|
|
// Attempt to set the network mounts given a provided destination and
|
|
|
|
// the path to use for it; return true if the given destination was a
|
|
|
|
// network mount file
|
|
|
|
func (container *Container) trySetNetworkMount(destination string, path string) bool {
|
|
|
|
if destination == "/etc/resolv.conf" {
|
|
|
|
container.ResolvConfPath = path
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if destination == "/etc/hostname" {
|
|
|
|
container.HostnamePath = path
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if destination == "/etc/hosts" {
|
|
|
|
container.HostsPath = path
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
func (container *Container) buildHostnameFile() error {
|
2015-07-30 21:01:53 +00:00
|
|
|
hostnamePath, err := container.getRootResourcePath("hostname")
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.HostnamePath = hostnamePath
|
|
|
|
|
|
|
|
if container.Config.Domainname != "" {
|
|
|
|
return ioutil.WriteFile(container.HostnamePath, []byte(fmt.Sprintf("%s.%s\n", container.Config.Hostname, container.Config.Domainname)), 0644)
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644)
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) buildSandboxOptions() ([]libnetwork.SandboxOption, error) {
|
2015-05-06 22:39:29 +00:00
|
|
|
var (
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions []libnetwork.SandboxOption
|
2015-05-06 22:39:29 +00:00
|
|
|
err error
|
|
|
|
dns []string
|
|
|
|
dnsSearch []string
|
2015-08-31 18:47:25 +00:00
|
|
|
dnsOptions []string
|
2015-05-06 22:39:29 +00:00
|
|
|
)
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionHostname(container.Config.Hostname),
|
|
|
|
libnetwork.OptionDomainname(container.Config.Domainname))
|
2015-05-06 22:39:29 +00:00
|
|
|
|
|
|
|
if container.hostConfig.NetworkMode.IsHost() {
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionUseDefaultSandbox())
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionOriginHostsPath("/etc/hosts"))
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"))
|
2015-09-11 19:05:57 +00:00
|
|
|
} else if container.daemon.execDriver.SupportsHooks() {
|
|
|
|
// OptionUseExternalKey is mandatory for userns support.
|
|
|
|
// But optional for non-userns support
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionUseExternalKey())
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
container.HostsPath, err = container.getRootResourcePath("hosts")
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
|
2015-05-06 22:39:29 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
container.ResolvConfPath, err = container.getRootResourcePath("resolv.conf")
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
|
2015-05-06 22:39:29 +00:00
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
if len(container.hostConfig.DNS) > 0 {
|
|
|
|
dns = container.hostConfig.DNS
|
2015-07-30 21:01:53 +00:00
|
|
|
} else if len(container.daemon.configStore.DNS) > 0 {
|
|
|
|
dns = container.daemon.configStore.DNS
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dns {
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNS(d))
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
if len(container.hostConfig.DNSSearch) > 0 {
|
|
|
|
dnsSearch = container.hostConfig.DNSSearch
|
2015-07-30 21:01:53 +00:00
|
|
|
} else if len(container.daemon.configStore.DNSSearch) > 0 {
|
|
|
|
dnsSearch = container.daemon.configStore.DNSSearch
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range dnsSearch {
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNSSearch(ds))
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:47:25 +00:00
|
|
|
if len(container.hostConfig.DNSOptions) > 0 {
|
|
|
|
dnsOptions = container.hostConfig.DNSOptions
|
|
|
|
} else if len(container.daemon.configStore.DNSOptions) > 0 {
|
|
|
|
dnsOptions = container.daemon.configStore.DNSOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range dnsOptions {
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNSOptions(ds))
|
|
|
|
}
|
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
if container.NetworkSettings.SecondaryIPAddresses != nil {
|
|
|
|
name := container.Config.Hostname
|
|
|
|
if container.Config.Domainname != "" {
|
|
|
|
name = name + "." + container.Config.Domainname
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range container.NetworkSettings.SecondaryIPAddresses {
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(name, a.Addr))
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var childEndpoints, parentEndpoints []string
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
children, err := container.daemon.children(container.Name)
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for linkAlias, child := range children {
|
|
|
|
_, alias := path.Split(linkAlias)
|
|
|
|
// allow access to the linked container via the alias, real name, and container hostname
|
|
|
|
aliasList := alias + " " + child.Config.Hostname
|
|
|
|
// only add the name if alias isn't equal to the name
|
|
|
|
if alias != child.Name[1:] {
|
|
|
|
aliasList = aliasList + " " + child.Name[1:]
|
|
|
|
}
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(aliasList, child.NetworkSettings.IPAddress))
|
2015-05-06 22:39:29 +00:00
|
|
|
if child.NetworkSettings.EndpointID != "" {
|
|
|
|
childEndpoints = append(childEndpoints, child.NetworkSettings.EndpointID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, extraHost := range container.hostConfig.ExtraHosts {
|
|
|
|
// allow IPv6 addresses in extra hosts; only split on first ":"
|
|
|
|
parts := strings.SplitN(extraHost, ":", 2)
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(parts[0], parts[1]))
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
refs := container.daemon.containerGraph().RefPaths(container.ID)
|
2015-05-06 22:39:29 +00:00
|
|
|
for _, ref := range refs {
|
|
|
|
if ref.ParentID == "0" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
c, err := container.daemon.Get(ref.ParentID)
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
if c != nil && !container.daemon.configStore.DisableBridge && container.hostConfig.NetworkMode.IsPrivate() {
|
2015-05-06 22:39:29 +00:00
|
|
|
logrus.Debugf("Update /etc/hosts of %s for alias %s with ip %s", c.ID, ref.Name, container.NetworkSettings.IPAddress)
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionParentUpdate(c.ID, ref.Name, container.NetworkSettings.IPAddress))
|
2015-05-06 22:39:29 +00:00
|
|
|
if c.NetworkSettings.EndpointID != "" {
|
|
|
|
parentEndpoints = append(parentEndpoints, c.NetworkSettings.EndpointID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
linkOptions := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"ParentEndpoints": parentEndpoints,
|
|
|
|
"ChildEndpoints": childEndpoints,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionGeneric(linkOptions))
|
2015-05-06 22:39:29 +00:00
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
return sboxOptions, nil
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 20:18:13 +00:00
|
|
|
func (container *Container) buildPortMapInfo(ep libnetwork.Endpoint, networkSettings *network.Settings) (*network.Settings, error) {
|
2015-05-06 22:39:29 +00:00
|
|
|
if ep == nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeEmptyEndpoint
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if networkSettings == nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeEmptyNetwork
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
driverInfo, err := ep.DriverInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if driverInfo == nil {
|
|
|
|
// It is not an error for epInfo to be nil
|
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if mac, ok := driverInfo[netlabel.MacAddress]; ok {
|
|
|
|
networkSettings.MacAddress = mac.(net.HardwareAddr).String()
|
|
|
|
}
|
|
|
|
|
2015-06-04 05:47:08 +00:00
|
|
|
networkSettings.Ports = nat.PortMap{}
|
|
|
|
|
|
|
|
if expData, ok := driverInfo[netlabel.ExposedPorts]; ok {
|
|
|
|
if exposedPorts, ok := expData.([]types.TransportPort); ok {
|
|
|
|
for _, tp := range exposedPorts {
|
2015-07-16 03:45:48 +00:00
|
|
|
natPort, err := nat.NewPort(tp.Proto.String(), strconv.Itoa(int(tp.Port)))
|
|
|
|
if err != nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeParsingPort.WithArgs(tp.Port, err)
|
2015-07-16 03:45:48 +00:00
|
|
|
}
|
2015-06-04 05:47:08 +00:00
|
|
|
networkSettings.Ports[natPort] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
mapData, ok := driverInfo[netlabel.PortMap]
|
|
|
|
if !ok {
|
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 06:00:03 +00:00
|
|
|
if portMapping, ok := mapData.([]types.PortBinding); ok {
|
2015-05-06 22:39:29 +00:00
|
|
|
for _, pp := range portMapping {
|
2015-07-16 03:45:48 +00:00
|
|
|
natPort, err := nat.NewPort(pp.Proto.String(), strconv.Itoa(int(pp.Port)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-21 13:43:32 +00:00
|
|
|
natBndg := nat.PortBinding{HostIP: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))}
|
2015-05-06 22:39:29 +00:00
|
|
|
networkSettings.Ports[natPort] = append(networkSettings.Ports[natPort], natBndg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 20:18:13 +00:00
|
|
|
func (container *Container) buildEndpointInfo(ep libnetwork.Endpoint, networkSettings *network.Settings) (*network.Settings, error) {
|
2015-05-06 22:39:29 +00:00
|
|
|
if ep == nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeEmptyEndpoint
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if networkSettings == nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeEmptyNetwork
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
epInfo := ep.Info()
|
|
|
|
if epInfo == nil {
|
|
|
|
// It is not an error to get an empty endpoint info
|
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
2015-09-14 18:04:12 +00:00
|
|
|
iface := epInfo.Iface()
|
|
|
|
if iface == nil {
|
2015-05-06 22:39:29 +00:00
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ones, _ := iface.Address().Mask.Size()
|
|
|
|
networkSettings.IPAddress = iface.Address().IP.String()
|
|
|
|
networkSettings.IPPrefixLen = ones
|
|
|
|
|
|
|
|
if iface.AddressIPv6().IP.To16() != nil {
|
|
|
|
onesv6, _ := iface.AddressIPv6().Mask.Size()
|
|
|
|
networkSettings.GlobalIPv6Address = iface.AddressIPv6().IP.String()
|
|
|
|
networkSettings.GlobalIPv6PrefixLen = onesv6
|
|
|
|
}
|
|
|
|
|
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) updateJoinInfo(ep libnetwork.Endpoint) error {
|
|
|
|
epInfo := ep.Info()
|
|
|
|
if epInfo == nil {
|
|
|
|
// It is not an error to get an empty endpoint info
|
2015-04-29 22:53:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
container.NetworkSettings.Gateway = epInfo.Gateway().String()
|
|
|
|
if epInfo.GatewayIPv6().To16() != nil {
|
|
|
|
container.NetworkSettings.IPv6Gateway = epInfo.GatewayIPv6().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
func (container *Container) updateEndpointNetworkSettings(n libnetwork.Network, ep libnetwork.Endpoint) error {
|
2015-05-06 22:39:29 +00:00
|
|
|
networkSettings := &network.Settings{NetworkID: n.ID(), EndpointID: ep.ID()}
|
|
|
|
|
2015-09-09 20:18:13 +00:00
|
|
|
networkSettings, err := container.buildPortMapInfo(ep, networkSettings)
|
2015-04-29 22:53:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-09 20:18:13 +00:00
|
|
|
networkSettings, err = container.buildEndpointInfo(ep, networkSettings)
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.hostConfig.NetworkMode == runconfig.NetworkMode("bridge") {
|
2015-07-30 21:01:53 +00:00
|
|
|
networkSettings.Bridge = container.daemon.configStore.Bridge.Iface
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
container.NetworkSettings = networkSettings
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
func (container *Container) updateSandboxNetworkSettings(sb libnetwork.Sandbox) error {
|
|
|
|
container.NetworkSettings.SandboxID = sb.ID()
|
|
|
|
container.NetworkSettings.SandboxKey = sb.Key()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-20 22:19:05 +00:00
|
|
|
// UpdateNetwork is used to update the container's network (e.g. when linked containers
|
|
|
|
// get removed/unlinked).
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) updateNetwork() error {
|
2015-09-02 23:43:28 +00:00
|
|
|
ctrl := container.daemon.netController
|
|
|
|
sid := container.NetworkSettings.SandboxID
|
2015-05-06 22:39:29 +00:00
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
sb, err := ctrl.SandboxByID(sid)
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNoSandbox.WithArgs(sid, err)
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
options, err := container.buildSandboxOptions()
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNetworkUpdate.WithArgs(err)
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
if err := sb.Refresh(options...); err != nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNetworkRefresh.WithArgs(sid, err)
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) buildCreateEndpointOptions() ([]libnetwork.EndpointOption, error) {
|
|
|
|
var (
|
|
|
|
portSpecs = make(nat.PortSet)
|
|
|
|
bindings = make(nat.PortMap)
|
2015-05-23 06:00:03 +00:00
|
|
|
pbList []types.PortBinding
|
|
|
|
exposeList []types.TransportPort
|
2015-05-06 22:39:29 +00:00
|
|
|
createOptions []libnetwork.EndpointOption
|
|
|
|
)
|
2015-04-29 22:53:35 +00:00
|
|
|
|
|
|
|
if container.Config.ExposedPorts != nil {
|
|
|
|
portSpecs = container.Config.ExposedPorts
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.hostConfig.PortBindings != nil {
|
|
|
|
for p, b := range container.hostConfig.PortBindings {
|
|
|
|
bindings[p] = []nat.PortBinding{}
|
|
|
|
for _, bb := range b {
|
|
|
|
bindings[p] = append(bindings[p], nat.PortBinding{
|
2015-07-21 13:43:32 +00:00
|
|
|
HostIP: bb.HostIP,
|
2015-04-29 22:53:35 +00:00
|
|
|
HostPort: bb.HostPort,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ports := make([]nat.Port, len(portSpecs))
|
|
|
|
var i int
|
|
|
|
for p := range portSpecs {
|
|
|
|
ports[i] = p
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
nat.SortPortMap(ports, bindings)
|
|
|
|
for _, port := range ports {
|
2015-05-23 06:00:03 +00:00
|
|
|
expose := types.TransportPort{}
|
|
|
|
expose.Proto = types.ParseProtocol(port.Proto())
|
2015-05-06 22:39:29 +00:00
|
|
|
expose.Port = uint16(port.Int())
|
|
|
|
exposeList = append(exposeList, expose)
|
|
|
|
|
2015-05-23 06:00:03 +00:00
|
|
|
pb := types.PortBinding{Port: expose.Port, Proto: expose.Proto}
|
2015-05-06 22:39:29 +00:00
|
|
|
binding := bindings[port]
|
|
|
|
for i := 0; i < len(binding); i++ {
|
|
|
|
pbCopy := pb.GetCopy()
|
2015-07-16 03:45:48 +00:00
|
|
|
newP, err := nat.NewPort(nat.SplitProtoPort(binding[i].HostPort))
|
2015-05-01 19:35:26 +00:00
|
|
|
var portStart, portEnd int
|
|
|
|
if err == nil {
|
|
|
|
portStart, portEnd, err = newP.Range()
|
|
|
|
}
|
2015-07-16 03:45:48 +00:00
|
|
|
if err != nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeHostPort.WithArgs(binding[i].HostPort, err)
|
2015-07-16 03:45:48 +00:00
|
|
|
}
|
2015-05-01 19:35:26 +00:00
|
|
|
pbCopy.HostPort = uint16(portStart)
|
|
|
|
pbCopy.HostPortEnd = uint16(portEnd)
|
2015-07-21 13:43:32 +00:00
|
|
|
pbCopy.HostIP = net.ParseIP(binding[i].HostIP)
|
2015-05-06 22:39:29 +00:00
|
|
|
pbList = append(pbList, pbCopy)
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
if container.hostConfig.PublishAllPorts && len(binding) == 0 {
|
|
|
|
pbList = append(pbList, pb)
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
createOptions = append(createOptions,
|
|
|
|
libnetwork.CreateOptionPortMapping(pbList),
|
|
|
|
libnetwork.CreateOptionExposedPorts(exposeList))
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
if container.Config.MacAddress != "" {
|
|
|
|
mac, err := net.ParseMAC(container.Config.MacAddress)
|
2015-04-29 22:53:35 +00:00
|
|
|
if err != nil {
|
2015-05-06 22:39:29 +00:00
|
|
|
return nil, err
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
genericOption := options.Generic{
|
|
|
|
netlabel.MacAddress: mac,
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-05-06 22:39:29 +00:00
|
|
|
return createOptions, nil
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-06-15 05:31:09 +00:00
|
|
|
func parseService(controller libnetwork.NetworkController, service string) (string, string, string) {
|
|
|
|
dn := controller.Config().Daemon.DefaultNetwork
|
|
|
|
dd := controller.Config().Daemon.DefaultDriver
|
|
|
|
|
|
|
|
snd := strings.Split(service, ".")
|
|
|
|
if len(snd) > 2 {
|
|
|
|
return strings.Join(snd[:len(snd)-2], "."), snd[len(snd)-2], snd[len(snd)-1]
|
|
|
|
}
|
|
|
|
if len(snd) > 1 {
|
|
|
|
return snd[0], snd[1], dd
|
|
|
|
}
|
|
|
|
return snd[0], dn, dd
|
|
|
|
}
|
|
|
|
|
|
|
|
func createNetwork(controller libnetwork.NetworkController, dnet string, driver string) (libnetwork.Network, error) {
|
2015-05-20 12:20:19 +00:00
|
|
|
createOptions := []libnetwork.NetworkOption{}
|
|
|
|
genericOption := options.Generic{}
|
|
|
|
|
|
|
|
// Bridge driver is special due to legacy reasons
|
|
|
|
if runconfig.NetworkMode(driver).IsBridge() {
|
|
|
|
genericOption[netlabel.GenericData] = map[string]interface{}{
|
|
|
|
"BridgeName": dnet,
|
|
|
|
"AllowNonDefaultBridge": "true",
|
|
|
|
}
|
|
|
|
networkOption := libnetwork.NetworkOptionGeneric(genericOption)
|
|
|
|
createOptions = append(createOptions, networkOption)
|
|
|
|
}
|
|
|
|
|
|
|
|
return controller.NewNetwork(driver, dnet, createOptions...)
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) allocateNetwork() error {
|
2015-05-06 22:39:29 +00:00
|
|
|
mode := container.hostConfig.NetworkMode
|
2015-05-20 12:20:19 +00:00
|
|
|
controller := container.daemon.netController
|
2015-05-06 22:39:29 +00:00
|
|
|
if container.Config.NetworkDisabled || mode.IsContainer() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-20 04:15:51 +00:00
|
|
|
networkDriver := string(mode)
|
2015-06-15 05:31:09 +00:00
|
|
|
service := container.Config.PublishService
|
2015-05-20 12:20:19 +00:00
|
|
|
networkName := mode.NetworkName()
|
|
|
|
if mode.IsDefault() {
|
2015-06-15 05:31:09 +00:00
|
|
|
if service != "" {
|
|
|
|
service, networkName, networkDriver = parseService(controller, service)
|
|
|
|
} else {
|
|
|
|
networkName = controller.Config().Daemon.DefaultNetwork
|
|
|
|
networkDriver = controller.Config().Daemon.DefaultDriver
|
|
|
|
}
|
|
|
|
} else if service != "" {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNetworkConflict
|
2015-06-15 05:31:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
if runconfig.NetworkMode(networkDriver).IsBridge() && container.daemon.configStore.DisableBridge {
|
2015-06-30 17:34:15 +00:00
|
|
|
container.Config.NetworkDisabled = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-15 05:31:09 +00:00
|
|
|
if service == "" {
|
2015-06-21 00:08:36 +00:00
|
|
|
// dot character "." has a special meaning to support SERVICE[.NETWORK] format.
|
2015-08-07 22:24:18 +00:00
|
|
|
// For backward compatibility, replacing "." with "-", instead of failing
|
2015-06-15 05:31:09 +00:00
|
|
|
service = strings.Replace(container.Name, ".", "-", -1)
|
2015-06-21 00:08:36 +00:00
|
|
|
// Service names dont like "/" in them. removing it instead of failing for backward compatibility
|
|
|
|
service = strings.Replace(service, "/", "", -1)
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.configureNetwork(networkName, service, networkDriver, mode.IsDefault()); err != nil {
|
2015-06-20 04:15:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
return container.writeHostConfig()
|
2015-06-20 04:15:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) configureNetwork(networkName, service, networkDriver string, canCreateNetwork bool) error {
|
2015-06-20 04:15:51 +00:00
|
|
|
controller := container.daemon.netController
|
2015-09-02 23:43:28 +00:00
|
|
|
|
2015-05-20 12:20:19 +00:00
|
|
|
n, err := controller.NetworkByName(networkName)
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
2015-06-20 04:15:51 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok || !canCreateNetwork {
|
2015-06-15 05:31:09 +00:00
|
|
|
return err
|
2015-05-20 12:20:19 +00:00
|
|
|
}
|
2015-06-15 05:31:09 +00:00
|
|
|
|
|
|
|
if n, err = createNetwork(controller, networkName, networkDriver); err != nil {
|
2015-05-20 12:20:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-06-15 05:31:09 +00:00
|
|
|
ep, err := n.EndpointByName(service)
|
2015-05-06 22:39:29 +00:00
|
|
|
if err != nil {
|
2015-06-15 05:31:09 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrNoSuchEndpoint); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-06 22:39:29 +00:00
|
|
|
|
2015-06-15 05:31:09 +00:00
|
|
|
createOptions, err := container.buildCreateEndpointOptions()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err = n.CreateEndpoint(service, createOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
if err := container.updateEndpointNetworkSettings(n, ep); err != nil {
|
2015-05-06 22:39:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
var sb libnetwork.Sandbox
|
|
|
|
controller.WalkSandboxes(func(s libnetwork.Sandbox) bool {
|
|
|
|
if s.ContainerID() == container.ID {
|
|
|
|
sb = s
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if sb == nil {
|
2015-09-29 17:51:40 +00:00
|
|
|
options, err := container.buildSandboxOptions()
|
2015-09-02 23:43:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sb, err = controller.NewSandbox(container.ID, options...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
2015-05-06 22:39:29 +00:00
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
container.updateSandboxNetworkSettings(sb)
|
|
|
|
|
|
|
|
if err := ep.Join(sb); err != nil {
|
2015-05-06 22:39:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.updateJoinInfo(ep); err != nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeJoinInfo.WithArgs(err)
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) initializeNetworking() error {
|
2015-05-06 22:39:29 +00:00
|
|
|
var err error
|
|
|
|
|
2015-04-29 22:53:35 +00:00
|
|
|
if container.hostConfig.NetworkMode.IsContainer() {
|
|
|
|
// we need to get the hosts files from the container to join
|
2015-09-29 17:51:40 +00:00
|
|
|
nc, err := container.getNetworkedContainer()
|
2015-04-29 22:53:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.HostnamePath = nc.HostnamePath
|
|
|
|
container.HostsPath = nc.HostsPath
|
|
|
|
container.ResolvConfPath = nc.ResolvConfPath
|
|
|
|
container.Config.Hostname = nc.Config.Hostname
|
|
|
|
container.Config.Domainname = nc.Config.Domainname
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-06 22:39:29 +00:00
|
|
|
|
|
|
|
if container.hostConfig.NetworkMode.IsHost() {
|
|
|
|
container.Config.Hostname, err = os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(container.Config.Hostname, ".", 2)
|
|
|
|
if len(parts) > 1 {
|
|
|
|
container.Config.Hostname = parts[0]
|
|
|
|
container.Config.Domainname = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.allocateNetwork(); err != nil {
|
2015-04-29 22:53:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-06 22:39:29 +00:00
|
|
|
|
|
|
|
return container.buildHostnameFile()
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 19:05:57 +00:00
|
|
|
// called from the libcontainer pre-start hook to set the network
|
|
|
|
// namespace configuration linkage to the libnetwork "sandbox" entity
|
|
|
|
func (container *Container) setNetworkNamespaceKey(pid int) error {
|
|
|
|
path := fmt.Sprintf("/proc/%d/ns/net", pid)
|
|
|
|
var sandbox libnetwork.Sandbox
|
|
|
|
search := libnetwork.SandboxContainerWalker(&sandbox, container.ID)
|
|
|
|
container.daemon.netController.WalkSandboxes(search)
|
|
|
|
if sandbox == nil {
|
2015-09-18 17:48:16 +00:00
|
|
|
return derr.ErrorCodeNoSandbox.WithArgs(container.ID)
|
2015-09-11 19:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sandbox.SetKey(path)
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) getIpcContainer() (*Container, error) {
|
2015-04-29 22:53:35 +00:00
|
|
|
containerID := container.hostConfig.IpcMode.Container()
|
2015-09-29 17:51:40 +00:00
|
|
|
c, err := container.daemon.Get(containerID)
|
2015-04-29 22:53:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !c.IsRunning() {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeIPCRunning
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) setupWorkingDirectory() error {
|
|
|
|
if container.Config.WorkingDir != "" {
|
|
|
|
container.Config.WorkingDir = filepath.Clean(container.Config.WorkingDir)
|
|
|
|
|
|
|
|
pth, err := container.GetResourcePath(container.Config.WorkingDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pthInfo, err := os.Stat(pth)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-11 18:29:29 +00:00
|
|
|
if err := system.MkdirAll(pth, 0755); err != nil {
|
2015-04-29 22:53:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pthInfo != nil && !pthInfo.IsDir() {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNotADir.WithArgs(container.Config.WorkingDir)
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) getNetworkedContainer() (*Container, error) {
|
2015-04-29 22:53:35 +00:00
|
|
|
parts := strings.SplitN(string(container.hostConfig.NetworkMode), ":", 2)
|
|
|
|
switch parts[0] {
|
|
|
|
case "container":
|
|
|
|
if len(parts) != 2 {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeParseContainer
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
2015-09-29 17:51:40 +00:00
|
|
|
nc, err := container.daemon.Get(parts[1])
|
2015-04-29 22:53:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if container == nc {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeJoinSelf
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
if !nc.IsRunning() {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeJoinRunning.WithArgs(parts[1])
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
return nc, nil
|
|
|
|
default:
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeModeNotContainer
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) releaseNetwork() {
|
2015-06-30 17:34:15 +00:00
|
|
|
if container.hostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
|
2015-04-29 22:53:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
sid := container.NetworkSettings.SandboxID
|
2015-06-21 00:09:36 +00:00
|
|
|
eid := container.NetworkSettings.EndpointID
|
|
|
|
nid := container.NetworkSettings.NetworkID
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-06-21 00:09:36 +00:00
|
|
|
container.NetworkSettings = &network.Settings{}
|
2015-05-06 22:39:29 +00:00
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
if sid == "" || nid == "" || eid == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := container.daemon.netController.SandboxByID(sid)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error locating sandbox id %s: %v", sid, err)
|
2015-06-24 17:23:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := container.daemon.netController.NetworkByID(nid)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error locating network id %s: %v", nid, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err := n.EndpointByID(eid)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error locating endpoint id %s: %v", eid, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-02 23:43:28 +00:00
|
|
|
if err := sb.Delete(); err != nil {
|
|
|
|
logrus.Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
|
|
|
|
return
|
2015-06-24 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In addition to leaving all endpoints, delete implicitly created endpoint
|
|
|
|
if container.Config.PublishService == "" {
|
2015-06-21 00:09:36 +00:00
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
logrus.Errorf("deleting endpoint failed: %v", err)
|
|
|
|
}
|
2015-05-06 22:39:29 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
2015-05-26 16:33:55 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) unmountVolumes(forceSyscall bool) error {
|
2015-05-26 21:20:55 +00:00
|
|
|
var volumeMounts []mountPoint
|
|
|
|
|
|
|
|
for _, mntPoint := range container.MountPoints {
|
|
|
|
dest, err := container.GetResourcePath(mntPoint.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
volumeMounts = append(volumeMounts, mountPoint{Destination: dest, Volume: mntPoint.Volume})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mnt := range container.networkMounts() {
|
|
|
|
dest, err := container.GetResourcePath(mnt.Destination)
|
2015-05-26 16:33:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-26 21:20:55 +00:00
|
|
|
volumeMounts = append(volumeMounts, mountPoint{Destination: dest})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, volumeMount := range volumeMounts {
|
2015-05-26 16:33:55 +00:00
|
|
|
if forceSyscall {
|
2015-05-26 21:20:55 +00:00
|
|
|
syscall.Unmount(volumeMount.Destination, 0)
|
2015-05-26 16:33:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 21:20:55 +00:00
|
|
|
if volumeMount.Volume != nil {
|
|
|
|
if err := volumeMount.Volume.Unmount(); err != nil {
|
2015-05-26 16:33:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 21:20:55 +00:00
|
|
|
|
2015-05-26 16:33:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-06-11 18:29:29 +00:00
|
|
|
|
2015-07-16 21:14:58 +00:00
|
|
|
func (container *Container) networkMounts() []execdriver.Mount {
|
|
|
|
var mounts []execdriver.Mount
|
2015-09-11 20:17:59 +00:00
|
|
|
shared := container.hostConfig.NetworkMode.IsContainer()
|
2015-07-16 21:14:58 +00:00
|
|
|
if container.ResolvConfPath != "" {
|
2015-09-11 20:17:59 +00:00
|
|
|
label.Relabel(container.ResolvConfPath, container.MountLabel, shared)
|
2015-07-24 18:23:20 +00:00
|
|
|
writable := !container.hostConfig.ReadonlyRootfs
|
|
|
|
if m, exists := container.MountPoints["/etc/resolv.conf"]; exists {
|
|
|
|
writable = m.RW
|
|
|
|
}
|
2015-07-16 21:14:58 +00:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.ResolvConfPath,
|
|
|
|
Destination: "/etc/resolv.conf",
|
2015-07-24 18:23:20 +00:00
|
|
|
Writable: writable,
|
2015-07-16 21:14:58 +00:00
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if container.HostnamePath != "" {
|
2015-09-11 20:17:59 +00:00
|
|
|
label.Relabel(container.HostnamePath, container.MountLabel, shared)
|
2015-07-24 18:23:20 +00:00
|
|
|
writable := !container.hostConfig.ReadonlyRootfs
|
|
|
|
if m, exists := container.MountPoints["/etc/hostname"]; exists {
|
|
|
|
writable = m.RW
|
|
|
|
}
|
2015-07-16 21:14:58 +00:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.HostnamePath,
|
|
|
|
Destination: "/etc/hostname",
|
2015-07-24 18:23:20 +00:00
|
|
|
Writable: writable,
|
2015-07-16 21:14:58 +00:00
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if container.HostsPath != "" {
|
2015-09-11 20:17:59 +00:00
|
|
|
label.Relabel(container.HostsPath, container.MountLabel, shared)
|
2015-07-24 18:23:20 +00:00
|
|
|
writable := !container.hostConfig.ReadonlyRootfs
|
|
|
|
if m, exists := container.MountPoints["/etc/hosts"]; exists {
|
|
|
|
writable = m.RW
|
|
|
|
}
|
2015-07-16 21:14:58 +00:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.HostsPath,
|
|
|
|
Destination: "/etc/hosts",
|
2015-07-24 18:23:20 +00:00
|
|
|
Writable: writable,
|
2015-07-16 21:14:58 +00:00
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return mounts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) addBindMountPoint(name, source, destination string, rw bool) {
|
|
|
|
container.MountPoints[destination] = &mountPoint{
|
|
|
|
Name: name,
|
|
|
|
Source: source,
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) addLocalMountPoint(name, destination string, rw bool) {
|
|
|
|
container.MountPoints[destination] = &mountPoint{
|
|
|
|
Name: name,
|
|
|
|
Driver: volume.DefaultDriverName,
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) addMountPointWithVolume(destination string, vol volume.Volume, rw bool) {
|
|
|
|
container.MountPoints[destination] = &mountPoint{
|
|
|
|
Name: vol.Name(),
|
|
|
|
Driver: vol.DriverName(),
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
Volume: vol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) isDestinationMounted(destination string) bool {
|
|
|
|
return container.MountPoints[destination] != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) prepareMountPoints() error {
|
|
|
|
for _, config := range container.MountPoints {
|
|
|
|
if len(config.Driver) > 0 {
|
2015-06-12 13:25:32 +00:00
|
|
|
v, err := container.daemon.createVolume(config.Name, config.Driver, nil)
|
2015-07-16 21:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Volume = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:25:32 +00:00
|
|
|
func (container *Container) removeMountPoints(rm bool) error {
|
|
|
|
var rmErrors []string
|
2015-07-16 21:14:58 +00:00
|
|
|
for _, m := range container.MountPoints {
|
2015-06-12 13:25:32 +00:00
|
|
|
if m.Volume == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
container.daemon.volumes.Decrement(m.Volume)
|
|
|
|
if rm {
|
2015-09-14 17:21:17 +00:00
|
|
|
err := container.daemon.volumes.Remove(m.Volume)
|
|
|
|
// ErrVolumeInUse is ignored because having this
|
|
|
|
// volume being referenced by othe container is
|
|
|
|
// not an error, but an implementation detail.
|
|
|
|
// This prevents docker from logging "ERROR: Volume in use"
|
|
|
|
// where there is another container using the volume.
|
2015-09-18 23:58:05 +00:00
|
|
|
if err != nil && err != store.ErrVolumeInUse {
|
2015-09-14 17:21:17 +00:00
|
|
|
rmErrors = append(rmErrors, err.Error())
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-12 13:25:32 +00:00
|
|
|
if len(rmErrors) > 0 {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeRemovingVolume.WithArgs(strings.Join(rmErrors, "\n"))
|
2015-06-12 13:25:32 +00:00
|
|
|
}
|
2015-07-16 21:14:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
|
|
|
|
func (container *Container) shmPath() (string, error) {
|
2015-08-26 12:00:01 +00:00
|
|
|
return container.getRootResourcePath("shm")
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
func (container *Container) mqueuePath() (string, error) {
|
2015-08-26 12:00:01 +00:00
|
|
|
return container.getRootResourcePath("mqueue")
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 21:28:27 +00:00
|
|
|
func (container *Container) hasMountFor(path string) bool {
|
|
|
|
_, exists := container.MountPoints[path]
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2015-08-03 22:05:34 +00:00
|
|
|
func (container *Container) setupIpcDirs() error {
|
2015-10-01 21:28:27 +00:00
|
|
|
if !container.hasMountFor("/dev/shm") {
|
|
|
|
shmPath, err := container.shmPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
|
2015-10-01 21:28:27 +00:00
|
|
|
if err := os.MkdirAll(shmPath, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
|
2015-10-01 21:28:27 +00:00
|
|
|
if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel("mode=1777,size=65536k", container.getMountLabel())); err != nil {
|
|
|
|
return fmt.Errorf("mounting shm tmpfs: %s", err)
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 21:28:27 +00:00
|
|
|
if !container.hasMountFor("/dev/mqueue") {
|
|
|
|
mqueuePath, err := container.mqueuePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
|
2015-10-01 21:28:27 +00:00
|
|
|
if err := os.MkdirAll(mqueuePath, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
|
2015-10-01 21:28:27 +00:00
|
|
|
if err := syscall.Mount("mqueue", mqueuePath, "mqueue", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), ""); err != nil {
|
|
|
|
return fmt.Errorf("mounting mqueue mqueue : %s", err)
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) unmountIpcMounts() error {
|
|
|
|
if container.hostConfig.IpcMode.IsContainer() || container.hostConfig.IpcMode.IsHost() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-26 12:00:01 +00:00
|
|
|
var errors []string
|
2015-10-01 21:28:27 +00:00
|
|
|
|
|
|
|
if !container.hasMountFor("/dev/shm") {
|
|
|
|
shmPath, err := container.shmPath()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2015-08-26 12:00:01 +00:00
|
|
|
errors = append(errors, err.Error())
|
2015-10-01 21:28:27 +00:00
|
|
|
} else {
|
|
|
|
if err := detachMounted(shmPath); err != nil {
|
|
|
|
logrus.Errorf("failed to umount %s: %v", shmPath, err)
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
|
2015-10-01 21:28:27 +00:00
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 21:28:27 +00:00
|
|
|
if !container.hasMountFor("/dev/mqueue") {
|
|
|
|
mqueuePath, err := container.mqueuePath()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2015-08-26 12:00:01 +00:00
|
|
|
errors = append(errors, err.Error())
|
2015-10-01 21:28:27 +00:00
|
|
|
} else {
|
|
|
|
if err := detachMounted(mqueuePath); err != nil {
|
|
|
|
logrus.Errorf("failed to umount %s: %v", mqueuePath, err)
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
2015-08-26 12:00:01 +00:00
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 12:00:01 +00:00
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf("failed to cleanup ipc mounts:\n%v", strings.Join(errors, "\n"))
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) ipcMounts() []execdriver.Mount {
|
|
|
|
var mounts []execdriver.Mount
|
2015-10-01 21:28:27 +00:00
|
|
|
|
|
|
|
if !container.hasMountFor("/dev/shm") {
|
|
|
|
label.SetFileLabel(container.ShmPath, container.MountLabel)
|
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.ShmPath,
|
|
|
|
Destination: "/dev/shm",
|
|
|
|
Writable: true,
|
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.hasMountFor("/dev/mqueue") {
|
|
|
|
label.SetFileLabel(container.MqueuePath, container.MountLabel)
|
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.MqueuePath,
|
|
|
|
Destination: "/dev/mqueue",
|
|
|
|
Writable: true,
|
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
return mounts
|
|
|
|
}
|
2015-08-26 12:00:01 +00:00
|
|
|
|
|
|
|
func detachMounted(path string) error {
|
|
|
|
return syscall.Unmount(path, syscall.MNT_DETACH)
|
|
|
|
}
|