Skip to content

Commit

Permalink
disk: replace the lvm name generation with genUniqueString()
Browse files Browse the repository at this point in the history
Replace the heart of the lvm name generator with the new function.
Update the vg.CreateMountpoint() test, because the old implementation
was off by one and would fail when '99' was available.
  • Loading branch information
achilleas-k authored and ondrejbudai committed Nov 1, 2024
1 parent 7ca293e commit d6365bd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 20 deletions.
2 changes: 2 additions & 0 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ func NewVolIDFromRand(r *rand.Rand) string {
// the existing set. If the base itself does not exist, it is returned as is,
// otherwise a two digit number is added and incremented until a unique string
// is found.
// This function is mimicking what blivet does for avoiding name collisions.
// See blivet/blivet.py#L1060 commit 2eb4bd4
func genUniqueString(base string, existing map[string]bool) (string, error) {
if !existing[base] {
return base, nil
Expand Down
22 changes: 3 additions & 19 deletions pkg/disk/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,9 @@ func (vg *LVMVolumeGroup) genLVName(base string) (string, error) {

base = lvname(base) // if the mountpoint is used (i.e. if the base contains /), sanitize it and append 'lv'

var exists bool
name := base

// Make sure that we don't collide with an existing volume, e.g. 'home/test'
// and /home/test_test would collide. We try 100 times and then give up. This
// is mimicking what blivet does. See blivet/blivet.py#L1060 commit 2eb4bd4
for i := 0; i < 100; i++ {
exists = names[name]
if !exists {
break
}

name = fmt.Sprintf("%s%02d", base, i)
}

if exists {
return "", fmt.Errorf("could not create logical volume: name collision")
}
return name, nil
// Make sure that we don't collide with an existing volume, e.g.
// 'home/test' and /home_test would collide.
return genUniqueString(base, names)
}

// CreateLogicalVolume creates a new logical volume on the volume group. If a
Expand Down
2 changes: 1 addition & 1 deletion pkg/disk/lvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestLVMVCreateMountpoint(t *testing.T) {
assert.Equal("home_testlv00", dedup.Name)

// Lets collide it
for i := 0; i < 98; i++ {
for i := 0; i < 99; i++ {
_, err = vg.CreateMountpoint("/home/test", 0)
assert.NoError(err)
}
Expand Down

0 comments on commit d6365bd

Please sign in to comment.