Merge branch 'project-overhaul' into dev

This commit is contained in:
2Shirt 2020-01-23 14:12:04 -07:00
commit dc97fb079d
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 74 additions and 63 deletions

View file

@ -53,6 +53,7 @@ def build_ufd():
args = docopt(DOCSTRING) args = docopt(DOCSTRING)
log.update_log_path(dest_name='build-ufd', timestamp=True) log.update_log_path(dest_name='build-ufd', timestamp=True)
try_print = std.TryAndPrint() try_print = std.TryAndPrint()
try_print.catch_all = False
try_print.indent = 2 try_print.indent = 2
# Check if running with root permissions # Check if running with root permissions
@ -75,7 +76,29 @@ def build_ufd():
# Prep UFD # Prep UFD
if not args['--update']: if not args['--update']:
std.print_info('Prep UFD') std.print_info('Prep UFD')
prep_device(ufd_dev, UFD_LABEL, use_mbr=args['--use-mbr']) try_print.run(
message='Zeroing first 64MiB...',
function=zero_device,
dev_path=ufd_dev,
)
try_print.run(
message='Creating partition table...',
function=create_table,
dev_path=ufd_dev,
use_mbr=args['--use-mbr'],
)
try_print.run(
message='Setting boot flag...',
function=set_boot_flag,
dev_path=ufd_dev,
use_mbr=args['--use-mbr'],
)
try_print.run(
message='Formatting partition...',
function=format_partition,
dev_path=ufd_dev,
label=UFD_LABEL,
)
# Mount UFD # Mount UFD
try_print.run( try_print.run(
@ -195,6 +218,19 @@ def copy_source(source, items, overwrite=False):
linux.unmount('/mnt/Source') linux.unmount('/mnt/Source')
def create_table(dev_path, use_mbr=False):
"""Create GPT or DOS partition table."""
cmd = [
'parted', dev_path,
'--script',
'--',
'mklabel', 'msdos' if use_mbr else 'gpt',
'mkpart', 'primary', 'fat32', '4MiB',
'-1s' if use_mbr else '-4MiB',
]
run_program(cmd)
def find_first_partition(dev_path): def find_first_partition(dev_path):
"""Find path to first partition of dev, returns str. """Find path to first partition of dev, returns str.
@ -218,6 +254,17 @@ def find_first_partition(dev_path):
return part_path return part_path
def format_partition(dev_path, label):
"""Format first partition on device FAT32."""
cmd = [
'mkfs.vfat',
'-F', '32',
'-n', label,
find_first_partition(dev_path),
]
run_program(cmd)
def get_uuid(path): def get_uuid(path):
"""Get filesystem UUID via findmnt, returns str.""" """Get filesystem UUID via findmnt, returns str."""
cmd = [ cmd = [
@ -288,66 +335,15 @@ def is_valid_path(path_obj, path_type):
return valid_path return valid_path
def prep_device(dev_path, label, use_mbr=False): def set_boot_flag(dev_path, use_mbr=False):
"""Format device in preparation for applying the WizardKit components """Set modern or legacy boot flag."""
cmd = [
This is done is four steps: 'parted', dev_path,
1. Zero-out first 64MB (this deletes the partition table and/or bootloader) 'set', '1',
2. Create a new partition table (GPT by default, optionally MBR) 'boot' if use_mbr else 'legacy_boot',
3. Set boot flag 'on',
4. Format partition (FAT32, 4K aligned) ]
""" run_program(cmd)
try_print = std.TryAndPrint()
try_print.indent = 2
def _create_table():
"""Create GPT or DOS partition table."""
cmd = [
'parted', dev_path,
'--script',
'--',
'mklabel', 'msdos' if use_mbr else 'gpt',
'mkpart', 'primary', 'fat32', '4MiB',
'-1s' if use_mbr else '-4MiB',
]
run_program(cmd)
def _format_partition():
"""Format first partition on device FAT32."""
cmd = [
'mkfs.vfat',
'-F', '32',
'-n', label,
find_first_partition(dev_path),
]
run_program(cmd)
def _set_boot_flag():
"""Set modern or legacy boot flag."""
cmd = [
'parted', dev_path,
'set', '1',
'boot' if use_mbr else 'legacy_boot',
'on',
]
run_program(cmd)
def _zero_device():
"""Zero-out first 64MB of device."""
cmd = [
'dd',
'bs=4M',
'count=16',
'if=/dev/zero',
f'of={dev_path}',
]
run_program(cmd)
# Run steps
try_print.run('Zeroing first 64MiB...', function=_zero_device)
try_print.run('Creating partition table...', function=_create_table)
try_print.run('Setting boot flag...', function=_set_boot_flag)
try_print.run('Formatting partition...', function=_format_partition)
def remove_arch(): def remove_arch():
@ -480,5 +476,17 @@ def verify_ufd(dev_path):
return ufd_dev return ufd_dev
def zero_device(dev_path):
"""Zero-out first 64MB of device."""
cmd = [
'dd',
'bs=4M',
'count=16',
'if=/dev/zero',
f'of={dev_path}',
]
run_program(cmd)
if __name__ == '__main__': if __name__ == '__main__':
print("This file is not meant to be called directly.") print("This file is not meant to be called directly.")

View file

@ -392,6 +392,7 @@ class TryAndPrint():
based on exception names. based on exception names.
""" """
def __init__(self, msg_bad='FAILED', msg_good='SUCCESS'): def __init__(self, msg_bad='FAILED', msg_good='SUCCESS'):
self.catch_all = True
self.indent = INDENT self.indent = INDENT
self.msg_bad = msg_bad self.msg_bad = msg_bad
self.msg_good = msg_good self.msg_good = msg_good
@ -522,13 +523,13 @@ class TryAndPrint():
def run( def run(
self, message, function, *args, self, message, function, *args,
catch_all=True, msg_good=None, verbose=False, **kwargs): catch_all=None, msg_good=None, verbose=False, **kwargs):
# pylint: disable=catching-non-exception # pylint: disable=catching-non-exception
"""Run a function and print the results, returns results as dict. """Run a function and print the results, returns results as dict.
If catch_all is True then (nearly) all exceptions will be caught. If catch_all is True then (nearly) all exceptions will be caught.
Otherwise if an exception occurs that wasn't specified it will be Otherwise if an exception occurs that wasn't specified it will be
re-raised. re-raised. If passed it will override self.catch_all for this call.
If the function returns data it will be used instead of msg_good, If the function returns data it will be used instead of msg_good,
msg_bad, or exception text. msg_bad, or exception text.
@ -553,6 +554,8 @@ class TryAndPrint():
f_exception = None f_exception = None
output = None output = None
result_msg = 'UNKNOWN' result_msg = 'UNKNOWN'
if catch_all is None:
catch_all = self.catch_all
# Build exception tuples # Build exception tuples
e_exceptions = tuple(self._get_exception(e) for e in self.list_errors) e_exceptions = tuple(self._get_exception(e) for e in self.list_errors)