From ca1fe71cf9867f22c9265a9b75ed895de515f892 Mon Sep 17 00:00:00 2001 From: Matthew Thode Date: Thu, 17 May 2018 10:50:47 -0500 Subject: [PATCH] Fall back to PARTUUID if UUID returns nothing. blkid does not always return a UUID when querying partitions (sda2 in my case). It does seem to return a PARTUUID though, so use that if needed. Change-Id: I6f8dbd8fb8a02f6246b7ab00801159544f3288d6 Story: 2002052 Task: 19701 --- ironic_lib/disk_utils.py | 14 +++++++++++--- ironic_lib/tests/test_disk_utils.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ironic_lib/disk_utils.py b/ironic_lib/disk_utils.py index 4859fa38..c2764850 100644 --- a/ironic_lib/disk_utils.py +++ b/ironic_lib/disk_utils.py @@ -325,10 +325,18 @@ def populate_image(src, dst): def block_uuid(dev): - """Get UUID of a block device.""" + """Get UUID of a block device. + + Try to fetch the UUID, if that fails, try to fetch the PARTUUID. + """ out, _err = utils.execute('blkid', '-s', 'UUID', '-o', 'value', dev, - run_as_root=True, - check_exit_code=[0]) + run_as_root=True, check_exit_code=[0]) + if not out: + LOG.debug('Falling back to partition UUID as the block device UUID ' + 'was not found while examining %(device)s', + {'device': dev}) + out, _err = utils.execute('blkid', '-s', 'PARTUUID', '-o', 'value', + dev, run_as_root=True, check_exit_code=[0]) return out.strip() diff --git a/ironic_lib/tests/test_disk_utils.py b/ironic_lib/tests/test_disk_utils.py index 2b7f8ba1..9281e1c7 100644 --- a/ironic_lib/tests/test_disk_utils.py +++ b/ironic_lib/tests/test_disk_utils.py @@ -807,6 +807,22 @@ class OtherFunctionTestCase(base.IronicLibTestCase): '/dev/fake', run_as_root=True, use_standard_locale=True) + @mock.patch.object(utils, 'execute', autospec=True) + def test_block_uuid_fallback_to_uuid(self, mock_execute): + mock_execute.side_effect = [('', ''), + ('value', '')] + self.assertEqual('value', + disk_utils.block_uuid('/dev/fake')) + execute_calls = [ + mock.call('blkid', '-s', 'UUID', '-o', 'value', + '/dev/fake', check_exit_code=[0], + run_as_root=True), + mock.call('blkid', '-s', 'PARTUUID', '-o', 'value', + '/dev/fake', check_exit_code=[0], + run_as_root=True) + ] + mock_execute.assert_has_calls(execute_calls) + @mock.patch.object(utils, 'execute', autospec=True) class WholeDiskPartitionTestCases(base.IronicLibTestCase):