From bcf22a4611a19b751c26ea8ca6d142623804460b 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/container.go | 2 +- daemon/mgr/spec_hook.go | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index cdc98c6852..f0c4b68564 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -2188,7 +2188,7 @@ func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *Cont var res []*quota.RegExp for path, size := range quotas { re := regexp.MustCompile(path) - res = append(res, "a.RegExp{re, path, size}) + res = append(res, "a.RegExp{Pattern: re, Path: path, Size: size}) } for _, mp := range c.Mounts { diff --git a/daemon/mgr/spec_hook.go b/daemon/mgr/spec_hook.go index 18bbc8d906..ed10a60b75 100644 --- a/daemon/mgr/spec_hook.go +++ b/daemon/mgr/spec_hook.go @@ -2,6 +2,8 @@ package mgr import ( "context" + "fmt" + "io/ioutil" "os" "path/filepath" "sort" @@ -10,6 +12,7 @@ import ( "github.com/alibaba/pouch/storage/quota" specs "github.com/opencontainers/runtime-spec/specs-go" + "github.com/pkg/errors" ) //setup hooks specified by user via plugins, if set rich mode and init-script exists set init-script @@ -71,6 +74,71 @@ func setupHook(ctx context.Context, c *ContainerMeta, specWrapper *SpecWrapper) Args: []string{"set-diskquota", c.BaseFS, rootFSQuota, qid}, }) + // set volume mount tab + if err := setMountTab(ctx, c, specWrapper); err != nil { + return errors.Wrap(err, "failed to set volume mount tab prestart hook") + } + + 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 }