#!/usr/bin/env python3 # -*- coding: utf-8 -* # Name: grub.py # Purpose: This module contain many functions used for updating grub.cfg file to provide support for EFI/UEFI booting # Authors: Sundar # Licence: This file is a part of multibootusb package. You can redistribute it or modify # under the terms of GNU General Public License, v.2 or above import os import re from . import config from . import iso from . import _7zip from . import gen from .usb import bytes2human from . import menus def mbusb_update_grub_cfg(): """ Function to update grub.cfg file to support UEFI/EFI systems :return: """ install_dir = os.path.join(config.usb_mount, 'multibootusb', iso.iso_basename(config.image_path)) grub_cfg_path = None syslinux_menu = None mbus_grub_cfg_path = os.path.join(config.usb_mount, 'multibootusb', 'grub', 'grub.cfg') isobin_dir = iso.isolinux_bin_dir(config.image_path) if isobin_dir is not False: for name in ['syslinux.cfg', 'isolinux.cfg']: cfg_path = os.path.join(isobin_dir, name) cfg_fullpath = os.path.join(install_dir, cfg_path) if os.path.exists(cfg_fullpath): syslinux_menu = cfg_path.replace('\\', '/') break # Decide which grub config file to boot by. loopback_cfg_list = iso.get_file_list( config.image_path, lambda x: os.path.basename(x).lower()=='loopback.cfg') grub_cfg_list = iso.get_file_list( config.image_path, lambda x: os.path.basename(x).lower().startswith('grub') and os.path.basename(x).lower().endswith('.cfg')) # favour 'grub.cfg' over variants. flagged = [(f, os.path.basename(f).lower()=='grub.cfg') for f in grub_cfg_list] grub_cfg_list = [ x[0] for x in flagged if x[1] ] + \ [ x[0] for x in flagged if not x[1] ] candidates = [] for src_list, predicate in [ # List in the order of decreasing preference. (loopback_cfg_list, lambda x: 'efi' in x.lower()), (loopback_cfg_list, lambda x: 'boot' in x.lower()), (grub_cfg_list, lambda x: 'efi' in x.lower()), (grub_cfg_list, lambda x: 'boot' in x.lower() and 'efi' not in x.lower()), (loopback_cfg_list, lambda x: 'efi' not in x.lower() and 'boot' not in x.lower()), (grub_cfg_list, lambda x: 'efi' not in x.lower() and 'boot' not in x.lower())]: sub_candidates = [x for x in src_list if predicate(x)] if len(sub_candidates): candidates.append(sub_candidates[0]) # We could 'break' here but will let the iteration continue # in order to lower the chance of keeping latent bugs. if config.distro == 'mageialive' and 1