Catch blkid error when device is not yet formatted

When a new device is added to the ring we first try to
identify whether the device is already in the ring by
polling for an fs uuid. If the device has never been
used this is expected to fail so lets catch the error.

Also fixes log message.

Change-Id: I20354dedfa27a6b8dec92828cabb50a20d0d8838
Closes-Bug: 1567198
This commit is contained in:
Edward Hope-Morley 2017-09-13 16:00:09 -06:00
parent f87d83b1ab
commit 656e79da18
2 changed files with 22 additions and 2 deletions

View File

@ -364,7 +364,21 @@ def is_device_in_ring(dev, skip_rel_check=False, ignore_deactivated=True):
def get_device_blkid(dev):
blk_uuid = subprocess.check_output(['blkid', '-s', 'UUID', dev])
"""Try to get the fs uuid of the provided device.
If this is called for a new unformatted device we expect blkid to fail
hence return None to indicate the device is not in use.
:param dev: block device path
:returns: UUID of device if found else None
"""
try:
blk_uuid = subprocess.check_output(['blkid', '-s', 'UUID', dev])
except CalledProcessError:
# If the device has not be used or formatted yet we expect this to
# fail.
return None
blk_uuid = re.match(r'^%s:\s+UUID="(.+)"$' % (dev), blk_uuid.strip())
if blk_uuid:
return blk_uuid.group(1)
@ -402,7 +416,7 @@ def remember_devices(devs):
level=WARNING)
else:
log("Adding device '%s' with blkid='%s' to devstore" %
(blk_uuid, dev),
(dev, blk_uuid),
level=DEBUG)
devstore[key] = {'blkid': blk_uuid, 'status': 'active'}

View File

@ -447,3 +447,9 @@ class SwiftStorageUtilsTests(CharmTestCase):
uuid = swift_utils.get_device_blkid(dev)
self.assertEqual(uuid, "808bc298-0609-4619-aaef-ed7a5ab0ebb7")
mock_check_output.assert_called_with(cmd)
def fake_check_output(*args, **kwargs):
raise swift_utils.CalledProcessError('a', 'b', 'c')
mock_check_output.side_effect = fake_check_output
self.assertIsNone(swift_utils.get_device_blkid(dev))