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

Support swap partitions and logical volumes in DiskCustomization (COMPOSER-2400) #1072

Merged
merged 17 commits into from
Nov 28, 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
462 changes: 438 additions & 24 deletions internal/testdisk/partition.go

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion pkg/blueprint/disk_customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type PartitionCustomization struct {
// - Does not define a size. The size is defined by its container: a
// partition ([PartitionCustomization]) or LVM logical volume
// ([LVCustomization]).
//
// Setting the FSType to "swap" creates a swap area (and the Mountpoint must be
// empty).
type FilesystemTypedCustomization struct {
Mountpoint string `json:"mountpoint" toml:"mountpoint"`
Label string `json:"label,omitempty" toml:"label,omitempty"`
Expand Down Expand Up @@ -332,6 +335,7 @@ func (v *PartitionCustomization) UnmarshalTOML(data any) error {
// - Plain filesystem types are valid for the partition type
// - All non-empty properties are valid for the partition type (e.g.
// LogicalVolumes is empty when the type is "plain" or "btrfs")
// - Filesystems with FSType set to "swap" do not specify a mountpoint.
//
// Note that in *addition* consumers should also call
// ValidateLayoutConstraints() to validate that the policy for disk
Expand Down Expand Up @@ -450,6 +454,14 @@ var validPlainFSTypes = []string{
}

func (p *PartitionCustomization) validatePlain(mountpoints map[string]bool) error {
if p.FSType == "swap" {
// make sure the mountpoint is empty and return
if p.Mountpoint != "" {
return fmt.Errorf("mountpoint for swap partition must be empty (got %q)", p.Mountpoint)
}
return nil
}

if err := validateMountpoint(p.Mountpoint); err != nil {
return err
}
Expand Down Expand Up @@ -490,6 +502,13 @@ func (p *PartitionCustomization) validateLVM(mountpoints, vgnames map[string]boo
}
lvnames[lv.Name] = true

if lv.FSType == "swap" {
// make sure the mountpoint is empty and return
if lv.Mountpoint != "" {
return fmt.Errorf("mountpoint for swap logical volume with name %q in volume group %q must be empty", lv.Name, p.Name)
}
return nil
}
if err := validateMountpoint(lv.Mountpoint); err != nil {
return fmt.Errorf("invalid logical volume customization: %w", err)
}
Expand Down Expand Up @@ -560,7 +579,9 @@ func CheckDiskMountpointsPolicy(partitioning *DiskCustomization, mountpointAllow
mountpoints = append(mountpoints, part.Mountpoint)
}
for _, lv := range part.LogicalVolumes {
mountpoints = append(mountpoints, lv.Mountpoint)
if lv.Mountpoint != "" {
mountpoints = append(mountpoints, lv.Mountpoint)
}
}
for _, subvol := range part.Subvolumes {
mountpoints = append(mountpoints, subvol.Mountpoint)
Expand Down
81 changes: 80 additions & 1 deletion pkg/blueprint/disk_customizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestPartitioningValidation(t *testing.T) {
},
expectedMsg: "",
},
"happy-plain+btrfs": {
"happy-plain+btrfs+swap": {
partitioning: &blueprint.DiskCustomization{
Partitions: []blueprint.PartitionCustomization{
{
Expand All @@ -46,6 +46,11 @@ func TestPartitioningValidation(t *testing.T) {
Mountpoint: "/data",
},
},
{
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
FSType: "swap",
},
},
{
Type: "btrfs",
BtrfsVolumeCustomization: blueprint.BtrfsVolumeCustomization{
Expand Down Expand Up @@ -89,6 +94,39 @@ func TestPartitioningValidation(t *testing.T) {
},
expectedMsg: "",
},
"happy-plain+lvm-with-swap": {
partitioning: &blueprint.DiskCustomization{
Partitions: []blueprint.PartitionCustomization{
{
Type: "plain",
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
FSType: "xfs",
Mountpoint: "/data",
},
},
{
Type: "lvm",
VGCustomization: blueprint.VGCustomization{
Name: "root",
LogicalVolumes: []blueprint.LVCustomization{
{
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
FSType: "ext4",
Mountpoint: "/",
},
},
{
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
FSType: "swap",
},
},
},
},
},
},
},
expectedMsg: "",
},
"happy-plain-with-boot-and-efi": {
partitioning: &blueprint.DiskCustomization{
Partitions: []blueprint.PartitionCustomization{
Expand Down Expand Up @@ -825,6 +863,47 @@ func TestPartitioningValidation(t *testing.T) {
},
expectedMsg: "invalid partitioning customizations:\nunknown partition type: what",
},
"unhappy-swap-with-mountpoint": {
partitioning: &blueprint.DiskCustomization{
Partitions: []blueprint.PartitionCustomization{
{
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
FSType: "ext4",
Mountpoint: "/home",
},
},
{
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
FSType: "swap",
Mountpoint: "/swap",
},
},
},
},
expectedMsg: "invalid partitioning customizations:\nmountpoint for swap partition must be empty (got \"/swap\")",
},
"unhappy-swaplv-with-mountpoint": {
partitioning: &blueprint.DiskCustomization{
Partitions: []blueprint.PartitionCustomization{
{
Type: "lvm",
VGCustomization: blueprint.VGCustomization{
Name: "badvg",
LogicalVolumes: []blueprint.LVCustomization{
{
Name: "swappylv",
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
FSType: "swap",
Mountpoint: "/var/swap",
},
},
},
},
},
},
},
expectedMsg: "invalid partitioning customizations:\nmountpoint for swap logical volume with name \"swappylv\" in volume group \"badvg\" must be empty",
},
}

for name := range testCases {
Expand Down
4 changes: 4 additions & 0 deletions pkg/disk/btrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func (bs *BtrfsSubvolume) GetMountpoint() string {
return bs.Mountpoint
}

func (bs *BtrfsSubvolume) GetFSFile() string {
mvo5 marked this conversation as resolved.
Show resolved Hide resolved
return bs.GetMountpoint()
}

func (bs *BtrfsSubvolume) GetFSType() string {
return "btrfs"
}
Expand Down
1 change: 1 addition & 0 deletions pkg/disk/btrfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ func TestImplementsInterfacesCompileTimeCheckBtrfs(t *testing.T) {
var _ = UniqueEntity(&Btrfs{})
var _ = Mountable(&BtrfsSubvolume{})
var _ = Sizeable(&BtrfsSubvolume{})
var _ = FSTabEntity(&BtrfsSubvolume{})
}
23 changes: 19 additions & 4 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const (

RootPartitionUUID = "6264D520-3FB9-423F-8AB8-7A0A8E3D3562"

SwapPartitionGUID = "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F"

// Extended Boot Loader Partition
XBootLDRPartitionGUID = "BC13C2FF-59E6-4262-A352-B275FD6F7172"

Expand All @@ -70,6 +72,9 @@ const (

// Partition type ID for ESP on dos
DosESPID = "ef00"

// Partition type ID for swap
DosSwapID = "82"
)

// pt type -> type -> ID mapping for convenience
Expand All @@ -80,13 +85,15 @@ var idMap = map[PartitionTableType]map[string]string{
"data": DosLinuxTypeID,
"esp": DosESPID,
"lvm": DosLinuxTypeID,
"swap": DosSwapID,
},
PT_GPT: {
"bios": BIOSBootPartitionGUID,
"boot": XBootLDRPartitionGUID,
"data": FilesystemDataGUID,
"esp": EFISystemPartitionGUID,
"lvm": LVMPartitionGUID,
"swap": SwapPartitionGUID,
},
}

Expand Down Expand Up @@ -248,13 +255,21 @@ type Mountable interface {
// GetMountPoint returns the path of the mount point.
GetMountpoint() string

// GetFSType returns the file system type, e.g. 'xfs'.
GetFSType() string
FSTabEntity
}

// GetFSSpec returns the file system spec information.
// FSTabEntity describes any entity that can appear in the fstab file.
type FSTabEntity interface {
// FSSpec for the entity (UUID and Label); the first field of fstab(5).
GetFSSpec() FSSpec

// GetFSTabOptions returns options for mounting the entity.
// The mount point (target) for a filesystem or "none" for swap areas; the second field of fstab(5).
GetFSFile() string

// The type of the filesystem or swap for swap areas; the third field of fstab(5).
GetFSType() string

// The mount options, freq, and passno for the entity; the fourth fifth, and sixth fields of fstab(5) respectively.
GetFSTabOptions() (FSTabOptions, error)
}

Expand Down
Loading
Loading