From 344fc968dee110bd0cfeeb66bfaeb1c07f784828 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Wed, 24 Jul 2024 22:31:49 +0200 Subject: [PATCH] disk: add a fitTo() method to Partition that uses minSize() Add a new Partition method, fitTo(), which resizes a partition to either fit the given size or the sum of its contents + metadata. The content sum and metadata is calculated using the new minSize() method for VolumeContainers. --- pkg/disk/partition.go | 13 +++++++++++++ pkg/disk/partition_table.go | 2 ++ 2 files changed, 15 insertions(+) diff --git a/pkg/disk/partition.go b/pkg/disk/partition.go index b550ea78d9..6549585532 100644 --- a/pkg/disk/partition.go +++ b/pkg/disk/partition.go @@ -59,6 +59,19 @@ func (p *Partition) GetChild(n uint) Entity { return p.Payload } +// fitTo resizes a partition to be either the given the size or the size of its +// payload if that is larger. The payload can be larger if it is a container +// with sized children. +func (p *Partition) fitTo(size uint64) { + payload, isVC := p.Payload.(VolumeContainer) + if isVC { + if payloadMinSize := payload.minSize(size); payloadMinSize > size { + size = payloadMinSize + } + } + p.Size = size +} + func (p *Partition) GetSize() uint64 { return p.Size } diff --git a/pkg/disk/partition_table.go b/pkg/disk/partition_table.go index 563bbc8877..4764d4e4e6 100644 --- a/pkg/disk/partition_table.go +++ b/pkg/disk/partition_table.go @@ -423,6 +423,7 @@ func (pt *PartitionTable) relayout(size uint64) uint64 { continue } partition.Start = start + partition.fitTo(partition.Size) partition.Size = pt.AlignUp(partition.Size) start += partition.Size } @@ -433,6 +434,7 @@ func (pt *PartitionTable) relayout(size uint64) uint64 { root := &pt.Partitions[rootIdx] root.Start = start + root.fitTo(root.Size) // add the extra padding specified in the partition table footer += pt.ExtraPadding