From 75c866d6a3aa92d5d2a4e6948fff00f90b36e872 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Wed, 20 Mar 2013 22:41:03 -0700 Subject: [PATCH] Unmount() and Mounted(): utility functions to unmount a mountpoint and check if it's mounted, respectively --- graph/image.go | 4 ++-- graph/mount.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 graph/mount.go diff --git a/graph/image.go b/graph/image.go index 9cb72a9632..37ccc41a8f 100644 --- a/graph/image.go +++ b/graph/image.go @@ -92,9 +92,9 @@ func MountAUFS(ro []string, rw string, target string) error { } func (image *Image) Mount(root, rw string) error { - if isMounted, err := IsMounted(root); err != nil { + if mounted, err := Mounted(root); err != nil { return err - } else if isMounted { + } else if mounted { return fmt.Errorf("%s is already mounted", root) } layers, err := image.layers() diff --git a/graph/mount.go b/graph/mount.go new file mode 100644 index 0000000000..ebf8485285 --- /dev/null +++ b/graph/mount.go @@ -0,0 +1,48 @@ +package graph + +import ( + "fmt" + "os" + "path/filepath" + "syscall" + "time" +) + +func Unmount(target string) error { + if err := syscall.Unmount(target, 0); err != nil { + return err + } + // Even though we just unmounted the filesystem, AUFS will prevent deleting the mntpoint + // for some time. We'll just keep retrying until it succeeds. + for retries := 0; retries < 1000; retries++ { + err := os.Remove(target) + if err == nil { + // rm mntpoint succeeded + return nil + } + if os.IsNotExist(err) { + // mntpoint doesn't exist anymore. Success. + return nil + } + // fmt.Printf("(%v) Remove %v returned: %v\n", retries, target, err) + time.Sleep(10 * time.Millisecond) + } + return fmt.Errorf("Umount: Failed to umount %v", target) +} + +func Mounted(mountpoint string) (bool, error) { + mntpoint, err := os.Stat(mountpoint) + if err != nil { + if os.IsNotExist(err) { + return false, nil + } + return false, err + } + parent, err := os.Stat(filepath.Join(mountpoint, "..")) + if err != nil { + return false, err + } + mntpointSt := mntpoint.Sys().(*syscall.Stat_t) + parentSt := parent.Sys().(*syscall.Stat_t) + return mntpointSt.Dev != parentSt.Dev, nil +}