-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validation: add cgroup devices validation
Signed-off-by: Zhou Hao <[email protected]>
- Loading branch information
Zhou Hao
committed
May 11, 2018
1 parent
1f9c0f1
commit 762800c
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/opencontainers/runtime-tools/cgroups" | ||
"github.com/opencontainers/runtime-tools/validation/util" | ||
) | ||
|
||
func main() { | ||
var major1, minor1, major2, minor2, major3, minor3 int64 = 10, 229, 8, 0, 10, 200 | ||
g, err := util.GetDefaultGenerator() | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
g.SetLinuxCgroupsPath(cgroups.AbsCgroupPath) | ||
g.AddLinuxResourcesDevice(true, "c", &major1, &minor1, "rwm") | ||
g.AddLinuxResourcesDevice(false, "b", &major2, &minor2, "rw") | ||
g.AddLinuxResourcesDevice(true, "a", &major3, &minor3, "r") | ||
err = util.RuntimeOutsideValidate(g, util.ValidateLinuxResourcesDevices) | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/opencontainers/runtime-tools/cgroups" | ||
"github.com/opencontainers/runtime-tools/validation/util" | ||
) | ||
|
||
func main() { | ||
var major1, minor1, major2, minor2, major3, minor3 int64 = 10, 229, 8, 0, 10, 200 | ||
g, err := util.GetDefaultGenerator() | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
g.SetLinuxCgroupsPath(cgroups.RelCgroupPath) | ||
g.AddLinuxResourcesDevice(true, "c", &major1, &minor1, "rwm") | ||
g.AddLinuxResourcesDevice(false, "b", &major2, &minor2, "rw") | ||
g.AddLinuxResourcesDevice(true, "a", &major3, &minor3, "r") | ||
err = util.RuntimeOutsideValidate(g, util.ValidateLinuxResourcesDevices) | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mndrix/tap-go" | ||
rspec "github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/opencontainers/runtime-tools/cgroups" | ||
) | ||
|
||
// ValidateLinuxResourcesDevices validates linux.resources.devices. | ||
func ValidateLinuxResourcesDevices(config *rspec.Spec, state *rspec.State) error { | ||
t := tap.New() | ||
t.Header(0) | ||
|
||
cg, err := cgroups.FindCgroup() | ||
t.Ok((err == nil), "find devices") | ||
if err != nil { | ||
t.Diagnostic(err.Error()) | ||
t.AutoPlan() | ||
return nil | ||
} | ||
|
||
lnd, err := cg.GetDevicesData(state.Pid, config.Linux.CgroupsPath) | ||
t.Ok((err == nil), "get devices data") | ||
if err != nil { | ||
t.Diagnostic(err.Error()) | ||
t.AutoPlan() | ||
return nil | ||
} | ||
|
||
for _, device := range config.Linux.Resources.Devices { | ||
for _, lid := range lnd { | ||
if lid.Allow == device.Allow { | ||
t.Ok(lid.Allow == device.Allow && lid.Type == device.Type && *lid.Major == *device.Major && | ||
*lid.Minor == *device.Minor && lid.Access == device.Access, | ||
fmt.Sprintf("devices %s %d:%d %s is set correctly", | ||
device.Type, *device.Major, *device.Minor, device.Access)) | ||
t.Diagnosticf("expect: %s %d:%d %s, actual: %s %d:%d %s", | ||
device.Type, *device.Major, *device.Minor, device.Access, lid.Type, *lid.Major, *lid.Minor, lid.Access) | ||
} | ||
} | ||
} | ||
|
||
t.AutoPlan() | ||
return nil | ||
} |