Skip to content

Commit

Permalink
LVM thin pool commands
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Sep 20, 2023
1 parent 1712280 commit a59d96d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
21 changes: 20 additions & 1 deletion RECIPE.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Create LVM logical volume.
- *Name* (`string`): Logical volume name.
- *VG* (`string`): Volume group name.
- *Type* (`string`): Logical volume type. See lvcreate(8) for available types.
- *Size* (`float`): Volume group size in MiB.
- *Size* (`float`): Logical volume size in MiB.

### lvrename

Expand All @@ -163,6 +163,25 @@ Deletes LVM logical volume.
**Accepts**:
- *Name* (`string`): The logical volume name.

### make-thin-pool

Creates a new LVM thin pool from two LVs: one for metadata and another one for the data itself.

**Accepts**:
- *Name* (`string`): The created thin pool name.
- *ThinDataLV* (`string`): The LV for storing data.
- *ThinMetaLV* (`string`): The LV for storing pool metadata.

### lvcreate-thin

Same as `lvcreate`, but creates a thin LV instead.

**Accepts**:
- *Name* (`string`): Thin logical volume name.
- *VG* (`string`): Volume group name.
- *Size* (`float`): Volume group size in MiB.
- *Thinpool* (`string`): Name of the thin pool to create the LV from.

---

## Post-Installation
Expand Down
19 changes: 19 additions & 0 deletions core/lvm/lv.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,25 @@ func FindLv(vgName, lvName string) (Lv, error) {
return lvs[0], nil
}

func MakeThinPool(poolMetadata, pool interface{}) error {
poolMetadataName, err := extractNameFromLv(poolMetadata)
if err != nil {
return err
}
poolName, err := extractNameFromLv(pool)
if err != nil {
return err
}

lvm := NewLvm()
_, err = lvm.lvm2Run("lvconvert --type thin-pool --poolmetadata %s %s", poolMetadataName, poolName)
if err != nil {
return err
}

return nil
}

func (l *Lv) Rename(newName string) error {
lvm := NewLvm()
newLv, err := lvm.Lvrename(l.Name, newName, l.VgName)
Expand Down
33 changes: 33 additions & 0 deletions core/lvm/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,25 @@ func (l *Lvm) Lvcreate(name string, vg interface{}, lvType LVType, size float64)
return nil
}

func (l *Lvm) LvThinCreate(name string, vg, pool interface{}, size float64) error {
vgName, err := extractNameFromVg(vg)
if err != nil {
return fmt.Errorf("lvmThinCreate: %v", err)
}

poolName, err := extractNameFromPool(pool)
if err != nil {
return fmt.Errorf("lvmThinCreate: %v", err)
}

_, err = l.lvm2Run("lvcreate -y -n %s -V %.2fm %s %s", name, size, poolName, vgName)
if err != nil {
return fmt.Errorf("lvmThinCreate: %v", err)
}

return nil
}

// lvs (list lvs)
func (l *Lvm) Lvs(filter ...string) ([]Lv, error) {
filterStr := ""
Expand Down Expand Up @@ -480,3 +499,17 @@ func extractNameFromLv(lv interface{}) (string, error) {

return lvName, nil
}

func extractNameFromPool(pool interface{}) (string, error) {
var poolName string
switch lvar := pool.(type) {
case string:
poolName = lvar
case *Lv:
poolName = lvar.Name
default:
return "", errors.New("invalid type for pool. Must be either a string with the pool's name or a pointer to a LV struct")
}

return poolName, nil
}
37 changes: 36 additions & 1 deletion core/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
* - *Name* (`string`): Logical volume name.
* - *VG* (`string`): Volume group name.
* - *Type* (`string`): Logical volume type. See lvcreate(8) for available types.
* - *Size* (`float`): Volume group size in MiB.
* - *Size* (`float`): Logical volume size in MiB.
*/
case "lvcreate":
name := args[0].(string)
Expand Down Expand Up @@ -491,6 +491,41 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
if err != nil {
return err
}
/* !! ### make-thin-pool
*
* Creates a new LVM thin pool from two LVs: one for metadata and another one for the data itself.
*
* **Accepts**:
* - *Name* (`string`): The created thin pool name.
* - *ThinDataLV* (`string`): The LV for storing data.
* - *ThinMetaLV* (`string`): The LV for storing pool metadata.
*/
case "make-thin-pool":
thinDataLV := args[0].(string)
thinMetaLV := args[1].(string)
err := lvm.MakeThinPool(thinMetaLV, thinDataLV)
if err != nil {
return err
}
/* !! ### lvcreate-thin
*
* Same as `lvcreate`, but creates a thin LV instead.
*
* **Accepts**:
* - *Name* (`string`): Thin logical volume name.
* - *VG* (`string`): Volume group name.
* - *Size* (`float`): Volume group size in MiB.
* - *Thinpool* (`string`): Name of the thin pool to create the LV from.
*/
case "lvcreate-thin":
name := args[0].(string)
vg := args[1].(string)
vgSize := args[2].(float64)
thinPool := args[3].(string)
err := LvmInstance.LvThinCreate(name, vg, thinPool, vgSize)
if err != nil {
return err
}
/* !! --- */
default:
return fmt.Errorf("unrecognized operation %s", operation)
Expand Down

0 comments on commit a59d96d

Please sign in to comment.