Skip to content
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: 4 additions & 5 deletions validation/linux_cgroups_blkio/linux_cgroups_blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ func testBlkioCgroups(rate uint64, isEmpty bool) error {
var weight uint16 = 500
var leafWeight uint16 = 300

// It's assumed that a device /dev/sda (8:0) exists on the test system.
// The minor number must be always 0, as it's only allowed to set blkio
// weights to a whole block device /dev/sda, not to partitions like sda1.
var major int64 = 8
var minor int64
major, minor, err := util.GetBlockDevice()
if err != nil {
return err
}

t := tap.New()
t.Header(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
func main() {
var weight uint16 = 500
var leafWeight uint16 = 300
var major, minor int64 = 8, 0
var rate uint64 = 102400

major, minor, err := util.GetBlockDevice()
if err != nil {
util.Fatal(err)
}

t := tap.New()
t.Header(0)
defer t.AutoPlan()
Expand Down
34 changes: 34 additions & 0 deletions validation/util/linux_resources_blkio.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
package util

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/mndrix/tap-go"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/cgroups"
)

// GetBlockDevice returns the device number of a whole block device on
// the host, as blkio device rules have to name one that exists. Devices
// backed by hardware are preferred over loop and RAM disks, which may
// have nothing behind them.
func GetBlockDevice() (int64, int64, error) {
devs, err := filepath.Glob("/sys/block/*/dev")
if err != nil {
return 0, 0, err
}

for _, hardware := range []bool{true, false} {
for _, dev := range devs {
if _, err := os.Stat(filepath.Join(filepath.Dir(dev), "device")); hardware && err != nil {
continue
}
contents, err := os.ReadFile(dev)
if err != nil {
continue
}
var major, minor int64
if _, err := fmt.Sscanf(strings.TrimSpace(string(contents)), "%d:%d", &major, &minor); err != nil {
continue
}
return major, minor, nil
}
}

return 0, 0, errors.New("no block device found in /sys/block")
}

// ValidateLinuxResourcesBlockIO validates linux.resources.blockIO.
func ValidateLinuxResourcesBlockIO(config *rspec.Spec, t *tap.T, state *rspec.State) error {
cg, err := cgroups.FindCgroup()
Expand Down