diff options
author | Ashley Cui <acui@redhat.com> | 2021-04-28 14:18:28 -0400 |
---|---|---|
committer | Ashley Cui <acui@redhat.com> | 2021-04-28 15:09:10 -0400 |
commit | 53057d5c20b2abf7ffc672972cbc83018ba0ed6c (patch) | |
tree | e0dbb98e5d80dc8726d5bf5ad8640be5edc60f02 | |
parent | 476c76f580d5cd092ff958765af36857b2a68d6c (diff) | |
download | podman-53057d5c20b2abf7ffc672972cbc83018ba0ed6c.tar.gz podman-53057d5c20b2abf7ffc672972cbc83018ba0ed6c.tar.bz2 podman-53057d5c20b2abf7ffc672972cbc83018ba0ed6c.zip |
Check if another VM is running on machine start
Only one VM can be up at a time. If another VM is running, or the current VM is running, error out on a podman machine start
[NO TESTS NEEDED]
Signed-off-by: Ashley Cui <acui@redhat.com>
-rw-r--r-- | cmd/podman/machine/start.go | 13 | ||||
-rw-r--r-- | docs/source/markdown/podman-machine-start.1.md | 3 | ||||
-rw-r--r-- | pkg/machine/config.go | 2 | ||||
-rw-r--r-- | pkg/machine/qemu/machine.go | 14 |
4 files changed, 32 insertions, 0 deletions
diff --git a/cmd/podman/machine/start.go b/cmd/podman/machine/start.go index d06e04f41..4334cfc0f 100644 --- a/cmd/podman/machine/start.go +++ b/cmd/podman/machine/start.go @@ -7,6 +7,7 @@ import ( "github.com/containers/podman/v3/pkg/domain/entities" "github.com/containers/podman/v3/pkg/machine" "github.com/containers/podman/v3/pkg/machine/qemu" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -40,6 +41,18 @@ func start(cmd *cobra.Command, args []string) error { if len(args) > 0 && len(args[0]) > 0 { vmName = args[0] } + + // We only have qemu VM's for now + active, activeName, err := qemu.CheckActiveVM() + if err != nil { + return err + } + if active { + if vmName == activeName { + return errors.Wrapf(machine.ErrVMAlreadyRunning, "cannot start VM %s", vmName) + } + return errors.Wrapf(machine.ErrMultipleActiveVM, "cannot start VM %s. VM %s is currently running", vmName, activeName) + } switch vmType { default: vm, err = qemu.LoadVMByName(vmName) diff --git a/docs/source/markdown/podman-machine-start.1.md b/docs/source/markdown/podman-machine-start.1.md index 22942767a..b430f51eb 100644 --- a/docs/source/markdown/podman-machine-start.1.md +++ b/docs/source/markdown/podman-machine-start.1.md @@ -14,6 +14,9 @@ Podman on MacOS requires a virtual machine. This is because containers are Linux containers do not run on any other OS because containers' core functionality are tied to the Linux kernel. +Only one Podman managed VM can be active at a time. If a VM is already running, +`podman machine start` will return an error. + **podman machine start** starts a Linux virtual machine where containers are run. ## OPTIONS diff --git a/pkg/machine/config.go b/pkg/machine/config.go index 32b3b5c2b..652229963 100644 --- a/pkg/machine/config.go +++ b/pkg/machine/config.go @@ -30,6 +30,8 @@ var ( DefaultIgnitionUserName = "core" ErrNoSuchVM = errors.New("VM does not exist") ErrVMAlreadyExists = errors.New("VM already exists") + ErrVMAlreadyRunning = errors.New("VM already running") + ErrMultipleActiveVM = errors.New("only one VM can be active at a time") ) type Download struct { diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index fd22f465b..269a2a2da 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -519,3 +519,17 @@ func IsValidVMName(name string) (bool, error) { } return false, nil } + +// CheckActiveVM checks if there is a VM already running +func CheckActiveVM() (bool, string, error) { + vms, err := GetVMInfos() + if err != nil { + return false, "", errors.Wrap(err, "error checking VM active") + } + for _, vm := range vms { + if vm.Running { + return true, vm.Name, nil + } + } + return false, "", nil +} |