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"
	bootsplash = model.BoolOption(u"Disable boot animation","Disable boot animation","Disable bootsplash")
        for i in self.targets:
            location.add_option(unicode(i))

        return Render(title, intro, bootloaders, location, bootsplash)

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

        """
        return None

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

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

    def disable_bootsplash(self, splash_disabled):
	"""Disable the bootsplash if asked to by the preference set by splash_opt.
	splash_opt is a Boolean"""
	ramdiskdir="/mnt/TARGET/boot"
	if not splash_disabled:
	    return
	else:
	    # Disable the bootsplash by renaming the initrd.
	    if not os.path.exists(os.path.join(ramdiskdir,'initrd.gz')):
	        LOG.error("User selected to disable bootsplash, but no initrd was found in target")
		return
	    LOG.debug("Renaming initrd.gz to disable bootsplash animation per user's request")
	    os.rename(os.path.join(ramdiskdir,"initrd.gz"),
		      os.path.join(ramdiskdir,"disabledinitrd.gz"))

    def command(self, bootloader, target, bootsplash):
        """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.depmod, tuple(), "Running depmod"
        yield self.mkinitrd, tuple(), "Creating initrd"
	if bootsplash: # This will have a True value if it was disabled by the user
	    LOG.debug("User selected to disable the bootsplash animation.  Scheduling method to disable it.")
	    yield self.disable_bootsplash, (bootsplash,), "Setting bootsplash preference"
        yield self.install_bootloader, (bootloader, target), "Installing bootloader"
        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():
	    LOG.debug("Adding %s entry to LiLo boot menu"% system.label)
            lilo.add_os(system)
	# Create the TUI entry for LiLO
	LOG.debug("Adding Linux-tui entry to LiLO boot menu")
	tui = bootloader.OperatingSystem()
	rootpart = self.config['target_device']
	tui.kernel = "/boot/vmlinuz"
	tui.root = rootpart.path()
	tui.type = "linux"
	tui.initrd = None
	tui.label = "Linux-tui"
	tui.appendline = 'root=%s ro vga=normal 2'% tui.root
	lilo.add_os(tui)
	

        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.