Skip to content

Commit

Permalink
disk: add new method minSize() to VolumeContainer
Browse files Browse the repository at this point in the history
Add a new method to the VolumeContainer interface that calculates the
minimum size in bytes that the container needs to hold all its children
and metadata.
  • Loading branch information
achilleas-k authored and supakeen committed Jul 25, 2024
1 parent ad16001 commit e392bf7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/disk/btrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ func (b *Btrfs) MetadataSize() uint64 {
return 0
}

func (b *Btrfs) minSize(size uint64) uint64 {
var subvolsum uint64
for _, sv := range b.Subvolumes {
subvolsum += sv.Size
}
minSize := subvolsum + b.MetadataSize()

if minSize > size {
size = minSize
}

return b.AlignUp(size)
}

type BtrfsSubvolume struct {
Name string
Size uint64
Expand Down
10 changes: 10 additions & 0 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ type VolumeContainer interface {
// bytes), i.e. the storage space that needs to be reserved for
// the container itself, in contrast to the data it contains.
MetadataSize() uint64

// minSize returns the size for the VolumeContainer that is either the
// provided desired size value or the sum of all children if that is
// larger. It will also add any space required for metadata. The returned
// value should, at minimum, be large enough to fit all the children, their
// metadata, and the VolumeContainer's metadata. In other words, the
// VolumeContainer's size, or its parent size, will be able to hold the
// VolumeContainer if it is created with the exact size returned by the
// function.
minSize(size uint64) uint64
}

// FSSpec for a filesystem (UUID and Label); the first field of fstab(5)
Expand Down
13 changes: 13 additions & 0 deletions pkg/disk/luks.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ func (lc *LUKSContainer) MetadataSize() uint64 {
// 16 MiB is the default size for the LUKS2 header
return 16 * common.MiB
}

func (lc *LUKSContainer) minSize(size uint64) uint64 {
// since a LUKS container can contain pretty much any payload, but we only
// care about the ones that have a size, or contain children with sizes
minSize := lc.MetadataSize()
switch payload := lc.Payload.(type) {
case VolumeContainer:
minSize += payload.minSize(size)
case Sizeable:
minSize += payload.GetSize()
}
return minSize
}
14 changes: 14 additions & 0 deletions pkg/disk/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ func (vg *LVMVolumeGroup) MetadataSize() uint64 {
return 1 * common.MiB
}

func (vg *LVMVolumeGroup) minSize(size uint64) uint64 {
var lvsum uint64
for _, lv := range vg.LogicalVolumes {
lvsum += lv.Size
}
minSize := lvsum + vg.MetadataSize()

if minSize > size {
size = minSize
}

return vg.AlignUp(size)
}

type LVMLogicalVolume struct {
Name string
Size uint64
Expand Down

0 comments on commit e392bf7

Please sign in to comment.