diff options
author | Neil Williams <codehelp@debian.org> | 2015-08-15 14:33:59 +0200 |
---|---|---|
committer | Neil Williams <codehelp@debian.org> | 2015-08-16 14:28:35 +0200 |
commit | 54b1ab7ea71a857791189d7a2108c8b76cbfde2a (patch) | |
tree | 1515a90e7e8a8986277adc2654f001e8ecdbcce0 /vmdebootstrap/grub.py | |
parent | ca8829488c49ae4801ef91eed6d17f7bad892eef (diff) | |
download | vmdebootstrap-54b1ab7ea71a857791189d7a2108c8b76cbfde2a.tar.gz |
move handlers into a module directory
Diffstat (limited to 'vmdebootstrap/grub.py')
-rw-r--r-- | vmdebootstrap/grub.py | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/vmdebootstrap/grub.py b/vmdebootstrap/grub.py new file mode 100644 index 0000000..8d55ce1 --- /dev/null +++ b/vmdebootstrap/grub.py @@ -0,0 +1,103 @@ +""" + Wrapper for Grub operations +""" +# -*- coding: utf-8 -*- +# +# grub.py +# +# Copyright 2015 Neil Williams <codehelp@debian.org> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import os +import cliapp +import logging +from vmdebootstrap.base import ( + Base, + runcmd, + mount_wrapper, + umount_wrapper +) +from vmdebootstrap.uefi import Uefi, arch_table + +# pylint: disable=missing-docstring + + +def grub_serial_console(rootdir): + cmdline = 'GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=tty1 console=ttyS0,38400n8"' + terminal = 'GRUB_TERMINAL="serial gfxterm"' + command = 'GRUB_SERIAL_COMMAND="serial --speed=38400 --unit=0 --parity=no --stop=1"' + grub_cfg = os.path.join(rootdir, 'etc', 'default', 'grub') + logging.debug("Allowing serial output in grub config %s", grub_cfg) + with open(grub_cfg, 'a+') as cfg: + cfg.write("# %s serial support\n" % os.path.basename(__file__)) + cfg.write("%s\n" % cmdline) + cfg.write("%s\n" % terminal) + cfg.write("%s\n" % command) + + +class GrubHandler(Base): + + name = 'grub' + + def __init__(self): + super(GrubHandler, self).__init__() + self.uefi = Uefi() + + def install_grub2(self, rootdev, rootdir): + self.message("Configuring grub2") + # rely on kpartx using consistent naming to map loop0p1 to loop0 + grub_opts = os.path.join('/dev', os.path.basename(rootdev)[:-2]) + if self.settings['serial-console']: + grub_serial_console(rootdir) + logging.debug("Running grub-install with options: %s", grub_opts) + mount_wrapper(rootdir) + try: + runcmd(['chroot', rootdir, 'update-grub']) + runcmd(['chroot', rootdir, 'grub-install', grub_opts]) + except cliapp.AppException as exc: + logging.warning(exc) + self.message("Failed. Is grub2-common installed? Using extlinux.") + self.install_extlinux(rootdev, rootdir) # FIXME! + umount_wrapper(rootdir) + + def install_grub_uefi(self, rootdir): + self.message("Configuring grub-uefi") + target = arch_table[self.settings['arch']]['target'] + grub_opts = "--target=%s" % target + logging.debug("Running grub-install with options: %s", grub_opts) + mount_wrapper(rootdir) + try: + runcmd(['chroot', rootdir, 'update-grub']) + runcmd(['chroot', rootdir, 'grub-install', grub_opts]) + except cliapp.AppException as exc: + logging.warning(exc) + self.message( + "Failed to configure grub-uefi for %s" % + self.settings['arch']) + umount_wrapper(rootdir) + self.uefi.configure_efi() # FIXME + extra = str(arch_table[self.settings['arch']]['extra']) + if extra: + target = arch_table[extra]['target'] + grub_opts = "--target=%s" % target + try: + runcmd(['chroot', rootdir, 'update-grub']) + runcmd(['chroot', rootdir, 'grub-install', grub_opts]) + except cliapp.AppException as exc: + logging.warning(exc) + self.message( + "Failed to configure grub-uefi for %s" % extra) + self.uefi.configure_extra_efi() # FIXME + umount_wrapper(rootdir) |