Skip to content

Commit

Permalink
Move Btrfs default_subvol calculation to yast2-storage
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Oct 28, 2016
1 parent 79d9f72 commit 95b1432
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 142 deletions.
30 changes: 28 additions & 2 deletions src/modules/AutoinstGeneral.rb
Expand Up @@ -200,6 +200,11 @@ def Export
Ops.set(general, "signature-handling", @signature_handling)
Ops.set(general, "ask-list", @askList)
Ops.set(general, "proposals", @proposals)

btrfs_set_default_subvol = btrfs_default_subvol_to_profile
unless btrfs_set_default_subvol.nil?
Ops.set(@storage, "btrfs_set_default_subvolume_name", btrfs_set_default_subvol)
end
Ops.set(general, "storage", @storage)

if Yast::Arch.s390
Expand Down Expand Up @@ -414,9 +419,14 @@ def SetMultipathing

# Set Btrfs default subvolume name
#
# @return ["@",""] Default subvolume name to use.
# Check "general/storage/btrfs_set_default_subvolume_name" in the profile.
# It does nothing if that element does not exist.
#
# @return ["@","",nil] Default subvolume name to use.
#
# @see FileSystems.default_subvol
def set_btrfs_default_subvolume_name
return unless Mode.autoinst && @storage.has_key?("btrfs_set_default_subvolume_name")
return unless @storage.has_key?("btrfs_set_default_subvolume_name")
value = @storage["btrfs_set_default_subvolume_name"] ? "@" : ""
log.info "Setting default subvolume to: '#{value}'"
FileSystems.default_subvol = value
Expand Down Expand Up @@ -501,6 +511,22 @@ def AutoinstGeneral
nil
end

protected

# Return the value to use as 'btrfs_set_default_subvolume_name' in the profile
#
# In case it matches the product's default, it should not be exported.
#
# @return [Boolean,nil] Value to use (true or false). If nil, no value
# should be exported.
def btrfs_default_subvol_to_profile
if FileSystems.default_subvol != FileSystems.default_subvol_from_product
return FileSystems.default_subvol == "" ? false : true
end
nil
end


publish :variable => :Confirm, :type => "boolean"
publish :variable => :second_stage, :type => "boolean"
publish :variable => :mode, :type => "map"
Expand Down
31 changes: 1 addition & 30 deletions src/modules/AutoinstPartPlan.rb
Expand Up @@ -347,6 +347,7 @@ def ReadHelper
Mode.SetMode("normal")
StorageDevices.InitDone
_StorageMap = Builtins.eval(Storage.GetTargetMap)
FileSystems.read_default_subvol_from_target

_StorageMap = _StorageMap.select do |d, p|
ok = true
Expand Down Expand Up @@ -771,36 +772,6 @@ def Read
)
end

# Try to find the default subvol for the installation
#
# * Root partition takes precedence
# * Not supported: more than 1 Btrfs filesystems, one using
# a '@' default subvolume and the other using ''. In that case,
# default_subvolume is set to product's default.
def find_btrfs_subvol_name_default
parts = Storage.GetTargetMap.map { |_k, d| d.fetch("partitions") }.flatten.compact
btrfs_parts = parts.select { |p| p["used_fs"] == :btrfs }
default_subvol_names = btrfs_parts.reduce({}) do |memo, part|
memo[part["mount"]] = btrfs_subvol_name_for(part)
memo
end

# Root takes precedence
return default_subvol_names["/"] if default_subvol_names.has_key?("/")

# If all has the same default subvolume name
found_names = default_subvol_names.values.uniq
return found_names.first if found_names.size == 1

# If there're different values, fallback to product's default
btrfs_subvol_name_from_product
end

def btrfs_subvol_name_for(partition)
ret = Yast::Execute.on_target("btrfs", "subvol", "list", partition["mount"], stdout: :capture)
ret.split("\n").first =~ /.+ @\z/ ? "@" : ""
end

# Dump the settings to a map, for autoinstallation use.
# @return [Array]
def Export
Expand Down
55 changes: 0 additions & 55 deletions test/AutoinstPartPlan_test.rb
Expand Up @@ -75,59 +75,4 @@
expect(snapshots).to be_empty
end
end

describe "#find_btrfs_subvol_name_default" do
let(:target_map) { YAML.load_file(File.join(FIXTURES_PATH, "storage", "subvolumes.yml")) }
let(:btrfs_list) { File.read(File.join(FIXTURES_PATH, "output", "btrfs_list.out")) }
let(:btrfs_list_no_at) { File.read(File.join(FIXTURES_PATH, "output", "btrfs_list_no_at.out")) }
let(:default_subvol) { "@" }

before do
allow(Yast::Storage).to receive(:GetTargetMap).and_return(target_map)
allow(Yast::ProductFeatures).to receive(:GetStringFeature)
.with("partitioning", "btrfs_default_subvolume").and_return(default_subvol)
end

context "when root partition uses the default subvolume name (@)" do
let(:btrfs_list) { File.read(File.join(FIXTURES_PATH, "output", "btrfs_list.out")) }

it "returns the default subvolume name" do
allow(Yast::Execute).to receive(:on_target).with("btrfs", "subvol", "list", "/", anything)
.and_return(btrfs_list)
allow(Yast::Execute).to receive(:on_target).with("btrfs", "subvol", "list", "/srv", anything)
.and_return(btrfs_list_no_at)
expect(subject.find_btrfs_subvol_name_default).to eq(default_subvol)
end
end

context "when root partitions does not use the default subvolume name (@)" do
before do
allow(Yast::Execute).to receive(:on_target).with("btrfs", "subvol", "list", "/", anything)
.and_return(btrfs_list_no_at)
end

context "but all partitions uses the same subvolume name" do
before do
allow(Yast::Execute).to receive(:on_target).with("btrfs", "subvol", "list", "/srv", anything)
.and_return(btrfs_list_no_at)
end

it "returns the used name ('' in this case)" do
expect(Yast::AutoinstPartPlan.find_btrfs_subvol_name_default).to eq("")
end
end

context "and partitions uses different subvolume names" do
before do
allow(Yast::Execute).to receive(:on_target).with("btrfs", "subvol", "list", "/srv", anything)
.and_return(btrfs_list)
end

it "returns the distribution default" do
value = Yast::AutoinstPartPlan.find_btrfs_subvol_name_default
expect(value).to eq("")
end
end
end
end
end
28 changes: 0 additions & 28 deletions test/fixtures/output/btrfs_list.out

This file was deleted.

27 changes: 0 additions & 27 deletions test/fixtures/output/btrfs_list_no_at.out

This file was deleted.

0 comments on commit 95b1432

Please sign in to comment.