Source

vinstall / vinstall / controller / bootloader.py

#-*- coding: utf-8 -*-

"""Bootloader options for the Vector installer

"""


import os
from vinstall.backend import bootloader, utils, sp
from vinstall.backend.media import Disk, Partition
from vinstall.core import Render, Controller, model, log


LOG = log.get_logger("bootloader")


class Bootloader(Controller):
    """Select a bootloader

    """
    def init(self):
        """Find the disks available in the system.

        """
        self.disks = [ i for i in Disk.all() if not i.is_read_only() ]
        self.partitions = [ i for i in Partition.all() if i.query_filesystem()
                != "xfs" ]
        self.targets = self.disks + self.partitions
        self.bootloaders = [ u"Lilo", u"Grub2", u"None" ]

    def render(self):
        """Show bootloader options

        """
        title = u"Bootloader"
        intro = u"Select a bootloader to boot your system"

        bootloaders = model.DropdownOptionList()
        bootloaders.label = u"Select a bootloader"
        for i in self.bootloaders:
            bootloaders.add_option(i)

        location = model.DropdownOptionList()
        location.label = u"Install the bootloader in"
        for i in self.targets:
            location.add_option(unicode(i))

        return Render(title, intro, bootloaders, location)

    def next(self):
        """Return next step

        """
        return None

    def previous(self):
        """Return previous step

        """
        from vinstall.controller import usersetup
        return usersetup.UserSetup

    def command(self, bootloader, target):
        """Schedule command for later execution

        """
        LOG.debug("Bootloader: %s", bootloader)
        LOG.debug("Target; %s", target)
        yield self.set_bind_mounts, tuple(), "Mounting pseudo filesystems"
        yield self.install_bootloader, (bootloader, target), "Installing bootloader"

        mkinitrd_doc = "Creating initrd"
        yield self.depmod, tuple(), "Running depmod"
        yield self.mkinitrd, tuple(), mkinitrd_doc
        yield self.clear_bind_mounts, tuple(), "Clearing bind mounts"

    def install_bootloader(self, bloader, target):
        """Install the bootloader

        """
        bloader = self.bootloaders[bloader]
        disk = self.targets[target]
        target = disk.path()

        if bloader.lower() == "lilo":
            self.install_lilo(target)
        elif bloader == "Grub2":
            self.install_grub(target)
        else:
            return

    def install_lilo(self, target):
        """ Install LILO to target

        """
        LOG.debug("Installing LILO to %s", target)
        lilo = bootloader.Lilo(target)
        for system in bootloader.OperatingSystem.all():
            lilo.add_os(system)
        with utils.Chroot("/mnt/TARGET"):
            lilo.backup_config()
            lilo.write_config()
            lilo.install()

    def install_grub(self, target):
        """ Install Grub2 to target

        """
        LOG.debug("Installing Grub2 to %s", target)
        grub2 = bootloader.Grub2(target)
        with utils.Chroot("/mnt/TARGET"):
            grub2.backup_config()
            grub2.install()
            grub2.write_config()

    def mkinitrd(self):
        LOG.debug("Creating initrd")
        device = self.config["target_device"]
        fs = self.config["target_device_fs"]
        err = None
        with utils.Chroot("/mnt/TARGET"):
            try:
                utils.mkinitrd(device.path(), fs)
            except Exception as e:
                # initrd no supported
                err = e
        if err is not None:
            LOG.error("Initrd not supported: %s", e)

    def depmod(self):
        LOG.debug("Running depmod -a")
        with utils.Chroot("/mnt/TARGET"):
            utils.depmod()

    def clear_bind_mounts(self):
        """Clear the mountpoints that were mounted for the configuration

        """
        for mpoint in ("sys","proc","dev"):
            cmd = "umount -f /mnt/TARGET/%s" % mpoint
            sp.check_call(cmd.split())

    def set_bind_mounts(self):
        """Bind mount some paths into the target.
        Later steps will need this when we chroot there

        """
        LOG.debug("Bind mounting filesystems")
        for point in ("sys","proc","dev"):
            cmd = "mount -o bind /%s /mnt/TARGET/%s" % (point, point)
            sp.check_call(cmd.split())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.