Skip to content

Commit

Permalink
feat: allow setting capacity on external sources prompting volume resize
Browse files Browse the repository at this point in the history
fixes: #52
  • Loading branch information
thomasklein94 committed Apr 8, 2023
1 parent 11e7490 commit 6c1e562
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions builder/libvirt/volume/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (vs *ExternalVolumeSource) PrepareVolume(pctx *PreparationContext) multiste
return pctx.HaltOnError(err, "preparing volume: %s", err)
}

storageTargetCapacity := pctx.VolumeDefinition.Capacity

pctx.VolumeDefinition.Allocation = &libvirtxml.StorageVolumeSize{
Value: uint64(stat.Size()),
Unit: "B",
Expand Down Expand Up @@ -110,5 +112,25 @@ func (vs *ExternalVolumeSource) PrepareVolume(pctx *PreparationContext) multiste
log.Printf("Error while refreshing volume definition: %s\n", err)
}

if storageTargetCapacity != nil {
pctx.Ui.Message(fmt.Sprintf(
"Resizing volume %s/%s to meet capacity %d%s",
pctx.VolumeConfig.Pool,
pctx.VolumeConfig.Name,
storageTargetCapacity.Value,
storageTargetCapacity.Unit,
))
multiplier, err := unitToMultiplier(storageTargetCapacity.Unit)
if err != nil {
return pctx.HaltOnError(err, "Error during volume resize: %s", err)
}
targetCapacityInBytes := storageTargetCapacity.Value * uint64(multiplier)

err = pctx.Driver.StorageVolResize(*pctx.VolumeRef, targetCapacityInBytes, 0)
if err != nil {
return pctx.HaltOnError(err, "Error during volume resize: %s", err)
}
}

return multistep.ActionContinue
}
16 changes: 16 additions & 0 deletions builder/libvirt/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,19 @@ func fmtReadPostfixedValue(s string) (value uint64, unit string, err error) {
}
return
}

func unitToMultiplier(unit string) (value int64, err error) {
switch unit {
case "B":
value = 1
case "KiB":
value = 1024
case "Mib":
value = 1024 * 1024
case "Gib":
value = 1024 * 1024 * 1024
default:
err = fmt.Errorf("unknown unit %s", unit)
}
return
}

0 comments on commit 6c1e562

Please sign in to comment.