diff --git a/src/lib/storage/shadowed_vol_helper.rb b/src/lib/storage/shadowed_vol_helper.rb index cac921505..2c0612f76 100644 --- a/src/lib/storage/shadowed_vol_helper.rb +++ b/src/lib/storage/shadowed_vol_helper.rb @@ -134,9 +134,11 @@ def restore_aborted_subvols(target_part) name = subvol["name"] log.info "Restore subvol #{name}?" log.debug "subvol: #{subvol}" - existing_subvol = target_part["subvol"].detect { |s| s["name"] == name} + subvols = target_part["subvol"] || [] + existing_subvol = subvols.detect { |s| s["name"] == name} if existing_subvol.nil? log.info "Restoring subvol" + target_part["subvol"] ||= [] target_part["subvol"] << subvol elsif existing_subvol["delete"] log.info "Rejecting subvol deletion" diff --git a/test/lib/storage_shadowed_vol_helper_test.rb b/test/lib/storage_shadowed_vol_helper_test.rb index 762fedab8..07c546618 100644 --- a/test/lib/storage_shadowed_vol_helper_test.rb +++ b/test/lib/storage_shadowed_vol_helper_test.rb @@ -84,6 +84,29 @@ def update_partition(target_map, path, new_part, clean_deleted: false) subvol = find_subvol(second_root, "@/boot/grub2/i386-pc") expect(subvol).to be_nil end + + # See bsc#1008740 + context "if all the other subvolumes have been deleted" do + before do + # If all the subvolumes are deleted (not the same that 'marked for + # deletion'), the value for 'subvol' becomes nil (not empty) + root = find_partition(target_map, "/") + root.delete("subvol") + end + + it "does not raise an exception" do + expect { helper.root_partition(target_map: target_map) } + .to_not raise_error + end + + it "restores only the corresponding subvolumes" do + second_root = helper.root_partition(target_map: target_map) + subvolumes = second_root["subvol"].reject { |s| s["delete"] } + expect(subvolumes.map { |s| s["name"] }).to contain_exactly( + "@/boot/grub2/i386-pc", "@/boot/grub2/x86_64-efi" + ) + end + end end context "before committing the changes" do