Use lsblk json output for safety_check_block_device

Change-Id: Ibfc2e203287d92e66567c33dc48f59392852b88e
This commit is contained in:
Riccardo Pittau 2022-07-20 11:56:27 +02:00
parent beb7484858
commit b5fac66bc3
2 changed files with 31 additions and 32 deletions

View File

@ -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',

View File

@ -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')