Skip to content

Commit 66ed818

Browse files
author
zhouhao
committed
perfect device-add option
Signed-off-by: zhouhao <[email protected]>
1 parent a652af1 commit 66ed818

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var generateFlags = []cli.Flag{
2626
cli.StringSliceFlag{Name: "cap-drop", Usage: "drop Linux capabilities"},
2727
cli.StringFlag{Name: "cgroups-path", Usage: "specify the path to the cgroups"},
2828
cli.StringFlag{Name: "cwd", Value: "/", Usage: "current working directory for the process"},
29-
cli.StringSliceFlag{Name: "device", Usage: "specifies a device which must be made available in the container"},
29+
cli.StringSliceFlag{Name: "device-add", Usage: "add a device which must be made available in the container"},
3030
cli.BoolFlag{Name: "disable-oom-kill", Usage: "disable OOM Killer"},
3131
cli.StringSliceFlag{Name: "env", Usage: "add environment variable e.g. key=value"},
3232
cli.StringSliceFlag{Name: "env-file", Usage: "read in a file of environment variables"},
@@ -502,15 +502,17 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
502502
g.ClearProcessRlimits()
503503
}
504504

505-
if context.IsSet("device") {
506-
devices := context.StringSlice("device")
505+
if context.IsSet("device-add") {
506+
devices := context.StringSlice("device-add")
507507
for _, deviceArg := range devices {
508-
err := addDevice(deviceArg, g)
508+
dev, err := parseDevice(deviceArg, g)
509509
if err != nil {
510510
return err
511511
}
512+
g.AddDevice(dev)
512513
}
513514
}
515+
514516
err := addSeccomp(context, g)
515517
return err
516518
}
@@ -642,33 +644,33 @@ var deviceType = map[string]bool{
642644
"p": true, // a FIFO
643645
}
644646

645-
// addDevice takes the raw string passed with the --device flag, parses it, and add it
646-
func addDevice(device string, g *generate.Generator) error {
647+
// parseDevice takes the raw string passed with the --device flag
648+
func parseDevice(device string, g *generate.Generator) (rspec.Device, error) {
647649
dev := rspec.Device{}
648650

649651
// The required part and optional part are seperated by ":"
650652
argsParts := strings.Split(device, ":")
651653
if len(argsParts) < 4 {
652-
return fmt.Errorf("Incomplete device arguments: %s", device)
654+
return dev, fmt.Errorf("Incomplete device arguments: %s", device)
653655
}
654656
requiredPart := argsParts[0:4]
655657
optionalPart := argsParts[4:]
656658

657659
// The required part must contain type, major, minor, and path
658660
dev.Type = requiredPart[0]
659661
if !deviceType[dev.Type] {
660-
return fmt.Errorf("Invalid device type: %s", dev.Type)
662+
return dev, fmt.Errorf("Invalid device type: %s", dev.Type)
661663
}
662664

663665
i, err := strconv.ParseInt(requiredPart[1], 10, 64)
664666
if err != nil {
665-
return err
667+
return dev, err
666668
}
667669
dev.Major = i
668670

669671
i, err = strconv.ParseInt(requiredPart[2], 10, 64)
670672
if err != nil {
671-
return err
673+
return dev, err
672674
}
673675
dev.Minor = i
674676
dev.Path = requiredPart[3]
@@ -678,7 +680,7 @@ func addDevice(device string, g *generate.Generator) error {
678680
parts := strings.SplitN(s, "=", 2)
679681

680682
if len(parts) != 2 {
681-
return fmt.Errorf("Incomplete device arguments: %s", s)
683+
return dev, fmt.Errorf("Incomplete device arguments: %s", s)
682684
}
683685

684686
name, value := parts[0], parts[1]
@@ -687,33 +689,31 @@ func addDevice(device string, g *generate.Generator) error {
687689
case "fileMode":
688690
i, err := strconv.ParseInt(value, 10, 32)
689691
if err != nil {
690-
return err
692+
return dev, err
691693
}
692694
mode := os.FileMode(i)
693695
dev.FileMode = &mode
694696
case "uid":
695697
i, err := strconv.ParseInt(value, 10, 32)
696698
if err != nil {
697-
return err
699+
return dev, err
698700
}
699701
uid := uint32(i)
700702
dev.UID = &uid
701703

702704
case "gid":
703705
i, err := strconv.ParseInt(value, 10, 32)
704706
if err != nil {
705-
return err
707+
return dev, err
706708
}
707709
gid := uint32(i)
708710
dev.GID = &gid
709711
default:
710-
return fmt.Errorf("'%s' is not supported by device section", name)
712+
return dev, fmt.Errorf("'%s' is not supported by device section", name)
711713
}
712714
}
713715

714-
g.AddDevice(dev.Path, dev.Type, dev.Major, dev.Minor, dev.FileMode, dev.UID, dev.GID)
715-
716-
return nil
716+
return dev, nil
717717
}
718718

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

completions/bash/oci-runtime-tool

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ _oci-runtime-tool_generate() {
305305
--cap-drop
306306
--cgroups-path
307307
--cwd
308-
--device
308+
--device-add
309309
--env
310310
--env-file
311311
--gid

generate/generate.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -915,18 +915,19 @@ func (g *Generator) RemoveLinuxNamespace(ns string) error {
915915
}
916916

917917
// AddDevice - add a device into g.spec.Linux.Devices
918-
func (g *Generator) AddDevice(path, devType string, major, minor int64, fileMode *os.FileMode, uid, gid *uint32) {
918+
func (g *Generator) AddDevice(device rspec.Device) {
919919
g.initSpecLinux()
920920

921-
device := rspec.Device{
922-
Path: path,
923-
Type: devType,
924-
Major: major,
925-
Minor: minor,
926-
FileMode: fileMode,
927-
UID: uid,
928-
GID: gid,
921+
for i, dev := range g.spec.Linux.Devices {
922+
if dev.Path == device.Path {
923+
g.spec.Linux.Devices[i] = device
924+
return
925+
}
926+
if dev.Type == device.Type && dev.Major == device.Major && dev.Minor == device.Minor {
927+
fmt.Println("WARNING: The same type, major and minor should not be used for multiple devices.")
928+
}
929929
}
930+
930931
g.spec.Linux.Devices = append(g.spec.Linux.Devices, device)
931932
}
932933

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ read the configuration from `config.json`.
5151
**--cwd**=PATH
5252
Current working directory for the process. The deafult is */*.
5353

54-
**--device**=*TYPE:MAJOR:MINOR:PATH[:OPTIONS...]*
54+
**--device-add**=*TYPE:MAJOR:MINOR:PATH[:OPTIONS...]*
5555
Add a device file in container. e.g. --device=c:10:229:/dev/fuse:fileMode=438:uid=0:gid=0
5656
The *TYPE*, *MAJOR*, *MINOR*, *PATH* are required.
5757
*TYPE* is the device type. The acceptable values are b (block), c (character), u (unbuffered), p (FIFO).
@@ -60,6 +60,7 @@ read the configuration from `config.json`.
6060
The *fileMode*, *uid*, *gid* are optional.
6161
*fileMode* is the file mode of the device file.
6262
*uid*/*gid* is the user/group id of the device file.
63+
This option can be specified multiple times.
6364

6465
**--disable-oom-kill**=true|false
6566
Whether to disable OOM Killer for the container or not.

0 commit comments

Comments
 (0)