Files
Paul Holzinger e82d196269 pkg/machine: make checkExclusiveActiveVM race free
We need to take another lock to prevent concurrent starts from different
machines.

I manually tested it by starting three VM in parallel with:
podman machine start & podman machine start test1 & podman machine start test2

I also added a CI test that seems to work as expected (failed with the
old binary, worked with the new)

Before this patch I was able to start more than VM, with this patch it
now only starts one of them and the other ones will fail to start with
a proper error.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-03-14 11:13:08 +01:00

37 lines
1004 B
Go

package lock
import (
"fmt"
"path/filepath"
"github.com/containers/podman/v5/pkg/machine/env"
"github.com/containers/storage/pkg/lockfile"
)
func GetMachineLock(name string, machineConfigDir string) (*lockfile.LockFile, error) {
lockPath := filepath.Join(machineConfigDir, name+".lock")
lock, err := lockfile.GetLockFile(lockPath)
if err != nil {
return nil, fmt.Errorf("creating lockfile for VM: %w", err)
}
return lock, nil
}
const machineStartLockName = "machine-start.lock"
// GetMachineStartLock is a lock only used to prevent starting different machines at the same time,
// This is required as most provides support at max 1 running VM and to check this race free we
// cannot allows starting two machine.
func GetMachineStartLock() (*lockfile.LockFile, error) {
lockDir, err := env.GetGlobalDataDir()
if err != nil {
return nil, err
}
lock, err := lockfile.GetLockFile(filepath.Join(lockDir, machineStartLockName))
if err != nil {
return nil, err
}
return lock, nil
}