Skip to content

Commit

Permalink
disk: support automatic name generation for swap devices
Browse files Browse the repository at this point in the history
This commit adds support for automatic geneation of names for
an lv swap devices. This saves the users of advanced blueprints
some typing.
  • Loading branch information
mvo5 committed Dec 5, 2024
1 parent 7600a5a commit f673cb1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/disk/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ func (vg *LVMVolumeGroup) CreateLogicalVolume(lvName string, size uint64, payloa

if lvName == "" {
// generate a name based on the payload's mountpoint
mntble, ok := payload.(Mountable)
if !ok {
return nil, fmt.Errorf("could not create logical volume: no name provided and payload is not mountable")
switch ent := payload.(type) {
case Mountable:
lvName = ent.GetMountpoint()
case *Swap:
lvName = "swap"
default:
return nil, fmt.Errorf("could not create logical volume: no name provided and payload %T is not mountable or swap", payload)
}
mountpoint := mntble.GetMountpoint()
autoName, err := vg.genLVName(mountpoint)
autoName, err := vg.genLVName(lvName)
if err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/disk/lvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ func TestLVMVCreateMountpoint(t *testing.T) {
assert.Error(err)
}

func TestLVMVCreateLogicalVolumeSwap(t *testing.T) {
vg := &LVMVolumeGroup{
Name: "root",
Description: "root volume group",
}
swap := &Swap{}
lv, err := vg.CreateLogicalVolume("", 12345, swap)
assert.NoError(t, err)
assert.Equal(t, "swaplv", lv.Name)
// one more
lv2, err := vg.CreateLogicalVolume("", 12345, swap)
assert.NoError(t, err)
assert.Equal(t, "swaplv00", lv2.Name)
}

func TestLVMVCreateLogicalVolumeWrongType(t *testing.T) {
vg := &LVMVolumeGroup{
Name: "root",
}
_, err := vg.CreateLogicalVolume("", 12345, &LUKSContainer{})
assert.EqualError(t, err, `could not create logical volume: no name provided and payload *disk.LUKSContainer is not mountable or swap`)
}

func TestImplementsInterfacesCompileTimeCheckLVM(t *testing.T) {
var _ = Container(&LVMVolumeGroup{})
var _ = Sizeable(&LVMLogicalVolume{})
Expand Down

0 comments on commit f673cb1

Please sign in to comment.