disk: add GenerateUUIDs helper

Add a new helper to `PartitionTable` that generates all necessary
uuids for uuid fields that are empty but should not. These are
the file system uuids and, in case of a GPT layout, partition ids.
This commit is contained in:
Christian Kellner 2021-11-15 13:59:36 +01:00 committed by Tom Gundersen
parent 3e72e5aa1d
commit 58112e7152

View file

@ -6,8 +6,10 @@ package disk
import (
"errors"
"math/rand"
"sort"
"github.com/google/uuid"
osbuild "github.com/osbuild/osbuild-composer/internal/osbuild1"
"github.com/osbuild/osbuild-composer/internal/osbuild2"
)
@ -242,6 +244,31 @@ func (pt *PartitionTable) BootFilesystem() *Filesystem {
return pt.FindFilesystemForMountpoint("/boot")
}
// Generate all needed UUIDs for all the partiton and filesystems
//
// Will not overwrite existing UUIDs and only generate UUIDs for
// partitions if the layout is GPT.
func (pt *PartitionTable) GenerateUUIDs(rng *rand.Rand) {
_ = pt.ForEachFilesystem(func(fs *Filesystem) error {
if fs.UUID == "" {
fs.UUID = uuid.Must(newRandomUUIDFromReader(rng)).String()
}
return nil
})
// if this is a MBR partition table, there is no need to generate
// uuids for the partitions themselves
if pt.Type != "gpt" {
return
}
for idx, part := range pt.Partitions {
if part.UUID == "" {
pt.Partitions[idx].UUID = uuid.Must(newRandomUUIDFromReader(rng)).String()
}
}
}
// dynamically calculate and update the start point
// for each of the existing partitions
// return the updated start point