Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partition table helper functions for adding root filesystem and boot partition #1009

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const (

// DosFat16B used for the ESP-System partition
DosFat16B = "06"

// Partition type ID for any native Linux filesystem on dos
DosLinuxTypeID = "83"
)

// FSType is the filesystem type enum.
Expand Down
162 changes: 161 additions & 1 deletion pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ func (pt *PartitionTable) ensureBtrfs() error {
if pt.Type == "gpt" {
part.Type = FilesystemDataGUID
} else {
part.Type = "83"
part.Type = DosLinuxTypeID
}

} else {
Expand Down Expand Up @@ -894,3 +894,163 @@ func (pt *PartitionTable) GetMountpointSize(mountpoint string) (uint64, error) {

panic(fmt.Sprintf("no sizeable of the entity path for mountpoint %s, this is a programming error", mountpoint))
}

// EnsureRootFilesystem adds a root filesystem if the partition table doesn't
// already have one.
//
// When adding the root filesystem, add it to:
//
// - The first LVM Volume Group if one exists, otherwise
// - The first Btrfs volume if one exists, otherwise
// - At the end of the plain partitions.
//
// For LVM and Plain, the fsType argument must be a valid filesystem type.
func EnsureRootFilesystem(pt *PartitionTable, defaultFsType FSType) error {
// collect all labels and subvolume names to avoid conflicts
ondrejbudai marked this conversation as resolved.
Show resolved Hide resolved
subvolNames := make(map[string]bool)
labels := make(map[string]bool)
var foundRoot bool
_ = pt.ForEachMountable(func(mnt Mountable, path []Entity) error {
if mnt.GetMountpoint() == "/" {
foundRoot = true
return nil
}

labels[mnt.GetFSSpec().Label] = true
switch mountable := mnt.(type) {
case *BtrfsSubvolume:
subvolNames[mountable.Name] = true
}
return nil
})
if foundRoot {
// nothing to do
return nil
}

for _, part := range pt.Partitions {
switch payload := part.Payload.(type) {
case *LVMVolumeGroup:
if defaultFsType == FS_NONE {
return fmt.Errorf("error creating root logical volume: no default filesystem type")
}

rootLabel, err := genUniqueString("root", labels)
if err != nil {
return fmt.Errorf("error creating root logical volume: %w", err)
}
rootfs := &Filesystem{
Type: defaultFsType.String(),
Label: rootLabel,
Mountpoint: "/",
FSTabOptions: "defaults",
}
// Let the function autogenerate the name to avoid conflicts
// with LV names from customizations.
// Set the size to 0 and it will be adjusted by
// EnsureDirectorySizes() and relayout().
if _, err := payload.CreateLogicalVolume("", 0, rootfs); err != nil {
return fmt.Errorf("error creating root logical volume: %w", err)
}
return nil
case *Btrfs:
rootName, err := genUniqueString("root", subvolNames)
if err != nil {
return fmt.Errorf("error creating root subvolume: %w", err)
}
rootsubvol := BtrfsSubvolume{
Name: rootName,
Mountpoint: "/",
}
payload.Subvolumes = append(payload.Subvolumes, rootsubvol)
return nil
}
}

// We're going to create a root partition, so we have to ensure the default type is set.
if defaultFsType == FS_NONE {
return fmt.Errorf("error creating root partition: no default filesystem type")
}

// add a plain root partition at the end of the partition table
rootLabel, err := genUniqueString("root", labels)
if err != nil {
return fmt.Errorf("error creating root partition: %w", err)
}

var partType string
switch pt.Type {
case "dos":
partType = DosLinuxTypeID
case "gpt":
partType = FilesystemDataGUID
default:
return fmt.Errorf("error creating root partition: unknown or unsupported partition table type: %s", pt.Type)
}
rootpart := Partition{
Type: partType,
Size: 0, // Set the size to 0 and it will be adjusted by EnsureDirectorySizes() and relayout()
Payload: &Filesystem{
Type: defaultFsType.String(),
Label: rootLabel,
Mountpoint: "/",
FSTabOptions: "defaults",
},
}
pt.Partitions = append(pt.Partitions, rootpart)
return nil
}

// EnsureBootPartition creates a boot partition if one does not already exist.
// The function will append the boot partition to the end of the existing
// partition table therefore it is best to call this function early to put boot
// near the front (as is conventional).
func EnsureBootPartition(pt *PartitionTable, bootFsType FSType) error {
// collect all labels to avoid conflicts
labels := make(map[string]bool)
var foundBoot bool
_ = pt.ForEachMountable(func(mnt Mountable, path []Entity) error {
if mnt.GetMountpoint() == "/boot" {
foundBoot = true
return nil
}

labels[mnt.GetFSSpec().Label] = true
return nil
})
if foundBoot {
// nothing to do
return nil
}

if bootFsType == FS_NONE {
return fmt.Errorf("error creating boot partition: no filesystem type")
}

bootLabel, err := genUniqueString("boot", labels)
if err != nil {
return fmt.Errorf("error creating boot partition: %w", err)
}

var partType string
switch pt.Type {
case "dos":
partType = DosLinuxTypeID
case "gpt":
partType = XBootLDRPartitionGUID
default:
return fmt.Errorf("error creating boot partition: unknown or unsupported partition table type: %s", pt.Type)
}
bootPart := Partition{
Type: partType,
Size: 512 * datasizes.MiB,
Payload: &Filesystem{
Type: bootFsType.String(),
Label: bootLabel,
Mountpoint: "/boot",
FSTabOptions: "defaults",
},
}
pt.Partitions = append(pt.Partitions, bootPart)
return nil
}
Loading
Loading