Skip to content

Commit

Permalink
Finish "iolimit" command
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Apr 3, 2024
1 parent bea7e54 commit f006fec
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
32 changes: 27 additions & 5 deletions cmd/iolimit/iolimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package iolimit

import (
"errors"
"fmt"
"strings"

"github.com/USTC-vlab/vct/pkg/cgroup"
"github.com/USTC-vlab/vct/pkg/pve"
"github.com/spf13/cobra"
)

Expand All @@ -16,8 +19,8 @@ func MakeCmd() *cobra.Command {
}
flags := cmd.Flags()
var iops cgroup.IOPS
flags.Int64VarP(&iops.Rbps, "rbps", "", 0, "set read bandwidth limit")
flags.Int64VarP(&iops.Wbps, "wbps", "", 0, "set write bandwidth limit")
flags.Int64VarP(&iops.Rbps, "rbps", "R", 0, "set read bandwidth limit")
flags.Int64VarP(&iops.Wbps, "wbps", "W", 0, "set write bandwidth limit")
flags.Int64VarP(&iops.Riops, "riops", "r", 0, "set read IOPS limit")
flags.Int64VarP(&iops.Wiops, "wiops", "w", 0, "set write IOPS limit")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
Expand All @@ -34,10 +37,29 @@ func MakeCmd() *cobra.Command {
}

func iolimitMain(ctid string, iops cgroup.IOPS) error {
// TODO: Find major:minor
pveStorage, err := pve.GetStorage()
if err != nil {
return err
}

config, err := pve.GetLXCConfig(ctid)
if err != nil {
return err
}
rootfs := config["rootfs"]
rootfsIdent := strings.SplitN(rootfs, ",", 2)[0]
rootfsParts := strings.Split(rootfsIdent, ":")
if len(rootfsParts) != 2 {
return fmt.Errorf("invalid rootfs %s", rootfsIdent)
}

major, minor, err := pve.GetBlockDevForStorage(rootfsParts[0], rootfsParts[1], pveStorage)
if err != nil {
return err
}
iopsline := cgroup.IOPSLine{
Major: 0,
Minor: 0,
Major: major,
Minor: minor,
IOPS: iops,
}
return cgroup.SetIOPSForLXC(ctid, iopsline)
Expand Down
4 changes: 2 additions & 2 deletions pkg/pve/pct.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func parseConfig(r io.Reader) map[string]string {
// Invalid line?
continue
}
config[fields[0]] = fields[1]
config[strings.TrimSuffix(fields[0], ":")] = fields[1]
}
return config
}
Expand All @@ -45,7 +45,7 @@ func GetConfig(typ, vmid string) (map[string]string, error) {
if typ == "qemu" {
typ = "qemu-server"
}
f, err := os.Open(fmt.Sprintf("%s/%s/%s.cfg", EtcPve, typ, vmid))
f, err := os.Open(fmt.Sprintf("%s/%s/%s.conf", EtcPve, typ, vmid))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/pve/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func getBlockDevForLVM(vgname, lvname string) (uint64, uint64, error) {
// The "aux" parameter is used to help determine the type of the storage.
func GetBlockDevForStorage(storage, name string, aux []PVEStorage) (uint64, uint64, error) {
i := slices.IndexFunc(aux, func(s PVEStorage) bool {
return s.Name == name
return s.Name == storage
})
if i == -1 {
return 0, 0, fs.ErrNotExist
Expand Down

0 comments on commit f006fec

Please sign in to comment.