Skip to content

Commit 786ea01

Browse files
committed
Error when machine memory exceeds system memory
Close loophole that would allow you to assign more memory than the system has to a podman machine Fixes: #18206 Signed-off-by: Brent Baude <[email protected]>
1 parent c86386e commit 786ea01

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

cmd/podman/machine/init.go

+21
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"os"
88

99
"github.com/containers/common/pkg/completion"
10+
"github.com/containers/common/pkg/strongunits"
1011
"github.com/containers/podman/v5/cmd/podman/registry"
1112
ldefine "github.com/containers/podman/v5/libpod/define"
1213
"github.com/containers/podman/v5/libpod/events"
1314
"github.com/containers/podman/v5/pkg/machine"
1415
"github.com/containers/podman/v5/pkg/machine/define"
1516
"github.com/containers/podman/v5/pkg/machine/shim"
1617
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
18+
"github.com/shirou/gopsutil/v3/mem"
1719
"github.com/sirupsen/logrus"
1820
"github.com/spf13/cobra"
1921
)
@@ -196,6 +198,12 @@ func initMachine(cmd *cobra.Command, args []string) error {
196198
initOpts.UserModeNetworking = &initOptionalFlags.UserModeNetworking
197199
}
198200

201+
if cmd.Flags().Changed("memory") {
202+
if err := checkMaxMemory(strongunits.MiB(initOpts.Memory)); err != nil {
203+
return err
204+
}
205+
}
206+
199207
// TODO need to work this back in
200208
// if finished, err := vm.Init(initOpts); err != nil || !finished {
201209
// // Finished = true, err = nil - Success! Log a message with further instructions
@@ -226,3 +234,16 @@ func initMachine(cmd *cobra.Command, args []string) error {
226234
fmt.Printf("To start your machine run:\n\n\tpodman machine start%s\n\n", extra)
227235
return err
228236
}
237+
238+
// checkMaxMemory gets the total system memory and compares it to the variable. if the variable
239+
// is larger than the total memory, it returns an error
240+
func checkMaxMemory(newMem strongunits.MiB) error {
241+
memStat, err := mem.VirtualMemory()
242+
if err != nil {
243+
return err
244+
}
245+
if total := strongunits.B(memStat.Total); strongunits.B(memStat.Total) < newMem.ToBytes() {
246+
return fmt.Errorf("requested amount of memory (%d MB) greater than total system memory (%d MB)", newMem, total)
247+
}
248+
return nil
249+
}

cmd/podman/machine/set.go

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func setMachine(cmd *cobra.Command, args []string) error {
111111
}
112112
if cmd.Flags().Changed("memory") {
113113
newMemory := strongunits.MiB(setFlags.Memory)
114+
if err := checkMaxMemory(newMemory); err != nil {
115+
return err
116+
}
114117
setOpts.Memory = &newMemory
115118
}
116119
if cmd.Flags().Changed("disk-size") {

pkg/machine/e2e/init_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/containers/common/pkg/strongunits"
1213
"github.com/containers/podman/v5/pkg/machine/define"
1314
"github.com/containers/podman/v5/utils"
1415
. "github.com/onsi/ginkgo/v2"
1516
. "github.com/onsi/gomega"
1617
. "github.com/onsi/gomega/gexec"
18+
"github.com/shirou/gopsutil/v3/mem"
1719
"github.com/sirupsen/logrus"
1820
)
1921

@@ -35,7 +37,7 @@ var _ = Describe("podman machine init", func() {
3537
cpus = 1
3638
}
3739

38-
It("bad init name", func() {
40+
It("bad init", func() {
3941
i := initMachine{}
4042
reallyLongName := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4143
session, err := mb.setName(reallyLongName).setCmd(&i).run()
@@ -77,6 +79,16 @@ var _ = Describe("podman machine init", func() {
7779
Expect(err).ToNot(HaveOccurred())
7880
Expect(session).To(Exit(125))
7981
Expect(session.errorToString()).To(ContainSubstring(`invalid username "-/a": names must match [a-zA-Z0-9][a-zA-Z0-9_.-]*: invalid argument`))
82+
83+
// this comes in bytes
84+
memStat, err := mem.VirtualMemory()
85+
Expect(err).ToNot(HaveOccurred())
86+
total := strongunits.ToMib(strongunits.B(memStat.Total)) + 1024
87+
88+
badMem := initMachine{}
89+
badMemSession, err := mb.setCmd(badMem.withMemory(uint(total))).run()
90+
Expect(err).ToNot(HaveOccurred())
91+
Expect(badMemSession).To(Exit(125))
8092
})
8193

8294
It("simple init", func() {

pkg/machine/e2e/set_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ var _ = Describe("podman machine set", func() {
3333
Expect(err).ToNot(HaveOccurred())
3434
Expect(session).To(Exit(0))
3535

36+
setMem := setMachine{}
37+
SetMemSession, err := mb.setName(name).setCmd(setMem.withMemory(524288)).run()
38+
Expect(err).ToNot(HaveOccurred())
39+
Expect(SetMemSession).To(Exit(125))
40+
3641
set := setMachine{}
3742
setSession, err := mb.setName(name).setCmd(set.withCPUs(2).withDiskSize(102).withMemory(4096)).run()
3843
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)