summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/machine/config.go7
-rw-r--r--pkg/machine/e2e/config_set.go43
-rw-r--r--pkg/machine/e2e/set_test.go139
-rw-r--r--pkg/machine/qemu/machine.go138
-rw-r--r--pkg/machine/qemu/machine_test.go17
-rw-r--r--pkg/machine/wsl/machine.go57
6 files changed, 346 insertions, 55 deletions
diff --git a/pkg/machine/config.go b/pkg/machine/config.go
index 5bbaf8c51..833f9cba8 100644
--- a/pkg/machine/config.go
+++ b/pkg/machine/config.go
@@ -95,7 +95,10 @@ type ListResponse struct {
}
type SetOptions struct {
- Rootful bool
+ CPUs *uint64
+ DiskSize *uint64
+ Memory *uint64
+ Rootful *bool
}
type SSHOptions struct {
@@ -118,7 +121,7 @@ type InspectOptions struct{}
type VM interface {
Init(opts InitOptions) (bool, error)
Remove(name string, opts RemoveOptions) (string, func() error, error)
- Set(name string, opts SetOptions) error
+ Set(name string, opts SetOptions) ([]error, error)
SSH(name string, opts SSHOptions) error
Start(name string, opts StartOptions) error
State(bypass bool) (Status, error)
diff --git a/pkg/machine/e2e/config_set.go b/pkg/machine/e2e/config_set.go
new file mode 100644
index 000000000..b310ab1b9
--- /dev/null
+++ b/pkg/machine/e2e/config_set.go
@@ -0,0 +1,43 @@
+package e2e
+
+import (
+ "strconv"
+)
+
+type setMachine struct {
+ cpus *uint
+ diskSize *uint
+ memory *uint
+
+ cmd []string
+}
+
+func (i *setMachine) buildCmd(m *machineTestBuilder) []string {
+ cmd := []string{"machine", "set"}
+ if i.cpus != nil {
+ cmd = append(cmd, "--cpus", strconv.Itoa(int(*i.cpus)))
+ }
+ if i.diskSize != nil {
+ cmd = append(cmd, "--disk-size", strconv.Itoa(int(*i.diskSize)))
+ }
+ if i.memory != nil {
+ cmd = append(cmd, "--memory", strconv.Itoa(int(*i.memory)))
+ }
+ cmd = append(cmd, m.name)
+ i.cmd = cmd
+ return cmd
+}
+
+func (i *setMachine) withCPUs(num uint) *setMachine {
+ i.cpus = &num
+ return i
+}
+func (i *setMachine) withDiskSize(size uint) *setMachine {
+ i.diskSize = &size
+ return i
+}
+
+func (i *setMachine) withMemory(num uint) *setMachine {
+ i.memory = &num
+ return i
+}
diff --git a/pkg/machine/e2e/set_test.go b/pkg/machine/e2e/set_test.go
new file mode 100644
index 000000000..4b95bde8e
--- /dev/null
+++ b/pkg/machine/e2e/set_test.go
@@ -0,0 +1,139 @@
+package e2e
+
+import (
+ . "github.com/onsi/ginkgo"
+ . "github.com/onsi/gomega"
+)
+
+var _ = Describe("podman machine set", func() {
+ var (
+ mb *machineTestBuilder
+ testDir string
+ )
+
+ BeforeEach(func() {
+ testDir, mb = setup()
+ })
+ AfterEach(func() {
+ teardown(originalHomeDir, testDir, mb)
+ })
+
+ It("set machine cpus", func() {
+ name := randomString(12)
+ i := new(initMachine)
+ session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
+ Expect(err).To(BeNil())
+ Expect(session.ExitCode()).To(Equal(0))
+
+ set := setMachine{}
+ setSession, err := mb.setName(name).setCmd(set.withCPUs(2)).run()
+ Expect(err).To(BeNil())
+ Expect(setSession.ExitCode()).To(Equal(0))
+
+ s := new(startMachine)
+ startSession, err := mb.setCmd(s).run()
+ Expect(err).To(BeNil())
+ Expect(startSession.ExitCode()).To(Equal(0))
+
+ ssh2 := sshMachine{}
+ sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"lscpu", "|", "grep", "\"CPU(s):\"", "|", "head", "-1"})).run()
+ Expect(err).To(BeNil())
+ Expect(sshSession2.ExitCode()).To(Equal(0))
+ Expect(sshSession2.outputToString()).To(ContainSubstring("2"))
+
+ })
+
+ It("increase machine disk size", func() {
+ name := randomString(12)
+ i := new(initMachine)
+ session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
+ Expect(err).To(BeNil())
+ Expect(session.ExitCode()).To(Equal(0))
+
+ set := setMachine{}
+ setSession, err := mb.setName(name).setCmd(set.withDiskSize(102)).run()
+ Expect(err).To(BeNil())
+ Expect(setSession.ExitCode()).To(Equal(0))
+
+ s := new(startMachine)
+ startSession, err := mb.setCmd(s).run()
+ Expect(err).To(BeNil())
+ Expect(startSession.ExitCode()).To(Equal(0))
+
+ ssh2 := sshMachine{}
+ sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"sudo", "fdisk", "-l", "|", "grep", "Disk"})).run()
+ Expect(err).To(BeNil())
+ Expect(sshSession2.ExitCode()).To(Equal(0))
+ Expect(sshSession2.outputToString()).To(ContainSubstring("102 GiB"))
+ })
+
+ It("decrease machine disk size should fail", func() {
+ name := randomString(12)
+ i := new(initMachine)
+ session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
+ Expect(err).To(BeNil())
+ Expect(session.ExitCode()).To(Equal(0))
+
+ set := setMachine{}
+ setSession, _ := mb.setName(name).setCmd(set.withDiskSize(50)).run()
+ // TODO seems like stderr is not being returned; re-enabled when fixed
+ // Expect(err).To(BeNil())
+ Expect(setSession.ExitCode()).To(Not(Equal(0)))
+ })
+
+ It("set machine ram", func() {
+
+ name := randomString(12)
+ i := new(initMachine)
+ session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
+ Expect(err).To(BeNil())
+ Expect(session.ExitCode()).To(Equal(0))
+
+ set := setMachine{}
+ setSession, err := mb.setName(name).setCmd(set.withMemory(4000)).run()
+ Expect(err).To(BeNil())
+ Expect(setSession.ExitCode()).To(Equal(0))
+
+ s := new(startMachine)
+ startSession, err := mb.setCmd(s).run()
+ Expect(err).To(BeNil())
+ Expect(startSession.ExitCode()).To(Equal(0))
+
+ ssh2 := sshMachine{}
+ sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"cat", "/proc/meminfo", "|", "numfmt", "--field", "2", "--from-unit=Ki", "--to-unit=Mi", "|", "sed", "'s/ kB/M/g'", "|", "grep", "MemTotal"})).run()
+ Expect(err).To(BeNil())
+ Expect(sshSession2.ExitCode()).To(Equal(0))
+ Expect(sshSession2.outputToString()).To(ContainSubstring("3824"))
+ })
+
+ It("no settings should change if no flags", func() {
+ name := randomString(12)
+ i := new(initMachine)
+ session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
+ Expect(err).To(BeNil())
+ Expect(session.ExitCode()).To(Equal(0))
+
+ set := setMachine{}
+ setSession, err := mb.setName(name).setCmd(&set).run()
+ Expect(err).To(BeNil())
+ Expect(setSession.ExitCode()).To(Equal(0))
+
+ s := new(startMachine)
+ startSession, err := mb.setCmd(s).run()
+ Expect(err).To(BeNil())
+ Expect(startSession.ExitCode()).To(Equal(0))
+
+ ssh2 := sshMachine{}
+ sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"lscpu", "|", "grep", "\"CPU(s):\"", "|", "head", "-1"})).run()
+ Expect(err).To(BeNil())
+ Expect(sshSession2.ExitCode()).To(Equal(0))
+ Expect(sshSession2.outputToString()).To(ContainSubstring("1"))
+
+ ssh3 := sshMachine{}
+ sshSession3, err := mb.setName(name).setCmd(ssh3.withSSHComand([]string{"sudo", "fdisk", "-l", "|", "grep", "Disk"})).run()
+ Expect(err).To(BeNil())
+ Expect(sshSession3.ExitCode()).To(Equal(0))
+ Expect(sshSession3.outputToString()).To(ContainSubstring("100 GiB"))
+ })
+
+})
diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go
index 5481bad29..091c9f8d1 100644
--- a/pkg/machine/qemu/machine.go
+++ b/pkg/machine/qemu/machine.go
@@ -390,25 +390,9 @@ func (v *MachineVM) Init(opts machine.InitOptions) (bool, error) {
if err != nil {
return false, err
}
- // Resize the disk image to input disk size
- // only if the virtualdisk size is less than
- // the given disk size
- if opts.DiskSize<<(10*3) > originalDiskSize {
- // Find the qemu executable
- cfg, err := config.Default()
- if err != nil {
- return false, err
- }
- resizePath, err := cfg.FindHelperBinary("qemu-img", true)
- if err != nil {
- return false, err
- }
- resize := exec.Command(resizePath, []string{"resize", v.getImageFile(), strconv.Itoa(int(opts.DiskSize)) + "G"}...)
- resize.Stdout = os.Stdout
- resize.Stderr = os.Stderr
- if err := resize.Run(); err != nil {
- return false, errors.Errorf("resizing image: %q", err)
- }
+
+ if err := v.resizeDisk(opts.DiskSize, originalDiskSize>>(10*3)); err != nil {
+ return false, err
}
// If the user provides an ignition file, we need to
// copy it into the conf dir
@@ -432,14 +416,14 @@ func (v *MachineVM) Init(opts machine.InitOptions) (bool, error) {
return err == nil, err
}
-func (v *MachineVM) Set(_ string, opts machine.SetOptions) error {
- if v.Rootful == opts.Rootful {
- return nil
- }
+func (v *MachineVM) Set(_ string, opts machine.SetOptions) ([]error, error) {
+ // If one setting fails to be applied, the others settings will not fail and still be applied.
+ // The setting(s) that failed to be applied will have its errors returned in setErrors
+ var setErrors []error
state, err := v.State(false)
if err != nil {
- return err
+ return setErrors, err
}
if state == machine.Running {
@@ -447,26 +431,45 @@ func (v *MachineVM) Set(_ string, opts machine.SetOptions) error {
if v.Name != machine.DefaultMachineName {
suffix = " " + v.Name
}
- return errors.Errorf("cannot change setting while the vm is running, run 'podman machine stop%s' first", suffix)
+ return setErrors, errors.Errorf("cannot change settings while the vm is running, run 'podman machine stop%s' first", suffix)
}
- changeCon, err := machine.AnyConnectionDefault(v.Name, v.Name+"-root")
- if err != nil {
- return err
+ if opts.Rootful != nil && v.Rootful != *opts.Rootful {
+ if err := v.setRootful(*opts.Rootful); err != nil {
+ setErrors = append(setErrors, errors.Wrapf(err, "failed to set rootful option"))
+ } else {
+ v.Rootful = *opts.Rootful
+ }
}
- if changeCon {
- newDefault := v.Name
- if opts.Rootful {
- newDefault += "-root"
- }
- if err := machine.ChangeDefault(newDefault); err != nil {
- return err
+ if opts.CPUs != nil && v.CPUs != *opts.CPUs {
+ v.CPUs = *opts.CPUs
+ v.editCmdLine("-smp", strconv.Itoa(int(v.CPUs)))
+ }
+
+ if opts.Memory != nil && v.Memory != *opts.Memory {
+ v.Memory = *opts.Memory
+ v.editCmdLine("-m", strconv.Itoa(int(v.Memory)))
+ }
+
+ if opts.DiskSize != nil && v.DiskSize != *opts.DiskSize {
+ if err := v.resizeDisk(*opts.DiskSize, v.DiskSize); err != nil {
+ setErrors = append(setErrors, errors.Wrapf(err, "failed to resize disk"))
+ } else {
+ v.DiskSize = *opts.DiskSize
}
}
- v.Rootful = opts.Rootful
- return v.writeConfig()
+ err = v.writeConfig()
+ if err != nil {
+ setErrors = append(setErrors, err)
+ }
+
+ if len(setErrors) > 0 {
+ return setErrors, setErrors[0]
+ }
+
+ return setErrors, nil
}
// Start executes the qemu command line and forks it
@@ -1462,3 +1465,64 @@ func (v *MachineVM) getImageFile() string {
func (v *MachineVM) getIgnitionFile() string {
return v.IgnitionFilePath.GetPath()
}
+
+//resizeDisk increases the size of the machine's disk in GB.
+func (v *MachineVM) resizeDisk(diskSize uint64, oldSize uint64) error {
+ // Resize the disk image to input disk size
+ // only if the virtualdisk size is less than
+ // the given disk size
+ if diskSize < oldSize {
+ return errors.Errorf("new disk size must be larger than current disk size: %vGB", oldSize)
+ }
+
+ // Find the qemu executable
+ cfg, err := config.Default()
+ if err != nil {
+ return err
+ }
+ resizePath, err := cfg.FindHelperBinary("qemu-img", true)
+ if err != nil {
+ return err
+ }
+ resize := exec.Command(resizePath, []string{"resize", v.getImageFile(), strconv.Itoa(int(diskSize)) + "G"}...)
+ resize.Stdout = os.Stdout
+ resize.Stderr = os.Stderr
+ if err := resize.Run(); err != nil {
+ return errors.Errorf("resizing image: %q", err)
+ }
+
+ return nil
+}
+
+func (v *MachineVM) setRootful(rootful bool) error {
+ changeCon, err := machine.AnyConnectionDefault(v.Name, v.Name+"-root")
+ if err != nil {
+ return err
+ }
+
+ if changeCon {
+ newDefault := v.Name
+ if rootful {
+ newDefault += "-root"
+ }
+ err := machine.ChangeDefault(newDefault)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (v *MachineVM) editCmdLine(flag string, value string) {
+ found := false
+ for i, val := range v.CmdLine {
+ if val == flag {
+ found = true
+ v.CmdLine[i+1] = value
+ break
+ }
+ }
+ if !found {
+ v.CmdLine = append(v.CmdLine, []string{flag, value}...)
+ }
+}
diff --git a/pkg/machine/qemu/machine_test.go b/pkg/machine/qemu/machine_test.go
new file mode 100644
index 000000000..62ca6068a
--- /dev/null
+++ b/pkg/machine/qemu/machine_test.go
@@ -0,0 +1,17 @@
+package qemu
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestEditCmd(t *testing.T) {
+ vm := new(MachineVM)
+ vm.CmdLine = []string{"command", "-flag", "value"}
+
+ vm.editCmdLine("-flag", "newvalue")
+ vm.editCmdLine("-anotherflag", "anothervalue")
+
+ require.Equal(t, vm.CmdLine, []string{"command", "-flag", "newvalue", "-anotherflag", "anothervalue"})
+}
diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go
index f57dbd299..1f1f2dcaf 100644
--- a/pkg/machine/wsl/machine.go
+++ b/pkg/machine/wsl/machine.go
@@ -736,28 +736,34 @@ func pipeCmdPassThrough(name string, input string, arg ...string) error {
return cmd.Run()
}
-func (v *MachineVM) Set(name string, opts machine.SetOptions) error {
- if v.Rootful == opts.Rootful {
- return nil
+func (v *MachineVM) Set(_ string, opts machine.SetOptions) ([]error, error) {
+ // If one setting fails to be applied, the others settings will not fail and still be applied.
+ // The setting(s) that failed to be applied will have its errors returned in setErrors
+ var setErrors []error
+
+ if opts.Rootful != nil && v.Rootful != *opts.Rootful {
+ err := v.setRootful(*opts.Rootful)
+ if err != nil {
+ setErrors = append(setErrors, errors.Wrapf(err, "error setting rootful option"))
+ } else {
+ v.Rootful = *opts.Rootful
+ }
}
- changeCon, err := machine.AnyConnectionDefault(v.Name, v.Name+"-root")
- if err != nil {
- return err
+ if opts.CPUs != nil {
+ setErrors = append(setErrors, errors.Errorf("changing CPUs not suppored for WSL machines"))
}
- if changeCon {
- newDefault := v.Name
- if opts.Rootful {
- newDefault += "-root"
- }
- if err := machine.ChangeDefault(newDefault); err != nil {
- return err
- }
+ if opts.Memory != nil {
+ setErrors = append(setErrors, errors.Errorf("changing memory not suppored for WSL machines"))
+
}
- v.Rootful = opts.Rootful
- return v.writeConfig()
+ if opts.DiskSize != nil {
+ setErrors = append(setErrors, errors.Errorf("changing Disk Size not suppored for WSL machines"))
+ }
+
+ return setErrors, v.writeConfig()
}
func (v *MachineVM) Start(name string, _ machine.StartOptions) error {
@@ -1362,3 +1368,22 @@ func (p *Provider) IsValidVMName(name string) (bool, error) {
func (p *Provider) CheckExclusiveActiveVM() (bool, string, error) {
return false, "", nil
}
+
+func (v *MachineVM) setRootful(rootful bool) error {
+ changeCon, err := machine.AnyConnectionDefault(v.Name, v.Name+"-root")
+ if err != nil {
+ return err
+ }
+
+ if changeCon {
+ newDefault := v.Name
+ if rootful {
+ newDefault += "-root"
+ }
+ err := machine.ChangeDefault(newDefault)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}