+ @grml_flavour: name of grml flavour the configuration should be generated for
+ @device: device/partition where grub should be installed to
+ @target: path of grub's configuration files"""
+
+ global UUID
+
+ logging.debug("Updating grub configuration")
+
+ grub_target = target + "/boot/grub/"
+ secureboot_target = target + "/EFI/ubuntu/"
+
+ bootid_re = re.compile(r"bootid=[\w_-]+")
+ live_media_path_re = re.compile(r"live-media-path=[\w_/-]+")
+
+ bootopt = get_bootoptions(grml_flavour)
+
+ remove_regexes = []
+ option_re = re.compile(r"(.*/boot/.*(linux26|vmlinuz).*)")
+
+ if options.removeoption:
+ for regex in options.removeoption:
+ remove_regexes.append(re.compile(regex))
+
+ shortname = get_shortname(grml_flavour)
+ for filename in glob.glob(grub_target + "*.cfg") + glob.glob(
+ secureboot_target + "*.cfg"
+ ):
+ for line in fileinput.input(filename, inplace=1):
+ line = line.rstrip("\r\n")
+ if option_re.search(line):
+ line = bootid_re.sub("", line)
+ if shortname in filename:
+ line = live_media_path_re.sub("", line)
+ line = line.rstrip() + " live-media-path=/live/%s/ " % (
+ grml_flavour
+ )
+ if bootopt.strip():
+ line = line.replace(" {} ".format(bootopt.strip()), " ")
+ if line.endswith(bootopt):
+ line = line[: -len(bootopt)]
+ line = line.rstrip() + r" bootid=%s %s " % (UUID, bootopt)
+ for regex in remove_regexes:
+ line = regex.sub(" ", line)
+ print(line)
+ fileinput.close()
+
+
+def initial_syslinux_config(target):
+ """Generates initial syslinux configuration
+
+ @target path of syslinux's configuration files"""
+
+ target = target + "/"
+ filename = target + "grmlmain.cfg"
+ if os.path.isfile(target + "grmlmain.cfg"):
+ return
+ data = open(filename, "w")
+ data.write(generate_main_syslinux_config())
+ data.close()
+
+ filename = target + "hiddens.cfg"
+ data = open(filename, "w")
+ data.write("include hidden.cfg\n")
+ data.close()
+
+
+def add_entry_if_not_present(filename, entry):
+ """Write entry into filename if entry is not already in the file
+
+ @filename: name of the file
+ @entry: data to write to the file
+ """
+ data = open(filename, "a+")
+ data.seek(0)
+ for line in data:
+ if line == entry:
+ break
+ else:
+ data.write(entry)
+
+ data.close()
+
+
+def get_flavour_filename(flavour):
+ """Generate a iso9960 save filename out of the specified flavour
+
+ @flavour: grml flavour
+ """
+ return flavour.replace("-", "_")
+
+
+def adjust_syslinux_bootoptions(src, flavour):
+ """Adjust existing bootoptions of specified syslinux config to
+ grml2usb specific ones, e.g. change the location of the kernel...
+
+ @src: config file to alter
+ @flavour: grml flavour
+ """
+
+ append_re = re.compile(r"^(\s*append.*/boot/.*)$", re.I)
+ # flavour_re = re.compile("(label.*)(grml\w+)")
+ default_re = re.compile(r"(default.cfg)")
+ bootid_re = re.compile(r"bootid=[\w_-]+")
+ live_media_path_re = re.compile(r"live-media-path=[\w_/-]+")
+
+ bootopt = get_bootoptions(flavour)
+
+ regexe = []
+ option_re = None
+ if options.removeoption:
+ option_re = re.compile(r"/boot/.*/(initrd.gz|initrd.img)")
+
+ for regex in options.removeoption:
+ regexe.append(re.compile(r"%s" % regex))
+
+ for line in fileinput.input(src, inplace=1):
+ # line = flavour_re.sub(r'\1 %s-\2' % flavour, line)
+ line = default_re.sub(r"%s-\1" % flavour, line)
+ line = bootid_re.sub("", line)
+ line = live_media_path_re.sub("", line)
+ line = append_re.sub(r"\1 live-media-path=/live/%s/ " % flavour, line)
+ line = append_re.sub(r"\1 boot=live %s " % bootopt, line)
+ line = append_re.sub(r"\1 %s=%s " % ("bootid", UUID), line)
+ if option_re and option_re.search(line):
+ for regex in regexe:
+ line = regex.sub(" ", line)
+ sys.stdout.write(line)
+ fileinput.close()
+
+
+def adjust_labels(src, replacement):
+ """Adjust the specified labels in the syslinux config file src with
+ specified replacement
+ """
+ label_re = re.compile(r"^(\s*label\s*) ([a-zA-Z0-9_-]+)", re.I)
+ for line in fileinput.input(src, inplace=1):
+ line = label_re.sub(replacement, line)
+ sys.stdout.write(line)
+ fileinput.close()
+
+
+def add_syslinux_entry(filename, grml_flavour):
+ """Add includes for a specific grml_flavour to the specified filename
+
+ @filename: syslinux config file
+ @grml_flavour: grml flavour to add
+ """
+
+ entry_filename = "option_%s.cfg" % grml_flavour
+ entry = "include %s\n" % entry_filename
+
+ add_entry_if_not_present(filename, entry)
+ path = os.path.dirname(filename)
+
+ data = open(path + "/" + entry_filename, "w")
+ data.write(generate_flavour_specific_syslinux_config(grml_flavour))
+ data.close()
+
+
+def modify_filenames(grml_flavour, target, filenames):
+ """Replace the standard filenames with the new ones