Skip to content

Commit 82b980e

Browse files
jcpunkmhashizume
andcommitted
(FACT-3140) Add partition type GUID
This commit adds a new fact to the partitions fact hash, partition type GUID. Co-authored-by: Michael Hashizume <[email protected]>
1 parent d6d5bd9 commit 82b980e

File tree

6 files changed

+85
-23
lines changed

6 files changed

+85
-23
lines changed

lib/facter/resolvers/partitions.rb

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,46 @@ def execute_and_extract_blkid_info
122122
def populate_from_lsblk(partition_name, blkid_and_lsblk)
123123
return {} unless available?('lsblk', blkid_and_lsblk)
124124

125-
blkid_and_lsblk[:lsblk] ||= Facter::Core::Execution.execute('lsblk -fp', logger: log)
125+
lsblk_version_raw = Facter::Core::Execution.execute('lsblk --version 2>&1', logger: log)
126+
lsblk_version = lsblk_version_raw.match(/ \d\.\d+/)[0].to_f
126127

127-
part_info = blkid_and_lsblk[:lsblk].match(/#{partition_name}.*/).to_s.split(' ')
128-
return {} if part_info.empty?
128+
# The -p/--paths option was added in lsblk 2.23, return early and fall back to blkid with earlier versions
129+
return {} if lsblk_version < 2.23
129130

130-
parse_part_info(part_info)
131-
end
131+
blkid_and_lsblk[:lsblk] ||= execute_and_extract_lsblk_info(lsblk_version)
132132

133-
def parse_part_info(part_info)
134-
result = { filesystem: part_info[1] }
133+
partition_data = blkid_and_lsblk[:lsblk][partition_name]
134+
return {} unless partition_data
135135

136-
if part_info.count.eql?(5)
137-
result[:label] = part_info[2]
138-
result[:uuid] = part_info[3]
139-
else
140-
result[:uuid] = part_info[2]
141-
end
136+
filesys = partition_data['FSTYPE']
137+
uuid = partition_data['UUID']
138+
label = partition_data['LABEL']
139+
part_uuid = partition_data['PARTUUID']
140+
part_label = partition_data['PARTLABEL']
141+
part_type = partition_data['PARTTYPE']
142+
143+
result = { filesystem: filesys, uuid: uuid, label: label, partuuid: part_uuid, partlabel: part_label }
144+
result[:parttype] = part_type if part_type
142145

143146
result
144147
end
148+
149+
def execute_and_extract_lsblk_info(lsblk_version)
150+
# lsblk 2.25 added support for GPT partition type GUIDs
151+
stdout = if lsblk_version >= 2.25
152+
Facter::Core::Execution.execute('lsblk -p -P -o NAME,FSTYPE,UUID,LABEL,PARTUUID,PARTLABEL,PARTTYPE', logger: log)
153+
else
154+
Facter::Core::Execution.execute('lsblk -p -P -o NAME,FSTYPE,LABEL,UUID,PARTUUID,PARTLABEL', logger: log)
155+
end
156+
157+
output_hash = Hash[*stdout.split(/^(NAME=\S+)/)[1..-1]]
158+
output_hash.transform_keys! { |key| key.delete('NAME=')[1..-2] }
159+
output_hash.each do |key, value|
160+
output_hash[key] = Hash[*value.chomp.rstrip.split(/ ([^= ]+)=/)[1..-1].each { |x| x.delete!('"') }]
161+
end
162+
output_hash.each_value { |value_hash| value_hash.delete_if { |_k, v| v.empty? } }
163+
output_hash.delete_if { |_k, v| v.empty? }
164+
end
145165
end
146166
end
147167
end

lib/schema/facter.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,9 @@ partitions:
13351335
backing_file:
13361336
type: string
13371337
description: The path to the file backing the partition.
1338+
parttype:
1339+
type: string
1340+
description: The partition type GUID.
13381341

13391342
path:
13401343
type: string

spec/facter/resolvers/partitions_spec.rb

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
allow(Facter::Core::Execution).to receive(:which)
4343
.with('lsblk').and_return('/usr/bin/lsblk')
4444
allow(Facter::Core::Execution).to receive(:execute)
45-
.with('lsblk -fp', logger: an_instance_of(Facter::Log)).and_return(load_fixture('lsblk_output').read)
45+
.with('lsblk --version 2>&1', logger: an_instance_of(Facter::Log)).and_return('lsblk from util-linux 2.24')
46+
allow(Facter::Core::Execution).to receive(:execute)
47+
.with('lsblk -p -P -o NAME,FSTYPE,LABEL,UUID,PARTUUID,PARTLABEL', logger: an_instance_of(Facter::Log)).and_return(load_fixture('lsblk_output_old').read)
4648
end
4749

4850
context 'when block has a device subdir' do
@@ -68,12 +70,12 @@
6870
context 'when there is more than one partition' do
6971
it 'checks for blkid only once' do
7072
resolver.resolve(:partitions)
71-
expect(Facter::Core::Execution).to have_received(:which).with('blkid').once
73+
expect(Facter::Core::Execution).to have_received(:which).with('blkid').at_most(:once)
7274
end
7375

7476
it 'checks for lsblk only once' do
7577
resolver.resolve(:partitions)
76-
expect(Facter::Core::Execution).to have_received(:which).with('lsblk').once
78+
expect(Facter::Core::Execution).to have_received(:which).with('lsblk').at_most(:once)
7779
end
7880
end
7981

@@ -189,5 +191,43 @@
189191
end
190192
end
191193
end
194+
195+
context 'when lsblk can read partition types' do
196+
let(:partitions) do
197+
{ '/dev/sda1' => { filesystem: 'ext3', label: '/boot', parttype: '21686148-6449-6E6F-744E-656564454649', size: '117.00 KiB',
198+
size_bytes: 119_808, uuid: '88077904-4fd4-476f-9af2-0f7a806ca25e',
199+
partuuid: '00061fe0-01' },
200+
'/dev/sda2' => { filesystem: 'LVM2_member', parttype: '0fc63daf-8483-4772-8e79-3d69d8477de4', size: '98.25 MiB',
201+
size_bytes: 103_021_056, uuid: 'edi7s0-2WVa-ZBan' } }
202+
end
203+
204+
let(:sda_subdirs) do
205+
['/sys/block/sda/queue',
206+
'/sys/block/sda/sda2',
207+
'/sys/block/sda/sda2/stat',
208+
'/sys/block/sda/sda2/dev',
209+
'/sys/block/sda/sda2/uevent',
210+
'/sys/block/sda/sda1']
211+
end
212+
213+
before do
214+
allow(File).to receive(:directory?).with("#{sys_block_path}/sda/device").and_return(true)
215+
allow(Dir).to receive(:[]).with("#{sys_block_path}/sda/**/*").and_return(sda_subdirs)
216+
sda_subdirs.each { |subdir| allow(File).to receive(:directory?).with(subdir).and_return(true) }
217+
allow(Facter::Util::FileHelper).to receive(:safe_read)
218+
.with("#{sys_block_path}/sda/sda2/size", '0').and_return('201213')
219+
allow(Facter::Util::FileHelper).to receive(:safe_read)
220+
.with("#{sys_block_path}/sda/sda1/size", '0').and_return('234')
221+
end
222+
223+
it 'return partitions fact with part_type' do
224+
allow(Facter::Core::Execution).to receive(:execute)
225+
.with('lsblk --version 2>&1', logger: an_instance_of(Facter::Log)).and_return('lsblk from util-linux 2.25')
226+
allow(Facter::Core::Execution).to receive(:execute)
227+
.with('lsblk -p -P -o NAME,FSTYPE,UUID,LABEL,PARTUUID,PARTLABEL,PARTTYPE', logger: an_instance_of(Facter::Log)).and_return(load_fixture('lsblk_output_new').read)
228+
229+
expect(resolver.resolve(:partitions)).to eq(partitions)
230+
end
231+
end
192232
end
193233
end

spec/fixtures/lsblk_output

Lines changed: 0 additions & 7 deletions
This file was deleted.

spec/fixtures/lsblk_output_new

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NAME="/dev/sda" FSTYPE="" UUID="" LABEL="" PARTUUID="" PARTLABEL="" PARTTYPE=""
2+
NAME="/dev/sda1" FSTYPE="ext3" UUID="88077904-4fd4-476f-9af2-0f7a806ca25e" LABEL="/boot" PARTUUID="00061fe0-01" PARTLABEL="" PARTTYPE="21686148-6449-6E6F-744E-656564454649"
3+
NAME="/dev/sda2" FSTYPE="LVM2_member" UUID="edi7s0-2WVa-ZBan" LABEL="" PARTUUID="" PARTLABEL="" PARTTYPE="0fc63daf-8483-4772-8e79-3d69d8477de4"

spec/fixtures/lsblk_output_old

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NAME="/dev/sda" FSTYPE="" UUID="" LABEL="" PARTUUID="" PARTLABEL="" PARTTYPE=""
2+
NAME="/dev/sda1" FSTYPE="ext3" UUID="88077904-4fd4-476f-9af2-0f7a806ca25e" LABEL="/boot" PARTUUID="00061fe0-01" PARTLABEL=""
3+
NAME="/dev/sda2" FSTYPE="LVM2_member" UUID="edi7s0-2WVa-ZBan" LABEL="" PARTUUID="" PARTLABEL=""

0 commit comments

Comments
 (0)