Skip to content

Commit

Permalink
tools: add fix_quota_id tool
Browse files Browse the repository at this point in the history
Add fix_quota_id tool to fix multi quota id for one volume or mount path.

Signed-off-by: Rudy Zhang <[email protected]>
  • Loading branch information
rudyfly authored and allencloud committed Nov 5, 2018
1 parent 638b38f commit 023f02b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
36 changes: 35 additions & 1 deletion storage/quota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/alibaba/pouch/pkg/exec"
"github.com/alibaba/pouch/pkg/kernel"
"github.com/alibaba/pouch/pkg/system"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -122,12 +121,21 @@ func StartQuotaDriver(dir string) (string, error) {

// SetSubtree is used to set quota id for directory.
func SetSubtree(dir string, qid uint32) (uint32, error) {
if isRegular, err := CheckRegularFile(dir); err != nil || !isRegular {
logrus.Debugf("set quota skip not regular file: %s", dir)
return 0, err
}

return GQuotaDriver.SetSubtree(dir, qid)
}

// SetDiskQuota is used to set quota for directory.
func SetDiskQuota(dir string, size string, quotaID uint32) error {
logrus.Infof("set disk quota, dir: (%s), size: (%s), quotaID: (%d)", dir, size, quotaID)
if isRegular, err := CheckRegularFile(dir); err != nil || !isRegular {
logrus.Debugf("set quota skip not regular file: %s", dir)
return err
}
return GQuotaDriver.SetDiskQuota(dir, size, quotaID)
}

Expand All @@ -143,11 +151,21 @@ func GetQuotaIDInFileAttr(dir string) uint32 {

// SetQuotaIDInFileAttr is used to set file attributes of quota ID.
func SetQuotaIDInFileAttr(dir string, id uint32) error {
if isRegular, err := CheckRegularFile(dir); err != nil || !isRegular {
logrus.Debugf("set quota skip not regular file: %s", dir)
return err
}

return GQuotaDriver.SetQuotaIDInFileAttr(dir, id)
}

// SetQuotaIDInFileAttrNoOutput is used to set file attribute of quota ID without error.
func SetQuotaIDInFileAttrNoOutput(dir string, quotaID uint32) {
if isRegular, err := CheckRegularFile(dir); err != nil || !isRegular {
logrus.Debugf("set quota skip not regular file: %s", dir)
return
}

GQuotaDriver.SetQuotaIDInFileAttrNoOutput(dir, quotaID)
}

Expand Down Expand Up @@ -217,6 +235,22 @@ func SetQuotaForDir(src string, quotaID uint32) error {
return nil
}

// CheckRegularFile is used to check the file is regular file or directory.
func CheckRegularFile(file string) (bool, error) {
fd, err := os.Lstat(file)
if err != nil {
logrus.Warnf("failed to check file: %s, err: %v", file, err)
return false, err
}

mode := fd.Mode()
if mode&(os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) == 0 {
return true, nil
}

return false, nil
}

// getOverlayMountInfo gets overlayFS informantion from /proc/mounts.
// upperdir, mergeddir and workdir would be dealt.
func getOverlayMountInfo(basefs string) (*OverlayMount, error) {
Expand Down
70 changes: 70 additions & 0 deletions tools/fix_quota_id/fix_quota_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"os"

"github.com/alibaba/pouch/storage/quota"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
dir string
size string
quotaID uint32
recursive bool
)

func run(cmd *cobra.Command) error {
_, err := quota.StartQuotaDriver(dir)
if err != nil {
logrus.Errorf("failed to start quota driver for %s, err: %v", dir, err)
return err
}

err = quota.SetDiskQuota(dir, size, quotaID)
if err != nil {
logrus.Errorf("failed to set subtree for %s, quota id: %d, err: %v", dir, quotaID, err)
return err
}

if !recursive {
return nil
}

if err := quota.SetQuotaForDir(dir, quotaID); err != nil {
logrus.Errorf("failed to set quota id for %s recursively, quota id: %d, err: %v", dir, quotaID, err)
return err
}

return nil
}

func setupFlags(cmd *cobra.Command) {
flagSet := cmd.Flags()

flagSet.StringVarP(&dir, "dir", "d", "", "The directory is set quota id.")
flagSet.StringVarP(&size, "size", "s", "", "The limit of directory")
flagSet.Uint32VarP(&quotaID, "quota-id", "i", 0, "The quota id to set.")
flagSet.BoolVarP(&recursive, "recursive", "r", true, "Set the directory recursively.")
}

func main() {
var cmdServe = &cobra.Command{
Use: "fix_quota_id <-d /path> <-s size> <-i id>",
Short: "Set the quota id of path",
Long: "Set the quota id of path, also can set its sub-directory by recursively",
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return run(cmd)
},
}

setupFlags(cmdServe)
if err := cmdServe.Execute(); err != nil {
logrus.Error(err)
os.Exit(1)
}
}

0 comments on commit 023f02b

Please sign in to comment.