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 ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net" "net"
"strconv" "strconv"
@ -93,6 +94,17 @@ func (a AppleHVStubber) RemoveAndCleanMachines(_ *define.MachineDirs) error {
} }
func (a AppleHVStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.SetOptions) 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 opts.DiskSize != nil {
if err := resizeDisk(mc, *opts.DiskSize); err != nil { if err := resizeDisk(mc, *opts.DiskSize); err != nil {
return err return err

View File

@ -192,4 +192,23 @@ var _ = Describe("podman machine set", func() {
Expect(inspectSession).To(Exit(0)) Expect(inspectSession).To(Exit(0))
Expect(inspectSession.outputToString()).To(Equal("true")) 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 return err
} }
// TODO lets move this up into set as a "rule" for all machines
if vm.State() != hypervctl.Disabled { if vm.State() != hypervctl.Disabled {
return errors.New("unable to change settings unless vm is stopped") return errors.New("unable to change settings unless vm is stopped")
} }

View File

@ -3,6 +3,7 @@ package qemu
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"errors"
"fmt" "fmt"
"net" "net"
"os" "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 { 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 opts.DiskSize != nil {
if err := q.resizeDisk(*opts.DiskSize, mc.ImagePath); err != nil { if err := q.resizeDisk(*opts.DiskSize, mc.ImagePath); err != nil {
return err return err

View File

@ -133,27 +133,17 @@ func (w WSLStubber) RemoveAndCleanMachines(_ *define.MachineDirs) error {
return nil return nil
} }
func (w WSLStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.SetOptions) error { func (w WSLStubber) SetProviderAttrs(mc *vmconfigs.MachineConfig, opts define.SetOptions) error {
mc.Lock() mc.Lock()
defer mc.Unlock() defer mc.Unlock()
// TODO the check for running when setting rootful is something I have not state, err := w.State(mc, false)
// seen in the other distributions. I wonder if this is true everywhere or just if err != nil {
// with WSL? return err
// TODO maybe the "rule" for set is that it must be done when the machine is }
// stopped? if state != define.Stopped {
// if opts.Rootful != nil && v.Rootful != *opts.Rootful { return errors.New("unable to change settings unless vm is stopped")
// 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
// }
// }
if opts.Rootful != nil && mc.HostUser.Rootful != *opts.Rootful { if opts.Rootful != nil && mc.HostUser.Rootful != *opts.Rootful {
if err := mc.SetRootful(*opts.Rootful); err != nil { if err := mc.SetRootful(*opts.Rootful); err != nil {