Skip to content

Commit

Permalink
Add support to export MD RAIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 1, 2018
1 parent ae96f9a commit a592c56
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 15 deletions.
24 changes: 23 additions & 1 deletion src/lib/y2storage/autoinst_profile/drive_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ def self.new_from_storage(device)
# <drive> section, like a disk, a DASD or an LVM volume group.
# @return [Boolean] if attributes were successfully read; false otherwise.
def init_from_device(device)
device.is_a?(LvmVg) ? init_from_vg(device) : init_from_disk(device)
case device
when LvmVg
init_from_vg(device)
when Md
init_from_md(device)
else
init_from_disk(device)
end
end

# Device name to be used for the real MD device
Expand Down Expand Up @@ -238,6 +245,21 @@ def init_from_vg(vg)
true
end

# Method used by {.new_from_storage} to populate the attributes when
# cloning a MD RAID.
#
# As usual, it keeps the behavior of the old clone functionality, check
# the implementation of this class for details.
#
# @param vg [Y2Storage::Md] RAID
def init_from_md(md)
@type = :CT_RAID
@device = md.name
@partitions = partitions_from_collection([md])
@enable_snapshots = enabled_snapshots?([md.filesystem])
true
end

def partitions_from_hash(hash)
return [] unless hash["partitions"]
hash["partitions"].map { |part| PartitionSection.new_from_hashes(part, self) }
Expand Down
23 changes: 16 additions & 7 deletions src/lib/y2storage/autoinst_profile/partition_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,7 @@ def init_from_device(device)
@create = true
@resize = false

if device.is_a?(LvmLv)
init_lv_fields(device)
else
init_partition_fields(device)
end

init_fields_by_type(device)
init_encryption_fields(device)
init_filesystem_fields(device)

Expand All @@ -247,7 +242,7 @@ def init_from_device(device)
# unit (that equals to 8225280 bytes in my experiments).
# According to the comments there, that was done due to bnc#415005 and
# bnc#262535.
@size = device.size.to_i.to_s if create
@size = device.size.to_i.to_s if create && !device.is?(:md)
end

def to_hashes
Expand Down Expand Up @@ -275,6 +270,16 @@ def partition_id_from(partition)
id.to_i_legacy
end

def init_fields_by_type(device)
if device.is?(:lvm_lv)
init_lv_fields(device)
elsif device.is?(:md)
init_md_fields(device)
else
init_partition_fields(device)
end
end

def init_partition_fields(partition)
@create = !NO_CREATE_IDS.include?(partition.id)
@partition_nr = partition.number
Expand All @@ -287,6 +292,10 @@ def init_lv_fields(lv)
@lv_name = lv.basename
end

def init_md_fields(md)
@raid_options = RaidOptionsSection.new_from_storage(md)
end

def init_encryption_fields(partition)
return unless partition.encrypted?

Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2storage/autoinst_profile/partitioning_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def self.new_from_hashes(drives_array)
def self.new_from_storage(devicegraph)
result = new
# TODO: consider also NFS and TMPFS
devices = devicegraph.lvm_vgs + devicegraph.disk_devices
devices = devicegraph.md_raids + devicegraph.lvm_vgs + devicegraph.disk_devices
result.drives = devices.each_with_object([]) do |dev, array|
drive = DriveSection.new_from_storage(dev)
array << drive if drive
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2storage/autoinst_profile/raid_options_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def self.new_from_storage(device)
def init_from_raid(md)
@raid_name = md.name
@raid_type = md.md_level.to_s
@chunk_size = md.chunk_size
@chunk_size = md.chunk_size.to_i.to_s
@parity_algorithm = md.md_parity.to_s
end
end
Expand Down
11 changes: 10 additions & 1 deletion test/y2storage/autoinst_profile/drive_section_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,17 @@ def lvm_vg(name)
end
end

context "given a MD RAID" do
# before { fake_scenario("md_raid.xml") }
before { fake_scenario("md2-devicegraph.xml") }

it "initializes #type to :CT_RAID" do
expect(described_class.new_from_storage(device("md0")).type).to eq :CT_RAID
end
end

context "given a volume group" do
it "initializes #type to :CT_LVM for LVM volume groups" do
it "initializes #type to :CT_LVM" do
expect(described_class.new_from_storage(lvm_vg("vg0")).type).to eq :CT_LVM
end

Expand Down
27 changes: 27 additions & 0 deletions test/y2storage/autoinst_profile/partition_section_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
require "y2storage"

describe Y2Storage::AutoinstProfile::PartitionSection do
using Y2Storage::Refinements::SizeCasts

subject(:section) { described_class.new }

before { fake_scenario("autoyast_drive_examples") }
Expand Down Expand Up @@ -73,6 +75,31 @@ def section_for(name)
end
end

context "given a RAIDs partition" do
let(:md) { instance_double(Y2Storage::Md, encrypted?: false, filesystem: filesystem) }
let(:raid_options) { instance_double(Y2Storage::AutoinstProfile::RaidOptionsSection) }
let(:filesystem) do
instance_double(
Y2Storage::Filesystems::Btrfs,
type: Y2Storage::Filesystems::Type::BTRFS,
label: "",
mkfs_options: "",
supports_btrfs_subvolumes?: false,
mount_point: nil
)
end

before do
allow(md).to receive(:is?) { |t| t == :md }
end

it "sets raid options" do
expect(Y2Storage::AutoinstProfile::RaidOptionsSection).to receive(:new_from_storage)
.with(md).and_return(raid_options)
expect(described_class.new_from_storage(md).raid_options).to eq(raid_options)
end
end

context "when filesystem is btrfs" do
it "initializes subvolumes" do
subvolumes = section_for("sdd3").subvolumes
Expand Down
12 changes: 9 additions & 3 deletions test/y2storage/autoinst_profile/partitioning_section_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
let(:disk_section) { instance_double(Y2Storage::AutoinstProfile::DriveSection) }
let(:dasd_section) { instance_double(Y2Storage::AutoinstProfile::DriveSection) }
let(:vg_section) { instance_double(Y2Storage::AutoinstProfile::DriveSection) }
let(:md_section) { instance_double(Y2Storage::AutoinstProfile::DriveSection) }
let(:partitioning) { [sda, sdb] }

describe ".new_from_hashes" do
Expand Down Expand Up @@ -62,12 +63,15 @@

describe ".new_from_storage" do
let(:devicegraph) do
instance_double(Y2Storage::Devicegraph, disk_devices: disks, lvm_vgs: [vg])
instance_double(
Y2Storage::Devicegraph, disk_devices: disks, lvm_vgs: [vg], md_raids: [md]
)
end
let(:disks) { [disk, dasd] }
let(:disk) { instance_double(Y2Storage::Disk) }
let(:dasd) { instance_double(Y2Storage::Dasd) }
let(:vg) { instance_double(Y2Storage::LvmVg) }
let(:md) { instance_double(Y2Storage::Md) }

before do
allow(Y2Storage::AutoinstProfile::DriveSection).to receive(:new_from_storage)
Expand All @@ -76,6 +80,8 @@
.with(dasd).and_return(dasd_section)
allow(Y2Storage::AutoinstProfile::DriveSection).to receive(:new_from_storage)
.with(vg).and_return(vg_section)
allow(Y2Storage::AutoinstProfile::DriveSection).to receive(:new_from_storage)
.with(md).and_return(md_section)
end

it "returns a new PartitioningSection object" do
Expand All @@ -84,14 +90,14 @@

it "creates an entry in #drives for every relevant VG, disk and DASD" do
section = described_class.new_from_storage(devicegraph)
expect(section.drives).to eq([vg_section, disk_section, dasd_section])
expect(section.drives).to eq([md_section, vg_section, disk_section, dasd_section])
end

it "ignores irrelevant drives" do
allow(Y2Storage::AutoinstProfile::DriveSection).to receive(:new_from_storage)
.with(disk).and_return(nil)
section = described_class.new_from_storage(devicegraph)
expect(section.drives).to eq([vg_section, dasd_section])
expect(section.drives).to eq([md_section, vg_section, dasd_section])
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
subject(:raid_options) { described_class.new_from_storage(md) }

it "initializes chunk_size" do
expect(raid_options.chunk_size).to eq(1.MB)
expect(raid_options.chunk_size).to eq("1000000")
end

it "initializes parity_algorithm" do
Expand Down

0 comments on commit a592c56

Please sign in to comment.