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 20, 2018
1 parent d0ba3a3 commit 89ac644
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions daemon/mgr/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ var setupFunc = []SetupFunc{

//hook
setupHook,

setMountTab,
}

// Register is used to registe spec setup function.
Expand Down
64 changes: 64 additions & 0 deletions daemon/mgr/spec_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

0 comments on commit 89ac644

Please sign in to comment.