From 9844c828066678acb1d7b233fc78a2472a1ae3c3 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 8 May 2014 19:58:33 +0200 Subject: [PATCH 1/2] libcontainer: Create dirs/files as needed for bind mounts If you specify a bind mount in a place that doesn't have a file yet we create that (and parent directories). This is needed because otherwise you can't use volumes like e.g. /dev/log, as that gets covered by the /dev tmpfs mounts. Docker-DCO-1.1-Signed-off-by: Alexander Larsson (github: alexlarsson) Upstream-commit: 70ef53f25e177e42046170ef59bb29ebd77a3016 Component: engine --- .../engine/pkg/libcontainer/mount/init.go | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/components/engine/pkg/libcontainer/mount/init.go b/components/engine/pkg/libcontainer/mount/init.go index cfe61d1532..b0a3ef1061 100644 --- a/components/engine/pkg/libcontainer/mount/init.go +++ b/components/engine/pkg/libcontainer/mount/init.go @@ -91,6 +91,28 @@ func mountSystem(rootfs string, container *libcontainer.Container) error { return nil } +func createIfNotExists(path string, isDir bool) error { + if _, err := os.Stat(path); err != nil { + if os.IsNotExist(err) { + if isDir { + if err := os.MkdirAll(path, 0755); err != nil { + return err + } + } else { + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + return err + } + f, err := os.OpenFile(path, os.O_CREATE, 0755) + if err != nil { + return err + } + f.Close() + } + } + } + return nil +} + func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error { for _, m := range bindMounts.OfType("bind") { var ( @@ -100,6 +122,15 @@ func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error { if !m.Writable { flags = flags | syscall.MS_RDONLY } + + stat, err := os.Stat(m.Source) + if err != nil { + return err + } + if err := createIfNotExists(dest, stat.IsDir()); err != nil { + return fmt.Errorf("Creating new bind-mount target, %s\n", err) + } + if err := system.Mount(m.Source, dest, "bind", uintptr(flags), ""); err != nil { return fmt.Errorf("mounting %s into %s %s", m.Source, dest, err) } From fba87d75f12d5882af1be8839f90758d0ff7b28e Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 12 May 2014 12:24:30 -0700 Subject: [PATCH 2/2] Remove newline char in error message Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) Upstream-commit: cc678a7078b417a330e8d4e3604b74f9e5d4cda4 Component: engine --- components/engine/pkg/libcontainer/mount/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/engine/pkg/libcontainer/mount/init.go b/components/engine/pkg/libcontainer/mount/init.go index b0a3ef1061..d01e9d5c95 100644 --- a/components/engine/pkg/libcontainer/mount/init.go +++ b/components/engine/pkg/libcontainer/mount/init.go @@ -128,7 +128,7 @@ func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error { return err } if err := createIfNotExists(dest, stat.IsDir()); err != nil { - return fmt.Errorf("Creating new bind-mount target, %s\n", err) + return fmt.Errorf("Creating new bind-mount target, %s", err) } if err := system.Mount(m.Source, dest, "bind", uintptr(flags), ""); err != nil {