Skip to content

Commit 6e253af

Browse files
author
zhouhao
committed
add device-remove-all option
Signed-off-by: zhouhao <[email protected]>
1 parent 261a736 commit 6e253af

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var generateFlags = []cli.Flag{
2828
cli.StringFlag{Name: "cwd", Value: "/", Usage: "current working directory for the process"},
2929
cli.StringSliceFlag{Name: "device-add", Usage: "add a device which must be made available in the container"},
3030
cli.StringSliceFlag{Name: "device-remove", Usage: "remove a device which must be made available in the container"},
31+
cli.BoolFlag{Name: "device-remove-all", Usage: "remove all devices which must be made available in the container"},
3132
cli.BoolFlag{Name: "disable-oom-kill", Usage: "disable OOM Killer"},
3233
cli.StringSliceFlag{Name: "env", Usage: "add environment variable e.g. key=value"},
3334
cli.StringSliceFlag{Name: "env-file", Usage: "read in a file of environment variables"},
@@ -524,6 +525,10 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
524525
}
525526
}
526527

528+
if context.Bool("device-remove-all") {
529+
g.ClearLinuxDevices()
530+
}
531+
527532
err := addSeccomp(context, g)
528533
return err
529534
}
@@ -655,8 +660,8 @@ var deviceType = map[string]bool{
655660
"p": true, // a FIFO
656661
}
657662

658-
// parseDevice takes the raw string passed with the --device flag
659-
func parseDevice(device string, g *generate.Generator) (rspec.Device, error) {
663+
// parseDevice takes the raw string passed with the --device-add flag
664+
func parseDevice(device string, g *generate.Generator) (rspec.LinuxDevice, error) {
660665
dev := rspec.LinuxDevice{}
661666

662667
// The required part and optional part are separated by ":"

completions/bash/oci-runtime-tool

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ _oci-runtime-tool_generate() {
368368
"
369369

370370
local boolean_options="
371+
--device-remove-all
371372
--disable-oom-kill
372373
--help -h
373374
--linux-namespace-remove-all

generate/generate.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,24 +1015,24 @@ func (g *Generator) RemoveLinuxNamespace(ns string) error {
10151015
}
10161016

10171017
// AddDevice - add a device into g.spec.Linux.Devices
1018-
func (g *Generator) AddDevice(device rspec.Device) {
1018+
func (g *Generator) AddDevice(device rspec.LinuxDevice) {
10191019
g.initSpecLinux()
10201020

1021-
for i, dev := range g.spec.Linux.LinuxDevices {
1021+
for i, dev := range g.spec.Linux.Devices {
10221022
if dev.Path == device.Path {
1023-
g.spec.Linux.LinuxDevices[i] = device
1023+
g.spec.Linux.Devices[i] = device
10241024
return
10251025
}
10261026
if dev.Type == device.Type && dev.Major == device.Major && dev.Minor == device.Minor {
1027-
fmt.Println("WARNING: The same type, major and minor should not be used for multiple devices.")
1027+
fmt.Fprintln(os.Stderr, "WARNING: The same type, major and minor should not be used for multiple devices.")
10281028
}
10291029
}
10301030

10311031
g.spec.Linux.Devices = append(g.spec.Linux.Devices, device)
10321032
}
10331033

10341034
//RemoveDevice remove a device from g.spec.Linux.Devices
1035-
func(g *Generator) RemoveDevice(path string) error {
1035+
func (g *Generator) RemoveDevice(path string) error {
10361036
if g.spec == nil || g.spec.Linux == nil || g.spec.Linux.Devices == nil {
10371037
return nil
10381038
}
@@ -1046,6 +1046,13 @@ func(g *Generator) RemoveDevice(path string) error {
10461046
return nil
10471047
}
10481048

1049+
func (g *Generator) ClearLinuxDevices() {
1050+
if g.spec == nil || g.spec.Linux == nil || g.spec.Linux.Devices == nil {
1051+
return
1052+
}
1053+
1054+
g.spec.Linux.Devices = []rspec.LinuxDevice{}
1055+
}
10491056

10501057
// strPtr returns the pointer pointing to the string s.
10511058
func strPtr(s string) *string { return &s }

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ read the configuration from `config.json`.
6666
Remove a device file in container.
6767
This option can be specified multiple times.
6868

69+
**--device-remove-all**=true|false
70+
Remove all devices for linux inside the container. The default is *false*.
71+
6972
**--disable-oom-kill**=true|false
7073
Whether to disable OOM Killer for the container or not.
7174

0 commit comments

Comments
 (0)