From a68b254c98bfffe7ab8d2481477c9a7807bf11a0 Mon Sep 17 00:00:00 2001 From: silenceper Date: Thu, 9 May 2024 20:34:30 +0800 Subject: [PATCH] support special backingFsBlockDev file --- cmd/xfsquota/xfsquota.go | 9 ++++--- pkg/projectquota/projectquota.go | 46 +++++++++++++++++++------------- pkg/xfsquota/quota.go | 12 ++++----- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/cmd/xfsquota/xfsquota.go b/cmd/xfsquota/xfsquota.go index b56a2bf..a4f0c17 100644 --- a/cmd/xfsquota/xfsquota.go +++ b/cmd/xfsquota/xfsquota.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/silenceper/xfsquota/pkg/xfsquota" "github.com/spf13/cobra" ) @@ -16,6 +17,7 @@ var ( Use: "xfsquota", Short: "xfsquota is a tool for managing XFS quotas", } + backingFsBlockDev string ) var getQuotaCmd = &cobra.Command{ @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/pkg/projectquota/projectquota.go b/pkg/projectquota/projectquota.go index ba88040..02fef4e 100644 --- a/pkg/projectquota/projectquota.go +++ b/pkg/projectquota/projectquota.go @@ -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" ) @@ -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 @@ -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 { @@ -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) } @@ -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 @@ -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) @@ -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 diff --git a/pkg/xfsquota/quota.go b/pkg/xfsquota/quota.go index 08b5e7f..dbd59f7 100644 --- a/pkg/xfsquota/quota.go +++ b/pkg/xfsquota/quota.go @@ -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 @@ -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) }