disk/partition_table: extract customization application into method

Extract the application into a utility method on `PartitionTable`.
In order for it to be usable for the first and second pass it does
take a `create` argument that controlls whether new partitons will
be created or return.
This commit is contained in:
Christian Kellner 2022-08-07 15:16:19 +02:00
parent b562d144ca
commit dd0be9e439

View file

@ -21,17 +21,9 @@ type PartitionTable struct {
func NewPartitionTable(basePT *PartitionTable, mountpoints []blueprint.FilesystemCustomization, imageSize uint64, lvmify bool, rng *rand.Rand) (*PartitionTable, error) {
newPT := basePT.Clone().(*PartitionTable)
newMountpoints := []blueprint.FilesystemCustomization{}
// first pass: enlarge existing mountpoints and collect new ones
for _, mnt := range mountpoints {
if path := entityPath(newPT, mnt.Mountpoint); len(path) != 0 {
size := newPT.AlignUp(clampFSSize(mnt.Mountpoint, mnt.MinSize))
resizeEntityBranch(path, size)
} else {
newMountpoints = append(newMountpoints, mnt)
}
}
newMountpoints, _ := newPT.applyCustomization(mountpoints, false)
// if there is any new mountpoint and lvmify is enabled, ensure we have LVM layout
if lvmify && len(newMountpoints) > 0 {
@ -43,15 +35,9 @@ func NewPartitionTable(basePT *PartitionTable, mountpoints []blueprint.Filesyste
// second pass: deal with new mountpoints and newly created ones, after switching to
// the LVM layout, if requested, which might introduce new mount points, i.e. `/boot`
for _, mnt := range newMountpoints {
size := newPT.AlignUp(clampFSSize(mnt.Mountpoint, mnt.MinSize))
if path := entityPath(newPT, mnt.Mountpoint); len(path) != 0 {
resizeEntityBranch(path, size)
} else {
if err := newPT.createFilesystem(mnt.Mountpoint, size); err != nil {
return nil, err
}
}
_, err := newPT.applyCustomization(newMountpoints, true)
if err != nil {
return nil, err
}
// TODO: make these overrideable for each image type
@ -328,6 +314,31 @@ func (pt *PartitionTable) HeaderSize() uint64 {
return header
}
// Apply filesystem filesystem customization to the partiton table. If create is false
// will only apply customizations to existing partitions and return unhandled, i.e new
// ones. An error can only occur if create is set. Conversely, it will only return non
// empty list of new mountpoints if create is false.
// Does not relayout the table, i.e. a call to relayout might be needed.
func (pt *PartitionTable) applyCustomization(mountpoints []blueprint.FilesystemCustomization, create bool) ([]blueprint.FilesystemCustomization, error) {
newMountpoints := []blueprint.FilesystemCustomization{}
for _, mnt := range mountpoints {
size := pt.AlignUp(clampFSSize(mnt.Mountpoint, mnt.MinSize))
if path := entityPath(pt, mnt.Mountpoint); len(path) != 0 {
resizeEntityBranch(path, size)
} else {
if !create {
newMountpoints = append(newMountpoints, mnt)
} else if err := pt.createFilesystem(mnt.Mountpoint, size); err != nil {
return nil, err
}
}
}
return newMountpoints, nil
}
// Dynamically calculate and update the start point for each of the existing
// partitions. Adjusts the overall size of image to either the supplied
// value in `size` or to the sum of all partitions if that is lager.