@@ -7,13 +7,15 @@ import (
7
7
"os"
8
8
9
9
"github.com/containers/common/pkg/completion"
10
+ "github.com/containers/common/pkg/strongunits"
10
11
"github.com/containers/podman/v5/cmd/podman/registry"
11
12
ldefine "github.com/containers/podman/v5/libpod/define"
12
13
"github.com/containers/podman/v5/libpod/events"
13
14
"github.com/containers/podman/v5/pkg/machine"
14
15
"github.com/containers/podman/v5/pkg/machine/define"
15
16
"github.com/containers/podman/v5/pkg/machine/shim"
16
17
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
18
+ "github.com/shirou/gopsutil/v3/mem"
17
19
"github.com/sirupsen/logrus"
18
20
"github.com/spf13/cobra"
19
21
)
@@ -196,6 +198,12 @@ func initMachine(cmd *cobra.Command, args []string) error {
196
198
initOpts .UserModeNetworking = & initOptionalFlags .UserModeNetworking
197
199
}
198
200
201
+ if cmd .Flags ().Changed ("memory" ) {
202
+ if err := checkMaxMemory (strongunits .MiB (initOpts .Memory )); err != nil {
203
+ return err
204
+ }
205
+ }
206
+
199
207
// TODO need to work this back in
200
208
// if finished, err := vm.Init(initOpts); err != nil || !finished {
201
209
// // Finished = true, err = nil - Success! Log a message with further instructions
@@ -226,3 +234,16 @@ func initMachine(cmd *cobra.Command, args []string) error {
226
234
fmt .Printf ("To start your machine run:\n \n \t podman machine start%s\n \n " , extra )
227
235
return err
228
236
}
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
+ }
0 commit comments