Skip to content

Commit

Permalink
machine: make more use of strongunits
Browse files Browse the repository at this point in the history
To make it very clear in the code what unit the uint represents.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Mar 6, 2024
1 parent 35a8b03 commit 4d2fc29
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 33 deletions.
9 changes: 5 additions & 4 deletions cmd/podman/machine/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ func setMachine(cmd *cobra.Command, args []string) error {
setOpts.CPUs = &mc.Resources.CPUs
}
if cmd.Flags().Changed("memory") {
mc.Resources.Memory = setFlags.Memory
mc.Resources.Memory = strongunits.MiB(setFlags.Memory)
setOpts.Memory = &mc.Resources.Memory
}
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)
}
mc.Resources.DiskSize = setFlags.DiskSize
newDiskSizeGB := strongunits.GiB(setFlags.DiskSize)
mc.Resources.DiskSize = newDiskSizeGB

setOpts.DiskSize = &newDiskSizeGB
}
if cmd.Flags().Changed("user-mode-networking") {
Expand Down
7 changes: 3 additions & 4 deletions pkg/machine/applehv/stubber.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/strongunits"
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/applehv/vfkit"
Expand Down Expand Up @@ -55,7 +54,7 @@ func (a AppleHVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.Machine
mc.AppleHypervisor = new(vmconfigs.AppleHVConfig)
mc.AppleHypervisor.Vfkit = vfkit.VfkitHelper{}
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()
if err != nil {
Expand All @@ -71,7 +70,7 @@ func (a AppleHVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.Machine
// Populate the ignition file with virtiofs stuff
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) {
Expand Down Expand Up @@ -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
// 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)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/define/setopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/containers/common/pkg/strongunits"
type SetOptions struct {
CPUs *uint64
DiskSize *strongunits.GiB
Memory *uint64
Memory *strongunits.MiB
Rootful *bool
UserModeNetworking *bool
USBs *[]string
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/e2e/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var _ = Describe("podman machine init", func() {
Expect(testMachine.Name).To(Equal(mb.names[0]))
if testProvider.VMType() != define.WSLVirt { // WSL hardware specs are hardcoded
Expect(testMachine.Resources.CPUs).To(Equal(uint64(cpus)))
Expect(testMachine.Resources.Memory).To(Equal(uint64(2048)))
Expect(testMachine.Resources.Memory).To(BeEquivalentTo(uint64(2048)))
}
})

Expand Down Expand Up @@ -150,7 +150,7 @@ var _ = Describe("podman machine init", func() {
Expect(testMachine.Name).To(Equal(mb.names[0]))
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.Memory).To(Equal(uint64(2048)))
Expect(testMachine.Resources.Memory).To(BeEquivalentTo(uint64(2048)))
}
Expect(testMachine.SSHConfig.RemoteUsername).To(Equal(remoteUsername))

Expand Down
13 changes: 7 additions & 6 deletions pkg/machine/hyperv/stubber.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (h HyperVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineC
hwConfig := hypervctl.HardwareConfig{
CPUs: uint16(mc.Resources.CPUs),
DiskPath: mc.ImagePath.GetPath(),
DiskSize: mc.Resources.DiskSize,
Memory: mc.Resources.Memory,
DiskSize: uint64(mc.Resources.DiskSize),
Memory: uint64(mc.Resources.Memory),
}

networkHVSock, err := vsock.NewHVSockRegistryEntry(mc.Name, vsock.Network)
Expand Down Expand Up @@ -120,7 +120,7 @@ func (h HyperVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineC
}

callbackFuncs.Add(vmRemoveCallback)
err = resizeDisk(strongunits.GiB(mc.Resources.DiskSize), mc.ImagePath)
err = resizeDisk(mc.Resources.DiskSize, mc.ImagePath)
return err
}

Expand Down Expand Up @@ -337,9 +337,10 @@ func (h HyperVStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define
}, func(ms *hypervctl.MemorySettings) {
if memoryChanged {
ms.DynamicMemoryEnabled = false
ms.VirtualQuantity = *opts.Memory
ms.Limit = *opts.Memory
ms.Reservation = *opts.Memory
mem := uint64(*opts.Memory)
ms.VirtualQuantity = mem
ms.Limit = mem
ms.Reservation = mem
}
})
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions pkg/machine/qemu/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"time"

"github.com/containers/common/pkg/strongunits"
"github.com/containers/podman/v5/pkg/machine/define"
)

Expand All @@ -32,8 +33,9 @@ func NewQemuBuilder(binary string, options []string) QemuCmd {
}

// SetMemory adds the specified amount of memory for the machine
func (q *QemuCmd) SetMemory(m uint64) {
*q = append(*q, "-m", strconv.FormatUint(m, 10))
func (q *QemuCmd) SetMemory(m strongunits.MiB) {
// 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
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/qemu/stubber.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (q *QEMUStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineCo

mc.QEMUHypervisor = &qemuConfig
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 {
Expand Down
5 changes: 2 additions & 3 deletions pkg/machine/shim/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/containers/common/pkg/strongunits"
"github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/connection"
machineDefine "github.com/containers/podman/v5/pkg/machine/define"
Expand Down Expand Up @@ -53,8 +52,8 @@ func List(vmstubbers []vmconfigs.VMProvider, _ machine.ListOptions) ([]*machine.
//Stream: "", // No longer applicable
VMType: s.VMType().String(),
CPUs: mc.Resources.CPUs,
Memory: strongunits.MiB(mc.Resources.Memory),
DiskSize: strongunits.GiB(mc.Resources.DiskSize),
Memory: mc.Resources.Memory,
DiskSize: mc.Resources.DiskSize,
Port: mc.SSH.Port,
RemoteUsername: mc.SSH.RemoteUsername,
IdentityPath: mc.SSH.IdentityPath,
Expand Down
5 changes: 3 additions & 2 deletions pkg/machine/vmconfigs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vmconfigs
import (
"time"

"github.com/containers/common/pkg/strongunits"
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/ignition"
Expand Down Expand Up @@ -124,9 +125,9 @@ type ResourceConfig struct {
// CPUs to be assigned to the VM
CPUs uint64
// Disk size in gigabytes assigned to the vm
DiskSize uint64
DiskSize strongunits.GiB
// Memory in megabytes assigned to the vm
Memory uint64
Memory strongunits.MiB
// Usbs
USBs []define.USBConfig
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/machine/vmconfigs/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/containers/common/pkg/strongunits"
define2 "github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/errorhandling"
"github.com/containers/podman/v5/pkg/machine/connection"
Expand Down Expand Up @@ -72,8 +73,8 @@ func NewMachineConfig(opts define.InitOptions, dirs *define.MachineDirs, sshIden
// System Resources
mrc := ResourceConfig{
CPUs: opts.CPUS,
DiskSize: opts.DiskSize,
Memory: opts.Memory,
DiskSize: strongunits.GiB(opts.DiskSize),
Memory: strongunits.MiB(opts.Memory),
USBs: usbs,
}
mc.Resources = mrc
Expand Down
14 changes: 8 additions & 6 deletions pkg/machine/wsl/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"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/define"
"github.com/containers/podman/v5/pkg/machine/env"
Expand Down Expand Up @@ -702,7 +703,7 @@ func isRunning(name string) (bool, error) {
}

//nolint:unused
func getDiskSize(name string) uint64 {
func getDiskSize(name string) strongunits.GiB {
vmDataDir, err := env.GetDataDir(vmtype)
if err != nil {
return 0
Expand All @@ -713,7 +714,7 @@ func getDiskSize(name string) uint64 {
if err != nil {
return 0
}
return uint64(info.Size())
return strongunits.ToGiB(strongunits.B(info.Size()))
}

//nolint:unused
Expand Down Expand Up @@ -742,7 +743,7 @@ func getCPUs(name string) (uint64, error) {
}

//nolint:unused
func getMem(name string) (uint64, error) {
func getMem(name string) (strongunits.MiB, error) {
dist := machine.ToDist(name)
if run, _ := isWSLRunning(dist); !run {
return 0, nil
Expand All @@ -761,21 +762,22 @@ func getMem(name string) (uint64, error) {
t, a int
)
for scanner.Scan() {
// fields are in kB so div to mb
fields := strings.Fields(scanner.Text())
if strings.HasPrefix(fields[0], "MemTotal") && len(fields) >= 2 {
t, err = strconv.Atoi(fields[1])
total = uint64(t) * 1024
total = uint64(t) / 1024
} else if strings.HasPrefix(fields[0], "MemAvailable") && len(fields) >= 2 {
a, err = strconv.Atoi(fields[1])
available = uint64(a) * 1024
available = uint64(a) / 1024
}
if err != nil {
break
}
}
_ = cmd.Wait()

return total - available, err
return strongunits.MiB(total - available), err
}

//nolint:unused
Expand Down

0 comments on commit 4d2fc29

Please sign in to comment.