Skip to content

Commit

Permalink
Refactor part of image module
Browse files Browse the repository at this point in the history
Shuffle some functions around and reduce size of _is_bootloader_loaded
moving logic out to a new function.

Change-Id: I9c10bf05186dcebb37f175d61bf4ac9ff86b6510
  • Loading branch information
elfosardo committed Jul 7, 2020
1 parent 2e9620a commit 9d9a6bc
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions ironic_python_agent/extensions/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,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.
Expand All @@ -157,20 +188,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':
Expand All @@ -186,19 +203,7 @@ def _has_boot_sector(device):
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):
Expand Down

0 comments on commit 9d9a6bc

Please sign in to comment.