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/
31 from inspect import isclass, isroutine
32 from optparse import OptionParser
34 # The line following this line is patched by debian/rules and tarball.sh.
35 PROG_VERSION = "***UNKNOWN***"
37 # when running from inside git, try to report version information via git-describe
39 git_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
40 with open(os.devnull, "w") as devnull:
42 subprocess.check_output(
43 ["git", "-C", git_dir, "describe", "--always", "--dirty"],
47 .decode("utf-8", errors="replace")
54 MOUNTED = set() # register mountpoints
55 TMPFILES = set() # register tmpfiles
56 DATESTAMP = time.mktime(
57 datetime.datetime.now().timetuple()
58 ) # unique identifier for syslinux.cfg
59 GRML_FLAVOURS = set() # which flavours are being installed?
63 "/usr/lib/syslinux/modules/bios/", # Debian
64 "/usr/lib/syslinux/bios/", # Arch Linux
66 GPT_HEADER = b"\x55\xaa\x45\x46\x49\x20\x50\x41\x52\x54" # original GPT header
69 RE_PARTITION = re.compile(r"([a-z/]*?)(\d+)$")
70 RE_P_PARTITION = re.compile(r"(.*?\d+)p(\d+)$")
71 RE_LOOP_DEVICE = re.compile(r"/dev/loop\d+$")
74 def syslinux_warning(option, opt, value, opt_parser):
75 """A helper function for printing a warning about deprecated option
77 # pylint: disable-msg=W0613
79 "Note: the --syslinux option is deprecated as syslinux "
80 "is grml2usb's default. Continuing anyway.\n"
82 setattr(opt_parser.values, option.dest, True)
85 # if grub option is set, unset syslinux option
86 def grub_option(option, opt, value, opt_parser):
87 """A helper function adjusting other option values
89 # pylint: disable-msg=W0613
90 setattr(opt_parser.values, option.dest, True)
91 setattr(opt_parser.values, "syslinux", False)
95 USAGE = "Usage: %prog [options] <[ISO[s] | /run/live/medium]> </dev/sdX#>\n\
97 %prog installs Grml ISO[s] to an USB device to be able to boot from it.\n\
98 Make sure you have at least one Grml ISO or a running Grml system (/run/live/medium),\n\
99 grub or syslinux and root access.\n\
101 Run %prog --help for usage hints, further information via: man grml2usb"
103 # pylint: disable-msg=C0103
104 # pylint: disable-msg=W0603
105 parser = OptionParser(usage=USAGE)
111 help="use specified bootoptions as default",
115 dest="bootloaderonly",
117 help="do not copy files but just install a bootloader",
123 help="copy files only but do not install bootloader",
126 "--dry-run", dest="dryrun", action="store_true", help="avoid executing commands"
132 help="format specified partition with FAT16",
138 help="force any actions requiring manual interaction",
144 callback=grub_option,
145 help="install grub bootloader instead of (default) syslinux",
151 help="install grub into MBR instead of (default) PBR",
157 help="enable interactive boot menu in MBR",
163 help="do not output anything but just errors on console",
166 "--remove-bootoption",
169 help="regex for removing existing bootoptions",
175 help="enforce read-write mode on involved block devices",
181 help="do not install /boot/addons/ files",
187 help="do not try to check whether the destination has the bootflag set",
190 "--skip-grub-config",
191 dest="skipgrubconfig",
193 help="skip generation of grub configuration files",
199 help="do not install a master boot record (MBR) on the device",
202 "--skip-syslinux-config",
203 dest="skipsyslinuxconfig",
205 help="skip generation of syslinux configuration files",
211 help="skip check to verify whether given device is removable",
218 callback=syslinux_warning,
219 help="install syslinux bootloader (deprecated as it's the default)",
225 help="install syslinux master boot record (MBR) instead of default",
232 help="syslinux modules directory path",
238 help="directory to be used for temporary files",
241 "--verbose", dest="verbose", action="store_true", help="enable verbose mode"
248 help="display version and exit",
250 (options, args) = parser.parse_args()
253 GRML2USB_BASE = "/usr/share/grml2usb"
254 if not os.path.isdir(GRML2USB_BASE):
255 GRML2USB_BASE = os.path.dirname(os.path.realpath(__file__))
258 class HodorException(Exception):
259 """Throw exception if the exact error is not known and not fatal.
261 @Exception: message"""
264 class CriticalException(Exception):
265 """Throw critical exception if the exact error is not known but fatal.
267 @Exception: message"""
270 class VerifyException(Exception):
271 """Throw critical exception if there is an fatal error when verifying something.
273 @Exception: message"""
276 # The following two functions help to operate on strings as
277 # array (list) of bytes (octets). In Python 3000, the bytes
278 # datatype will need to be used. This is intended for using
279 # with manipulation of files on the octet level, like shell
280 # arrays, e.g. in MBR creation.
283 def array2string(*a):
284 """Convert a list of integers [0;255] to a string."""
285 return struct.pack("%sB" % len(a), *a)
289 """Convert a (bytes) string into a list of integers."""
290 return struct.unpack("%sB" % len(s), s)
294 """Cleanup function to make sure there aren't any mounted devices left behind.
297 def del_failed(fn, path, exc):
298 logging.warn("Deletion of %s failed in temporary folder" % path)
300 logging.info("Cleaning up before exiting...")
301 proc = subprocess.Popen(["sync"])
304 for device in MOUNTED.copy():
307 logging.debug("Unmounted %s" % device)
309 logging.debug("RuntimeError while umount %s, ignoring" % device)
311 for tmppath in TMPFILES.copy():
313 if os.path.isdir(tmppath) and not os.path.islink(tmppath):
314 # symbolic links to directories are ignored
315 # without the check it will throw an OSError
316 shutil.rmtree(tmppath, onerror=del_failed)
317 logging.debug("temporary directory %s deleted" % tmppath)
318 unregister_tmpfile(tmppath)
321 logging.debug("temporary file %s deleted" % tmppath)
322 unregister_tmpfile(tmppath)
324 msg = "RuntimeError while removing temporary %s, ignoring"
325 logging.debug(msg % tmppath)
328 def register_tmpfile(path):
336 def unregister_tmpfile(path):
338 remove registered tmpfile
342 TMPFILES.remove(path)
347 def register_mountpoint(target):
348 """register specified target in a set() for handling clean exiting
350 @target: destination target of mountpoint
356 def unregister_mountpoint(target):
357 """unregister specified target in a set() for handling clean exiting
359 @target: destination target of mountpoint
362 if target in MOUNTED:
363 MOUNTED.remove(target)
366 def get_function_name(obj):
367 """Helper function for use in execute() to retrieve name of a function
369 @obj: the function object
371 if not (isroutine(obj) or isclass(obj)):
373 return obj.__module__ + "." + obj.__name__
377 if not options.rwblockdev:
380 logging.debug("executing: blockdev --setrw %s", device)
381 proc = subprocess.Popen(["blockdev", "--setrw", device])
383 if proc.returncode != 0:
384 raise Exception("error executing blockdev on %s" % device)
387 def execute(f, *exec_arguments):
388 """Wrapper for executing a command. Either really executes
389 the command (default) or when using --dry-run commandline option
390 just displays what would be executed."""
391 # usage: execute(subprocess.Popen, (["ls", "-la"]))
393 # pylint: disable-msg=W0141
395 "dry-run only: %s(%s)",
396 get_function_name(f),
397 ", ".join(map(repr, exec_arguments)),
400 # pylint: disable-msg=W0142
401 return f(*exec_arguments)
405 """Check whether a given file can be executed
407 @fpath: full path to file
409 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
413 """Check whether a given program is available in PATH
415 @program: name of executable"""
416 fpath = os.path.split(program)[0]
421 for path in os.environ["PATH"].split(os.pathsep):
422 exe_file = os.path.join(path, program)
429 def get_defaults_file(iso_mount, flavour, name):
430 """get the default file for syslinux
432 bootloader_dirs = ["/boot/isolinux/", "/boot/syslinux/"]
433 for directory in bootloader_dirs:
434 for name in name, "%s_%s" % (get_flavour_filename(flavour), name):
435 if os.path.isfile(iso_mount + directory + name):
436 return (directory, name)
442 search_path="/bin" + os.pathsep + "/usr/bin",
446 """Given a search path, find file
448 @filename: name of file to search for
449 @search_path: path where searching for the specified filename
450 @lst_return: return list of matching files instead one file"""
451 paths = search_path.split(os.pathsep)
452 current_dir = "" # make pylint happy :)
456 """Helper function ffor testing if specified file exists in cwd
458 @cwd: current working directory
460 return os.path.exists(os.path.join(cwd, filename))
464 if match_file(current_dir):
465 retval.append(os.path.abspath(os.path.join(current_dir, filename)))
468 # pylint: disable-msg=W0612
469 for current_dir, directories, files in os.walk(path):
470 if match_file(current_dir):
471 retval.append(os.path.abspath(os.path.join(current_dir, filename)))
475 if required and not retval:
476 raise CriticalException(
477 "Required file %s not found in %s" % (filename, search_path)
488 def check_uid_root():
489 """Check for root permissions"""
490 if not os.geteuid() == 0:
491 raise CriticalException("please run this script with uid 0 (root).")
494 # for usage inside check_boot_flag
495 def get_partition_for_path(path):
498 boot_dev, x = get_device_from_partition(path)
500 d = parted.getDevice(boot_dev)
501 disk = parted.Disk(d)
502 return disk.getPartitionByPath(path)
505 def check_boot_flag(device):
506 boot_dev, x = get_device_from_partition(device)
511 part = get_partition_for_path(device)
513 raise HodorException("parted could not find partition")
514 if part.getFlag(parted.PARTITION_BOOT):
515 logging.debug("bootflag is enabled on %s" % device)
517 except HodorException as e:
518 logging.info("%s, falling back to old bootflag detection", e)
520 logging.debug("could not import parted, falling back to old bootflag detection")
522 with open(boot_dev, "rb") as image:
523 data = image.read(520)
524 bootcode = data[440:]
525 gpt_data = bootcode[70:80]
527 if gpt_data == GPT_HEADER:
528 logging.info("GPT detected, skipping bootflag check")
529 elif bootcode[6] == b"\x80":
530 logging.debug("bootflag is enabled")
532 logging.debug("bootflag is NOT enabled")
533 raise VerifyException(
534 "Device %s does not have the bootflag set. "
535 "Please enable it to be able to boot." % device
539 def mkfs_fat16(device):
540 """Format specified device with VFAT/FAT16 filesystem.
542 @device: partition that should be formated"""
545 logging.info("Would execute mkfs.vfat -F 16 %s now.", device)
548 logging.info("Formating partition with fat16 filesystem")
549 logging.debug("mkfs.vfat -F 16 %s", device)
550 proc = subprocess.Popen(["mkfs.vfat", "-F", "16", device])
552 if proc.returncode != 0:
553 raise CriticalException("error executing mkfs.vfat")
556 def generate_isolinux_splash(grml_flavour):
557 """Generate bootsplash for isolinux/syslinux
559 @grml_flavour: name of grml flavour the configuration should be generated for"""
561 grml_name = grml_flavour
564 \ f17
\f\18/boot/syslinux/logo.16
566 Some information and boot options available via keys F2 - F10. http://grml.org/
569 "grml_name": grml_name
573 def generate_main_syslinux_config(*arg):
574 """Generate main configuration for use in syslinux.cfg
576 @*arg: just for backward compatibility"""
577 # pylint: disable-msg=W0613
578 # remove warning about unused arg
582 menu label Default boot modes:
591 menu label Additional boot entries for:
593 include additional.cfg
600 include promptname.cfg
603 Jump to old style isolinux prompt
604 featuring further information
605 regarding available boot options.
613 def generate_flavour_specific_syslinux_config(grml_flavour):
614 """Generate flavour specific configuration for use in syslinux.cfg
616 @grml_flavour: name of grml flavour the configuration should be generated for"""
619 menu begin grml %(grml_flavour)s
620 menu title %(display_name)s
622 menu label ^Back to main menu...
625 # include config for boot parameters from disk
626 include %(grml_flavour)s_grml.cfg
630 "grml_flavour": grml_flavour,
631 "display_name": get_flavour_filename(grml_flavour),
635 def install_grub(device):
636 """Install grub on specified device.
638 @mntpoint: mountpoint of device where grub should install its files to
639 @device: partition where grub should be installed to"""
643 "Would execute grub-install [--root-directory=mount_point] %s now.", device
646 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
647 register_tmpfile(device_mountpoint)
649 # If using --grub-mbr then make sure we install grub in MBR instead of PBR
651 logging.debug("Using option --grub-mbr ...")
652 grub_device, x = get_device_from_partition(device)
657 mount(device, device_mountpoint, "")
659 logging.info("Installing grub as bootloader")
660 for opt in ["--", "--force"]:
664 "%s --recheck --no-floppy --target=i386-pc --root-directory=%s %s %s",
670 proc = subprocess.Popen(
676 "--root-directory=%s" % device_mountpoint,
680 stdout=open(os.devnull, "r+"),
683 if proc.returncode == 0:
686 if proc.returncode != 0:
687 # raise Exception("error executing grub-install")
689 "Fatal: error executing grub-install "
690 "(please check the grml2usb FAQ or drop the --grub option)"
693 "Note: if using grub2 consider using "
694 "the --grub-mbr option as grub considers PBR problematic."
698 except CriticalException as error:
699 logging.critical("Fatal: %s", error)
703 unmount(device_mountpoint, "")
704 os.rmdir(device_mountpoint)
705 unregister_tmpfile(device_mountpoint)
708 def install_syslinux(device):
709 """Install syslinux on specified device.
711 @device: partition where syslinux should be installed to"""
714 logging.info("Would install syslinux as bootloader on %s", device)
719 # syslinux -d boot/isolinux /dev/sdb1
720 logging.info("Installing syslinux as bootloader")
721 logging.debug("syslinux -d boot/syslinux %s", device)
722 proc = subprocess.Popen(["syslinux", "-d", "boot/syslinux", device])
724 if proc.returncode != 0:
725 raise CriticalException(
726 "Error executing syslinux (either try --fat16 or use grub?)"
730 def install_bootloader(device):
731 """Install bootloader on specified device.
733 @device: partition where bootloader should be installed to"""
735 # by default we use grub, so install syslinux only on request
739 except CriticalException as error:
740 logging.critical("Fatal: %s", error)
745 install_syslinux(device)
746 except CriticalException as error:
747 logging.critical("Fatal: %s", error)
752 def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
753 """install 'mbr' master boot record (MBR) on a device
755 Retrieve the partition table from "device", install an MBR from the
756 "mbrtemplate" file, set the "partition" (0..3) active, and install the
757 result back to "device".
759 @mbrtemplate: default MBR file
761 @device: name of a file assumed to be a hard disc (or USB stick) image, or
762 something like "/dev/sdb"
764 @partition: must be a number between 0 and 3, inclusive
766 @mbrtemplate: must be a valid MBR file of at least 440 (or 439 if
769 @ismirbsdmbr: if true then ignore the active flag, set the mirbsdmbr
770 specific flag to 0/1/2/3 and set the MBR's default value accordingly. If
771 false then leave the mirbsdmbr specific flag set to FFh, set all
772 active flags to 0 and set the active flag of the partition to 80h. Note:
773 behaviour of mirbsdmbr: if flag = 0/1/2/3 then use it, otherwise search for
776 logging.info("Installing default MBR")
778 if not os.path.isfile(mbrtemplate):
780 "Error installing MBR (either try --syslinux-mbr or "
781 'install missing file "%s"?)',
784 raise CriticalException("%s can not be read." % mbrtemplate)
786 if partition is not None and ((partition < 0) or (partition > 3)):
787 logging.warn("Cannot activate partition %d", partition)
795 tmpf = tempfile.NamedTemporaryFile()
797 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1", device, tmpf.name)
798 proc = subprocess.Popen(
799 ["dd", "if=%s" % device, "of=%s" % tmpf.name, "bs=512", "count=1"],
800 stderr=open(os.devnull, "r+"),
803 if proc.returncode != 0:
804 raise Exception("error executing dd (first run)")
807 "executing: dd if=%s of=%s bs=%s count=1 conv=notrunc",
812 proc = subprocess.Popen(
815 "if=%s" % mbrtemplate,
821 stderr=open(os.devnull, "r+"),
824 if proc.returncode != 0:
825 raise Exception("error executing dd (second run)")
827 mbrcode = tmpf.file.read(512)
828 if len(mbrcode) < 512:
829 raise EOFError("MBR size (%d) < 512" % len(mbrcode))
831 if partition is not None:
835 + chr(partition).encode("latin-1")
840 actives = [b"\x00", b"\x00", b"\x00", b"\x00"]
841 actives[partition] = b"\x80"
857 tmpf.file.write(mbrcode)
863 "executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc", tmpf.name, device
865 proc = subprocess.Popen(
874 stderr=open(os.devnull, "r+"),
877 if proc.returncode != 0:
878 raise Exception("error executing dd (third run)")
881 # make sure we sync filesystems before returning
882 proc = subprocess.Popen(["sync"])
888 def is_writeable(device):
889 """Check if the device is writeable for the current user
891 @device: partition where bootloader should be installed to"""
896 if not os.path.exists(device):
899 return os.access(device, os.W_OK) and os.access(device, os.R_OK)
902 def mount(source, target, mount_options):
903 """Mount specified source on given target
905 @source: name of device/ISO that should be mounted
906 @target: directory where the ISO should be mounted to
907 @options: mount specific options"""
909 # note: options.dryrun does not work here, as we have to
910 # locate files and identify the grml flavour
912 for x in open("/proc/mounts", "r").readlines():
913 if x.startswith(source):
914 raise CriticalException(
916 "Error executing mount: %s already mounted - "
917 "please unmount before invoking grml2usb"
921 if os.path.isdir(source):
922 logging.debug("Source %s is not a device, therefore not mounting.", source)
925 logging.debug("mount %s %s %s", mount_options, source, target)
926 proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target])
928 if proc.returncode != 0:
929 raise CriticalException(
930 "Error executing mount (no filesystem on the partition?)"
933 logging.debug("register_mountpoint(%s)", target)
934 register_mountpoint(target)
937 def unmount(target, unmount_options):
938 """Unmount specified target
940 @target: target where something is mounted on and which should be unmounted
941 @options: options for umount command"""
943 # make sure we unmount only already mounted targets
944 target_unmount = False
945 mounts = open("/proc/mounts").readlines()
946 mountstring = re.compile(".*%s.*" % re.escape(os.path.realpath(target)))
948 if re.match(mountstring, line):
949 target_unmount = True
951 if not target_unmount:
952 logging.debug("%s not mounted anymore", target)
954 logging.debug("umount %s %s", list(unmount_options), target)
955 proc = subprocess.Popen(["umount"] + list(unmount_options) + [target])
957 if proc.returncode != 0:
958 raise Exception("Error executing umount")
960 logging.debug("unregister_mountpoint(%s)", target)
961 unregister_mountpoint(target)
964 def check_for_usbdevice(device):
965 """Check whether the specified device is a removable USB device
967 @device: device name, like /dev/sda1 or /dev/sda
970 usbdevice = re.match(r"/dev/(.*?)\d*$", device).group(1)
972 usbdev = os.path.realpath("/sys/class/block/" + usbdevice + "/removable")
973 if not os.path.isfile(usbdev):
974 # Ubuntu with kernel 2.6.24 for example:
975 usbdev = os.path.realpath("/sys/block/" + usbdevice + "/removable")
977 if os.path.isfile(usbdev):
978 is_usb = open(usbdev).readline()
985 def check_for_fat(partition):
986 """Check whether specified partition is a valid VFAT/FAT16 filesystem
988 @partition: device name of partition"""
990 if not os.access(partition, os.R_OK):
991 raise CriticalException(
992 "Failed to read device %s"
993 " (wrong UID/permissions or device/directory not present?)" % partition
998 subprocess.check_output(
999 ["/sbin/blkid", "-s", "TYPE", "-o", "value", partition]
1005 if filesystem != "vfat":
1006 raise CriticalException(
1007 "Partition %s does not contain a FAT16 filesystem. "
1008 "(Use --fat16 or run mkfs.vfat %s)" % (partition, partition)
1012 raise CriticalException(
1013 "Sorry, /sbin/blkid not available (install util-linux?)"
1017 def mkdir(directory):
1018 """Simple wrapper around os.makedirs to get shell mkdir -p behaviour"""
1020 # just silently pass as it's just fine it the directory exists
1021 if not os.path.isdir(directory):
1023 os.makedirs(directory)
1024 # pylint: disable-msg=W0704
1029 def exec_rsync(source, target):
1030 """Simple wrapper around rsync to install files
1032 @source: source file/directory
1033 @target: target file/directory"""
1034 logging.debug("Source: %s / Target: %s", source, target)
1035 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", source, target])
1037 if proc.returncode == 12:
1038 logging.critical("Fatal: No space left on device")
1042 if proc.returncode != 0:
1043 logging.critical("Fatal: could not install %s", source)
1048 def write_uuid(target_file):
1049 """Generates an returns uuid and write it to the specified file
1051 @target_file: filename to write the uuid to
1054 fileh = open(target_file, "w")
1055 uid = str(uuid.uuid4())
1061 def get_uuid(target):
1062 """Get the uuid of the specified target. Will generate an uuid if none exist.
1064 @target: directory/mountpoint containing the grml layout
1067 conf_target = target + "/conf/"
1068 uuid_file_name = conf_target + "/bootid.txt"
1069 if os.path.isdir(conf_target):
1070 if os.path.isfile(uuid_file_name):
1071 uuid_file = open(uuid_file_name, "r")
1072 uid = uuid_file.readline().strip()
1076 return write_uuid(uuid_file_name)
1078 execute(mkdir, conf_target)
1079 return write_uuid(uuid_file_name)
1082 def get_shortname(grml_flavour):
1083 """Get shortname based from grml_flavour name. The rules applied are the same as in grml-live
1084 @grml_flavour: flavour name which shold be translated to shortname"""
1086 return re.sub(r"[,._-]", "", grml_flavour)
1089 def copy_system_files(grml_flavour, iso_mount, target):
1090 """copy grml's main files (like squashfs, kernel and initrd) to a given target
1092 @grml_flavour: name of grml flavour the configuration should be generated for
1093 @iso_mount: path where a grml ISO is mounted on
1094 @target: path where grml's main files should be copied to"""
1096 squashfs = search_file(grml_flavour + ".squashfs", iso_mount)
1097 if squashfs is None:
1098 logging.error("error locating squashfs file")
1099 raise CriticalException(
1100 "squashfs file not found, please check that your iso is not corrupt"
1103 squashfs_target = target + "/live/" + grml_flavour + "/"
1104 execute(mkdir, squashfs_target)
1105 exec_rsync(squashfs, squashfs_target + grml_flavour + ".squashfs")
1107 for prefix in grml_flavour + "/", "":
1108 filesystem_module = search_file(prefix + "filesystem.module", iso_mount)
1109 if filesystem_module:
1111 if filesystem_module is None:
1112 logging.error("error locating filesystem.module file")
1113 raise CriticalException("filesystem.module not found")
1115 exec_rsync(filesystem_module, squashfs_target + "filesystem.module")
1117 shortname = get_shortname(grml_flavour)
1118 if os.path.isdir(iso_mount + "/boot/" + shortname):
1119 exec_rsync(iso_mount + "/boot/" + shortname, target + "/boot")
1121 kernel = search_file("vmlinuz", iso_mount)
1123 # compat for releases < 2011.12
1124 kernel = search_file("linux26", iso_mount)
1127 logging.error("error locating kernel file")
1128 raise CriticalException("Kernel not found")
1130 source = os.path.dirname(kernel) + "/"
1131 dest = target + "/" + os.path.dirname(kernel).replace(iso_mount, "") + "/"
1132 execute(mkdir, dest)
1133 exec_rsync(source, dest)
1136 def update_grml_versions(iso_mount, target):
1137 """Update the grml version file on a cd
1138 Returns true if version was updated successfully,
1139 False if grml-version does not exist yet on the mountpoint
1141 @iso_mount: string of the iso mount point
1142 @target: path of the target mount point
1144 grml_target = target + "/grml/"
1145 target_grml_version_file = search_file("grml-version", grml_target)
1146 if target_grml_version_file:
1147 iso_grml_version_file = search_file("grml-version", iso_mount)
1148 if not iso_grml_version_file:
1150 "Warning: %s could not be found - can not install it",
1151 iso_grml_version_file,
1155 # read the flavours from the iso image
1157 iso_file = open(iso_grml_version_file, "r")
1158 for line in iso_file:
1159 iso_versions[get_flavour(line)] = line.strip()
1161 # update the existing flavours on the target
1162 for line in fileinput.input([target_grml_version_file], inplace=1):
1163 flavour = get_flavour(line)
1164 if flavour in list(iso_versions.keys()):
1165 print(iso_versions.pop(flavour))
1170 target_file = open(target_grml_version_file, "a")
1171 # add the new flavours from the current iso
1172 for flavour in iso_versions:
1173 target_file.write("%s\n" % iso_versions[flavour])
1175 logging.warn("Warning: Could not write file")
1184 def copy_grml_files(grml_flavour, iso_mount, target):
1185 """copy some minor grml files to a given target
1187 @grml_flavour: the current grml_flavour
1188 @iso_mount: path where a grml ISO is mounted on
1189 @target: path where grml's main files should be copied to"""
1191 grml_target = target + "/grml/"
1192 execute(mkdir, grml_target)
1194 grml_prefixe = ["GRML", "grml"]
1195 for prefix in grml_prefixe:
1196 filename = "{0}/{1}/{2}".format(iso_mount, prefix, grml_flavour)
1197 if os.path.exists(filename):
1198 exec_rsync(filename, grml_target)
1201 logging.warn("Warning: could not find flavour directory for %s ", grml_flavour)
1204 def copy_addons(iso_mount, target):
1205 """copy grml's addons files (like allinoneimg, bsd4grml,..) to a given target
1207 @iso_mount: path where a grml ISO is mounted on
1208 @target: path where grml's main files should be copied to"""
1210 addons = target + "/boot/addons/"
1211 execute(mkdir, addons)
1213 for addon_file in glob.glob(iso_mount + "/boot/addons/*"):
1214 filename = os.path.basename(addon_file)
1215 src_file = iso_mount + "/boot/addons/" + os.path.basename(addon_file)
1216 logging.debug("Copying addon file %s" % filename)
1217 exec_rsync(src_file, addons)
1220 def build_loopbackcfg(target):
1221 """Generate GRUB's loopback.cfg based on existing config files.
1223 @target: target directory
1226 grub_dir = "/boot/grub/"
1227 mkdir(os.path.join(target, grub_dir))
1229 f = open(target + grub_dir + "loopback.cfg", "w")
1231 f.write("# grml2usb generated grub2 configuration file\n")
1232 f.write("source /boot/grub/header.cfg\n")
1234 for defaults in glob.glob(
1235 target + os.path.sep + grub_dir + os.path.sep + "*_default.cfg"
1237 sourcefile = defaults.split(target + os.path.sep)[1]
1238 logging.debug("Found source file" + sourcefile)
1239 os.path.isfile(defaults) and f.write("source " + sourcefile + "\n")
1241 for ops in glob.glob(
1242 target + os.path.sep + grub_dir + os.path.sep + "*_options.cfg"
1244 sourcefile = ops.split(target + os.path.sep)[1]
1245 logging.debug("Found source file" + sourcefile)
1246 os.path.isfile(ops) and f.write("source " + sourcefile + "\n")
1248 f.write("source /boot/grub/addons.cfg\n")
1249 f.write("source /boot/grub/footer.cfg\n")
1253 def glob_and_copy(filepattern, dst):
1254 """Glob on specified filepattern and copy the result to dst
1256 @filepattern: globbing pattern
1257 @dst: target directory
1259 for name in glob.glob(filepattern):
1260 copy_if_exist(name, dst)
1263 def search_and_copy(filename, search_path, dst):
1264 """Search for the specified filename at searchpath and copy it to dst
1266 @filename: filename to look for
1267 @search_path: base search file
1268 @dst: destionation to copy the file to
1270 file_location = search_file(filename, search_path)
1271 copy_if_exist(file_location, dst)
1274 def copy_if_exist(filename, dst):
1275 """Copy filename to dst if filename is set.
1277 @filename: a filename
1280 if filename and (os.path.isfile(filename) or os.path.isdir(filename)):
1281 exec_rsync(filename, dst)
1284 def copy_bootloader_files(iso_mount, target, grml_flavour):
1285 """Copy grml's bootloader files to a given target
1287 @iso_mount: path where a grml ISO is mounted on
1288 @target: path where grml's main files should be copied to
1289 @grml_flavour: name of the current processed grml_flavour
1292 syslinux_target = target + "/boot/syslinux/"
1293 execute(mkdir, syslinux_target)
1295 grub_target = target + "/boot/grub/"
1296 execute(mkdir, grub_target)
1298 logo = search_file("logo.16", iso_mount, required=True)
1299 exec_rsync(logo, syslinux_target + "logo.16")
1301 bootx64_efi = search_file("bootx64.efi", iso_mount)
1303 mkdir(target + "/efi/boot/")
1304 exec_rsync(bootx64_efi, target + "/efi/boot/bootx64.efi")
1306 efi_img = search_file("efi.img", iso_mount)
1308 mkdir(target + "/boot/")
1309 exec_rsync(efi_img, target + "/boot/efi.img")
1310 handle_secure_boot(target, efi_img)
1312 for ffile in ["f%d" % number for number in range(1, 11)]:
1313 search_and_copy(ffile, iso_mount, syslinux_target + ffile)
1315 # avoid the "file is read only, overwrite anyway (y/n) ?" question
1316 # of mtools by syslinux ("mmove -D o -D O s:/ldlinux.sys $target_file")
1317 if os.path.isfile(syslinux_target + "ldlinux.sys"):
1318 os.unlink(syslinux_target + "ldlinux.sys")
1320 (source_dir, name) = get_defaults_file(iso_mount, grml_flavour, "default.cfg")
1321 (source_dir, defaults_file) = get_defaults_file(iso_mount, grml_flavour, "grml.cfg")
1324 raise CriticalException(
1325 "file default.cfg could not be found.\n"
1326 "Note: this grml2usb version requires an ISO generated by grml-live >=0.9.24 ...\n"
1327 " ... either use grml releases >=2009.10 or switch to an older grml2usb version."
1330 if not os.path.exists(iso_mount + "/boot/grub/footer.cfg"):
1332 "Warning: Grml releases older than 2011.12 support only one flavour in grub."
1350 glob_and_copy(iso_mount + source_dir + expr, syslinux_target)
1352 for modules_dir in options.syslinuxlibs + SYSLINUX_LIBS:
1353 if os.path.isdir(modules_dir):
1354 for filename in glob.glob1(syslinux_target, "*.c32"):
1355 copy_if_exist(os.path.join(modules_dir, filename), syslinux_target)
1358 # copy the addons_*.cfg file to the new syslinux directory
1359 glob_and_copy(iso_mount + source_dir + "addon*.cfg", syslinux_target)
1362 "hidden.cfg", iso_mount + source_dir, syslinux_target + "new_" + "hidden.cfg"
1365 # copy all grub files from ISO
1366 glob_and_copy(iso_mount + "/boot/grub/*", grub_target)
1368 # finally (after all GRUB files have been installed) build static loopback.cfg
1369 build_loopbackcfg(target)
1372 def install_iso_files(grml_flavour, iso_mount, device, target):
1373 """Copy files from ISO to given target
1375 @grml_flavour: name of grml flavour the configuration should be generated for
1376 @iso_mount: path where a grml ISO is mounted on
1377 @device: device/partition where bootloader should be installed to
1378 @target: path where grml's main files should be copied to"""
1381 GRML_DEFAULT = GRML_DEFAULT or grml_flavour
1384 elif not options.bootloaderonly:
1385 logging.info("Copying files. This might take a while....")
1387 copy_system_files(grml_flavour, iso_mount, target)
1388 copy_grml_files(grml_flavour, iso_mount, target)
1389 except CriticalException as error:
1390 logging.critical("Execution failed: %s", error)
1393 if not options.skipaddons:
1394 if not search_file("addons", iso_mount):
1395 logging.info("Could not find addons, therefore not installing.")
1397 copy_addons(iso_mount, target)
1399 if not options.copyonly:
1400 copy_bootloader_files(iso_mount, target, grml_flavour)
1402 if not options.dryrun:
1403 handle_bootloader_config(grml_flavour, device, target)
1405 # make sure we sync filesystems before returning
1406 proc = subprocess.Popen(["sync"])
1410 def get_device_from_partition(partition):
1412 partition_number = None
1413 if partition[-1].isdigit() and not RE_LOOP_DEVICE.match(partition):
1414 m = RE_P_PARTITION.match(partition) or RE_PARTITION.match(partition)
1417 partition_number = int(m.group(2)) - 1
1418 return (device, partition_number)
1421 def get_flavour(flavour_str):
1422 """Returns the flavour of a grml version string
1424 return re.match(r"[\w-]*", flavour_str).group()
1427 def identify_grml_flavour(mountpath):
1428 """Get name of grml flavour
1430 @mountpath: path where the grml ISO is mounted to
1431 @return: name of grml-flavour"""
1433 version_files = search_file("grml-version", mountpath, lst_return=True)
1435 if not version_files:
1436 if mountpath.startswith("/run/live/medium"):
1437 logging.critical("Error: could not find grml-version file.")
1439 "Looks like your system is running from RAM but required files are not available."
1442 "Please either boot without toram=... or use boot option toram instead of toram=..."
1445 logging.critical("Error: could not find grml-version file.")
1450 logging.debug("version_files = %s", version_files)
1451 for version_file in version_files:
1454 tmpfile = open(version_file, "r")
1455 for line in tmpfile.readlines():
1456 flavours.append(get_flavour(line))
1464 def get_bootoptions(grml_flavour):
1465 """Returns bootoptions for specific flavour
1467 @grml_flavour: name of the grml_flavour
1469 # do NOT write "None" in kernel cmdline
1470 if not options.bootoptions:
1473 bootopt = " ".join(options.bootoptions)
1474 bootopt = bootopt.replace("%flavour", grml_flavour)
1478 def handle_grub_config(grml_flavour, device, target):
1479 """Main handler for generating grub (v1 and v2) configuration
1481 @grml_flavour: name of grml flavour the configuration should be generated for
1482 @device: device/partition where grub should be installed to
1483 @target: path of grub's configuration files"""
1487 logging.debug("Updating grub configuration")
1489 grub_target = target + "/boot/grub/"
1490 secureboot_target = target + "/EFI/ubuntu/"
1492 bootid_re = re.compile(r"bootid=[\w_-]+")
1493 live_media_path_re = re.compile(r"live-media-path=[\w_/-]+")
1495 bootopt = get_bootoptions(grml_flavour)
1498 option_re = re.compile(r"(.*/boot/.*(linux26|vmlinuz).*)")
1500 if options.removeoption:
1501 for regex in options.removeoption:
1502 remove_regexes.append(re.compile(regex))
1504 shortname = get_shortname(grml_flavour)
1505 for filename in glob.glob(grub_target + "*.cfg") + glob.glob(
1506 secureboot_target + "*.cfg"
1508 for line in fileinput.input(filename, inplace=1):
1509 line = line.rstrip("\r\n")
1510 if option_re.search(line):
1511 line = bootid_re.sub("", line)
1512 if shortname in filename:
1513 line = live_media_path_re.sub("", line)
1514 line = line.rstrip() + " live-media-path=/live/%s/ " % (
1518 line = line.replace(" {} ".format(bootopt.strip()), " ")
1519 if line.endswith(bootopt):
1520 line = line[: -len(bootopt)]
1521 line = line.rstrip() + r" bootid=%s %s " % (UUID, bootopt)
1522 for regex in remove_regexes:
1523 line = regex.sub(" ", line)
1528 def initial_syslinux_config(target):
1529 """Generates initial syslinux configuration
1531 @target path of syslinux's configuration files"""
1533 target = target + "/"
1534 filename = target + "grmlmain.cfg"
1535 if os.path.isfile(target + "grmlmain.cfg"):
1537 data = open(filename, "w")
1538 data.write(generate_main_syslinux_config())
1541 filename = target + "hiddens.cfg"
1542 data = open(filename, "w")
1543 data.write("include hidden.cfg\n")
1547 def add_entry_if_not_present(filename, entry):
1548 """Write entry into filename if entry is not already in the file
1550 @filename: name of the file
1551 @entry: data to write to the file
1553 data = open(filename, "a+")
1564 def get_flavour_filename(flavour):
1565 """Generate a iso9960 save filename out of the specified flavour
1567 @flavour: grml flavour
1569 return flavour.replace("-", "_")
1572 def adjust_syslinux_bootoptions(src, flavour):
1573 """Adjust existing bootoptions of specified syslinux config to
1574 grml2usb specific ones, e.g. change the location of the kernel...
1576 @src: config file to alter
1577 @flavour: grml flavour
1580 append_re = re.compile(r"^(\s*append.*/boot/.*)$", re.I)
1581 # flavour_re = re.compile("(label.*)(grml\w+)")
1582 default_re = re.compile(r"(default.cfg)")
1583 bootid_re = re.compile(r"bootid=[\w_-]+")
1584 live_media_path_re = re.compile(r"live-media-path=[\w_/-]+")
1586 bootopt = get_bootoptions(flavour)
1590 if options.removeoption:
1591 option_re = re.compile(r"/boot/.*/(initrd.gz|initrd.img)")
1593 for regex in options.removeoption:
1594 regexe.append(re.compile(r"%s" % regex))
1596 for line in fileinput.input(src, inplace=1):
1597 # line = flavour_re.sub(r'\1 %s-\2' % flavour, line)
1598 line = default_re.sub(r"%s-\1" % flavour, line)
1599 line = bootid_re.sub("", line)
1600 line = live_media_path_re.sub("", line)
1601 line = append_re.sub(r"\1 live-media-path=/live/%s/ " % flavour, line)
1602 line = append_re.sub(r"\1 boot=live %s " % bootopt, line)
1603 line = append_re.sub(r"\1 %s=%s " % ("bootid", UUID), line)
1604 if option_re and option_re.search(line):
1605 for regex in regexe:
1606 line = regex.sub(" ", line)
1607 sys.stdout.write(line)
1611 def adjust_labels(src, replacement):
1612 """Adjust the specified labels in the syslinux config file src with
1613 specified replacement
1615 label_re = re.compile(r"^(\s*label\s*) ([a-zA-Z0-9_-]+)", re.I)
1616 for line in fileinput.input(src, inplace=1):
1617 line = label_re.sub(replacement, line)
1618 sys.stdout.write(line)
1622 def add_syslinux_entry(filename, grml_flavour):
1623 """Add includes for a specific grml_flavour to the specified filename
1625 @filename: syslinux config file
1626 @grml_flavour: grml flavour to add
1629 entry_filename = "option_%s.cfg" % grml_flavour
1630 entry = "include %s\n" % entry_filename
1632 add_entry_if_not_present(filename, entry)
1633 path = os.path.dirname(filename)
1635 data = open(path + "/" + entry_filename, "w")
1636 data.write(generate_flavour_specific_syslinux_config(grml_flavour))
1640 def modify_filenames(grml_flavour, target, filenames):
1641 """Replace the standard filenames with the new ones
1643 @grml_flavour: grml-flavour strin
1644 @target: directory where the files are located
1645 @filenames: list of filenames to alter
1647 grml_filename = get_flavour_filename(grml_flavour)
1648 for filename in filenames:
1649 old_filename = "%s/%s" % (target, filename)
1650 new_filename = "%s/%s_%s" % (target, grml_filename, filename)
1651 os.rename(old_filename, new_filename)
1654 def remove_default_entry(filename):
1655 """Remove the default entry from specified syslinux file
1657 @filename: syslinux config file
1659 default_re = re.compile(r"^(\s*menu\s*default\s*)$", re.I)
1660 for line in fileinput.input(filename, inplace=1):
1661 if default_re.match(line):
1663 sys.stdout.write(line)
1667 def handle_syslinux_config(grml_flavour, target):
1668 """Main handler for generating syslinux configuration
1670 @grml_flavour: name of grml flavour the configuration should be generated for
1671 @target: path of syslinux's configuration files"""
1673 logging.debug("Generating syslinux configuration")
1674 syslinux_target = target + "/boot/syslinux/"
1675 # should be present via copy_bootloader_files(), but make sure it exits:
1676 execute(mkdir, syslinux_target)
1677 syslinux_cfg = syslinux_target + "syslinux.cfg"
1679 # install main configuration only *once*, no matter how many ISOs we have:
1680 syslinux_config_file = open(syslinux_cfg, "w")
1681 syslinux_config_file.write("timeout 300\n")
1682 syslinux_config_file.write("include vesamenu.cfg\n")
1683 syslinux_config_file.close()
1685 prompt_name = open(syslinux_target + "promptname.cfg", "w")
1686 prompt_name.write("menu label S^yslinux prompt\n")
1689 initial_syslinux_config(syslinux_target)
1690 flavour_filename = get_flavour_filename(grml_flavour)
1692 if search_file("default.cfg", syslinux_target):
1693 modify_filenames(grml_flavour, syslinux_target, ["grml.cfg", "default.cfg"])
1695 filename = search_file("new_hidden.cfg", syslinux_target)
1697 # process hidden file
1698 if not search_file("hidden.cfg", syslinux_target):
1699 new_hidden = syslinux_target + "hidden.cfg"
1700 os.rename(filename, new_hidden)
1701 adjust_syslinux_bootoptions(new_hidden, grml_flavour)
1703 new_hidden_file = "%s/%s_hidden.cfg" % (syslinux_target, flavour_filename)
1704 os.rename(filename, new_hidden_file)
1705 adjust_labels(new_hidden_file, r"\1 %s-\2" % grml_flavour)
1706 adjust_syslinux_bootoptions(new_hidden_file, grml_flavour)
1707 entry = "include %s_hidden.cfg\n" % flavour_filename
1708 add_entry_if_not_present("%s/hiddens.cfg" % syslinux_target, entry)
1710 new_default = "%s_default.cfg" % (flavour_filename)
1711 entry = "include %s\n" % new_default
1712 defaults_file = "%s/defaults.cfg" % syslinux_target
1713 new_default_with_path = "%s/%s" % (syslinux_target, new_default)
1714 new_grml_cfg = "%s/%s_grml.cfg" % (syslinux_target, flavour_filename)
1716 if os.path.isfile(defaults_file):
1718 # remove default menu entry in menu
1719 remove_default_entry(new_default_with_path)
1721 # adjust all labels for additional isos
1722 adjust_labels(new_default_with_path, r"\1 %s" % grml_flavour)
1723 adjust_labels(new_grml_cfg, r"\1 %s-\2" % grml_flavour)
1725 # always adjust bootoptions
1726 adjust_syslinux_bootoptions(new_default_with_path, grml_flavour)
1727 adjust_syslinux_bootoptions(new_grml_cfg, grml_flavour)
1729 add_entry_if_not_present("%s/defaults.cfg" % syslinux_target, entry)
1731 add_syslinux_entry("%s/additional.cfg" % syslinux_target, flavour_filename)
1734 def handle_secure_boot(target, efi_img):
1735 """Provide secure boot support by extracting files from /boot/efi.img
1737 @target: path where grml's main files should be copied to
1738 @efi_img: path to the efi.img file that includes the files for secure boot
1741 mkdir(target + "/efi/boot/")
1742 efi_mountpoint = tempfile.mkdtemp(
1743 prefix="grml2usb", dir=os.path.abspath(options.tmpdir)
1745 logging.debug("efi_mountpoint = %s" % efi_mountpoint)
1746 register_tmpfile(efi_mountpoint)
1750 "mount(%s, %s, ['-o', 'ro', '-t', 'vfat']" % (efi_img, efi_mountpoint)
1752 mount(efi_img, efi_mountpoint, ["-o", "ro", "-t", "vfat"])
1753 except CriticalException as error:
1754 logging.critical("Fatal: %s", error)
1757 ubuntu_cfg = search_file("grub.cfg", efi_mountpoint + "/EFI/ubuntu")
1758 logging.debug("ubuntu_cfg = %s" % ubuntu_cfg)
1761 "No /EFI/ubuntu/grub.cfg found inside EFI image, looks like Secure Boot support is missing."
1764 mkdir(target + "/efi/ubuntu")
1766 "exec_rsync(%s, %s + '/efi/ubuntu/grub.cfg')" % (ubuntu_cfg, target)
1768 exec_rsync(ubuntu_cfg, target + "/efi/ubuntu/grub.cfg")
1771 "exec_rsync(%s + '/EFI/BOOT/grubx64.efi', %s + '/efi/boot/grubx64.efi')'"
1772 % (efi_mountpoint, target)
1775 efi_mountpoint + "/EFI/BOOT/grubx64.efi", target + "/efi/boot/grubx64.efi"
1778 # NOTE - we're overwriting /efi/boot/bootx64.efi from copy_bootloader_files here
1780 "exec_rsync(%s + '/EFI/BOOT/bootx64.efi', %s + '/efi/boot/bootx64.efi')'"
1781 % (efi_mountpoint, target)
1784 efi_mountpoint + "/EFI/BOOT/bootx64.efi", target + "/efi/boot/bootx64.efi"
1788 unmount(efi_mountpoint, "")
1789 logging.debug("Unmounted %s" % efi_mountpoint)
1790 os.rmdir(efi_mountpoint)
1791 logging.debug("Removed directory %s" % efi_mountpoint)
1793 logging.critical("RuntimeError while umount %s" % efi_mountpoint)
1797 def handle_bootloader_config(grml_flavour, device, target):
1798 """Main handler for generating bootloader's configuration
1800 @grml_flavour: name of grml flavour the configuration should be generated for
1801 @device: device/partition where bootloader should be installed to
1802 @target: path of bootloader's configuration files"""
1805 UUID = get_uuid(target)
1806 if options.skipsyslinuxconfig:
1807 logging.info("Skipping generation of syslinux configuration as requested.")
1810 handle_syslinux_config(grml_flavour, target)
1811 except CriticalException as error:
1812 logging.critical("Fatal: %s", error)
1815 if options.skipgrubconfig:
1816 logging.info("Skipping generation of grub configuration as requested.")
1819 handle_grub_config(grml_flavour, device, target)
1820 except CriticalException as error:
1821 logging.critical("Fatal: %s", error)
1825 def install(image, device):
1826 """Install a grml image to the specified device
1828 @image: directory or is file
1829 @device: partition or directory to install the device
1831 iso_mountpoint = image
1832 remove_image_mountpoint = False
1833 if os.path.isdir(image):
1834 if options.force or os.path.exists(os.path.join(image, "live")):
1835 logging.info("Using %s as install base", image)
1838 "%s does not look like a Grml system. "
1839 "Do you really want to use this image? y/N " % image
1841 if q.lower() == "y":
1842 logging.info("Using %s as install base", image)
1844 logging.info("Skipping install base %s", image)
1846 logging.info("Using ISO %s", image)
1847 iso_mountpoint = tempfile.mkdtemp(
1848 prefix="grml2usb", dir=os.path.abspath(options.tmpdir)
1850 register_tmpfile(iso_mountpoint)
1851 remove_image_mountpoint = True
1853 mount(image, iso_mountpoint, ["-o", "loop,ro", "-t", "iso9660"])
1854 except CriticalException as error:
1855 logging.critical("Fatal: %s", error)
1859 install_grml(iso_mountpoint, device)
1861 if remove_image_mountpoint:
1863 remove_mountpoint(iso_mountpoint)
1864 except CriticalException:
1869 def install_grml(mountpoint, device):
1870 """Main logic for copying files of the currently running Grml system.
1872 @mountpoint: directory where currently running live system resides (usually /run/live/medium)
1873 @device: partition where the specified ISO should be installed to"""
1875 device_mountpoint = device
1876 if os.path.isdir(device):
1877 logging.info("Specified device is a directory, therefore not mounting.")
1878 remove_device_mountpoint = False
1880 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1881 register_tmpfile(device_mountpoint)
1882 remove_device_mountpoint = True
1884 check_for_fat(device)
1885 if not options.skipbootflag:
1886 check_boot_flag(device)
1889 mount(device, device_mountpoint, ["-o", "utf8,iocharset=iso8859-1"])
1890 except CriticalException:
1891 mount(device, device_mountpoint, "")
1893 grml_flavours = identify_grml_flavour(mountpoint)
1894 for flavour in set(grml_flavours):
1896 logging.warning("No valid flavour found, please check your iso")
1897 logging.info('Identified grml flavour "%s".', flavour)
1898 install_iso_files(flavour, mountpoint, device, device_mountpoint)
1899 GRML_FLAVOURS.add(flavour)
1901 if remove_device_mountpoint:
1902 remove_mountpoint(device_mountpoint)
1905 def remove_mountpoint(mountpoint):
1906 """remove a registered mountpoint
1910 unmount(mountpoint, "")
1911 if os.path.isdir(mountpoint):
1912 os.rmdir(mountpoint)
1913 unregister_tmpfile(mountpoint)
1914 except CriticalException:
1919 def handle_mbr(device):
1920 """Main handler for installing master boot record (MBR)
1922 @device: device where the MBR should be installed to"""
1925 logging.info("Would install MBR")
1928 mbr_device, partition_number = get_device_from_partition(device)
1929 if partition_number is None:
1930 logging.warn("Could not detect partition number, not activating partition")
1932 # if we get e.g. /dev/loop1 as device we don't want to put the MBR
1933 # into /dev/loop of course, therefore use /dev/loop1 as mbr_device
1934 if mbr_device == "/dev/loop":
1937 "Detected loop device - using %s as MBR device therefore", mbr_device
1940 mbrcode = GRML2USB_BASE + "/mbr/mbrldr"
1941 if options.syslinuxmbr:
1944 "/usr/lib/syslinux/mbr.bin",
1945 "/usr/lib/syslinux/bios/mbr.bin",
1946 "/usr/share/syslinux/mbr.bin",
1948 for mbrpath in mbr_locations:
1949 if os.path.isfile(mbrpath):
1954 str_locations = " or ".join(['"%s"' % l for l in mbr_locations])
1955 logging.error("Cannot find syslinux MBR, install it at %s)", str_locations)
1956 raise CriticalException(
1957 "syslinux MBR can not be found at %s." % str_locations
1959 elif options.mbrmenu:
1960 mbrcode = GRML2USB_BASE + "/mbr/mbrldr"
1963 install_mbr(mbrcode, mbr_device, partition_number, True)
1964 except (IOError, Exception) as error:
1965 logging.critical("Execution failed: %s", error)
1969 def handle_vfat(device):
1970 """Check for FAT specific settings and options
1972 @device: device that should checked / formated"""
1974 # make sure we have mkfs.vfat available
1976 if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
1977 logging.critical("Sorry, mkfs.vfat not available. Exiting.")
1978 logging.critical("Please make sure to install dosfstools.")
1982 print("Forcing mkfs.fat16 on %s as requested via option --force." % device)
1984 # make sure the user is aware of what he is doing
1986 "Are you sure you want to format the specified partition with fat16? y/N "
1988 if f == "y" or f == "Y":
1990 "Note: you can skip this question using the option --force"
1996 except CriticalException as error:
1997 logging.critical("Execution failed: %s", error)
2000 # check for vfat filesystem
2001 if device is not None and not os.path.isdir(device) and options.syslinux:
2003 check_for_fat(device)
2004 except CriticalException as error:
2005 logging.critical("Execution failed: %s", error)
2008 if options.skipusbcheck:
2010 "Not checking for removable USB device as requested via option --skip-usb-check."
2015 not os.path.isdir(device)
2016 and not check_for_usbdevice(device)
2017 and not options.force
2020 "Warning: the specified device %s does not look like a removable usb device."
2023 f = input("Do you really want to continue? y/N ")
2024 if f.lower() != "y":
2028 def handle_compat_warning(device):
2029 """Backwards compatible checks
2031 @device: device that should be checked"""
2033 # make sure we can replace old grml2usb script and warn user when using old way of life:
2035 device.startswith("/mnt/external")
2036 or device.startswith("/mnt/usb")
2037 and not options.force
2039 print("Warning: the semantics of grml2usb has changed.")
2040 print("Instead of using grml2usb /path/to/iso %s you might" % device)
2041 print("want to use grml2usb /path/to/iso /dev/... instead.")
2042 print("Please check out the grml2usb manpage for details.")
2043 f = input("Do you really want to continue? y/N ")
2044 if f.lower() != "y":
2048 def handle_logging():
2049 """Log handling and configuration"""
2051 if options.verbose and options.quiet:
2052 parser.error("please use either verbose (--verbose) or quiet (--quiet) option")
2054 FORMAT = "%(message)s"
2056 FORMAT = "%(asctime)-15s %(message)s"
2057 logging.basicConfig(level=logging.DEBUG, format=FORMAT)
2059 logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
2061 logging.basicConfig(level=logging.INFO, format=FORMAT)
2064 def handle_bootloader(device):
2065 """wrapper for installing bootloader
2067 @device: device where bootloader should be installed to"""
2069 # Install bootloader only if not using the --copy-only option
2070 if options.copyonly:
2072 "Not installing bootloader and its files as requested via option copyonly."
2074 elif os.path.isdir(device):
2075 logging.info("Not installing bootloader as %s is a directory.", device)
2077 install_bootloader(device)
2080 def check_options(opts):
2081 """Check compatibility of provided user opts
2083 @opts option dict from OptionParser
2085 if opts.grubmbr and not opts.grub:
2086 raise CriticalException("--grub-mbr requires --grub option.")
2088 if opts.copyonly and opts.grub:
2089 raise CriticalException("Cannot use --copy-only and --grub at the same time.")
2092 def check_programs():
2093 """check if all needed programs are installed"""
2096 GRUB_INSTALL = which("grub-install") or which("grub2-install")
2097 if not GRUB_INSTALL:
2099 "Fatal: grub-install not available (please install the "
2100 "grub package or drop the --grub option)"
2104 if options.syslinux:
2105 if not which("syslinux"):
2107 "Fatal: syslinux not available (please install the "
2108 "syslinux package or use the --grub option)"
2112 if not which("rsync"):
2113 logging.critical("Fatal: rsync not available, can not continue - sorry.")
2118 """Runs modprobe loop and throws away its output"""
2119 if not which("modprobe"):
2120 logging.critical("Fatal: modprobe not available, can not continue - sorry.")
2121 logging.critical("Hint: is /sbin missing in PATH?")
2124 proc = subprocess.Popen(
2125 ["modprobe", "loop"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
2131 """Main invocation"""
2135 print(os.path.basename(sys.argv[0]) + " " + PROG_VERSION)
2139 parser.error("invalid usage")
2144 # make sure we have the appropriate permissions
2147 check_options(options)
2151 logging.info("Executing grml2usb version %s", PROG_VERSION)
2154 logging.info("Running in simulation mode as requested via option dry-run.")
2158 # specified arguments
2159 device = os.path.realpath(args[len(args) - 1])
2160 isos = args[0 : len(args) - 1]
2162 if not os.path.isdir(device):
2163 if device[-1:].isdigit():
2164 if int(device[-1:]) > 4 or device[-2:].isdigit():
2166 "Warning: installing on partition number >4, booting *might* fail depending on your system."
2169 # provide upgrade path
2170 handle_compat_warning(device)
2172 # check for vfat partition
2175 # main operation (like installing files)
2177 install(iso, device)
2180 is_superfloppy = not device[-1:].isdigit()
2182 logging.info("Detected superfloppy format - not installing MBR")
2184 if not options.skipmbr and not os.path.isdir(device) and not is_superfloppy:
2187 handle_bootloader(device)
2190 "Note: grml flavour %s was installed as the default booting system.",
2194 for flavour in GRML_FLAVOURS:
2196 "Note: you can boot flavour %s using '%s' on the commandline.",
2201 # finally be polite :)
2203 "Finished execution of grml2usb (%s). Have fun with your Grml system.",
2207 except Exception as error:
2208 logging.critical("Fatal: %s", str(error))
2210 logging.exception("Exception:")
2214 if __name__ == "__main__":
2217 except KeyboardInterrupt:
2218 logging.info("Received KeyboardInterrupt")
2221 # END OF FILE ##################################################################
2222 # vim:foldmethod=indent expandtab ai ft=python tw=120 fileencoding=utf-8