From 89ac644c2d105724a3b78d49d9d11d659f5f5532 Mon Sep 17 00:00:00 2001 From: Rudy Zhang Date: Thu, 19 Apr 2018 20:02:00 +0800 Subject: [PATCH] feature: set volumes to /etc/mtab set volumes to /etc/mtab in container, it makes the "df" command to show the volume's information. Signed-off-by: Rudy Zhang --- daemon/mgr/spec.go | 2 ++ daemon/mgr/spec_volume.go | 64 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/daemon/mgr/spec.go b/daemon/mgr/spec.go index acf7cdb659..0391262a91 100644 --- a/daemon/mgr/spec.go +++ b/daemon/mgr/spec.go @@ -79,6 +79,8 @@ var setupFunc = []SetupFunc{ //hook setupHook, + + setMountTab, } // Register is used to registe spec setup function. diff --git a/daemon/mgr/spec_volume.go b/daemon/mgr/spec_volume.go index e3e6ab4a10..7221099bda 100644 --- a/daemon/mgr/spec_volume.go +++ b/daemon/mgr/spec_volume.go @@ -3,7 +3,11 @@ package mgr import ( "context" "fmt" + "io/ioutil" + "os" + "path/filepath" + "github.com/alibaba/pouch/storage/quota" specs "github.com/opencontainers/runtime-spec/specs-go" ) @@ -78,3 +82,63 @@ func setupMounts(ctx context.Context, c *ContainerMeta, spec *SpecWrapper) error } return nil } + +func setMountTab(ctx context.Context, c *ContainerMeta, spec *SpecWrapper) error { + if len(c.BaseFS) == 0 { + return nil + } + + // set rootfs mount tab + context := "/ / ext4 rw 0 0\n" + if rootId, e := quota.GetDevID(c.BaseFS); e == nil { + _, _, rootFsType := quota.CheckMountpoint(rootId) + if len(rootFsType) > 0 { + context = fmt.Sprintf("/ / %s rw 0 0\n", rootFsType) + } + } + + // set mount point tab + i := 1 + for _, m := range c.Mounts { + if m.Source == "" || m.Destination == "" { + continue + } + + finfo, err := os.Stat(m.Source) + if err != nil || !finfo.IsDir() { + continue + } + + tempLine := fmt.Sprintf("/dev/v%02dd %s ext4 rw 0 0\n", i, m.Destination) + if tmpId, e := quota.GetDevID(m.Source); e == nil { + _, _, fsType := quota.CheckMountpoint(tmpId) + if len(fsType) > 0 { + tempLine = fmt.Sprintf("/dev/v%02dd %s %s rw 0 0\n", i, m.Destination, fsType) + } + } + + context += tempLine + } + + // set shm mount tab + context += "shm /dev/shm tmpfs rw 0 0\n" + + // save into mtab file. + mtabPath := filepath.Join(c.BaseFS, "etc/mtab") + hostmtabPath := filepath.Join(spec.ctrMgr.(*ContainerManager).Store.BaseDir, c.ID, "mtab") + + os.Remove(hostmtabPath) + os.MkdirAll(filepath.Dir(hostmtabPath), 0755) + err := ioutil.WriteFile(hostmtabPath, []byte(context), 0644) + if err != nil { + return fmt.Errorf("write %s failure", hostmtabPath) + } + + mtabPrestart := specs.Hook{ + Path: "/bin/cp", + Args: []string{"-f", hostmtabPath, mtabPath}, + } + spec.s.Hooks.Prestart = append(spec.s.Hooks.Prestart, mtabPrestart) + + return nil +}