+def handle_bootloader_config(grml_flavour, target):
+ """TODO"""
+
+ logging.debug("Generating grub configuration")
+ #with open("...", "w") as f:
+ #f.write("bla bla bal")
+
+ grub_target = target + '/boot/grub/'
+ # should be present via copy_bootloader_files(), but make sure it exists:
+ execute(mkdir, grub_target)
+ grub_config_file = open(grub_target + 'menu.lst', 'w')
+ grub_config_file.write(generate_grub_config(grml_flavour))
+ grub_config_file.close()
+
+ logging.info("Generating syslinux configuration")
+ syslinux_target = target + '/boot/syslinux/'
+ # should be present via copy_bootloader_files(), but make sure it exits:
+ execute(mkdir, syslinux_target)
+ syslinux_cfg = syslinux_target + 'syslinux.cfg'
+
+ # install main configuration only *once*, no matter how many ISOs we have:
+ if os.path.isfile(syslinux_cfg):
+ string = open(syslinux_cfg).readline()
+ main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
+ if not re.match(main_identifier, string):
+ syslinux_config_file = open(syslinux_cfg, 'w')
+ logging.info("Notice: grml flavour %s is being installed as the default booting system." % grml_flavour)
+ syslinux_config_file.write(generate_main_syslinux_config(grml_flavour, options.bootoptions))
+ syslinux_config_file.close()
+ else:
+ syslinux_config_file = open(syslinux_cfg, 'w')
+ syslinux_config_file.write(generate_main_syslinux_config(grml_flavour, options.bootoptions))
+ syslinux_config_file.close()
+
+ # install flavour specific configuration only *once* as well
+ # kind of ugly - I'm pretty sure this could be smoother...
+ flavour_config = True
+ if os.path.isfile(syslinux_cfg):
+ string = open(syslinux_cfg).readlines()
+ logging.info("Notice: you can boot flavour %s using '%s' on the commandline." % (grml_flavour, grml_flavour))
+ flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
+ for line in string:
+ if flavour.match(line):
+ flavour_config = False
+
+ if flavour_config:
+ syslinux_config_file = open(syslinux_cfg, 'a')
+ syslinux_config_file.write(generate_flavour_specific_syslinux_config(grml_flavour, options.bootoptions))
+ syslinux_config_file.close( )
+
+ logging.debug("Generating isolinux/syslinux splash %s" % syslinux_target + 'boot.msg')
+ isolinux_splash = open(syslinux_target + 'boot.msg', 'w')
+ isolinux_splash.write(generate_isolinux_splash(grml_flavour))
+ isolinux_splash.close( )
+
+
+def handle_iso(iso, device):
+ """Main logic for mounting ISOs and copying files.
+
+ @iso: full path to the ISO that should be installed to the specified device
+ @device: partition where the specified ISO should be installed to"""
+
+ logging.info("Using ISO %s" % iso)
+
+ if os.path.isdir(iso):
+ logging.critical("TODO: /live/image handling not yet implemented - sorry") # TODO
+ sys.exit(1)
+
+ iso_mountpoint = tempfile.mkdtemp()
+ register_tmpfile(iso_mountpoint)
+ remove_iso_mountpoint = True
+
+ if not os.path.isfile(iso):
+ logging.critical("Fatal: specified ISO %s could not be read" % iso)
+ cleanup()
+ sys.exit(1)
+
+ try:
+ mount(iso, iso_mountpoint, ["-o", "loop", "-t", "iso9660"])
+ except CriticalException, error:
+ logging.critical("Fatal: %s" % error)
+ sys.exit(1)
+
+ if os.path.isdir(device):
+ logging.info("Specified target is a directory, not mounting therefor.")
+ device_mountpoint = device
+ remove_device_mountpoint = False
+ # skip_mbr = True
+ else:
+ device_mountpoint = tempfile.mkdtemp()
+ register_tmpfile(device_mountpoint)
+ remove_device_mountpoint = True
+ try:
+ mount(device, device_mountpoint, "")
+ except CriticalException, error:
+ logging.critical("Fatal: %s" % error)
+ cleanup()
+
+ try:
+ grml_flavour = identify_grml_flavour(iso_mountpoint)
+ logging.info("Identified grml flavour \"%s\"." % grml_flavour)
+ install_iso_files(grml_flavour, iso_mountpoint, device_mountpoint)
+ except TypeError:
+ logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
+ sys.exit(1)
+ finally:
+ if os.path.isdir(iso_mountpoint) and remove_iso_mountpoint:
+ unmount(iso_mountpoint, "")
+ os.rmdir(iso_mountpoint)
+ unregister_tmpfile(iso_mountpoint)
+ if remove_device_mountpoint:
+ unmount(device_mountpoint, "")
+ if os.path.isdir(device_mountpoint):
+ os.rmdir(device_mountpoint)
+ unregister_tmpfile(device_mountpoint)
+
+
+def handle_mbr(device):
+ """TODO"""
+
+ # install MBR
+ # if not options.mbr:
+ # logging.info("You are NOT using the --mbr option. Consider using it if your device does not boot.")
+ # else:
+ # make sure we install MBR on /dev/sdX and not /dev/sdX#
+
+ # make sure we have syslinux available
+ # if options.mbr:
+ if not options.skipmbr:
+ if not which("syslinux") and not options.copyonly and not options.dryrun:
+ logging.critical('Sorry, syslinux not available. Exiting.')
+ logging.critical('Please install syslinux or consider using the --grub option.')
+ sys.exit(1)
+
+ if not options.skipmbr:
+ if device[-1:].isdigit():
+ mbr_device = re.match(r'(.*?)\d*$', device).group(1)
+
+ try:
+ install_mbr(mbr_device)
+ except IOError, error:
+ logging.critical("Execution failed: %s", error)
+ sys.exit(1)
+ except Exception, error:
+ logging.critical("Execution failed: %s", error)
+ sys.exit(1)
+
+
+def handle_vfat(device):
+ """TODO"""
+
+ # make sure we have mkfs.vfat available
+ if options.fat16 and not options.force:
+ if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
+ logging.critical('Sorry, mkfs.vfat not available. Exiting.')
+ logging.critical('Please make sure to install dosfstools.')
+ sys.exit(1)
+
+ # make sure the user is aware of what he is doing
+ f = raw_input("Are you sure you want to format the device with a fat16 filesystem? y/N ")
+ if f == "y" or f == "Y":
+ logging.info("Note: you can skip this question using the option --force")
+ mkfs_fat16(device)
+ else:
+ sys.exit(1)
+
+ # check for vfat filesystem
+ if device is not None and not os.path.isdir(device):
+ try:
+ check_for_fat(device)
+ except CriticalException, error:
+ logging.critical("Execution failed: %s", error)
+ sys.exit(1)
+
+ if not check_for_usbdevice(device):
+ print "Warning: the specified device %s does not look like a removable usb device." % device
+ f = raw_input("Do you really want to continue? y/N ")
+ if f == "y" or f == "Y":
+ pass
+ else:
+ sys.exit(1)
+
+ # format partition:
+ if options.fat16:
+ mkfs_fat16(device)
+
+
+def handle_compat_warning(device):
+ """TODO"""
+
+ # make sure we can replace old grml2usb script and warn user when using old way of life:
+ if device.startswith("/mnt/external") or device.startswith("/mnt/usb") and not options.force:
+ print "Warning: the semantics of grml2usb has changed."
+ print "Instead of using grml2usb /path/to/iso %s you might" % device
+ print "want to use grml2usb /path/to/iso /dev/... instead."
+ print "Please check out the grml2usb manpage for details."
+ f = raw_input("Do you really want to continue? y/N ")
+ if f == "y" or f == "Y":
+ pass
+ else:
+ sys.exit(1)
+
+
+def handle_logging():
+ """TODO"""
+
+ if options.verbose:
+ FORMAT = "%(asctime)-15s %(message)s"
+ logging.basicConfig(level=logging.DEBUG, format=FORMAT)
+ elif options.quiet:
+ FORMAT = "Critial: %(message)s"
+ logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
+ else:
+ FORMAT = "Info: %(message)s"
+ logging.basicConfig(level=logging.INFO, format=FORMAT)
+
+
+def handle_bootloader(device):
+ """TODO"""
+ # Install bootloader only if not using the --copy-only option
+ if options.copyonly:
+ logging.info("Not installing bootloader and its files as requested via option copyonly.")
+ else:
+ install_bootloader(device)
+
+