2015-07-29 14:25:56 -04:00
|
|
|
// +build linux freebsd
|
2015-04-29 18:53:35 -04:00
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-05-06 18:39:29 -04:00
|
|
|
"net"
|
2015-04-29 18:53:35 -04:00
|
|
|
"os"
|
2015-05-06 18:39:29 -04:00
|
|
|
"path"
|
2015-04-29 18:53:35 -04:00
|
|
|
"path/filepath"
|
2015-05-06 18:39:29 -04:00
|
|
|
"strconv"
|
2015-04-29 18:53:35 -04:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-07-20 18:19:05 -04:00
|
|
|
"github.com/docker/docker/daemon/links"
|
2015-04-29 18:53:35 -04:00
|
|
|
"github.com/docker/docker/daemon/network"
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-11-11 16:53:38 -05:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-11-11 16:53:38 -05:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2015-06-30 12:41:01 -04:00
|
|
|
"github.com/docker/docker/pkg/nat"
|
2015-04-29 18:53:35 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-09-09 22:23:06 -04:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2015-06-11 14:29:29 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-04-29 18:53:35 -04:00
|
|
|
"github.com/docker/docker/pkg/ulimit"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/docker/utils"
|
2015-07-16 17:14:58 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2015-05-06 18:39:29 -04:00
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
2015-05-23 02:00:03 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/devices"
|
2015-07-16 17:14:58 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2015-04-29 18:53:35 -04:00
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04: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 18:53:35 -04:00
|
|
|
const DefaultPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// Container holds the fields specific to unixen implementations. See
|
|
|
|
// CommonContainer for standard fields common to all containers.
|
2015-04-29 18:53:35 -04:00
|
|
|
type Container struct {
|
|
|
|
CommonContainer
|
|
|
|
|
|
|
|
// Fields below here are platform specific.
|
2015-05-19 16:05:25 -04:00
|
|
|
activeLinks map[string]*links.Link
|
2015-07-16 17:14:58 -04:00
|
|
|
AppArmorProfile string
|
|
|
|
HostnamePath string
|
|
|
|
HostsPath string
|
2015-11-13 16:07:27 -05:00
|
|
|
ShmPath string
|
|
|
|
MqueuePath string
|
2015-07-16 17:14:58 -04:00
|
|
|
ResolvConfPath string
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func killProcessDirectly(container *Container) error {
|
|
|
|
if _, err := container.WaitStop(10 * time.Second); err != nil {
|
|
|
|
// Ensure that we don't kill ourselves
|
2015-11-02 18:25:26 -05:00
|
|
|
if pid := container.GetPID(); pid != 0 {
|
2015-04-29 18:53:35 -04: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-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) setupLinkedContainers(container *Container) ([]string, error) {
|
|
|
|
var env []string
|
2015-09-29 13:51:40 -04:00
|
|
|
children, err := daemon.children(container.Name)
|
2015-04-29 18:53:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-26 22:35:49 -04:00
|
|
|
bridgeSettings := container.NetworkSettings.Networks["bridge"]
|
|
|
|
if bridgeSettings == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:53:35 -04:00
|
|
|
if len(children) > 0 {
|
|
|
|
for linkAlias, child := range children {
|
|
|
|
if !child.IsRunning() {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeLinkNotRunning.WithArgs(child.Name, linkAlias)
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-26 22:35:49 -04:00
|
|
|
childBridgeSettings := child.NetworkSettings.Networks["bridge"]
|
|
|
|
if childBridgeSettings == nil {
|
2015-11-02 09:51:48 -05:00
|
|
|
return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
|
2015-10-26 22:35:49 -04:00
|
|
|
}
|
|
|
|
|
2015-07-20 18:19:05 -04:00
|
|
|
link := links.NewLink(
|
2015-10-26 22:35:49 -04:00
|
|
|
bridgeSettings.IPAddress,
|
|
|
|
childBridgeSettings.IPAddress,
|
2015-04-29 18:53:35 -04:00
|
|
|
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 14:56:26 -04:00
|
|
|
return devs, derr.ErrorCodeDeviceInfo.WithArgs(deviceMapping.PathOnHost, err)
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) populateCommand(c *Container, env []string) error {
|
2015-05-24 11:26:56 -04:00
|
|
|
var en *execdriver.Network
|
2015-06-03 20:31:22 -04:00
|
|
|
if !c.Config.NetworkDisabled {
|
2015-09-11 15:05:57 -04:00
|
|
|
en = &execdriver.Network{}
|
2015-11-03 13:25:09 -05:00
|
|
|
if !daemon.execDriver.SupportsHooks() || c.hostConfig.NetworkMode.IsHost() {
|
2015-09-11 15:05:57 -04:00
|
|
|
en.NamespacePath = c.NetworkSettings.SandboxKey
|
2015-05-24 11:26:56 -04:00
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
if c.hostConfig.NetworkMode.IsContainer() {
|
|
|
|
nc, err := daemon.getNetworkedContainer(c.ID, c.hostConfig.NetworkMode.ConnectedContainer())
|
2015-05-24 11:26:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
en.ContainerID = nc.ID
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc := &execdriver.Ipc{}
|
2015-08-03 18:05:34 -04: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 18:53:35 -04:00
|
|
|
|
|
|
|
if c.hostConfig.IpcMode.IsContainer() {
|
2015-11-03 13:25:09 -05:00
|
|
|
ic, err := daemon.getIpcContainer(c)
|
2015-04-29 18:53:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ipc.ContainerID = ic.ID
|
2015-08-03 18:05:34 -04:00
|
|
|
c.ShmPath = ic.ShmPath
|
|
|
|
c.MqueuePath = ic.MqueuePath
|
2015-04-29 18:53:35 -04:00
|
|
|
} else {
|
|
|
|
ipc.HostIpc = c.hostConfig.IpcMode.IsHost()
|
2015-08-03 18:05:34 -04:00
|
|
|
if ipc.HostIpc {
|
2015-10-14 10:50:43 -04:00
|
|
|
if _, err := os.Stat("/dev/shm"); err != nil {
|
2015-11-08 13:05:34 -05:00
|
|
|
return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
|
2015-10-14 10:50:43 -04:00
|
|
|
}
|
|
|
|
if _, err := os.Stat("/dev/mqueue"); err != nil {
|
2015-11-08 13:05:34 -05:00
|
|
|
return fmt.Errorf("/dev/mqueue is not mounted, but must be for --ipc=host")
|
2015-10-14 10:50:43 -04:00
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
c.ShmPath = "/dev/shm"
|
|
|
|
c.MqueuePath = "/dev/mqueue"
|
|
|
|
}
|
2015-04-29 18:53:35 -04: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 11:29:56 -04:00
|
|
|
allowedDevices := mergeDevices(configs.DefaultAllowedDevices, userSpecifiedDevices)
|
|
|
|
|
|
|
|
autoCreatedDevices := mergeDevices(configs.DefaultAutoCreatedDevices, userSpecifiedDevices)
|
2015-04-29 18:53:35 -04:00
|
|
|
|
|
|
|
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-11-03 13:25:09 -05:00
|
|
|
for name, ul := range daemon.configStore.Ulimits {
|
2015-04-29 18:53:35 -04:00
|
|
|
if _, exists := ulIdx[name]; !exists {
|
|
|
|
ulimits = append(ulimits, ul)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:34:20 -04:00
|
|
|
weightDevices, err := getBlkioWeightDevices(c.hostConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:53:35 -04:00
|
|
|
for _, limit := range ulimits {
|
|
|
|
rl, err := limit.GetRlimit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rlimits = append(rlimits, rl)
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := &execdriver.Resources{
|
2015-10-05 13:11:10 -04:00
|
|
|
CommonResources: execdriver.CommonResources{
|
|
|
|
Memory: c.hostConfig.Memory,
|
|
|
|
MemoryReservation: c.hostConfig.MemoryReservation,
|
|
|
|
CPUShares: c.hostConfig.CPUShares,
|
|
|
|
BlkioWeight: c.hostConfig.BlkioWeight,
|
|
|
|
},
|
2015-06-11 20:34:20 -04:00
|
|
|
MemorySwap: c.hostConfig.MemorySwap,
|
|
|
|
KernelMemory: c.hostConfig.KernelMemory,
|
|
|
|
CpusetCpus: c.hostConfig.CpusetCpus,
|
|
|
|
CpusetMems: c.hostConfig.CpusetMems,
|
|
|
|
CPUPeriod: c.hostConfig.CPUPeriod,
|
|
|
|
CPUQuota: c.hostConfig.CPUQuota,
|
|
|
|
Rlimits: rlimits,
|
|
|
|
BlkioWeightDevice: weightDevices,
|
|
|
|
OomKillDisable: c.hostConfig.OomKillDisable,
|
|
|
|
MemorySwappiness: -1,
|
2015-07-29 16:04:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.hostConfig.MemorySwappiness != nil {
|
|
|
|
resources.MemorySwappiness = *c.hostConfig.MemorySwappiness
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
processConfig := execdriver.ProcessConfig{
|
2015-11-01 10:53:15 -05:00
|
|
|
CommonProcessConfig: execdriver.CommonProcessConfig{
|
|
|
|
Entrypoint: c.Path,
|
|
|
|
Arguments: c.Args,
|
|
|
|
Tty: c.Config.Tty,
|
|
|
|
},
|
2015-04-29 18:53:35 -04:00
|
|
|
Privileged: c.hostConfig.Privileged,
|
|
|
|
User: c.Config.User,
|
|
|
|
}
|
|
|
|
|
|
|
|
processConfig.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
|
|
|
processConfig.Env = env
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
remappedRoot := &execdriver.User{}
|
2015-11-03 13:25:09 -05:00
|
|
|
rootUID, rootGID := daemon.GetRemappedUIDGID()
|
2015-10-08 11:51:41 -04:00
|
|
|
if rootUID != 0 {
|
|
|
|
remappedRoot.UID = rootUID
|
|
|
|
remappedRoot.GID = rootGID
|
|
|
|
}
|
2015-11-03 13:25:09 -05:00
|
|
|
uidMap, gidMap := daemon.GetUIDGIDMaps()
|
2015-10-08 11:51:41 -04:00
|
|
|
|
2015-04-29 18:53:35 -04:00
|
|
|
c.command = &execdriver.Command{
|
2015-10-05 17:27:39 -04:00
|
|
|
CommonCommand: execdriver.CommonCommand{
|
|
|
|
ID: c.ID,
|
|
|
|
InitPath: "/.dockerinit",
|
|
|
|
MountLabel: c.getMountLabel(),
|
|
|
|
Network: en,
|
|
|
|
ProcessConfig: processConfig,
|
|
|
|
ProcessLabel: c.getProcessLabel(),
|
|
|
|
Rootfs: c.rootfsPath(),
|
|
|
|
Resources: resources,
|
|
|
|
WorkingDir: c.Config.WorkingDir,
|
|
|
|
},
|
2015-04-29 18:53:35 -04:00
|
|
|
AllowedDevices: allowedDevices,
|
2015-10-05 17:27:39 -04:00
|
|
|
AppArmorProfile: c.AppArmorProfile,
|
2015-04-29 18:53:35 -04:00
|
|
|
AutoCreatedDevices: autoCreatedDevices,
|
2015-07-01 13:59:18 -04:00
|
|
|
CapAdd: c.hostConfig.CapAdd.Slice(),
|
|
|
|
CapDrop: c.hostConfig.CapDrop.Slice(),
|
2015-10-05 17:27:39 -04:00
|
|
|
CgroupParent: c.hostConfig.CgroupParent,
|
|
|
|
GIDMapping: gidMap,
|
2015-06-17 14:39:17 -04:00
|
|
|
GroupAdd: c.hostConfig.GroupAdd,
|
2015-10-05 17:27:39 -04:00
|
|
|
Ipc: ipc,
|
|
|
|
Pid: pid,
|
|
|
|
ReadonlyRootfs: c.hostConfig.ReadonlyRootfs,
|
|
|
|
RemappedRoot: remappedRoot,
|
|
|
|
UIDMapping: uidMap,
|
|
|
|
UTS: uts,
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-29 11:29:56 -04: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-11-02 20:06:09 -05:00
|
|
|
// getSize returns the real size & virtual size of the container.
|
|
|
|
func (daemon *Daemon) getSize(container *Container) (int64, int64) {
|
2015-04-29 18:53:35 -04:00
|
|
|
var (
|
|
|
|
sizeRw, sizeRootfs int64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-11-02 20:06:09 -05:00
|
|
|
if err := daemon.Mount(container); err != nil {
|
2015-04-29 18:53:35 -04:00
|
|
|
logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
|
|
|
|
return sizeRw, sizeRootfs
|
|
|
|
}
|
2015-11-02 20:06:09 -05:00
|
|
|
defer daemon.Unmount(container)
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
sizeRw, err = container.rwlayer.Size()
|
2015-04-29 18:53:35 -04:00
|
|
|
if err != nil {
|
2015-11-02 20:06:09 -05:00
|
|
|
logrus.Errorf("Driver %s couldn't return diff size of container %s: %s", daemon.driver, container.ID, err)
|
2015-04-29 18:53:35 -04:00
|
|
|
// FIXME: GetSize should return an error. Not changing it now in case
|
|
|
|
// there is a side-effect.
|
|
|
|
sizeRw = -1
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
if parent := container.rwlayer.Parent(); parent != nil {
|
|
|
|
sizeRootfs, err = parent.Size()
|
|
|
|
if err != nil {
|
2015-04-29 18:53:35 -04:00
|
|
|
sizeRootfs = -1
|
2015-11-18 17:20:54 -05:00
|
|
|
} else if sizeRw != -1 {
|
|
|
|
sizeRootfs += sizeRw
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sizeRw, sizeRootfs
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:54:00 -04: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 18:39:29 -04:00
|
|
|
func (container *Container) buildHostnameFile() error {
|
2015-07-30 17:01:53 -04:00
|
|
|
hostnamePath, err := container.getRootResourcePath("hostname")
|
2015-05-06 18:39:29 -04: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-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) buildSandboxOptions(container *Container, n libnetwork.Network) ([]libnetwork.SandboxOption, error) {
|
2015-05-06 18:39:29 -04:00
|
|
|
var (
|
2015-09-02 19:43:28 -04:00
|
|
|
sboxOptions []libnetwork.SandboxOption
|
2015-05-06 18:39:29 -04:00
|
|
|
err error
|
|
|
|
dns []string
|
|
|
|
dnsSearch []string
|
2015-08-31 14:47:25 -04:00
|
|
|
dnsOptions []string
|
2015-05-06 18:39:29 -04:00
|
|
|
)
|
|
|
|
|
2015-09-02 19:43:28 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionHostname(container.Config.Hostname),
|
|
|
|
libnetwork.OptionDomainname(container.Config.Domainname))
|
2015-05-06 18:39:29 -04:00
|
|
|
|
|
|
|
if container.hostConfig.NetworkMode.IsHost() {
|
2015-09-02 19:43:28 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionUseDefaultSandbox())
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionOriginHostsPath("/etc/hosts"))
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"))
|
2015-11-03 13:25:09 -05:00
|
|
|
} else if daemon.execDriver.SupportsHooks() {
|
2015-09-11 15:05:57 -04:00
|
|
|
// OptionUseExternalKey is mandatory for userns support.
|
|
|
|
// But optional for non-userns support
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionUseExternalKey())
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
container.HostsPath, err = container.getRootResourcePath("hosts")
|
2015-05-06 18:39:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-02 19:43:28 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
container.ResolvConfPath, err = container.getRootResourcePath("resolv.conf")
|
2015-05-06 18:39:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-02 19:43:28 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
if len(container.hostConfig.DNS) > 0 {
|
|
|
|
dns = container.hostConfig.DNS
|
2015-11-03 13:25:09 -05:00
|
|
|
} else if len(daemon.configStore.DNS) > 0 {
|
|
|
|
dns = daemon.configStore.DNS
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dns {
|
2015-09-02 19:43:28 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNS(d))
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
if len(container.hostConfig.DNSSearch) > 0 {
|
|
|
|
dnsSearch = container.hostConfig.DNSSearch
|
2015-11-03 13:25:09 -05:00
|
|
|
} else if len(daemon.configStore.DNSSearch) > 0 {
|
|
|
|
dnsSearch = daemon.configStore.DNSSearch
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range dnsSearch {
|
2015-09-02 19:43:28 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNSSearch(ds))
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:47:25 -04:00
|
|
|
if len(container.hostConfig.DNSOptions) > 0 {
|
|
|
|
dnsOptions = container.hostConfig.DNSOptions
|
2015-11-03 13:25:09 -05:00
|
|
|
} else if len(daemon.configStore.DNSOptions) > 0 {
|
|
|
|
dnsOptions = daemon.configStore.DNSOptions
|
2015-08-31 14:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range dnsOptions {
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNSOptions(ds))
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:39:29 -04: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 19:43:28 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(name, a.Addr))
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
for _, extraHost := range container.hostConfig.ExtraHosts {
|
|
|
|
// allow IPv6 addresses in extra hosts; only split on first ":"
|
|
|
|
parts := strings.SplitN(extraHost, ":", 2)
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(parts[0], parts[1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link feature is supported only for the default bridge network.
|
|
|
|
// return if this call to build join options is not for default bridge network
|
|
|
|
if n.Name() != "bridge" {
|
|
|
|
return sboxOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, _ := container.getEndpointInNetwork(n)
|
|
|
|
if ep == nil {
|
|
|
|
return sboxOptions, nil
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
var childEndpoints, parentEndpoints []string
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
children, err := daemon.children(container.Name)
|
2015-05-06 18:39:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for linkAlias, child := range children {
|
2015-10-21 12:08:19 -04:00
|
|
|
if !isLinkable(child) {
|
|
|
|
return nil, fmt.Errorf("Cannot link to %s, as it does not belong to the default network", child.Name)
|
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
_, 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-10-26 22:35:49 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(aliasList, child.NetworkSettings.Networks["bridge"].IPAddress))
|
2015-09-25 06:19:17 -04:00
|
|
|
cEndpoint, _ := child.getEndpointInNetwork(n)
|
|
|
|
if cEndpoint != nil && cEndpoint.ID() != "" {
|
|
|
|
childEndpoints = append(childEndpoints, cEndpoint.ID())
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 22:35:49 -04:00
|
|
|
bridgeSettings := container.NetworkSettings.Networks["bridge"]
|
2015-11-03 13:25:09 -05:00
|
|
|
refs := daemon.containerGraph().RefPaths(container.ID)
|
2015-05-06 18:39:29 -04:00
|
|
|
for _, ref := range refs {
|
|
|
|
if ref.ParentID == "0" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
c, err := daemon.Get(ref.ParentID)
|
2015-05-06 18:39:29 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
if c != nil && !daemon.configStore.DisableBridge && container.hostConfig.NetworkMode.IsPrivate() {
|
2015-10-26 22:35:49 -04:00
|
|
|
logrus.Debugf("Update /etc/hosts of %s for alias %s with ip %s", c.ID, ref.Name, bridgeSettings.IPAddress)
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionParentUpdate(c.ID, ref.Name, bridgeSettings.IPAddress))
|
2015-09-25 06:19:17 -04:00
|
|
|
if ep.ID() != "" {
|
|
|
|
parentEndpoints = append(parentEndpoints, ep.ID())
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
linkOptions := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"ParentEndpoints": parentEndpoints,
|
|
|
|
"ChildEndpoints": childEndpoints,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:43:28 -04:00
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionGeneric(linkOptions))
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-09-02 19:43:28 -04:00
|
|
|
return sboxOptions, nil
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
2015-10-21 12:08:19 -04:00
|
|
|
func isLinkable(child *Container) bool {
|
|
|
|
// A container is linkable only if it belongs to the default network
|
2015-10-26 22:35:49 -04:00
|
|
|
_, ok := child.NetworkSettings.Networks["bridge"]
|
|
|
|
return ok
|
2015-10-21 12:08:19 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
func (container *Container) getEndpointInNetwork(n libnetwork.Network) (libnetwork.Endpoint, error) {
|
|
|
|
endpointName := strings.TrimPrefix(container.Name, "/")
|
|
|
|
return n.EndpointByName(endpointName)
|
|
|
|
}
|
|
|
|
|
2015-09-09 16:18:13 -04:00
|
|
|
func (container *Container) buildPortMapInfo(ep libnetwork.Endpoint, networkSettings *network.Settings) (*network.Settings, error) {
|
2015-05-06 18:39:29 -04:00
|
|
|
if ep == nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeEmptyEndpoint
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if networkSettings == nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeEmptyNetwork
|
2015-05-06 18:39:29 -04: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
|
|
|
|
}
|
|
|
|
|
2015-11-06 08:34:49 -05:00
|
|
|
if networkSettings.Ports == nil {
|
|
|
|
networkSettings.Ports = nat.PortMap{}
|
|
|
|
}
|
2015-06-04 01:47:08 -04:00
|
|
|
|
|
|
|
if expData, ok := driverInfo[netlabel.ExposedPorts]; ok {
|
|
|
|
if exposedPorts, ok := expData.([]types.TransportPort); ok {
|
|
|
|
for _, tp := range exposedPorts {
|
2015-07-15 23:45:48 -04:00
|
|
|
natPort, err := nat.NewPort(tp.Proto.String(), strconv.Itoa(int(tp.Port)))
|
|
|
|
if err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeParsingPort.WithArgs(tp.Port, err)
|
2015-07-15 23:45:48 -04:00
|
|
|
}
|
2015-06-04 01:47:08 -04:00
|
|
|
networkSettings.Ports[natPort] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
mapData, ok := driverInfo[netlabel.PortMap]
|
|
|
|
if !ok {
|
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 02:00:03 -04:00
|
|
|
if portMapping, ok := mapData.([]types.PortBinding); ok {
|
2015-05-06 18:39:29 -04:00
|
|
|
for _, pp := range portMapping {
|
2015-07-15 23:45:48 -04:00
|
|
|
natPort, err := nat.NewPort(pp.Proto.String(), strconv.Itoa(int(pp.Port)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-21 09:43:32 -04:00
|
|
|
natBndg := nat.PortBinding{HostIP: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))}
|
2015-05-06 18:39:29 -04:00
|
|
|
networkSettings.Ports[natPort] = append(networkSettings.Ports[natPort], natBndg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:00:49 -04:00
|
|
|
func (container *Container) buildEndpointInfo(n libnetwork.Network, ep libnetwork.Endpoint, networkSettings *network.Settings) (*network.Settings, error) {
|
2015-05-06 18:39:29 -04:00
|
|
|
if ep == nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeEmptyEndpoint
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if networkSettings == nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeEmptyNetwork
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
epInfo := ep.Info()
|
|
|
|
if epInfo == nil {
|
|
|
|
// It is not an error to get an empty endpoint info
|
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 22:35:49 -04:00
|
|
|
if _, ok := networkSettings.Networks[n.Name()]; !ok {
|
|
|
|
networkSettings.Networks[n.Name()] = new(network.EndpointSettings)
|
|
|
|
}
|
2015-10-26 08:00:49 -04:00
|
|
|
networkSettings.Networks[n.Name()].EndpointID = ep.ID()
|
|
|
|
|
2015-09-14 14:04:12 -04:00
|
|
|
iface := epInfo.Iface()
|
|
|
|
if iface == nil {
|
2015-05-06 18:39:29 -04:00
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:25:29 -04:00
|
|
|
if iface.MacAddress() != nil {
|
|
|
|
networkSettings.Networks[n.Name()].MacAddress = iface.MacAddress().String()
|
|
|
|
}
|
|
|
|
|
2015-10-10 12:43:03 -04:00
|
|
|
if iface.Address() != nil {
|
|
|
|
ones, _ := iface.Address().Mask.Size()
|
2015-10-26 08:00:49 -04:00
|
|
|
networkSettings.Networks[n.Name()].IPAddress = iface.Address().IP.String()
|
|
|
|
networkSettings.Networks[n.Name()].IPPrefixLen = ones
|
2015-10-10 12:43:03 -04:00
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-10-10 12:43:03 -04:00
|
|
|
if iface.AddressIPv6() != nil && iface.AddressIPv6().IP.To16() != nil {
|
2015-05-06 18:39:29 -04:00
|
|
|
onesv6, _ := iface.AddressIPv6().Mask.Size()
|
2015-10-26 08:00:49 -04:00
|
|
|
networkSettings.Networks[n.Name()].GlobalIPv6Address = iface.AddressIPv6().IP.String()
|
|
|
|
networkSettings.Networks[n.Name()].GlobalIPv6PrefixLen = onesv6
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
return networkSettings, nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:00:49 -04:00
|
|
|
func (container *Container) updateJoinInfo(n libnetwork.Network, ep libnetwork.Endpoint) error {
|
2015-10-29 12:25:29 -04:00
|
|
|
if _, err := container.buildPortMapInfo(ep, container.NetworkSettings); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
epInfo := ep.Info()
|
|
|
|
if epInfo == nil {
|
|
|
|
// It is not an error to get an empty endpoint info
|
2015-04-29 18:53:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-27 21:47:12 -04:00
|
|
|
if epInfo.Gateway() != nil {
|
|
|
|
container.NetworkSettings.Networks[n.Name()].Gateway = epInfo.Gateway().String()
|
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
if epInfo.GatewayIPv6().To16() != nil {
|
2015-10-26 08:00:49 -04:00
|
|
|
container.NetworkSettings.Networks[n.Name()].IPv6Gateway = epInfo.GatewayIPv6().String()
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) updateNetworkSettings(container *Container, n libnetwork.Network) error {
|
2015-09-25 06:19:17 -04:00
|
|
|
if container.NetworkSettings == nil {
|
2015-10-26 08:00:49 -04:00
|
|
|
container.NetworkSettings = &network.Settings{Networks: make(map[string]*network.EndpointSettings)}
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-11-08 21:19:53 -05:00
|
|
|
if !container.hostConfig.NetworkMode.IsHost() && runconfig.NetworkMode(n.Type()).IsHost() {
|
|
|
|
return runconfig.ErrConflictHostNetwork
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:00:49 -04:00
|
|
|
for s := range container.NetworkSettings.Networks {
|
2015-11-03 13:25:09 -05:00
|
|
|
sn, err := daemon.FindNetwork(s)
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if sn.Name() == n.Name() {
|
|
|
|
// Avoid duplicate config
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !runconfig.NetworkMode(sn.Type()).IsPrivate() ||
|
|
|
|
!runconfig.NetworkMode(n.Type()).IsPrivate() {
|
|
|
|
return runconfig.ErrConflictSharedNetwork
|
|
|
|
}
|
|
|
|
if runconfig.NetworkMode(sn.Name()).IsNone() ||
|
|
|
|
runconfig.NetworkMode(n.Name()).IsNone() {
|
|
|
|
return runconfig.ErrConflictNoNetwork
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 08:00:49 -04:00
|
|
|
container.NetworkSettings.Networks[n.Name()] = new(network.EndpointSettings)
|
2015-09-25 06:19:17 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
func (daemon *Daemon) updateEndpointNetworkSettings(container *Container, n libnetwork.Network, ep libnetwork.Endpoint) error {
|
2015-10-29 12:25:29 -04:00
|
|
|
networkSettings, err := container.buildEndpointInfo(n, ep, container.NetworkSettings)
|
2015-05-06 18:39:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.hostConfig.NetworkMode == runconfig.NetworkMode("bridge") {
|
2015-11-03 14:25:22 -05:00
|
|
|
networkSettings.Bridge = daemon.configStore.Bridge.Iface
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:43:28 -04:00
|
|
|
func (container *Container) updateSandboxNetworkSettings(sb libnetwork.Sandbox) error {
|
|
|
|
container.NetworkSettings.SandboxID = sb.ID()
|
|
|
|
container.NetworkSettings.SandboxKey = sb.Key()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:19:05 -04:00
|
|
|
// UpdateNetwork is used to update the container's network (e.g. when linked containers
|
|
|
|
// get removed/unlinked).
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) updateNetwork(container *Container) error {
|
|
|
|
ctrl := daemon.netController
|
2015-09-02 19:43:28 -04:00
|
|
|
sid := container.NetworkSettings.SandboxID
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-09-02 19:43:28 -04:00
|
|
|
sb, err := ctrl.SandboxByID(sid)
|
2015-05-06 18:39:29 -04:00
|
|
|
if err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeNoSandbox.WithArgs(sid, err)
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
// Find if container is connected to the default bridge network
|
|
|
|
var n libnetwork.Network
|
2015-10-26 08:00:49 -04:00
|
|
|
for name := range container.NetworkSettings.Networks {
|
2015-11-03 13:25:09 -05:00
|
|
|
sn, err := daemon.FindNetwork(name)
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sn.Name() == "bridge" {
|
|
|
|
n = sn
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == nil {
|
|
|
|
// Not connected to the default bridge network; Nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
options, err := daemon.buildSandboxOptions(container, n)
|
2015-05-06 18:39:29 -04:00
|
|
|
if err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeNetworkUpdate.WithArgs(err)
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-02 19:43:28 -04:00
|
|
|
if err := sb.Refresh(options...); err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeNetworkRefresh.WithArgs(sid, err)
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-22 11:41:50 -04:00
|
|
|
func (container *Container) buildCreateEndpointOptions(n libnetwork.Network) ([]libnetwork.EndpointOption, error) {
|
2015-05-06 18:39:29 -04:00
|
|
|
var (
|
|
|
|
portSpecs = make(nat.PortSet)
|
|
|
|
bindings = make(nat.PortMap)
|
2015-05-23 02:00:03 -04:00
|
|
|
pbList []types.PortBinding
|
|
|
|
exposeList []types.TransportPort
|
2015-05-06 18:39:29 -04:00
|
|
|
createOptions []libnetwork.EndpointOption
|
|
|
|
)
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-06 08:34:49 -05:00
|
|
|
if n.Name() == "bridge" || container.NetworkSettings.IsAnonymousEndpoint {
|
|
|
|
createOptions = append(createOptions, libnetwork.CreateOptionAnonymous())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other configs are applicable only for the endpoint in the network
|
|
|
|
// to which container was connected to on docker run.
|
|
|
|
if n.Name() != container.hostConfig.NetworkMode.NetworkName() &&
|
|
|
|
!(n.Name() == "bridge" && container.hostConfig.NetworkMode.IsDefault()) {
|
|
|
|
return createOptions, nil
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:53:35 -04: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 09:43:32 -04:00
|
|
|
HostIP: bb.HostIP,
|
2015-04-29 18:53:35 -04: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 02:00:03 -04:00
|
|
|
expose := types.TransportPort{}
|
|
|
|
expose.Proto = types.ParseProtocol(port.Proto())
|
2015-05-06 18:39:29 -04:00
|
|
|
expose.Port = uint16(port.Int())
|
|
|
|
exposeList = append(exposeList, expose)
|
|
|
|
|
2015-05-23 02:00:03 -04:00
|
|
|
pb := types.PortBinding{Port: expose.Port, Proto: expose.Proto}
|
2015-05-06 18:39:29 -04:00
|
|
|
binding := bindings[port]
|
|
|
|
for i := 0; i < len(binding); i++ {
|
|
|
|
pbCopy := pb.GetCopy()
|
2015-07-15 23:45:48 -04:00
|
|
|
newP, err := nat.NewPort(nat.SplitProtoPort(binding[i].HostPort))
|
2015-05-01 15:35:26 -04:00
|
|
|
var portStart, portEnd int
|
|
|
|
if err == nil {
|
|
|
|
portStart, portEnd, err = newP.Range()
|
|
|
|
}
|
2015-07-15 23:45:48 -04:00
|
|
|
if err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeHostPort.WithArgs(binding[i].HostPort, err)
|
2015-07-15 23:45:48 -04:00
|
|
|
}
|
2015-05-01 15:35:26 -04:00
|
|
|
pbCopy.HostPort = uint16(portStart)
|
|
|
|
pbCopy.HostPortEnd = uint16(portEnd)
|
2015-07-21 09:43:32 -04:00
|
|
|
pbCopy.HostIP = net.ParseIP(binding[i].HostIP)
|
2015-05-06 18:39:29 -04:00
|
|
|
pbList = append(pbList, pbCopy)
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
if container.hostConfig.PublishAllPorts && len(binding) == 0 {
|
|
|
|
pbList = append(pbList, pb)
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
createOptions = append(createOptions,
|
|
|
|
libnetwork.CreateOptionPortMapping(pbList),
|
|
|
|
libnetwork.CreateOptionExposedPorts(exposeList))
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
if container.Config.MacAddress != "" {
|
|
|
|
mac, err := net.ParseMAC(container.Config.MacAddress)
|
2015-04-29 18:53:35 -04:00
|
|
|
if err != nil {
|
2015-05-06 18:39:29 -04:00
|
|
|
return nil, err
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
genericOption := options.Generic{
|
|
|
|
netlabel.MacAddress: mac,
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-05-06 18:39:29 -04:00
|
|
|
return createOptions, nil
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) allocateNetwork(container *Container) error {
|
|
|
|
controller := daemon.netController
|
2015-11-01 13:32:37 -05:00
|
|
|
|
|
|
|
// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
|
|
|
|
if err := controller.SandboxDestroy(container.ID); err != nil {
|
|
|
|
logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
|
2015-10-29 21:56:56 -04:00
|
|
|
}
|
2015-11-01 13:32:37 -05:00
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
updateSettings := false
|
2015-10-26 08:00:49 -04:00
|
|
|
if len(container.NetworkSettings.Networks) == 0 {
|
2015-09-25 06:19:17 -04:00
|
|
|
mode := container.hostConfig.NetworkMode
|
|
|
|
if container.Config.NetworkDisabled || mode.IsContainer() {
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
networkName := mode.NetworkName()
|
|
|
|
if mode.IsDefault() {
|
2015-06-15 01:31:09 -04:00
|
|
|
networkName = controller.Config().Daemon.DefaultNetwork
|
|
|
|
}
|
2015-10-30 23:32:03 -04:00
|
|
|
if mode.IsUserDefined() {
|
2015-11-03 13:25:09 -05:00
|
|
|
n, err := daemon.FindNetwork(networkName)
|
2015-10-30 23:32:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
networkName = n.Name()
|
|
|
|
}
|
2015-10-26 08:00:49 -04:00
|
|
|
container.NetworkSettings.Networks = make(map[string]*network.EndpointSettings)
|
|
|
|
container.NetworkSettings.Networks[networkName] = new(network.EndpointSettings)
|
2015-09-25 06:19:17 -04:00
|
|
|
updateSettings = true
|
2015-06-15 01:31:09 -04:00
|
|
|
}
|
|
|
|
|
2015-10-26 08:00:49 -04:00
|
|
|
for n := range container.NetworkSettings.Networks {
|
2015-11-03 13:25:09 -05:00
|
|
|
if err := daemon.connectToNetwork(container, n, updateSettings); err != nil {
|
2015-10-19 17:59:23 -04:00
|
|
|
return err
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
2015-06-30 13:34:15 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
return container.writeHostConfig()
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) getNetworkSandbox(container *Container) libnetwork.Sandbox {
|
2015-10-29 21:56:56 -04:00
|
|
|
var sb libnetwork.Sandbox
|
2015-11-03 13:25:09 -05:00
|
|
|
daemon.netController.WalkSandboxes(func(s libnetwork.Sandbox) bool {
|
2015-10-29 21:56:56 -04:00
|
|
|
if s.ContainerID() == container.ID {
|
|
|
|
sb = s
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return sb
|
|
|
|
}
|
|
|
|
|
2015-11-10 04:47:32 -05:00
|
|
|
// ConnectToNetwork connects a container to a network
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) ConnectToNetwork(container *Container, idOrName string) error {
|
2015-09-25 06:19:17 -04:00
|
|
|
if !container.Running {
|
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
2015-05-20 08:20:19 -04:00
|
|
|
}
|
2015-11-03 13:25:09 -05:00
|
|
|
if err := daemon.connectToNetwork(container, idOrName, true); err != nil {
|
2015-10-30 18:06:44 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := container.toDiskLocking(); err != nil {
|
|
|
|
return fmt.Errorf("Error saving container to disk: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
2015-05-20 08:20:19 -04:00
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) connectToNetwork(container *Container, idOrName string, updateSettings bool) (err error) {
|
2015-09-25 06:19:17 -04:00
|
|
|
if container.hostConfig.NetworkMode.IsContainer() {
|
|
|
|
return runconfig.ErrConflictSharedNetwork
|
2015-06-20 00:15:51 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
if runconfig.NetworkMode(idOrName).IsBridge() &&
|
2015-11-03 13:25:09 -05:00
|
|
|
daemon.configStore.DisableBridge {
|
2015-09-25 06:19:17 -04:00
|
|
|
container.Config.NetworkDisabled = true
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-20 00:15:51 -04:00
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
controller := daemon.netController
|
2015-09-02 19:43:28 -04:00
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
n, err := daemon.FindNetwork(idOrName)
|
2015-05-06 18:39:29 -04:00
|
|
|
if err != nil {
|
2015-09-25 06:19:17 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-06-15 01:31:09 -04:00
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
if updateSettings {
|
2015-11-03 13:25:09 -05:00
|
|
|
if err := daemon.updateNetworkSettings(container, n); err != nil {
|
2015-05-20 08:20:19 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
ep, err := container.getEndpointInNetwork(n)
|
2015-10-19 18:24:26 -04:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("container already connected to network %s", idOrName)
|
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-10-19 18:24:26 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrNoSuchEndpoint); !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-22 11:41:50 -04:00
|
|
|
createOptions, err := container.buildCreateEndpointOptions(n)
|
2015-10-19 18:24:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-15 01:31:09 -04:00
|
|
|
|
2015-10-19 18:24:26 -04:00
|
|
|
endpointName := strings.TrimPrefix(container.Name, "/")
|
|
|
|
ep, err = n.CreateEndpoint(endpointName, createOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2015-06-15 01:31:09 -04:00
|
|
|
if err != nil {
|
2015-10-19 18:24:26 -04:00
|
|
|
if e := ep.Delete(); e != nil {
|
|
|
|
logrus.Warnf("Could not rollback container connection to network %s", idOrName)
|
|
|
|
}
|
2015-06-15 01:31:09 -04:00
|
|
|
}
|
2015-10-19 18:24:26 -04:00
|
|
|
}()
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil {
|
2015-05-06 18:39:29 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
sb := daemon.getNetworkSandbox(container)
|
2015-09-02 19:43:28 -04:00
|
|
|
if sb == nil {
|
2015-11-03 13:25:09 -05:00
|
|
|
options, err := daemon.buildSandboxOptions(container, n)
|
2015-09-02 19:43:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sb, err = controller.NewSandbox(container.ID, options...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-10-19 18:24:26 -04:00
|
|
|
container.updateSandboxNetworkSettings(sb)
|
|
|
|
}
|
2015-09-02 19:43:28 -04:00
|
|
|
|
|
|
|
if err := ep.Join(sb); err != nil {
|
2015-05-06 18:39:29 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:00:49 -04:00
|
|
|
if err := container.updateJoinInfo(n, ep); err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeJoinInfo.WithArgs(err)
|
2015-05-06 18:39:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) initializeNetworking(container *Container) error {
|
2015-05-06 18:39:29 -04:00
|
|
|
var err error
|
|
|
|
|
2015-04-29 18:53:35 -04:00
|
|
|
if container.hostConfig.NetworkMode.IsContainer() {
|
|
|
|
// we need to get the hosts files from the container to join
|
2015-11-03 13:25:09 -05:00
|
|
|
nc, err := daemon.getNetworkedContainer(container.ID, container.hostConfig.NetworkMode.ConnectedContainer())
|
2015-04-29 18:53:35 -04: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 18:39:29 -04: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-11-03 13:25:09 -05:00
|
|
|
if err := daemon.allocateNetwork(container); err != nil {
|
2015-04-29 18:53:35 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-05-06 18:39:29 -04:00
|
|
|
|
|
|
|
return container.buildHostnameFile()
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-09-11 15:05:57 -04:00
|
|
|
// called from the libcontainer pre-start hook to set the network
|
|
|
|
// namespace configuration linkage to the libnetwork "sandbox" entity
|
2015-11-03 14:25:22 -05:00
|
|
|
func (daemon *Daemon) setNetworkNamespaceKey(containerID string, pid int) error {
|
2015-09-11 15:05:57 -04:00
|
|
|
path := fmt.Sprintf("/proc/%d/ns/net", pid)
|
|
|
|
var sandbox libnetwork.Sandbox
|
2015-11-03 14:25:22 -05:00
|
|
|
search := libnetwork.SandboxContainerWalker(&sandbox, containerID)
|
2015-11-03 13:25:09 -05:00
|
|
|
daemon.netController.WalkSandboxes(search)
|
2015-09-11 15:05:57 -04:00
|
|
|
if sandbox == nil {
|
2015-11-05 12:01:18 -05:00
|
|
|
return derr.ErrorCodeNoSandbox.WithArgs(containerID, "no sandbox found")
|
2015-09-11 15:05:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return sandbox.SetKey(path)
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) getIpcContainer(container *Container) (*Container, error) {
|
2015-04-29 18:53:35 -04:00
|
|
|
containerID := container.hostConfig.IpcMode.Container()
|
2015-11-03 13:25:09 -05:00
|
|
|
c, err := daemon.Get(containerID)
|
2015-04-29 18:53:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !c.IsRunning() {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeIPCRunning
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) setupWorkingDirectory() error {
|
2015-11-03 17:33:31 -05:00
|
|
|
if container.Config.WorkingDir == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
container.Config.WorkingDir = filepath.Clean(container.Config.WorkingDir)
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-03 17:33:31 -05:00
|
|
|
pth, err := container.GetResourcePath(container.Config.WorkingDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pthInfo, err := os.Stat(pth)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2015-04-29 18:53:35 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:33:31 -05:00
|
|
|
if err := system.MkdirAll(pth, 0755); err != nil {
|
|
|
|
return err
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
2015-11-03 17:33:31 -05:00
|
|
|
if pthInfo != nil && !pthInfo.IsDir() {
|
|
|
|
return derr.ErrorCodeNotADir.WithArgs(container.Config.WorkingDir)
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID string) (*Container, error) {
|
|
|
|
nc, err := daemon.Get(connectedContainerID)
|
2015-11-03 13:25:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-03 14:25:22 -05:00
|
|
|
if containerID == nc.ID {
|
2015-11-03 13:25:09 -05:00
|
|
|
return nil, derr.ErrorCodeJoinSelf
|
|
|
|
}
|
|
|
|
if !nc.IsRunning() {
|
2015-11-03 14:25:22 -05:00
|
|
|
return nil, derr.ErrorCodeJoinRunning.WithArgs(connectedContainerID)
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
2015-11-03 13:25:09 -05:00
|
|
|
return nc, nil
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:43:36 -05:00
|
|
|
func (daemon *Daemon) releaseNetwork(container *Container) {
|
2015-06-30 13:34:15 -04:00
|
|
|
if container.hostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
|
2015-04-29 18:53:35 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:43:28 -04:00
|
|
|
sid := container.NetworkSettings.SandboxID
|
2015-09-25 06:19:17 -04:00
|
|
|
networks := container.NetworkSettings.Networks
|
2015-10-26 08:00:49 -04:00
|
|
|
for n := range networks {
|
|
|
|
networks[n] = &network.EndpointSettings{}
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
container.NetworkSettings = &network.Settings{Networks: networks}
|
2015-05-06 18:39:29 -04:00
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
if sid == "" || len(networks) == 0 {
|
2015-09-02 19:43:28 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-03 12:43:36 -05:00
|
|
|
sb, err := daemon.netController.SandboxByID(sid)
|
2015-09-02 19:43:28 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error locating sandbox id %s: %v", sid, err)
|
2015-06-24 13:23:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:43:28 -04:00
|
|
|
if err := sb.Delete(); err != nil {
|
|
|
|
logrus.Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisconnectFromNetwork disconnects a container from a network
|
|
|
|
func (container *Container) DisconnectFromNetwork(n libnetwork.Network) error {
|
|
|
|
if !container.Running {
|
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
|
|
|
}
|
|
|
|
|
2015-11-08 21:19:53 -05:00
|
|
|
if container.hostConfig.NetworkMode.IsHost() && runconfig.NetworkMode(n.Type()).IsHost() {
|
|
|
|
return runconfig.ErrConflictHostNetwork
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:06:44 -04:00
|
|
|
if err := container.disconnectFromNetwork(n); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.toDiskLocking(); err != nil {
|
|
|
|
return fmt.Errorf("Error saving container to disk: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
2015-10-19 18:24:26 -04:00
|
|
|
func (container *Container) disconnectFromNetwork(n libnetwork.Network) error {
|
2015-09-25 06:19:17 -04:00
|
|
|
var (
|
|
|
|
ep libnetwork.Endpoint
|
|
|
|
sbox libnetwork.Sandbox
|
|
|
|
)
|
|
|
|
|
|
|
|
s := func(current libnetwork.Endpoint) bool {
|
2015-11-03 17:15:34 -05:00
|
|
|
epInfo := current.Info()
|
|
|
|
if epInfo == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if sb := epInfo.Sandbox(); sb != nil {
|
2015-09-25 06:19:17 -04:00
|
|
|
if sb.ContainerID() == container.ID {
|
|
|
|
ep = current
|
|
|
|
sbox = sb
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n.WalkEndpoints(s)
|
|
|
|
|
|
|
|
if ep == nil {
|
2015-10-10 12:43:03 -04:00
|
|
|
return fmt.Errorf("container %s is not connected to the network", container.ID)
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep.Leave(sbox); err != nil {
|
|
|
|
return fmt.Errorf("container %s failed to leave network %s: %v", container.ID, n.Name(), err)
|
2015-06-24 13:23:00 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", container.ID, n.Name(), err)
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:00:49 -04:00
|
|
|
delete(container.NetworkSettings.Networks, n.Name())
|
2015-09-25 06:19:17 -04:00
|
|
|
return nil
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
2015-05-26 12:33:55 -04:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
// appendNetworkMounts appends any network mounts to the array of mount points passed in
|
|
|
|
func appendNetworkMounts(container *Container, volumeMounts []volume.MountPoint) ([]volume.MountPoint, error) {
|
2015-05-26 17:20:55 -04:00
|
|
|
for _, mnt := range container.networkMounts() {
|
|
|
|
dest, err := container.GetResourcePath(mnt.Destination)
|
2015-05-26 12:33:55 -04:00
|
|
|
if err != nil {
|
2015-09-09 22:23:06 -04:00
|
|
|
return nil, err
|
2015-05-26 12:33:55 -04:00
|
|
|
}
|
2015-09-09 22:23:06 -04:00
|
|
|
volumeMounts = append(volumeMounts, volume.MountPoint{Destination: dest})
|
2015-05-26 12:33:55 -04:00
|
|
|
}
|
2015-09-09 22:23:06 -04:00
|
|
|
return volumeMounts, nil
|
2015-05-26 12:33:55 -04:00
|
|
|
}
|
2015-06-11 14:29:29 -04:00
|
|
|
|
2015-07-16 17:14:58 -04:00
|
|
|
func (container *Container) networkMounts() []execdriver.Mount {
|
|
|
|
var mounts []execdriver.Mount
|
2015-09-11 16:17:59 -04:00
|
|
|
shared := container.hostConfig.NetworkMode.IsContainer()
|
2015-07-16 17:14:58 -04:00
|
|
|
if container.ResolvConfPath != "" {
|
2015-10-12 17:18:34 -04:00
|
|
|
if _, err := os.Stat(container.ResolvConfPath); err != nil {
|
|
|
|
logrus.Warnf("ResolvConfPath set to %q, but can't stat this filename (err = %v); skipping", container.ResolvConfPath, err)
|
|
|
|
} else {
|
|
|
|
label.Relabel(container.ResolvConfPath, container.MountLabel, shared)
|
|
|
|
writable := !container.hostConfig.ReadonlyRootfs
|
|
|
|
if m, exists := container.MountPoints["/etc/resolv.conf"]; exists {
|
|
|
|
writable = m.RW
|
|
|
|
}
|
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.ResolvConfPath,
|
|
|
|
Destination: "/etc/resolv.conf",
|
|
|
|
Writable: writable,
|
|
|
|
Private: true,
|
|
|
|
})
|
2015-07-24 14:23:20 -04:00
|
|
|
}
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
if container.HostnamePath != "" {
|
2015-10-12 17:18:34 -04:00
|
|
|
if _, err := os.Stat(container.HostnamePath); err != nil {
|
|
|
|
logrus.Warnf("HostnamePath set to %q, but can't stat this filename (err = %v); skipping", container.HostnamePath, err)
|
|
|
|
} else {
|
|
|
|
label.Relabel(container.HostnamePath, container.MountLabel, shared)
|
|
|
|
writable := !container.hostConfig.ReadonlyRootfs
|
|
|
|
if m, exists := container.MountPoints["/etc/hostname"]; exists {
|
|
|
|
writable = m.RW
|
|
|
|
}
|
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.HostnamePath,
|
|
|
|
Destination: "/etc/hostname",
|
|
|
|
Writable: writable,
|
|
|
|
Private: true,
|
|
|
|
})
|
2015-07-24 14:23:20 -04:00
|
|
|
}
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
if container.HostsPath != "" {
|
2015-10-12 17:18:34 -04:00
|
|
|
if _, err := os.Stat(container.HostsPath); err != nil {
|
|
|
|
logrus.Warnf("HostsPath set to %q, but can't stat this filename (err = %v); skipping", container.HostsPath, err)
|
|
|
|
} else {
|
|
|
|
label.Relabel(container.HostsPath, container.MountLabel, shared)
|
|
|
|
writable := !container.hostConfig.ReadonlyRootfs
|
|
|
|
if m, exists := container.MountPoints["/etc/hosts"]; exists {
|
|
|
|
writable = m.RW
|
|
|
|
}
|
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.HostsPath,
|
|
|
|
Destination: "/etc/hosts",
|
|
|
|
Writable: writable,
|
|
|
|
Private: true,
|
|
|
|
})
|
2015-07-24 14:23:20 -04:00
|
|
|
}
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
return mounts
|
|
|
|
}
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
func (container *Container) copyImagePathContent(v volume.Volume, destination string) error {
|
|
|
|
rootfs, err := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, destination), container.basefs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
if _, err = ioutil.ReadDir(rootfs); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
path, err := v.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
if err := copyExistingContents(rootfs, path); err != nil {
|
|
|
|
return err
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
return v.Unmount()
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
|
|
|
|
func (container *Container) shmPath() (string, error) {
|
2015-08-26 08:00:01 -04:00
|
|
|
return container.getRootResourcePath("shm")
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
|
|
|
func (container *Container) mqueuePath() (string, error) {
|
2015-08-26 08:00:01 -04:00
|
|
|
return container.getRootResourcePath("mqueue")
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 17:28:27 -04:00
|
|
|
func (container *Container) hasMountFor(path string) bool {
|
|
|
|
_, exists := container.MountPoints[path]
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
func (daemon *Daemon) setupIpcDirs(container *Container) error {
|
|
|
|
rootUID, rootGID := daemon.GetRemappedUIDGID()
|
2015-10-01 17:28:27 -04:00
|
|
|
if !container.hasMountFor("/dev/shm") {
|
|
|
|
shmPath, err := container.shmPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
|
2015-10-01 17:28:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
|
2015-09-09 02:30:56 -04:00
|
|
|
// When ShmSize is 0 or less, the SHM size is set to 64MB.
|
|
|
|
if container.hostConfig.ShmSize <= 0 {
|
|
|
|
container.hostConfig.ShmSize = 67108864
|
|
|
|
}
|
|
|
|
shmproperty := "mode=1777,size=" + strconv.FormatInt(container.hostConfig.ShmSize, 10)
|
|
|
|
if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, container.getMountLabel())); err != nil {
|
2015-10-01 17:28:27 -04:00
|
|
|
return fmt.Errorf("mounting shm tmpfs: %s", err)
|
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 17:28:27 -04:00
|
|
|
if !container.hasMountFor("/dev/mqueue") {
|
|
|
|
mqueuePath, err := container.mqueuePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := idtools.MkdirAllAs(mqueuePath, 0700, rootUID, rootGID); err != nil {
|
2015-10-01 17:28:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
|
2015-10-01 17:28:27 -04: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-10-08 11:51:41 -04:00
|
|
|
if err := os.Chown(mqueuePath, rootUID, rootGID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:00:01 -04:00
|
|
|
func (container *Container) unmountIpcMounts(unmount func(pth string) error) {
|
2015-08-03 18:05:34 -04:00
|
|
|
if container.hostConfig.IpcMode.IsContainer() || container.hostConfig.IpcMode.IsHost() {
|
2015-10-30 19:00:01 -04:00
|
|
|
return
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:00:01 -04:00
|
|
|
var warnings []string
|
2015-10-01 17:28:27 -04:00
|
|
|
|
|
|
|
if !container.hasMountFor("/dev/shm") {
|
|
|
|
shmPath, err := container.shmPath()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2015-10-30 19:00:01 -04:00
|
|
|
warnings = append(warnings, err.Error())
|
|
|
|
} else if shmPath != "" {
|
2015-10-30 14:55:52 -04:00
|
|
|
if err := unmount(shmPath); err != nil {
|
2015-10-30 19:00:01 -04:00
|
|
|
warnings = append(warnings, fmt.Sprintf("failed to umount %s: %v", shmPath, err))
|
2015-10-01 17:28:27 -04:00
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
|
2015-10-01 17:28:27 -04:00
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 17:28:27 -04:00
|
|
|
if !container.hasMountFor("/dev/mqueue") {
|
|
|
|
mqueuePath, err := container.mqueuePath()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2015-10-30 19:00:01 -04:00
|
|
|
warnings = append(warnings, err.Error())
|
|
|
|
} else if mqueuePath != "" {
|
2015-10-30 14:55:52 -04:00
|
|
|
if err := unmount(mqueuePath); err != nil {
|
2015-10-30 19:00:01 -04:00
|
|
|
warnings = append(warnings, fmt.Sprintf("failed to umount %s: %v", mqueuePath, err))
|
2015-10-01 17:28:27 -04:00
|
|
|
}
|
2015-08-26 08:00:01 -04:00
|
|
|
}
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:00:01 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
logrus.Warnf("failed to cleanup ipc mounts:\n%v", strings.Join(warnings, "\n"))
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) ipcMounts() []execdriver.Mount {
|
|
|
|
var mounts []execdriver.Mount
|
2015-10-01 17:28:27 -04: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 18:05:34 -04:00
|
|
|
return mounts
|
|
|
|
}
|
2015-08-26 08:00:01 -04:00
|
|
|
|
|
|
|
func detachMounted(path string) error {
|
|
|
|
return syscall.Unmount(path, syscall.MNT_DETACH)
|
|
|
|
}
|
2015-11-11 16:53:38 -05:00
|
|
|
|
|
|
|
func (daemon *Daemon) mountVolumes(container *Container) error {
|
|
|
|
mounts, err := daemon.setupMounts(container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mounts {
|
|
|
|
dest, err := container.GetResourcePath(m.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stat os.FileInfo
|
|
|
|
stat, err = os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := "rbind,ro"
|
|
|
|
if m.Writable {
|
|
|
|
opts = "rbind,rw"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) unmountVolumes(forceSyscall bool) error {
|
|
|
|
var (
|
|
|
|
volumeMounts []volume.MountPoint
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, mntPoint := range container.MountPoints {
|
|
|
|
dest, err := container.GetResourcePath(mntPoint.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
volumeMounts = append(volumeMounts, volume.MountPoint{Destination: dest, Volume: mntPoint.Volume})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append any network mounts to the list (this is a no-op on Windows)
|
|
|
|
if volumeMounts, err = appendNetworkMounts(container, volumeMounts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, volumeMount := range volumeMounts {
|
|
|
|
if forceSyscall {
|
2015-11-19 15:46:07 -05:00
|
|
|
if err := detachMounted(volumeMount.Destination); err != nil {
|
|
|
|
logrus.Warnf("%s unmountVolumes: Failed to do lazy umount %v", container.ID, err)
|
2015-11-11 16:53:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if volumeMount.Volume != nil {
|
|
|
|
if err := volumeMount.Volume.Unmount(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|