Skip to content

Commit

Permalink
Merge pull request #855 from Letty5411/0.2.1-0309
Browse files Browse the repository at this point in the history
bugfix: fix project quota can't be set on kernel-4.9
  • Loading branch information
allencloud authored Mar 9, 2018
2 parents 415badc + 7413782 commit 880f270
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
12 changes: 12 additions & 0 deletions pkg/quota/grpquota.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type GrpQuota struct {

// StartQuotaDriver is used to start quota driver.
func (quota *GrpQuota) StartQuotaDriver(dir string) (string, error) {
logrus.Debugf("start group quota driver: %s", dir)
if !UseQuota {
return "", nil
}
Expand Down Expand Up @@ -105,6 +106,7 @@ func (quota *GrpQuota) StartQuotaDriver(dir string) (string, error) {
// SetSubtree is used to set quota id for directory,
// setfattr -n system.subtree -v $QUOTAID
func (quota *GrpQuota) SetSubtree(dir string, qid uint32) (uint32, error) {
logrus.Debugf("set subtree, dir: %s, quotaID: %d", dir, qid)
if !UseQuota {
return 0, nil
}
Expand All @@ -129,6 +131,7 @@ func (quota *GrpQuota) SetSubtree(dir string, qid uint32) (uint32, error) {

// SetDiskQuota is used to set quota for directory.
func (quota *GrpQuota) SetDiskQuota(dir string, size string, quotaID int) error {
logrus.Debugf("set disk quota, dir: %s, size: %s, quotaID: %d", dir, size, quotaID)
if !UseQuota {
return nil
}
Expand Down Expand Up @@ -167,6 +170,7 @@ func (quota *GrpQuota) SetDiskQuota(dir string, size string, quotaID int) error
// cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
// cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
func (quota *GrpQuota) CheckMountpoint(devID uint64) (string, bool, string) {
logrus.Debugf("check mountpoint, devID: %d", devID)
output, err := ioutil.ReadFile("/proc/mounts")
if err != nil {
logrus.Warnf("ReadFile: %v", err)
Expand Down Expand Up @@ -198,6 +202,8 @@ func (quota *GrpQuota) CheckMountpoint(devID uint64) (string, bool, string) {
}

func (quota *GrpQuota) setUserQuota(quotaID uint32, diskQuota uint64, mountPoint string) error {
logrus.Debugf("set user quota, quotaID: %d, limit: %d, mountpoint: %s", quotaID, diskQuota, mountPoint)

uid := strconv.FormatUint(uint64(quotaID), 10)
limit := strconv.FormatUint(diskQuota, 10)

Expand All @@ -208,6 +214,8 @@ func (quota *GrpQuota) setUserQuota(quotaID uint32, diskQuota uint64, mountPoint
// GetFileAttr returns the directory attributes
// getfattr -n system.subtree --only-values --absolute-names /
func (quota *GrpQuota) GetFileAttr(dir string) uint32 {
logrus.Debugf("get file attr, dir: %s", dir)

v := 0
_, out, _, err := exec.Run(0, "getfattr", "-n", "system.subtree", "--only-values", "--absolute-names", dir)
if err == nil {
Expand All @@ -218,6 +226,8 @@ func (quota *GrpQuota) GetFileAttr(dir string) uint32 {

// SetFileAttr is used to set file attributes.
func (quota *GrpQuota) SetFileAttr(dir string, id uint32) error {
logrus.Debugf("set file attr, dir: %s, quotaID: %d", dir, id)

strid := strconv.FormatUint(uint64(id), 10)
_, _, _, err := exec.Run(0, "setfattr", "-n", "system.subtree", "-v", strid, dir)
return err
Expand Down Expand Up @@ -293,6 +303,8 @@ func (quota *GrpQuota) GetNextQuatoID() (uint32, error) {
}
quota.quotaIDs[id] = 1
quota.quotaLastID = id

logrus.Debugf("get next project quota id: %d", id)
return id, nil
}

Expand Down
17 changes: 15 additions & 2 deletions pkg/quota/prjquota.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type PrjQuota struct {

// StartQuotaDriver is used to start quota driver.
func (quota *PrjQuota) StartQuotaDriver(dir string) (string, error) {
logrus.Debugf("start project quota driver: %s", dir)
if !UseQuota {
return "", nil
}
Expand Down Expand Up @@ -58,9 +59,9 @@ func (quota *PrjQuota) StartQuotaDriver(dir string) (string, error) {
}

// on
_, out, _, err := exec.Run(0, "quotaon", "-P", mountPoint)
_, _, stderr, err := exec.Run(0, "quotaon", "-P", mountPoint)
if err != nil {
if strings.Contains(out, " File exists") {
if strings.Contains(stderr, " File exists") {
err = nil
} else {
mountPoint = ""
Expand All @@ -74,6 +75,7 @@ func (quota *PrjQuota) StartQuotaDriver(dir string) (string, error) {
// SetSubtree is used to set quota id for directory,
//chattr -p qid +P $QUOTAID
func (quota *PrjQuota) SetSubtree(dir string, qid uint32) (uint32, error) {
logrus.Debugf("set subtree, dir: %s, quotaID: %d", dir, qid)
if !UseQuota {
return 0, nil
}
Expand All @@ -98,6 +100,7 @@ func (quota *PrjQuota) SetSubtree(dir string, qid uint32) (uint32, error) {

// SetDiskQuota is used to set quota for directory.
func (quota *PrjQuota) SetDiskQuota(dir string, size string, quotaID int) error {
logrus.Debugf("set disk quota, dir: %s, size: %s, quotaID: %d", dir, size, quotaID)
if !UseQuota {
return nil
}
Expand Down Expand Up @@ -138,6 +141,7 @@ func (quota *PrjQuota) SetDiskQuota(dir string, size string, quotaID int) error
// cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
// cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
func (quota *PrjQuota) CheckMountpoint(devID uint64) (string, bool, string) {
logrus.Debugf("check mountpoint, devID: %d", devID)
output, err := ioutil.ReadFile("/proc/mounts")
if err != nil {
logrus.Warnf("ReadFile: %v", err)
Expand All @@ -153,6 +157,7 @@ func (quota *PrjQuota) CheckMountpoint(devID uint64) (string, bool, string) {
continue
}
devID2, _ := GetDevID(parts[1])

if devID == devID2 {
mountPoint = parts[1]
fsType = parts[2]
Expand All @@ -169,6 +174,8 @@ func (quota *PrjQuota) CheckMountpoint(devID uint64) (string, bool, string) {
}

func (quota *PrjQuota) setUserQuota(quotaID uint32, diskQuota uint64, mountPoint string) error {
logrus.Debugf("set user quota, quotaID: %d, limit: %d, mountpoint: %s", quotaID, diskQuota, mountPoint)

uid := strconv.FormatUint(uint64(quotaID), 10)
limit := strconv.FormatUint(diskQuota, 10)
_, _, _, err := exec.Run(0, "setquota", "-P", uid, "0", limit, "0", "0", mountPoint)
Expand All @@ -178,6 +185,8 @@ func (quota *PrjQuota) setUserQuota(quotaID uint32, diskQuota uint64, mountPoint
// GetFileAttr returns the directory attributes
// lsattr -p $dir
func (quota *PrjQuota) GetFileAttr(dir string) uint32 {
logrus.Debugf("get file attr, dir: %s", dir)

v := 0
_, out, _, err := exec.Run(0, "lsattr", "-p", dir)
if err == nil {
Expand All @@ -189,6 +198,8 @@ func (quota *PrjQuota) GetFileAttr(dir string) uint32 {

// SetFileAttr is used to set file attributes.
func (quota *PrjQuota) SetFileAttr(dir string, id uint32) error {
logrus.Debugf("set file attr, dir: %s, quotaID: %d", dir, id)

strid := strconv.FormatUint(uint64(id), 10)
_, _, _, err := exec.Run(0, "chattr", "-p", strid, "+P", dir)
return err
Expand Down Expand Up @@ -260,6 +271,8 @@ func (quota *PrjQuota) GetNextQuatoID() (uint32, error) {
}
quota.quotaIDs[id] = 1
quota.quotaLastID = id

logrus.Debugf("get next project quota id: %d", id)
return id, nil
}

Expand Down

0 comments on commit 880f270

Please sign in to comment.