From b00a1f2adb8dd4f2f20be2b70de8b85a38029435 Mon Sep 17 00:00:00 2001 From: Oana Tanasoiu Date: Tue, 8 Sep 2020 16:38:58 +0300 Subject: [PATCH] (FACT-2776) Fix Linux partitions fact --- .rubocop.yml | 1 + lib/facter/resolvers/partitions.rb | 38 +++++++++++++++++++++++- spec/facter/resolvers/partitions_spec.rb | 9 ++++-- spec/fixtures/lsblk_output | 7 +++++ 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/lsblk_output diff --git a/.rubocop.yml b/.rubocop.yml index 9e2ec1d159..ecadd73850 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -82,6 +82,7 @@ Metrics/CyclomaticComplexity: Metrics/ClassLength: Exclude: + - 'lib/facter/resolvers/partitions.rb' - 'lib/facter/custom_facts/util/fact.rb' - 'lib/facter/resolvers/windows/networking_resolver.rb' - 'lib/facter/custom_facts/util/collection.rb' diff --git a/lib/facter/resolvers/partitions.rb b/lib/facter/resolvers/partitions.rb index e1b902b463..e9b7541095 100644 --- a/lib/facter/resolvers/partitions.rb +++ b/lib/facter/resolvers/partitions.rb @@ -66,10 +66,18 @@ def populate_partitions(partition_name, block_path, backing_file = nil) info_hash = { size_bytes: size_bytes, size: Facter::FactsUtils::UnitConverter.bytes_to_human_readable(size_bytes), backing_file: backing_file } - info_hash.merge!(populate_from_blkid(partition_name)) + info_hash.merge!(populate_from_syscalls(partition_name)) @fact_list[:partitions][partition_name] = info_hash.reject { |_key, value| value.nil? } end + def populate_from_syscalls(partition_name) + part_info = populate_from_blkid(partition_name) + + return pupulate_from_lsblk(partition_name) if part_info.empty? + + part_info + end + def populate_from_blkid(partition_name) return {} unless blkid_command? @@ -92,6 +100,34 @@ def blkid_command? @blkid_exists = !output.empty? end + def pupulate_from_lsblk(partition_name) + return {} unless lsblk_command? + + @lsblk_content ||= Facter::Core::Execution.execute('lsblk -fp', logger: log) + + part_info = @lsblk_content.match(/#{partition_name}.*/).to_s.split(' ') + return {} if part_info.empty? + + result = { filesystem: part_info[1] } + + if part_info.count.eql?(5) + result[:label] = part_info[2] + result[:uuid] = part_info[3] + else + result[:uuid] = part_info[2] + end + + result + end + + def lsblk_command? + return @lsblk_exists unless @lsblk_exists.nil? + + output = Facter::Core::Execution.execute('which lsblk', logger: log) + + @lsblk_exists = !output.empty? + end + def execute_and_extract_blkid_info stdout = Facter::Core::Execution.execute('blkid', logger: log) output_hash = Hash[*stdout.split(/^([^:]+):/)[1..-1]] diff --git a/spec/facter/resolvers/partitions_spec.rb b/spec/facter/resolvers/partitions_spec.rb index 85959d092b..3ff1dc15ee 100644 --- a/spec/facter/resolvers/partitions_spec.rb +++ b/spec/facter/resolvers/partitions_spec.rb @@ -48,6 +48,10 @@ .and_return('/usr/bin/blkid') allow(Open3).to receive(:capture3).with({ 'LC_ALL' => 'C', 'LANG' => 'C' }, 'blkid') .and_return(load_fixture('blkid_output').read) + allow(Open3).to receive(:capture3).with({ 'LC_ALL' => 'C', 'LANG' => 'C' }, 'which lsblk') + .and_return('/usr/bin/lsblk') + allow(Open3).to receive(:capture3).with({ 'LC_ALL' => 'C', 'LANG' => 'C' }, 'lsblk -fp') + .and_return(load_fixture('lsblk_output').read) end context 'when device size files are readable' do @@ -55,7 +59,8 @@ { '/dev/sda1' => { filesystem: 'ext3', label: '/boot', size: '117.00 KiB', size_bytes: 119_808, uuid: '88077904-4fd4-476f-9af2-0f7a806ca25e', partuuid: '00061fe0-01' }, - '/dev/sda2' => { size: '98.25 MiB', size_bytes: 103_021_056 } } + '/dev/sda2' => { filesystem: 'LVM2_member', size: '98.25 MiB', size_bytes: 103_021_056, + uuid: 'edi7s0-2WVa-ZBan' } } end it 'return partitions fact' do @@ -67,7 +72,7 @@ let(:partitions_with_no_sizes) do { '/dev/sda1' => { filesystem: 'ext3', label: '/boot', size: '0 bytes', size_bytes: 0, uuid: '88077904-4fd4-476f-9af2-0f7a806ca25e', partuuid: '00061fe0-01' }, - '/dev/sda2' => { size: '0 bytes', size_bytes: 0 } } + '/dev/sda2' => { filesystem: 'LVM2_member', size: '0 bytes', size_bytes: 0, uuid: 'edi7s0-2WVa-ZBan' } } end it 'return partitions fact with 0 sizes' do diff --git a/spec/fixtures/lsblk_output b/spec/fixtures/lsblk_output new file mode 100644 index 0000000000..e8852caea6 --- /dev/null +++ b/spec/fixtures/lsblk_output @@ -0,0 +1,7 @@ +NAME FSTYPE LABEL UUID MOUNTPOINT +/dev/sda +|-/dev/sda1 xfs 5ba36204-050f-4ab6 /boot +`-/dev/sda2 LVM2_member edi7s0-2WVa-ZBan + |-/dev/mapper/rhel-root xfs cb455c09-da6b-44e6 / + `-/dev/mapper/rhel-swap swap 0fd5997d-eb82-493d [SWAP] +/dev/sr0 \ No newline at end of file