2 # -*- coding: utf-8 -*-
3 # pylint: disable-msg=C0302
8 This script installs a Grml system (either a running system or ISO[s]) to a USB device
10 :copyright: (c) 2009-2019 by Michael Prokop <mika@grml.org>
11 :license: GPL v2 or any later version
12 :bugreports: http://grml.org/bugs/
28 from inspect import isclass, isroutine
29 from optparse import OptionParser
31 # The line following this line is patched by debian/rules and tarball.sh.
32 PROG_VERSION = "***UNKNOWN***"
34 # when running from inside git, try to report version information via git-describe
36 git_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
37 with open(os.devnull, "w") as devnull:
39 subprocess.check_output(
40 ["git", "-C", git_dir, "describe", "--always", "--dirty"],
44 .decode("utf-8", errors="replace")
51 MOUNTED = set() # register mountpoints
52 TMPFILES = set() # register tmpfiles
53 GRML_FLAVOURS = set() # which flavours are being installed?
57 "/usr/lib/syslinux/modules/bios/", # Debian
58 "/usr/lib/syslinux/bios/", # Arch Linux
62 RE_PARTITION = re.compile(r"([a-z/]*?)(\d+)$")
63 RE_P_PARTITION = re.compile(r"(.*?\d+)p(\d+)$")
64 RE_LOOP_DEVICE = re.compile(r"/dev/loop\d+$")
67 def syslinux_warning(option, opt, opt_parser):
68 """A helper function for printing a warning about deprecated option"""
69 # pylint: disable-msg=W0613
71 "Note: the --syslinux option is deprecated as syslinux "
72 "is grml2usb's default. Continuing anyway.\n"
74 setattr(opt_parser.values, option.dest, True)
77 # if grub option is set, unset syslinux option
78 def grub_option(option, opt, opt_parser):
79 """A helper function adjusting other option values"""
80 # pylint: disable-msg=W0613
81 setattr(opt_parser.values, option.dest, True)
82 setattr(opt_parser.values, "syslinux", False)
86 USAGE = "Usage: %prog [options] <[ISO[s] | /run/live/medium]> </dev/sdX#>\n\
88 %prog installs Grml ISO[s] to an USB device to be able to boot from it.\n\
89 Make sure you have at least one Grml ISO or a running Grml system (/run/live/medium),\n\
90 grub or syslinux and root access.\n\
92 Run %prog --help for usage hints, further information via: man grml2usb"
94 # pylint: disable-msg=C0103
95 # pylint: disable-msg=W0603
96 parser = OptionParser(usage=USAGE)
102 help="use specified bootoptions as default",
106 dest="bootloaderonly",
108 help="do not copy files but just install a bootloader",
114 help="copy files only but do not install bootloader",
117 "--dry-run", dest="dryrun", action="store_true", help="avoid executing commands"
123 help="format specified partition with FAT16",
129 help="force any actions requiring manual interaction",
135 callback=grub_option,
136 help="install grub bootloader instead of (default) syslinux",
142 help="install grub into MBR instead of (default) PBR",
148 help="enable interactive boot menu in MBR",
154 help="do not output anything but just errors on console",
157 "--remove-bootoption",
160 help="regex for removing existing bootoptions",
166 help="enforce read-write mode on involved block devices",
172 help="do not install /boot/addons/ files",
178 help="do not try to check whether the destination has the boot flag set",
181 "--skip-grub-config",
182 dest="skipgrubconfig",
184 help="skip generation of grub configuration files",
190 help="do not install a master boot record (MBR) on the device",
193 "--skip-syslinux-config",
194 dest="skipsyslinuxconfig",
196 help="skip generation of syslinux configuration files",
202 help="skip check to verify whether given device is removable",
209 callback=syslinux_warning,
210 help="install syslinux bootloader (deprecated as it's the default)",
216 help="install syslinux master boot record (MBR) instead of default",
223 help="syslinux modules directory path",
229 help="directory to be used for temporary files",
232 "--verbose", dest="verbose", action="store_true", help="enable verbose mode"
239 help="display version and exit",
241 (options, args) = parser.parse_args()
244 GRML2USB_BASE = "/usr/share/grml2usb"
245 if not os.path.isdir(GRML2USB_BASE):
246 GRML2USB_BASE = os.path.dirname(os.path.realpath(__file__))
249 class HodorException(Exception):
250 """Throw exception if the exact error is not known and not fatal.
252 @Exception: message"""
255 class CriticalException(Exception):
256 """Throw critical exception if the exact error is not known but fatal.
258 @Exception: message"""
261 class VerifyException(Exception):
262 """Throw critical exception if there is an fatal error when verifying something.
264 @Exception: message"""
268 """Cleanup function to make sure there aren't any mounted devices left behind."""
270 def del_failed(_fn, path, _exc):
271 logging.warning("Deletion of %s failed in temporary folder", path)
273 logging.info("Cleaning up before exiting...")
274 proc = subprocess.Popen(["sync"])
277 for device in MOUNTED.copy():
280 logging.debug("Unmounted %s" % device)
282 logging.debug("RuntimeError while umount %s, ignoring" % device)
284 for tmppath in TMPFILES.copy():
286 if os.path.isdir(tmppath) and not os.path.islink(tmppath):
287 # symbolic links to directories are ignored
288 # without the check it will throw an OSError
289 shutil.rmtree(tmppath, onerror=del_failed)
290 logging.debug("temporary directory %s deleted" % tmppath)
291 unregister_tmpfile(tmppath)
294 logging.debug("temporary file %s deleted" % tmppath)
295 unregister_tmpfile(tmppath)
297 msg = "RuntimeError while removing temporary %s, ignoring"
298 logging.debug(msg % tmppath)
301 def register_tmpfile(path):
309 def unregister_tmpfile(path):
311 remove registered tmpfile
315 TMPFILES.remove(path)
320 def register_mountpoint(target):
321 """register specified target in a set() for handling clean exiting
323 @target: destination target of mountpoint
329 def unregister_mountpoint(target):
330 """unregister specified target in a set() for handling clean exiting
332 @target: destination target of mountpoint
335 if target in MOUNTED:
336 MOUNTED.remove(target)
339 def get_function_name(obj):
340 """Helper function for use in execute() to retrieve name of a function
342 @obj: the function object
344 if not (isroutine(obj) or isclass(obj)):
346 return obj.__module__ + "." + obj.__name__
350 if not options.rwblockdev:
353 logging.debug("executing: blockdev --setrw %s", device)
354 proc = subprocess.Popen(["blockdev", "--setrw", device])
356 if proc.returncode != 0:
357 raise Exception("error executing blockdev on %s" % device)
360 def execute(f, *exec_arguments):
361 """Wrapper for executing a command. Either really executes
362 the command (default) or when using --dry-run commandline option
363 just displays what would be executed."""
364 # usage: execute(subprocess.Popen, (["ls", "-la"]))
366 # pylint: disable-msg=W0141
368 "dry-run only: %s(%s)",
369 get_function_name(f),
370 ", ".join(map(repr, exec_arguments)),
373 # pylint: disable-msg=W0142
374 return f(*exec_arguments)
378 """Check whether a given file can be executed
380 @fpath: full path to file
382 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
386 """Check whether a given program is available in PATH
388 @program: name of executable"""
389 fpath = os.path.split(program)[0]
394 for path in os.environ["PATH"].split(os.pathsep):
395 exe_file = os.path.join(path, program)
402 def get_defaults_file(iso_mount, flavour, name):
403 """get the default file for syslinux"""
404 bootloader_dirs = ["/boot/isolinux/", "/boot/syslinux/"]
405 for directory in bootloader_dirs:
406 for name in name, "%s_%s" % (get_flavour_filename(flavour), name):
407 if os.path.isfile(iso_mount + directory + name):
408 return (directory, name)
414 search_path="/bin" + os.pathsep + "/usr/bin",
418 """Given a search path, find file
420 @filename: name of file to search for
421 @search_path: path where searching for the specified filename
422 @lst_return: return list of matching files instead one file"""
423 paths = search_path.split(os.pathsep)
424 current_dir = "" # make pylint happy :)
428 """Helper function ffor testing if specified file exists in cwd
430 @cwd: current working directory
432 return os.path.exists(os.path.join(cwd, filename))
436 if match_file(current_dir):
437 retval.append(os.path.abspath(os.path.join(current_dir, filename)))
440 for current_dir, _directories, _files in os.walk(path):
441 if match_file(current_dir):
442 retval.append(os.path.abspath(os.path.join(current_dir, filename)))
446 if required and not retval:
447 raise CriticalException(
448 "Required file %s not found in %s" % (filename, search_path)
459 def check_uid_root():
460 """Check for root permissions"""
461 if not os.geteuid() == 0:
462 raise CriticalException("please run this script with uid 0 (root).")
465 # for usage inside check_boot_flag
466 def get_partition_for_path(path):
469 boot_dev, x = get_device_from_partition(path)
471 d = parted.getDevice(boot_dev)
472 disk = parted.Disk(d)
473 return disk.getPartitionByPath(path)
476 def check_boot_flag(device):
477 if os.path.isdir(device):
479 "Device %s is a directory, skipping check for boot flag." % device
483 boot_dev, x = get_device_from_partition(device)
485 logging.info("Checking for boot flag")
489 part = get_partition_for_path(device)
491 raise HodorException("parted could not find partition")
492 if part.getFlag(parted.PARTITION_BOOT):
493 logging.debug("boot flag is enabled on %s" % device)
496 logging.debug("boot flag is NOT enabled on %s" % device)
497 raise VerifyException(
498 "Device %s does not have the boot flag set. "
499 "Please enable it to be able to boot." % device
502 raise VerifyException(
503 "Could not import parted to verify boot flag on %s, please make sure python3-parted is installed."
508 def mkfs_fat16(device):
509 """Format specified device with VFAT/FAT16 filesystem.
511 @device: partition that should be formated"""
514 logging.info("Would execute mkfs.vfat -F 16 %s now.", device)
517 logging.info("Formating partition with fat16 filesystem")
518 logging.debug("mkfs.vfat -F 16 %s", device)
519 proc = subprocess.Popen(["mkfs.vfat", "-n", "GRML", "-F", "16", device])
521 if proc.returncode != 0:
522 raise CriticalException("error executing mkfs.vfat")
525 def generate_main_syslinux_config():
526 """Generate main configuration for use in syslinux.cfg"""
530 menu label Default boot modes:
539 menu label Additional boot entries for:
541 include additional.cfg
548 include promptname.cfg
551 Jump to old style isolinux prompt
552 featuring further information
553 regarding available boot options.
561 def generate_flavour_specific_syslinux_config(grml_flavour):
562 """Generate flavour specific configuration for use in syslinux.cfg
564 @grml_flavour: name of grml flavour the configuration should be generated for"""
567 menu begin grml %(grml_flavour)s
568 menu title %(display_name)s
570 menu label ^Back to main menu...
573 # include config for boot parameters from disk
574 include %(grml_flavour)s_grml.cfg
578 "grml_flavour": grml_flavour,
579 "display_name": get_flavour_filename(grml_flavour),
583 def install_grub(device):
584 """Install grub on specified device.
586 @mntpoint: mountpoint of device where grub should install its files to
587 @device: partition where grub should be installed to"""
591 "Would execute grub-install [--root-directory=mount_point] %s now.", device
594 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
595 register_tmpfile(device_mountpoint)
597 # If using --grub-mbr then make sure we install grub in MBR instead of PBR
599 logging.debug("Using option --grub-mbr ...")
600 grub_device, x = get_device_from_partition(device)
605 mount(device, device_mountpoint, "")
607 logging.info("Installing grub as bootloader")
608 for opt in ["--", "--force"]:
612 "%s --recheck --no-floppy --target=i386-pc --root-directory=%s %s %s",
618 proc = subprocess.Popen(
624 "--root-directory=%s" % device_mountpoint,
628 stdout=open(os.devnull, "r+"),
631 if proc.returncode == 0:
634 if proc.returncode != 0:
635 # raise Exception("error executing grub-install")
637 "Fatal: error executing grub-install "
638 "(please check the grml2usb FAQ or drop the --grub option)"
641 "Note: if using grub2 consider using "
642 "the --grub-mbr option as grub considers PBR problematic."
646 except CriticalException as error:
647 logging.critical("Fatal: %s", error)
651 unmount(device_mountpoint, "")
652 os.rmdir(device_mountpoint)
653 unregister_tmpfile(device_mountpoint)
656 def install_syslinux(device):
657 """Install syslinux on specified device.
659 @device: partition where syslinux should be installed to"""
662 logging.info("Would install syslinux as bootloader on %s", device)
667 # syslinux -d boot/isolinux /dev/sdb1
668 logging.info("Installing syslinux as bootloader")
669 logging.debug("syslinux -d boot/syslinux %s", device)
670 proc = subprocess.Popen(["syslinux", "-d", "boot/syslinux", device])
672 if proc.returncode != 0:
673 raise CriticalException(
674 "Error executing syslinux (either try --fat16 or use grub?)"
678 def install_bootloader(device):
679 """Install bootloader on specified device.
681 @device: partition where bootloader should be installed to"""
683 # by default we use grub, so install syslinux only on request
687 except CriticalException as error:
688 logging.critical("Fatal: %s", error)
693 install_syslinux(device)
694 except CriticalException as error:
695 logging.critical("Fatal: %s", error)
700 def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
701 """install 'mbr' master boot record (MBR) on a device
703 Retrieve the partition table from "device", install an MBR from the
704 "mbrtemplate" file, set the "partition" (0..3) active, and install the
705 result back to "device".
707 @mbrtemplate: default MBR file (must be a valid MBR file of at least 440
708 (or 439 if ismirbsdmbr) bytes)
710 @device: name of a file assumed to be a hard disc (or USB stick) image, or
711 something like "/dev/sdb"
713 @partition: must be a number between 0 and 3, inclusive
715 @ismirbsdmbr: if true then ignore the active flag, set the mirbsdmbr
716 specific flag to 0/1/2/3 and set the MBR's default value accordingly. If
717 false then leave the mirbsdmbr specific flag set to FFh, set all
718 active flags to 0 and set the active flag of the partition to 80h. Note:
719 behaviour of mirbsdmbr: if flag = 0/1/2/3 then use it, otherwise search for
722 logging.info("Installing default MBR")
724 if not os.path.isfile(mbrtemplate):
726 "Error installing MBR (either try --syslinux-mbr or "
727 'install missing file "%s"?)',
730 raise CriticalException("%s can not be read." % mbrtemplate)
732 if partition is not None and ((partition < 0) or (partition > 3)):
733 logging.warning("Cannot activate partition %d", partition)
741 tmpf = tempfile.NamedTemporaryFile()
743 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1", device, tmpf.name)
744 proc = subprocess.Popen(
745 ["dd", "if=%s" % device, "of=%s" % tmpf.name, "bs=512", "count=1"],
746 stderr=open(os.devnull, "r+"),
749 if proc.returncode != 0:
750 raise Exception("error executing dd (first run)")
753 "executing: dd if=%s of=%s bs=%s count=1 conv=notrunc",
758 proc = subprocess.Popen(
761 "if=%s" % mbrtemplate,
767 stderr=open(os.devnull, "r+"),
770 if proc.returncode != 0:
771 raise Exception("error executing dd (second run)")
773 mbrcode = tmpf.file.read(512)
774 if len(mbrcode) < 512:
775 raise EOFError("MBR size (%d) < 512" % len(mbrcode))
777 if partition is not None:
781 + chr(partition).encode("latin-1")
786 actives = [b"\x00", b"\x00", b"\x00", b"\x00"]
787 actives[partition] = b"\x80"
803 tmpf.file.write(mbrcode)
809 "executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc,fsync",
813 proc = subprocess.Popen(
820 "conv=notrunc,fsync",
822 stderr=open(os.devnull, "r+"),
825 if proc.returncode != 0:
826 raise Exception("error executing dd (third run)")
829 logging.debug("Probing device via 'blockdev --rereadpt %s'", device)
830 proc = subprocess.Popen(["blockdev", "--rereadpt", device])
832 if proc.returncode != 0:
834 "Couldn't execute blockdev on '%s' (install util-linux?)", device
840 def mount(source, target, mount_options):
841 """Mount specified source on given target
843 @source: name of device/ISO that should be mounted
844 @target: directory where the ISO should be mounted to
845 @options: mount specific options"""
847 # note: options.dryrun does not work here, as we have to
848 # locate files and identify the grml flavour
850 for x in open("/proc/mounts", "r").readlines():
851 if x.startswith(source):
852 raise CriticalException(
854 "Error executing mount: {0} already mounted - "
855 "please unmount before invoking grml2usb"
859 if os.path.isdir(source):
860 logging.debug("Source %s is not a device, therefore not mounting.", source)
863 logging.debug("mount %s %s %s", mount_options, source, target)
864 proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target])
866 if proc.returncode != 0:
867 raise CriticalException(
868 "Error executing mount (no filesystem on the partition?)"
871 logging.debug("register_mountpoint(%s)", target)
872 register_mountpoint(target)
875 def unmount(target, unmount_options):
876 """Unmount specified target
878 @target: target where something is mounted on and which should be unmounted
879 @options: options for umount command"""
881 # make sure we unmount only already mounted targets
882 target_unmount = False
883 mounts = open("/proc/mounts").readlines()
884 mountstring = re.compile(".*%s.*" % re.escape(os.path.realpath(target)))
886 if re.match(mountstring, line):
887 target_unmount = True
889 if not target_unmount:
890 logging.debug("%s not mounted anymore", target)
892 logging.debug("umount %s %s", list(unmount_options), target)
893 proc = subprocess.Popen(["umount"] + list(unmount_options) + [target])
895 if proc.returncode != 0:
896 raise Exception("Error executing umount")
898 logging.debug("unregister_mountpoint(%s)", target)
899 unregister_mountpoint(target)
902 def extract_device_name(device):
903 """Extract the device name of a given path
905 @device: device name, like /dev/sda1 or /dev/sda
907 return re.match(r"/dev/(.*?)\d*$", device).group(1)
910 def check_for_usbdevice(device):
911 """Check whether the specified device is a removable USB device
913 @device: device name, like /dev/sda1 or /dev/sda
916 usbdevice = extract_device_name(device)
918 usbdev = os.path.realpath("/sys/class/block/" + usbdevice + "/removable")
919 if not os.path.isfile(usbdev):
920 # Ubuntu with kernel 2.6.24 for example:
921 usbdev = os.path.realpath("/sys/block/" + usbdevice + "/removable")
923 if os.path.isfile(usbdev):
924 is_usb = open(usbdev).readline()
931 def check_for_fat(partition):
932 """Check whether specified partition is a valid VFAT/FAT16 filesystem
934 @partition: device name of partition"""
936 if not os.access(partition, os.R_OK):
937 raise CriticalException(
938 "Failed to read device %s"
939 " (wrong UID/permissions or device/directory not present?)" % partition
944 subprocess.check_output(
945 ["/sbin/blkid", "-s", "TYPE", "-o", "value", partition]
951 if filesystem != "vfat":
952 raise CriticalException(
953 "Partition %s does not contain a FAT16 filesystem. "
954 "(Use --fat16 or run mkfs.vfat %s)" % (partition, partition)
958 raise CriticalException(
959 "Sorry, /sbin/blkid not available (install util-linux?)"
963 def mkdir(directory):
964 """Simple wrapper around os.makedirs to get shell mkdir -p behaviour"""
966 # just silently pass as it's just fine it the directory exists
967 if not os.path.isdir(directory):
969 os.makedirs(directory)
970 # pylint: disable-msg=W0704
975 def exec_rsync(source, target):
976 """Simple wrapper around rsync to install files
978 @source: source file/directory
979 @target: target file/directory"""
980 logging.debug("Source: %s / Target: %s", source, target)
981 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", source, target])
983 if proc.returncode == 12:
984 logging.critical("Fatal: No space left on device")
988 if proc.returncode != 0:
989 logging.critical("Fatal: could not install %s", source)
994 def write_uuid(target_file):
995 """Generates an returns uuid and write it to the specified file
997 @target_file: filename to write the uuid to
1000 fileh = open(target_file, "w")
1001 uid = str(uuid.uuid4())
1007 def get_uuid(target):
1008 """Get the uuid of the specified target. Will generate an uuid if none exist.
1010 @target: directory/mountpoint containing the grml layout
1013 conf_target = target + "/conf/"
1014 uuid_file_name = conf_target + "/bootid.txt"
1015 if os.path.isdir(conf_target):
1016 if os.path.isfile(uuid_file_name):
1017 uuid_file = open(uuid_file_name, "r")
1018 uid = uuid_file.readline().strip()
1022 return write_uuid(uuid_file_name)
1024 execute(mkdir, conf_target)
1025 return write_uuid(uuid_file_name)
1028 def get_shortname(grml_flavour):
1029 """Get shortname based from grml_flavour name. The rules applied are the same as in grml-live
1030 @grml_flavour: flavour name which shold be translated to shortname"""
1032 return re.sub(r"[,._-]", "", grml_flavour)
1035 def copy_system_files(grml_flavour, iso_mount, target):
1036 """copy grml's main files (like squashfs, kernel and initrd) to a given target
1038 @grml_flavour: name of grml flavour the configuration should be generated for
1039 @iso_mount: path where a grml ISO is mounted on
1040 @target: path where grml's main files should be copied to"""
1042 squashfs = search_file(grml_flavour + ".squashfs", iso_mount)
1043 if squashfs is None:
1044 logging.error("error locating squashfs file")
1045 raise CriticalException(
1046 "squashfs file not found, please check that your iso is not corrupt"
1049 squashfs_target = target + "/live/" + grml_flavour + "/"
1050 execute(mkdir, squashfs_target)
1051 exec_rsync(squashfs, squashfs_target + grml_flavour + ".squashfs")
1053 for prefix in grml_flavour + "/", "":
1054 filesystem_module = search_file(prefix + "filesystem.module", iso_mount)
1055 if filesystem_module:
1057 if filesystem_module is None:
1058 logging.error("error locating filesystem.module file")
1059 raise CriticalException("filesystem.module not found")
1061 exec_rsync(filesystem_module, squashfs_target + "filesystem.module")
1063 shortname = get_shortname(grml_flavour)
1064 if os.path.isdir(iso_mount + "/boot/" + shortname):
1065 exec_rsync(iso_mount + "/boot/" + shortname, target + "/boot")
1067 kernel = search_file("vmlinuz", iso_mount)
1069 # compat for releases < 2011.12
1070 kernel = search_file("linux26", iso_mount)
1073 logging.error("error locating kernel file")
1074 raise CriticalException("Kernel not found")
1076 source = os.path.dirname(kernel) + "/"
1077 dest = target + "/" + os.path.dirname(kernel).replace(iso_mount, "") + "/"
1078 execute(mkdir, dest)
1079 exec_rsync(source, dest)
1082 def copy_grml_files(grml_flavour, iso_mount, target):
1083 """copy some minor grml files to a given target
1085 @grml_flavour: the current grml_flavour
1086 @iso_mount: path where a grml ISO is mounted on
1087 @target: path where grml's main files should be copied to"""
1089 grml_target = target + "/grml/"
1090 execute(mkdir, grml_target)
1092 grml_prefixe = ["GRML", "grml"]
1093 for prefix in grml_prefixe:
1094 filename = "{0}/{1}/{2}".format(iso_mount, prefix, grml_flavour)
1095 if os.path.exists(filename):
1096 exec_rsync(filename, grml_target)
1100 "Warning: could not find flavour directory for %s ", grml_flavour
1104 def copy_addons(iso_mount, target):
1105 """copy grml's addons files (like allinoneimg, bsd4grml,..) to a given target
1107 @iso_mount: path where a grml ISO is mounted on
1108 @target: path where grml's main files should be copied to"""
1110 addons = target + "/boot/addons/"
1111 execute(mkdir, addons)
1113 for addon_file in glob.glob(iso_mount + "/boot/addons/*"):
1114 filename = os.path.basename(addon_file)
1115 src_file = iso_mount + "/boot/addons/" + os.path.basename(addon_file)
1116 logging.debug("Copying addon file %s" % filename)
1117 exec_rsync(src_file, addons)
1120 def build_loopbackcfg(target):
1121 """Generate GRUB's loopback.cfg based on existing config files.
1123 @target: target directory
1126 grub_dir = "/boot/grub/"
1127 mkdir(os.path.join(target, grub_dir))
1129 f = open(target + grub_dir + "loopback.cfg", "w")
1131 f.write("# grml2usb generated grub2 configuration file\n")
1132 f.write("source /boot/grub/header.cfg\n")
1134 for defaults in glob.glob(
1135 target + os.path.sep + grub_dir + os.path.sep + "*_default.cfg"
1137 sourcefile = defaults.split(target + os.path.sep)[1]
1138 logging.debug("Found source file" + sourcefile)
1139 os.path.isfile(defaults) and f.write("source " + sourcefile + "\n")
1141 for ops in glob.glob(
1142 target + os.path.sep + grub_dir + os.path.sep + "*_options.cfg"
1144 sourcefile = ops.split(target + os.path.sep)[1]
1145 logging.debug("Found source file" + sourcefile)
1146 os.path.isfile(ops) and f.write("source " + sourcefile + "\n")
1148 f.write("source /boot/grub/addons.cfg\n")
1149 f.write("source /boot/grub/footer.cfg\n")
1153 def glob_and_copy(filepattern, dst):
1154 """Glob on specified filepattern and copy the result to dst
1156 @filepattern: globbing pattern
1157 @dst: target directory
1159 for name in glob.glob(filepattern):
1160 copy_if_exist(name, dst)
1163 def search_and_copy(filename, search_path, dst):
1164 """Search for the specified filename at searchpath and copy it to dst
1166 @filename: filename to look for
1167 @search_path: base search file
1168 @dst: destionation to copy the file to
1170 file_location = search_file(filename, search_path)
1171 copy_if_exist(file_location, dst)
1174 def copy_if_exist(filename, dst):
1175 """Copy filename to dst if filename is set.
1177 @filename: a filename
1180 if filename and (os.path.isfile(filename) or os.path.isdir(filename)):
1181 exec_rsync(filename, dst)
1184 def copy_bootloader_files(iso_mount, target, grml_flavour):
1185 """Copy grml's bootloader files to a given target
1187 @iso_mount: path where a grml ISO is mounted on
1188 @target: path where grml's main files should be copied to
1189 @grml_flavour: name of the current processed grml_flavour
1192 syslinux_target = target + "/boot/syslinux/"
1193 execute(mkdir, syslinux_target)
1195 grub_target = target + "/boot/grub/"
1196 execute(mkdir, grub_target)
1198 logo = search_file("logo.16", iso_mount, required=True)
1199 exec_rsync(logo, syslinux_target + "logo.16")
1201 bootx64_efi = search_file("bootx64.efi", iso_mount)
1203 mkdir(target + "/efi/boot/")
1204 exec_rsync(bootx64_efi, target + "/efi/boot/bootx64.efi")
1206 efi_img = search_file("efi.img", iso_mount)
1208 mkdir(target + "/boot/")
1209 exec_rsync(efi_img, target + "/boot/efi.img")
1210 handle_secure_boot(target, efi_img)
1212 execute(mkdir, target + "/conf/")
1213 glob_and_copy(iso_mount + "/conf/bootfile_*", target + "/conf/")
1215 for ffile in ["f%d" % number for number in range(1, 11)]:
1216 search_and_copy(ffile, iso_mount, syslinux_target + ffile)
1218 # avoid the "file is read only, overwrite anyway (y/n) ?" question
1219 # of mtools by syslinux ("mmove -D o -D O s:/ldlinux.sys $target_file")
1220 if os.path.isfile(syslinux_target + "ldlinux.sys"):
1221 os.unlink(syslinux_target + "ldlinux.sys")
1223 (source_dir, name) = get_defaults_file(iso_mount, grml_flavour, "default.cfg")
1224 (source_dir, defaults_file) = get_defaults_file(iso_mount, grml_flavour, "grml.cfg")
1227 raise CriticalException(
1228 "file default.cfg could not be found.\n"
1229 "Note: this grml2usb version requires an ISO generated by grml-live >=0.9.24 ...\n"
1230 " ... either use grml releases >=2009.10 or switch to an older grml2usb version."
1233 if not os.path.exists(iso_mount + "/boot/grub/footer.cfg"):
1235 "Warning: Grml releases older than 2011.12 support only one flavour in grub."
1253 glob_and_copy(iso_mount + source_dir + expr, syslinux_target)
1255 for modules_dir in options.syslinuxlibs + SYSLINUX_LIBS:
1256 if os.path.isdir(modules_dir):
1257 for filename in glob.glob1(syslinux_target, "*.c32"):
1258 copy_if_exist(os.path.join(modules_dir, filename), syslinux_target)
1261 # copy the addons_*.cfg file to the new syslinux directory
1262 glob_and_copy(iso_mount + source_dir + "addon*.cfg", syslinux_target)
1265 "hidden.cfg", iso_mount + source_dir, syslinux_target + "new_" + "hidden.cfg"
1268 # copy all grub files from ISO
1269 glob_and_copy(iso_mount + "/boot/grub/*", grub_target)
1271 # finally (after all GRUB files have been installed) build static loopback.cfg
1272 build_loopbackcfg(target)
1275 def install_iso_files(grml_flavour, iso_mount, device, target):
1276 """Copy files from ISO to given target
1278 @grml_flavour: name of grml flavour the configuration should be generated for
1279 @iso_mount: path where a grml ISO is mounted on
1280 @device: device/partition where bootloader should be installed to
1281 @target: path where grml's main files should be copied to"""
1284 GRML_DEFAULT = GRML_DEFAULT or grml_flavour
1287 elif not options.bootloaderonly:
1288 logging.info("Copying files. This might take a while....")
1290 copy_system_files(grml_flavour, iso_mount, target)
1291 copy_grml_files(grml_flavour, iso_mount, target)
1292 except CriticalException as error:
1293 logging.critical("Execution failed: %s", error)
1296 if not options.skipaddons:
1297 if not search_file("addons", iso_mount):
1298 logging.info("Could not find addons, therefore not installing.")
1300 copy_addons(iso_mount, target)
1302 if not options.copyonly:
1303 copy_bootloader_files(iso_mount, target, grml_flavour)
1305 if not options.dryrun:
1306 handle_bootloader_config(grml_flavour, device, target)
1308 # make sure we sync filesystems before returning
1309 logging.info("Synching data (this might take a while)")
1310 proc = subprocess.Popen(["sync"])
1314 def get_device_from_partition(partition):
1316 partition_number = None
1317 if partition[-1].isdigit() and not RE_LOOP_DEVICE.match(partition):
1318 m = RE_P_PARTITION.match(partition) or RE_PARTITION.match(partition)
1321 partition_number = int(m.group(2)) - 1
1322 return (device, partition_number)
1325 def get_flavour(flavour_str):
1326 """Returns the flavour of a grml version string"""
1327 return re.match(r"[\w-]*", flavour_str).group()
1330 def identify_grml_flavour(mountpath):
1331 """Get name of grml flavour
1333 @mountpath: path where the grml ISO is mounted to
1334 @return: name of grml-flavour"""
1336 version_files = search_file("grml-version", mountpath, lst_return=True)
1338 if not version_files:
1339 if mountpath.startswith("/run/live/medium"):
1340 logging.critical("Error: could not find grml-version file.")
1342 "Looks like your system is running from RAM but required files are not available."
1345 "Please either boot without toram=... or use boot option toram instead of toram=..."
1348 logging.critical("Error: could not find grml-version file.")
1353 logging.debug("version_files = %s", version_files)
1354 for version_file in version_files:
1357 tmpfile = open(version_file, "r")
1358 for line in tmpfile.readlines():
1359 flavours.append(get_flavour(line))
1367 def get_bootoptions(grml_flavour):
1368 """Returns bootoptions for specific flavour
1370 @grml_flavour: name of the grml_flavour
1372 # do NOT write "None" in kernel cmdline
1373 if not options.bootoptions:
1376 bootopt = " ".join(options.bootoptions)
1377 bootopt = bootopt.replace("%flavour", grml_flavour)
1381 def handle_grub_config(grml_flavour, device, target):
1382 """Main handler for generating grub (v1 and v2) configuration
1384 @grml_flavour: name of grml flavour the configuration should be generated for
1385 @device: device/partition where grub should be installed to
1386 @target: path of grub's configuration files"""
1390 logging.debug("Updating grub configuration")
1392 grub_target = target + "/boot/grub/"
1394 bootid_re = re.compile(r"bootid=[\w_-]+")
1395 live_media_path_re = re.compile(r"live-media-path=[\w_/-]+")
1397 bootopt = get_bootoptions(grml_flavour)
1400 option_re = re.compile(r"(.*/boot/.*(linux26|vmlinuz).*)")
1402 if options.removeoption:
1403 for regex in options.removeoption:
1404 remove_regexes.append(re.compile(regex))
1406 shortname = get_shortname(grml_flavour)
1407 for filename in glob.glob(grub_target + "*.cfg"):
1408 for line in fileinput.input(filename, inplace=1):
1409 line = line.rstrip("\r\n")
1410 if option_re.search(line):
1411 line = bootid_re.sub("", line)
1412 if shortname in filename:
1413 line = live_media_path_re.sub("", line)
1414 line = line.rstrip() + " live-media-path=/live/%s/ " % (
1418 line = line.replace(" {} ".format(bootopt.strip()), " ")
1419 if line.endswith(bootopt):
1420 line = line[: -len(bootopt)]
1421 line = line.rstrip() + r" bootid=%s %s " % (UUID, bootopt)
1422 for regex in remove_regexes:
1423 line = regex.sub(" ", line)
1428 def initial_syslinux_config(target):
1429 """Generates initial syslinux configuration
1431 @target path of syslinux's configuration files"""
1433 target = target + "/"
1434 filename = target + "grmlmain.cfg"
1435 if os.path.isfile(target + "grmlmain.cfg"):
1437 data = open(filename, "w")
1438 data.write(generate_main_syslinux_config())
1441 filename = target + "hiddens.cfg"
1442 data = open(filename, "w")
1443 data.write("include hidden.cfg\n")
1447 def add_entry_if_not_present(filename, entry):
1448 """Write entry into filename if entry is not already in the file
1450 @filename: name of the file
1451 @entry: data to write to the file
1453 data = open(filename, "a+")
1464 def get_flavour_filename(flavour):
1465 """Generate a iso9960 save filename out of the specified flavour
1467 @flavour: grml flavour
1469 return flavour.replace("-", "_")
1472 def adjust_syslinux_bootoptions(src, flavour):
1473 """Adjust existing bootoptions of specified syslinux config to
1474 grml2usb specific ones, e.g. change the location of the kernel...
1476 @src: config file to alter
1477 @flavour: grml flavour
1480 append_re = re.compile(r"^(\s*append.*/boot/.*)$", re.I)
1481 # flavour_re = re.compile("(label.*)(grml\w+)")
1482 default_re = re.compile(r"(default.cfg)")
1483 bootid_re = re.compile(r"bootid=[\w_-]+")
1484 live_media_path_re = re.compile(r"live-media-path=[\w_/-]+")
1486 bootopt = get_bootoptions(flavour)
1490 if options.removeoption:
1491 option_re = re.compile(r"/boot/.*/(initrd.gz|initrd.img)")
1493 for regex in options.removeoption:
1494 regexe.append(re.compile(r"%s" % regex))
1496 for line in fileinput.input(src, inplace=1):
1497 # line = flavour_re.sub(r'\1 %s-\2' % flavour, line)
1498 line = default_re.sub(r"%s-\1" % flavour, line)
1499 line = bootid_re.sub("", line)
1500 line = live_media_path_re.sub("", line)
1501 line = append_re.sub(r"\1 live-media-path=/live/%s/ " % flavour, line)
1502 line = append_re.sub(r"\1 boot=live %s " % bootopt, line)
1503 line = append_re.sub(r"\1 %s=%s " % ("bootid", UUID), line)
1504 if option_re and option_re.search(line):
1505 for regex in regexe:
1506 line = regex.sub(" ", line)
1507 sys.stdout.write(line)
1511 def adjust_labels(src, replacement):
1512 """Adjust the specified labels in the syslinux config file src with
1513 specified replacement
1515 label_re = re.compile(r"^(\s*label\s*) ([a-zA-Z0-9_-]+)", re.I)
1516 for line in fileinput.input(src, inplace=1):
1517 line = label_re.sub(replacement, line)
1518 sys.stdout.write(line)
1522 def add_syslinux_entry(filename, grml_flavour):
1523 """Add includes for a specific grml_flavour to the specified filename
1525 @filename: syslinux config file
1526 @grml_flavour: grml flavour to add
1529 entry_filename = "option_%s.cfg" % grml_flavour
1530 entry = "include %s\n" % entry_filename
1532 add_entry_if_not_present(filename, entry)
1533 path = os.path.dirname(filename)
1535 data = open(path + "/" + entry_filename, "w")
1536 data.write(generate_flavour_specific_syslinux_config(grml_flavour))
1540 def modify_filenames(grml_flavour, target, filenames):
1541 """Replace the standard filenames with the new ones
1543 @grml_flavour: grml-flavour strin
1544 @target: directory where the files are located
1545 @filenames: list of filenames to alter
1547 grml_filename = get_flavour_filename(grml_flavour)
1548 for filename in filenames:
1549 old_filename = "%s/%s" % (target, filename)
1550 new_filename = "%s/%s_%s" % (target, grml_filename, filename)
1551 os.rename(old_filename, new_filename)
1554 def remove_default_entry(filename):
1555 """Remove the default entry from specified syslinux file
1557 @filename: syslinux config file
1559 default_re = re.compile(r"^(\s*menu\s*default\s*)$", re.I)
1560 for line in fileinput.input(filename, inplace=1):
1561 if default_re.match(line):
1563 sys.stdout.write(line)
1567 def handle_syslinux_config(grml_flavour, target):
1568 """Main handler for generating syslinux configuration
1570 @grml_flavour: name of grml flavour the configuration should be generated for
1571 @target: path of syslinux's configuration files"""
1573 logging.debug("Generating syslinux configuration")
1574 syslinux_target = target + "/boot/syslinux/"
1575 # should be present via copy_bootloader_files(), but make sure it exits:
1576 execute(mkdir, syslinux_target)
1577 syslinux_cfg = syslinux_target + "syslinux.cfg"
1579 # install main configuration only *once*, no matter how many ISOs we have:
1580 syslinux_config_file = open(syslinux_cfg, "w")
1581 syslinux_config_file.write("timeout 300\n")
1582 syslinux_config_file.write("include vesamenu.cfg\n")
1583 syslinux_config_file.close()
1585 prompt_name = open(syslinux_target + "promptname.cfg", "w")
1586 prompt_name.write("menu label S^yslinux prompt\n")
1589 initial_syslinux_config(syslinux_target)
1590 flavour_filename = get_flavour_filename(grml_flavour)
1592 if search_file("default.cfg", syslinux_target):
1593 modify_filenames(grml_flavour, syslinux_target, ["grml.cfg", "default.cfg"])
1595 filename = search_file("new_hidden.cfg", syslinux_target)
1597 # process hidden file
1598 if not search_file("hidden.cfg", syslinux_target):
1599 new_hidden = syslinux_target + "hidden.cfg"
1600 os.rename(filename, new_hidden)
1601 adjust_syslinux_bootoptions(new_hidden, grml_flavour)
1603 new_hidden_file = "%s/%s_hidden.cfg" % (syslinux_target, flavour_filename)
1604 os.rename(filename, new_hidden_file)
1605 adjust_labels(new_hidden_file, r"\1 %s-\2" % grml_flavour)
1606 adjust_syslinux_bootoptions(new_hidden_file, grml_flavour)
1607 entry = "include %s_hidden.cfg\n" % flavour_filename
1608 add_entry_if_not_present("%s/hiddens.cfg" % syslinux_target, entry)
1610 new_default = "%s_default.cfg" % (flavour_filename)
1611 entry = "include %s\n" % new_default
1612 defaults_file = "%s/defaults.cfg" % syslinux_target
1613 new_default_with_path = "%s/%s" % (syslinux_target, new_default)
1614 new_grml_cfg = "%s/%s_grml.cfg" % (syslinux_target, flavour_filename)
1616 if os.path.isfile(defaults_file):
1618 # remove default menu entry in menu
1619 remove_default_entry(new_default_with_path)
1621 # adjust all labels for additional isos
1622 adjust_labels(new_default_with_path, r"\1 %s" % grml_flavour)
1623 adjust_labels(new_grml_cfg, r"\1 %s-\2" % grml_flavour)
1625 # always adjust bootoptions
1626 adjust_syslinux_bootoptions(new_default_with_path, grml_flavour)
1627 adjust_syslinux_bootoptions(new_grml_cfg, grml_flavour)
1629 add_entry_if_not_present("%s/defaults.cfg" % syslinux_target, entry)
1631 add_syslinux_entry("%s/additional.cfg" % syslinux_target, flavour_filename)
1634 def handle_secure_boot(target, efi_img):
1635 """Provide secure boot support by extracting files from /boot/efi.img
1637 @target: path where grml's main files should be copied to
1638 @efi_img: path to the efi.img file that includes the files for secure boot
1641 mkdir(target + "/efi/boot/")
1642 efi_mountpoint = tempfile.mkdtemp(
1643 prefix="grml2usb", dir=os.path.abspath(options.tmpdir)
1645 logging.debug("efi_mountpoint = %s" % efi_mountpoint)
1646 register_tmpfile(efi_mountpoint)
1650 "mount(%s, %s, ['-o', 'ro', '-t', 'vfat']" % (efi_img, efi_mountpoint)
1652 mount(efi_img, efi_mountpoint, ["-o", "ro", "-t", "vfat"])
1653 except CriticalException as error:
1654 logging.critical("Fatal: %s", error)
1657 grub_cfg = search_file("grub.cfg", efi_mountpoint + "/boot/grub/")
1658 logging.debug("grub_cfg = %s" % grub_cfg)
1661 "No /boot/grub/grub.cfg found inside EFI image, looks like Secure Boot support is missing."
1664 mkdir(target + "/boot/grub/x86_64-efi/")
1666 "exec_rsync(%s, %s + '/boot/grub/x86_64-efi/grub.cfg')" % (grub_cfg, target)
1668 exec_rsync(grub_cfg, target + "/boot/grub/x86_64-efi/grub.cfg")
1671 "exec_rsync(%s + '/EFI/BOOT/grubx64.efi', %s + '/efi/boot/grubx64.efi')'"
1672 % (efi_mountpoint, target)
1675 efi_mountpoint + "/EFI/BOOT/grubx64.efi", target + "/efi/boot/grubx64.efi"
1678 # NOTE - we're overwriting /efi/boot/bootx64.efi from copy_bootloader_files here
1680 "exec_rsync(%s + '/EFI/BOOT/bootx64.efi', %s + '/efi/boot/bootx64.efi')'"
1681 % (efi_mountpoint, target)
1684 efi_mountpoint + "/EFI/BOOT/bootx64.efi", target + "/efi/boot/bootx64.efi"
1688 unmount(efi_mountpoint, "")
1689 logging.debug("Unmounted %s" % efi_mountpoint)
1690 os.rmdir(efi_mountpoint)
1691 logging.debug("Removed directory %s" % efi_mountpoint)
1693 logging.critical("RuntimeError while umount %s" % efi_mountpoint)
1697 def handle_bootloader_config(grml_flavour, device, target):
1698 """Main handler for generating bootloader's configuration
1700 @grml_flavour: name of grml flavour the configuration should be generated for
1701 @device: device/partition where bootloader should be installed to
1702 @target: path of bootloader's configuration files"""
1705 UUID = get_uuid(target)
1706 if options.skipsyslinuxconfig:
1707 logging.info("Skipping generation of syslinux configuration as requested.")
1710 handle_syslinux_config(grml_flavour, target)
1711 except CriticalException as error:
1712 logging.critical("Fatal: %s", error)
1715 if options.skipgrubconfig:
1716 logging.info("Skipping generation of grub configuration as requested.")
1719 handle_grub_config(grml_flavour, device, target)
1720 except CriticalException as error:
1721 logging.critical("Fatal: %s", error)
1725 def install(image, device):
1726 """Install a grml image to the specified device
1728 @image: directory or is file
1729 @device: partition or directory to install the device
1731 iso_mountpoint = image
1732 remove_image_mountpoint = False
1733 if os.path.isdir(image):
1734 if options.force or os.path.exists(os.path.join(image, "live")):
1735 logging.info("Using %s as install base", image)
1738 "%s does not look like a Grml system. "
1739 "Do you really want to use this image? y/N " % image
1741 if q.lower() == "y":
1742 logging.info("Using %s as install base", image)
1744 logging.info("Skipping install base %s", image)
1746 logging.info("Using ISO %s", image)
1747 iso_mountpoint = tempfile.mkdtemp(
1748 prefix="grml2usb", dir=os.path.abspath(options.tmpdir)
1750 register_tmpfile(iso_mountpoint)
1751 remove_image_mountpoint = True
1753 mount(image, iso_mountpoint, ["-o", "loop,ro", "-t", "iso9660"])
1754 except CriticalException as error:
1755 logging.critical("Fatal: %s", error)
1759 install_grml(iso_mountpoint, device)
1761 if remove_image_mountpoint:
1763 remove_mountpoint(iso_mountpoint)
1764 except CriticalException:
1769 def install_grml(mountpoint, device):
1770 """Main logic for copying files of the currently running Grml system.
1772 @mountpoint: directory where currently running live system resides (usually /run/live/medium)
1773 @device: partition where the specified ISO should be installed to"""
1775 device_mountpoint = device
1776 if os.path.isdir(device):
1777 logging.info("Specified device is a directory, therefore not mounting.")
1778 remove_device_mountpoint = False
1780 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1781 register_tmpfile(device_mountpoint)
1782 remove_device_mountpoint = True
1785 mount(device, device_mountpoint, ["-o", "utf8,iocharset=iso8859-1"])
1786 except CriticalException:
1787 mount(device, device_mountpoint, "")
1789 grml_flavours = identify_grml_flavour(mountpoint)
1790 for flavour in set(grml_flavours):
1792 logging.warning("No valid flavour found, please check your iso")
1793 logging.info('Identified grml flavour "%s".', flavour)
1794 install_iso_files(flavour, mountpoint, device, device_mountpoint)
1795 GRML_FLAVOURS.add(flavour)
1797 if remove_device_mountpoint:
1798 remove_mountpoint(device_mountpoint)
1801 def remove_mountpoint(mountpoint):
1802 """remove a registered mountpoint"""
1805 unmount(mountpoint, "")
1806 if os.path.isdir(mountpoint):
1807 os.rmdir(mountpoint)
1808 unregister_tmpfile(mountpoint)
1809 except CriticalException:
1814 def handle_mbr(device):
1815 """Main handler for installing master boot record (MBR)
1817 @device: device where the MBR should be installed to"""
1820 logging.info("Would install MBR")
1823 mbr_device, partition_number = get_device_from_partition(device)
1824 if partition_number is None:
1825 logging.warning("Could not detect partition number, not activating partition")
1827 # if we get e.g. /dev/loop1 as device we don't want to put the MBR
1828 # into /dev/loop of course, therefore use /dev/loop1 as mbr_device
1829 if mbr_device == "/dev/loop":
1832 "Detected loop device - using %s as MBR device therefore", mbr_device
1835 mbrcode = GRML2USB_BASE + "/mbr/mbrldr"
1836 if options.syslinuxmbr:
1839 "/usr/lib/syslinux/mbr.bin",
1840 "/usr/lib/syslinux/bios/mbr.bin",
1841 "/usr/share/syslinux/mbr.bin",
1843 for mbrpath in mbr_locations:
1844 if os.path.isfile(mbrpath):
1849 str_locations = " or ".join(['"%s"' % x for x in mbr_locations])
1850 logging.error("Cannot find syslinux MBR, install it at %s)", str_locations)
1851 raise CriticalException(
1852 "syslinux MBR can not be found at %s." % str_locations
1854 elif options.mbrmenu:
1855 mbrcode = GRML2USB_BASE + "/mbr/mbrldr"
1858 install_mbr(mbrcode, mbr_device, partition_number, True)
1859 except (IOError, Exception) as error:
1860 logging.critical("Execution failed: %s", error)
1864 def handle_vfat(device):
1865 """Check for FAT specific settings and options
1867 @device: device that should checked / formated"""
1869 # make sure we have mkfs.vfat available
1871 if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
1872 logging.critical("Sorry, mkfs.vfat not available. Exiting.")
1873 logging.critical("Please make sure to install dosfstools.")
1877 print("Forcing mkfs.fat16 on %s as requested via option --force." % device)
1879 # make sure the user is aware of what he is doing
1881 "Are you sure you want to format the specified partition with fat16? y/N "
1883 if f == "y" or f == "Y":
1885 "Note: you can skip this question using the option --force"
1891 except CriticalException as error:
1892 logging.critical("Execution failed: %s", error)
1895 # check for vfat filesystem
1896 if device is not None and not os.path.isdir(device) and options.syslinux:
1898 check_for_fat(device)
1899 except CriticalException as error:
1900 logging.critical("Execution failed: %s", error)
1903 if options.skipusbcheck:
1905 "Not checking for removable USB device as requested via option --skip-usb-check."
1910 not os.path.isdir(device)
1911 and not check_for_usbdevice(device)
1912 and not options.force
1915 "Warning: the specified device %s does not look like a removable usb device."
1918 f = input("Do you really want to continue? y/N ")
1919 if f.lower() != "y":
1923 def handle_compat_warning(device):
1924 """Backwards compatible checks
1926 @device: device that should be checked"""
1928 # make sure we can replace old grml2usb script and warn user when using old way of life:
1930 device.startswith("/mnt/external")
1931 or device.startswith("/mnt/usb")
1932 and not options.force
1934 print("Warning: the semantics of grml2usb has changed.")
1935 print("Instead of using grml2usb /path/to/iso %s you might" % device)
1936 print("want to use grml2usb /path/to/iso /dev/... instead.")
1937 print("Please check out the grml2usb manpage for details.")
1938 f = input("Do you really want to continue? y/N ")
1939 if f.lower() != "y":
1943 def handle_logging():
1944 """Log handling and configuration"""
1946 if options.verbose and options.quiet:
1947 parser.error("please use either verbose (--verbose) or quiet (--quiet) option")
1949 FORMAT = "%(message)s"
1951 FORMAT = "%(asctime)-15s %(message)s"
1952 logging.basicConfig(level=logging.DEBUG, format=FORMAT)
1954 logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
1956 logging.basicConfig(level=logging.INFO, format=FORMAT)
1959 def handle_bootloader(device):
1960 """wrapper for installing bootloader
1962 @device: device where bootloader should be installed to"""
1964 # Install bootloader only if not using the --copy-only option
1965 if options.copyonly:
1967 "Not installing bootloader and its files as requested via option copyonly."
1969 elif os.path.isdir(device):
1970 logging.info("Not installing bootloader as %s is a directory.", device)
1972 install_bootloader(device)
1975 def check_options(opts):
1976 """Check compatibility of provided user opts
1978 @opts option dict from OptionParser
1980 if opts.grubmbr and not opts.grub:
1981 raise CriticalException("--grub-mbr requires --grub option.")
1983 if opts.copyonly and opts.grub:
1984 raise CriticalException("Cannot use --copy-only and --grub at the same time.")
1987 def check_programs():
1988 """check if all needed programs are installed"""
1991 GRUB_INSTALL = which("grub-install") or which("grub2-install")
1992 if not GRUB_INSTALL:
1994 "Fatal: grub-install not available (please install the "
1995 "grub package or drop the --grub option)"
1999 if options.syslinux:
2000 if not which("syslinux"):
2002 "Fatal: syslinux not available (please install the "
2003 "syslinux package or use the --grub option)"
2007 if not which("rsync"):
2008 logging.critical("Fatal: rsync not available, can not continue - sorry.")
2013 """Runs modprobe loop and throws away its output"""
2014 if not which("modprobe"):
2015 logging.critical("Fatal: modprobe not available, can not continue - sorry.")
2016 logging.critical("Hint: is /sbin missing in PATH?")
2019 proc = subprocess.Popen(
2020 ["modprobe", "loop"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
2026 """Main invocation"""
2030 print(os.path.basename(sys.argv[0]) + " " + PROG_VERSION)
2034 parser.error("invalid usage")
2039 # make sure we have the appropriate permissions
2042 check_options(options)
2046 logging.info("Executing grml2usb version %s", PROG_VERSION)
2049 logging.info("Running in simulation mode as requested via option dry-run.")
2053 # specified arguments
2054 device = os.path.realpath(args[len(args) - 1])
2055 isos = args[0 : len(args) - 1]
2057 if not os.path.isdir(device):
2058 if device[-1:].isdigit():
2059 if int(device[-1:]) > 4 or device[-2:].isdigit():
2061 "Warning: installing on partition number >4, booting *might* fail depending on your system."
2064 # provide upgrade path
2065 handle_compat_warning(device)
2067 if not options.skipbootflag:
2068 check_boot_flag(device)
2070 # check for vfat partition
2073 # main operation (like installing files)
2075 install(iso, device)
2078 is_superfloppy = not device[-1:].isdigit()
2080 logging.info("Detected superfloppy format - not installing MBR")
2082 if not options.skipmbr and not os.path.isdir(device) and not is_superfloppy:
2085 handle_bootloader(device)
2088 "Note: grml flavour %s was installed as the default booting system.",
2092 for flavour in GRML_FLAVOURS:
2094 "Note: you can boot flavour %s using '%s' on the commandline.",
2099 # finally be polite :)
2101 "Finished execution of grml2usb (%s). Have fun with your Grml system.",
2105 except Exception as error:
2106 logging.critical("Fatal: %s", str(error))
2108 logging.exception("Exception:")
2112 if __name__ == "__main__":
2115 except KeyboardInterrupt:
2116 logging.info("Received KeyboardInterrupt")
2119 # END OF FILE ##################################################################
2120 # vim:foldmethod=indent expandtab ai ft=python tw=120 fileencoding=utf-8