summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/plugins/source/bootimg-biosplusefi.py
blob: 5bd7390680b56cf8474987c7a50f020495564d4e (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# DESCRIPTION
# This implements the 'bootimg-biosplusefi' source plugin class for 'wic'
#
# AUTHORS
# William Bourque <wbourque [at) gmail.com>

import types

from wic.pluginbase import SourcePlugin
from importlib.machinery import SourceFileLoader

class BootimgBiosPlusEFIPlugin(SourcePlugin):
    """
    Create MBR + EFI boot partition

    This plugin creates a boot partition that contains both
    legacy BIOS and EFI content. It will be able to boot from both.
    This is useful when managing PC fleet with some older machines
    without EFI support.

    Note it is possible to create an image that can boot from both
    legacy BIOS and EFI by defining two partitions : one with arg
    --source bootimg-efi  and another one with --source bootimg-pcbios.
    However, this method has the obvious downside that it requires TWO
    partitions to be created on the storage device.
    Both partitions will also be marked as "bootable" which does not work on
    most BIOS, has BIOS often uses the "bootable" flag to determine
    what to boot. If you have such a BIOS, you need to manually remove the
    "bootable" flag from the EFI partition for the drive to be bootable.
    Having two partitions also seems to confuse wic : the content of
    the first partition will be duplicated into the second, even though it
    will not be used at all.

    Also, unlike "isoimage-isohybrid" that also does BIOS and EFI, this plugin
    allows you to have more than only a single rootfs partitions and does
    not turn the rootfs into an initramfs RAM image.

    This plugin is made to put everything into a single /boot partition so it
    does not have the limitations listed above.

    The plugin is made so it does tries not to reimplement what's already
    been done in other plugins; as such it imports "bootimg-pcbios"
    and "bootimg-efi".
    Plugin "bootimg-pcbios" is used to generate legacy BIOS boot.
    Plugin "bootimg-efi" is used to generate the UEFI boot. Note that it
    requires a --sourceparams argument to know which loader to use; refer
    to "bootimg-efi" code/documentation for the list of loader.

    Imports are handled with "SourceFileLoader" from importlib as it is
    otherwise very difficult to import module that has hyphen "-" in their
    filename.
    The SourcePlugin() methods used in the plugins (do_install_disk,
    do_configure_partition, do_prepare_partition) are then called on both,
    beginning by "bootimg-efi".

    Plugin options, such as "--sourceparams" can still be passed to a
    plugin, as long they does not cause issue in the other plugin.

    Example wic configuration:
    part /boot --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\\
               --ondisk sda --label os_boot --active --align 1024 --use-uuid
    """

    name = 'bootimg-biosplusefi'

    __PCBIOS_MODULE_NAME = "bootimg-pcbios"
    __EFI_MODULE_NAME = "bootimg-efi"

    __imgEFIObj = None
    __imgBiosObj = None

    @classmethod
    def __init__(cls):
        """
        Constructor (init)
        """

        # XXX
        # For some reasons, __init__ constructor is never called.
        # Something to do with how pluginbase works?
        cls.__instanciateSubClasses()

    @classmethod
    def __instanciateSubClasses(cls):
        """

        """

        # Import bootimg-pcbios (class name "BootimgPcbiosPlugin")
        modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                  cls.__PCBIOS_MODULE_NAME + ".py")
        loader = SourceFileLoader(cls.__PCBIOS_MODULE_NAME, modulePath)
        mod = types.ModuleType(loader.name)
        loader.exec_module(mod)
        cls.__imgBiosObj = mod.BootimgPcbiosPlugin()

        # Import bootimg-efi (class name "BootimgEFIPlugin")
        modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                  cls.__EFI_MODULE_NAME + ".py")
        loader = SourceFileLoader(cls.__EFI_MODULE_NAME, modulePath)
        mod = types.ModuleType(loader.name)
        loader.exec_module(mod)
        cls.__imgEFIObj = mod.BootimgEFIPlugin()

    @classmethod
    def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
                        bootimg_dir, kernel_dir, native_sysroot):
        """
        Called after all partitions have been prepared and assembled into a
        disk image.
        """

        if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
            cls.__instanciateSubClasses()

        cls.__imgEFIObj.do_install_disk(
            disk,
            disk_name,
            creator,
            workdir,
            oe_builddir,
            bootimg_dir,
            kernel_dir,
            native_sysroot)

        cls.__imgBiosObj.do_install_disk(
            disk,
            disk_name,
            creator,
            workdir,
            oe_builddir,
            bootimg_dir,
            kernel_dir,
            native_sysroot)

    @classmethod
    def do_configure_partition(cls, part, source_params, creator, cr_workdir,
                               oe_builddir, bootimg_dir, kernel_dir,
                               native_sysroot):
        """
        Called before do_prepare_partition()
        """

        if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
            cls.__instanciateSubClasses()

        cls.__imgEFIObj.do_configure_partition(
            part,
            source_params,
            creator,
            cr_workdir,
            oe_builddir,
            bootimg_dir,
            kernel_dir,
            native_sysroot)

        cls.__imgBiosObj.do_configure_partition(
            part,
            source_params,
            creator,
            cr_workdir,
            oe_builddir,
            bootimg_dir,
            kernel_dir,
            native_sysroot)

    @classmethod
    def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
                             oe_builddir, bootimg_dir, kernel_dir,
                             rootfs_dir, native_sysroot):
        """
        Called to do the actual content population for a partition i.e. it
        'prepares' the partition to be incorporated into the image.
        """

        if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
            cls.__instanciateSubClasses()

        cls.__imgEFIObj.do_prepare_partition(
            part,
            source_params,
            creator,
            cr_workdir,
            oe_builddir,
            bootimg_dir,
            kernel_dir,
            rootfs_dir,
            native_sysroot)

        cls.__imgBiosObj.do_prepare_partition(
            part,
            source_params,
            creator,
            cr_workdir,
            oe_builddir,
            bootimg_dir,
            kernel_dir,
            rootfs_dir,
            native_sysroot)