diff --git a/pkg/machine/applehv/stubber.go b/pkg/machine/applehv/stubber.go index 3ebaf1eaa4..7f18b419ff 100644 --- a/pkg/machine/applehv/stubber.go +++ b/pkg/machine/applehv/stubber.go @@ -4,6 +4,7 @@ package applehv import ( "context" + "errors" "fmt" "net" "strconv" @@ -40,7 +41,7 @@ type AppleHVStubber struct { } func (a AppleHVStubber) UserModeNetworkEnabled(_ *vmconfigs.MachineConfig) bool { -return true + return true } func (a AppleHVStubber) UseProviderNetworkSetup() bool { @@ -93,6 +94,17 @@ func (a AppleHVStubber) RemoveAndCleanMachines(_ *define.MachineDirs) error { } func (a AppleHVStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.SetOptions) error { + mc.Lock() + defer mc.Unlock() + + state, err := a.State(mc, false) + if err != nil { + return err + } + if state != define.Stopped { + return errors.New("unable to change settings unless vm is stopped") + } + if opts.DiskSize != nil { if err := resizeDisk(mc, *opts.DiskSize); err != nil { return err diff --git a/pkg/machine/e2e/set_test.go b/pkg/machine/e2e/set_test.go index 439539746b..f1aa5b4771 100644 --- a/pkg/machine/e2e/set_test.go +++ b/pkg/machine/e2e/set_test.go @@ -192,4 +192,23 @@ var _ = Describe("podman machine set", func() { Expect(inspectSession).To(Exit(0)) Expect(inspectSession.outputToString()).To(Equal("true")) }) + + It("set while machine already running", func() { + name := randomString() + i := new(initMachine) + session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() + Expect(err).ToNot(HaveOccurred()) + Expect(session).To(Exit(0)) + + s := new(startMachine) + startSession, err := mb.setCmd(s).run() + Expect(err).ToNot(HaveOccurred()) + Expect(startSession).To(Exit(0)) + + set := setMachine{} + setSession, err := mb.setName(name).setCmd(set.withRootful(true)).run() + Expect(err).ToNot(HaveOccurred()) + Expect(setSession).To(Exit(125)) + Expect(setSession.errorToString()).To(ContainSubstring("Error: unable to change settings unless vm is stopped")) + }) }) diff --git a/pkg/machine/hyperv/stubber.go b/pkg/machine/hyperv/stubber.go index 080d78c039..6d5c79d14d 100644 --- a/pkg/machine/hyperv/stubber.go +++ b/pkg/machine/hyperv/stubber.go @@ -317,7 +317,6 @@ func (h HyperVStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define return err } - // TODO lets move this up into set as a "rule" for all machines if vm.State() != hypervctl.Disabled { return errors.New("unable to change settings unless vm is stopped") } diff --git a/pkg/machine/qemu/stubber.go b/pkg/machine/qemu/stubber.go index 28614246bb..19b9758244 100644 --- a/pkg/machine/qemu/stubber.go +++ b/pkg/machine/qemu/stubber.go @@ -3,6 +3,7 @@ package qemu import ( "bufio" "bytes" + "errors" "fmt" "net" "os" @@ -255,6 +256,17 @@ func (q *QEMUStubber) resizeDisk(newSize strongunits.GiB, diskPath *define.VMFil } func (q *QEMUStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.SetOptions) error { + mc.Lock() + defer mc.Unlock() + + state, err := q.State(mc, false) + if err != nil { + return err + } + if state != define.Stopped { + return errors.New("unable to change settings unless vm is stopped") + } + if opts.DiskSize != nil { if err := q.resizeDisk(*opts.DiskSize, mc.ImagePath); err != nil { return err diff --git a/pkg/machine/wsl/stubber.go b/pkg/machine/wsl/stubber.go index a3693d9c99..9d0938530c 100644 --- a/pkg/machine/wsl/stubber.go +++ b/pkg/machine/wsl/stubber.go @@ -133,27 +133,17 @@ func (w WSLStubber) RemoveAndCleanMachines(_ *define.MachineDirs) error { return nil } - func (w WSLStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.SetOptions) error { mc.Lock() defer mc.Unlock() - // TODO the check for running when setting rootful is something I have not - // seen in the other distributions. I wonder if this is true everywhere or just - // with WSL? - // TODO maybe the "rule" for set is that it must be done when the machine is - // stopped? - // if opts.Rootful != nil && v.Rootful != *opts.Rootful { - // err := v.setRootful(*opts.Rootful) - // if err != nil { - // setErrors = append(setErrors, fmt.Errorf("setting rootful option: %w", err)) - // } else { - // if v.isRunning() { - // logrus.Warn("restart is necessary for rootful change to go into effect") - // } - // v.Rootful = *opts.Rootful - // } - // } + state, err := w.State(mc, false) + if err != nil { + return err + } + if state != define.Stopped { + return errors.New("unable to change settings unless vm is stopped") + } if opts.Rootful != nil && mc.HostUser.Rootful != *opts.Rootful { if err := mc.SetRootful(*opts.Rootful); err != nil {