From caaee11b33930dd2ef73ef5578f3727d80219c60 Mon Sep 17 00:00:00 2001 From: Ivan Shvedunov Date: Sat, 9 Feb 2019 21:22:55 +0300 Subject: [PATCH] Fix 9pfs mounts of hostPath volumes hostPath was only working for the host paths that are mounted inside the virtlet container. --- pkg/fs/fs.go | 3 ++- pkg/fs/fs_linux.go | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go index cf2fa21e1..c41b92dbd 100644 --- a/pkg/fs/fs.go +++ b/pkg/fs/fs.go @@ -51,7 +51,8 @@ type DelimitedReader interface { // FileSystem defines a filesystem interface interface type FileSystem interface { // Mount mounts the specified source under the target path. - // For bind mounts, bind must be true. + // For bind mounts, bind must be true. The bind mounts will + // happen in the host mount namespace (that of PID 1). Mount(source string, target string, fstype string, bind bool) error // Unmount unmounts the specified target directory. If detach // is true, MNT_DETACH option is used (disconnect the diff --git a/pkg/fs/fs_linux.go b/pkg/fs/fs_linux.go index 5636a238c..74f2ba880 100644 --- a/pkg/fs/fs_linux.go +++ b/pkg/fs/fs_linux.go @@ -18,15 +18,27 @@ limitations under the License. package fs -import "syscall" +import ( + "fmt" + "os/exec" + "syscall" +) // Mount inplements Mount method of FileSystem interface. func (fs *realFileSystem) Mount(source string, target string, fstype string, bind bool) error { - flags := uintptr(0) - if bind { - flags = syscall.MS_BIND | syscall.MS_REC + if !bind { + return syscall.Mount(source, target, fstype, uintptr(0), "") } - return syscall.Mount(source, target, fstype, flags, "") + + // In case of bind mounts, we want to do it in the outer mount namespace. + // This is used for hostPath volumes, for example. + args := []string{"/usr/bin/nsenter", "-t", "1", "-m", "/bin/mount", "--bind", source, target} + if out, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil { + return fmt.Errorf("mount %v: %v; output: %v", args, err, string(out)) + } + + return nil + } // Unmount inplements Unmount method of FileSystem interface.