diff --git a/src/lib/y2storage/free_disk_space.rb b/src/lib/y2storage/free_disk_space.rb index f268a127c..ebedb7665 100644 --- a/src/lib/y2storage/free_disk_space.rb +++ b/src/lib/y2storage/free_disk_space.rb @@ -145,6 +145,26 @@ def require_end_alignment? @require_end_alignment ||= disk.as_not_empty { disk.partition_table.require_end_alignment? } end + # Finds the remaining free space within the scope of the disk chunk defined by + # this (possibly outdated) FreeDiskSpace object + # + # @raise [NoDiskSpaceError] if there is no free space in the devicegraph at the region + # defined by the current FreeDiskSpace object + # + # @param devicegraph [Devicegraph] + # @return [FreeDiskSpace] free space within the area of the original FreeDiskSpace object + def updated_free_space(devicegraph) + disk = devicegraph.blk_devices.detect { |d| d.name == disk_name } + spaces = disk.as_not_empty { disk.free_spaces }.select do |space| + space.region.start >= region.start && + space.region.start < region.end + end + raise NoDiskSpaceError, "Exhausted free space" if spaces.empty? + + spaces.first + end + + # @return [String] def to_s "#" end diff --git a/src/lib/y2storage/planned/assigned_space.rb b/src/lib/y2storage/planned/assigned_space.rb index 2f6fe7ab0..9e291d3ff 100644 --- a/src/lib/y2storage/planned/assigned_space.rb +++ b/src/lib/y2storage/planned/assigned_space.rb @@ -208,6 +208,14 @@ def disk_size @disk_size ||= @disk_space.disk_size end + # Recalculates the information about the available space, in case it has been modified + def update_disk_space + @region = nil + @disk_size = nil + @space_start = nil + @disk_space = @disk_space.updated_free_space(devicegraph) + end + protected # Checks whether the disk space is inside an extended partition @@ -324,6 +332,11 @@ def enforced_last end end + # @return [Devicegraph] devicegraph in which the space is defined + def devicegraph + disk_space.disk.devicegraph + end + def partitions_sorted_by_attr(*attrs, nils_first: false, descending: false) partitions.each_with_index.sort do |one, other| compare(one, other, attrs, nils_first, descending) diff --git a/src/lib/y2storage/proposal/partition_creator.rb b/src/lib/y2storage/proposal/partition_creator.rb index fff009529..c967c7505 100644 --- a/src/lib/y2storage/proposal/partition_creator.rb +++ b/src/lib/y2storage/proposal/partition_creator.rb @@ -104,7 +104,7 @@ def create_planned_partitions(planned_partitions, initial_free_space, num_logica devices_map = {} planned_partitions.each_with_index do |part, idx| - space = free_space_within(initial_free_space) + space = initial_free_space.updated_free_space(devicegraph) primary = planned_partitions.size - idx > num_logical partition = create_partition(part, space, primary) part.format!(partition) @@ -117,22 +117,6 @@ def create_planned_partitions(planned_partitions, initial_free_space, num_logica devices_map end - # Finds the remaining free space within the scope of the disk chunk - # defined by a (probably outdated) FreeDiskSpace object - # - # @param initial_free_space [FreeDiskSpace] the original disk chunk, the - # returned free space will be within this area - def free_space_within(initial_free_space) - disk = devicegraph.blk_devices.detect { |d| d.name == initial_free_space.disk_name } - spaces = disk.as_not_empty { disk.free_spaces }.select do |space| - space.region.start >= initial_free_space.region.start && - space.region.start < initial_free_space.region.end - end - raise NoDiskSpaceError, "Exhausted free space" if spaces.empty? - - spaces.first - end - # Create a real partition for the specified planned partition within the # specified slot of free space. # @@ -150,7 +134,7 @@ def create_partition(planned_partition, free_space, primary) create_primary_partition(planned_partition, free_space) elsif !ptable.has_extended? create_extended_partition(free_space) - free_space = free_space_within(free_space) + free_space = free_space.updated_free_space(devicegraph) create_logical_partition(planned_partition, free_space) else create_logical_partition(planned_partition, free_space)