machine: make more use of strongunits

To make it very clear in the code what unit the uint represents.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2024-03-06 15:59:55 +01:00
parent 35a8b03267
commit 4d2fc293c0
11 changed files with 39 additions and 33 deletions

View File

@ -117,15 +117,16 @@ func setMachine(cmd *cobra.Command, args []string) error {
setOpts.CPUs = &mc.Resources.CPUs setOpts.CPUs = &mc.Resources.CPUs
} }
if cmd.Flags().Changed("memory") { if cmd.Flags().Changed("memory") {
mc.Resources.Memory = setFlags.Memory mc.Resources.Memory = strongunits.MiB(setFlags.Memory)
setOpts.Memory = &mc.Resources.Memory setOpts.Memory = &mc.Resources.Memory
} }
if cmd.Flags().Changed("disk-size") { if cmd.Flags().Changed("disk-size") {
if setFlags.DiskSize <= mc.Resources.DiskSize { newDiskSizeGB := strongunits.GiB(setFlags.DiskSize)
if newDiskSizeGB <= mc.Resources.DiskSize {
return fmt.Errorf("new disk size must be larger than %d GB", mc.Resources.DiskSize) return fmt.Errorf("new disk size must be larger than %d GB", mc.Resources.DiskSize)
} }
mc.Resources.DiskSize = setFlags.DiskSize mc.Resources.DiskSize = newDiskSizeGB
newDiskSizeGB := strongunits.GiB(setFlags.DiskSize)
setOpts.DiskSize = &newDiskSizeGB setOpts.DiskSize = &newDiskSizeGB
} }
if cmd.Flags().Changed("user-mode-networking") { if cmd.Flags().Changed("user-mode-networking") {

View File

@ -11,7 +11,6 @@ import (
"time" "time"
"github.com/containers/common/pkg/config" "github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/strongunits"
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types" gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/applehv/vfkit" "github.com/containers/podman/v5/pkg/machine/applehv/vfkit"
@ -55,7 +54,7 @@ func (a AppleHVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.Machine
mc.AppleHypervisor = new(vmconfigs.AppleHVConfig) mc.AppleHypervisor = new(vmconfigs.AppleHVConfig)
mc.AppleHypervisor.Vfkit = vfkit.VfkitHelper{} mc.AppleHypervisor.Vfkit = vfkit.VfkitHelper{}
bl := vfConfig.NewEFIBootloader(fmt.Sprintf("%s/efi-bl-%s", opts.Dirs.DataDir.GetPath(), opts.Name), true) bl := vfConfig.NewEFIBootloader(fmt.Sprintf("%s/efi-bl-%s", opts.Dirs.DataDir.GetPath(), opts.Name), true)
mc.AppleHypervisor.Vfkit.VirtualMachine = vfConfig.NewVirtualMachine(uint(mc.Resources.CPUs), mc.Resources.Memory, bl) mc.AppleHypervisor.Vfkit.VirtualMachine = vfConfig.NewVirtualMachine(uint(mc.Resources.CPUs), uint64(mc.Resources.Memory), bl)
randPort, err := utils.GetRandomPort() randPort, err := utils.GetRandomPort()
if err != nil { if err != nil {
@ -71,7 +70,7 @@ func (a AppleHVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.Machine
// Populate the ignition file with virtiofs stuff // Populate the ignition file with virtiofs stuff
ignBuilder.WithUnit(generateSystemDFilesForVirtiofsMounts(virtiofsMounts)...) ignBuilder.WithUnit(generateSystemDFilesForVirtiofsMounts(virtiofsMounts)...)
return resizeDisk(mc, strongunits.GiB(mc.Resources.DiskSize)) return resizeDisk(mc, mc.Resources.DiskSize)
} }
func (a AppleHVStubber) Exists(name string) (bool, error) { func (a AppleHVStubber) Exists(name string) (bool, error) {
@ -164,7 +163,7 @@ func (a AppleHVStubber) StartVM(mc *vmconfigs.MachineConfig) (func() error, func
// create a one-time virtual machine for starting because we dont want all this information in the // create a one-time virtual machine for starting because we dont want all this information in the
// machineconfig if possible. the preference was to derive this stuff // machineconfig if possible. the preference was to derive this stuff
vm := vfConfig.NewVirtualMachine(uint(mc.Resources.CPUs), mc.Resources.Memory, mc.AppleHypervisor.Vfkit.VirtualMachine.Bootloader) vm := vfConfig.NewVirtualMachine(uint(mc.Resources.CPUs), uint64(mc.Resources.Memory), mc.AppleHypervisor.Vfkit.VirtualMachine.Bootloader)
defaultDevices, readySocket, err := getDefaultDevices(mc) defaultDevices, readySocket, err := getDefaultDevices(mc)
if err != nil { if err != nil {

View File

@ -5,7 +5,7 @@ import "github.com/containers/common/pkg/strongunits"
type SetOptions struct { type SetOptions struct {
CPUs *uint64 CPUs *uint64
DiskSize *strongunits.GiB DiskSize *strongunits.GiB
Memory *uint64 Memory *strongunits.MiB
Rootful *bool Rootful *bool
UserModeNetworking *bool UserModeNetworking *bool
USBs *[]string USBs *[]string

View File

@ -94,7 +94,7 @@ var _ = Describe("podman machine init", func() {
Expect(testMachine.Name).To(Equal(mb.names[0])) Expect(testMachine.Name).To(Equal(mb.names[0]))
if testProvider.VMType() != define.WSLVirt { // WSL hardware specs are hardcoded if testProvider.VMType() != define.WSLVirt { // WSL hardware specs are hardcoded
Expect(testMachine.Resources.CPUs).To(Equal(uint64(cpus))) Expect(testMachine.Resources.CPUs).To(Equal(uint64(cpus)))
Expect(testMachine.Resources.Memory).To(Equal(uint64(2048))) Expect(testMachine.Resources.Memory).To(BeEquivalentTo(uint64(2048)))
} }
}) })
@ -150,7 +150,7 @@ var _ = Describe("podman machine init", func() {
Expect(testMachine.Name).To(Equal(mb.names[0])) Expect(testMachine.Name).To(Equal(mb.names[0]))
if testProvider.VMType() != define.WSLVirt { // memory and cpus something we cannot set with WSL if testProvider.VMType() != define.WSLVirt { // memory and cpus something we cannot set with WSL
Expect(testMachine.Resources.CPUs).To(Equal(uint64(cpus))) Expect(testMachine.Resources.CPUs).To(Equal(uint64(cpus)))
Expect(testMachine.Resources.Memory).To(Equal(uint64(2048))) Expect(testMachine.Resources.Memory).To(BeEquivalentTo(uint64(2048)))
} }
Expect(testMachine.SSHConfig.RemoteUsername).To(Equal(remoteUsername)) Expect(testMachine.SSHConfig.RemoteUsername).To(Equal(remoteUsername))

View File

@ -53,8 +53,8 @@ func (h HyperVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineC
hwConfig := hypervctl.HardwareConfig{ hwConfig := hypervctl.HardwareConfig{
CPUs: uint16(mc.Resources.CPUs), CPUs: uint16(mc.Resources.CPUs),
DiskPath: mc.ImagePath.GetPath(), DiskPath: mc.ImagePath.GetPath(),
DiskSize: mc.Resources.DiskSize, DiskSize: uint64(mc.Resources.DiskSize),
Memory: mc.Resources.Memory, Memory: uint64(mc.Resources.Memory),
} }
networkHVSock, err := vsock.NewHVSockRegistryEntry(mc.Name, vsock.Network) networkHVSock, err := vsock.NewHVSockRegistryEntry(mc.Name, vsock.Network)
@ -120,7 +120,7 @@ func (h HyperVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineC
} }
callbackFuncs.Add(vmRemoveCallback) callbackFuncs.Add(vmRemoveCallback)
err = resizeDisk(strongunits.GiB(mc.Resources.DiskSize), mc.ImagePath) err = resizeDisk(mc.Resources.DiskSize, mc.ImagePath)
return err return err
} }
@ -337,9 +337,10 @@ func (h HyperVStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define
}, func(ms *hypervctl.MemorySettings) { }, func(ms *hypervctl.MemorySettings) {
if memoryChanged { if memoryChanged {
ms.DynamicMemoryEnabled = false ms.DynamicMemoryEnabled = false
ms.VirtualQuantity = *opts.Memory mem := uint64(*opts.Memory)
ms.Limit = *opts.Memory ms.VirtualQuantity = mem
ms.Reservation = *opts.Memory ms.Limit = mem
ms.Reservation = mem
} }
}) })
if err != nil { if err != nil {

View File

@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/containers/common/pkg/strongunits"
"github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/define"
) )
@ -32,8 +33,9 @@ func NewQemuBuilder(binary string, options []string) QemuCmd {
} }
// SetMemory adds the specified amount of memory for the machine // SetMemory adds the specified amount of memory for the machine
func (q *QemuCmd) SetMemory(m uint64) { func (q *QemuCmd) SetMemory(m strongunits.MiB) {
*q = append(*q, "-m", strconv.FormatUint(m, 10)) // qemu accepts the memory in MiB
*q = append(*q, "-m", strconv.FormatUint(uint64(m), 10))
} }
// SetCPUs adds the number of CPUs the machine will have // SetCPUs adds the number of CPUs the machine will have

View File

@ -116,7 +116,7 @@ func (q *QEMUStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineCo
mc.QEMUHypervisor = &qemuConfig mc.QEMUHypervisor = &qemuConfig
mc.QEMUHypervisor.QEMUPidPath = qemuPidPath mc.QEMUHypervisor.QEMUPidPath = qemuPidPath
return q.resizeDisk(strongunits.GiB(mc.Resources.DiskSize), mc.ImagePath) return q.resizeDisk(mc.Resources.DiskSize, mc.ImagePath)
} }
func runStartVMCommand(cmd *exec.Cmd) error { func runStartVMCommand(cmd *exec.Cmd) error {

View File

@ -10,7 +10,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/containers/common/pkg/strongunits"
"github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/connection" "github.com/containers/podman/v5/pkg/machine/connection"
machineDefine "github.com/containers/podman/v5/pkg/machine/define" machineDefine "github.com/containers/podman/v5/pkg/machine/define"
@ -53,8 +52,8 @@ func List(vmstubbers []vmconfigs.VMProvider, _ machine.ListOptions) ([]*machine.
//Stream: "", // No longer applicable //Stream: "", // No longer applicable
VMType: s.VMType().String(), VMType: s.VMType().String(),
CPUs: mc.Resources.CPUs, CPUs: mc.Resources.CPUs,
Memory: strongunits.MiB(mc.Resources.Memory), Memory: mc.Resources.Memory,
DiskSize: strongunits.GiB(mc.Resources.DiskSize), DiskSize: mc.Resources.DiskSize,
Port: mc.SSH.Port, Port: mc.SSH.Port,
RemoteUsername: mc.SSH.RemoteUsername, RemoteUsername: mc.SSH.RemoteUsername,
IdentityPath: mc.SSH.IdentityPath, IdentityPath: mc.SSH.IdentityPath,

View File

@ -3,6 +3,7 @@ package vmconfigs
import ( import (
"time" "time"
"github.com/containers/common/pkg/strongunits"
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types" gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/ignition" "github.com/containers/podman/v5/pkg/machine/ignition"
@ -124,9 +125,9 @@ type ResourceConfig struct {
// CPUs to be assigned to the VM // CPUs to be assigned to the VM
CPUs uint64 CPUs uint64
// Disk size in gigabytes assigned to the vm // Disk size in gigabytes assigned to the vm
DiskSize uint64 DiskSize strongunits.GiB
// Memory in megabytes assigned to the vm // Memory in megabytes assigned to the vm
Memory uint64 Memory strongunits.MiB
// Usbs // Usbs
USBs []define.USBConfig USBs []define.USBConfig
} }

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/containers/common/pkg/strongunits"
define2 "github.com/containers/podman/v5/libpod/define" define2 "github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/errorhandling" "github.com/containers/podman/v5/pkg/errorhandling"
"github.com/containers/podman/v5/pkg/machine/connection" "github.com/containers/podman/v5/pkg/machine/connection"
@ -72,8 +73,8 @@ func NewMachineConfig(opts define.InitOptions, dirs *define.MachineDirs, sshIden
// System Resources // System Resources
mrc := ResourceConfig{ mrc := ResourceConfig{
CPUs: opts.CPUS, CPUs: opts.CPUS,
DiskSize: opts.DiskSize, DiskSize: strongunits.GiB(opts.DiskSize),
Memory: opts.Memory, Memory: strongunits.MiB(opts.Memory),
USBs: usbs, USBs: usbs,
} }
mc.Resources = mrc mc.Resources = mrc

View File

@ -15,6 +15,7 @@ import (
"time" "time"
"github.com/containers/common/pkg/config" "github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/strongunits"
"github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/env"
@ -702,7 +703,7 @@ func isRunning(name string) (bool, error) {
} }
//nolint:unused //nolint:unused
func getDiskSize(name string) uint64 { func getDiskSize(name string) strongunits.GiB {
vmDataDir, err := env.GetDataDir(vmtype) vmDataDir, err := env.GetDataDir(vmtype)
if err != nil { if err != nil {
return 0 return 0
@ -713,7 +714,7 @@ func getDiskSize(name string) uint64 {
if err != nil { if err != nil {
return 0 return 0
} }
return uint64(info.Size()) return strongunits.ToGiB(strongunits.B(info.Size()))
} }
//nolint:unused //nolint:unused
@ -742,7 +743,7 @@ func getCPUs(name string) (uint64, error) {
} }
//nolint:unused //nolint:unused
func getMem(name string) (uint64, error) { func getMem(name string) (strongunits.MiB, error) {
dist := machine.ToDist(name) dist := machine.ToDist(name)
if run, _ := isWSLRunning(dist); !run { if run, _ := isWSLRunning(dist); !run {
return 0, nil return 0, nil
@ -761,13 +762,14 @@ func getMem(name string) (uint64, error) {
t, a int t, a int
) )
for scanner.Scan() { for scanner.Scan() {
// fields are in kB so div to mb
fields := strings.Fields(scanner.Text()) fields := strings.Fields(scanner.Text())
if strings.HasPrefix(fields[0], "MemTotal") && len(fields) >= 2 { if strings.HasPrefix(fields[0], "MemTotal") && len(fields) >= 2 {
t, err = strconv.Atoi(fields[1]) t, err = strconv.Atoi(fields[1])
total = uint64(t) * 1024 total = uint64(t) / 1024
} else if strings.HasPrefix(fields[0], "MemAvailable") && len(fields) >= 2 { } else if strings.HasPrefix(fields[0], "MemAvailable") && len(fields) >= 2 {
a, err = strconv.Atoi(fields[1]) a, err = strconv.Atoi(fields[1])
available = uint64(a) * 1024 available = uint64(a) / 1024
} }
if err != nil { if err != nil {
break break
@ -775,7 +777,7 @@ func getMem(name string) (uint64, error) {
} }
_ = cmd.Wait() _ = cmd.Wait()
return total - available, err return strongunits.MiB(total - available), err
} }
//nolint:unused //nolint:unused