diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/machine/config.go | 7 | ||||
-rw-r--r-- | pkg/machine/e2e/config_set.go | 43 | ||||
-rw-r--r-- | pkg/machine/e2e/init_test.go | 6 | ||||
-rw-r--r-- | pkg/machine/e2e/inspect_test.go | 9 | ||||
-rw-r--r-- | pkg/machine/e2e/rm_test.go | 11 | ||||
-rw-r--r-- | pkg/machine/e2e/set_test.go | 134 | ||||
-rw-r--r-- | pkg/machine/e2e/ssh_test.go | 11 | ||||
-rw-r--r-- | pkg/machine/e2e/start_test.go | 5 | ||||
-rw-r--r-- | pkg/machine/e2e/stop_test.go | 9 | ||||
-rw-r--r-- | pkg/machine/qemu/machine.go | 138 | ||||
-rw-r--r-- | pkg/machine/qemu/machine_test.go | 17 | ||||
-rw-r--r-- | pkg/machine/wsl/machine.go | 57 |
12 files changed, 369 insertions, 78 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/init_test.go b/pkg/machine/e2e/init_test.go index 309d460a9..304122738 100644 --- a/pkg/machine/e2e/init_test.go +++ b/pkg/machine/e2e/init_test.go @@ -28,13 +28,13 @@ var _ = Describe("podman machine init", func() { reallyLongName := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" session, err := mb.setName(reallyLongName).setCmd(&i).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(125)) + Expect(session).To(Exit(125)) }) It("simple init", func() { i := new(initMachine) session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(0)) + Expect(session).To(Exit(0)) inspectBefore, ec, err := mb.toQemuInspectInfo() Expect(err).To(BeNil()) @@ -52,7 +52,7 @@ var _ = Describe("podman machine init", func() { i := initMachine{} session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(0)) + Expect(session).To(Exit(0)) inspectBefore, ec, err := mb.toQemuInspectInfo() Expect(ec).To(BeZero()) diff --git a/pkg/machine/e2e/inspect_test.go b/pkg/machine/e2e/inspect_test.go index 30d810b8f..e282dd21d 100644 --- a/pkg/machine/e2e/inspect_test.go +++ b/pkg/machine/e2e/inspect_test.go @@ -7,6 +7,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gexec" ) var _ = Describe("podman machine stop", func() { @@ -27,24 +28,24 @@ var _ = Describe("podman machine stop", func() { reallyLongName := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" session, err := mb.setName(reallyLongName).setCmd(&i).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(125)) + Expect(session).To(Exit(125)) }) It("inspect two machines", func() { i := new(initMachine) foo1, err := mb.setName("foo1").setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) - Expect(foo1.ExitCode()).To(Equal(0)) + Expect(foo1).To(Exit(0)) ii := new(initMachine) foo2, err := mb.setName("foo2").setCmd(ii.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) - Expect(foo2.ExitCode()).To(Equal(0)) + Expect(foo2).To(Exit(0)) inspect := new(inspectMachine) inspectSession, err := mb.setName("foo1").setCmd(inspect).run() Expect(err).To(BeNil()) - Expect(inspectSession.ExitCode()).To(Equal(0)) + Expect(inspectSession).To(Exit(0)) type fakeInfos struct { Status string diff --git a/pkg/machine/e2e/rm_test.go b/pkg/machine/e2e/rm_test.go index 011da5dde..43b8c594c 100644 --- a/pkg/machine/e2e/rm_test.go +++ b/pkg/machine/e2e/rm_test.go @@ -3,6 +3,7 @@ package e2e import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gexec" ) var _ = Describe("podman machine rm", func() { @@ -23,14 +24,14 @@ var _ = Describe("podman machine rm", func() { reallyLongName := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" session, err := mb.setName(reallyLongName).setCmd(&i).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(125)) + Expect(session).To(Exit(125)) }) It("Remove machine", func() { i := new(initMachine) session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(0)) + Expect(session).To(Exit(0)) rm := rmMachine{} _, err = mb.setCmd(rm.withForce()).run() Expect(err).To(BeNil()) @@ -46,18 +47,18 @@ var _ = Describe("podman machine rm", func() { i := new(initMachine) session, err := mb.setCmd(i.withImagePath(mb.imagePath).withNow()).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(0)) + Expect(session).To(Exit(0)) rm := new(rmMachine) // Removing a running machine should fail stop, err := mb.setCmd(rm).run() Expect(err).To(BeNil()) - Expect(stop.ExitCode()).To(Equal(125)) + Expect(stop).To(Exit(125)) // Removing again with force stopAgain, err := mb.setCmd(rm.withForce()).run() Expect(err).To(BeNil()) - Expect(stopAgain.ExitCode()).To(BeZero()) + Expect(stopAgain).To(Exit(0)) // Inspect to be dead sure _, ec, err := mb.toQemuInspectInfo() diff --git a/pkg/machine/e2e/set_test.go b/pkg/machine/e2e/set_test.go new file mode 100644 index 000000000..9af29c560 --- /dev/null +++ b/pkg/machine/e2e/set_test.go @@ -0,0 +1,134 @@ +package e2e + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gexec" +) + +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).To(Exit(0)) + + set := setMachine{} + setSession, err := mb.setName(name).setCmd(set.withCPUs(2)).run() + Expect(err).To(BeNil()) + Expect(setSession).To(Exit(0)) + + s := new(startMachine) + startSession, err := mb.setCmd(s).run() + Expect(err).To(BeNil()) + Expect(startSession).To(Exit(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).To(Exit(0)) + Expect(sshSession2.outputToString()).To(ContainSubstring("2")) + + // Setting a running machine results in 125 + runner, err := mb.setName(name).setCmd(set.withCPUs(4)).run() + Expect(err).To(BeNil()) + Expect(runner).To(Exit(125)) + }) + + 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).To(Exit(0)) + + set := setMachine{} + setSession, err := mb.setName(name).setCmd(set.withDiskSize(102)).run() + Expect(err).To(BeNil()) + Expect(setSession).To(Exit(0)) + + // shrinking disk size iss verboten + shrink, err := mb.setName(name).setCmd(set.withDiskSize(5)).run() + Expect(err).To(BeNil()) + Expect(shrink).To(Exit(125)) + + s := new(startMachine) + startSession, err := mb.setCmd(s).run() + Expect(err).To(BeNil()) + Expect(startSession).To(Exit(0)) + + ssh2 := sshMachine{} + sshSession2, err := mb.setName(name).setCmd(ssh2.withSSHComand([]string{"sudo", "fdisk", "-l", "|", "grep", "Disk"})).run() + Expect(err).To(BeNil()) + Expect(sshSession2).To(Exit(0)) + Expect(sshSession2.outputToString()).To(ContainSubstring("102 GiB")) + }) + + 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).To(Exit(0)) + + set := setMachine{} + setSession, err := mb.setName(name).setCmd(set.withMemory(4000)).run() + Expect(err).To(BeNil()) + Expect(setSession).To(Exit(0)) + + s := new(startMachine) + startSession, err := mb.setCmd(s).run() + Expect(err).To(BeNil()) + Expect(startSession).To(Exit(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).To(Exit(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).To(Exit(0)) + + set := setMachine{} + setSession, err := mb.setName(name).setCmd(&set).run() + Expect(err).To(BeNil()) + Expect(setSession).To(Exit(0)) + + s := new(startMachine) + startSession, err := mb.setCmd(s).run() + Expect(err).To(BeNil()) + Expect(startSession).To(Exit(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).To(Exit(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).To(Exit(0)) + Expect(sshSession3.outputToString()).To(ContainSubstring("100 GiB")) + }) + +}) diff --git a/pkg/machine/e2e/ssh_test.go b/pkg/machine/e2e/ssh_test.go index 90296fa10..155d39a64 100644 --- a/pkg/machine/e2e/ssh_test.go +++ b/pkg/machine/e2e/ssh_test.go @@ -3,6 +3,7 @@ package e2e import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gexec" ) var _ = Describe("podman machine ssh", func() { @@ -23,7 +24,7 @@ var _ = Describe("podman machine ssh", func() { ssh := sshMachine{} session, err := mb.setName(name).setCmd(ssh).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(125)) + Expect(session).To(Exit(125)) // TODO seems like stderr is not being returned; re-enabled when fixed //Expect(session.outputToString()).To(ContainSubstring("not exist")) }) @@ -33,14 +34,14 @@ var _ = Describe("podman machine ssh", func() { i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(0)) + Expect(session).To(Exit(0)) ssh := sshMachine{} sshSession, err := mb.setName(name).setCmd(ssh).run() Expect(err).To(BeNil()) // TODO seems like stderr is not being returned; re-enabled when fixed //Expect(sshSession.outputToString()).To(ContainSubstring("is not running")) - Expect(sshSession.ExitCode()).To(Equal(125)) + Expect(sshSession).To(Exit(125)) }) It("ssh to running machine and check os-type", func() { @@ -48,12 +49,12 @@ var _ = Describe("podman machine ssh", func() { i := new(initMachine) session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(0)) + Expect(session).To(Exit(0)) ssh := sshMachine{} sshSession, err := mb.setName(name).setCmd(ssh.withSSHComand([]string{"cat", "/etc/os-release"})).run() Expect(err).To(BeNil()) - Expect(sshSession.ExitCode()).To(Equal(0)) + Expect(sshSession).To(Exit(0)) Expect(sshSession.outputToString()).To(ContainSubstring("Fedora CoreOS")) }) }) diff --git a/pkg/machine/e2e/start_test.go b/pkg/machine/e2e/start_test.go index 1cda0e8f1..1de66eb9a 100644 --- a/pkg/machine/e2e/start_test.go +++ b/pkg/machine/e2e/start_test.go @@ -4,6 +4,7 @@ import ( "github.com/containers/podman/v4/pkg/machine" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gexec" ) var _ = Describe("podman machine start", func() { @@ -22,11 +23,11 @@ var _ = Describe("podman machine start", func() { i := new(initMachine) session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(0)) + Expect(session).To(Exit(0)) s := new(startMachine) startSession, err := mb.setCmd(s).run() Expect(err).To(BeNil()) - Expect(startSession.ExitCode()).To(Equal(0)) + Expect(startSession).To(Exit(0)) info, ec, err := mb.toQemuInspectInfo() Expect(err).To(BeNil()) diff --git a/pkg/machine/e2e/stop_test.go b/pkg/machine/e2e/stop_test.go index 5dee6a345..0c27045a6 100644 --- a/pkg/machine/e2e/stop_test.go +++ b/pkg/machine/e2e/stop_test.go @@ -3,6 +3,7 @@ package e2e import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gexec" ) var _ = Describe("podman machine stop", func() { @@ -23,24 +24,24 @@ var _ = Describe("podman machine stop", func() { reallyLongName := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" session, err := mb.setName(reallyLongName).setCmd(&i).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(125)) + Expect(session).To(Exit(125)) }) It("Stop running machine", func() { i := new(initMachine) session, err := mb.setCmd(i.withImagePath(mb.imagePath).withNow()).run() Expect(err).To(BeNil()) - Expect(session.ExitCode()).To(Equal(0)) + Expect(session).To(Exit(0)) stop := new(stopMachine) // Removing a running machine should fail stopSession, err := mb.setCmd(stop).run() Expect(err).To(BeNil()) - Expect(stopSession.ExitCode()).To(Equal(0)) + Expect(stopSession).To(Exit(0)) // Stopping it again should not result in an error stopAgain, err := mb.setCmd(stop).run() Expect(err).To(BeNil()) - Expect(stopAgain.ExitCode()).To(BeZero()) + Expect(stopAgain).To(Exit((0))) }) }) diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index 5481bad29..ccfad90f7 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 +} |