Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: set volumes to /etc/mtab #1170

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,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
70 changes: 70 additions & 0 deletions daemon/mgr/spec_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package mgr

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/alibaba/pouch/storage/quota"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a blank line below this line, thanks a lot


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 @@ -71,6 +75,72 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable i should increase?

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

i++
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