示例#1
0
文件: binary.py 项目: alunduil/upkern
    def install_initramfs(self, dracut_options = ""):
        """Build and install the initramfs using dracut."""

        if not self.arguments["quiet"]:
            print("Building and installing initramfs ...")

        if not len(GentoolkitQuery("sys-kernel/dracut").find_installed()):
            return

        if self.arguments["verbose"]:
            helpers.verbose("Building and Installing initramfs: True")

        command = [
                "dracut -H --force {options} /boot/{initrd} {suffix}".format(
                    options = dracut_options,
                    initrd = self.initrd,
                    suffix = self.suffix[1:]),
                ]

        if self.arguments["dry_run"]:
            helpers.colorize("GREEN", "".join(command))
        else:
            status = subprocess.call("".join(command), shell = True)
            if status != 0:
                pass # TODO raise an appropriate exception

        if not self.arguments["quiet"]:
            print("initramfs built and installed.")
示例#2
0
文件: grub2.py 项目: alunduil/upkern
    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()
示例#3
0
文件: grub2.py 项目: alunduil/upkern
    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)
示例#4
0
文件: grub.py 项目: alunduil/upkern
    def install(self):
        """Install the configuration and make the system bootable."""

        if not self.arguments["quiet"]:
            print("Installing GRUB configuration.")

        if self.arguments["dry_run"]:
            dry_list = [
                    "cp {config}{{,.bak}}".format(
                        config = self.configuration_uri),
                    "cat > {config}".format(config = self.configuration_uri),
                    "\n".join(self.configuration),
                    "^d",
                    "rm {config}.bak".format(config = self.configuration_uri),
                    ]
            helpers.colorize("GREEN", "\n".join(dry_list))
        else:
            if os.access(self.configuration_uri, os.W_OK):
                try:
                    shutil.copy(self.configuration_uri, "{config}.bak".format(
                        config = self.configuration_uri))
                    configuration = open(self.configuration_uri, "w")
                    configuration.write("\n".join(self.configuration))
                    configuration.flush()
                    configuration.close()
                except Exception as error:
                    os.rename("{config}.bak".format(
                        config = self.configuration_uri),
                        self.configuration_uri)
                    raise error
                finally:
                    if os.access("{config}.bak".format(
                        config = self.configuration_uri), os.W_OK):
                        os.remove("{config}.bak".format(
                            config = self.configuration_uri))

        if not self.arguments["quiet"]:
            print("GRUB configuration installed.")
示例#5
0
文件: binary.py 项目: alunduil/upkern
    def install(self):
        """Install the kernel into /boot.

        1. Go into the source directory.
        2. Copy the image file to /boot.
        3. Copy the config file to /boot
        4. Copy System.map to /boot.
        5. Copy System.map to /

        """

        if not self.arguments["quiet"]:
            print("Installing the kernel binaries ...")

        original_directory = os.getcwd()

        if self.arguments["dry_run"]:
            dry_list = [
                    "pushd /usr/src/linux",
                    "cp {directory}{image} /boot/{image}{suffix}".format(
                        directory = self.image_directory,
                        image = self.install_image, suffix = self.suffix),
                    "cp .config /boot/config{suffix}".format(
                        suffix = self.suffix),
                    "cp System.map /boot/System.map{suffix}".format(
                        suffix = self.suffix),
                    "cp System.map /System.map",
                    "popd",
                    ]
            helpers.colorize("GREEN", "\n".join(dry_list))
        else:
            def undo():
                if os.access("/boot/{image}{suffix}".format(
                    image = self.install_image, suffix = self.suffix), os.W_OK):
                    os.remove("/boot/{image}{suffix}".format(
                        image = self.install_image, suffix = self.suffix))
                if os.access("/boot/config{suffix}".format(
                    suffix = self.suffix), os.W_OK):
                    os.remove("/boot/config{suffix}".format(
                        suffix = self.suffix))
                if os.access("/bot/System.map{suffix}".format(
                    suffix = self.suffix), os.W_OK):
                    os.remove("/boot/System.map{suffix}".format(
                        suffix = self.suffix))
                if os.access("/System.map", os.W_OK) and os.access(
                    "/System.map.bak", os.W_OK):
                    os.remove("/System.map")
                    os.rename("/System.map.bak", "/System.map")
            try:
                os.chdir("/usr/src/linux")
                shutil.copy("{directory}{image}".format(
                    directory = self.image_directory, image = self.install_image),
                    "/boot/{image}{suffix}".format(image = self.install_image,
                        suffix = self.suffix))
                shutil.copy(".config", "/boot/config{suffix}".format(
                    suffix = self.suffix))
                shutil.copy("System.map", "/boot/System.map{suffix}".format(
                    suffix = self.suffix))
                if os.access("/System.map", os.W_OK):
                    shutil.copy("/System.map", "/System.map.bak")
                shutil.copy("System.map", "/System.map")
                os.chdir(original_directory)
            except IOError as error:
                undo()
                raise IOError(error.errno, error.strerror, "/boot")
            except Exception as error:
                undo()
                raise error
            finally:
                if os.access("/System.map.bak", os.W_OK):
                    os.remove("/System.map.bak")

        if not self.arguments["quiet"]:
            print("Kernel binaries installed.")