Skip to content

Commit c41c30b

Browse files
Merge pull request #21180 from rhatdan/nvidia
Make --gpus work with nvidia gpus
2 parents 7ed4478 + 46cfc98 commit c41c30b

10 files changed

Lines changed: 27 additions & 12 deletions

File tree

cmd/podman/common/create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,10 @@ func DefineCreateFlags(cmd *cobra.Command, cf *entities.ContainerCreateOptions,
700700
)
701701
_ = cmd.RegisterFlagCompletionFunc(gidmapFlagName, completion.AutocompleteNone)
702702

703+
gpuFlagName := "gpus"
704+
createFlags.StringSliceVar(&cf.GPUs, gpuFlagName, []string{}, "GPU devices to add to the container ('all' to pass all GPUs)")
705+
_ = cmd.RegisterFlagCompletionFunc(gpuFlagName, completion.AutocompleteNone)
706+
703707
uidmapFlagName := "uidmap"
704708
createFlags.StringSliceVar(
705709
&cf.UIDMap,

cmd/podman/containers/run.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ func runFlags(cmd *cobra.Command) {
8080
flags.StringVar(&runOpts.DetachKeys, detachKeysFlagName, containerConfig.DetachKeys(), "Override the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-cf`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`")
8181
_ = cmd.RegisterFlagCompletionFunc(detachKeysFlagName, common.AutocompleteDetachKeys)
8282

83-
gpuFlagName := "gpus"
84-
flags.String(gpuFlagName, "", "This is a Docker specific option and is a NOOP")
85-
_ = cmd.RegisterFlagCompletionFunc(gpuFlagName, completion.AutocompleteNone)
86-
_ = flags.MarkHidden("gpus")
87-
8883
passwdFlagName := "passwd"
8984
flags.BoolVar(&runOpts.Passwd, passwdFlagName, true, "add entries to /etc/passwd and /etc/group")
9085

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
####> This option file is used in:
2+
####> podman create, pod clone, pod create, run
3+
####> If file is edited, make sure the changes
4+
####> are applicable to all of those.
5+
#### **--gpus**=*ENTRY*
6+
7+
GPU devices to add to the container ('all' to pass all GPUs) Currently only
8+
Nvidia devices are supported.

docs/source/markdown/podman-create.1.md.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ See [**Environment**](#environment) note below for precedence and examples.
159159

160160
@@option gidmap.container
161161

162+
@@option gpus
163+
162164
@@option group-add
163165

164166
@@option group-entry

docs/source/markdown/podman-pod-clone.1.md.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Note: the pod implements devices by storing the initial configuration passed by
4141

4242
@@option gidmap.pod
4343

44+
@@option gpus
45+
4446
#### **--help**, **-h**
4547

4648
Print usage statement.

docs/source/markdown/podman-pod-create.1.md.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Set the exit policy of the pod when the last container exits. Supported policie
8080

8181
@@option gidmap.pod
8282

83+
@@option gpus
84+
8385
#### **--help**, **-h**
8486

8587
Print usage statement.

docs/source/markdown/podman-run.1.md.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ See [**Environment**](#environment) note below for precedence and examples.
193193

194194
@@option gidmap.container
195195

196+
@@option gpus
197+
196198
@@option group-add
197199

198200
@@option group-entry

pkg/domain/entities/pods.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ type ContainerCreateOptions struct {
167167
EnvFile []string
168168
Expose []string
169169
GIDMap []string
170+
GPUs []string
170171
GroupAdd []string
171172
HealthCmd string
172173
HealthInterval string

pkg/specgenutil/specgen.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,12 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
784784
s.ImageVolumes = imageVolumes
785785
}
786786

787-
for _, dev := range c.Devices {
787+
devices := c.Devices
788+
for _, gpu := range c.GPUs {
789+
devices = append(devices, "nvidia.com/gpu="+gpu)
790+
}
791+
792+
for _, dev := range devices {
788793
s.Devices = append(s.Devices, specs.LinuxDevice{Path: dev})
789794
}
790795

test/e2e/run_device_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,6 @@ var _ = Describe("Podman run device", func() {
119119
Expect(session).Should(ExitCleanly())
120120
})
121121

122-
It("podman run --gpus noop", func() {
123-
session := podmanTest.Podman([]string{"run", "--gpus", "all", ALPINE, "true"})
124-
session.WaitWithDefaultTimeout()
125-
Expect(session).Should(ExitCleanly())
126-
})
127-
128122
It("podman run cannot access non default devices", func() {
129123
session := podmanTest.Podman([]string{"run", "-v /dev:/dev-host", ALPINE, "head", "-1", "/dev-host/kmsg"})
130124
session.WaitWithDefaultTimeout()

0 commit comments

Comments
 (0)