Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | # |
| 4 | # DESCRIPTION |
| 5 | # This implements the 'isoimage-isohybrid' source plugin class for 'wic' |
| 6 | # |
| 7 | # AUTHORS |
| 8 | # Mihaly Varga <mihaly.varga (at] ni.com> |
| 9 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | import glob |
| 11 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | import os |
| 13 | import re |
| 14 | import shutil |
| 15 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 16 | from wic import WicError |
| 17 | from wic.engine import get_custom_config |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | from wic.pluginbase import SourcePlugin |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 19 | from wic.misc import exec_cmd, exec_native_cmd, get_bitbake_var |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 20 | |
| 21 | logger = logging.getLogger('wic') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | |
| 23 | class IsoImagePlugin(SourcePlugin): |
| 24 | """ |
| 25 | Create a bootable ISO image |
| 26 | |
| 27 | This plugin creates a hybrid, legacy and EFI bootable ISO image. The |
| 28 | generated image can be used on optical media as well as USB media. |
| 29 | |
| 30 | Legacy boot uses syslinux and EFI boot uses grub or gummiboot (not |
| 31 | implemented yet) as bootloader. The plugin creates the directories required |
| 32 | by bootloaders and populates them by creating and configuring the |
| 33 | bootloader files. |
| 34 | |
| 35 | Example kickstart file: |
| 36 | part /boot --source isoimage-isohybrid --sourceparams="loader=grub-efi, \\ |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 37 | image_name= IsoImage" --ondisk cd --label LIVECD |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | bootloader --timeout=10 --append=" " |
| 39 | |
| 40 | In --sourceparams "loader" specifies the bootloader used for booting in EFI |
| 41 | mode, while "image_name" specifies the name of the generated image. In the |
| 42 | example above, wic creates an ISO image named IsoImage-cd.direct (default |
| 43 | extension added by direct imeger plugin) and a file named IsoImage-cd.iso |
| 44 | """ |
| 45 | |
| 46 | name = 'isoimage-isohybrid' |
| 47 | |
| 48 | @classmethod |
| 49 | def do_configure_syslinux(cls, creator, cr_workdir): |
| 50 | """ |
| 51 | Create loader-specific (syslinux) config |
| 52 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | splash = os.path.join(cr_workdir, "ISO/boot/splash.jpg") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | if os.path.exists(splash): |
| 55 | splashline = "menu background splash.jpg" |
| 56 | else: |
| 57 | splashline = "" |
| 58 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 59 | bootloader = creator.ks.bootloader |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
| 61 | syslinux_conf = "" |
| 62 | syslinux_conf += "PROMPT 0\n" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 63 | syslinux_conf += "TIMEOUT %s \n" % (bootloader.timeout or 10) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | syslinux_conf += "\n" |
| 65 | syslinux_conf += "ALLOWOPTIONS 1\n" |
| 66 | syslinux_conf += "SERIAL 0 115200\n" |
| 67 | syslinux_conf += "\n" |
| 68 | if splashline: |
| 69 | syslinux_conf += "%s\n" % splashline |
| 70 | syslinux_conf += "DEFAULT boot\n" |
| 71 | syslinux_conf += "LABEL boot\n" |
| 72 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 73 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") |
| 74 | if not kernel: |
| 75 | kernel = "bzImage" |
| 76 | syslinux_conf += "KERNEL /" + kernel + "\n" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 77 | syslinux_conf += "APPEND initrd=/initrd LABEL=boot %s\n" \ |
| 78 | % bootloader.append |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 80 | logger.debug("Writing syslinux config %s/ISO/isolinux/isolinux.cfg", |
| 81 | cr_workdir) |
| 82 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | with open("%s/ISO/isolinux/isolinux.cfg" % cr_workdir, "w") as cfg: |
| 84 | cfg.write(syslinux_conf) |
| 85 | |
| 86 | @classmethod |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 87 | def do_configure_grubefi(cls, part, creator, target_dir): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | """ |
| 89 | Create loader-specific (grub-efi) config |
| 90 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 91 | configfile = creator.ks.bootloader.configfile |
| 92 | if configfile: |
| 93 | grubefi_conf = get_custom_config(configfile) |
| 94 | if grubefi_conf: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 95 | logger.debug("Using custom configuration file %s for grub.cfg", |
| 96 | configfile) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 97 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 98 | raise WicError("configfile is specified " |
| 99 | "but failed to get it from %s", configfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 101 | splash = os.path.join(target_dir, "splash.jpg") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 102 | if os.path.exists(splash): |
| 103 | splashline = "menu background splash.jpg" |
| 104 | else: |
| 105 | splashline = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 107 | bootloader = creator.ks.bootloader |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 109 | grubefi_conf = "" |
| 110 | grubefi_conf += "serial --unit=0 --speed=115200 --word=8 " |
| 111 | grubefi_conf += "--parity=no --stop=1\n" |
| 112 | grubefi_conf += "default=boot\n" |
| 113 | grubefi_conf += "timeout=%s\n" % (bootloader.timeout or 10) |
| 114 | grubefi_conf += "\n" |
| 115 | grubefi_conf += "search --set=root --label %s " % part.label |
| 116 | grubefi_conf += "\n" |
| 117 | grubefi_conf += "menuentry 'boot'{\n" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 118 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 119 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") |
| 120 | if not kernel: |
| 121 | kernel = "bzImage" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 123 | grubefi_conf += "linux /%s rootwait %s\n" \ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 124 | % (kernel, bootloader.append) |
| 125 | grubefi_conf += "initrd /initrd \n" |
| 126 | grubefi_conf += "}\n" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 127 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 128 | if splashline: |
| 129 | grubefi_conf += "%s\n" % splashline |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 131 | cfg_path = os.path.join(target_dir, "grub.cfg") |
| 132 | logger.debug("Writing grubefi config %s", cfg_path) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 133 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 134 | with open(cfg_path, "w") as cfg: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 135 | cfg.write(grubefi_conf) |
| 136 | |
| 137 | @staticmethod |
| 138 | def _build_initramfs_path(rootfs_dir, cr_workdir): |
| 139 | """ |
| 140 | Create path for initramfs image |
| 141 | """ |
| 142 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 143 | initrd = get_bitbake_var("INITRD_LIVE") or get_bitbake_var("INITRD") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 144 | if not initrd: |
| 145 | initrd_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") |
| 146 | if not initrd_dir: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 147 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | |
| 149 | image_name = get_bitbake_var("IMAGE_BASENAME") |
| 150 | if not image_name: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 151 | raise WicError("Couldn't find IMAGE_BASENAME, exiting.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | |
| 153 | image_type = get_bitbake_var("INITRAMFS_FSTYPES") |
| 154 | if not image_type: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 155 | raise WicError("Couldn't find INITRAMFS_FSTYPES, exiting.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 157 | machine = os.path.basename(initrd_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 159 | pattern = '%s/%s*%s.%s' % (initrd_dir, image_name, machine, image_type) |
| 160 | files = glob.glob(pattern) |
| 161 | if files: |
| 162 | initrd = files[0] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 163 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 164 | if not initrd or not os.path.exists(initrd): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 165 | # Create initrd from rootfs directory |
| 166 | initrd = "%s/initrd.cpio.gz" % cr_workdir |
| 167 | initrd_dir = "%s/INITRD" % cr_workdir |
| 168 | shutil.copytree("%s" % rootfs_dir, \ |
| 169 | "%s" % initrd_dir, symlinks=True) |
| 170 | |
| 171 | if os.path.isfile("%s/init" % rootfs_dir): |
| 172 | shutil.copy2("%s/init" % rootfs_dir, "%s/init" % initrd_dir) |
| 173 | elif os.path.lexists("%s/init" % rootfs_dir): |
| 174 | os.symlink(os.readlink("%s/init" % rootfs_dir), \ |
| 175 | "%s/init" % initrd_dir) |
| 176 | elif os.path.isfile("%s/sbin/init" % rootfs_dir): |
| 177 | shutil.copy2("%s/sbin/init" % rootfs_dir, \ |
| 178 | "%s" % initrd_dir) |
| 179 | elif os.path.lexists("%s/sbin/init" % rootfs_dir): |
| 180 | os.symlink(os.readlink("%s/sbin/init" % rootfs_dir), \ |
| 181 | "%s/init" % initrd_dir) |
| 182 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 183 | raise WicError("Couldn't find or build initrd, exiting.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 184 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 185 | exec_cmd("cd %s && find . | cpio -o -H newc -R root:root >%s/initrd.cpio " \ |
| 186 | % (initrd_dir, cr_workdir), as_shell=True) |
| 187 | exec_cmd("gzip -f -9 %s/initrd.cpio" % cr_workdir, as_shell=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 188 | shutil.rmtree(initrd_dir) |
| 189 | |
| 190 | return initrd |
| 191 | |
| 192 | @classmethod |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | def do_configure_partition(cls, part, source_params, creator, cr_workdir, |
| 194 | oe_builddir, bootimg_dir, kernel_dir, |
| 195 | native_sysroot): |
| 196 | """ |
| 197 | Called before do_prepare_partition(), creates loader-specific config |
| 198 | """ |
| 199 | isodir = "%s/ISO/" % cr_workdir |
| 200 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 201 | if os.path.exists(isodir): |
| 202 | shutil.rmtree(isodir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 203 | |
| 204 | install_cmd = "install -d %s " % isodir |
| 205 | exec_cmd(install_cmd) |
| 206 | |
| 207 | # Overwrite the name of the created image |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 208 | logger.debug(source_params) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 209 | if 'image_name' in source_params and \ |
| 210 | source_params['image_name'].strip(): |
| 211 | creator.name = source_params['image_name'].strip() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 212 | logger.debug("The name of the image is: %s", creator.name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 213 | |
| 214 | @classmethod |
| 215 | def do_prepare_partition(cls, part, source_params, creator, cr_workdir, |
| 216 | oe_builddir, bootimg_dir, kernel_dir, |
| 217 | rootfs_dir, native_sysroot): |
| 218 | """ |
| 219 | Called to do the actual content population for a partition i.e. it |
| 220 | 'prepares' the partition to be incorporated into the image. |
| 221 | In this case, prepare content for a bootable ISO image. |
| 222 | """ |
| 223 | |
| 224 | isodir = "%s/ISO" % cr_workdir |
| 225 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 226 | if part.rootfs_dir is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | if not 'ROOTFS_DIR' in rootfs_dir: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 228 | raise WicError("Couldn't find --rootfs-dir, exiting.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 229 | rootfs_dir = rootfs_dir['ROOTFS_DIR'] |
| 230 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 231 | if part.rootfs_dir in rootfs_dir: |
| 232 | rootfs_dir = rootfs_dir[part.rootfs_dir] |
| 233 | elif part.rootfs_dir: |
| 234 | rootfs_dir = part.rootfs_dir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 235 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 236 | raise WicError("Couldn't find --rootfs-dir=%s connection " |
| 237 | "or it is not a valid path, exiting." % |
| 238 | part.rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 239 | |
| 240 | if not os.path.isdir(rootfs_dir): |
| 241 | rootfs_dir = get_bitbake_var("IMAGE_ROOTFS") |
| 242 | if not os.path.isdir(rootfs_dir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 243 | raise WicError("Couldn't find IMAGE_ROOTFS, exiting.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 244 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 245 | part.rootfs_dir = rootfs_dir |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 246 | deploy_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 247 | img_iso_dir = get_bitbake_var("ISODIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 248 | |
| 249 | # Remove the temporary file created by part.prepare_rootfs() |
| 250 | if os.path.isfile(part.source_file): |
| 251 | os.remove(part.source_file) |
| 252 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 253 | # Support using a different initrd other than default |
| 254 | if source_params.get('initrd'): |
| 255 | initrd = source_params['initrd'] |
| 256 | if not deploy_dir: |
| 257 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") |
| 258 | cp_cmd = "cp %s/%s %s" % (deploy_dir, initrd, cr_workdir) |
| 259 | exec_cmd(cp_cmd) |
| 260 | else: |
| 261 | # Prepare initial ramdisk |
| 262 | initrd = "%s/initrd" % deploy_dir |
| 263 | if not os.path.isfile(initrd): |
| 264 | initrd = "%s/initrd" % img_iso_dir |
| 265 | if not os.path.isfile(initrd): |
| 266 | initrd = cls._build_initramfs_path(rootfs_dir, cr_workdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 267 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 268 | install_cmd = "install -m 0644 %s %s/initrd" % (initrd, isodir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 269 | exec_cmd(install_cmd) |
| 270 | |
| 271 | # Remove the temporary file created by _build_initramfs_path function |
| 272 | if os.path.isfile("%s/initrd.cpio.gz" % cr_workdir): |
| 273 | os.remove("%s/initrd.cpio.gz" % cr_workdir) |
| 274 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 275 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") |
| 276 | if not kernel: |
| 277 | kernel = "bzImage" |
| 278 | |
| 279 | install_cmd = "install -m 0644 %s/%s %s/%s" % \ |
| 280 | (kernel_dir, kernel, isodir, kernel) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 281 | exec_cmd(install_cmd) |
| 282 | |
| 283 | #Create bootloader for efi boot |
| 284 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 285 | target_dir = "%s/EFI/BOOT" % isodir |
| 286 | if os.path.exists(target_dir): |
| 287 | shutil.rmtree(target_dir) |
| 288 | |
| 289 | os.makedirs(target_dir) |
| 290 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 291 | if source_params['loader'] == 'grub-efi': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 292 | # Builds bootx64.efi/bootia32.efi if ISODIR didn't exist or |
| 293 | # didn't contains it |
| 294 | target_arch = get_bitbake_var("TARGET_SYS") |
| 295 | if not target_arch: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 296 | raise WicError("Coludn't find target architecture") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 297 | |
| 298 | if re.match("x86_64", target_arch): |
Brad Bishop | c4ea075 | 2018-11-15 14:30:15 -0800 | [diff] [blame] | 299 | grub_src_image = "grub-efi-bootx64.efi" |
| 300 | grub_dest_image = "bootx64.efi" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 301 | elif re.match('i.86', target_arch): |
Brad Bishop | c4ea075 | 2018-11-15 14:30:15 -0800 | [diff] [blame] | 302 | grub_src_image = "grub-efi-bootia32.efi" |
| 303 | grub_dest_image = "bootia32.efi" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 304 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 305 | raise WicError("grub-efi is incompatible with target %s" % |
| 306 | target_arch) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 307 | |
Brad Bishop | c4ea075 | 2018-11-15 14:30:15 -0800 | [diff] [blame] | 308 | grub_target = os.path.join(target_dir, grub_dest_image) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 309 | if not os.path.isfile(grub_target): |
Brad Bishop | c4ea075 | 2018-11-15 14:30:15 -0800 | [diff] [blame] | 310 | grub_src = os.path.join(deploy_dir, grub_src_image) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 311 | if not os.path.exists(grub_src): |
| 312 | raise WicError("Grub loader %s is not found in %s. " |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 313 | "Please build grub-efi first" % (grub_src_image, deploy_dir)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 314 | shutil.copy(grub_src, grub_target) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 315 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 316 | if not os.path.isfile(os.path.join(target_dir, "boot.cfg")): |
| 317 | cls.do_configure_grubefi(part, creator, target_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 318 | |
| 319 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 320 | raise WicError("unrecognized bootimg-efi loader: %s" % |
| 321 | source_params['loader']) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 322 | except KeyError: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 323 | raise WicError("bootimg-efi requires a loader, none specified") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 324 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 325 | # Create efi.img that contains bootloader files for EFI booting |
| 326 | # if ISODIR didn't exist or didn't contains it |
| 327 | if os.path.isfile("%s/efi.img" % img_iso_dir): |
| 328 | install_cmd = "install -m 0644 %s/efi.img %s/efi.img" % \ |
| 329 | (img_iso_dir, isodir) |
| 330 | exec_cmd(install_cmd) |
| 331 | else: |
| 332 | du_cmd = "du -bks %s/EFI" % isodir |
| 333 | out = exec_cmd(du_cmd) |
| 334 | blocks = int(out.split()[0]) |
| 335 | # Add some extra space for file system overhead |
| 336 | blocks += 100 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 337 | logger.debug("Added 100 extra blocks to %s to get to %d " |
| 338 | "total blocks", part.mountpoint, blocks) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 339 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 340 | # dosfs image for EFI boot |
| 341 | bootimg = "%s/efi.img" % isodir |
| 342 | |
| 343 | dosfs_cmd = 'mkfs.vfat -n "EFIimg" -S 512 -C %s %d' \ |
| 344 | % (bootimg, blocks) |
| 345 | exec_native_cmd(dosfs_cmd, native_sysroot) |
| 346 | |
| 347 | mmd_cmd = "mmd -i %s ::/EFI" % bootimg |
| 348 | exec_native_cmd(mmd_cmd, native_sysroot) |
| 349 | |
| 350 | mcopy_cmd = "mcopy -i %s -s %s/EFI/* ::/EFI/" \ |
| 351 | % (bootimg, isodir) |
| 352 | exec_native_cmd(mcopy_cmd, native_sysroot) |
| 353 | |
| 354 | chmod_cmd = "chmod 644 %s" % bootimg |
| 355 | exec_cmd(chmod_cmd) |
| 356 | |
| 357 | # Prepare files for legacy boot |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 358 | syslinux_dir = get_bitbake_var("STAGING_DATADIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 359 | if not syslinux_dir: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 360 | raise WicError("Couldn't find STAGING_DATADIR, exiting.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 361 | |
| 362 | if os.path.exists("%s/isolinux" % isodir): |
| 363 | shutil.rmtree("%s/isolinux" % isodir) |
| 364 | |
| 365 | install_cmd = "install -d %s/isolinux" % isodir |
| 366 | exec_cmd(install_cmd) |
| 367 | |
| 368 | cls.do_configure_syslinux(creator, cr_workdir) |
| 369 | |
| 370 | install_cmd = "install -m 444 %s/syslinux/ldlinux.sys " % syslinux_dir |
| 371 | install_cmd += "%s/isolinux/ldlinux.sys" % isodir |
| 372 | exec_cmd(install_cmd) |
| 373 | |
| 374 | install_cmd = "install -m 444 %s/syslinux/isohdpfx.bin " % syslinux_dir |
| 375 | install_cmd += "%s/isolinux/isohdpfx.bin" % isodir |
| 376 | exec_cmd(install_cmd) |
| 377 | |
| 378 | install_cmd = "install -m 644 %s/syslinux/isolinux.bin " % syslinux_dir |
| 379 | install_cmd += "%s/isolinux/isolinux.bin" % isodir |
| 380 | exec_cmd(install_cmd) |
| 381 | |
| 382 | install_cmd = "install -m 644 %s/syslinux/ldlinux.c32 " % syslinux_dir |
| 383 | install_cmd += "%s/isolinux/ldlinux.c32" % isodir |
| 384 | exec_cmd(install_cmd) |
| 385 | |
| 386 | #create ISO image |
| 387 | iso_img = "%s/tempiso_img.iso" % cr_workdir |
| 388 | iso_bootimg = "isolinux/isolinux.bin" |
| 389 | iso_bootcat = "isolinux/boot.cat" |
| 390 | efi_img = "efi.img" |
| 391 | |
| 392 | mkisofs_cmd = "mkisofs -V %s " % part.label |
| 393 | mkisofs_cmd += "-o %s -U " % iso_img |
| 394 | mkisofs_cmd += "-J -joliet-long -r -iso-level 2 -b %s " % iso_bootimg |
| 395 | mkisofs_cmd += "-c %s -no-emul-boot -boot-load-size 4 " % iso_bootcat |
| 396 | mkisofs_cmd += "-boot-info-table -eltorito-alt-boot " |
| 397 | mkisofs_cmd += "-eltorito-platform 0xEF -eltorito-boot %s " % efi_img |
| 398 | mkisofs_cmd += "-no-emul-boot %s " % isodir |
| 399 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 400 | logger.debug("running command: %s", mkisofs_cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 401 | exec_native_cmd(mkisofs_cmd, native_sysroot) |
| 402 | |
| 403 | shutil.rmtree(isodir) |
| 404 | |
| 405 | du_cmd = "du -Lbks %s" % iso_img |
| 406 | out = exec_cmd(du_cmd) |
| 407 | isoimg_size = int(out.split()[0]) |
| 408 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 409 | part.size = isoimg_size |
| 410 | part.source_file = iso_img |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 411 | |
| 412 | @classmethod |
| 413 | def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, |
| 414 | bootimg_dir, kernel_dir, native_sysroot): |
| 415 | """ |
| 416 | Called after all partitions have been prepared and assembled into a |
| 417 | disk image. In this case, we insert/modify the MBR using isohybrid |
| 418 | utility for booting via BIOS from disk storage devices. |
| 419 | """ |
| 420 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 421 | iso_img = "%s.p1" % disk.path |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 422 | full_path = creator._full_path(workdir, disk_name, "direct") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 423 | full_path_iso = creator._full_path(workdir, disk_name, "iso") |
| 424 | |
| 425 | isohybrid_cmd = "isohybrid -u %s" % iso_img |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 426 | logger.debug("running command: %s", isohybrid_cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 427 | exec_native_cmd(isohybrid_cmd, native_sysroot) |
| 428 | |
| 429 | # Replace the image created by direct plugin with the one created by |
| 430 | # mkisofs command. This is necessary because the iso image created by |
| 431 | # mkisofs has a very specific MBR is system area of the ISO image, and |
| 432 | # direct plugin adds and configures an another MBR. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 433 | logger.debug("Replaceing the image created by direct plugin\n") |
| 434 | os.remove(disk.path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 435 | shutil.copy2(iso_img, full_path_iso) |
| 436 | shutil.copy2(full_path_iso, full_path) |