1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Update libcontainer to 55430d0db7c6bb1198c0bb573a9

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2014-09-01 15:17:33 -07:00
parent 7ff488e741
commit cf31787cd0
14 changed files with 260 additions and 86 deletions

View file

@ -62,7 +62,7 @@ if [ "$1" = '--go' ]; then
mv tmp-tar src/code.google.com/p/go/src/pkg/archive/tar mv tmp-tar src/code.google.com/p/go/src/pkg/archive/tar
fi fi
clone git github.com/docker/libcontainer db65c35051d05f3fb218a0e84a11267e0894fe0a clone git github.com/docker/libcontainer 55430d0db7c6bb1198c0bb573a9700a859d5ec26
# see src/github.com/docker/libcontainer/update-vendor.sh which is the "source of truth" for libcontainer deps (just like this file) # see src/github.com/docker/libcontainer/update-vendor.sh which is the "source of truth" for libcontainer deps (just like this file)
rm -rf src/github.com/docker/libcontainer/vendor rm -rf src/github.com/docker/libcontainer/vendor
eval "$(grep '^clone ' src/github.com/docker/libcontainer/update-vendor.sh | grep -v 'github.com/codegangsta/cli')" eval "$(grep '^clone ' src/github.com/docker/libcontainer/update-vendor.sh | grep -v 'github.com/codegangsta/cli')"

View file

@ -4,10 +4,10 @@ all:
test: test:
# we need NET_ADMIN for the netlink tests and SYS_ADMIN for mounting # we need NET_ADMIN for the netlink tests and SYS_ADMIN for mounting
docker run --rm -it --cap-add NET_ADMIN --cap-add SYS_ADMIN docker/libcontainer docker run --rm -it --privileged docker/libcontainer
sh: sh:
docker run --rm -it --cap-add NET_ADMIN --cap-add SYS_ADMIN -w /busybox docker/libcontainer nsinit exec sh docker run --rm -it --privileged -w /busybox docker/libcontainer nsinit exec sh
GO_PACKAGES = $(shell find . -not \( -wholename ./vendor -prune \) -name '*.go' -print0 | xargs -0n1 dirname | sort -u) GO_PACKAGES = $(shell find . -not \( -wholename ./vendor -prune \) -name '*.go' -print0 | xargs -0n1 dirname | sort -u)

View file

@ -115,6 +115,14 @@ func getBlkioStat(path string) ([]cgroups.BlkioStatEntry, error) {
} }
func (s *BlkioGroup) GetStats(path string, stats *cgroups.Stats) error { func (s *BlkioGroup) GetStats(path string, stats *cgroups.Stats) error {
// Try to read CFQ stats available on all CFQ enabled kernels first
if blkioStats, err := getBlkioStat(filepath.Join(path, "blkio.io_serviced_recursive")); err == nil && blkioStats != nil {
return getCFQStats(path, stats)
}
return getStats(path, stats) // Use generic stats as fallback
}
func getCFQStats(path string, stats *cgroups.Stats) error {
var blkioStats []cgroups.BlkioStatEntry var blkioStats []cgroups.BlkioStatEntry
var err error var err error
@ -140,3 +148,20 @@ func (s *BlkioGroup) GetStats(path string, stats *cgroups.Stats) error {
return nil return nil
} }
func getStats(path string, stats *cgroups.Stats) error {
var blkioStats []cgroups.BlkioStatEntry
var err error
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.throttle.io_service_bytes")); err != nil {
return err
}
stats.BlkioStats.IoServiceBytesRecursive = blkioStats
if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.throttle.io_serviced")); err != nil {
return err
}
stats.BlkioStats.IoServicedRecursive = blkioStats
return nil
}

View file

@ -26,10 +26,30 @@ Total 50`
8:0 Async 3 8:0 Async 3
8:0 Total 5 8:0 Total 5
Total 5` Total 5`
throttleServiceBytes = `8:0 Read 11030528
8:0 Write 23
8:0 Sync 42
8:0 Async 11030528
8:0 Total 11030528
252:0 Read 11030528
252:0 Write 23
252:0 Sync 42
252:0 Async 11030528
252:0 Total 11030528
Total 22061056`
throttleServiced = `8:0 Read 164
8:0 Write 23
8:0 Sync 42
8:0 Async 164
8:0 Total 164
252:0 Read 164
252:0 Write 23
252:0 Sync 42
252:0 Async 164
252:0 Total 164
Total 328`
) )
var actualStats = *cgroups.NewStats()
func appendBlkioStatEntry(blkioStatEntries *[]cgroups.BlkioStatEntry, major, minor, value uint64, op string) { func appendBlkioStatEntry(blkioStatEntries *[]cgroups.BlkioStatEntry, major, minor, value uint64, op string) {
*blkioStatEntries = append(*blkioStatEntries, cgroups.BlkioStatEntry{Major: major, Minor: minor, Value: value, Op: op}) *blkioStatEntries = append(*blkioStatEntries, cgroups.BlkioStatEntry{Major: major, Minor: minor, Value: value, Op: op})
} }
@ -45,6 +65,7 @@ func TestBlkioStats(t *testing.T) {
}) })
blkio := &BlkioGroup{} blkio := &BlkioGroup{}
actualStats := *cgroups.NewStats()
err := blkio.GetStats(helper.CgroupPath, &actualStats) err := blkio.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -85,6 +106,7 @@ func TestBlkioStatsNoSectorsFile(t *testing.T) {
}) })
blkio := &BlkioGroup{} blkio := &BlkioGroup{}
actualStats := *cgroups.NewStats()
err := blkio.GetStats(helper.CgroupPath, &actualStats) err := blkio.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatalf("Failed unexpectedly: %s", err) t.Fatalf("Failed unexpectedly: %s", err)
@ -101,6 +123,7 @@ func TestBlkioStatsNoServiceBytesFile(t *testing.T) {
}) })
blkio := &BlkioGroup{} blkio := &BlkioGroup{}
actualStats := *cgroups.NewStats()
err := blkio.GetStats(helper.CgroupPath, &actualStats) err := blkio.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatalf("Failed unexpectedly: %s", err) t.Fatalf("Failed unexpectedly: %s", err)
@ -117,6 +140,7 @@ func TestBlkioStatsNoServicedFile(t *testing.T) {
}) })
blkio := &BlkioGroup{} blkio := &BlkioGroup{}
actualStats := *cgroups.NewStats()
err := blkio.GetStats(helper.CgroupPath, &actualStats) err := blkio.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatalf("Failed unexpectedly: %s", err) t.Fatalf("Failed unexpectedly: %s", err)
@ -133,6 +157,7 @@ func TestBlkioStatsNoQueuedFile(t *testing.T) {
}) })
blkio := &BlkioGroup{} blkio := &BlkioGroup{}
actualStats := *cgroups.NewStats()
err := blkio.GetStats(helper.CgroupPath, &actualStats) err := blkio.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatalf("Failed unexpectedly: %s", err) t.Fatalf("Failed unexpectedly: %s", err)
@ -150,6 +175,7 @@ func TestBlkioStatsUnexpectedNumberOfFields(t *testing.T) {
}) })
blkio := &BlkioGroup{} blkio := &BlkioGroup{}
actualStats := *cgroups.NewStats()
err := blkio.GetStats(helper.CgroupPath, &actualStats) err := blkio.GetStats(helper.CgroupPath, &actualStats)
if err == nil { if err == nil {
t.Fatal("Expected to fail, but did not") t.Fatal("Expected to fail, but did not")
@ -167,8 +193,56 @@ func TestBlkioStatsUnexpectedFieldType(t *testing.T) {
}) })
blkio := &BlkioGroup{} blkio := &BlkioGroup{}
actualStats := *cgroups.NewStats()
err := blkio.GetStats(helper.CgroupPath, &actualStats) err := blkio.GetStats(helper.CgroupPath, &actualStats)
if err == nil { if err == nil {
t.Fatal("Expected to fail, but did not") t.Fatal("Expected to fail, but did not")
} }
} }
func TestNonCFQBlkioStats(t *testing.T) {
helper := NewCgroupTestUtil("blkio", t)
defer helper.cleanup()
helper.writeFileContents(map[string]string{
"blkio.io_service_bytes_recursive": "",
"blkio.io_serviced_recursive": "",
"blkio.io_queued_recursive": "",
"blkio.sectors_recursive": "",
"blkio.throttle.io_service_bytes": throttleServiceBytes,
"blkio.throttle.io_serviced": throttleServiced,
})
blkio := &BlkioGroup{}
actualStats := *cgroups.NewStats()
err := blkio.GetStats(helper.CgroupPath, &actualStats)
if err != nil {
t.Fatal(err)
}
// Verify expected stats.
expectedStats := cgroups.BlkioStats{}
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 11030528, "Read")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 23, "Write")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 42, "Sync")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 11030528, "Async")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 11030528, "Total")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 11030528, "Read")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 23, "Write")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 42, "Sync")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 11030528, "Async")
appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 11030528, "Total")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 164, "Read")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 23, "Write")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 42, "Sync")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 164, "Async")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 164, "Total")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 164, "Read")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 23, "Write")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 42, "Sync")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 164, "Async")
appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 164, "Total")
expectBlkioStatsEquals(t, expectedStats, actualStats.BlkioStats)
}

View file

@ -24,6 +24,7 @@ func TestCpuStats(t *testing.T) {
}) })
cpu := &CpuGroup{} cpu := &CpuGroup{}
actualStats := *cgroups.NewStats()
err := cpu.GetStats(helper.CgroupPath, &actualStats) err := cpu.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -42,6 +43,7 @@ func TestNoCpuStatFile(t *testing.T) {
defer helper.cleanup() defer helper.cleanup()
cpu := &CpuGroup{} cpu := &CpuGroup{}
actualStats := *cgroups.NewStats()
err := cpu.GetStats(helper.CgroupPath, &actualStats) err := cpu.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatal("Expected not to fail, but did") t.Fatal("Expected not to fail, but did")
@ -59,6 +61,7 @@ func TestInvalidCpuStat(t *testing.T) {
}) })
cpu := &CpuGroup{} cpu := &CpuGroup{}
actualStats := *cgroups.NewStats()
err := cpu.GetStats(helper.CgroupPath, &actualStats) err := cpu.GetStats(helper.CgroupPath, &actualStats)
if err == nil { if err == nil {
t.Fatal("Expected failed stat parsing.") t.Fatal("Expected failed stat parsing.")

View file

@ -25,6 +25,7 @@ func TestMemoryStats(t *testing.T) {
}) })
memory := &MemoryGroup{} memory := &MemoryGroup{}
actualStats := *cgroups.NewStats()
err := memory.GetStats(helper.CgroupPath, &actualStats) err := memory.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -42,6 +43,7 @@ func TestMemoryStatsNoStatFile(t *testing.T) {
}) })
memory := &MemoryGroup{} memory := &MemoryGroup{}
actualStats := *cgroups.NewStats()
err := memory.GetStats(helper.CgroupPath, &actualStats) err := memory.GetStats(helper.CgroupPath, &actualStats)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -57,6 +59,7 @@ func TestMemoryStatsNoUsageFile(t *testing.T) {
}) })
memory := &MemoryGroup{} memory := &MemoryGroup{}
actualStats := *cgroups.NewStats()
err := memory.GetStats(helper.CgroupPath, &actualStats) err := memory.GetStats(helper.CgroupPath, &actualStats)
if err == nil { if err == nil {
t.Fatal("Expected failure") t.Fatal("Expected failure")
@ -72,6 +75,7 @@ func TestMemoryStatsNoMaxUsageFile(t *testing.T) {
}) })
memory := &MemoryGroup{} memory := &MemoryGroup{}
actualStats := *cgroups.NewStats()
err := memory.GetStats(helper.CgroupPath, &actualStats) err := memory.GetStats(helper.CgroupPath, &actualStats)
if err == nil { if err == nil {
t.Fatal("Expected failure") t.Fatal("Expected failure")
@ -88,6 +92,7 @@ func TestMemoryStatsBadStatFile(t *testing.T) {
}) })
memory := &MemoryGroup{} memory := &MemoryGroup{}
actualStats := *cgroups.NewStats()
err := memory.GetStats(helper.CgroupPath, &actualStats) err := memory.GetStats(helper.CgroupPath, &actualStats)
if err == nil { if err == nil {
t.Fatal("Expected failure") t.Fatal("Expected failure")
@ -104,6 +109,7 @@ func TestMemoryStatsBadUsageFile(t *testing.T) {
}) })
memory := &MemoryGroup{} memory := &MemoryGroup{}
actualStats := *cgroups.NewStats()
err := memory.GetStats(helper.CgroupPath, &actualStats) err := memory.GetStats(helper.CgroupPath, &actualStats)
if err == nil { if err == nil {
t.Fatal("Expected failure") t.Fatal("Expected failure")
@ -120,6 +126,7 @@ func TestMemoryStatsBadMaxUsageFile(t *testing.T) {
}) })
memory := &MemoryGroup{} memory := &MemoryGroup{}
actualStats := *cgroups.NewStats()
err := memory.GetStats(helper.CgroupPath, &actualStats) err := memory.GetStats(helper.CgroupPath, &actualStats)
if err == nil { if err == nil {
t.Fatal("Expected failure") t.Fatal("Expected failure")

View file

@ -9,8 +9,8 @@ package libcontainer
// be destroyed by a separate process, any function may return that the container // be destroyed by a separate process, any function may return that the container
// was not found. // was not found.
type Container interface { type Container interface {
// Returns the path to the container which contains the state // Returns the ID of the container
Path() string ID() string
// Returns the current run state of the container. // Returns the current run state of the container.
// //

View file

@ -12,14 +12,14 @@ type Factory interface {
// System error // System error
// //
// On error, any partially created container parts are cleaned up (the operation is atomic). // On error, any partially created container parts are cleaned up (the operation is atomic).
Create(path string, config *Config) (Container, error) Create(id string, config *Config) (Container, error)
// Load takes the path for an existing container and reconstructs the container // Load takes an ID for an existing container and reconstructs the container
// from the state. // from the state.
// //
// Errors: // Errors:
// Path does not exist // Path does not exist
// Container is stopped // Container is stopped
// System error // System error
Load(path string) (Container, error) Load(id string) (Container, error)
} }

View file

@ -39,3 +39,7 @@ func Init() {
func ReserveLabel(label string) error { func ReserveLabel(label string) error {
return nil return nil
} }
func UnreserveLabel(label string) error {
return nil
}

View file

@ -128,3 +128,11 @@ func ReserveLabel(label string) error {
selinux.ReserveLabel(label) selinux.ReserveLabel(label)
return nil return nil
} }
// UnreserveLabel will remove the reservation of the MCS label.
// This will allow InitLabels to use the MCS label in a newly created
// containers
func UnreserveLabel(label string) error {
selinux.FreeLxcContexts(label)
return nil
}

View file

@ -8,7 +8,6 @@ import (
"path/filepath" "path/filepath"
"syscall" "syscall"
"github.com/docker/docker/pkg/symlink"
"github.com/docker/libcontainer/label" "github.com/docker/libcontainer/label"
"github.com/docker/libcontainer/mount/nodes" "github.com/docker/libcontainer/mount/nodes"
) )
@ -31,24 +30,34 @@ func InitializeMountNamespace(rootfs, console string, sysReadonly bool, mountCon
err error err error
flag = syscall.MS_PRIVATE flag = syscall.MS_PRIVATE
) )
if mountConfig.NoPivotRoot { if mountConfig.NoPivotRoot {
flag = syscall.MS_SLAVE flag = syscall.MS_SLAVE
} }
if err := syscall.Mount("", "/", "", uintptr(flag|syscall.MS_REC), ""); err != nil { if err := syscall.Mount("", "/", "", uintptr(flag|syscall.MS_REC), ""); err != nil {
return fmt.Errorf("mounting / with flags %X %s", (flag | syscall.MS_REC), err) return fmt.Errorf("mounting / with flags %X %s", (flag | syscall.MS_REC), err)
} }
if err := syscall.Mount(rootfs, rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, ""); err != nil { if err := syscall.Mount(rootfs, rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
return fmt.Errorf("mouting %s as bind %s", rootfs, err) return fmt.Errorf("mouting %s as bind %s", rootfs, err)
} }
if err := mountSystem(rootfs, sysReadonly, mountConfig); err != nil { if err := mountSystem(rootfs, sysReadonly, mountConfig); err != nil {
return fmt.Errorf("mount system %s", err) return fmt.Errorf("mount system %s", err)
} }
if err := setupBindmounts(rootfs, mountConfig); err != nil {
return fmt.Errorf("bind mounts %s", err) // apply any user specified mounts within the new mount namespace
for _, m := range mountConfig.Mounts {
if err := m.Mount(rootfs, mountConfig.MountLabel); err != nil {
return err
}
} }
if err := nodes.CreateDeviceNodes(rootfs, mountConfig.DeviceNodes); err != nil { if err := nodes.CreateDeviceNodes(rootfs, mountConfig.DeviceNodes); err != nil {
return fmt.Errorf("create device nodes %s", err) return fmt.Errorf("create device nodes %s", err)
} }
if err := SetupPtmx(rootfs, console, mountConfig.MountLabel); err != nil { if err := SetupPtmx(rootfs, console, mountConfig.MountLabel); err != nil {
return err return err
} }
@ -72,6 +81,7 @@ func InitializeMountNamespace(rootfs, console string, sysReadonly bool, mountCon
} else { } else {
err = PivotRoot(rootfs) err = PivotRoot(rootfs)
} }
if err != nil { if err != nil {
return err return err
} }
@ -90,7 +100,7 @@ func InitializeMountNamespace(rootfs, console string, sysReadonly bool, mountCon
// mountSystem sets up linux specific system mounts like sys, proc, shm, and devpts // mountSystem sets up linux specific system mounts like sys, proc, shm, and devpts
// inside the mount namespace // inside the mount namespace
func mountSystem(rootfs string, sysReadonly bool, mountConfig *MountConfig) error { func mountSystem(rootfs string, sysReadonly bool, mountConfig *MountConfig) error {
for _, m := range newSystemMounts(rootfs, mountConfig.MountLabel, sysReadonly, mountConfig.Mounts) { for _, m := range newSystemMounts(rootfs, mountConfig.MountLabel, sysReadonly) {
if err := os.MkdirAll(m.path, 0755); err != nil && !os.IsExist(err) { if err := os.MkdirAll(m.path, 0755); err != nil && !os.IsExist(err) {
return fmt.Errorf("mkdirall %s %s", m.path, err) return fmt.Errorf("mkdirall %s %s", m.path, err)
} }
@ -151,56 +161,9 @@ func setupDevSymlinks(rootfs string) error {
return nil return nil
} }
func setupBindmounts(rootfs string, mountConfig *MountConfig) error {
bindMounts := mountConfig.Mounts
for _, m := range bindMounts.OfType("bind") {
var (
flags = syscall.MS_BIND | syscall.MS_REC
dest = filepath.Join(rootfs, m.Destination)
)
if !m.Writable {
flags = flags | syscall.MS_RDONLY
}
stat, err := os.Stat(m.Source)
if err != nil {
return err
}
dest, err = symlink.FollowSymlinkInScope(dest, rootfs)
if err != nil {
return err
}
if err := createIfNotExists(dest, stat.IsDir()); err != nil {
return fmt.Errorf("Creating new bind-mount target, %s", err)
}
if err := syscall.Mount(m.Source, dest, "bind", uintptr(flags), ""); err != nil {
return fmt.Errorf("mounting %s into %s %s", m.Source, dest, err)
}
if !m.Writable {
if err := syscall.Mount(m.Source, dest, "bind", uintptr(flags|syscall.MS_REMOUNT), ""); err != nil {
return fmt.Errorf("remounting %s into %s %s", m.Source, dest, err)
}
}
if m.Relabel != "" {
if err := label.Relabel(m.Source, mountConfig.MountLabel, m.Relabel); err != nil {
return fmt.Errorf("relabeling %s to %s %s", m.Source, mountConfig.MountLabel, err)
}
}
if m.Private {
if err := syscall.Mount("", dest, "none", uintptr(syscall.MS_PRIVATE), ""); err != nil {
return fmt.Errorf("mounting %s private %s", dest, err)
}
}
}
return nil
}
// TODO: this is crappy right now and should be cleaned up with a better way of handling system and // TODO: this is crappy right now and should be cleaned up with a better way of handling system and
// standard bind mounts allowing them to be more dynamic // standard bind mounts allowing them to be more dynamic
func newSystemMounts(rootfs, mountLabel string, sysReadonly bool, mounts Mounts) []mount { func newSystemMounts(rootfs, mountLabel string, sysReadonly bool) []mount {
systemMounts := []mount{ systemMounts := []mount{
{source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags}, {source: "proc", path: filepath.Join(rootfs, "proc"), device: "proc", flags: defaultMountFlags},
{source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: label.FormatMountLabel("mode=755", mountLabel)}, {source: "tmpfs", path: filepath.Join(rootfs, "dev"), device: "tmpfs", flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME, data: label.FormatMountLabel("mode=755", mountLabel)},
@ -212,6 +175,7 @@ func newSystemMounts(rootfs, mountLabel string, sysReadonly bool, mounts Mounts)
if sysReadonly { if sysReadonly {
sysMountFlags |= syscall.MS_RDONLY sysMountFlags |= syscall.MS_RDONLY
} }
systemMounts = append(systemMounts, mount{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: sysMountFlags}) systemMounts = append(systemMounts, mount{source: "sysfs", path: filepath.Join(rootfs, "sys"), device: "sysfs", flags: sysMountFlags})
return systemMounts return systemMounts

View file

@ -0,0 +1,104 @@
package mount
import (
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/docker/docker/pkg/symlink"
"github.com/docker/libcontainer/label"
)
type Mount struct {
Type string `json:"type,omitempty"`
Source string `json:"source,omitempty"` // Source path, in the host namespace
Destination string `json:"destination,omitempty"` // Destination path, in the container
Writable bool `json:"writable,omitempty"`
Relabel string `json:"relabel,omitempty"` // Relabel source if set, "z" indicates shared, "Z" indicates unshared
Private bool `json:"private,omitempty"`
}
func (m *Mount) Mount(rootfs, mountLabel string) error {
switch m.Type {
case "bind":
return m.bindMount(rootfs, mountLabel)
case "tmpfs":
return m.tmpfsMount(rootfs, mountLabel)
default:
return fmt.Errorf("unsupported mount type %s for %s", m.Type, m.Destination)
}
}
func (m *Mount) bindMount(rootfs, mountLabel string) error {
var (
flags = syscall.MS_BIND | syscall.MS_REC
dest = filepath.Join(rootfs, m.Destination)
)
if !m.Writable {
flags = flags | syscall.MS_RDONLY
}
stat, err := os.Stat(m.Source)
if err != nil {
return err
}
// FIXME: (crosbymichael) This does not belong here and should be done a layer above
dest, err = symlink.FollowSymlinkInScope(dest, rootfs)
if err != nil {
return err
}
if err := createIfNotExists(dest, stat.IsDir()); err != nil {
return fmt.Errorf("creating new bind mount target %s", err)
}
if err := syscall.Mount(m.Source, dest, "bind", uintptr(flags), ""); err != nil {
return fmt.Errorf("mounting %s into %s %s", m.Source, dest, err)
}
if !m.Writable {
if err := syscall.Mount(m.Source, dest, "bind", uintptr(flags|syscall.MS_REMOUNT), ""); err != nil {
return fmt.Errorf("remounting %s into %s %s", m.Source, dest, err)
}
}
if m.Relabel != "" {
if err := label.Relabel(m.Source, mountLabel, m.Relabel); err != nil {
return fmt.Errorf("relabeling %s to %s %s", m.Source, mountLabel, err)
}
}
if m.Private {
if err := syscall.Mount("", dest, "none", uintptr(syscall.MS_PRIVATE), ""); err != nil {
return fmt.Errorf("mounting %s private %s", dest, err)
}
}
return nil
}
func (m *Mount) tmpfsMount(rootfs, mountLabel string) error {
var (
err error
l = label.FormatMountLabel("", mountLabel)
dest = filepath.Join(rootfs, m.Destination)
)
// FIXME: (crosbymichael) This does not belong here and should be done a layer above
if dest, err = symlink.FollowSymlinkInScope(dest, rootfs); err != nil {
return err
}
if err := createIfNotExists(dest, true); err != nil {
return fmt.Errorf("creating new tmpfs mount target %s", err)
}
if err := syscall.Mount("tmpfs", dest, "tmpfs", uintptr(defaultMountFlags), l); err != nil {
return fmt.Errorf("%s mounting %s in tmpfs", err, dest)
}
return nil
}

View file

@ -6,6 +6,8 @@ import (
"github.com/docker/libcontainer/devices" "github.com/docker/libcontainer/devices"
) )
var ErrUnsupported = errors.New("Unsupported method")
type MountConfig struct { type MountConfig struct {
// NoPivotRoot will use MS_MOVE and a chroot to jail the process into the container's rootfs // NoPivotRoot will use MS_MOVE and a chroot to jail the process into the container's rootfs
// This is a common option when the container is running in ramdisk // This is a common option when the container is running in ramdisk
@ -17,33 +19,10 @@ type MountConfig struct {
// Mounts specify additional source and destination paths that will be mounted inside the container's // Mounts specify additional source and destination paths that will be mounted inside the container's
// rootfs and mount namespace if specified // rootfs and mount namespace if specified
Mounts Mounts `json:"mounts,omitempty"` Mounts []*Mount `json:"mounts,omitempty"`
// The device nodes that should be automatically created within the container upon container start. Note, make sure that the node is marked as allowed in the cgroup as well! // The device nodes that should be automatically created within the container upon container start. Note, make sure that the node is marked as allowed in the cgroup as well!
DeviceNodes []*devices.Device `json:"device_nodes,omitempty"` DeviceNodes []*devices.Device `json:"device_nodes,omitempty"`
MountLabel string `json:"mount_label,omitempty"` MountLabel string `json:"mount_label,omitempty"`
} }
type Mount struct {
Type string `json:"type,omitempty"`
Source string `json:"source,omitempty"` // Source path, in the host namespace
Destination string `json:"destination,omitempty"` // Destination path, in the container
Writable bool `json:"writable,omitempty"`
Relabel string `json:"relabel,omitempty"` // Relabel source if set, "z" indicates shared, "Z" indicates unshared
Private bool `json:"private,omitempty"`
}
type Mounts []Mount
var ErrUnsupported = errors.New("Unsupported method")
func (s Mounts) OfType(t string) Mounts {
out := Mounts{}
for _, m := range s {
if m.Type == t {
out = append(out, m)
}
}
return out
}

View file

@ -166,6 +166,12 @@
"path": "/dev/random", "path": "/dev/random",
"type": 99 "type": 99
} }
],
"mounts": [
{
"type": "tmpfs",
"destination": "/tmp"
}
] ]
}, },
"environment": [ "environment": [