]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
bootloaders: Write a proper GRUB configuration file
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Feb 2023 17:59:42 +0000 (17:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Feb 2023 18:17:44 +0000 (18:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/__init__.py
src/python/bootloaders.py

index e92e8ac13b076eaa6795815d2927756555d0cb7c..03edb7e7fbeb536a810c4a59d7ba6349101c1ba3 100644 (file)
@@ -217,6 +217,20 @@ class Bricklayer(object):
                with open("/etc/os-release") as f:
                        return util.config_read(f)
 
+       def open(self, path, *args, **kwargs):
+               """
+                       Opens a file inside the environment
+               """
+               # Make the path relative
+               while path.startswith("/"):
+                       path = path[1:]
+
+               # Make the path relative to the root directory
+               path = os.path.join(self.root, path)
+
+               # Open the file and return the handle
+               return open(path, *args, **kwargs)
+
        def command(self, command, error_ok=False, interactive=False, chroot=False, bind=None):
                """
                        Runs a command in a shell environment
index d8cb9064351eb3aeadd6070f646eb778ff40a29c..665daebf851bfe39e1aa2ea34df51bb0b2106204 100644 (file)
@@ -27,6 +27,8 @@ from .i18n import _, N_
 # Setup logging
 log = logging.getLogger("bricklayer.bootloaders")
 
+GRUB_TIMEOUT = 5
+
 class Bootloader(object):
        """
                A generic class for bootloaders
@@ -82,6 +84,9 @@ class Grub(Bootloader):
        requires_bootldr_partition = True
 
        def install(self):
+               # Write the configuration file
+               self.write_configuration()
+
                # Install GRUB2 on all disks that have been selected
                for disk in self.bricklayer.disks.selected:
                        self.install_on_disk(disk)
@@ -107,6 +112,41 @@ class Grub(Bootloader):
                        *self.grub_args, disk.path,
                ], chroot=True, bind=["/dev", "/proc", "/sys"])
 
+       def write_configuration(self):
+               """
+                       This method writes /etc/default/grub
+               """
+               log.debug("Writing GRUB Configuration:")
+
+               conf = {
+                       # Tell GRUB who we are
+                       "GRUB_DISTRIBUTOR" : "\"$(sed 's, release .*$,,g' /etc/system-release)\"",
+
+                       # Give the user a moment to select an option
+                       "GRUB_TIMEOUT" : GRUB_TIMEOUT,
+
+                       # Remember the selected option
+                       "GRUB_DEFAULT" : "saved",
+
+                       # Do not show a submenu
+                       "GRUB_DISABLE_SUBMENU" : "true",
+
+                       # Disable the recovery option
+                       "GRUB_DISABLE_RECOVERY" : "true",
+               }
+
+               # XXX Handle serial console
+               conf["GRUB_TERMINAL_OUTPUT"] = "\"console\""
+
+               # Write everything to file
+               with self.bricklayer.open("/etc/default/grub", "w") as f:
+                       for key, val in conf.items():
+                               # Log the line
+                               log.debug("    %s = %s" % (key, val))
+
+                               # Write the line to file
+                               f.write("%s=%s\n" % (key, val))
+
        def install_configuration(self):
                """
                        Generates a GRUB configuration file