From b5fac66bc3ce2cd03a6561f32fb2aba31e8f9fcb Mon Sep 17 00:00:00 2001 From: Riccardo Pittau Date: Wed, 20 Jul 2022 11:56:27 +0200 Subject: [PATCH] Use lsblk json output for safety_check_block_device Change-Id: Ibfc2e203287d92e66567c33dc48f59392852b88e --- ironic_python_agent/hardware.py | 31 ++++++++---------- .../tests/unit/test_hardware.py | 32 ++++++++++--------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/ironic_python_agent/hardware.py b/ironic_python_agent/hardware.py index dfcce6f80..e5f51025b 100644 --- a/ironic_python_agent/hardware.py +++ b/ironic_python_agent/hardware.py @@ -3016,26 +3016,24 @@ def safety_check_block_device(node, device): di_info = node.get('driver_internal_info', {}) if not di_info.get('wipe_special_filesystems', True): return - report, _e = il_utils.execute('lsblk', '-Pbia', - '-oFSTYPE,UUID,PTUUID,PARTTYPE,PARTUUID', - device) + lsblk_ids = ['UUID', 'PTUUID', 'PARTTYPE', 'PARTUUID'] + report = il_utils.execute('lsblk', '-bia', '--json', + '-o{}'.format(','.join(lsblk_ids)), + device, check_exit_code=[0])[0] - lines = report.splitlines() + try: + report_json = json.loads(report) + except json.decoder.JSONDecodeError as ex: + LOG.error("Unable to decode lsblk output, invalid JSON: %s", ex) + device_json = report_json['blockdevices'][0] identified_fs_types = [] identified_ids = [] - for line in lines: - device = {} - # Split into KEY=VAL pairs - vals = shlex.split(line) - if not vals: - continue - for key, val in (v.split('=', 1) for v in vals): - if key == 'FSTYPE': - identified_fs_types.append(val) - if key in ['UUID', 'PTUUID', 'PARTTYPE', 'PARTUUID']: - identified_ids.append(val) - # Ignore block types not specified + + fstype = device_json.get('fstype') + identified_fs_types.append(fstype) + for key in lsblk_ids: + identified_ids.append(device_json.get(key.lower())) _check_for_special_partitions_filesystems( device, @@ -3053,7 +3051,6 @@ def _check_for_special_partitions_filesystems(device, ids, fs_types): be discovered which suggests a shared disk clustered filesystem has been discovered. """ - guarded_ids = { # Apparently GPFS can used shared volumes.... '37AFFC90-EF7D-4E96-91C3-2D7AE055B174': 'IBM GPFS Partition', diff --git a/ironic_python_agent/tests/unit/test_hardware.py b/ironic_python_agent/tests/unit/test_hardware.py index 213490505..d3393cb4d 100644 --- a/ironic_python_agent/tests/unit/test_hardware.py +++ b/ironic_python_agent/tests/unit/test_hardware.py @@ -5154,29 +5154,31 @@ class TestProtectedDiskSafetyChecks(base.IronicAgentTest): mock_execute.assert_not_called() def test_special_filesystem_guard_enabled_no_results(self, mock_execute): - mock_execute.return_value = ('', '') + mock_execute.return_value = ('{"blockdevices": [{"foo": "bar"}]}', '') hardware.safety_check_block_device({}, '/dev/foo') def test_special_filesystem_guard_raises(self, mock_execute): - GFS2 = 'FSTYPE="gfs2"\n' - GPFS1 = 'UUID="37AFFC90-EF7D-4E96-91C3-2D7AE055B174"\n' - GPFS2 = 'PTUUID="37AFFC90-EF7D-4E96-91C3-2D7AE055B174"\n' - GPFS3 = 'PARTTYPE="37AFFC90-EF7D-4E96-91C3-2D7AE055B174"\n' - GPFS4 = 'PARTUUID="37AFFC90-EF7D-4E96-91C3-2D7AE055B174"\n' - VMFS1 = 'UUID="AA31E02A-400F-11DB-9590-000C2911D1B8"\n' - VMFS2 = 'UUID="AA31E02A-400F-11DB-9590-000C2911D1B8"\n' - VMFS3 = 'UUID="AA31E02A-400F-11DB-9590-000C2911D1B8"\n' - VMFS4 = 'UUID="AA31E02A-400F-11DB-9590-000C2911D1B8"\n' - VMFS5 = 'UUID="0xfb"\n' - VMFS6 = 'PTUUID="0xfb"\n' - VMFS7 = 'PARTTYPE="0xfb"\n' - VMFS8 = 'PARTUUID="0xfb"\n' + GFS2 = '"fstype": "gfs2"' + GPFS1 = '"uuid": "37AFFC90-EF7D-4E96-91C3-2D7AE055B174"' + GPFS2 = '"ptuuid": "37AFFC90-EF7D-4E96-91C3-2D7AE055B174"' + GPFS3 = '"parttype": "37AFFC90-EF7D-4E96-91C3-2D7AE055B174"' + GPFS4 = '"partuuid": "37AFFC90-EF7D-4E96-91C3-2D7AE055B174"' + VMFS1 = '"uuid": "AA31E02A-400F-11DB-9590-000C2911D1B8"' + VMFS2 = '"uuid": "AA31E02A-400F-11DB-9590-000C2911D1B8"' + VMFS3 = '"uuid": "AA31E02A-400F-11DB-9590-000C2911D1B8"' + VMFS4 = '"uuid": "AA31E02A-400F-11DB-9590-000C2911D1B8"' + VMFS5 = '"uuid": "0xfb"' + VMFS6 = '"ptuuid": "0xfb"' + VMFS7 = '"parttype": "0xfb"' + VMFS8 = '"partuuid": "0xfb"' expected_failures = [GFS2, GPFS1, GPFS2, GPFS3, GPFS4, VMFS1, VMFS2, VMFS3, VMFS4, VMFS5, VMFS6, VMFS7, VMFS8] for failure in expected_failures: mock_execute.reset_mock() - mock_execute.return_value = (failure, '') + dev_failure = ('{{"blockdevices": [{{{failure}}}]}}' + .format(failure=failure)) + mock_execute.return_value = (dev_failure, '') self.assertRaises(errors.ProtectedDeviceError, hardware.safety_check_block_device, {}, '/dev/foo')