Make sure network files exist before adding them to mounts

It is possible that network files do not exist, especially in the case
of `--net=host` where a host OS (like CoreOS) does not use certain
standard network files.  This patch verifies that the source file of a
network mount point exists before adding it to the list of mount points
for bind mounting from the container's metadata directory.

Docker-DCO-1.1-Signed-off-by: Phil Estes <estesp@linux.vnet.ibm.com> (github: estesp)
This commit is contained in:
Phil Estes 2015-10-12 17:18:34 -04:00
parent 151564d39b
commit ed68486f68
1 changed files with 42 additions and 30 deletions

View File

@ -1231,43 +1231,55 @@ func (container *Container) networkMounts() []execdriver.Mount {
var mounts []execdriver.Mount var mounts []execdriver.Mount
shared := container.hostConfig.NetworkMode.IsContainer() shared := container.hostConfig.NetworkMode.IsContainer()
if container.ResolvConfPath != "" { if container.ResolvConfPath != "" {
label.Relabel(container.ResolvConfPath, container.MountLabel, shared) if _, err := os.Stat(container.ResolvConfPath); err != nil {
writable := !container.hostConfig.ReadonlyRootfs logrus.Warnf("ResolvConfPath set to %q, but can't stat this filename (err = %v); skipping", container.ResolvConfPath, err)
if m, exists := container.MountPoints["/etc/resolv.conf"]; exists { } else {
writable = m.RW 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,
})
} }
mounts = append(mounts, execdriver.Mount{
Source: container.ResolvConfPath,
Destination: "/etc/resolv.conf",
Writable: writable,
Private: true,
})
} }
if container.HostnamePath != "" { if container.HostnamePath != "" {
label.Relabel(container.HostnamePath, container.MountLabel, shared) if _, err := os.Stat(container.HostnamePath); err != nil {
writable := !container.hostConfig.ReadonlyRootfs logrus.Warnf("HostnamePath set to %q, but can't stat this filename (err = %v); skipping", container.HostnamePath, err)
if m, exists := container.MountPoints["/etc/hostname"]; exists { } else {
writable = m.RW 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,
})
} }
mounts = append(mounts, execdriver.Mount{
Source: container.HostnamePath,
Destination: "/etc/hostname",
Writable: writable,
Private: true,
})
} }
if container.HostsPath != "" { if container.HostsPath != "" {
label.Relabel(container.HostsPath, container.MountLabel, shared) if _, err := os.Stat(container.HostsPath); err != nil {
writable := !container.hostConfig.ReadonlyRootfs logrus.Warnf("HostsPath set to %q, but can't stat this filename (err = %v); skipping", container.HostsPath, err)
if m, exists := container.MountPoints["/etc/hosts"]; exists { } else {
writable = m.RW 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,
})
} }
mounts = append(mounts, execdriver.Mount{
Source: container.HostsPath,
Destination: "/etc/hosts",
Writable: writable,
Private: true,
})
} }
return mounts return mounts
} }