-#!/usr/bin/env python
+#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# pylint: disable-msg=C0302
"""
This script installs a Grml system (either a running system or ISO[s]) to a USB device
-:copyright: (c) 2009, 2010, 2011 by Michael Prokop <mika@grml.org>
+:copyright: (c) 2009-2019 by Michael Prokop <mika@grml.org>
:license: GPL v2 or any later version
:bugreports: http://grml.org/bugs/
GRML_DEFAULT = None
UUID = None
SYSLINUX_LIBS = "/usr/lib/syslinux/"
-GPT_HEADER = "\x55\xaa\x45\x46\x49\x20\x50\x41\x52\x54" # original GPT header
+GPT_HEADER = b"\x55\xaa\x45\x46\x49\x20\x50\x41\x52\x54" # original GPT header
GRUB_INSTALL = None
RE_PARTITION = re.compile(r'([a-z/]*?)(\d+)$')
setattr(opt_parser.values, option.dest, True)
setattr(opt_parser.values, 'syslinux', False)
+
# cmdline parsing
-USAGE = "Usage: %prog [options] <[ISO[s] | /lib/live/mount/medium]> </dev/sdX#>\n\
+USAGE = "Usage: %prog [options] <[ISO[s] | /run/live/medium]> </dev/sdX#>\n\
\n\
%prog installs Grml ISO[s] to an USB device to be able to boot from it.\n\
-Make sure you have at least one Grml ISO or a running Grml system (/lib/live/mount/medium),\n\
+Make sure you have at least one Grml ISO or a running Grml system (/run/live/medium),\n\
grub or syslinux and root access.\n\
\n\
Run %prog --help for usage hints, further information via: man grml2usb"
def get_function_name(obj):
- """Helper function for use in execute() to retrive name of a function
+ """Helper function for use in execute() to retrieve name of a function
@obj: the function object
"""
if part.getFlag(parted.PARTITION_BOOT):
logging.debug("bootflag is enabled on %s" % device)
return
- except HodorException, e:
+ except HodorException as e:
logging.info("%s, falling back to old bootflag detection", e)
- except ImportError, e:
+ except ImportError as e:
logging.debug("could not import parted, falling back to old bootflag detection")
with open(boot_dev, 'r') as image:
if gpt_data == GPT_HEADER:
logging.info("GPT detected, skipping bootflag check")
- elif bootcode[6] == '\x80':
+ elif bootcode[6] == b"\x80":
logging.debug("bootflag is enabled")
else:
logging.debug("bootflag is NOT enabled")
device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
register_tmpfile(device_mountpoint)
try:
- mount(device, device_mountpoint, "")
-
# If using --grub-mbr then make sure we install grub in MBR instead of PBR
if options.grubmbr:
logging.debug("Using option --grub-mbr ...")
else:
grub_device = device
+ set_rw(device)
+ mount(device, device_mountpoint, "")
+
logging.info("Installing grub as bootloader")
- for opt in ["", "--force"]:
+ for opt in ["--", "--force"]:
set_rw(device)
set_rw(grub_device)
- logging.debug("grub-install --recheck %s --no-floppy --root-directory=%s %s",
- opt, device_mountpoint, grub_device)
- proc = subprocess.Popen([GRUB_INSTALL, "--recheck", opt,
+ logging.debug("%s --recheck --no-floppy --target=i386-pc --root-directory=%s %s %s",
+ GRUB_INSTALL, device_mountpoint, opt, grub_device)
+ proc = subprocess.Popen([GRUB_INSTALL, "--recheck",
"--no-floppy", "--target=i386-pc",
- "--root-directory=%s" % device_mountpoint, grub_device],
- stdout=file(os.devnull, "r+"))
+ "--root-directory=%s" % device_mountpoint,
+ opt, grub_device],
+ stdout=open(os.devnull, "r+"))
proc.wait()
if proc.returncode == 0:
break
logging.debug("executing: dd if='%s' of='%s' bs=512 count=1", device, tmpf.name)
proc = subprocess.Popen(["dd", "if=%s" % device, "of=%s" % tmpf.name, "bs=512", "count=1"],
- stderr=file(os.devnull, "r+"))
+ stderr=open(os.devnull, "r+"))
proc.wait()
if proc.returncode != 0:
raise Exception("error executing dd (first run)")
logging.debug("executing: dd if=%s of=%s bs=%s count=1 conv=notrunc", mbrtemplate,
tmpf.name, nmbrbytes)
proc = subprocess.Popen(["dd", "if=%s" % mbrtemplate, "of=%s" % tmpf.name, "bs=%s" % nmbrbytes,
- "count=1", "conv=notrunc"], stderr=file(os.devnull, "r+"))
+ "count=1", "conv=notrunc"], stderr=open(os.devnull, "r+"))
proc.wait()
if proc.returncode != 0:
raise Exception("error executing dd (second run)")
if partition is not None:
if ismirbsdmbr:
- mbrcode = mbrcode[0:439] + chr(partition) + \
- mbrcode[440:510] + "\x55\xAA"
+ mbrcode = mbrcode[0:439] + chr(partition).encode('latin-1') + \
+ mbrcode[440:510] + b"\x55\xAA"
else:
- actives = ["\x00", "\x00", "\x00", "\x00"]
- actives[partition] = "\x80"
+ actives = [b"\x00", b"\x00", b"\x00", b"\x00"]
+ actives[partition] = b"\x80"
mbrcode = mbrcode[0:446] + actives[0] + \
mbrcode[447:462] + actives[1] + \
mbrcode[463:478] + actives[2] + \
mbrcode[479:494] + actives[3] + \
- mbrcode[495:510] + "\x55\xAA"
+ mbrcode[495:510] + b"\x55\xAA"
tmpf.file.seek(0)
tmpf.file.truncate()
logging.debug("executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc", tmpf.name, device)
proc = subprocess.Popen(["dd", "if=%s" % tmpf.name, "of=%s" % device, "bs=512", "count=1",
- "conv=notrunc"], stderr=file(os.devnull, "r+"))
+ "conv=notrunc"], stderr=open(os.devnull, "r+"))
proc.wait()
if proc.returncode != 0:
raise Exception("error executing dd (third run)")
# note: options.dryrun does not work here, as we have to
# locate files and identify the grml flavour
- for x in file('/proc/mounts').readlines():
+ for x in open('/proc/mounts', 'r').readlines():
if x.startswith(source):
raise CriticalException("Error executing mount: %s already mounted - " % source +
"please unmount before invoking grml2usb")
# ipxe.lkrn
handle_addon_copy('ipxe.lkrn', addons, iso_mount)
+ # netboot.xyz
+ handle_addon_copy('netboot.xyz.lkrn', addons, iso_mount)
+
def build_loopbackcfg(target):
"""Generate GRUB's loopback.cfg based on existing config files.
if efi_img:
mkdir(target + '/boot/')
exec_rsync(efi_img, target + '/boot/efi.img')
+ handle_secure_boot(target, efi_img)
for ffile in ['f%d' % number for number in range(1, 11)]:
search_and_copy(ffile, iso_mount, syslinux_target + ffile)
# copy all grub files from ISO
glob_and_copy(iso_mount + '/boot/grub/*', grub_target)
- # finally (after all GRUB files have been been installed) build static loopback.cfg
+ # finally (after all GRUB files have been installed) build static loopback.cfg
build_loopbackcfg(target)
version_files = search_file('grml-version', mountpath, lst_return=True)
if not version_files:
- if mountpath.startswith("/lib/live/mount/medium"):
+ if mountpath.startswith("/run/live/medium"):
logging.critical("Error: could not find grml-version file.")
logging.critical("Looks like your system is running from RAM but required files are not available.")
logging.critical("Please either boot without toram=... or use boot option toram instead of toram=...")
logging.debug("Updating grub configuration")
grub_target = target + '/boot/grub/'
+ secureboot_target = target + '/EFI/ubuntu/'
bootid_re = re.compile("bootid=[\w_-]+")
live_media_path_re = re.compile("live-media-path=[\w_/-]+")
remove_regexes.append(re.compile(regex))
shortname = get_shortname(grml_flavour)
- for filename in glob.glob(grub_target + '*.cfg'):
+ for filename in glob.glob(grub_target + '*.cfg') + glob.glob(secureboot_target + '*.cfg'):
for line in fileinput.input(filename, inplace=1):
line = line.rstrip("\r\n")
if option_re.search(line):
if shortname in filename:
line = live_media_path_re.sub('', line)
line = line.rstrip() + ' live-media-path=/live/%s/ ' % (grml_flavour)
+ if bootopt.strip():
+ line = line.replace(' {} '.format(bootopt.strip()), ' ')
+ if line.endswith(bootopt):
+ line = line[:-len(bootopt)]
line = line.rstrip() + r' bootid=%s %s ' % (UUID, bootopt)
for regex in remove_regexes:
line = regex.sub(' ', line)
def initial_syslinux_config(target):
- """Generates intial syslinux configuration
+ """Generates initial syslinux configuration
@target path of syslinux's configuration files"""
add_syslinux_entry("%s/additional.cfg" % syslinux_target, flavour_filename)
+def handle_secure_boot(target, efi_img):
+ """Provide secure boot support by extracting files from /boot/efi.img
+
+ @target: path where grml's main files should be copied to
+ @efi_img: path to the efi.img file that includes the files for secure boot
+ """
+
+ mkdir(target + '/efi/boot/')
+ efi_mountpoint = tempfile.mkdtemp(prefix="grml2usb", dir=os.path.abspath(options.tmpdir))
+ logging.debug("efi_mountpoint = %s" % efi_mountpoint)
+ register_tmpfile(efi_mountpoint)
+
+ try:
+ logging.debug("mount(%s, %s, ['-o', 'ro', '-t', 'vfat']" % (efi_img, efi_mountpoint))
+ mount(efi_img, efi_mountpoint, ['-o', 'ro', '-t', 'vfat'])
+ except CriticalException as error:
+ logging.critical("Fatal: %s", error)
+ sys.exit(1)
+
+ ubuntu_cfg = search_file('grub.cfg', efi_mountpoint + '/EFI/ubuntu')
+ logging.debug("ubuntu_cfg = %s" % ubuntu_cfg)
+ if not ubuntu_cfg:
+ logging.info("No /EFI/ubuntu/grub.cfg found inside EFI image, looks like Secure Boot support is missing.")
+ else:
+ mkdir(target + '/efi/ubuntu')
+ logging.debug("exec_rsync(%s, %s + '/efi/ubuntu/grub.cfg')" % (ubuntu_cfg, target))
+ exec_rsync(ubuntu_cfg, target + '/efi/ubuntu/grub.cfg')
+
+ logging.debug("exec_rsync(%s + '/EFI/BOOT/grubx64.efi', %s + '/efi/boot/grubx64.efi')'" % (efi_mountpoint, target))
+ exec_rsync(efi_mountpoint + '/EFI/BOOT/grubx64.efi', target + '/efi/boot/grubx64.efi')
+
+ # NOTE - we're overwriting /efi/boot/bootx64.efi from copy_bootloader_files here
+ logging.debug("exec_rsync(%s + '/EFI/BOOT/bootx64.efi', %s + '/efi/boot/bootx64.efi')'" % (efi_mountpoint, target))
+ exec_rsync(efi_mountpoint + '/EFI/BOOT/bootx64.efi', target + '/efi/boot/bootx64.efi')
+
+ try:
+ unmount(efi_mountpoint, "")
+ logging.debug('Unmounted %s' % efi_mountpoint)
+ os.rmdir(efi_mountpoint)
+ logging.debug('Removed directory %s' % efi_mountpoint)
+ except StandardError:
+ logging.critical('RuntimeError while umount %s' % efi_mountpoint)
+ sys.exit(1)
+
+
def handle_bootloader_config(grml_flavour, device, target):
"""Main handler for generating bootloader's configuration
def install_grml(mountpoint, device):
"""Main logic for copying files of the currently running Grml system.
- @mountpoint: directory where currently running live system resides (usually /lib/live/mount/medium)
+ @mountpoint: directory where currently running live system resides (usually /run/live/medium)
@device: partition where the specified ISO should be installed to"""
device_mountpoint = device
check_for_fat(device)
if not options.skipbootflag:
check_boot_flag(device)
+
+ set_rw(device)
mount(device, device_mountpoint, ['-o', 'utf8,iocharset=iso8859-1'])
except CriticalException as error:
mount(device, device_mountpoint, "")
def check_options(opts):
- """Check compability of provided user opts
+ """Check compatibility of provided user opts
@opts option dict from OptionParser
"""