-
Notifications
You must be signed in to change notification settings - Fork 142
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 <zhouhao@cn.fujitsu.com>
- Loading branch information
Zhou Hao
committed
May 21, 2018
1 parent
1f9c0f1
commit 1794938
Showing
4 changed files
with
143 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, 20, 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(true, "b", &major2, &minor2, "rw") | ||
g.AddLinuxResourcesDevice(true, "b", &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, 20, 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(true, "b", &major2, &minor2, "rw") | ||
g.AddLinuxResourcesDevice(true, "b", &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,53 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mndrix/tap-go" | ||
rspec "github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/opencontainers/runtime-tools/cgroups" | ||
"github.com/opencontainers/runtime-tools/specerror" | ||
) | ||
|
||
// 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 i, device := range config.Linux.Resources.Devices { | ||
if device.Allow == true { | ||
found := false | ||
if lnd[i-1].Type == device.Type && *lnd[i-1].Major == *device.Major && *lnd[i-1].Minor == *device.Minor && lnd[i-1].Access == device.Access { | ||
found = true | ||
} | ||
t.Ok(found, 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, lnd[i-1].Type, *lnd[i-1].Major, *lnd[i-1].Minor, lnd[i-1].Access) | ||
if !found { | ||
err := specerror.NewError(specerror.DevicesApplyInOrder, fmt.Errorf("The runtime MUST apply entries in the listed order"), rspec.Version) | ||
t.Diagnostic(err.Error()) | ||
t.AutoPlan() | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
t.AutoPlan() | ||
return nil | ||
} |