From 1397c9e83fc6021cccd80294fb44f03eef446221 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 5 Dec 2024 13:24:46 +0100 Subject: [PATCH] disk: support automatic name generation for swap devices This commit adds support for automatic geneation of names for an lv swap devices. This saves the users of advanced blueprints some typing. --- pkg/disk/lvm.go | 11 +++++++---- pkg/disk/lvm_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/disk/lvm.go b/pkg/disk/lvm.go index 54fc2523d5..7181b53ce9 100644 --- a/pkg/disk/lvm.go +++ b/pkg/disk/lvm.go @@ -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 { + 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 is not mountable") } - mountpoint := mntble.GetMountpoint() - autoName, err := vg.genLVName(mountpoint) + autoName, err := vg.genLVName(lvName) if err != nil { return nil, err } diff --git a/pkg/disk/lvm_test.go b/pkg/disk/lvm_test.go index e75dc9304c..eb66036322 100644 --- a/pkg/disk/lvm_test.go +++ b/pkg/disk/lvm_test.go @@ -39,6 +39,21 @@ 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 TestImplementsInterfacesCompileTimeCheckLVM(t *testing.T) { var _ = Container(&LVMVolumeGroup{}) var _ = Sizeable(&LVMLogicalVolume{})