Skip to content

Commit

Permalink
Support specifying btrfs volume attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Nov 3, 2016
1 parent 0f7b36c commit d5ed0a6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
39 changes: 32 additions & 7 deletions src/include/autoinstall/autopart.rb
Expand Up @@ -35,13 +35,8 @@ def AddSubvolData(st_map, xml_map)
Ops.set(
ret,
"subvol",
Builtins.maplist(Ops.get_list(xml_map, "subvolumes", [])) do |s|
if !Builtins.isempty(sv_prep) &&
Builtins.substring(s, 0, Builtins.size(sv_prep)) != sv_prep
s = Ops.add(sv_prep, s)
end
{ "name" => s, "create" => true }
end
# Convert from "vol" or {"name" => "vol", "options" => "nocow" } to { "name" => "x", "nocow" => true}
xml_map.fetch("subvolumes", []).map { |s| import_subvolume(s, sv_prep) }
)
end
if Builtins.haskey(ret, "subvolumes")
Expand All @@ -52,6 +47,36 @@ def AddSubvolData(st_map, xml_map)
deep_copy(ret)
end

# Build a subvolume definition
#
# This method support two kind of subvolume specification:
#
# * just a name
# * or a hash containing a "name" and an optional "options" keys
#
# @param spec_or_name [Hash,String] Subvolume specification
# @param prefix [String] Subvolume prefix (usually default subvolume + '/')
# @return [Hash] Internal representation of a subvolume
def import_subvolume(spec_or_name, prefix = "")
log.info "spec_or_name: #{spec_or_name.inspect}"
# Support strings or hashes
spec = spec_or_name.is_a?(::String) ? { "name" => spec_or_name } : spec_or_name

# Base information
subvolume = {
"name" => spec["name"],
"create" => true
}
subvolume["name"].prepend(prefix) unless spec["name"].start_with?(prefix)

# Append options
options = spec.fetch("options", "").split(",").map(&:strip).map do |option|
key, value = option.split("=").map(&:strip)
[key, value.nil? ? true : value]
end
subvolume.merge(Hash[options])
end

def AddFilesysData(st_map, xml_map)
st_map = deep_copy(st_map)
xml_map = deep_copy(xml_map)
Expand Down
24 changes: 20 additions & 4 deletions test/AutoinstPartPlan_test.rb
Expand Up @@ -7,9 +7,8 @@
Yast.import "Profile"

describe Yast::AutoinstPartPlan do
FIXTURES_PATH = File.join(File.dirname(__FILE__), 'fixtures')
let(:target_map_path) { File.join(FIXTURES_PATH, 'storage', "nfs_root.yml") }
let(:target_map_clone) { File.join(FIXTURES_PATH, 'storage', "target_clone.yml") }
let(:target_map_path) { File.join(FIXTURES_PATH, "storage", "nfs_root.yml") }
let(:target_map_clone) { File.join(FIXTURES_PATH, "storage", "target_clone.yml") }

describe "#read partition target" do

Expand Down Expand Up @@ -44,7 +43,24 @@
]
)
end

end

describe "#Export" do
let(:target_map) { YAML.load_file(target_map_clone) }
let(:default_subvol) { "@" }
let(:target_map_path) { File.join(FIXTURES_PATH, 'storage', "subvolumes.yml") }

before do
expect(Yast::Storage).to receive(:GetTargetMap).and_return(target_map)
stub_const("Yast::FileSystems", double("filesystems", default_subvol: default_subvol))
Yast::AutoinstPartPlan.Read
end

it "does not include snapshots" do
byebug
exported = Yast::AutoinstPartPlan.Export
expect(Yast::AutoinstPartPlan.Export).to eq([])
end

end
end
16 changes: 16 additions & 0 deletions test/include/autopart_test.rb
Expand Up @@ -10,6 +10,8 @@

module DummyYast
class AutoinstallAutopartClient < Yast::Client
include Yast::Logger

def main
Yast.include self, "autoinstall/autopart.rb"
@planHasBoot=false
Expand Down Expand Up @@ -140,6 +142,20 @@ def plan_has_boot_or_prep_partition=(v)
expect(new_target["subvol"]).to eq([{"name" => "@/home", "create" => true}])
end
end

context "when subvolumes specification contains options" do
let(:subvolumes) do
[ { "name" => "home" }, { "name" => "var/lib/pgsql", "options" => "nocow,opt1=val1"} ]
end

it "adds those options" do
new_target = client.AddSubvolData(target, "subvolumes" => subvolumes)
expect(new_target["subvol"]).to eq([
{ "name" => "home", "create" => true },
{ "name" => "var/lib/pgsql", "create" => true, "nocow" => true, "opt1" => "val1" }
])
end
end
end
end
end

0 comments on commit d5ed0a6

Please sign in to comment.