Skip to content

Commit c791f0d

Browse files
committed
BUGFIX: Correctly identify if a block device is a disk or partition on sles11
Previous code assumed the output of hwinfo would be more consistent, but this does not appear to be the case and could cause a stacktrace that led to storage not being initialized.
1 parent ad312a5 commit c791f0d

1 file changed

Lines changed: 42 additions & 17 deletions

File tree

common/src/stack/storage-config/lib/stacki_storage.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def getHostDisks(nukedisks):
6767
# 'nuke' : 0
6868
# }]
6969
#
70-
lsblk = ['lsblk', '-lio', 'NAME,RM,RO,TYPE']
70+
lsblk = ['lsblk', '--noheadings', '-lio', 'NAME,RM,RO,TYPE']
7171
# sles 11 doesn't support the TYPE column
7272
if attributes['os.version'] == "11.x" and attributes['os'] == "sles":
73-
lsblk = ['lsblk', '-lio', 'NAME,RM,RO']
73+
lsblk = ['lsblk', '--noheadings', '-lio', 'NAME,RM,RO']
7474
p = subprocess.run(lsblk,
7575
stdin=subprocess.PIPE,
7676
stdout=subprocess.PIPE,
@@ -289,22 +289,47 @@ def getHostFstab(devices):
289289
def get_sles11_media_type(dev_name):
290290
"""Determines disk and partition for SLES 11.
291291
Because SLES 11 doesn't support TYPE field on lsblk commands
292+
293+
`dev_name` is expected to be just the block device, without leading /dev/
294+
return value should only ever be 'loop', 'part', or 'disk'
292295
"""
293-
p = subprocess.run(["hwinfo", "--block", "--short"],
294-
stdin=subprocess.PIPE,
296+
p = subprocess.run(f"hwinfo --block --short --only /dev/{dev_name}",
297+
shell=True,
298+
encoding='utf-8',
295299
stdout=subprocess.PIPE,
296300
stderr=subprocess.PIPE)
297-
for l in p.stdout.decode().split('\n'):
298-
# Ignore empty lines or non-path
299-
if not l.strip() or "/" not in l.strip():
300-
continue
301-
arr = l.split()
302-
if dev_name == os.path.split(arr[0])[1]:
303-
if str(arr[1]).lower() == "disk":
304-
return "disk"
305-
if str(arr[1]).lower() == "partition":
306-
return "part"
307-
return arr[1]
301+
302+
# hwinfo --only against a block device will return either empty string OR something like:
303+
# partition:
304+
# /dev/sda1 Partition
305+
# OR
306+
# disk:
307+
# /dev/sda Some crazy string
308+
# 'Some crazy string' is maybe controller model, or the iscsi target type, or maybe just 'disk'
309+
# anyway, just steal line 0 for the type
310+
# other possible line 0 values can be 'cdrom', 'unknown', depending on the state of system
311+
308312
# If we can't find what we are looking for, lie and say its "loop"
309-
# I think that is the only other option if it is not a disk or parition.
310-
return "loop"
313+
blk_type = 'loop'
314+
if p.stdout == '':
315+
return blk_type
316+
317+
# we could probably do without a loop here, but I don't fully trust the output of hwinfo
318+
for l in p.stdout.splitlines():
319+
# there should be just two lines...
320+
arr = l.strip().split()
321+
322+
# line 0
323+
if len(arr) == 1 and arr[0] == 'partition':
324+
blk_type = 'part'
325+
continue
326+
327+
# line 0
328+
if len(arr) == 1 and arr[0] == 'disk':
329+
blk_type = 'disk'
330+
continue
331+
332+
# line 1
333+
if f'/dev/{dev_name}' == arr[0]:
334+
return blk_type
335+

0 commit comments

Comments
 (0)