bootimg-biosplusefi.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License version 2 as
  4. # published by the Free Software Foundation.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License along
  12. # with this program; if not, write to the Free Software Foundation, Inc.,
  13. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. #
  15. # DESCRIPTION
  16. # This implements the 'bootimg-biosplusefi' source plugin class for 'wic'
  17. #
  18. # AUTHORS
  19. # William Bourque <wbourque [at) gmail.com>
  20. import types
  21. from wic.pluginbase import SourcePlugin
  22. from importlib.machinery import SourceFileLoader
  23. class BootimgBiosPlusEFIPlugin(SourcePlugin):
  24. """
  25. Create MBR + EFI boot partition
  26. This plugin creates a boot partition that contains both
  27. legacy BIOS and EFI content. It will be able to boot from both.
  28. This is useful when managing PC fleet with some older machines
  29. without EFI support.
  30. Note it is possible to create an image that can boot from both
  31. legacy BIOS and EFI by defining two partitions : one with arg
  32. --source bootimg-efi and another one with --source bootimg-pcbios.
  33. However, this method has the obvious downside that it requires TWO
  34. partitions to be created on the storage device.
  35. Both partitions will also be marked as "bootable" which does not work on
  36. most BIOS, has BIOS often uses the "bootable" flag to determine
  37. what to boot. If you have such a BIOS, you need to manually remove the
  38. "bootable" flag from the EFI partition for the drive to be bootable.
  39. Having two partitions also seems to confuse wic : the content of
  40. the first partition will be duplicated into the second, even though it
  41. will not be used at all.
  42. Also, unlike "isoimage-isohybrid" that also does BIOS and EFI, this plugin
  43. allows you to have more than only a single rootfs partitions and does
  44. not turn the rootfs into an initramfs RAM image.
  45. This plugin is made to put everything into a single /boot partition so it
  46. does not have the limitations listed above.
  47. The plugin is made so it does tries not to reimplement what's already
  48. been done in other plugins; as such it imports "bootimg-pcbios"
  49. and "bootimg-efi".
  50. Plugin "bootimg-pcbios" is used to generate legacy BIOS boot.
  51. Plugin "bootimg-efi" is used to generate the UEFI boot. Note that it
  52. requires a --sourceparams argument to know which loader to use; refer
  53. to "bootimg-efi" code/documentation for the list of loader.
  54. Imports are handled with "SourceFileLoader" from importlib as it is
  55. otherwise very difficult to import module that has hyphen "-" in their
  56. filename.
  57. The SourcePlugin() methods used in the plugins (do_install_disk,
  58. do_configure_partition, do_prepare_partition) are then called on both,
  59. beginning by "bootimg-efi".
  60. Plugin options, such as "--sourceparams" can still be passed to a
  61. plugin, as long they does not cause issue in the other plugin.
  62. Example wic configuration:
  63. part /boot --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\\
  64. --ondisk sda --label os_boot --active --align 1024 --use-uuid
  65. """
  66. name = 'bootimg-biosplusefi'
  67. __PCBIOS_MODULE_NAME = "bootimg-pcbios"
  68. __EFI_MODULE_NAME = "bootimg-efi"
  69. __imgEFIObj = None
  70. __imgBiosObj = None
  71. @classmethod
  72. def __init__(cls):
  73. """
  74. Constructor (init)
  75. """
  76. # XXX
  77. # For some reasons, __init__ constructor is never called.
  78. # Something to do with how pluginbase works?
  79. cls.__instanciateSubClasses()
  80. @classmethod
  81. def __instanciateSubClasses(cls):
  82. """
  83. """
  84. # Import bootimg-pcbios (class name "BootimgPcbiosPlugin")
  85. modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
  86. cls.__PCBIOS_MODULE_NAME + ".py")
  87. loader = SourceFileLoader(cls.__PCBIOS_MODULE_NAME, modulePath)
  88. mod = types.ModuleType(loader.name)
  89. loader.exec_module(mod)
  90. cls.__imgBiosObj = mod.BootimgPcbiosPlugin()
  91. # Import bootimg-efi (class name "BootimgEFIPlugin")
  92. modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
  93. cls.__EFI_MODULE_NAME + ".py")
  94. loader = SourceFileLoader(cls.__EFI_MODULE_NAME, modulePath)
  95. mod = types.ModuleType(loader.name)
  96. loader.exec_module(mod)
  97. cls.__imgEFIObj = mod.BootimgEFIPlugin()
  98. @classmethod
  99. def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
  100. bootimg_dir, kernel_dir, native_sysroot):
  101. """
  102. Called after all partitions have been prepared and assembled into a
  103. disk image.
  104. """
  105. if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
  106. cls.__instanciateSubClasses()
  107. cls.__imgEFIObj.do_install_disk(
  108. disk,
  109. disk_name,
  110. creator,
  111. workdir,
  112. oe_builddir,
  113. bootimg_dir,
  114. kernel_dir,
  115. native_sysroot)
  116. cls.__imgBiosObj.do_install_disk(
  117. disk,
  118. disk_name,
  119. creator,
  120. workdir,
  121. oe_builddir,
  122. bootimg_dir,
  123. kernel_dir,
  124. native_sysroot)
  125. @classmethod
  126. def do_configure_partition(cls, part, source_params, creator, cr_workdir,
  127. oe_builddir, bootimg_dir, kernel_dir,
  128. native_sysroot):
  129. """
  130. Called before do_prepare_partition()
  131. """
  132. if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
  133. cls.__instanciateSubClasses()
  134. cls.__imgEFIObj.do_configure_partition(
  135. part,
  136. source_params,
  137. creator,
  138. cr_workdir,
  139. oe_builddir,
  140. bootimg_dir,
  141. kernel_dir,
  142. native_sysroot)
  143. cls.__imgBiosObj.do_configure_partition(
  144. part,
  145. source_params,
  146. creator,
  147. cr_workdir,
  148. oe_builddir,
  149. bootimg_dir,
  150. kernel_dir,
  151. native_sysroot)
  152. @classmethod
  153. def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
  154. oe_builddir, bootimg_dir, kernel_dir,
  155. rootfs_dir, native_sysroot):
  156. """
  157. Called to do the actual content population for a partition i.e. it
  158. 'prepares' the partition to be incorporated into the image.
  159. """
  160. if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
  161. cls.__instanciateSubClasses()
  162. cls.__imgEFIObj.do_prepare_partition(
  163. part,
  164. source_params,
  165. creator,
  166. cr_workdir,
  167. oe_builddir,
  168. bootimg_dir,
  169. kernel_dir,
  170. rootfs_dir,
  171. native_sysroot)
  172. cls.__imgBiosObj.do_prepare_partition(
  173. part,
  174. source_params,
  175. creator,
  176. cr_workdir,
  177. oe_builddir,
  178. bootimg_dir,
  179. kernel_dir,
  180. rootfs_dir,
  181. native_sysroot)