Skip to content

Commit

Permalink
Merge pull request #753 from yast/backport/component_of
Browse files Browse the repository at this point in the history
Backport BlkDevice#component_of
  • Loading branch information
imobachgs committed Oct 1, 2018
2 parents acfb97d + 53ab7fc commit a3a5217
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib/y2storage/blk_device.rb
Expand Up @@ -361,13 +361,44 @@ def direct_md
descendants.detect { |dev| dev.is?(:md) && dev.devices.include?(self) }
end

# DM arrays defined on top of the device
#
# @return [Array<DmRaid>] empty if the device is not used by any DM RAID
# device
def dm_raids
children.select { |dev| dev.is?(:dm_raid) }
end

# Multipath device defined on top of the device
#
# @return [Multipath, nil] nil if the device is not part of any multipath
def multipath
children.find { |dev| dev.is?(:multipath) }
end

# Whether the device forms part of an LVM or MD RAID
#
# @return [Boolean]
def part_of_lvm_or_md?
!lvm_pv.nil? || !md.nil?
end

# Devices built on top of this device, to be used mainly by the Partitioner
# to display which devices are using this one as its base.
#
# This does not include all the descendants, but only those multi-device
# devices for which this is one of the components. For example, it will
# include any LVM VG for which this device is one of its physical volumes
# (directly or through an encryption) or any RAID having this device as
# one of its members.
#
# @return [Array<Device>] a collection of MD RAIDs, DM RAIDs, volume groups,
# and multipath devices
def component_of
vg = lvm_pv ? lvm_pv.lvm_vg : nil
(dm_raids + [vg] + [md] + [multipath]).compact
end

# Label of the filesystem, if any
# @return [String, nil]
def filesystem_label
Expand Down
97 changes: 97 additions & 0 deletions test/y2storage/blk_device_test.rb
Expand Up @@ -678,6 +678,103 @@
end
end

describe "#component_of" do
context "for a device not used in an LVM or in a RAID or in multipath" do
let(:scenario) { "mixed_disks" }
let(:device_name) { "/dev/sda1" }

it "returns an empty array" do
expect(device.component_of).to eq []
end
end

context "for a device that is part of several DM RAIDs" do
let(:scenario) { "empty-dm_raids.xml" }
let(:device_name) { "/dev/sdb" }

it "returns an array with all the corresponding DM RAIDs" do
expect(device.component_of.size).to eq 2
expect(device.component_of).to all(be_a(Y2Storage::DmRaid))
expect(device.component_of.map(&:name)).to contain_exactly(
"/dev/mapper/isw_ddgdcbibhd_test1", "/dev/mapper/isw_ddgdcbibhd_test2"
)
end
end

context "for a device directly used in an LVM" do
let(:scenario) { "complex-lvm-encrypt" }
let(:device_name) { "/dev/sde2" }

it "returns an array with the LVM VG" do
expect(device.component_of.size).to eq 1
expect(device.component_of.first).to be_a Y2Storage::LvmVg
expect(device.component_of.first.name).to eq "/dev/vg1"
end
end

context "for an encrypted device directly used in an LVM" do
let(:scenario) { "complex-lvm-encrypt" }
let(:device_name) { "/dev/sde1" }

it "returns an array with the LVM VG" do
expect(device.component_of.size).to eq 1
expect(device.component_of.first).to be_a Y2Storage::LvmVg
expect(device.component_of.first.name).to eq "/dev/vg0"
end
end

context "for a disk used indirectly (through its partitions) in an LVM" do
let(:scenario) { "complex-lvm-encrypt" }
let(:device_name) { "/dev/sde" }

it "returns an empty array" do
expect(device.component_of).to eq []
end
end

context "for a device directly used in an MD RAID" do
let(:scenario) { "subvolumes-and-empty-md.xml" }
let(:device_name) { "/dev/sda4" }

it "returns an array with the MD RAID" do
expect(device.component_of.size).to eq 1
expect(device.component_of.first).to be_a Y2Storage::Md
expect(device.component_of.first.name).to eq "/dev/md/strip0"
end
end

context "for an encrypted device directly used in an MD RAID" do
let(:scenario) { "subvolumes-and-empty-md.xml" }
let(:device_name) { "/dev/sda5" }

it "returns an array with the MD RAID" do
expect(device.component_of.size).to eq 1
expect(device.component_of.first).to be_a Y2Storage::Md
expect(device.component_of.first.name).to eq "/dev/md/strip0"
end
end

context "for a disk used indirectly (through its partitions) in an MD RAID" do
let(:scenario) { "subvolumes-and-empty-md.xml" }
let(:device_name) { "/dev/sda" }

it "returns an empty array" do
expect(device.component_of).to eq []
end
end

context "for a disk that is part of a multipath setup" do
let(:scenario) { "multipath-formatted.xml" }
let(:device_name) { "/dev/sda" }

it "returns an array with the multipath device" do
expect(device.component_of.size).to eq 1
expect(device.component_of.first).to be_a Y2Storage::Multipath
expect(device.component_of.first.name).to eq "/dev/mapper/0QEMU_QEMU_HARDDISK_mpath1"
end
end
end

describe "#hwinfo" do
let(:device_name) { "/dev/sda" }

Expand Down

0 comments on commit a3a5217

Please sign in to comment.