Skip to content

Commit

Permalink
Check if another VM is running on machine start
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ashley-cui committed Apr 28, 2021
1 parent 476c76f commit 53057d5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/podman/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions docs/source/markdown/podman-machine-start.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 53057d5

Please sign in to comment.