Skip to content

Commit 8c3af49

Browse files
Merge pull request #28347 from Honny1/machine-cpu-limits
Reject `--cpus` above host CPU count on podman machine init and set
2 parents 7952067 + c722f2f commit 8c3af49

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

cmd/podman/machine/init.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"os"
9+
"runtime"
910
"slices"
1011

1112
"github.com/containers/podman/v6/cmd/podman/registry"
@@ -252,10 +253,11 @@ func initMachine(cmd *cobra.Command, args []string) error {
252253
initOpts.UserModeNetworking = &initOptionalFlags.UserModeNetworking
253254
}
254255

255-
if cmd.Flags().Changed("memory") {
256-
if err := checkMaxMemory(strongunits.MiB(initOpts.Memory)); err != nil {
257-
return err
258-
}
256+
if err := checkMaxMemory(strongunits.MiB(initOpts.Memory)); err != nil {
257+
return err
258+
}
259+
if err := checkMaxCPUs(initOpts.CPUS); err != nil {
260+
return err
259261
}
260262

261263
// initOpts.SkipTlsVerify defaults to OptionalBoolUndefined, which means the backend library
@@ -320,3 +322,12 @@ func checkMaxMemory(newMem strongunits.MiB) error {
320322
}
321323
return nil
322324
}
325+
326+
// checkMaxCPUs compares requested CPUs to the host (runtime.NumCPU).
327+
func checkMaxCPUs(requestedCPUs uint64) error {
328+
hostCPUs := uint64(runtime.NumCPU())
329+
if requestedCPUs > hostCPUs {
330+
return fmt.Errorf("requested number of CPUs (%d) greater than number of host CPUs (%d)", requestedCPUs, hostCPUs)
331+
}
332+
return nil
333+
}

cmd/podman/machine/set.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func setMachine(cmd *cobra.Command, args []string) error {
9898
setOpts.Rootful = &setFlags.Rootful
9999
}
100100
if cmd.Flags().Changed("cpus") {
101+
if err := checkMaxCPUs(setFlags.CPUs); err != nil {
102+
return err
103+
}
101104
setOpts.CPUs = &setFlags.CPUs
102105
}
103106
if cmd.Flags().Changed("memory") {

pkg/machine/e2e/init_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ var _ = Describe("podman machine init", func() {
8181
Expect(err).ToNot(HaveOccurred())
8282
Expect(badMemSession.errorToString()).To(ContainSubstring(fmt.Sprintf("greater than total system memory (%d MB)", systemMem)))
8383
Expect(badMemSession).To(Exit(125))
84+
85+
badCPU := initMachine{}
86+
badCPUSession, err := mb.setCmd(badCPU.withCPUs(9999999)).run()
87+
Expect(err).ToNot(HaveOccurred())
88+
Expect(badCPUSession).To(Exit(125))
89+
Expect(badCPUSession.errorToString()).To(ContainSubstring("greater than number of host CPUs"))
8490
})
8591

8692
It("init volume check", func() {

pkg/machine/e2e/set_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ import (
1313
)
1414

1515
var _ = Describe("podman machine set", func() {
16+
It("machine set rejects excessive cpus", func() {
17+
skipIfWSL("WSL cannot change cpus via set")
18+
name := randomString()
19+
i := new(initMachine)
20+
session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath)).run()
21+
Expect(err).ToNot(HaveOccurred())
22+
Expect(session).To(Exit(0))
23+
24+
badSet := setMachine{}
25+
badCPUSession, err := mb.setName(name).setCmd(badSet.withCPUs(9999999)).run()
26+
Expect(err).ToNot(HaveOccurred())
27+
Expect(badCPUSession).To(Exit(125))
28+
Expect(badCPUSession.errorToString()).To(ContainSubstring("greater than number of host CPUs"))
29+
})
30+
1631
It("set machine cpus, disk, memory", func() {
1732
skipIfWSL("WSL cannot change set properties of disk, processor, or memory")
1833
name := randomString()

0 commit comments

Comments
 (0)