Commit graph

130 commits

Author SHA1 Message Date
Simon de Vlieger
ca836a87f4 internal/disk: Remove RequiredSizes property.
The property was only used in a single method where it is passed as an
argument. There is no need to expose it.
2023-03-20 11:04:14 +01:00
Simon de Vlieger
34f7f6c7e2 disk: make testcase less confusing
This testcase tested for a / size of 1 GiB but better is to test for a
minimal size of 4 GiB which is the actual lower limit when the required
sizes are added together.
2023-03-20 11:04:14 +01:00
Simon de Vlieger
39879a9f60 disk: add RequiredSizes to ImageOptions
These RequiredSizes are a map that is passed on to the partition table
logic which had hardcoded defaults. This makes it possible to define
either no RequiredSizes (`nil`) or empty RequiredSizes which means no
further constraint checks or partition resizes will be done.
2023-03-20 11:04:14 +01:00
Simon de Vlieger
d7f5fac183 disk: clarify comment 2023-03-20 11:04:14 +01:00
Tomáš Hozza
26e6983320 disk: move remaining mountpoint policy code to different packages
Move the `CheckMountpoints()` implementation to `blueprint` package,
since it does not operate on any data structures from the `disk`.

Move the default mountpoint allow list policy definition to the
`pathpolicy` package.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2023-02-22 12:17:36 +01:00
Tomáš Hozza
3075d3a2ad disk: separate PathPolicies implementation to its own package
The `PathPolicies` implements a generic concept that can be fit on more
use cases than just mountpoints. Another one would be a policy for
creating custom files and directories in the image. Having the
implementation in the `disk` package and using data structures from the
`disk` and `blueprint` packages makes it impossible to use it for any
additional BP customization due to a circular dependencies that always
occurs.

Split out the implementation into a separate package `pathpolicy` as the
first step.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2023-02-22 12:17:36 +01:00
Ondřej Budai
b997142db0 common: merge all *ToPtr methods to one generic ToPtr
After introducing Go 1.18 to a project, it's required by law to convert at
least one method to a generic one.

Everyone hates IntToPtr, StringToPtr, BoolToPtr and Uint64ToPtr, so let's
convert them to the ultimate generic ToPtr one.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
2023-01-09 14:03:18 +01:00
Christian Kellner
9ea58d1486 disk: align LVM2 volumes to the extent size
When the size of a logical volume is not aligned to the extent size of
the volume group, LVM2 will automatically align it by rounding up[1]:
	Rounding up size to full physical extent 29.80 GiB
	Rounding up size to full physical extent <3.82 GiB

Since we don't take that into account when we create a new volume or
set the size of an existing one, the size for the whole volume group
will be short by that amount and thus the creation of the last volume
will fail:
  	Volume group <uuid> has insufficient free space (975 extents): 977 required.

To fix this a new `AlignUp` method is added to the `MountpointCreator`
creator interface. It will align a given size to the requirements of
the implementing container, like e.g. `LVMVolumeGroup`. It is then
used by a new `alignEntityBranch` which takes a size and walks the
entity path, calling `AlignUp` for all entities that implement said
`MountpointCreator` interface; thus the resulting size should fullfil
the alignment requirement for all elements in the path.
NB: `PartitionTable` already had an `AlignUp` method.

Add a corresponding test.

[1]: 8686657664/lib/metadata/metadata.c (L1072)

Co-authored-by: Achilleas Koutsou <achilleas@koutsou.net>
2022-11-18 18:44:12 +01:00
Christian Kellner
93875576e9 disk/lvm: split up CreateMountpoint
Extract a `CreateLogicalVolume` method from `CreateMountpoint`
and implement the latter via the former. This makes it possible
to create a Logical Volume for an existing payload.
2022-11-18 18:44:12 +01:00
Tomáš Hozza
63f1b8d9fb internal/disk: fix potential nil pointer dereference
The `LVMVolumeGroup.Clone()` method could end up dereferencing a `nil`
pointer in the `lv` variable, if there would be a `nil` logical volume
in the LVM volume group. Such situation would be an error of its own.
There is no point in checking if the cloned logical volume is not `nil`
and casting it to another variable. The logic should check if the cloned
logical volume is `nil` and panic in such situation. The following code
can then cast the clone to a different variable without issues and there
is no risk of dereferencing a `nil` pointer.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2022-11-07 16:10:41 +01:00
Tomáš Hozza
2735ea5b96 internal/disk: fix potential nil pointer dereference
The `PartitionTable.Clone()` method could end up dereferencing a `nil`
pointer in the `part` variable, if there would be a `nil` partition in
the partition table. Such situation would be an error of its own.
There is no point in checking if the cloned partition is not `nil` and
casting it to another variable. The logic should check if the cloned
partition is `nil` and panic in such situation. The following code can
then cast the clone to a different variable without issues and there is
no risk of dereferencing a `nil` pointer.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2022-11-07 16:10:41 +01:00
Achilleas Koutsou
c38c7ad126 disk: add packages for LUKS containers
Add the packages required for LUKS containers to the build root when
getting the list of packages from the PartitionTable.
2022-09-13 16:06:19 +01:00
Christian Kellner
dd0be9e439 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.
2022-08-08 18:05:05 +02:00
Christian Kellner
b562d144ca distro/*: allow /boot to be customized
Since the LVM support was added to all distros, our disk
related code is adaptive, i.e. we will set the correct BLS
and grub2 prefix if there a `boot` partiton is present in
the layout after all customizations happen, which includes
LVMification.
One thing that was not yet fully working was layouts that
do not yet have a `/boot` partition but allow LVMification.
In that case `NewPartitionTable` and if `/boot` was the
first (or only) customization, would LVMify the partition
which in turn would create the `/boot` partition; but after
`newPT.ensureLVM()` the call to `newPT.createFilesystem`
with `/boot` would try to create another `/boot` mountpoint.
In order to deal with this situation correctly we are now
using a two phase approach: 1) enlarge existing mountpoints
and collect new ones. 2) if there are new ones and LMVify
was allowed, switch to LVM layout. Do a second pass and now
create or enlarge existing partitions, handling `/boot` in
the process.
2022-08-08 18:05:05 +02:00
Christian Kellner
3c3f1b4969 disk: rename MountpointAllowList -> MountpointPolicies
Now that the mountpoint allow is is actually a trie of path
policies we should rename it to better refelct its contents.
2022-08-08 18:05:05 +02:00
Christian Kellner
de52e024a0 disk: use path policy for mount point checking
Replace the simple allow list of paths with the more sophisticated
path policies. It enables us to e.g. allow one path but not any
sub-path. This will be useful for `/boot` where we want to allow
its customization but not any sub-path because that might actually
break booting.
2022-08-08 18:05:05 +02:00
Christian Kellner
9523694879 disk: new path policies struct and methods
Build a new path policy struct, ased on the new path trie struct.
It is designed to be able to store policies for paths. A Check
method can then be used to look up the policy for a given path
based on the defined policies.
2022-08-08 18:05:05 +02:00
Christian Kellner
00555722b2 disk: add generic path trie implementation
Add a simple implementation of a path trie structure that can be
used to look up assoicated data for any given path. The constructor
will build the trie from a dict of paths to associated data. Later
modification is currently not support. Add tests for it creation
and lookup.
2022-08-08 18:05:05 +02:00
Christian Kellner
1ca2efe6cf distro/*: move mount point allow list to disk
All distributions have exactly the same list of allowed mountpoint
customization points. Therefore move it to `disk` and share it
between all distros.
2022-08-08 18:05:05 +02:00
Christian Kellner
2b03a839f0 disk: move mount point checking to central location
Instead of all distribution implementing their own mount point
checking code, we de-duplicate it and move it to `disk`.
2022-08-08 18:05:05 +02:00
Tom Gundersen
fed620b861 disk/partition_table: expose GetBuildPackages() function
This describes the packages necessary to create the partition table.

Use this in the pipelines and drop the explicit mentions from the build
pipeline.
2022-07-12 08:19:57 +01:00
Achilleas Koutsou
8be2c5c89a disk: test minimum directory size calculations
/ and /usr have minimum sizes defined (1 GiB and 2 GiB respectively).
When /usr is not defined, the minimum size of /usr gets added to the
minimum size for /.
This new test runs through a few scenarios and checks whether the sizes
fit.
2022-04-29 08:55:36 +02:00
Achilleas Koutsou
b8cd4bb839 disk: add an empty blueprint to the tests
Adjusted tests accordingly
2022-04-29 08:55:36 +02:00
Achilleas Koutsou
ae9960b8db disk: test that all mountpoints are the minimum 1 GiB
This tests that the clampFSSize() function ensures all user-defined
mountpoints are at least 1 GiB.
Added a blueprint with < 1 GiB minsizes to test this.
Testing all blueprints in TestCreatePartitionTable() now.
2022-04-29 08:55:36 +02:00
Achilleas Koutsou
61be84d634 disk: define blueprint map for test
Convenient for iterating and to define more cases.
2022-04-29 08:55:36 +02:00
Achilleas Koutsou
9920d798f5 disk: remove special case for /usr in clampFSSize
The `/usr` case is now handled by the `EnsureDirectorySizes()` function.
2022-04-29 08:55:36 +02:00
Achilleas Koutsou
275e60f199 disk: add default minimum directory sizes
Currently, we only specify a minimum size for
- `/` (1 GiB), and
- `/usr` (2 GiB).

This ensures that
- a separate `/usr` partition is at least 2 GiB,
- `/` is always at least 1 GiB,
- if `/usr` it not a separate partition, `/` is at least 3 GiB.

We could (or should), in the future, make it possible for image types to
override this mapping as part of their default config, for example, if
an image type by default requires a larger `/usr`.
2022-04-29 08:55:36 +02:00
Achilleas Koutsou
efa10e56e1 disk: use constants for size units in tests
Makes the test values more readable (without needing comments).
Some values in the default partition table were fixed, e.g., cases where
we had `Size: 1024000, // 500 MB`.
2022-04-29 08:55:36 +02:00
Christian Kellner
b365b18390 disk: fix ensureLVM for partition tables without /boot
When the partition table did not have a boot partition, we created it
but then _unconditionally_ returned, which meant that we did not create
the LVM skeleton and wrap the root partition. Properly handle this case
and also re-initialize the `rootPath` in this case since we change the
underlying `Partition[]` array in `PartitionTable` object. Add an extra
blueprint with only one customization which exposes this bug.

Co-Authored-By: Achilleas Koutsou <achilleas@koutsou.net>
2022-04-29 08:55:36 +02:00
Achilleas Koutsou
55a2e8ddac disk: add function for resizing Entities based on dirs
New function that ensures that a partition can hold the total sum of all
the required sizes of specific directories on the partition.  The
function sums the required directory sizes grouped by their mountpoint
and then resizes the entity path of that Mountable.
2022-04-27 13:49:19 +02:00
Achilleas Koutsou
ce5a28c113 disk: add function for finding entity path for a dir
New function that returns the Entity path for the Mountpoint that
contains a given directory path.
2022-04-27 13:49:19 +02:00
Achilleas Koutsou
75a0a74064 disk: add docstring for resizeEntityBranch()
resizeEntityBranch() is a private function, but it's not immediately
clear how it works so a docstring is useful for internal development.
2022-04-27 13:49:19 +02:00
Tomas Hozza
2ec55b4c53 RHEL-90: use XBOOTLDR partition GUID for /boot
Use the Extended Bootloader Partition GUID for `/boot`, instead of the
Linux filesystem data GUID. This is useful for autodetection of a
partition purpose based on its GUID without reading the `/etc/fstab`
first.

Ensure that when creating mountpoints, e.g. when converting the
partitions layout to LVM, the `/boot` partition get the proper GUID
assigned.

Regenerate RHEL-90 and centos-9 image test cases.

Related to https://bugzilla.redhat.com/show_bug.cgi?id=2057231
2022-03-24 15:40:10 +01:00
Tom Gundersen
973b5141b3 ditro/rhel86: set volid like in rhel9.0
This is only required in RHEL9.0, but best practice is to always pin these things
down. Also increases uniformity between distros.

Simplify a bit the volid generator by making it require `rand.Rand` rather than
`io.Reader`, and hence eliminating the need for error handling.
2022-02-28 23:05:45 +00:00
Antonio Murdaca
3be67ad965 Simplified installer: add support for encrypted raw images
Signed-off-by: Antonio Murdaca <runcom@linux.com>
2022-02-28 23:05:45 +00:00
Achilleas Koutsou
437dd397cd disk: test for duplicate Entities when cloning PartitionTables
Collect all entities and compare pointers.
2022-02-28 23:05:45 +00:00
Achilleas Koutsou
10095e382a disk: fix Payload cloning bugs
In LUKSContainer and LVMLogicalVolume we neglected to clone the Payload
which means we would modify the base PartitionTable when manipulating
the clone.
2022-02-28 23:05:45 +00:00
Christian Kellner
ddc0126a36 test: check we always have a boot on lvmification
Check that the `ensureLVM` code creates a boot partition, if it does
not yet exist.
2022-02-28 21:36:25 +01:00
Christian Kellner
63aa1556fc rhel86: automatically convert to LVM on fs customizations
Whenever we create a new mountpoint due to a user customization,
ensure the layout uses LVM, i.e. convert plain layouts to it, if
needed. It uses the existing lvm-ification code but enhances it
so that we also create a `/boot` partition in case it does not
yet exist.
Adjust the existing tests that assumed we can not create more
than 4 partitions on mbr layouts, since that is now not true
anymore.
2022-02-28 21:36:25 +01:00
Christian Kellner
ed4e0a94a4 disk: honour the fact that some containers have metadata
Re-introduce the VolumeContainer interface but with a different
meaning: it is supposed to be implemented by all container that
contain volumes and as a result have themselves a size, like eg
LVM2, LUKS2 and PartitionTable (the latter is not yet included).
The sole method on the interface for now is MetadataSize, which
should return the metadata for the container itself.
Use that new `VolumeContainer.MetadataSize` method when we up-
date the sizes of elements in `resizeEntitybranch`.
2022-02-28 17:09:30 +01:00
Christian Kellner
17fa96b84a disk: rename VolumeContainer → MountpointCreator
Rename the interface after the one method it has and the attribute
it describes for the implementing entities: being able to create
mountpoints.
2022-02-28 17:09:30 +01:00
Christian Kellner
7bfd0bb49f disk: rename Create{Volume → Mountpoint}
The function is indeed creating a mounpoint not a Volume; the
latter is not even well defined in our "ontology".
2022-02-28 17:09:30 +01:00
Christian Kellner
45f898c05c disk: remove unused CreateFilesystem method
The `CreateFilesystem` method on `PartitionTable` is not used
anymore since it got replaced by `CreateVolume`.
2022-02-28 17:09:30 +01:00
Christian Kellner
e57cccc3fe disk: NewPartitionTable can wrap plain partitions in LVM
Add a new parameter `lvmify` to `NewPartitionTable` that, if set to
`true`, will cause the root partition to be wrapped in LVM in case
it is not in a LVM volume group. Set this to `false` for now so no
actual change should happen anywhere. Layouts where the root is
directly on a LUKS container are not yet supported.
Add tests for this.
2022-02-28 17:09:30 +01:00
Christian Kellner
f91d8c6292 distro/rhel86: add support for azure marketplace
Add support for building images for the Azure marketplace: add a
new image type "azure-rhui" that can be used to build images
tailored to the Azure marketplace.
Add two sample manifests for 8.5 and 8.6, but note that even the
8.5 is using the 8.6 distro definitions. Also no image-info is
included since `image-info` cannot (yet) handle LVM setups and
the azure marketplace images use the LVM setup.
2022-02-27 22:03:36 +01:00
Christian Kellner
ca61baf03b disk: add partition type helpers
Add helper methods on `Partition` so that we can easily "detect" if
they are of the type BIOS-BOOT or PReP. Add the PReP GUID for GPT
as well.
2022-02-27 22:03:36 +01:00
Christian Kellner
1060885386 disk: extract GPT header calculation code
Extract the code that calculates the GPT header size into its own
method so that we can use it in other places as well.
2022-02-27 22:03:36 +01:00
Christian Kellner
a9ef16a95e disk/lvm: generate logical volume name
When creating a new logical volume via the `CreateVolume` method,
the logical volume name was left blank. Generate an name based
on the mountpoint.
We will detect collisions for names and will try to correct them
by attaching a suffix. We do give up after 100 attempts though.
Add a simple test for it.
2022-02-27 22:03:36 +01:00
Gianluca Zuccarelli
62c44e86f9 disk: ensure minimum size for filesystems
This commit fixes #2347 by ensuring that a minimum
size of 1GB is set for all file systems. The only
exception to this is the `/usr` which is set to 2GB,
since this was the only mountpoint that was previously
being checked.
2022-02-24 11:00:35 +01:00
Achilleas Koutsou
c347373c90 disk: basic partition table creation tests
Add a few test partition tables with well known configurations, like
LVM, LVM on top of LVM and Btrfs in addition to the plain layout that
is currently used in most partition tables. Add very basic checks,
such as that `NewPartitionTable` works and we have approximately the
correct size.

Co-Authored-By: Christian Kellner <christian@kellner.me>
2022-02-22 19:23:41 +00:00