Skip to content

Commit bb4dda4

Browse files
committed
Reject --cpus above host CPU count on podman machine init and set
Fixes: #28322 Signed-off-by: Jan Rodák <hony.com@seznam.cz>
1 parent 03ee20c commit bb4dda4

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

cmd/podman/machine/init.go

Lines changed: 13 additions & 0 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"
@@ -257,6 +258,9 @@ func initMachine(cmd *cobra.Command, args []string) error {
257258
return err
258259
}
259260
}
261+
if err := checkMaxCPUs(initOpts.CPUS); err != nil {
262+
return err
263+
}
260264

261265
// initOpts.SkipTlsVerify defaults to OptionalBoolUndefined, which means the backend library
262266
// decides whether to verify TLS. We only explicitly set it if the user specifies the
@@ -320,3 +324,12 @@ func checkMaxMemory(newMem strongunits.MiB) error {
320324
}
321325
return nil
322326
}
327+
328+
// checkMaxCPUs compares requested CPUs to the host (runtime.NumCPU).
329+
func checkMaxCPUs(requestedCPUs uint64) error {
330+
hostCPUs := uint64(runtime.NumCPU())
331+
if requestedCPUs > hostCPUs {
332+
return fmt.Errorf("requested number of CPUs (%d) greater than number of host CPUs (%d)", requestedCPUs, hostCPUs)
333+
}
334+
return nil
335+
}

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)