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

support special backingFsBlockDev file #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions cmd/xfsquota/xfsquota.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/silenceper/xfsquota/pkg/xfsquota"
"github.com/spf13/cobra"
)
Expand All @@ -16,6 +17,7 @@ var (
Use: "xfsquota",
Short: "xfsquota is a tool for managing XFS quotas",
}
backingFsBlockDev string
)

var getQuotaCmd = &cobra.Command{
Expand All @@ -27,7 +29,7 @@ var getQuotaCmd = &cobra.Command{
cmd.Help()
return
}
quotaRes, err := xfsQuota.GetQuota(args[0])
quotaRes, err := xfsQuota.GetQuota(args[0], backingFsBlockDev)
if err != nil {
fmt.Println(err)
return
Expand All @@ -50,7 +52,7 @@ var setQuotaCmd = &cobra.Command{
cmd.Help()
return
}
err := xfsQuota.SetQuota(args[0], size, inodes)
err := xfsQuota.SetQuota(args[0], size, inodes, backingFsBlockDev)
if err != nil {
fmt.Println(err)
return
Expand All @@ -68,7 +70,7 @@ var cleanQuotaCmd = &cobra.Command{
cmd.Help()
return
}
err := xfsQuota.CleanQuota(args[0])
err := xfsQuota.CleanQuota(args[0], backingFsBlockDev)
if err != nil {
fmt.Println(err)
return
Expand All @@ -89,6 +91,7 @@ var versionCmd = &cobra.Command{
func init() {
setQuotaCmd.Flags().StringVarP(&size, "size", "s", "0", "quota size")
setQuotaCmd.Flags().StringVarP(&inodes, "inodes", "i", "0", "quota inodes")
rootCmd.PersistentFlags().StringVar(&backingFsBlockDev, "backingFsBlockDev", "", "backing filesystem block device")
rootCmd.AddCommand(getQuotaCmd)
rootCmd.AddCommand(setQuotaCmd)
rootCmd.AddCommand(cleanQuotaCmd)
Expand Down
46 changes: 28 additions & 18 deletions pkg/projectquota/projectquota.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ import "C"
import (
"errors"
"fmt"
"github.com/silenceper/xfsquota/pkg/mountpoint"
"path"
"strings"
"unsafe"

"github.com/silenceper/xfsquota/pkg/mountpoint"

"golang.org/x/sys/unix"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -151,10 +152,13 @@ func NewProjectQuota() *ProjectQuota {
}

// GetQuota get disk quota for the target path
func (p *ProjectQuota) GetQuota(targetPath string) (*DiskQuotaSize, error) {
backingFsBlockDev, err := p.findAvailableBackingDev(targetPath)
if err != nil {
return nil, err
func (p *ProjectQuota) GetQuota(targetPath string, backingFsBlockDev string) (*DiskQuotaSize, error) {
if backingFsBlockDev == "" {
backingFsBlockDevObj, err := p.findAvailableBackingDev(targetPath)
if err != nil {
return nil, err
}
backingFsBlockDev = backingFsBlockDevObj.device
}

// no need to create new project id
Expand All @@ -167,23 +171,26 @@ func (p *ProjectQuota) GetQuota(targetPath string) (*DiskQuotaSize, error) {
return nil, fmt.Errorf("no project id found(maybe not set quota) for %s", targetPath)
}

return getProjectQuota(backingFsBlockDev.device, prjId)
return getProjectQuota(backingFsBlockDev, prjId)
}

// SetQuota set quota limit for the path
func (p *ProjectQuota) SetQuota(targetPath string, size *DiskQuotaSize) error {
func (p *ProjectQuota) SetQuota(targetPath string, size *DiskQuotaSize, backingFsBlockDev string) error {
//if first set size is 0 , can not set nonzero size again,we must use clean quota
if size == nil {
return nil
}

backingFsBlockDev, err := p.findAvailableBackingDev(targetPath)
if err != nil {
return err
if backingFsBlockDev == "" {
backingFsBlockDevObj, err := p.findAvailableBackingDev(targetPath)
if err != nil {
return err
}
backingFsBlockDev = backingFsBlockDevObj.device
}

var prjId quotaID
var isNewId bool
var err error

prjId, isNewId, err = p.findOrCreateProjectId(targetPath, !projIdNoCreate, persistToFile)
if err != nil {
Expand All @@ -192,7 +199,7 @@ func (p *ProjectQuota) SetQuota(targetPath string, size *DiskQuotaSize) error {

if isNewId {
klog.V(2).Infof("set disk quota for path(%s) with size: %+v", targetPath, size)
err = setProjectQuota(backingFsBlockDev.device, prjId, size)
err = setProjectQuota(backingFsBlockDev, prjId, size)
if err != nil {
klog.Errorf("set disk quota for path(%s) with size(%+v) failed: %v", targetPath, size, err)
}
Expand All @@ -205,10 +212,13 @@ func (p *ProjectQuota) SetQuota(targetPath string, size *DiskQuotaSize) error {
}

// ClearQuota clear quota limit for the path
func (p *ProjectQuota) ClearQuota(targetPath string) error {
backingFsBlockDev, err := p.findAvailableBackingDev(targetPath)
if err != nil {
return err
func (p *ProjectQuota) ClearQuota(targetPath, backingFsBlockDev string) error {
if backingFsBlockDev == "" {
backingFsBlockDevObj, err := p.findAvailableBackingDev(targetPath)
if err != nil {
return err
}
backingFsBlockDev = backingFsBlockDevObj.device
}

// no need to create new project id
Expand All @@ -225,7 +235,7 @@ func (p *ProjectQuota) ClearQuota(targetPath string) error {
Quota: 0,
Inodes: 0,
}
err = setProjectQuota(backingFsBlockDev.device, prjId, size)
err = setProjectQuota(backingFsBlockDev, prjId, size)
if err != nil {
// just warning
klog.Errorf("set zero quota failed for path(%s) with id(%d): %v", targetPath, prjId, err)
Expand Down Expand Up @@ -332,7 +342,7 @@ func (p *ProjectQuota) findOrCreateBackingDev(targetPath string) (*backingDev, e
return backingDevice, nil
}

//findOrCreateSharedProjectId check if the path already has an shared project id, creating if not.
// findOrCreateSharedProjectId check if the path already has an shared project id, creating if not.
func (p *ProjectQuota) findOrCreateSharedProjectId(targetPath, projName string) (quotaID, bool, error) {
isNewId := false

Expand Down
12 changes: 6 additions & 6 deletions pkg/xfsquota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func NewXfsQuota() *XfsQuota {
}

// GetQuota returns the quota for the given path
func (q *XfsQuota) GetQuota(path string) (*projectquota.DiskQuotaSize, error) {
return q.ProjectQuota.GetQuota(path)
func (q *XfsQuota) GetQuota(path, backingFsBlockDev string) (*projectquota.DiskQuotaSize, error) {
return q.ProjectQuota.GetQuota(path, backingFsBlockDev)
}

// SetQuota sets the quota for the given path
func (q *XfsQuota) SetQuota(path string, sizeVal, inodeVal string) error {
func (q *XfsQuota) SetQuota(path, sizeVal, inodeVal, backingFsBlockDev string) error {
size, err := units.RAMInBytes(sizeVal)
if err != nil {
return err
Expand All @@ -37,10 +37,10 @@ func (q *XfsQuota) SetQuota(path string, sizeVal, inodeVal string) error {
return q.ProjectQuota.SetQuota(path, &projectquota.DiskQuotaSize{
Quota: uint64(size),
Inodes: inodes,
})
}, backingFsBlockDev)
}

// CleanQuota clears the quota for the given path
func (q *XfsQuota) CleanQuota(path string) error {
return q.ProjectQuota.ClearQuota(path)
func (q *XfsQuota) CleanQuota(path, backingFsBlockDev string) error {
return q.ProjectQuota.ClearQuota(path, backingFsBlockDev)
}