Skip to content

Commit

Permalink
Merge pull request #2076 from oanatmaria/FACT-2776
Browse files Browse the repository at this point in the history
(FACT-2776) Fix Linux partitions fact
  • Loading branch information
sebastian-miclea authored Sep 9, 2020
2 parents 2a683ab + b00a1f2 commit ca9a6e1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
38 changes: 37 additions & 1 deletion lib/facter/resolvers/partitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -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]]
Expand Down
9 changes: 7 additions & 2 deletions spec/facter/resolvers/partitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@
.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
let(:partitions) do
{ '/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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/lsblk_output
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ca9a6e1

Please sign in to comment.