summaryrefslogtreecommitdiff
path: root/vmdebootstrap/grub.py
blob: 95b17eb3c339c1da36a9465f70b532f088407a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
  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/>.

# pylint: disable=missing-docstring,duplicate-code


import os
import cliapp
import logging
from vmdebootstrap.base import (
    Base,
    runcmd,
    mount_wrapper,
    umount_wrapper
)
from vmdebootstrap.uefi import arch_table


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__()

    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.")
            umount_wrapper(rootdir)
            return False
        umount_wrapper(rootdir)
        return True

    def install_grub_uefi(self, rootdir):
        ret = True
        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)
            ret = False
            self.message(
                "Failed to configure grub-uefi for %s" %
                self.settings['arch'])
        finally:
            umount_wrapper(rootdir)
        if not ret:
            raise cliapp.AppException("Failed to install grub uefi")

    def install_extra_grub_uefi(self, rootdir):
        ret = True
        extra = arch_table[self.settings['arch']]['extra']
        if extra:
            logging.debug("Installing extra grub support for %s", extra)
            mount_wrapper(rootdir)
            target = arch_table[extra]['target']
            grub_opts = "--target=%s" % target
            self.message("Adding grub target %s" % grub_opts)
            try:
                runcmd(['chroot', rootdir, 'update-grub'])
                runcmd(['chroot', rootdir, 'grub-install', grub_opts])
            except cliapp.AppException as exc:
                logging.warning(exc)
                ret = False
                self.message(
                    "Failed to configure grub-uefi for %s" % extra)
            finally:
                umount_wrapper(rootdir)
            if not ret:
                raise cliapp.AppException("Failed to install extra grub uefi")

    def grub_packages(self):
        if self.settings['grub'] and not self.settings['use-uefi']:
            return ['grub-pc']
        return []