Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
pkg/lvm: serialize lvm invocations using lock file
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustav Paul committed Aug 8, 2019
1 parent ba76dce commit b326cef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/lvm/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,10 @@ func LookupPhysicalVolume(name string) (*PhysicalVolume, error) {
// https://github.com/Jajcus/lvm2/blob/266d6564d7a72fcff5b25367b7a95424ccf8089e/lib/metadata/metadata.c#L983

func run(cmd string, v interface{}, extraArgs ...string) error {
if lvmlock != nil {

This comment has been minimized.

Copy link
@bbannier

bbannier Aug 8, 2019

Do we need this check? Looks like the locking library already short-circuits if we already hold the lock, https://godoc.org/github.com/gofrs/flock#Flock.Lock.

lvmlock.Lock()

This comment has been minimized.

Copy link
@bbannier

bbannier Aug 8, 2019

Let's add a comment here why we do not use TryLock as advised by the package (we do not want to make any progress until we can acquire the lock).

defer lvmlock.Unlock()
}
var args []string
if v != nil {
args = append(args, "--reportformat=json")
Expand Down
11 changes: 11 additions & 0 deletions pkg/lvm/lvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package lvm

import (
"fmt"
"io/ioutil"
"reflect"
"sort"
"strings"
Expand All @@ -18,6 +19,16 @@ const (
pvsize = 100 << 20
)

func init() {
// Set the lock file to use in tests.
file, err := ioutil.TempFile("", "csilvm-test-lock-file")
if err != nil {
panic(err)
}
file.Close()
SetLockFilePath(file.Name())
}

func TestCreatePhysicalDevice(t *testing.T) {
loop, err := CreateLoopDevice(pvsize)
if err != nil {
Expand Down

0 comments on commit b326cef

Please sign in to comment.