Skip to content

Commit e6bee7d

Browse files
Zhai Zhaoxuanzhouhao
Zhai Zhaoxuan
authored and
zhouhao
committed
Add manpages and bash-completion for --device-access-add and --device-access-remove
Signed-off-by: Zhai Zhaoxuan <[email protected]> Signed-off-by: zhouhao <[email protected]>
1 parent 60c7e77 commit e6bee7d

File tree

4 files changed

+44
-48
lines changed

4 files changed

+44
-48
lines changed

cmd/oci-runtime-tool/generate.go

+24-40
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ import (
1818

1919
var generateFlags = []cli.Flag{
2020
cli.StringSliceFlag{Name: "args", Usage: "command to run in the container"},
21-
<<<<<<< 9e0e42dbf918070406a2a4a2e1476e7350ba9129
22-
=======
23-
cli.StringSliceFlag{Name: "bind", Usage: "bind mount directories src:dest[:options...]"},
24-
cli.StringSliceFlag{Name: "cap-add", Usage: "add Linux capabilities"},
25-
cli.StringSliceFlag{Name: "cap-drop", Usage: "drop Linux capabilities"},
26-
cli.StringFlag{Name: "cgroup", Usage: "cgroup namespace"},
27-
cli.StringFlag{Name: "cgroups-path", Usage: "specify the path to the cgroups"},
28-
cli.StringFlag{Name: "cwd", Value: "/", Usage: "current working directory for the process"},
29-
cli.StringSliceFlag{Name: "device-access-add", Usage: "add a device access rule"},
30-
cli.StringSliceFlag{Name: "device-access-remove", Usage: "remove a device access rule"},
31-
cli.BoolFlag{Name: "disable-oom-kill", Usage: "disable OOM Killer"},
32-
>>>>>>> generate: add --device-access-add and --device-access-remove option
3321
cli.StringSliceFlag{Name: "env", Usage: "add environment variable e.g. key=value"},
3422
cli.StringSliceFlag{Name: "env-file", Usage: "read in a file of environment variables"},
3523
cli.StringSliceFlag{Name: "hooks-poststart", Usage: "set command to run in poststart hooks"},
@@ -75,6 +63,8 @@ var generateFlags = []cli.Flag{
7563
cli.StringSliceFlag{Name: "linux-readonly-paths", Usage: "specifies paths readonly inside container"},
7664
cli.Int64Flag{Name: "linux-realtime-period", Usage: "CPU period to be used for realtime scheduling (in usecs)"},
7765
cli.Int64Flag{Name: "linux-realtime-runtime", Usage: "the time realtime scheduling may use (in usecs)"},
66+
cli.StringSliceFlag{Name: "linux-resources-device-add", Usage: "add a device access rule"},
67+
cli.StringSliceFlag{Name: "linux-resources-device-remove", Usage: "remove a device access rule"},
7868
cli.StringFlag{Name: "linux-rootfs-propagation", Usage: "mount propagation for rootfs"},
7969
cli.StringFlag{Name: "linux-seccomp-allow", Usage: "specifies syscalls to respond with allow"},
8070
cli.StringFlag{Name: "linux-seccomp-arch", Usage: "specifies additional architectures permitted to be used for system calls"},
@@ -246,12 +236,15 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
246236
g.SetLinuxCgroupsPath(context.String("linux-cgroups-path"))
247237
}
248238

249-
<<<<<<< 9e0e42dbf918070406a2a4a2e1476e7350ba9129
250239
if context.IsSet("linux-masked-paths") {
251240
paths := context.StringSlice("linux-masked-paths")
252-
=======
253-
if context.IsSet("device-access-add") {
254-
devices := context.StringSlice("device-access-add")
241+
for _, path := range paths {
242+
g.AddLinuxMaskedPaths(path)
243+
}
244+
}
245+
246+
if context.IsSet("linux-resources-device-add") {
247+
devices := context.StringSlice("linux-resources-device-add")
255248
for _, device := range devices {
256249
dev, err := parseLinuxResourcesDeviceAccess(device, g)
257250
if err != nil {
@@ -261,8 +254,8 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
261254
}
262255
}
263256

264-
if context.IsSet("device-access-remove") {
265-
devices := context.StringSlice("device-access-remove")
257+
if context.IsSet("linux-resources-device-remove") {
258+
devices := context.StringSlice("linux-resources-device-remove")
266259
for _, device := range devices {
267260
dev, err := parseLinuxResourcesDeviceAccess(device, g)
268261
if err != nil {
@@ -272,14 +265,6 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
272265
}
273266
}
274267

275-
if context.IsSet("masked-paths") {
276-
paths := context.StringSlice("masked-paths")
277-
>>>>>>> generate: add --device-access-add and --device-access-remove option
278-
for _, path := range paths {
279-
g.AddLinuxMaskedPaths(path)
280-
}
281-
}
282-
283268
if context.IsSet("linux-readonly-paths") {
284269
paths := context.StringSlice("linux-readonly-paths")
285270
for _, path := range paths {
@@ -850,7 +835,6 @@ func parseRlimit(rlimit string) (string, uint64, uint64, error) {
850835
return parts[0], uint64(hard), uint64(soft), nil
851836
}
852837

853-
<<<<<<< 9e0e42dbf918070406a2a4a2e1476e7350ba9129
854838
func parseNamespace(ns string) (string, string, error) {
855839
parts := strings.SplitN(ns, ":", 2)
856840
if len(parts) == 0 || parts[0] == "" {
@@ -944,7 +928,8 @@ func parseDevice(device string, g *generate.Generator) (rspec.LinuxDevice, error
944928
}
945929

946930
return dev, nil
947-
=======
931+
}
932+
948933
var cgroupDeviceType = map[string]bool{
949934
"a": true, // all
950935
"b": true, // block device
@@ -957,9 +942,9 @@ var cgroupDeviceAccess = map[string]bool{
957942
}
958943

959944
// parseLinuxResourcesDeviceAccess parses the raw string passed with the --device-access-add flag
960-
func parseLinuxResourcesDeviceAccess(device string, g *generate.Generator) (rspec.DeviceCgroup, error) {
945+
func parseLinuxResourcesDeviceAccess(device string, g *generate.Generator) (rspec.LinuxDeviceCgroup, error) {
961946
var allow bool
962-
var devType, access *string
947+
var devType, access string
963948
var major, minor *int64
964949

965950
argsParts := strings.Split(device, ",")
@@ -970,7 +955,7 @@ func parseLinuxResourcesDeviceAccess(device string, g *generate.Generator) (rspe
970955
case "deny":
971956
allow = false
972957
default:
973-
return rspec.DeviceCgroup{},
958+
return rspec.LinuxDeviceCgroup{},
974959
fmt.Errorf("Only 'allow' and 'deny' are allowed in the first field of device-access-add: %s", device)
975960
}
976961

@@ -981,45 +966,44 @@ func parseLinuxResourcesDeviceAccess(device string, g *generate.Generator) (rspe
981966
}
982967
parts := strings.SplitN(s, "=", 2)
983968
if len(parts) != 2 {
984-
return rspec.DeviceCgroup{}, fmt.Errorf("Incomplete device-access-add arguments: %s", s)
969+
return rspec.LinuxDeviceCgroup{}, fmt.Errorf("Incomplete device-access-add arguments: %s", s)
985970
}
986971
name, value := parts[0], parts[1]
987972

988973
switch name {
989974
case "type":
990975
if !cgroupDeviceType[value] {
991-
return rspec.DeviceCgroup{}, fmt.Errorf("Invalid device type in device-access-add: %s", value)
976+
return rspec.LinuxDeviceCgroup{}, fmt.Errorf("Invalid device type in device-access-add: %s", value)
992977
}
993-
devType = &value
978+
devType = value
994979
case "major":
995980
i, err := strconv.ParseInt(value, 10, 64)
996981
if err != nil {
997-
return rspec.DeviceCgroup{}, err
982+
return rspec.LinuxDeviceCgroup{}, err
998983
}
999984
major = &i
1000985
case "minor":
1001986
i, err := strconv.ParseInt(value, 10, 64)
1002987
if err != nil {
1003-
return rspec.DeviceCgroup{}, err
988+
return rspec.LinuxDeviceCgroup{}, err
1004989
}
1005990
minor = &i
1006991
case "access":
1007992
for _, c := range strings.Split(value, "") {
1008993
if !cgroupDeviceAccess[c] {
1009-
return rspec.DeviceCgroup{}, fmt.Errorf("Invalid device access in device-access-add: %s", c)
994+
return rspec.LinuxDeviceCgroup{}, fmt.Errorf("Invalid device access in device-access-add: %s", c)
1010995
}
1011996
}
1012-
access = &value
997+
access = value
1013998
}
1014999
}
1015-
return rspec.DeviceCgroup{
1000+
return rspec.LinuxDeviceCgroup{
10161001
Allow: allow,
10171002
Type: devType,
10181003
Major: major,
10191004
Minor: minor,
10201005
Access: access,
10211006
}, nil
1022-
>>>>>>> generate: add --device-access-add and --device-access-remove option
10231007
}
10241008

10251009
func addSeccomp(context *cli.Context, g *generate.Generator) error {

completions/bash/oci-runtime-tool

+2
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ _oci-runtime-tool_generate() {
347347
--linux-readonly-paths
348348
--linux-realtime-period
349349
--linux-realtime-runtime
350+
--linux-resources-device-add
351+
--linux-resources-device-remove
350352
--linux-rootfs-propagation
351353
--linux-seccomp-allow
352354
--linux-seccomp-arch

generate/generate.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,6 @@ func (g *Generator) RemoveLinuxNamespace(ns string) error {
11351135
return nil
11361136
}
11371137

1138-
<<<<<<< 9e0e42dbf918070406a2a4a2e1476e7350ba9129
11391138
// AddDevice - add a device into g.spec.Linux.Devices
11401139
func (g *Generator) AddDevice(device rspec.LinuxDevice) {
11411140
g.initSpecLinux()
@@ -1175,12 +1174,13 @@ func (g *Generator) ClearLinuxDevices() {
11751174
}
11761175

11771176
g.spec.Linux.Devices = []rspec.LinuxDevice{}
1178-
=======
1177+
}
1178+
11791179
// AddLinuxResourcesDevice - add a device into g.spec.Linux.Resources.Devices
1180-
func (g *Generator) AddLinuxResourcesDevice(allow bool, devType *string, major, minor *int64, access *string) {
1180+
func (g *Generator) AddLinuxResourcesDevice(allow bool, devType string, major, minor *int64, access string) {
11811181
g.initSpecLinuxResources()
11821182

1183-
device := rspec.DeviceCgroup{
1183+
device := rspec.LinuxDeviceCgroup{
11841184
Allow: allow,
11851185
Type: devType,
11861186
Access: access,
@@ -1191,14 +1191,14 @@ func (g *Generator) AddLinuxResourcesDevice(allow bool, devType *string, major,
11911191
}
11921192

11931193
// RemoveLinuxResourcesDevice - remove a device from g.spec.Linux.Resources.Devices
1194-
func (g *Generator) RemoveLinuxResourcesDevice(allow bool, devType *string, major, minor *int64, access *string) {
1194+
func (g *Generator) RemoveLinuxResourcesDevice(allow bool, devType string, major, minor *int64, access string) {
11951195
if g.spec == nil || g.spec.Linux == nil || g.spec.Linux.Resources == nil {
11961196
return
11971197
}
11981198
for i, device := range g.spec.Linux.Resources.Devices {
11991199
if device.Allow == allow &&
1200-
(devType == device.Type || (devType != nil && device.Type != nil && *devType == *device.Type)) &&
1201-
(access == device.Access || (access != nil && device.Access != nil && *access == *device.Access)) &&
1200+
(devType == device.Type || (devType != "" && device.Type != "" && devType == device.Type)) &&
1201+
(access == device.Access || (access != "" && device.Access != "" && access == device.Access)) &&
12021202
(major == device.Major || (major != nil && device.Major != nil && *major == *device.Major)) &&
12031203
(minor == device.Minor || (minor != nil && device.Minor != nil && *minor == *device.Minor)) {
12041204

@@ -1207,7 +1207,6 @@ func (g *Generator) RemoveLinuxResourcesDevice(allow bool, devType *string, majo
12071207
}
12081208
}
12091209
return
1210-
>>>>>>> generate: add --device-access-add and --device-access-remove option
12111210
}
12121211

12131212
// strPtr returns the pointer pointing to the string s.

man/oci-runtime-tool-generate.1.md

+11
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ read the configuration from `config.json`.
211211
**--linux-realtime-runtime**=REALTIMERUNTIME
212212
Specifies a period of time in microseconds for the longest continuous period in which the tasks in a cgroup have access to CPU resources.
213213

214+
**--linux-resources-device-add**=allow|deny[,type=TYPE][,major=MAJOR][,minor=MINOR][,access=ACCESS]
215+
Add a device control rule.
216+
allow|deny: whether the entry is allowed or denied.
217+
TYPE: the device type. The value could be one of 'a' (all), 'b' (block), 'c' (character).
218+
MAJOR/MINOR: the major/minor id of device.
219+
ACCESS: cgroup permissions for device. A composition of r (read), w (write), and m (mknod).
220+
221+
**--linux-resources-device-remove**=allow|deny[,type=TYPE][,major=MAJOR][,minor=MINOR][,access=ACCESS]
222+
Remove a device control rule.
223+
The arguments is same as *--linux-resources-device-add*.
224+
214225
**--linux-rootfs-propagation**=PROPOGATIONMODE
215226
Mount propagation for root filesystem.
216227
Values are "shared, rshared, private, rprivate, slave, rslave"

0 commit comments

Comments
 (0)