Skip to content

Commit

Permalink
Method to refresh the information of an AssignedSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorgs committed Sep 19, 2024
1 parent 75ca2b4 commit 86b0940
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
20 changes: 20 additions & 0 deletions src/lib/y2storage/free_disk_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
"#<FreeDiskSpace disk_name=#{disk_name}, size=#{disk_size}, start_offset=#{start_offset}>"
end
Expand Down
13 changes: 13 additions & 0 deletions src/lib/y2storage/planned/assigned_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 2 additions & 18 deletions src/lib/y2storage/proposal/partition_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
#
Expand All @@ -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)
Expand Down

0 comments on commit 86b0940

Please sign in to comment.