summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRiccardo Pittau <elfosardo@gmail.com>2020-07-03 17:04:27 +0200
committerSteve Baker <sbaker@redhat.com>2021-03-24 11:13:36 +1300
commit83932952c18dbe347c81e91d5525d13ca567a419 (patch)
tree93e68d99ac361682e334743bf2cd9c83a56aff2c
parent045bcfef32e4ab679678500d2a58c92398ad838c (diff)
downloadironic-python-agent-83932952c18dbe347c81e91d5525d13ca567a419.tar.gz
Refactor part of image module
Shuffle some functions around and reduce size of _is_bootloader_loaded moving logic out to a new function. Change-Id: I9c10bf05186dcebb37f175d61bf4ac9ff86b6510 (cherry picked from commit 9d9a6bce5c246d18d50dc405265e865fb77142eb) (cherry picked from commit d0f69e9948bf81d338bbc7c84711263fbd937598)
-rw-r--r--ironic_python_agent/extensions/image.py59
1 files changed, 32 insertions, 27 deletions
diff --git a/ironic_python_agent/extensions/image.py b/ironic_python_agent/extensions/image.py
index 6c6ba496..576404fc 100644
--- a/ironic_python_agent/extensions/image.py
+++ b/ironic_python_agent/extensions/image.py
@@ -144,6 +144,37 @@ def _has_dracut(root):
return True
+def _has_boot_sector(device):
+ """Checks the device for a boot sector indicator."""
+ stdout, stderr = utils.execute('file', '-s', device)
+ if 'boot sector' not in stdout:
+ return False
+ else:
+ # Now lets check the signature
+ ddout, dderr = utils.execute(
+ 'dd', 'if=%s' % device, 'bs=218', 'count=1', binary=True)
+ stdout, stderr = utils.execute('file', '-', process_input=ddout)
+ # The bytes recovered by dd show as a "dos executable" when
+ # examined with file. In other words, the bootloader is present.
+ if 'executable' in stdout:
+ return True
+ return False
+
+
+def _find_bootable_device(partitions, dev):
+ """Checks the base device and partition for bootloader contents."""
+ LOG.debug('Looking for a bootable device in %s', dev)
+ for line in partitions.splitlines():
+ partition = line.split(':')
+ try:
+ if 'boot' in partition[6]:
+ if _has_boot_sector(dev) or _has_boot_sector(partition[0]):
+ return True
+ except IndexError:
+ continue
+ return False
+
+
def _is_bootloader_loaded(dev):
"""Checks the device to see if a MBR bootloader is present.
@@ -153,20 +184,6 @@ def _is_bootloader_loaded(dev):
loader, otherwise False.
"""
- def _has_boot_sector(device):
- """Check the device for a boot sector indiator."""
- stdout, stderr = utils.execute('file', '-s', device)
- if 'boot sector' in stdout:
- # Now lets check the signature
- ddout, dderr = utils.execute(
- 'dd', 'if=%s' % device, 'bs=218', 'count=1', binary=True)
- stdout, stderr = utils.execute('file', '-', process_input=ddout)
- # The bytes recovered by dd show as a "dos executable" when
- # examined with file. In other words, the bootloader is present.
- if 'executable' in stdout:
- return True
- return False
-
boot = hardware.dispatch_to_managers('get_boot_info')
if boot.current_boot_mode != 'bios':
@@ -182,19 +199,7 @@ def _is_bootloader_loaded(dev):
except processutils.ProcessExecutionError:
return False
- lines = stdout.splitlines()
- for line in lines:
- partition = line.split(':')
- try:
- # Find the bootable device, and check the base
- # device and partition for bootloader contents.
- if 'boot' in partition[6]:
- if (_has_boot_sector(dev)
- or _has_boot_sector(partition[0])):
- return True
- except IndexError:
- continue
- return False
+ return _find_bootable_device(stdout, dev)
def _get_efi_bootloaders(location):