Skip to content

Commit

Permalink
StorageScanner: Add parent device name to lsblk
Browse files Browse the repository at this point in the history
Modify the StorageInfo model to include path and name of the parent
device. Use StorageScanner to collect this information.

Morover fix lsblk test, there should be a full device path in "lsblk
-pbnr" output (just names were used in the original test).
  • Loading branch information
danzatt authored and Your Name committed Aug 6, 2024
1 parent 79abdf9 commit 72fa433
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


def test_actor_with_luks(current_actor_context):
with_luks = [LsblkEntry(name='luks-132', kname='kname1', maj_min='253:0', rm='0',
size='10G', bsize=10*(1 << 39), ro='0', tp='crypt', mountpoint='')]
with_luks = [LsblkEntry(name='luks-132', kname='kname1', maj_min='253:0', rm='0', size='10G', bsize=10*(1 << 39),
ro='0', tp='crypt', mountpoint='', parent_name='', parent_path='')]

current_actor_context.feed(StorageInfo(lsblk=with_luks))
current_actor_context.run()
Expand All @@ -16,8 +16,8 @@ def test_actor_with_luks(current_actor_context):


def test_actor_with_luks_ceph_only(current_actor_context):
with_luks = [LsblkEntry(name='luks-132', kname='kname1', maj_min='253:0', rm='0',
size='10G', bsize=10*(1 << 39), ro='0', tp='crypt', mountpoint='')]
with_luks = [LsblkEntry(name='luks-132', kname='kname1', maj_min='253:0', rm='0', size='10G', bsize=10*(1 << 39),
ro='0', tp='crypt', mountpoint='', parent_name='', parent_path='')]
ceph_volume = ['luks-132']
current_actor_context.feed(StorageInfo(lsblk=with_luks))
current_actor_context.feed(CephInfo(encrypted_volumes=ceph_volume))
Expand All @@ -26,8 +26,8 @@ def test_actor_with_luks_ceph_only(current_actor_context):


def test_actor_without_luks(current_actor_context):
without_luks = [LsblkEntry(name='sda1', kname='sda1', maj_min='8:0', rm='0',
size='10G', bsize=10*(1 << 39), ro='0', tp='part', mountpoint='/boot')]
without_luks = [LsblkEntry(name='sda1', kname='sda1', maj_min='8:0', rm='0', size='10G', bsize=10*(1 << 39),
ro='0', tp='part', mountpoint='/boot', parent_name='', parent_path='')]

current_actor_context.feed(StorageInfo(lsblk=without_luks))
current_actor_context.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,31 @@ def _get_mount_info(path):
)


def _get_lsblk_info_for_devpath(dev_path):
lsblk_cmd = ['lsblk', '-nr', '--output', 'NAME,KNAME,SIZE', dev_path]
lsblk_info_for_devpath = next(_get_cmd_output(lsblk_cmd, ' ', 3), None)

return lsblk_info_for_devpath


@aslist
def _get_lsblk_info():
""" Collect storage info from lsblk command """
cmd = ['lsblk', '-pbnr', '--output', 'NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT']
for entry in _get_cmd_output(cmd, ' ', 7):
dev_path, maj_min, rm, bsize, ro, tp, mountpoint = entry
lsblk_cmd = ['lsblk', '-nr', '--output', 'NAME,KNAME,SIZE', dev_path]
lsblk_info_for_devpath = next(_get_cmd_output(lsblk_cmd, ' ', 3), None)
cmd = ['lsblk', '-pbnr', '--output', 'NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT,PKNAME']
for entry in _get_cmd_output(cmd, ' ', 8):
dev_path, maj_min, rm, bsize, ro, tp, mountpoint, parent_path = entry

lsblk_info_for_devpath = _get_lsblk_info_for_devpath(dev_path)
if not lsblk_info_for_devpath:
return

name, kname, size = lsblk_info_for_devpath

parent_name = ""
if parent_path:
parent_info = _get_lsblk_info_for_devpath(parent_path)
if parent_info:
parent_name, _, _ = parent_info

yield LsblkEntry(
name=name,
kname=kname,
Expand All @@ -185,7 +198,9 @@ def _get_lsblk_info():
bsize=int(bsize),
ro=ro,
tp=tp,
mountpoint=mountpoint)
mountpoint=mountpoint,
parent_name=parent_name,
parent_path=parent_path)


@aslist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,36 @@ def test_get_lsblk_info(monkeypatch):
bytes_per_gb = 1 << 30

def get_cmd_output_mocked(cmd, delim, expected_len):
if cmd == ['lsblk', '-pbnr', '--output', 'NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT']:
if cmd == ['lsblk', '-pbnr', '--output', 'NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT,PKNAME']:
output_lines_split_on_whitespace = [
['vda', '252:0', '0', str(40 * bytes_per_gb), '0', 'disk', ''],
['vda1', '252:1', '0', str(1 * bytes_per_gb), '0', 'part', '/boot'],
['vda2', '252:2', '0', str(39 * bytes_per_gb), '0', 'part', ''],
['rhel_ibm--p8--kvm--03--guest--02-root', '253:0', '0', str(38 * bytes_per_gb), '0', 'lvm', '/'],
['rhel_ibm--p8--kvm--03--guest--02-swap', '253:1', '0', str(1 * bytes_per_gb), '0', 'lvm', '[SWAP]']
['/dev/vda', '252:0', '0', str(40 * bytes_per_gb), '0', 'disk', '', ''],
['/dev/vda1', '252:1', '0', str(1 * bytes_per_gb), '0', 'part', '/boot', ''],
['/dev/vda2', '252:2', '0', str(39 * bytes_per_gb), '0', 'part', '', ''],
['/dev/mapper/rhel_ibm--p8--kvm--03--guest--02-root', '253:0', '0', str(38 * bytes_per_gb), '0', 'lvm',
'/', ''],
['/dev/mapper/rhel_ibm--p8--kvm--03--guest--02-swap', '253:1', '0', str(1 * bytes_per_gb), '0', 'lvm',
'[SWAP]', ''],
['/dev/mapper/luks-01b60fff-a2a8-4c03-893f-056bfc3f06f6', '254:0', '0', str(38 * bytes_per_gb), '0',
'crypt', '', '/dev/nvme0n1p1'],
['/dev/nvme0n1p1', '259:1', '0', str(39 * bytes_per_gb), '0', 'part', '', '/dev/nvme0n1'],
]
for output_line_parts in output_lines_split_on_whitespace:
yield output_line_parts
elif len(cmd) == 5 and cmd[:4] == ['lsblk', '-nr', '--output', 'NAME,KNAME,SIZE']:
# We cannot have the output in a list, since the command is called per device. Therefore, we have to map
# each device path to its output.
output_lines_split_on_whitespace_per_device = {
'vda': ['vda', 'vda', '40G'],
'vda1': ['vda1', 'vda1', '1G'],
'vda2': ['vda2', 'vda2', '39G'],
'rhel_ibm--p8--kvm--03--guest--02-root': ['rhel_ibm--p8--kvm--03--guest--02-root', 'kname1', '38G'],
'rhel_ibm--p8--kvm--03--guest--02-swap': ['rhel_ibm--p8--kvm--03--guest--02-swap', 'kname2', '1G']
'/dev/vda': ['vda', 'vda', '40G'],
'/dev/vda1': ['vda1', 'vda1', '1G'],
'/dev/vda2': ['vda2', 'vda2', '39G'],
'/dev/mapper/rhel_ibm--p8--kvm--03--guest--02-root':
['rhel_ibm--p8--kvm--03--guest--02-root', 'kname1', '38G'],
'/dev/mapper/rhel_ibm--p8--kvm--03--guest--02-swap':
['rhel_ibm--p8--kvm--03--guest--02-swap', 'kname2', '1G'],
'/dev/mapper/luks-01b60fff-a2a8-4c03-893f-056bfc3f06f6':
['luks-01b60fff-a2a8-4c03-893f-056bfc3f06f6', 'dm-0', '38G'],
'/dev/nvme0n1p1': ['nvme0n1p1', 'nvme0n1p1', '39G'],
'/dev/nvme0n1': ['nvme0n1', 'nvme0n1', '40G'],
}
dev_path = cmd[4]
if dev_path not in output_lines_split_on_whitespace_per_device:
Expand All @@ -294,7 +305,9 @@ def get_cmd_output_mocked(cmd, delim, expected_len):
bsize=40 * bytes_per_gb,
ro='0',
tp='disk',
mountpoint=''),
mountpoint='',
parent_name='',
parent_path=''),
LsblkEntry(
name='vda1',
kname='vda1',
Expand All @@ -304,7 +317,9 @@ def get_cmd_output_mocked(cmd, delim, expected_len):
bsize=1 * bytes_per_gb,
ro='0',
tp='part',
mountpoint='/boot'),
mountpoint='/boot',
parent_name='',
parent_path=''),
LsblkEntry(
name='vda2',
kname='vda2',
Expand All @@ -314,7 +329,9 @@ def get_cmd_output_mocked(cmd, delim, expected_len):
bsize=39 * bytes_per_gb,
ro='0',
tp='part',
mountpoint=''),
mountpoint='',
parent_name='',
parent_path=''),
LsblkEntry(
name='rhel_ibm--p8--kvm--03--guest--02-root',
kname='kname1',
Expand All @@ -324,7 +341,9 @@ def get_cmd_output_mocked(cmd, delim, expected_len):
bsize=38 * bytes_per_gb,
ro='0',
tp='lvm',
mountpoint='/'),
mountpoint='/',
parent_name='',
parent_path=''),
LsblkEntry(
name='rhel_ibm--p8--kvm--03--guest--02-swap',
kname='kname2',
Expand All @@ -334,7 +353,34 @@ def get_cmd_output_mocked(cmd, delim, expected_len):
bsize=1 * bytes_per_gb,
ro='0',
tp='lvm',
mountpoint='[SWAP]')]
mountpoint='[SWAP]',
parent_name='',
parent_path=''),
LsblkEntry(
name='luks-01b60fff-a2a8-4c03-893f-056bfc3f06f6',
kname='dm-0',
maj_min='254:0',
rm='0',
size='38G',
bsize=38 * bytes_per_gb,
ro='0',
tp='crypt',
mountpoint='',
parent_name='nvme0n1p1',
parent_path='/dev/nvme0n1p1'),
LsblkEntry(
name='nvme0n1p1',
kname='nvme0n1p1',
maj_min='259:1',
rm='0',
size='39G',
bsize=39 * bytes_per_gb,
ro='0',
tp='part',
mountpoint='',
parent_name='nvme0n1',
parent_path='/dev/nvme0n1'),
]

actual = storagescanner._get_lsblk_info()
assert expected == actual
Expand Down
2 changes: 2 additions & 0 deletions repos/system_upgrade/common/models/storageinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class LsblkEntry(Model):
ro = fields.String()
tp = fields.String()
mountpoint = fields.String()
parent_name = fields.String()
parent_path = fields.String()


class PvsEntry(Model):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def _lsblk_entry(prefix, number, types, size='128G', bsize=2 ** 37):
bsize=bsize,
ro='0',
tp=types[random.randint(0, len(types) - 1)],
mountpoint='')
mountpoint='',
parent_name='',
parent_path='')


@aslist
Expand Down

0 comments on commit 72fa433

Please sign in to comment.