Skip to content

Commit

Permalink
feature: set volumes to /etc/mtab
Browse files Browse the repository at this point in the history
set volumes to /etc/mtab in container, it makes the "df" command to show
the volume's information.

Signed-off-by: Rudy Zhang <[email protected]>
  • Loading branch information
rudyfly committed Apr 25, 2018
1 parent a8c1729 commit 8143edd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,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, &quota.RegExp{re, path, size})
res = append(res, &quota.RegExp{Pattern: re, Path: path, Size: size})
}

for _, mp := range c.Mounts {
Expand Down
68 changes: 68 additions & 0 deletions daemon/mgr/spec_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mgr

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand All @@ -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
Expand Down Expand Up @@ -69,6 +72,71 @@ func setupHook(ctx context.Context, c *ContainerMeta, specWrapper *SpecWrapper)
})
}

// 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
}

Expand Down

0 comments on commit 8143edd

Please sign in to comment.