Skip to content

Commit

Permalink
lxd/storage/drivers: Refactor volume size rounding logic
Browse files Browse the repository at this point in the history
Signed-off-by: Wesley Hershberger <wesley.hershberger@canonical.com>
  • Loading branch information
MggMuggins committed May 22, 2024
1 parent 39e5776 commit 7144430
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 40 deletions.
17 changes: 3 additions & 14 deletions lxd/storage/drivers/driver_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,23 +549,12 @@ func (d *common) DeleteBucketKey(bucket Volume, keyName string, op *operations.O
return nil
}

// roundVolumeBlockSizeBytes returns size rounded to the nearest multiple of MinBlockBoundary bytes that is equal
// to or larger than sizeBytes.
// roundVolumeBlockSizeBytes returns sizeBytes rounded up to the next multiple
// of MinBlockBoundary.
func (d *common) roundVolumeBlockSizeBytes(vol Volume, sizeBytes int64) int64 {
// QEMU requires image files to be in traditional storage block boundaries.
// We use 8k here to ensure our images are compatible with all of our backend drivers.
if sizeBytes < MinBlockBoundary {
sizeBytes = MinBlockBoundary
}

roundedSizeBytes := int64(sizeBytes/MinBlockBoundary) * MinBlockBoundary

// Ensure the rounded size is at least the size specified in sizeBytes.
if roundedSizeBytes < sizeBytes {
roundedSizeBytes += MinBlockBoundary
}

return roundedSizeBytes
return roundAbove(MinBlockBoundary, sizeBytes)
}

func (d *common) isBlockBacked(vol Volume) bool {
Expand Down
17 changes: 3 additions & 14 deletions lxd/storage/drivers/driver_lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,21 +782,10 @@ func (d *lvm) GetResources() (*api.ResourcesStoragePool, error) {
return &res, nil
}

// roundVolumeBlockSizeBytes returns size rounded to the nearest multiple of the volume group extent size that is
// equal to or larger than sizeBytes.
// roundVolumeBlockSizeBytes returns sizeBytes rounded up to the next multiple
// of the volume group extent size.
func (d *lvm) roundVolumeBlockSizeBytes(vol Volume, sizeBytes int64) int64 {
// Get the volume group's physical extent size, and use that as minimum size.
vgExtentSize, _ := d.volumeGroupExtentSize(d.config["lvm.vg_name"])
if sizeBytes < vgExtentSize {
sizeBytes = vgExtentSize
}

roundedSizeBytes := int64(sizeBytes/vgExtentSize) * vgExtentSize

// Ensure the rounded size is at least the size specified in sizeBytes.
if roundedSizeBytes < sizeBytes {
roundedSizeBytes += vgExtentSize
}

return roundedSizeBytes
return roundAbove(vgExtentSize, sizeBytes)
}
13 changes: 1 addition & 12 deletions lxd/storage/drivers/driver_zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,16 +755,5 @@ func (d *zfs) roundVolumeBlockSizeBytes(vol Volume, sizeBytes int64) int64 {
minBlockSize = 16 * 1024
}

if sizeBytes < minBlockSize {
sizeBytes = minBlockSize
}

roundedSizeBytes := int64(sizeBytes/minBlockSize) * minBlockSize

// Ensure the rounded size is at least the size specified in sizeBytes.
if roundedSizeBytes < sizeBytes {
roundedSizeBytes += minBlockSize
}

return roundedSizeBytes
return roundAbove(minBlockSize, sizeBytes)
}
16 changes: 16 additions & 0 deletions lxd/storage/drivers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,19 @@ func wipeBlockHeaders(path string) error {
func IsContentBlock(contentType ContentType) bool {
return contentType == ContentTypeBlock || contentType == ContentTypeISO
}

// roundAbove returns the next multiple of `above` greater than `val`.
func roundAbove(above, val int64) int64 {
if val < above {
val = above
}

rounded := int64(val/above) * above

// Ensure the rounded size is at least x.
if rounded < val {
rounded += above
}

return rounded
}

0 comments on commit 7144430

Please sign in to comment.