machine: `machine set` only when machine's stopped

Requires that the specified machine's state is `define.Stopped` in order
to set settings.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
This commit is contained in:
Jake Correnti 2024-02-13 15:28:36 -05:00
parent 4304e1075a
commit 0e9d867555
5 changed files with 51 additions and 19 deletions

View File

@ -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

View File

@ -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"))
})
})

View File

@ -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")
}

View File

@ -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

View File

@ -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 {