def prepare(self, kernel = None, kernel_options = ""): """Prepare the configuration file.""" grub_image = "kernel" + "".join(kernel.image.partition("-")[1:]) if self.arguments["dry_run"]: dry_list = [ "pushd /boot", "ln -s {kernel_image} {grub_image}".format( grub_image = grub_image, kernel_image = kernel.image), "popd", # TODO find a better way to depict this ... "".join([ "sed -i -e 's/(GRUB_CMDLINE_LINUX_DEFAULT=\")(.*)(\")", "/\\0\\1 {kernel_options}\\3/' {defaults}", ]).format(kernel_options = kernel_options, defaults = self.grub_defaults_uri), ] helpers.colorize("GREEN", "\n".join(dry_list)) else: original_pwd = os.getcwd() os.chdir("/boot") if not os.path.islink(grub_image): os.symlink(kernel.image, grub_image) os.chdir(original_pwd) new_grub_defaults = [] for line in open(self.grub_defaults_uri, "r").readlines(): line = line.rstrip("\n") if len(kernel_options) and re.search( r"GRUB_CMDLINE_LINUX_DEFAULT", line): if self.arguments["debug"]: helpers.debug({ "line": line, "line[:-1]": line[:-1], }) line = line[:-1] + " " + kernel_options + "\"" new_grub_defaults.append(line.rstrip("\n")) else: new_grub_defaults.append(line.rstrip("\n")) grub_defaults = open(self.grub_defaults_uri, "w") grub_defaults.write("\n".join(new_grub_defaults)) grub_defaults.flush() grub_defaults.close()
def grub_root(self): """The grub root parameter.""" if not hasattr(self, "_grub_root"): match = re.match(r"/dev/[\w\d]+(?P<letter>\w)(?P<number>\d+)", self.boot_partition) if self.arguments["debug"]: helpers.debug({ "self.boot_partition": self.boot_partition, }) self._grub_root = "(hd{letter!s},{number!s})".format( letter = "abcdefghijklmnopqrstuvwxyz".find( match.group("letter")), number = -1 + int(match.group("number"))) return self._grub_root
def install(self): """Install the configuration and make the system bootable.""" if self.arguments["dry_run"]: dry_list = [ "pushd /boot/grub2", "cp {grub_config}{{.,bak}}", "grub2-mkconfig -o {grub_config}", "rm {grub_config}.bak", "popd", ] helpers.colorize("GREEN", "\n".join(dry_list).format( grub_config = self.configuration_uri)) else: original_directory = os.getcwd() try: os.chdir("/boot/grub2") if os.access(self.configuration_uri, os.W_OK): shutil.copy(self.configuration_uri, "{grub_config}.bak".format(grub_config = self.configuration_uri)) status = subprocess.call( "grub2-mkconfig -o {grub_config}".format( grub_config = self.configuration_uri), shell = True) if status != 0: pass # TODO raise an appropriate error except Exception as error: if self.arguments["debug"]: helpers.debug({ "error": error, }) if os.access("{grub_config}.bak".format( grub_config = self.configuration_uri), os.W_OK): os.rename("{grub_config}.bak".format( grub_config = self.configuration_uri), self.configuration_uri) raise error finally: if os.access("{grub_config}.bak".format( grub_config = self.configuration_uri), os.W_OK): os.remove("{grub_config}.bak".format( grub_config = self.configuration_uri)) os.chdir(original_directory)
def prepare(self, kernel = None, kernel_options = "", initrd = False): """Prepare the configuration file.""" if not self.arguments["quiet"]: print("Preparing GRUB configuration ...") if not self._has_kernel(kernel.name): new_configuration = [] options = [] for line in self.configuration: if re.search("default", line, re.I): new_configuration.append("".join([ "default {default!s}".format( default = 1 + int(line.partition(" ")[2])), ])) elif re.search("^[^#]*kernel", line, re.I): new_configuration.append(line) if self.arguments["debug"]: helpers.debug({ "kernel_options": kernel_options, "kernel_options.split(" ")": kernel_options.split(" "), "type(kernel_options)": type(kernel_options), }) options = [ option.strip() for option in line.split(" ") \ if not re.search("(?:kernel|/boot/|root=)", option, re.I) ] if self.arguments["debug"]: helpers.debug({ "options": options, "type(options)": type(options), "len(kernel_options)": len(kernel_options), }) else: new_configuration.append(line) if len(kernel_options): kernel_options = " ".join(list(set( kernel_options.split(" ").extend(options)))) else: kernel_options = " ".join(list(set(options))) kernel_options = kernel_options.lstrip(" ") if self.arguments["debug"]: helpers.debug({ "kernel_options": kernel_options, }) kernel_entry = [ "", "# Kernel added {time!s}:".format( time = datetime.datetime.now()), "title={kernel_name}".format(kernel_name = kernel.name), " root {grub_root}".format(grub_root = self.grub_root), " kernel /boot/{image} root={root} {options}".format( image = kernel.image, root = self.root_partition, options = kernel_options), ] if self.arguments["debug"]: helpers.debug({ "kernel_entry": kernel_entry, }) if initrd: kernel_entry.append( " initrd /boot/{initrd}".format( initrd = kernel.initrd)) new_configuration.extend(kernel_entry) self.configuration = new_configuration if not self.arguments["quiet"]: print("GRUB configuration prepared.")