diff options
Diffstat (limited to 'scripts/lib/wic/plugins')
| -rw-r--r-- | scripts/lib/wic/plugins/imager/direct.py | 704 | ||||
| -rw-r--r-- | scripts/lib/wic/plugins/source/bootimg-biosplusefi.py | 213 | ||||
| -rw-r--r-- | scripts/lib/wic/plugins/source/bootimg-efi.py | 435 | ||||
| -rw-r--r-- | scripts/lib/wic/plugins/source/bootimg-partition.py | 162 | ||||
| -rw-r--r-- | scripts/lib/wic/plugins/source/bootimg-pcbios.py | 209 | ||||
| -rw-r--r-- | scripts/lib/wic/plugins/source/empty.py | 89 | ||||
| -rw-r--r-- | scripts/lib/wic/plugins/source/isoimage-isohybrid.py | 463 | ||||
| -rw-r--r-- | scripts/lib/wic/plugins/source/rawcopy.py | 115 | ||||
| -rw-r--r-- | scripts/lib/wic/plugins/source/rootfs.py | 236 |
9 files changed, 0 insertions, 2626 deletions
diff --git a/scripts/lib/wic/plugins/imager/direct.py b/scripts/lib/wic/plugins/imager/direct.py deleted file mode 100644 index 2124ceac7f..0000000000 --- a/scripts/lib/wic/plugins/imager/direct.py +++ /dev/null | |||
| @@ -1,704 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright (c) 2013, Intel Corporation. | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | # DESCRIPTION | ||
| 7 | # This implements the 'direct' imager plugin class for 'wic' | ||
| 8 | # | ||
| 9 | # AUTHORS | ||
| 10 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> | ||
| 11 | # | ||
| 12 | |||
| 13 | import logging | ||
| 14 | import os | ||
| 15 | import random | ||
| 16 | import shutil | ||
| 17 | import tempfile | ||
| 18 | import uuid | ||
| 19 | |||
| 20 | from time import strftime | ||
| 21 | |||
| 22 | from oe.path import copyhardlinktree | ||
| 23 | |||
| 24 | from wic import WicError | ||
| 25 | from wic.filemap import sparse_copy | ||
| 26 | from wic.ksparser import KickStart, KickStartError | ||
| 27 | from wic.pluginbase import PluginMgr, ImagerPlugin | ||
| 28 | from wic.misc import get_bitbake_var, exec_cmd, exec_native_cmd | ||
| 29 | |||
| 30 | logger = logging.getLogger('wic') | ||
| 31 | |||
| 32 | class DirectPlugin(ImagerPlugin): | ||
| 33 | """ | ||
| 34 | Install a system into a file containing a partitioned disk image. | ||
| 35 | |||
| 36 | An image file is formatted with a partition table, each partition | ||
| 37 | created from a rootfs or other OpenEmbedded build artifact and dd'ed | ||
| 38 | into the virtual disk. The disk image can subsequently be dd'ed onto | ||
| 39 | media and used on actual hardware. | ||
| 40 | """ | ||
| 41 | name = 'direct' | ||
| 42 | |||
| 43 | def __init__(self, wks_file, rootfs_dir, bootimg_dir, kernel_dir, | ||
| 44 | native_sysroot, oe_builddir, options): | ||
| 45 | try: | ||
| 46 | self.ks = KickStart(wks_file) | ||
| 47 | except KickStartError as err: | ||
| 48 | raise WicError(str(err)) | ||
| 49 | |||
| 50 | # parse possible 'rootfs=name' items | ||
| 51 | self.rootfs_dir = dict(rdir.split('=') for rdir in rootfs_dir.split(' ')) | ||
| 52 | self.bootimg_dir = bootimg_dir | ||
| 53 | self.kernel_dir = kernel_dir | ||
| 54 | self.native_sysroot = native_sysroot | ||
| 55 | self.oe_builddir = oe_builddir | ||
| 56 | |||
| 57 | self.debug = options.debug | ||
| 58 | self.outdir = options.outdir | ||
| 59 | self.compressor = options.compressor | ||
| 60 | self.bmap = options.bmap | ||
| 61 | self.no_fstab_update = options.no_fstab_update | ||
| 62 | self.updated_fstab_path = None | ||
| 63 | |||
| 64 | self.name = "%s-%s" % (os.path.splitext(os.path.basename(wks_file))[0], | ||
| 65 | strftime("%Y%m%d%H%M")) | ||
| 66 | self.workdir = self.setup_workdir(options.workdir) | ||
| 67 | self._image = None | ||
| 68 | self.ptable_format = self.ks.bootloader.ptable | ||
| 69 | self.parts = self.ks.partitions | ||
| 70 | |||
| 71 | # as a convenience, set source to the boot partition source | ||
| 72 | # instead of forcing it to be set via bootloader --source | ||
| 73 | for part in self.parts: | ||
| 74 | if not self.ks.bootloader.source and part.mountpoint == "/boot": | ||
| 75 | self.ks.bootloader.source = part.source | ||
| 76 | break | ||
| 77 | |||
| 78 | image_path = self._full_path(self.workdir, self.parts[0].disk, "direct") | ||
| 79 | self._image = PartitionedImage(image_path, self.ptable_format, | ||
| 80 | self.parts, self.native_sysroot, | ||
| 81 | options.extra_space) | ||
| 82 | |||
| 83 | def setup_workdir(self, workdir): | ||
| 84 | if workdir: | ||
| 85 | if os.path.exists(workdir): | ||
| 86 | raise WicError("Internal workdir '%s' specified in wic arguments already exists!" % (workdir)) | ||
| 87 | |||
| 88 | os.makedirs(workdir) | ||
| 89 | return workdir | ||
| 90 | else: | ||
| 91 | return tempfile.mkdtemp(dir=self.outdir, prefix='tmp.wic.') | ||
| 92 | |||
| 93 | def do_create(self): | ||
| 94 | """ | ||
| 95 | Plugin entry point. | ||
| 96 | """ | ||
| 97 | try: | ||
| 98 | self.create() | ||
| 99 | self.assemble() | ||
| 100 | self.finalize() | ||
| 101 | self.print_info() | ||
| 102 | finally: | ||
| 103 | self.cleanup() | ||
| 104 | |||
| 105 | def update_fstab(self, image_rootfs): | ||
| 106 | """Assume partition order same as in wks""" | ||
| 107 | if not image_rootfs: | ||
| 108 | return | ||
| 109 | |||
| 110 | fstab_path = image_rootfs + "/etc/fstab" | ||
| 111 | if not os.path.isfile(fstab_path): | ||
| 112 | return | ||
| 113 | |||
| 114 | with open(fstab_path) as fstab: | ||
| 115 | fstab_lines = fstab.readlines() | ||
| 116 | |||
| 117 | updated = False | ||
| 118 | for part in self.parts: | ||
| 119 | if not part.realnum or not part.mountpoint \ | ||
| 120 | or part.mountpoint == "/" or not (part.mountpoint.startswith('/') or part.mountpoint == "swap"): | ||
| 121 | continue | ||
| 122 | |||
| 123 | if part.use_uuid: | ||
| 124 | if part.fsuuid: | ||
| 125 | # FAT UUID is different from others | ||
| 126 | if len(part.fsuuid) == 10: | ||
| 127 | device_name = "UUID=%s-%s" % \ | ||
| 128 | (part.fsuuid[2:6], part.fsuuid[6:]) | ||
| 129 | else: | ||
| 130 | device_name = "UUID=%s" % part.fsuuid | ||
| 131 | else: | ||
| 132 | device_name = "PARTUUID=%s" % part.uuid | ||
| 133 | elif part.use_label: | ||
| 134 | device_name = "LABEL=%s" % part.label | ||
| 135 | else: | ||
| 136 | # mmc device partitions are named mmcblk0p1, mmcblk0p2.. | ||
| 137 | prefix = 'p' if part.disk.startswith('mmcblk') else '' | ||
| 138 | device_name = "/dev/%s%s%d" % (part.disk, prefix, part.realnum) | ||
| 139 | |||
| 140 | opts = part.fsopts if part.fsopts else "defaults" | ||
| 141 | passno = part.fspassno if part.fspassno else "0" | ||
| 142 | line = "\t".join([device_name, part.mountpoint, part.fstype, | ||
| 143 | opts, "0", passno]) + "\n" | ||
| 144 | |||
| 145 | fstab_lines.append(line) | ||
| 146 | updated = True | ||
| 147 | |||
| 148 | if updated: | ||
| 149 | self.updated_fstab_path = os.path.join(self.workdir, "fstab") | ||
| 150 | with open(self.updated_fstab_path, "w") as f: | ||
| 151 | f.writelines(fstab_lines) | ||
| 152 | if os.getenv('SOURCE_DATE_EPOCH'): | ||
| 153 | fstab_time = int(os.getenv('SOURCE_DATE_EPOCH')) | ||
| 154 | os.utime(self.updated_fstab_path, (fstab_time, fstab_time)) | ||
| 155 | |||
| 156 | def _full_path(self, path, name, extention): | ||
| 157 | """ Construct full file path to a file we generate. """ | ||
| 158 | return os.path.join(path, "%s-%s.%s" % (self.name, name, extention)) | ||
| 159 | |||
| 160 | # | ||
| 161 | # Actual implemention | ||
| 162 | # | ||
| 163 | def create(self): | ||
| 164 | """ | ||
| 165 | For 'wic', we already have our build artifacts - we just create | ||
| 166 | filesystems from the artifacts directly and combine them into | ||
| 167 | a partitioned image. | ||
| 168 | """ | ||
| 169 | if not self.no_fstab_update: | ||
| 170 | self.update_fstab(self.rootfs_dir.get("ROOTFS_DIR")) | ||
| 171 | |||
| 172 | for part in self.parts: | ||
| 173 | # get rootfs size from bitbake variable if it's not set in .ks file | ||
| 174 | if not part.size: | ||
| 175 | # and if rootfs name is specified for the partition | ||
| 176 | image_name = self.rootfs_dir.get(part.rootfs_dir) | ||
| 177 | if image_name and os.path.sep not in image_name: | ||
| 178 | # Bitbake variable ROOTFS_SIZE is calculated in | ||
| 179 | # Image._get_rootfs_size method from meta/lib/oe/image.py | ||
| 180 | # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT, | ||
| 181 | # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE | ||
| 182 | rsize_bb = get_bitbake_var('ROOTFS_SIZE', image_name) | ||
| 183 | if rsize_bb: | ||
| 184 | part.size = int(round(float(rsize_bb))) | ||
| 185 | |||
| 186 | self._image.prepare(self) | ||
| 187 | self._image.layout_partitions() | ||
| 188 | self._image.create() | ||
| 189 | |||
| 190 | def assemble(self): | ||
| 191 | """ | ||
| 192 | Assemble partitions into disk image | ||
| 193 | """ | ||
| 194 | self._image.assemble() | ||
| 195 | |||
| 196 | def finalize(self): | ||
| 197 | """ | ||
| 198 | Finalize the disk image. | ||
| 199 | |||
| 200 | For example, prepare the image to be bootable by e.g. | ||
| 201 | creating and installing a bootloader configuration. | ||
| 202 | """ | ||
| 203 | source_plugin = self.ks.bootloader.source | ||
| 204 | disk_name = self.parts[0].disk | ||
| 205 | if source_plugin: | ||
| 206 | plugin = PluginMgr.get_plugins('source')[source_plugin] | ||
| 207 | plugin.do_install_disk(self._image, disk_name, self, self.workdir, | ||
| 208 | self.oe_builddir, self.bootimg_dir, | ||
| 209 | self.kernel_dir, self.native_sysroot) | ||
| 210 | |||
| 211 | full_path = self._image.path | ||
| 212 | # Generate .bmap | ||
| 213 | if self.bmap: | ||
| 214 | logger.debug("Generating bmap file for %s", disk_name) | ||
| 215 | python = os.path.join(self.native_sysroot, 'usr/bin/python3-native/python3') | ||
| 216 | bmaptool = os.path.join(self.native_sysroot, 'usr/bin/bmaptool') | ||
| 217 | exec_native_cmd("%s %s create %s -o %s.bmap" % \ | ||
| 218 | (python, bmaptool, full_path, full_path), self.native_sysroot) | ||
| 219 | # Compress the image | ||
| 220 | if self.compressor: | ||
| 221 | logger.debug("Compressing disk %s with %s", disk_name, self.compressor) | ||
| 222 | exec_cmd("%s %s" % (self.compressor, full_path)) | ||
| 223 | |||
| 224 | def print_info(self): | ||
| 225 | """ | ||
| 226 | Print the image(s) and artifacts used, for the user. | ||
| 227 | """ | ||
| 228 | msg = "The new image(s) can be found here:\n" | ||
| 229 | |||
| 230 | extension = "direct" + {"gzip": ".gz", | ||
| 231 | "bzip2": ".bz2", | ||
| 232 | "xz": ".xz", | ||
| 233 | None: ""}.get(self.compressor) | ||
| 234 | full_path = self._full_path(self.outdir, self.parts[0].disk, extension) | ||
| 235 | msg += ' %s\n\n' % full_path | ||
| 236 | |||
| 237 | msg += 'The following build artifacts were used to create the image(s):\n' | ||
| 238 | for part in self.parts: | ||
| 239 | if part.rootfs_dir is None: | ||
| 240 | continue | ||
| 241 | if part.mountpoint == '/': | ||
| 242 | suffix = ':' | ||
| 243 | else: | ||
| 244 | suffix = '["%s"]:' % (part.mountpoint or part.label) | ||
| 245 | rootdir = part.rootfs_dir | ||
| 246 | msg += ' ROOTFS_DIR%s%s\n' % (suffix.ljust(20), rootdir) | ||
| 247 | |||
| 248 | msg += ' BOOTIMG_DIR: %s\n' % self.bootimg_dir | ||
| 249 | msg += ' KERNEL_DIR: %s\n' % self.kernel_dir | ||
| 250 | msg += ' NATIVE_SYSROOT: %s\n' % self.native_sysroot | ||
| 251 | |||
| 252 | logger.info(msg) | ||
| 253 | |||
| 254 | @property | ||
| 255 | def rootdev(self): | ||
| 256 | """ | ||
| 257 | Get root device name to use as a 'root' parameter | ||
| 258 | in kernel command line. | ||
| 259 | |||
| 260 | Assume partition order same as in wks | ||
| 261 | """ | ||
| 262 | for part in self.parts: | ||
| 263 | if part.mountpoint == "/": | ||
| 264 | if part.uuid: | ||
| 265 | return "PARTUUID=%s" % part.uuid | ||
| 266 | elif part.label and self.ptable_format != 'msdos': | ||
| 267 | return "PARTLABEL=%s" % part.label | ||
| 268 | else: | ||
| 269 | suffix = 'p' if part.disk.startswith('mmcblk') else '' | ||
| 270 | return "/dev/%s%s%-d" % (part.disk, suffix, part.realnum) | ||
| 271 | |||
| 272 | def cleanup(self): | ||
| 273 | if self._image: | ||
| 274 | self._image.cleanup() | ||
| 275 | |||
| 276 | # Move results to the output dir | ||
| 277 | if not os.path.exists(self.outdir): | ||
| 278 | os.makedirs(self.outdir) | ||
| 279 | |||
| 280 | for fname in os.listdir(self.workdir): | ||
| 281 | path = os.path.join(self.workdir, fname) | ||
| 282 | if os.path.isfile(path): | ||
| 283 | shutil.move(path, os.path.join(self.outdir, fname)) | ||
| 284 | |||
| 285 | # remove work directory when it is not in debugging mode | ||
| 286 | if not self.debug: | ||
| 287 | shutil.rmtree(self.workdir, ignore_errors=True) | ||
| 288 | |||
| 289 | # Overhead of the MBR partitioning scheme (just one sector) | ||
| 290 | MBR_OVERHEAD = 1 | ||
| 291 | |||
| 292 | # Overhead of the GPT partitioning scheme | ||
| 293 | GPT_OVERHEAD = 34 | ||
| 294 | |||
| 295 | # Size of a sector in bytes | ||
| 296 | SECTOR_SIZE = 512 | ||
| 297 | |||
| 298 | class PartitionedImage(): | ||
| 299 | """ | ||
| 300 | Partitioned image in a file. | ||
| 301 | """ | ||
| 302 | |||
| 303 | def __init__(self, path, ptable_format, partitions, native_sysroot=None, extra_space=0): | ||
| 304 | self.path = path # Path to the image file | ||
| 305 | self.numpart = 0 # Number of allocated partitions | ||
| 306 | self.realpart = 0 # Number of partitions in the partition table | ||
| 307 | self.primary_part_num = 0 # Number of primary partitions (msdos) | ||
| 308 | self.extendedpart = 0 # Create extended partition before this logical partition (msdos) | ||
| 309 | self.extended_size_sec = 0 # Size of exteded partition (msdos) | ||
| 310 | self.logical_part_cnt = 0 # Number of total logical paritions (msdos) | ||
| 311 | self.offset = 0 # Offset of next partition (in sectors) | ||
| 312 | self.min_size = 0 # Minimum required disk size to fit | ||
| 313 | # all partitions (in bytes) | ||
| 314 | self.ptable_format = ptable_format # Partition table format | ||
| 315 | # Disk system identifier | ||
| 316 | if os.getenv('SOURCE_DATE_EPOCH'): | ||
| 317 | self.identifier = random.Random(int(os.getenv('SOURCE_DATE_EPOCH'))).randint(1, 0xffffffff) | ||
| 318 | else: | ||
| 319 | self.identifier = random.SystemRandom().randint(1, 0xffffffff) | ||
| 320 | |||
| 321 | self.partitions = partitions | ||
| 322 | self.partimages = [] | ||
| 323 | # Size of a sector used in calculations | ||
| 324 | sector_size_str = get_bitbake_var('WIC_SECTOR_SIZE') | ||
| 325 | if sector_size_str is not None: | ||
| 326 | try: | ||
| 327 | self.sector_size = int(sector_size_str) | ||
| 328 | except ValueError: | ||
| 329 | self.sector_size = SECTOR_SIZE | ||
| 330 | else: | ||
| 331 | self.sector_size = SECTOR_SIZE | ||
| 332 | |||
| 333 | self.native_sysroot = native_sysroot | ||
| 334 | num_real_partitions = len([p for p in self.partitions if not p.no_table]) | ||
| 335 | self.extra_space = extra_space | ||
| 336 | |||
| 337 | # calculate the real partition number, accounting for partitions not | ||
| 338 | # in the partition table and logical partitions | ||
| 339 | realnum = 0 | ||
| 340 | for part in self.partitions: | ||
| 341 | if part.no_table: | ||
| 342 | part.realnum = 0 | ||
| 343 | else: | ||
| 344 | realnum += 1 | ||
| 345 | if self.ptable_format == 'msdos' and realnum > 3 and num_real_partitions > 4: | ||
| 346 | part.realnum = realnum + 1 | ||
| 347 | continue | ||
| 348 | part.realnum = realnum | ||
| 349 | |||
| 350 | # generate parition and filesystem UUIDs | ||
| 351 | for part in self.partitions: | ||
| 352 | if not part.uuid and part.use_uuid: | ||
| 353 | if self.ptable_format in ('gpt', 'gpt-hybrid'): | ||
| 354 | part.uuid = str(uuid.uuid4()) | ||
| 355 | else: # msdos partition table | ||
| 356 | part.uuid = '%08x-%02d' % (self.identifier, part.realnum) | ||
| 357 | if not part.fsuuid: | ||
| 358 | if part.fstype == 'vfat' or part.fstype == 'msdos': | ||
| 359 | part.fsuuid = '0x' + str(uuid.uuid4())[:8].upper() | ||
| 360 | else: | ||
| 361 | part.fsuuid = str(uuid.uuid4()) | ||
| 362 | else: | ||
| 363 | #make sure the fsuuid for vfat/msdos align with format 0xYYYYYYYY | ||
| 364 | if part.fstype == 'vfat' or part.fstype == 'msdos': | ||
| 365 | if part.fsuuid.upper().startswith("0X"): | ||
| 366 | part.fsuuid = '0x' + part.fsuuid.upper()[2:].rjust(8,"0") | ||
| 367 | else: | ||
| 368 | part.fsuuid = '0x' + part.fsuuid.upper().rjust(8,"0") | ||
| 369 | |||
| 370 | def prepare(self, imager): | ||
| 371 | """Prepare an image. Call prepare method of all image partitions.""" | ||
| 372 | for part in self.partitions: | ||
| 373 | # need to create the filesystems in order to get their | ||
| 374 | # sizes before we can add them and do the layout. | ||
| 375 | part.prepare(imager, imager.workdir, imager.oe_builddir, | ||
| 376 | imager.rootfs_dir, imager.bootimg_dir, | ||
| 377 | imager.kernel_dir, imager.native_sysroot, | ||
| 378 | imager.updated_fstab_path) | ||
| 379 | |||
| 380 | # Converting kB to sectors for parted | ||
| 381 | part.size_sec = part.disk_size * 1024 // self.sector_size | ||
| 382 | |||
| 383 | def layout_partitions(self): | ||
| 384 | """ Layout the partitions, meaning calculate the position of every | ||
| 385 | partition on the disk. The 'ptable_format' parameter defines the | ||
| 386 | partition table format and may be "msdos". """ | ||
| 387 | |||
| 388 | logger.debug("Assigning %s partitions to disks", self.ptable_format) | ||
| 389 | |||
| 390 | # The number of primary and logical partitions. Extended partition and | ||
| 391 | # partitions not listed in the table are not included. | ||
| 392 | num_real_partitions = len([p for p in self.partitions if not p.no_table]) | ||
| 393 | |||
| 394 | # Go through partitions in the order they are added in .ks file | ||
| 395 | for num in range(len(self.partitions)): | ||
| 396 | part = self.partitions[num] | ||
| 397 | |||
| 398 | if self.ptable_format == 'msdos' and part.part_name: | ||
| 399 | raise WicError("setting custom partition name is not " \ | ||
| 400 | "implemented for msdos partitions") | ||
| 401 | |||
| 402 | if self.ptable_format == 'msdos' and part.part_type: | ||
| 403 | # The --part-type can also be implemented for MBR partitions, | ||
| 404 | # in which case it would map to the 1-byte "partition type" | ||
| 405 | # filed at offset 3 of the partition entry. | ||
| 406 | raise WicError("setting custom partition type is not " \ | ||
| 407 | "implemented for msdos partitions") | ||
| 408 | |||
| 409 | if part.mbr and self.ptable_format != 'gpt-hybrid': | ||
| 410 | raise WicError("Partition may only be included in MBR with " \ | ||
| 411 | "a gpt-hybrid partition table") | ||
| 412 | |||
| 413 | # Get the disk where the partition is located | ||
| 414 | self.numpart += 1 | ||
| 415 | if not part.no_table: | ||
| 416 | self.realpart += 1 | ||
| 417 | |||
| 418 | if self.numpart == 1: | ||
| 419 | if self.ptable_format == "msdos": | ||
| 420 | overhead = MBR_OVERHEAD | ||
| 421 | elif self.ptable_format in ("gpt", "gpt-hybrid"): | ||
| 422 | overhead = GPT_OVERHEAD | ||
| 423 | |||
| 424 | # Skip one sector required for the partitioning scheme overhead | ||
| 425 | self.offset += overhead | ||
| 426 | |||
| 427 | if self.ptable_format == "msdos": | ||
| 428 | if self.primary_part_num > 3 or \ | ||
| 429 | (self.extendedpart == 0 and self.primary_part_num >= 3 and num_real_partitions > 4): | ||
| 430 | part.type = 'logical' | ||
| 431 | # Reserve a sector for EBR for every logical partition | ||
| 432 | # before alignment is performed. | ||
| 433 | if part.type == 'logical': | ||
| 434 | self.offset += 2 | ||
| 435 | |||
| 436 | align_sectors = 0 | ||
| 437 | if part.align: | ||
| 438 | # If not first partition and we do have alignment set we need | ||
| 439 | # to align the partition. | ||
| 440 | # FIXME: This leaves a empty spaces to the disk. To fill the | ||
| 441 | # gaps we could enlargea the previous partition? | ||
| 442 | |||
| 443 | # Calc how much the alignment is off. | ||
| 444 | align_sectors = self.offset % (part.align * 1024 // self.sector_size) | ||
| 445 | |||
| 446 | if align_sectors: | ||
| 447 | # If partition is not aligned as required, we need | ||
| 448 | # to move forward to the next alignment point | ||
| 449 | align_sectors = (part.align * 1024 // self.sector_size) - align_sectors | ||
| 450 | |||
| 451 | logger.debug("Realignment for %s%s with %s sectors, original" | ||
| 452 | " offset %s, target alignment is %sK.", | ||
| 453 | part.disk, self.numpart, align_sectors, | ||
| 454 | self.offset, part.align) | ||
| 455 | |||
| 456 | # increase the offset so we actually start the partition on right alignment | ||
| 457 | self.offset += align_sectors | ||
| 458 | |||
| 459 | if part.offset is not None: | ||
| 460 | offset = part.offset // self.sector_size | ||
| 461 | |||
| 462 | if offset * self.sector_size != part.offset: | ||
| 463 | raise WicError("Could not place %s%s at offset %d with sector size %d" % (part.disk, self.numpart, part.offset, self.sector_size)) | ||
| 464 | |||
| 465 | delta = offset - self.offset | ||
| 466 | if delta < 0: | ||
| 467 | raise WicError("Could not place %s%s at offset %d: next free sector is %d (delta: %d)" % (part.disk, self.numpart, part.offset, self.offset, delta)) | ||
| 468 | |||
| 469 | logger.debug("Skipping %d sectors to place %s%s at offset %dK", | ||
| 470 | delta, part.disk, self.numpart, part.offset) | ||
| 471 | |||
| 472 | self.offset = offset | ||
| 473 | |||
| 474 | part.start = self.offset | ||
| 475 | self.offset += part.size_sec | ||
| 476 | |||
| 477 | if not part.no_table: | ||
| 478 | part.num = self.realpart | ||
| 479 | else: | ||
| 480 | part.num = 0 | ||
| 481 | |||
| 482 | if self.ptable_format == "msdos" and not part.no_table: | ||
| 483 | if part.type == 'logical': | ||
| 484 | self.logical_part_cnt += 1 | ||
| 485 | part.num = self.logical_part_cnt + 4 | ||
| 486 | if self.extendedpart == 0: | ||
| 487 | # Create extended partition as a primary partition | ||
| 488 | self.primary_part_num += 1 | ||
| 489 | self.extendedpart = part.num | ||
| 490 | else: | ||
| 491 | self.extended_size_sec += align_sectors | ||
| 492 | self.extended_size_sec += part.size_sec + 2 | ||
| 493 | else: | ||
| 494 | self.primary_part_num += 1 | ||
| 495 | part.num = self.primary_part_num | ||
| 496 | |||
| 497 | logger.debug("Assigned %s to %s%d, sectors range %d-%d size %d " | ||
| 498 | "sectors (%d bytes).", part.mountpoint, part.disk, | ||
| 499 | part.num, part.start, self.offset - 1, part.size_sec, | ||
| 500 | part.size_sec * self.sector_size) | ||
| 501 | |||
| 502 | # Once all the partitions have been layed out, we can calculate the | ||
| 503 | # minumim disk size | ||
| 504 | self.min_size = self.offset | ||
| 505 | if self.ptable_format in ("gpt", "gpt-hybrid"): | ||
| 506 | self.min_size += GPT_OVERHEAD | ||
| 507 | |||
| 508 | self.min_size *= self.sector_size | ||
| 509 | self.min_size += self.extra_space | ||
| 510 | |||
| 511 | def _create_partition(self, device, parttype, fstype, start, size): | ||
| 512 | """ Create a partition on an image described by the 'device' object. """ | ||
| 513 | |||
| 514 | # Start is included to the size so we need to substract one from the end. | ||
| 515 | end = start + size - 1 | ||
| 516 | logger.debug("Added '%s' partition, sectors %d-%d, size %d sectors", | ||
| 517 | parttype, start, end, size) | ||
| 518 | |||
| 519 | cmd = "export PARTED_SECTOR_SIZE=%d; parted -s %s unit s mkpart %s" % \ | ||
| 520 | (self.sector_size, device, parttype) | ||
| 521 | if fstype: | ||
| 522 | cmd += " %s" % fstype | ||
| 523 | cmd += " %d %d" % (start, end) | ||
| 524 | |||
| 525 | return exec_native_cmd(cmd, self.native_sysroot) | ||
| 526 | |||
| 527 | def _write_identifier(self, device, identifier): | ||
| 528 | logger.debug("Set disk identifier %x", identifier) | ||
| 529 | with open(device, 'r+b') as img: | ||
| 530 | img.seek(0x1B8) | ||
| 531 | img.write(identifier.to_bytes(4, 'little')) | ||
| 532 | |||
| 533 | def _make_disk(self, device, ptable_format, min_size): | ||
| 534 | logger.debug("Creating sparse file %s", device) | ||
| 535 | with open(device, 'w') as sparse: | ||
| 536 | os.ftruncate(sparse.fileno(), min_size) | ||
| 537 | |||
| 538 | logger.debug("Initializing partition table for %s", device) | ||
| 539 | exec_native_cmd("export PARTED_SECTOR_SIZE=%d; parted -s %s mklabel %s" % | ||
| 540 | (self.sector_size, device, ptable_format), self.native_sysroot) | ||
| 541 | |||
| 542 | def _write_disk_guid(self): | ||
| 543 | if self.ptable_format in ('gpt', 'gpt-hybrid'): | ||
| 544 | if os.getenv('SOURCE_DATE_EPOCH'): | ||
| 545 | self.disk_guid = uuid.UUID(int=int(os.getenv('SOURCE_DATE_EPOCH'))) | ||
| 546 | else: | ||
| 547 | self.disk_guid = uuid.uuid4() | ||
| 548 | |||
| 549 | logger.debug("Set disk guid %s", self.disk_guid) | ||
| 550 | sfdisk_cmd = "sfdisk --sector-size %s --disk-id %s %s" % \ | ||
| 551 | (self.sector_size, self.path, self.disk_guid) | ||
| 552 | exec_native_cmd(sfdisk_cmd, self.native_sysroot) | ||
| 553 | |||
| 554 | def create(self): | ||
| 555 | self._make_disk(self.path, | ||
| 556 | "gpt" if self.ptable_format == "gpt-hybrid" else self.ptable_format, | ||
| 557 | self.min_size) | ||
| 558 | |||
| 559 | self._write_identifier(self.path, self.identifier) | ||
| 560 | self._write_disk_guid() | ||
| 561 | |||
| 562 | if self.ptable_format == "gpt-hybrid": | ||
| 563 | mbr_path = self.path + ".mbr" | ||
| 564 | self._make_disk(mbr_path, "msdos", self.min_size) | ||
| 565 | self._write_identifier(mbr_path, self.identifier) | ||
| 566 | |||
| 567 | logger.debug("Creating partitions") | ||
| 568 | |||
| 569 | hybrid_mbr_part_num = 0 | ||
| 570 | |||
| 571 | for part in self.partitions: | ||
| 572 | if part.num == 0: | ||
| 573 | continue | ||
| 574 | |||
| 575 | if self.ptable_format == "msdos" and part.num == self.extendedpart: | ||
| 576 | # Create an extended partition (note: extended | ||
| 577 | # partition is described in MBR and contains all | ||
| 578 | # logical partitions). The logical partitions save a | ||
| 579 | # sector for an EBR just before the start of a | ||
| 580 | # partition. The extended partition must start one | ||
| 581 | # sector before the start of the first logical | ||
| 582 | # partition. This way the first EBR is inside of the | ||
| 583 | # extended partition. Since the extended partitions | ||
| 584 | # starts a sector before the first logical partition, | ||
| 585 | # add a sector at the back, so that there is enough | ||
| 586 | # room for all logical partitions. | ||
| 587 | self._create_partition(self.path, "extended", | ||
| 588 | None, part.start - 2, | ||
| 589 | self.extended_size_sec) | ||
| 590 | |||
| 591 | if part.fstype == "swap": | ||
| 592 | parted_fs_type = "linux-swap" | ||
| 593 | elif part.fstype == "vfat": | ||
| 594 | parted_fs_type = "fat32" | ||
| 595 | elif part.fstype == "msdos": | ||
| 596 | parted_fs_type = "fat16" | ||
| 597 | if not part.system_id: | ||
| 598 | part.system_id = '0x6' # FAT16 | ||
| 599 | else: | ||
| 600 | # Type for ext2/ext3/ext4/btrfs | ||
| 601 | parted_fs_type = "ext2" | ||
| 602 | |||
| 603 | # Boot ROM of OMAP boards require vfat boot partition to have an | ||
| 604 | # even number of sectors. | ||
| 605 | if part.mountpoint == "/boot" and part.fstype in ["vfat", "msdos"] \ | ||
| 606 | and part.size_sec % 2: | ||
| 607 | logger.debug("Subtracting one sector from '%s' partition to " | ||
| 608 | "get even number of sectors for the partition", | ||
| 609 | part.mountpoint) | ||
| 610 | part.size_sec -= 1 | ||
| 611 | |||
| 612 | self._create_partition(self.path, part.type, | ||
| 613 | parted_fs_type, part.start, part.size_sec) | ||
| 614 | |||
| 615 | if self.ptable_format == "gpt-hybrid" and part.mbr: | ||
| 616 | hybrid_mbr_part_num += 1 | ||
| 617 | if hybrid_mbr_part_num > 4: | ||
| 618 | raise WicError("Extended MBR partitions are not supported in hybrid MBR") | ||
| 619 | self._create_partition(mbr_path, "primary", | ||
| 620 | parted_fs_type, part.start, part.size_sec) | ||
| 621 | |||
| 622 | if self.ptable_format in ("gpt", "gpt-hybrid") and (part.part_name or part.label): | ||
| 623 | partition_label = part.part_name if part.part_name else part.label | ||
| 624 | logger.debug("partition %d: set name to %s", | ||
| 625 | part.num, partition_label) | ||
| 626 | exec_native_cmd("sfdisk --sector-size %s --part-label %s %d %s" % \ | ||
| 627 | (self.sector_size, self.path, part.num, | ||
| 628 | partition_label), self.native_sysroot) | ||
| 629 | if part.part_type: | ||
| 630 | logger.debug("partition %d: set type UID to %s", | ||
| 631 | part.num, part.part_type) | ||
| 632 | exec_native_cmd("sfdisk --sector-size %s --part-type %s %d %s" % \ | ||
| 633 | (self.sector_size, self.path, part.num, | ||
| 634 | part.part_type), self.native_sysroot) | ||
| 635 | |||
| 636 | if part.uuid and self.ptable_format in ("gpt", "gpt-hybrid"): | ||
| 637 | logger.debug("partition %d: set UUID to %s", | ||
| 638 | part.num, part.uuid) | ||
| 639 | exec_native_cmd("sfdisk --sector-size %s --part-uuid %s %d %s" % \ | ||
| 640 | (self.sector_size, self.path, part.num, part.uuid), | ||
| 641 | self.native_sysroot) | ||
| 642 | |||
| 643 | if part.active: | ||
| 644 | flag_name = "legacy_boot" if self.ptable_format in ('gpt', 'gpt-hybrid') else "boot" | ||
| 645 | logger.debug("Set '%s' flag for partition '%s' on disk '%s'", | ||
| 646 | flag_name, part.num, self.path) | ||
| 647 | exec_native_cmd("export PARTED_SECTOR_SIZE=%d; parted -s %s set %d %s on" % \ | ||
| 648 | (self.sector_size, self.path, part.num, flag_name), | ||
| 649 | self.native_sysroot) | ||
| 650 | if self.ptable_format == 'gpt-hybrid' and part.mbr: | ||
| 651 | exec_native_cmd("export PARTED_SECTOR_SIZE=%d; parted -s %s set %d %s on" % \ | ||
| 652 | (self.sector_size, mbr_path, hybrid_mbr_part_num, "boot"), | ||
| 653 | self.native_sysroot) | ||
| 654 | if part.system_id: | ||
| 655 | exec_native_cmd("sfdisk --sector-size %s --part-type %s %s %s" % \ | ||
| 656 | (self.sector_size, self.path, part.num, part.system_id), | ||
| 657 | self.native_sysroot) | ||
| 658 | |||
| 659 | if part.hidden and self.ptable_format == "gpt": | ||
| 660 | logger.debug("Set hidden attribute for partition '%s' on disk '%s'", | ||
| 661 | part.num, self.path) | ||
| 662 | exec_native_cmd("sfdisk --sector-size %s --part-attrs %s %s RequiredPartition" % \ | ||
| 663 | (self.sector_size, self.path, part.num), | ||
| 664 | self.native_sysroot) | ||
| 665 | |||
| 666 | if self.ptable_format == "gpt-hybrid": | ||
| 667 | # Write a protective GPT partition | ||
| 668 | hybrid_mbr_part_num += 1 | ||
| 669 | if hybrid_mbr_part_num > 4: | ||
| 670 | raise WicError("Extended MBR partitions are not supported in hybrid MBR") | ||
| 671 | |||
| 672 | # parted cannot directly create a protective GPT partition, so | ||
| 673 | # create with an arbitrary type, then change it to the correct type | ||
| 674 | # with sfdisk | ||
| 675 | self._create_partition(mbr_path, "primary", "fat32", 1, GPT_OVERHEAD) | ||
| 676 | exec_native_cmd("sfdisk --sector-size %s --part-type %s %d 0xee" % \ | ||
| 677 | (self.sector_size, mbr_path, hybrid_mbr_part_num), | ||
| 678 | self.native_sysroot) | ||
| 679 | |||
| 680 | # Copy hybrid MBR | ||
| 681 | with open(mbr_path, "rb") as mbr_file: | ||
| 682 | with open(self.path, "r+b") as image_file: | ||
| 683 | mbr = mbr_file.read(512) | ||
| 684 | image_file.write(mbr) | ||
| 685 | |||
| 686 | def cleanup(self): | ||
| 687 | pass | ||
| 688 | |||
| 689 | def assemble(self): | ||
| 690 | logger.debug("Installing partitions") | ||
| 691 | |||
| 692 | for part in self.partitions: | ||
| 693 | source = part.source_file | ||
| 694 | if source: | ||
| 695 | # install source_file contents into a partition | ||
| 696 | sparse_copy(source, self.path, seek=part.start * self.sector_size) | ||
| 697 | |||
| 698 | logger.debug("Installed %s in partition %d, sectors %d-%d, " | ||
| 699 | "size %d sectors", source, part.num, part.start, | ||
| 700 | part.start + part.size_sec - 1, part.size_sec) | ||
| 701 | |||
| 702 | partimage = self.path + '.p%d' % part.num | ||
| 703 | os.rename(source, partimage) | ||
| 704 | self.partimages.append(partimage) | ||
diff --git a/scripts/lib/wic/plugins/source/bootimg-biosplusefi.py b/scripts/lib/wic/plugins/source/bootimg-biosplusefi.py deleted file mode 100644 index 5bd7390680..0000000000 --- a/scripts/lib/wic/plugins/source/bootimg-biosplusefi.py +++ /dev/null | |||
| @@ -1,213 +0,0 @@ | |||
| 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 | |||
| 21 | import types | ||
| 22 | |||
| 23 | from wic.pluginbase import SourcePlugin | ||
| 24 | from importlib.machinery import SourceFileLoader | ||
| 25 | |||
| 26 | class BootimgBiosPlusEFIPlugin(SourcePlugin): | ||
| 27 | """ | ||
| 28 | Create MBR + EFI boot partition | ||
| 29 | |||
| 30 | This plugin creates a boot partition that contains both | ||
| 31 | legacy BIOS and EFI content. It will be able to boot from both. | ||
| 32 | This is useful when managing PC fleet with some older machines | ||
| 33 | without EFI support. | ||
| 34 | |||
| 35 | Note it is possible to create an image that can boot from both | ||
| 36 | legacy BIOS and EFI by defining two partitions : one with arg | ||
| 37 | --source bootimg-efi and another one with --source bootimg-pcbios. | ||
| 38 | However, this method has the obvious downside that it requires TWO | ||
| 39 | partitions to be created on the storage device. | ||
| 40 | Both partitions will also be marked as "bootable" which does not work on | ||
| 41 | most BIOS, has BIOS often uses the "bootable" flag to determine | ||
| 42 | what to boot. If you have such a BIOS, you need to manually remove the | ||
| 43 | "bootable" flag from the EFI partition for the drive to be bootable. | ||
| 44 | Having two partitions also seems to confuse wic : the content of | ||
| 45 | the first partition will be duplicated into the second, even though it | ||
| 46 | will not be used at all. | ||
| 47 | |||
| 48 | Also, unlike "isoimage-isohybrid" that also does BIOS and EFI, this plugin | ||
| 49 | allows you to have more than only a single rootfs partitions and does | ||
| 50 | not turn the rootfs into an initramfs RAM image. | ||
| 51 | |||
| 52 | This plugin is made to put everything into a single /boot partition so it | ||
| 53 | does not have the limitations listed above. | ||
| 54 | |||
| 55 | The plugin is made so it does tries not to reimplement what's already | ||
| 56 | been done in other plugins; as such it imports "bootimg-pcbios" | ||
| 57 | and "bootimg-efi". | ||
| 58 | Plugin "bootimg-pcbios" is used to generate legacy BIOS boot. | ||
| 59 | Plugin "bootimg-efi" is used to generate the UEFI boot. Note that it | ||
| 60 | requires a --sourceparams argument to know which loader to use; refer | ||
| 61 | to "bootimg-efi" code/documentation for the list of loader. | ||
| 62 | |||
| 63 | Imports are handled with "SourceFileLoader" from importlib as it is | ||
| 64 | otherwise very difficult to import module that has hyphen "-" in their | ||
| 65 | filename. | ||
| 66 | The SourcePlugin() methods used in the plugins (do_install_disk, | ||
| 67 | do_configure_partition, do_prepare_partition) are then called on both, | ||
| 68 | beginning by "bootimg-efi". | ||
| 69 | |||
| 70 | Plugin options, such as "--sourceparams" can still be passed to a | ||
| 71 | plugin, as long they does not cause issue in the other plugin. | ||
| 72 | |||
| 73 | Example wic configuration: | ||
| 74 | part /boot --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\\ | ||
| 75 | --ondisk sda --label os_boot --active --align 1024 --use-uuid | ||
| 76 | """ | ||
| 77 | |||
| 78 | name = 'bootimg-biosplusefi' | ||
| 79 | |||
| 80 | __PCBIOS_MODULE_NAME = "bootimg-pcbios" | ||
| 81 | __EFI_MODULE_NAME = "bootimg-efi" | ||
| 82 | |||
| 83 | __imgEFIObj = None | ||
| 84 | __imgBiosObj = None | ||
| 85 | |||
| 86 | @classmethod | ||
| 87 | def __init__(cls): | ||
| 88 | """ | ||
| 89 | Constructor (init) | ||
| 90 | """ | ||
| 91 | |||
| 92 | # XXX | ||
| 93 | # For some reasons, __init__ constructor is never called. | ||
| 94 | # Something to do with how pluginbase works? | ||
| 95 | cls.__instanciateSubClasses() | ||
| 96 | |||
| 97 | @classmethod | ||
| 98 | def __instanciateSubClasses(cls): | ||
| 99 | """ | ||
| 100 | |||
| 101 | """ | ||
| 102 | |||
| 103 | # Import bootimg-pcbios (class name "BootimgPcbiosPlugin") | ||
| 104 | modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), | ||
| 105 | cls.__PCBIOS_MODULE_NAME + ".py") | ||
| 106 | loader = SourceFileLoader(cls.__PCBIOS_MODULE_NAME, modulePath) | ||
| 107 | mod = types.ModuleType(loader.name) | ||
| 108 | loader.exec_module(mod) | ||
| 109 | cls.__imgBiosObj = mod.BootimgPcbiosPlugin() | ||
| 110 | |||
| 111 | # Import bootimg-efi (class name "BootimgEFIPlugin") | ||
| 112 | modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), | ||
| 113 | cls.__EFI_MODULE_NAME + ".py") | ||
| 114 | loader = SourceFileLoader(cls.__EFI_MODULE_NAME, modulePath) | ||
| 115 | mod = types.ModuleType(loader.name) | ||
| 116 | loader.exec_module(mod) | ||
| 117 | cls.__imgEFIObj = mod.BootimgEFIPlugin() | ||
| 118 | |||
| 119 | @classmethod | ||
| 120 | def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, | ||
| 121 | bootimg_dir, kernel_dir, native_sysroot): | ||
| 122 | """ | ||
| 123 | Called after all partitions have been prepared and assembled into a | ||
| 124 | disk image. | ||
| 125 | """ | ||
| 126 | |||
| 127 | if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): | ||
| 128 | cls.__instanciateSubClasses() | ||
| 129 | |||
| 130 | cls.__imgEFIObj.do_install_disk( | ||
| 131 | disk, | ||
| 132 | disk_name, | ||
| 133 | creator, | ||
| 134 | workdir, | ||
| 135 | oe_builddir, | ||
| 136 | bootimg_dir, | ||
| 137 | kernel_dir, | ||
| 138 | native_sysroot) | ||
| 139 | |||
| 140 | cls.__imgBiosObj.do_install_disk( | ||
| 141 | disk, | ||
| 142 | disk_name, | ||
| 143 | creator, | ||
| 144 | workdir, | ||
| 145 | oe_builddir, | ||
| 146 | bootimg_dir, | ||
| 147 | kernel_dir, | ||
| 148 | native_sysroot) | ||
| 149 | |||
| 150 | @classmethod | ||
| 151 | def do_configure_partition(cls, part, source_params, creator, cr_workdir, | ||
| 152 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 153 | native_sysroot): | ||
| 154 | """ | ||
| 155 | Called before do_prepare_partition() | ||
| 156 | """ | ||
| 157 | |||
| 158 | if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): | ||
| 159 | cls.__instanciateSubClasses() | ||
| 160 | |||
| 161 | cls.__imgEFIObj.do_configure_partition( | ||
| 162 | part, | ||
| 163 | source_params, | ||
| 164 | creator, | ||
| 165 | cr_workdir, | ||
| 166 | oe_builddir, | ||
| 167 | bootimg_dir, | ||
| 168 | kernel_dir, | ||
| 169 | native_sysroot) | ||
| 170 | |||
| 171 | cls.__imgBiosObj.do_configure_partition( | ||
| 172 | part, | ||
| 173 | source_params, | ||
| 174 | creator, | ||
| 175 | cr_workdir, | ||
| 176 | oe_builddir, | ||
| 177 | bootimg_dir, | ||
| 178 | kernel_dir, | ||
| 179 | native_sysroot) | ||
| 180 | |||
| 181 | @classmethod | ||
| 182 | def do_prepare_partition(cls, part, source_params, creator, cr_workdir, | ||
| 183 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 184 | rootfs_dir, native_sysroot): | ||
| 185 | """ | ||
| 186 | Called to do the actual content population for a partition i.e. it | ||
| 187 | 'prepares' the partition to be incorporated into the image. | ||
| 188 | """ | ||
| 189 | |||
| 190 | if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): | ||
| 191 | cls.__instanciateSubClasses() | ||
| 192 | |||
| 193 | cls.__imgEFIObj.do_prepare_partition( | ||
| 194 | part, | ||
| 195 | source_params, | ||
| 196 | creator, | ||
| 197 | cr_workdir, | ||
| 198 | oe_builddir, | ||
| 199 | bootimg_dir, | ||
| 200 | kernel_dir, | ||
| 201 | rootfs_dir, | ||
| 202 | native_sysroot) | ||
| 203 | |||
| 204 | cls.__imgBiosObj.do_prepare_partition( | ||
| 205 | part, | ||
| 206 | source_params, | ||
| 207 | creator, | ||
| 208 | cr_workdir, | ||
| 209 | oe_builddir, | ||
| 210 | bootimg_dir, | ||
| 211 | kernel_dir, | ||
| 212 | rootfs_dir, | ||
| 213 | native_sysroot) | ||
diff --git a/scripts/lib/wic/plugins/source/bootimg-efi.py b/scripts/lib/wic/plugins/source/bootimg-efi.py deleted file mode 100644 index 38da5080fb..0000000000 --- a/scripts/lib/wic/plugins/source/bootimg-efi.py +++ /dev/null | |||
| @@ -1,435 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright (c) 2014, Intel Corporation. | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | # DESCRIPTION | ||
| 7 | # This implements the 'bootimg-efi' source plugin class for 'wic' | ||
| 8 | # | ||
| 9 | # AUTHORS | ||
| 10 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> | ||
| 11 | # | ||
| 12 | |||
| 13 | import logging | ||
| 14 | import os | ||
| 15 | import tempfile | ||
| 16 | import shutil | ||
| 17 | import re | ||
| 18 | |||
| 19 | from glob import glob | ||
| 20 | |||
| 21 | from wic import WicError | ||
| 22 | from wic.engine import get_custom_config | ||
| 23 | from wic.pluginbase import SourcePlugin | ||
| 24 | from wic.misc import (exec_cmd, exec_native_cmd, | ||
| 25 | get_bitbake_var, BOOTDD_EXTRA_SPACE) | ||
| 26 | |||
| 27 | logger = logging.getLogger('wic') | ||
| 28 | |||
| 29 | class BootimgEFIPlugin(SourcePlugin): | ||
| 30 | """ | ||
| 31 | Create EFI boot partition. | ||
| 32 | This plugin supports GRUB 2 and systemd-boot bootloaders. | ||
| 33 | """ | ||
| 34 | |||
| 35 | name = 'bootimg-efi' | ||
| 36 | |||
| 37 | @classmethod | ||
| 38 | def _copy_additional_files(cls, hdddir, initrd, dtb): | ||
| 39 | bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") | ||
| 40 | if not bootimg_dir: | ||
| 41 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") | ||
| 42 | |||
| 43 | if initrd: | ||
| 44 | initrds = initrd.split(';') | ||
| 45 | for rd in initrds: | ||
| 46 | cp_cmd = "cp -v -p %s/%s %s" % (bootimg_dir, rd, hdddir) | ||
| 47 | out = exec_cmd(cp_cmd, True) | ||
| 48 | logger.debug("initrd files:\n%s" % (out)) | ||
| 49 | else: | ||
| 50 | logger.debug("Ignoring missing initrd") | ||
| 51 | |||
| 52 | if dtb: | ||
| 53 | if ';' in dtb: | ||
| 54 | raise WicError("Only one DTB supported, exiting") | ||
| 55 | cp_cmd = "cp -v -p %s/%s %s" % (bootimg_dir, dtb, hdddir) | ||
| 56 | out = exec_cmd(cp_cmd, True) | ||
| 57 | logger.debug("dtb files:\n%s" % (out)) | ||
| 58 | |||
| 59 | @classmethod | ||
| 60 | def do_configure_grubefi(cls, hdddir, creator, cr_workdir, source_params): | ||
| 61 | """ | ||
| 62 | Create loader-specific (grub-efi) config | ||
| 63 | """ | ||
| 64 | configfile = creator.ks.bootloader.configfile | ||
| 65 | custom_cfg = None | ||
| 66 | if configfile: | ||
| 67 | custom_cfg = get_custom_config(configfile) | ||
| 68 | if custom_cfg: | ||
| 69 | # Use a custom configuration for grub | ||
| 70 | grubefi_conf = custom_cfg | ||
| 71 | logger.debug("Using custom configuration file " | ||
| 72 | "%s for grub.cfg", configfile) | ||
| 73 | else: | ||
| 74 | raise WicError("configfile is specified but failed to " | ||
| 75 | "get it from %s." % configfile) | ||
| 76 | |||
| 77 | initrd = source_params.get('initrd') | ||
| 78 | dtb = source_params.get('dtb') | ||
| 79 | |||
| 80 | cls._copy_additional_files(hdddir, initrd, dtb) | ||
| 81 | |||
| 82 | if not custom_cfg: | ||
| 83 | # Create grub configuration using parameters from wks file | ||
| 84 | bootloader = creator.ks.bootloader | ||
| 85 | title = source_params.get('title') | ||
| 86 | |||
| 87 | grubefi_conf = "" | ||
| 88 | grubefi_conf += "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n" | ||
| 89 | grubefi_conf += "default=boot\n" | ||
| 90 | grubefi_conf += "timeout=%s\n" % bootloader.timeout | ||
| 91 | grubefi_conf += "menuentry '%s'{\n" % (title if title else "boot") | ||
| 92 | |||
| 93 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 94 | if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": | ||
| 95 | if get_bitbake_var("INITRAMFS_IMAGE"): | ||
| 96 | kernel = "%s-%s.bin" % \ | ||
| 97 | (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) | ||
| 98 | |||
| 99 | label = source_params.get('label') | ||
| 100 | label_conf = "root=%s" % creator.rootdev | ||
| 101 | if label: | ||
| 102 | label_conf = "LABEL=%s" % label | ||
| 103 | |||
| 104 | grubefi_conf += "linux /%s %s rootwait %s\n" \ | ||
| 105 | % (kernel, label_conf, bootloader.append) | ||
| 106 | |||
| 107 | if initrd: | ||
| 108 | initrds = initrd.split(';') | ||
| 109 | grubefi_conf += "initrd" | ||
| 110 | for rd in initrds: | ||
| 111 | grubefi_conf += " /%s" % rd | ||
| 112 | grubefi_conf += "\n" | ||
| 113 | |||
| 114 | if dtb: | ||
| 115 | grubefi_conf += "devicetree /%s\n" % dtb | ||
| 116 | |||
| 117 | grubefi_conf += "}\n" | ||
| 118 | |||
| 119 | logger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg", | ||
| 120 | cr_workdir) | ||
| 121 | cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w") | ||
| 122 | cfg.write(grubefi_conf) | ||
| 123 | cfg.close() | ||
| 124 | |||
| 125 | @classmethod | ||
| 126 | def do_configure_systemdboot(cls, hdddir, creator, cr_workdir, source_params): | ||
| 127 | """ | ||
| 128 | Create loader-specific systemd-boot/gummiboot config. Unified Kernel Image (uki) | ||
| 129 | support is done in image recipe with uki.bbclass and only systemd-boot loader config | ||
| 130 | and ESP partition structure is created here. | ||
| 131 | """ | ||
| 132 | # detect uki.bbclass usage | ||
| 133 | image_classes = get_bitbake_var("IMAGE_CLASSES").split() | ||
| 134 | unified_image = False | ||
| 135 | if "uki" in image_classes: | ||
| 136 | unified_image = True | ||
| 137 | |||
| 138 | install_cmd = "install -d %s/loader" % hdddir | ||
| 139 | exec_cmd(install_cmd) | ||
| 140 | |||
| 141 | install_cmd = "install -d %s/loader/entries" % hdddir | ||
| 142 | exec_cmd(install_cmd) | ||
| 143 | |||
| 144 | bootloader = creator.ks.bootloader | ||
| 145 | loader_conf = "" | ||
| 146 | |||
| 147 | # 5 seconds is a sensible default timeout | ||
| 148 | loader_conf += "timeout %d\n" % (bootloader.timeout or 5) | ||
| 149 | |||
| 150 | logger.debug("Writing systemd-boot config " | ||
| 151 | "%s/hdd/boot/loader/loader.conf", cr_workdir) | ||
| 152 | cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w") | ||
| 153 | cfg.write(loader_conf) | ||
| 154 | logger.debug("loader.conf:\n%s" % (loader_conf)) | ||
| 155 | cfg.close() | ||
| 156 | |||
| 157 | initrd = source_params.get('initrd') | ||
| 158 | dtb = source_params.get('dtb') | ||
| 159 | if not unified_image: | ||
| 160 | cls._copy_additional_files(hdddir, initrd, dtb) | ||
| 161 | |||
| 162 | configfile = creator.ks.bootloader.configfile | ||
| 163 | custom_cfg = None | ||
| 164 | boot_conf = "" | ||
| 165 | if configfile: | ||
| 166 | custom_cfg = get_custom_config(configfile) | ||
| 167 | if custom_cfg: | ||
| 168 | # Use a custom configuration for systemd-boot | ||
| 169 | boot_conf = custom_cfg | ||
| 170 | logger.debug("Using custom configuration file " | ||
| 171 | "%s for systemd-boots's boot.conf", configfile) | ||
| 172 | else: | ||
| 173 | raise WicError("configfile is specified but failed to " | ||
| 174 | "get it from %s.", configfile) | ||
| 175 | else: | ||
| 176 | # Create systemd-boot configuration using parameters from wks file | ||
| 177 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 178 | if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": | ||
| 179 | if get_bitbake_var("INITRAMFS_IMAGE"): | ||
| 180 | kernel = "%s-%s.bin" % \ | ||
| 181 | (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) | ||
| 182 | |||
| 183 | title = source_params.get('title') | ||
| 184 | |||
| 185 | boot_conf += "title %s\n" % (title if title else "boot") | ||
| 186 | boot_conf += "linux /%s\n" % kernel | ||
| 187 | |||
| 188 | label = source_params.get('label') | ||
| 189 | label_conf = "LABEL=Boot root=%s" % creator.rootdev | ||
| 190 | if label: | ||
| 191 | label_conf = "LABEL=%s" % label | ||
| 192 | |||
| 193 | boot_conf += "options %s %s\n" % \ | ||
| 194 | (label_conf, bootloader.append) | ||
| 195 | |||
| 196 | if initrd: | ||
| 197 | initrds = initrd.split(';') | ||
| 198 | for rd in initrds: | ||
| 199 | boot_conf += "initrd /%s\n" % rd | ||
| 200 | |||
| 201 | if dtb: | ||
| 202 | boot_conf += "devicetree /%s\n" % dtb | ||
| 203 | |||
| 204 | if not unified_image: | ||
| 205 | logger.debug("Writing systemd-boot config " | ||
| 206 | "%s/hdd/boot/loader/entries/boot.conf", cr_workdir) | ||
| 207 | cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w") | ||
| 208 | cfg.write(boot_conf) | ||
| 209 | logger.debug("boot.conf:\n%s" % (boot_conf)) | ||
| 210 | cfg.close() | ||
| 211 | |||
| 212 | |||
| 213 | @classmethod | ||
| 214 | def do_configure_partition(cls, part, source_params, creator, cr_workdir, | ||
| 215 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 216 | native_sysroot): | ||
| 217 | """ | ||
| 218 | Called before do_prepare_partition(), creates loader-specific config | ||
| 219 | """ | ||
| 220 | hdddir = "%s/hdd/boot" % cr_workdir | ||
| 221 | |||
| 222 | install_cmd = "install -d %s/EFI/BOOT" % hdddir | ||
| 223 | exec_cmd(install_cmd) | ||
| 224 | |||
| 225 | try: | ||
| 226 | if source_params['loader'] == 'grub-efi': | ||
| 227 | cls.do_configure_grubefi(hdddir, creator, cr_workdir, source_params) | ||
| 228 | elif source_params['loader'] == 'systemd-boot': | ||
| 229 | cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params) | ||
| 230 | elif source_params['loader'] == 'uefi-kernel': | ||
| 231 | pass | ||
| 232 | else: | ||
| 233 | raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader']) | ||
| 234 | except KeyError: | ||
| 235 | raise WicError("bootimg-efi requires a loader, none specified") | ||
| 236 | |||
| 237 | if get_bitbake_var("IMAGE_EFI_BOOT_FILES") is None: | ||
| 238 | logger.debug('No boot files defined in IMAGE_EFI_BOOT_FILES') | ||
| 239 | else: | ||
| 240 | boot_files = None | ||
| 241 | for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)): | ||
| 242 | if fmt: | ||
| 243 | var = fmt % id | ||
| 244 | else: | ||
| 245 | var = "" | ||
| 246 | |||
| 247 | boot_files = get_bitbake_var("IMAGE_EFI_BOOT_FILES" + var) | ||
| 248 | if boot_files: | ||
| 249 | break | ||
| 250 | |||
| 251 | logger.debug('Boot files: %s', boot_files) | ||
| 252 | |||
| 253 | # list of tuples (src_name, dst_name) | ||
| 254 | deploy_files = [] | ||
| 255 | for src_entry in re.findall(r'[\w;\-\.\+/\*]+', boot_files): | ||
| 256 | if ';' in src_entry: | ||
| 257 | dst_entry = tuple(src_entry.split(';')) | ||
| 258 | if not dst_entry[0] or not dst_entry[1]: | ||
| 259 | raise WicError('Malformed boot file entry: %s' % src_entry) | ||
| 260 | else: | ||
| 261 | dst_entry = (src_entry, src_entry) | ||
| 262 | |||
| 263 | logger.debug('Destination entry: %r', dst_entry) | ||
| 264 | deploy_files.append(dst_entry) | ||
| 265 | |||
| 266 | cls.install_task = []; | ||
| 267 | for deploy_entry in deploy_files: | ||
| 268 | src, dst = deploy_entry | ||
| 269 | if '*' in src: | ||
| 270 | # by default install files under their basename | ||
| 271 | entry_name_fn = os.path.basename | ||
| 272 | if dst != src: | ||
| 273 | # unless a target name was given, then treat name | ||
| 274 | # as a directory and append a basename | ||
| 275 | entry_name_fn = lambda name: \ | ||
| 276 | os.path.join(dst, | ||
| 277 | os.path.basename(name)) | ||
| 278 | |||
| 279 | srcs = glob(os.path.join(kernel_dir, src)) | ||
| 280 | |||
| 281 | logger.debug('Globbed sources: %s', ', '.join(srcs)) | ||
| 282 | for entry in srcs: | ||
| 283 | src = os.path.relpath(entry, kernel_dir) | ||
| 284 | entry_dst_name = entry_name_fn(entry) | ||
| 285 | cls.install_task.append((src, entry_dst_name)) | ||
| 286 | else: | ||
| 287 | cls.install_task.append((src, dst)) | ||
| 288 | |||
| 289 | @classmethod | ||
| 290 | def do_prepare_partition(cls, part, source_params, creator, cr_workdir, | ||
| 291 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 292 | rootfs_dir, native_sysroot): | ||
| 293 | """ | ||
| 294 | Called to do the actual content population for a partition i.e. it | ||
| 295 | 'prepares' the partition to be incorporated into the image. | ||
| 296 | In this case, prepare content for an EFI (grub) boot partition. | ||
| 297 | """ | ||
| 298 | if not kernel_dir: | ||
| 299 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") | ||
| 300 | if not kernel_dir: | ||
| 301 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") | ||
| 302 | |||
| 303 | staging_kernel_dir = kernel_dir | ||
| 304 | |||
| 305 | hdddir = "%s/hdd/boot" % cr_workdir | ||
| 306 | |||
| 307 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 308 | if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": | ||
| 309 | if get_bitbake_var("INITRAMFS_IMAGE"): | ||
| 310 | kernel = "%s-%s.bin" % \ | ||
| 311 | (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) | ||
| 312 | |||
| 313 | if source_params.get('create-unified-kernel-image') == "true": | ||
| 314 | raise WicError("create-unified-kernel-image is no longer supported. Please use uki.bbclass.") | ||
| 315 | |||
| 316 | if source_params.get('install-kernel-into-boot-dir') != 'false': | ||
| 317 | install_cmd = "install -v -p -m 0644 %s/%s %s/%s" % \ | ||
| 318 | (staging_kernel_dir, kernel, hdddir, kernel) | ||
| 319 | out = exec_cmd(install_cmd) | ||
| 320 | logger.debug("Installed kernel files:\n%s" % out) | ||
| 321 | |||
| 322 | if get_bitbake_var("IMAGE_EFI_BOOT_FILES"): | ||
| 323 | for src_path, dst_path in cls.install_task: | ||
| 324 | install_cmd = "install -v -p -m 0644 -D %s %s" \ | ||
| 325 | % (os.path.join(kernel_dir, src_path), | ||
| 326 | os.path.join(hdddir, dst_path)) | ||
| 327 | out = exec_cmd(install_cmd) | ||
| 328 | logger.debug("Installed IMAGE_EFI_BOOT_FILES:\n%s" % out) | ||
| 329 | |||
| 330 | try: | ||
| 331 | if source_params['loader'] == 'grub-efi': | ||
| 332 | shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, | ||
| 333 | "%s/grub.cfg" % cr_workdir) | ||
| 334 | for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]: | ||
| 335 | cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:]) | ||
| 336 | exec_cmd(cp_cmd, True) | ||
| 337 | shutil.move("%s/grub.cfg" % cr_workdir, | ||
| 338 | "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir) | ||
| 339 | elif source_params['loader'] == 'systemd-boot': | ||
| 340 | for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]: | ||
| 341 | cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:]) | ||
| 342 | out = exec_cmd(cp_cmd, True) | ||
| 343 | logger.debug("systemd-boot files:\n%s" % out) | ||
| 344 | elif source_params['loader'] == 'uefi-kernel': | ||
| 345 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 346 | if not kernel: | ||
| 347 | raise WicError("Empty KERNEL_IMAGETYPE") | ||
| 348 | target = get_bitbake_var("TARGET_SYS") | ||
| 349 | if not target: | ||
| 350 | raise WicError("Empty TARGET_SYS") | ||
| 351 | |||
| 352 | if re.match("x86_64", target): | ||
| 353 | kernel_efi_image = "bootx64.efi" | ||
| 354 | elif re.match('i.86', target): | ||
| 355 | kernel_efi_image = "bootia32.efi" | ||
| 356 | elif re.match('aarch64', target): | ||
| 357 | kernel_efi_image = "bootaa64.efi" | ||
| 358 | elif re.match('arm', target): | ||
| 359 | kernel_efi_image = "bootarm.efi" | ||
| 360 | else: | ||
| 361 | raise WicError("UEFI stub kernel is incompatible with target %s" % target) | ||
| 362 | |||
| 363 | for mod in [x for x in os.listdir(kernel_dir) if x.startswith(kernel)]: | ||
| 364 | cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, kernel_efi_image) | ||
| 365 | out = exec_cmd(cp_cmd, True) | ||
| 366 | logger.debug("uefi-kernel files:\n%s" % out) | ||
| 367 | else: | ||
| 368 | raise WicError("unrecognized bootimg-efi loader: %s" % | ||
| 369 | source_params['loader']) | ||
| 370 | |||
| 371 | # must have installed at least one EFI bootloader | ||
| 372 | out = glob(os.path.join(hdddir, 'EFI', 'BOOT', 'boot*.efi')) | ||
| 373 | logger.debug("Installed EFI loader files:\n%s" % out) | ||
| 374 | if not out: | ||
| 375 | raise WicError("No EFI loaders installed to ESP partition. Check that grub-efi, systemd-boot or similar is installed.") | ||
| 376 | |||
| 377 | except KeyError: | ||
| 378 | raise WicError("bootimg-efi requires a loader, none specified") | ||
| 379 | |||
| 380 | startup = os.path.join(kernel_dir, "startup.nsh") | ||
| 381 | if os.path.exists(startup): | ||
| 382 | cp_cmd = "cp -v -p %s %s/" % (startup, hdddir) | ||
| 383 | out = exec_cmd(cp_cmd, True) | ||
| 384 | logger.debug("startup files:\n%s" % out) | ||
| 385 | |||
| 386 | for paths in part.include_path or []: | ||
| 387 | for path in paths: | ||
| 388 | cp_cmd = "cp -v -p -r %s %s/" % (path, hdddir) | ||
| 389 | exec_cmd(cp_cmd, True) | ||
| 390 | logger.debug("include_path files:\n%s" % out) | ||
| 391 | |||
| 392 | du_cmd = "du -bks %s" % hdddir | ||
| 393 | out = exec_cmd(du_cmd) | ||
| 394 | blocks = int(out.split()[0]) | ||
| 395 | |||
| 396 | extra_blocks = part.get_extra_block_count(blocks) | ||
| 397 | |||
| 398 | if extra_blocks < BOOTDD_EXTRA_SPACE: | ||
| 399 | extra_blocks = BOOTDD_EXTRA_SPACE | ||
| 400 | |||
| 401 | blocks += extra_blocks | ||
| 402 | |||
| 403 | logger.debug("Added %d extra blocks to %s to get to %d total blocks", | ||
| 404 | extra_blocks, part.mountpoint, blocks) | ||
| 405 | |||
| 406 | # required for compatibility with certain devices expecting file system | ||
| 407 | # block count to be equal to partition block count | ||
| 408 | if blocks < part.fixed_size: | ||
| 409 | blocks = part.fixed_size | ||
| 410 | logger.debug("Overriding %s to %d total blocks for compatibility", | ||
| 411 | part.mountpoint, blocks) | ||
| 412 | |||
| 413 | # dosfs image, created by mkdosfs | ||
| 414 | bootimg = "%s/boot.img" % cr_workdir | ||
| 415 | |||
| 416 | label = part.label if part.label else "ESP" | ||
| 417 | |||
| 418 | dosfs_cmd = "mkdosfs -v -n %s -i %s -C %s %d" % \ | ||
| 419 | (label, part.fsuuid, bootimg, blocks) | ||
| 420 | exec_native_cmd(dosfs_cmd, native_sysroot) | ||
| 421 | logger.debug("mkdosfs:\n%s" % (str(out))) | ||
| 422 | |||
| 423 | mcopy_cmd = "mcopy -v -p -i %s -s %s/* ::/" % (bootimg, hdddir) | ||
| 424 | out = exec_native_cmd(mcopy_cmd, native_sysroot) | ||
| 425 | logger.debug("mcopy:\n%s" % (str(out))) | ||
| 426 | |||
| 427 | chmod_cmd = "chmod 644 %s" % bootimg | ||
| 428 | exec_cmd(chmod_cmd) | ||
| 429 | |||
| 430 | du_cmd = "du -Lbks %s" % bootimg | ||
| 431 | out = exec_cmd(du_cmd) | ||
| 432 | bootimg_size = out.split()[0] | ||
| 433 | |||
| 434 | part.size = int(bootimg_size) | ||
| 435 | part.source_file = bootimg | ||
diff --git a/scripts/lib/wic/plugins/source/bootimg-partition.py b/scripts/lib/wic/plugins/source/bootimg-partition.py deleted file mode 100644 index 589853a439..0000000000 --- a/scripts/lib/wic/plugins/source/bootimg-partition.py +++ /dev/null | |||
| @@ -1,162 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright OpenEmbedded Contributors | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | # DESCRIPTION | ||
| 7 | # This implements the 'bootimg-partition' source plugin class for | ||
| 8 | # 'wic'. The plugin creates an image of boot partition, copying over | ||
| 9 | # files listed in IMAGE_BOOT_FILES bitbake variable. | ||
| 10 | # | ||
| 11 | # AUTHORS | ||
| 12 | # Maciej Borzecki <maciej.borzecki (at] open-rnd.pl> | ||
| 13 | # | ||
| 14 | |||
| 15 | import logging | ||
| 16 | import os | ||
| 17 | import re | ||
| 18 | |||
| 19 | from oe.bootfiles import get_boot_files | ||
| 20 | |||
| 21 | from wic import WicError | ||
| 22 | from wic.engine import get_custom_config | ||
| 23 | from wic.pluginbase import SourcePlugin | ||
| 24 | from wic.misc import exec_cmd, get_bitbake_var | ||
| 25 | |||
| 26 | logger = logging.getLogger('wic') | ||
| 27 | |||
| 28 | class BootimgPartitionPlugin(SourcePlugin): | ||
| 29 | """ | ||
| 30 | Create an image of boot partition, copying over files | ||
| 31 | listed in IMAGE_BOOT_FILES bitbake variable. | ||
| 32 | """ | ||
| 33 | |||
| 34 | name = 'bootimg-partition' | ||
| 35 | image_boot_files_var_name = 'IMAGE_BOOT_FILES' | ||
| 36 | |||
| 37 | @classmethod | ||
| 38 | def do_configure_partition(cls, part, source_params, cr, cr_workdir, | ||
| 39 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 40 | native_sysroot): | ||
| 41 | """ | ||
| 42 | Called before do_prepare_partition(), create u-boot specific boot config | ||
| 43 | """ | ||
| 44 | hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) | ||
| 45 | install_cmd = "install -d %s" % hdddir | ||
| 46 | exec_cmd(install_cmd) | ||
| 47 | |||
| 48 | if not kernel_dir: | ||
| 49 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") | ||
| 50 | if not kernel_dir: | ||
| 51 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") | ||
| 52 | |||
| 53 | boot_files = None | ||
| 54 | for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)): | ||
| 55 | if fmt: | ||
| 56 | var = fmt % id | ||
| 57 | else: | ||
| 58 | var = "" | ||
| 59 | |||
| 60 | boot_files = get_bitbake_var(cls.image_boot_files_var_name + var) | ||
| 61 | if boot_files is not None: | ||
| 62 | break | ||
| 63 | |||
| 64 | if boot_files is None: | ||
| 65 | raise WicError('No boot files defined, %s unset for entry #%d' % (cls.image_boot_files_var_name, part.lineno)) | ||
| 66 | |||
| 67 | logger.debug('Boot files: %s', boot_files) | ||
| 68 | |||
| 69 | cls.install_task = get_boot_files(kernel_dir, boot_files) | ||
| 70 | if source_params.get('loader') != "u-boot": | ||
| 71 | return | ||
| 72 | |||
| 73 | configfile = cr.ks.bootloader.configfile | ||
| 74 | custom_cfg = None | ||
| 75 | if configfile: | ||
| 76 | custom_cfg = get_custom_config(configfile) | ||
| 77 | if custom_cfg: | ||
| 78 | # Use a custom configuration for extlinux.conf | ||
| 79 | extlinux_conf = custom_cfg | ||
| 80 | logger.debug("Using custom configuration file " | ||
| 81 | "%s for extlinux.conf", configfile) | ||
| 82 | else: | ||
| 83 | raise WicError("configfile is specified but failed to " | ||
| 84 | "get it from %s." % configfile) | ||
| 85 | |||
| 86 | if not custom_cfg: | ||
| 87 | # The kernel types supported by the sysboot of u-boot | ||
| 88 | kernel_types = ["zImage", "Image", "fitImage", "uImage", "vmlinux"] | ||
| 89 | has_dtb = False | ||
| 90 | fdt_dir = '/' | ||
| 91 | kernel_name = None | ||
| 92 | |||
| 93 | # Find the kernel image name, from the highest precedence to lowest | ||
| 94 | for image in kernel_types: | ||
| 95 | for task in cls.install_task: | ||
| 96 | src, dst = task | ||
| 97 | if re.match(image, src): | ||
| 98 | kernel_name = os.path.join('/', dst) | ||
| 99 | break | ||
| 100 | if kernel_name: | ||
| 101 | break | ||
| 102 | |||
| 103 | for task in cls.install_task: | ||
| 104 | src, dst = task | ||
| 105 | # We suppose that all the dtb are in the same directory | ||
| 106 | if re.search(r'\.dtb', src) and fdt_dir == '/': | ||
| 107 | has_dtb = True | ||
| 108 | fdt_dir = os.path.join(fdt_dir, os.path.dirname(dst)) | ||
| 109 | break | ||
| 110 | |||
| 111 | if not kernel_name: | ||
| 112 | raise WicError('No kernel file found') | ||
| 113 | |||
| 114 | # Compose the extlinux.conf | ||
| 115 | extlinux_conf = "default Yocto\n" | ||
| 116 | extlinux_conf += "label Yocto\n" | ||
| 117 | extlinux_conf += " kernel %s\n" % kernel_name | ||
| 118 | if has_dtb: | ||
| 119 | extlinux_conf += " fdtdir %s\n" % fdt_dir | ||
| 120 | bootloader = cr.ks.bootloader | ||
| 121 | extlinux_conf += "append root=%s rootwait %s\n" \ | ||
| 122 | % (cr.rootdev, bootloader.append if bootloader.append else '') | ||
| 123 | |||
| 124 | install_cmd = "install -d %s/extlinux/" % hdddir | ||
| 125 | exec_cmd(install_cmd) | ||
| 126 | cfg = open("%s/extlinux/extlinux.conf" % hdddir, "w") | ||
| 127 | cfg.write(extlinux_conf) | ||
| 128 | cfg.close() | ||
| 129 | |||
| 130 | |||
| 131 | @classmethod | ||
| 132 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, | ||
| 133 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 134 | rootfs_dir, native_sysroot): | ||
| 135 | """ | ||
| 136 | Called to do the actual content population for a partition i.e. it | ||
| 137 | 'prepares' the partition to be incorporated into the image. | ||
| 138 | In this case, does the following: | ||
| 139 | - sets up a vfat partition | ||
| 140 | - copies all files listed in IMAGE_BOOT_FILES variable | ||
| 141 | """ | ||
| 142 | hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) | ||
| 143 | |||
| 144 | if not kernel_dir: | ||
| 145 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") | ||
| 146 | if not kernel_dir: | ||
| 147 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") | ||
| 148 | |||
| 149 | logger.debug('Kernel dir: %s', bootimg_dir) | ||
| 150 | |||
| 151 | |||
| 152 | for task in cls.install_task: | ||
| 153 | src_path, dst_path = task | ||
| 154 | logger.debug('Install %s as %s', src_path, dst_path) | ||
| 155 | install_cmd = "install -m 0644 -D %s %s" \ | ||
| 156 | % (os.path.join(kernel_dir, src_path), | ||
| 157 | os.path.join(hdddir, dst_path)) | ||
| 158 | exec_cmd(install_cmd) | ||
| 159 | |||
| 160 | logger.debug('Prepare boot partition using rootfs in %s', hdddir) | ||
| 161 | part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, | ||
| 162 | native_sysroot, False) | ||
diff --git a/scripts/lib/wic/plugins/source/bootimg-pcbios.py b/scripts/lib/wic/plugins/source/bootimg-pcbios.py deleted file mode 100644 index a207a83530..0000000000 --- a/scripts/lib/wic/plugins/source/bootimg-pcbios.py +++ /dev/null | |||
| @@ -1,209 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright (c) 2014, Intel Corporation. | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | # DESCRIPTION | ||
| 7 | # This implements the 'bootimg-pcbios' source plugin class for 'wic' | ||
| 8 | # | ||
| 9 | # AUTHORS | ||
| 10 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> | ||
| 11 | # | ||
| 12 | |||
| 13 | import logging | ||
| 14 | import os | ||
| 15 | import re | ||
| 16 | |||
| 17 | from wic import WicError | ||
| 18 | from wic.engine import get_custom_config | ||
| 19 | from wic.pluginbase import SourcePlugin | ||
| 20 | from wic.misc import (exec_cmd, exec_native_cmd, | ||
| 21 | get_bitbake_var, BOOTDD_EXTRA_SPACE) | ||
| 22 | |||
| 23 | logger = logging.getLogger('wic') | ||
| 24 | |||
| 25 | class BootimgPcbiosPlugin(SourcePlugin): | ||
| 26 | """ | ||
| 27 | Create MBR boot partition and install syslinux on it. | ||
| 28 | """ | ||
| 29 | |||
| 30 | name = 'bootimg-pcbios' | ||
| 31 | |||
| 32 | @classmethod | ||
| 33 | def _get_bootimg_dir(cls, bootimg_dir, dirname): | ||
| 34 | """ | ||
| 35 | Check if dirname exists in default bootimg_dir or in STAGING_DIR. | ||
| 36 | """ | ||
| 37 | staging_datadir = get_bitbake_var("STAGING_DATADIR") | ||
| 38 | for result in (bootimg_dir, staging_datadir): | ||
| 39 | if os.path.exists("%s/%s" % (result, dirname)): | ||
| 40 | return result | ||
| 41 | |||
| 42 | # STAGING_DATADIR is expanded with MLPREFIX if multilib is enabled | ||
| 43 | # but dependency syslinux is still populated to original STAGING_DATADIR | ||
| 44 | nonarch_datadir = re.sub('/[^/]*recipe-sysroot', '/recipe-sysroot', staging_datadir) | ||
| 45 | if os.path.exists(os.path.join(nonarch_datadir, dirname)): | ||
| 46 | return nonarch_datadir | ||
| 47 | |||
| 48 | raise WicError("Couldn't find correct bootimg_dir, exiting") | ||
| 49 | |||
| 50 | @classmethod | ||
| 51 | def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, | ||
| 52 | bootimg_dir, kernel_dir, native_sysroot): | ||
| 53 | """ | ||
| 54 | Called after all partitions have been prepared and assembled into a | ||
| 55 | disk image. In this case, we install the MBR. | ||
| 56 | """ | ||
| 57 | bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') | ||
| 58 | mbrfile = "%s/syslinux/" % bootimg_dir | ||
| 59 | if creator.ptable_format == 'msdos': | ||
| 60 | mbrfile += "mbr.bin" | ||
| 61 | elif creator.ptable_format == 'gpt': | ||
| 62 | mbrfile += "gptmbr.bin" | ||
| 63 | else: | ||
| 64 | raise WicError("Unsupported partition table: %s" % | ||
| 65 | creator.ptable_format) | ||
| 66 | |||
| 67 | if not os.path.exists(mbrfile): | ||
| 68 | raise WicError("Couldn't find %s. If using the -e option, do you " | ||
| 69 | "have the right MACHINE set in local.conf? If not, " | ||
| 70 | "is the bootimg_dir path correct?" % mbrfile) | ||
| 71 | |||
| 72 | full_path = creator._full_path(workdir, disk_name, "direct") | ||
| 73 | logger.debug("Installing MBR on disk %s as %s with size %s bytes", | ||
| 74 | disk_name, full_path, disk.min_size) | ||
| 75 | |||
| 76 | dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path) | ||
| 77 | exec_cmd(dd_cmd, native_sysroot) | ||
| 78 | |||
| 79 | @classmethod | ||
| 80 | def do_configure_partition(cls, part, source_params, creator, cr_workdir, | ||
| 81 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 82 | native_sysroot): | ||
| 83 | """ | ||
| 84 | Called before do_prepare_partition(), creates syslinux config | ||
| 85 | """ | ||
| 86 | hdddir = "%s/hdd/boot" % cr_workdir | ||
| 87 | |||
| 88 | install_cmd = "install -d %s" % hdddir | ||
| 89 | exec_cmd(install_cmd) | ||
| 90 | |||
| 91 | bootloader = creator.ks.bootloader | ||
| 92 | |||
| 93 | custom_cfg = None | ||
| 94 | if bootloader.configfile: | ||
| 95 | custom_cfg = get_custom_config(bootloader.configfile) | ||
| 96 | if custom_cfg: | ||
| 97 | # Use a custom configuration for grub | ||
| 98 | syslinux_conf = custom_cfg | ||
| 99 | logger.debug("Using custom configuration file %s " | ||
| 100 | "for syslinux.cfg", bootloader.configfile) | ||
| 101 | else: | ||
| 102 | raise WicError("configfile is specified but failed to " | ||
| 103 | "get it from %s." % bootloader.configfile) | ||
| 104 | |||
| 105 | if not custom_cfg: | ||
| 106 | # Create syslinux configuration using parameters from wks file | ||
| 107 | splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg") | ||
| 108 | if os.path.exists(splash): | ||
| 109 | splashline = "menu background splash.jpg" | ||
| 110 | else: | ||
| 111 | splashline = "" | ||
| 112 | |||
| 113 | syslinux_conf = "" | ||
| 114 | syslinux_conf += "PROMPT 0\n" | ||
| 115 | syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n" | ||
| 116 | syslinux_conf += "\n" | ||
| 117 | syslinux_conf += "ALLOWOPTIONS 1\n" | ||
| 118 | syslinux_conf += "SERIAL 0 115200\n" | ||
| 119 | syslinux_conf += "\n" | ||
| 120 | if splashline: | ||
| 121 | syslinux_conf += "%s\n" % splashline | ||
| 122 | syslinux_conf += "DEFAULT boot\n" | ||
| 123 | syslinux_conf += "LABEL boot\n" | ||
| 124 | |||
| 125 | kernel = "/" + get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 126 | syslinux_conf += "KERNEL " + kernel + "\n" | ||
| 127 | |||
| 128 | syslinux_conf += "APPEND label=boot root=%s %s\n" % \ | ||
| 129 | (creator.rootdev, bootloader.append) | ||
| 130 | |||
| 131 | logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg", | ||
| 132 | cr_workdir) | ||
| 133 | cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w") | ||
| 134 | cfg.write(syslinux_conf) | ||
| 135 | cfg.close() | ||
| 136 | |||
| 137 | @classmethod | ||
| 138 | def do_prepare_partition(cls, part, source_params, creator, cr_workdir, | ||
| 139 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 140 | rootfs_dir, native_sysroot): | ||
| 141 | """ | ||
| 142 | Called to do the actual content population for a partition i.e. it | ||
| 143 | 'prepares' the partition to be incorporated into the image. | ||
| 144 | In this case, prepare content for legacy bios boot partition. | ||
| 145 | """ | ||
| 146 | bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') | ||
| 147 | |||
| 148 | staging_kernel_dir = kernel_dir | ||
| 149 | |||
| 150 | hdddir = "%s/hdd/boot" % cr_workdir | ||
| 151 | |||
| 152 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 153 | if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": | ||
| 154 | if get_bitbake_var("INITRAMFS_IMAGE"): | ||
| 155 | kernel = "%s-%s.bin" % \ | ||
| 156 | (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) | ||
| 157 | |||
| 158 | cmds = ("install -m 0644 %s/%s %s/%s" % | ||
| 159 | (staging_kernel_dir, kernel, hdddir, get_bitbake_var("KERNEL_IMAGETYPE")), | ||
| 160 | "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" % | ||
| 161 | (bootimg_dir, hdddir), | ||
| 162 | "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" % | ||
| 163 | (bootimg_dir, hdddir), | ||
| 164 | "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" % | ||
| 165 | (bootimg_dir, hdddir), | ||
| 166 | "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" % | ||
| 167 | (bootimg_dir, hdddir)) | ||
| 168 | |||
| 169 | for install_cmd in cmds: | ||
| 170 | exec_cmd(install_cmd) | ||
| 171 | |||
| 172 | du_cmd = "du -bks %s" % hdddir | ||
| 173 | out = exec_cmd(du_cmd) | ||
| 174 | blocks = int(out.split()[0]) | ||
| 175 | |||
| 176 | extra_blocks = part.get_extra_block_count(blocks) | ||
| 177 | |||
| 178 | if extra_blocks < BOOTDD_EXTRA_SPACE: | ||
| 179 | extra_blocks = BOOTDD_EXTRA_SPACE | ||
| 180 | |||
| 181 | blocks += extra_blocks | ||
| 182 | |||
| 183 | logger.debug("Added %d extra blocks to %s to get to %d total blocks", | ||
| 184 | extra_blocks, part.mountpoint, blocks) | ||
| 185 | |||
| 186 | # dosfs image, created by mkdosfs | ||
| 187 | bootimg = "%s/boot%s.img" % (cr_workdir, part.lineno) | ||
| 188 | |||
| 189 | label = part.label if part.label else "boot" | ||
| 190 | |||
| 191 | dosfs_cmd = "mkdosfs -n %s -i %s -S 512 -C %s %d" % \ | ||
| 192 | (label, part.fsuuid, bootimg, blocks) | ||
| 193 | exec_native_cmd(dosfs_cmd, native_sysroot) | ||
| 194 | |||
| 195 | mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) | ||
| 196 | exec_native_cmd(mcopy_cmd, native_sysroot) | ||
| 197 | |||
| 198 | syslinux_cmd = "syslinux %s" % bootimg | ||
| 199 | exec_native_cmd(syslinux_cmd, native_sysroot) | ||
| 200 | |||
| 201 | chmod_cmd = "chmod 644 %s" % bootimg | ||
| 202 | exec_cmd(chmod_cmd) | ||
| 203 | |||
| 204 | du_cmd = "du -Lbks %s" % bootimg | ||
| 205 | out = exec_cmd(du_cmd) | ||
| 206 | bootimg_size = out.split()[0] | ||
| 207 | |||
| 208 | part.size = int(bootimg_size) | ||
| 209 | part.source_file = bootimg | ||
diff --git a/scripts/lib/wic/plugins/source/empty.py b/scripts/lib/wic/plugins/source/empty.py deleted file mode 100644 index 4178912377..0000000000 --- a/scripts/lib/wic/plugins/source/empty.py +++ /dev/null | |||
| @@ -1,89 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright OpenEmbedded Contributors | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: MIT | ||
| 5 | # | ||
| 6 | |||
| 7 | # The empty wic plugin is used to create unformatted empty partitions for wic | ||
| 8 | # images. | ||
| 9 | # To use it you must pass "empty" as argument for the "--source" parameter in | ||
| 10 | # the wks file. For example: | ||
| 11 | # part foo --source empty --ondisk sda --size="1024" --align 1024 | ||
| 12 | # | ||
| 13 | # The plugin supports writing zeros to the start of the | ||
| 14 | # partition. This is useful to overwrite old content like | ||
| 15 | # filesystem signatures which may be re-recognized otherwise. | ||
| 16 | # This feature can be enabled with | ||
| 17 | # '--sourceparams="[fill|size=<N>[S|s|K|k|M|G]][,][bs=<N>[S|s|K|k|M|G]]"' | ||
| 18 | # Conflicting or missing options throw errors. | ||
| 19 | |||
| 20 | import logging | ||
| 21 | import os | ||
| 22 | |||
| 23 | from wic import WicError | ||
| 24 | from wic.ksparser import sizetype | ||
| 25 | from wic.pluginbase import SourcePlugin | ||
| 26 | |||
| 27 | logger = logging.getLogger('wic') | ||
| 28 | |||
| 29 | class EmptyPartitionPlugin(SourcePlugin): | ||
| 30 | """ | ||
| 31 | Populate unformatted empty partition. | ||
| 32 | |||
| 33 | The following sourceparams are supported: | ||
| 34 | - fill | ||
| 35 | Fill the entire partition with zeros. Requires '--fixed-size' option | ||
| 36 | to be set. | ||
| 37 | - size=<N>[S|s|K|k|M|G] | ||
| 38 | Set the first N bytes of the partition to zero. Default unit is 'K'. | ||
| 39 | - bs=<N>[S|s|K|k|M|G] | ||
| 40 | Write at most N bytes at a time during source file creation. | ||
| 41 | Defaults to '1M'. Default unit is 'K'. | ||
| 42 | """ | ||
| 43 | |||
| 44 | name = 'empty' | ||
| 45 | |||
| 46 | @classmethod | ||
| 47 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, | ||
| 48 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 49 | rootfs_dir, native_sysroot): | ||
| 50 | """ | ||
| 51 | Called to do the actual content population for a partition i.e. it | ||
| 52 | 'prepares' the partition to be incorporated into the image. | ||
| 53 | """ | ||
| 54 | get_byte_count = sizetype('K', True) | ||
| 55 | size = 0 | ||
| 56 | |||
| 57 | if 'fill' in source_params and 'size' in source_params: | ||
| 58 | raise WicError("Conflicting source parameters 'fill' and 'size' specified, exiting.") | ||
| 59 | |||
| 60 | # Set the size of the zeros to be written to the partition | ||
| 61 | if 'fill' in source_params: | ||
| 62 | if part.fixed_size == 0: | ||
| 63 | raise WicError("Source parameter 'fill' only works with the '--fixed-size' option, exiting.") | ||
| 64 | size = get_byte_count(part.fixed_size) | ||
| 65 | elif 'size' in source_params: | ||
| 66 | size = get_byte_count(source_params['size']) | ||
| 67 | |||
| 68 | if size == 0: | ||
| 69 | # Nothing to do, create empty partition | ||
| 70 | return | ||
| 71 | |||
| 72 | if 'bs' in source_params: | ||
| 73 | bs = get_byte_count(source_params['bs']) | ||
| 74 | else: | ||
| 75 | bs = get_byte_count('1M') | ||
| 76 | |||
| 77 | # Create a binary file of the requested size filled with zeros | ||
| 78 | source_file = os.path.join(cr_workdir, 'empty-plugin-zeros%s.bin' % part.lineno) | ||
| 79 | if not os.path.exists(os.path.dirname(source_file)): | ||
| 80 | os.makedirs(os.path.dirname(source_file)) | ||
| 81 | |||
| 82 | quotient, remainder = divmod(size, bs) | ||
| 83 | with open(source_file, 'wb') as file: | ||
| 84 | for _ in range(quotient): | ||
| 85 | file.write(bytearray(bs)) | ||
| 86 | file.write(bytearray(remainder)) | ||
| 87 | |||
| 88 | part.size = (size + 1024 - 1) // 1024 # size in KB rounded up | ||
| 89 | part.source_file = source_file | ||
diff --git a/scripts/lib/wic/plugins/source/isoimage-isohybrid.py b/scripts/lib/wic/plugins/source/isoimage-isohybrid.py deleted file mode 100644 index 607356ad13..0000000000 --- a/scripts/lib/wic/plugins/source/isoimage-isohybrid.py +++ /dev/null | |||
| @@ -1,463 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright OpenEmbedded Contributors | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | # DESCRIPTION | ||
| 7 | # This implements the 'isoimage-isohybrid' source plugin class for 'wic' | ||
| 8 | # | ||
| 9 | # AUTHORS | ||
| 10 | # Mihaly Varga <mihaly.varga (at] ni.com> | ||
| 11 | |||
| 12 | import glob | ||
| 13 | import logging | ||
| 14 | import os | ||
| 15 | import re | ||
| 16 | import shutil | ||
| 17 | |||
| 18 | from wic import WicError | ||
| 19 | from wic.engine import get_custom_config | ||
| 20 | from wic.pluginbase import SourcePlugin | ||
| 21 | from wic.misc import exec_cmd, exec_native_cmd, get_bitbake_var | ||
| 22 | |||
| 23 | logger = logging.getLogger('wic') | ||
| 24 | |||
| 25 | class IsoImagePlugin(SourcePlugin): | ||
| 26 | """ | ||
| 27 | Create a bootable ISO image | ||
| 28 | |||
| 29 | This plugin creates a hybrid, legacy and EFI bootable ISO image. The | ||
| 30 | generated image can be used on optical media as well as USB media. | ||
| 31 | |||
| 32 | Legacy boot uses syslinux and EFI boot uses grub or gummiboot (not | ||
| 33 | implemented yet) as bootloader. The plugin creates the directories required | ||
| 34 | by bootloaders and populates them by creating and configuring the | ||
| 35 | bootloader files. | ||
| 36 | |||
| 37 | Example kickstart file: | ||
| 38 | part /boot --source isoimage-isohybrid --sourceparams="loader=grub-efi, \\ | ||
| 39 | image_name= IsoImage" --ondisk cd --label LIVECD | ||
| 40 | bootloader --timeout=10 --append=" " | ||
| 41 | |||
| 42 | In --sourceparams "loader" specifies the bootloader used for booting in EFI | ||
| 43 | mode, while "image_name" specifies the name of the generated image. In the | ||
| 44 | example above, wic creates an ISO image named IsoImage-cd.direct (default | ||
| 45 | extension added by direct imeger plugin) and a file named IsoImage-cd.iso | ||
| 46 | """ | ||
| 47 | |||
| 48 | name = 'isoimage-isohybrid' | ||
| 49 | |||
| 50 | @classmethod | ||
| 51 | def do_configure_syslinux(cls, creator, cr_workdir): | ||
| 52 | """ | ||
| 53 | Create loader-specific (syslinux) config | ||
| 54 | """ | ||
| 55 | splash = os.path.join(cr_workdir, "ISO/boot/splash.jpg") | ||
| 56 | if os.path.exists(splash): | ||
| 57 | splashline = "menu background splash.jpg" | ||
| 58 | else: | ||
| 59 | splashline = "" | ||
| 60 | |||
| 61 | bootloader = creator.ks.bootloader | ||
| 62 | |||
| 63 | syslinux_conf = "" | ||
| 64 | syslinux_conf += "PROMPT 0\n" | ||
| 65 | syslinux_conf += "TIMEOUT %s \n" % (bootloader.timeout or 10) | ||
| 66 | syslinux_conf += "\n" | ||
| 67 | syslinux_conf += "ALLOWOPTIONS 1\n" | ||
| 68 | syslinux_conf += "SERIAL 0 115200\n" | ||
| 69 | syslinux_conf += "\n" | ||
| 70 | if splashline: | ||
| 71 | syslinux_conf += "%s\n" % splashline | ||
| 72 | syslinux_conf += "DEFAULT boot\n" | ||
| 73 | syslinux_conf += "LABEL boot\n" | ||
| 74 | |||
| 75 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 76 | if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": | ||
| 77 | if get_bitbake_var("INITRAMFS_IMAGE"): | ||
| 78 | kernel = "%s-%s.bin" % \ | ||
| 79 | (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) | ||
| 80 | |||
| 81 | syslinux_conf += "KERNEL /" + kernel + "\n" | ||
| 82 | syslinux_conf += "APPEND initrd=/initrd LABEL=boot %s\n" \ | ||
| 83 | % bootloader.append | ||
| 84 | |||
| 85 | logger.debug("Writing syslinux config %s/ISO/isolinux/isolinux.cfg", | ||
| 86 | cr_workdir) | ||
| 87 | |||
| 88 | with open("%s/ISO/isolinux/isolinux.cfg" % cr_workdir, "w") as cfg: | ||
| 89 | cfg.write(syslinux_conf) | ||
| 90 | |||
| 91 | @classmethod | ||
| 92 | def do_configure_grubefi(cls, part, creator, target_dir): | ||
| 93 | """ | ||
| 94 | Create loader-specific (grub-efi) config | ||
| 95 | """ | ||
| 96 | configfile = creator.ks.bootloader.configfile | ||
| 97 | if configfile: | ||
| 98 | grubefi_conf = get_custom_config(configfile) | ||
| 99 | if grubefi_conf: | ||
| 100 | logger.debug("Using custom configuration file %s for grub.cfg", | ||
| 101 | configfile) | ||
| 102 | else: | ||
| 103 | raise WicError("configfile is specified " | ||
| 104 | "but failed to get it from %s", configfile) | ||
| 105 | else: | ||
| 106 | splash = os.path.join(target_dir, "splash.jpg") | ||
| 107 | if os.path.exists(splash): | ||
| 108 | splashline = "menu background splash.jpg" | ||
| 109 | else: | ||
| 110 | splashline = "" | ||
| 111 | |||
| 112 | bootloader = creator.ks.bootloader | ||
| 113 | |||
| 114 | grubefi_conf = "" | ||
| 115 | grubefi_conf += "serial --unit=0 --speed=115200 --word=8 " | ||
| 116 | grubefi_conf += "--parity=no --stop=1\n" | ||
| 117 | grubefi_conf += "default=boot\n" | ||
| 118 | grubefi_conf += "timeout=%s\n" % (bootloader.timeout or 10) | ||
| 119 | grubefi_conf += "\n" | ||
| 120 | grubefi_conf += "search --set=root --label %s " % part.label | ||
| 121 | grubefi_conf += "\n" | ||
| 122 | grubefi_conf += "menuentry 'boot'{\n" | ||
| 123 | |||
| 124 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 125 | if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": | ||
| 126 | if get_bitbake_var("INITRAMFS_IMAGE"): | ||
| 127 | kernel = "%s-%s.bin" % \ | ||
| 128 | (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) | ||
| 129 | |||
| 130 | grubefi_conf += "linux /%s rootwait %s\n" \ | ||
| 131 | % (kernel, bootloader.append) | ||
| 132 | grubefi_conf += "initrd /initrd \n" | ||
| 133 | grubefi_conf += "}\n" | ||
| 134 | |||
| 135 | if splashline: | ||
| 136 | grubefi_conf += "%s\n" % splashline | ||
| 137 | |||
| 138 | cfg_path = os.path.join(target_dir, "grub.cfg") | ||
| 139 | logger.debug("Writing grubefi config %s", cfg_path) | ||
| 140 | |||
| 141 | with open(cfg_path, "w") as cfg: | ||
| 142 | cfg.write(grubefi_conf) | ||
| 143 | |||
| 144 | @staticmethod | ||
| 145 | def _build_initramfs_path(rootfs_dir, cr_workdir): | ||
| 146 | """ | ||
| 147 | Create path for initramfs image | ||
| 148 | """ | ||
| 149 | |||
| 150 | initrd = get_bitbake_var("INITRD_LIVE") or get_bitbake_var("INITRD") | ||
| 151 | if not initrd: | ||
| 152 | initrd_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") | ||
| 153 | if not initrd_dir: | ||
| 154 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting.") | ||
| 155 | |||
| 156 | image_name = get_bitbake_var("IMAGE_BASENAME") | ||
| 157 | if not image_name: | ||
| 158 | raise WicError("Couldn't find IMAGE_BASENAME, exiting.") | ||
| 159 | |||
| 160 | image_type = get_bitbake_var("INITRAMFS_FSTYPES") | ||
| 161 | if not image_type: | ||
| 162 | raise WicError("Couldn't find INITRAMFS_FSTYPES, exiting.") | ||
| 163 | |||
| 164 | machine = os.path.basename(initrd_dir) | ||
| 165 | |||
| 166 | pattern = '%s/%s*%s.%s' % (initrd_dir, image_name, machine, image_type) | ||
| 167 | files = glob.glob(pattern) | ||
| 168 | if files: | ||
| 169 | initrd = files[0] | ||
| 170 | |||
| 171 | if not initrd or not os.path.exists(initrd): | ||
| 172 | # Create initrd from rootfs directory | ||
| 173 | initrd = "%s/initrd.cpio.gz" % cr_workdir | ||
| 174 | initrd_dir = "%s/INITRD" % cr_workdir | ||
| 175 | shutil.copytree("%s" % rootfs_dir, \ | ||
| 176 | "%s" % initrd_dir, symlinks=True) | ||
| 177 | |||
| 178 | if os.path.isfile("%s/init" % rootfs_dir): | ||
| 179 | shutil.copy2("%s/init" % rootfs_dir, "%s/init" % initrd_dir) | ||
| 180 | elif os.path.lexists("%s/init" % rootfs_dir): | ||
| 181 | os.symlink(os.readlink("%s/init" % rootfs_dir), \ | ||
| 182 | "%s/init" % initrd_dir) | ||
| 183 | elif os.path.isfile("%s/sbin/init" % rootfs_dir): | ||
| 184 | shutil.copy2("%s/sbin/init" % rootfs_dir, \ | ||
| 185 | "%s" % initrd_dir) | ||
| 186 | elif os.path.lexists("%s/sbin/init" % rootfs_dir): | ||
| 187 | os.symlink(os.readlink("%s/sbin/init" % rootfs_dir), \ | ||
| 188 | "%s/init" % initrd_dir) | ||
| 189 | else: | ||
| 190 | raise WicError("Couldn't find or build initrd, exiting.") | ||
| 191 | |||
| 192 | exec_cmd("cd %s && find . | cpio -o -H newc -R root:root >%s/initrd.cpio " \ | ||
| 193 | % (initrd_dir, cr_workdir), as_shell=True) | ||
| 194 | exec_cmd("gzip -f -9 %s/initrd.cpio" % cr_workdir, as_shell=True) | ||
| 195 | shutil.rmtree(initrd_dir) | ||
| 196 | |||
| 197 | return initrd | ||
| 198 | |||
| 199 | @classmethod | ||
| 200 | def do_configure_partition(cls, part, source_params, creator, cr_workdir, | ||
| 201 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 202 | native_sysroot): | ||
| 203 | """ | ||
| 204 | Called before do_prepare_partition(), creates loader-specific config | ||
| 205 | """ | ||
| 206 | isodir = "%s/ISO/" % cr_workdir | ||
| 207 | |||
| 208 | if os.path.exists(isodir): | ||
| 209 | shutil.rmtree(isodir) | ||
| 210 | |||
| 211 | install_cmd = "install -d %s " % isodir | ||
| 212 | exec_cmd(install_cmd) | ||
| 213 | |||
| 214 | # Overwrite the name of the created image | ||
| 215 | logger.debug(source_params) | ||
| 216 | if 'image_name' in source_params and \ | ||
| 217 | source_params['image_name'].strip(): | ||
| 218 | creator.name = source_params['image_name'].strip() | ||
| 219 | logger.debug("The name of the image is: %s", creator.name) | ||
| 220 | |||
| 221 | @staticmethod | ||
| 222 | def _install_payload(source_params, iso_dir): | ||
| 223 | """ | ||
| 224 | Copies contents of payload directory (as specified in 'payload_dir' param) into iso_dir | ||
| 225 | """ | ||
| 226 | |||
| 227 | if source_params.get('payload_dir'): | ||
| 228 | payload_dir = source_params['payload_dir'] | ||
| 229 | |||
| 230 | logger.debug("Payload directory: %s", payload_dir) | ||
| 231 | shutil.copytree(payload_dir, iso_dir, symlinks=True, dirs_exist_ok=True) | ||
| 232 | |||
| 233 | @classmethod | ||
| 234 | def do_prepare_partition(cls, part, source_params, creator, cr_workdir, | ||
| 235 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 236 | rootfs_dir, native_sysroot): | ||
| 237 | """ | ||
| 238 | Called to do the actual content population for a partition i.e. it | ||
| 239 | 'prepares' the partition to be incorporated into the image. | ||
| 240 | In this case, prepare content for a bootable ISO image. | ||
| 241 | """ | ||
| 242 | |||
| 243 | isodir = "%s/ISO" % cr_workdir | ||
| 244 | |||
| 245 | cls._install_payload(source_params, isodir) | ||
| 246 | |||
| 247 | if part.rootfs_dir is None: | ||
| 248 | if not 'ROOTFS_DIR' in rootfs_dir: | ||
| 249 | raise WicError("Couldn't find --rootfs-dir, exiting.") | ||
| 250 | rootfs_dir = rootfs_dir['ROOTFS_DIR'] | ||
| 251 | else: | ||
| 252 | if part.rootfs_dir in rootfs_dir: | ||
| 253 | rootfs_dir = rootfs_dir[part.rootfs_dir] | ||
| 254 | elif part.rootfs_dir: | ||
| 255 | rootfs_dir = part.rootfs_dir | ||
| 256 | else: | ||
| 257 | raise WicError("Couldn't find --rootfs-dir=%s connection " | ||
| 258 | "or it is not a valid path, exiting." % | ||
| 259 | part.rootfs_dir) | ||
| 260 | |||
| 261 | if not os.path.isdir(rootfs_dir): | ||
| 262 | rootfs_dir = get_bitbake_var("IMAGE_ROOTFS") | ||
| 263 | if not os.path.isdir(rootfs_dir): | ||
| 264 | raise WicError("Couldn't find IMAGE_ROOTFS, exiting.") | ||
| 265 | |||
| 266 | part.rootfs_dir = rootfs_dir | ||
| 267 | deploy_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") | ||
| 268 | img_iso_dir = get_bitbake_var("ISODIR") | ||
| 269 | |||
| 270 | # Remove the temporary file created by part.prepare_rootfs() | ||
| 271 | if os.path.isfile(part.source_file): | ||
| 272 | os.remove(part.source_file) | ||
| 273 | |||
| 274 | # Support using a different initrd other than default | ||
| 275 | if source_params.get('initrd'): | ||
| 276 | initrd = source_params['initrd'] | ||
| 277 | if not deploy_dir: | ||
| 278 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") | ||
| 279 | cp_cmd = "cp %s/%s %s" % (deploy_dir, initrd, cr_workdir) | ||
| 280 | exec_cmd(cp_cmd) | ||
| 281 | else: | ||
| 282 | # Prepare initial ramdisk | ||
| 283 | initrd = "%s/initrd" % deploy_dir | ||
| 284 | if not os.path.isfile(initrd): | ||
| 285 | initrd = "%s/initrd" % img_iso_dir | ||
| 286 | if not os.path.isfile(initrd): | ||
| 287 | initrd = cls._build_initramfs_path(rootfs_dir, cr_workdir) | ||
| 288 | |||
| 289 | install_cmd = "install -m 0644 %s %s/initrd" % (initrd, isodir) | ||
| 290 | exec_cmd(install_cmd) | ||
| 291 | |||
| 292 | # Remove the temporary file created by _build_initramfs_path function | ||
| 293 | if os.path.isfile("%s/initrd.cpio.gz" % cr_workdir): | ||
| 294 | os.remove("%s/initrd.cpio.gz" % cr_workdir) | ||
| 295 | |||
| 296 | kernel = get_bitbake_var("KERNEL_IMAGETYPE") | ||
| 297 | if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": | ||
| 298 | if get_bitbake_var("INITRAMFS_IMAGE"): | ||
| 299 | kernel = "%s-%s.bin" % \ | ||
| 300 | (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) | ||
| 301 | |||
| 302 | install_cmd = "install -m 0644 %s/%s %s/%s" % \ | ||
| 303 | (kernel_dir, kernel, isodir, kernel) | ||
| 304 | exec_cmd(install_cmd) | ||
| 305 | |||
| 306 | #Create bootloader for efi boot | ||
| 307 | try: | ||
| 308 | target_dir = "%s/EFI/BOOT" % isodir | ||
| 309 | if os.path.exists(target_dir): | ||
| 310 | shutil.rmtree(target_dir) | ||
| 311 | |||
| 312 | os.makedirs(target_dir) | ||
| 313 | |||
| 314 | if source_params['loader'] == 'grub-efi': | ||
| 315 | # Builds bootx64.efi/bootia32.efi if ISODIR didn't exist or | ||
| 316 | # didn't contains it | ||
| 317 | target_arch = get_bitbake_var("TARGET_SYS") | ||
| 318 | if not target_arch: | ||
| 319 | raise WicError("Coludn't find target architecture") | ||
| 320 | |||
| 321 | if re.match("x86_64", target_arch): | ||
| 322 | grub_src_image = "grub-efi-bootx64.efi" | ||
| 323 | grub_dest_image = "bootx64.efi" | ||
| 324 | elif re.match('i.86', target_arch): | ||
| 325 | grub_src_image = "grub-efi-bootia32.efi" | ||
| 326 | grub_dest_image = "bootia32.efi" | ||
| 327 | else: | ||
| 328 | raise WicError("grub-efi is incompatible with target %s" % | ||
| 329 | target_arch) | ||
| 330 | |||
| 331 | grub_target = os.path.join(target_dir, grub_dest_image) | ||
| 332 | if not os.path.isfile(grub_target): | ||
| 333 | grub_src = os.path.join(deploy_dir, grub_src_image) | ||
| 334 | if not os.path.exists(grub_src): | ||
| 335 | raise WicError("Grub loader %s is not found in %s. " | ||
| 336 | "Please build grub-efi first" % (grub_src_image, deploy_dir)) | ||
| 337 | shutil.copy(grub_src, grub_target) | ||
| 338 | |||
| 339 | if not os.path.isfile(os.path.join(target_dir, "boot.cfg")): | ||
| 340 | cls.do_configure_grubefi(part, creator, target_dir) | ||
| 341 | |||
| 342 | else: | ||
| 343 | raise WicError("unrecognized bootimg-efi loader: %s" % | ||
| 344 | source_params['loader']) | ||
| 345 | except KeyError: | ||
| 346 | raise WicError("bootimg-efi requires a loader, none specified") | ||
| 347 | |||
| 348 | # Create efi.img that contains bootloader files for EFI booting | ||
| 349 | # if ISODIR didn't exist or didn't contains it | ||
| 350 | if os.path.isfile("%s/efi.img" % img_iso_dir): | ||
| 351 | install_cmd = "install -m 0644 %s/efi.img %s/efi.img" % \ | ||
| 352 | (img_iso_dir, isodir) | ||
| 353 | exec_cmd(install_cmd) | ||
| 354 | else: | ||
| 355 | # Default to 100 blocks of extra space for file system overhead | ||
| 356 | esp_extra_blocks = int(source_params.get('esp_extra_blocks', '100')) | ||
| 357 | |||
| 358 | du_cmd = "du -bks %s/EFI" % isodir | ||
| 359 | out = exec_cmd(du_cmd) | ||
| 360 | blocks = int(out.split()[0]) | ||
| 361 | blocks += esp_extra_blocks | ||
| 362 | logger.debug("Added 100 extra blocks to %s to get to %d " | ||
| 363 | "total blocks", part.mountpoint, blocks) | ||
| 364 | |||
| 365 | # dosfs image for EFI boot | ||
| 366 | bootimg = "%s/efi.img" % isodir | ||
| 367 | |||
| 368 | esp_label = source_params.get('esp_label', 'EFIimg') | ||
| 369 | |||
| 370 | dosfs_cmd = 'mkfs.vfat -n \'%s\' -S 512 -C %s %d' \ | ||
| 371 | % (esp_label, bootimg, blocks) | ||
| 372 | exec_native_cmd(dosfs_cmd, native_sysroot) | ||
| 373 | |||
| 374 | mmd_cmd = "mmd -i %s ::/EFI" % bootimg | ||
| 375 | exec_native_cmd(mmd_cmd, native_sysroot) | ||
| 376 | |||
| 377 | mcopy_cmd = "mcopy -i %s -s %s/EFI/* ::/EFI/" \ | ||
| 378 | % (bootimg, isodir) | ||
| 379 | exec_native_cmd(mcopy_cmd, native_sysroot) | ||
| 380 | |||
| 381 | chmod_cmd = "chmod 644 %s" % bootimg | ||
| 382 | exec_cmd(chmod_cmd) | ||
| 383 | |||
| 384 | # Prepare files for legacy boot | ||
| 385 | syslinux_dir = get_bitbake_var("STAGING_DATADIR") | ||
| 386 | if not syslinux_dir: | ||
| 387 | raise WicError("Couldn't find STAGING_DATADIR, exiting.") | ||
| 388 | |||
| 389 | if os.path.exists("%s/isolinux" % isodir): | ||
| 390 | shutil.rmtree("%s/isolinux" % isodir) | ||
| 391 | |||
| 392 | install_cmd = "install -d %s/isolinux" % isodir | ||
| 393 | exec_cmd(install_cmd) | ||
| 394 | |||
| 395 | cls.do_configure_syslinux(creator, cr_workdir) | ||
| 396 | |||
| 397 | install_cmd = "install -m 444 %s/syslinux/ldlinux.sys " % syslinux_dir | ||
| 398 | install_cmd += "%s/isolinux/ldlinux.sys" % isodir | ||
| 399 | exec_cmd(install_cmd) | ||
| 400 | |||
| 401 | install_cmd = "install -m 444 %s/syslinux/isohdpfx.bin " % syslinux_dir | ||
| 402 | install_cmd += "%s/isolinux/isohdpfx.bin" % isodir | ||
| 403 | exec_cmd(install_cmd) | ||
| 404 | |||
| 405 | install_cmd = "install -m 644 %s/syslinux/isolinux.bin " % syslinux_dir | ||
| 406 | install_cmd += "%s/isolinux/isolinux.bin" % isodir | ||
| 407 | exec_cmd(install_cmd) | ||
| 408 | |||
| 409 | install_cmd = "install -m 644 %s/syslinux/ldlinux.c32 " % syslinux_dir | ||
| 410 | install_cmd += "%s/isolinux/ldlinux.c32" % isodir | ||
| 411 | exec_cmd(install_cmd) | ||
| 412 | |||
| 413 | #create ISO image | ||
| 414 | iso_img = "%s/tempiso_img.iso" % cr_workdir | ||
| 415 | iso_bootimg = "isolinux/isolinux.bin" | ||
| 416 | iso_bootcat = "isolinux/boot.cat" | ||
| 417 | efi_img = "efi.img" | ||
| 418 | |||
| 419 | mkisofs_cmd = "mkisofs -V %s " % part.label | ||
| 420 | mkisofs_cmd += "-o %s -U " % iso_img | ||
| 421 | mkisofs_cmd += "-J -joliet-long -r -iso-level 2 -b %s " % iso_bootimg | ||
| 422 | mkisofs_cmd += "-c %s -no-emul-boot -boot-load-size 4 " % iso_bootcat | ||
| 423 | mkisofs_cmd += "-boot-info-table -eltorito-alt-boot " | ||
| 424 | mkisofs_cmd += "-eltorito-platform 0xEF -eltorito-boot %s " % efi_img | ||
| 425 | mkisofs_cmd += "-no-emul-boot %s " % isodir | ||
| 426 | |||
| 427 | logger.debug("running command: %s", mkisofs_cmd) | ||
| 428 | exec_native_cmd(mkisofs_cmd, native_sysroot) | ||
| 429 | |||
| 430 | shutil.rmtree(isodir) | ||
| 431 | |||
| 432 | du_cmd = "du -Lbks %s" % iso_img | ||
| 433 | out = exec_cmd(du_cmd) | ||
| 434 | isoimg_size = int(out.split()[0]) | ||
| 435 | |||
| 436 | part.size = isoimg_size | ||
| 437 | part.source_file = iso_img | ||
| 438 | |||
| 439 | @classmethod | ||
| 440 | def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, | ||
| 441 | bootimg_dir, kernel_dir, native_sysroot): | ||
| 442 | """ | ||
| 443 | Called after all partitions have been prepared and assembled into a | ||
| 444 | disk image. In this case, we insert/modify the MBR using isohybrid | ||
| 445 | utility for booting via BIOS from disk storage devices. | ||
| 446 | """ | ||
| 447 | |||
| 448 | iso_img = "%s.p1" % disk.path | ||
| 449 | full_path = creator._full_path(workdir, disk_name, "direct") | ||
| 450 | full_path_iso = creator._full_path(workdir, disk_name, "iso") | ||
| 451 | |||
| 452 | isohybrid_cmd = "isohybrid -u %s" % iso_img | ||
| 453 | logger.debug("running command: %s", isohybrid_cmd) | ||
| 454 | exec_native_cmd(isohybrid_cmd, native_sysroot) | ||
| 455 | |||
| 456 | # Replace the image created by direct plugin with the one created by | ||
| 457 | # mkisofs command. This is necessary because the iso image created by | ||
| 458 | # mkisofs has a very specific MBR is system area of the ISO image, and | ||
| 459 | # direct plugin adds and configures an another MBR. | ||
| 460 | logger.debug("Replaceing the image created by direct plugin\n") | ||
| 461 | os.remove(disk.path) | ||
| 462 | shutil.copy2(iso_img, full_path_iso) | ||
| 463 | shutil.copy2(full_path_iso, full_path) | ||
diff --git a/scripts/lib/wic/plugins/source/rawcopy.py b/scripts/lib/wic/plugins/source/rawcopy.py deleted file mode 100644 index 21903c2f23..0000000000 --- a/scripts/lib/wic/plugins/source/rawcopy.py +++ /dev/null | |||
| @@ -1,115 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright OpenEmbedded Contributors | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | |||
| 7 | import logging | ||
| 8 | import os | ||
| 9 | import signal | ||
| 10 | import subprocess | ||
| 11 | |||
| 12 | from wic import WicError | ||
| 13 | from wic.pluginbase import SourcePlugin | ||
| 14 | from wic.misc import exec_cmd, get_bitbake_var | ||
| 15 | from wic.filemap import sparse_copy | ||
| 16 | |||
| 17 | logger = logging.getLogger('wic') | ||
| 18 | |||
| 19 | class RawCopyPlugin(SourcePlugin): | ||
| 20 | """ | ||
| 21 | Populate partition content from raw image file. | ||
| 22 | """ | ||
| 23 | |||
| 24 | name = 'rawcopy' | ||
| 25 | |||
| 26 | @staticmethod | ||
| 27 | def do_image_label(fstype, dst, label): | ||
| 28 | # don't create label when fstype is none | ||
| 29 | if fstype == 'none': | ||
| 30 | return | ||
| 31 | |||
| 32 | if fstype.startswith('ext'): | ||
| 33 | cmd = 'tune2fs -L %s %s' % (label, dst) | ||
| 34 | elif fstype in ('msdos', 'vfat'): | ||
| 35 | cmd = 'dosfslabel %s %s' % (dst, label) | ||
| 36 | elif fstype == 'btrfs': | ||
| 37 | cmd = 'btrfs filesystem label %s %s' % (dst, label) | ||
| 38 | elif fstype == 'swap': | ||
| 39 | cmd = 'mkswap -L %s %s' % (label, dst) | ||
| 40 | elif fstype in ('squashfs', 'erofs'): | ||
| 41 | raise WicError("It's not possible to update a %s " | ||
| 42 | "filesystem label '%s'" % (fstype, label)) | ||
| 43 | else: | ||
| 44 | raise WicError("Cannot update filesystem label: " | ||
| 45 | "Unknown fstype: '%s'" % (fstype)) | ||
| 46 | |||
| 47 | exec_cmd(cmd) | ||
| 48 | |||
| 49 | @staticmethod | ||
| 50 | def do_image_uncompression(src, dst, workdir): | ||
| 51 | def subprocess_setup(): | ||
| 52 | # Python installs a SIGPIPE handler by default. This is usually not what | ||
| 53 | # non-Python subprocesses expect. | ||
| 54 | # SIGPIPE errors are known issues with gzip/bash | ||
| 55 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) | ||
| 56 | |||
| 57 | extension = os.path.splitext(src)[1] | ||
| 58 | decompressor = { | ||
| 59 | ".bz2": "bzip2", | ||
| 60 | ".gz": "gzip", | ||
| 61 | ".xz": "xz", | ||
| 62 | ".zst": "zstd -f", | ||
| 63 | }.get(extension) | ||
| 64 | if not decompressor: | ||
| 65 | raise WicError("Not supported compressor filename extension: %s" % extension) | ||
| 66 | cmd = "%s -dc %s > %s" % (decompressor, src, dst) | ||
| 67 | subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=workdir) | ||
| 68 | |||
| 69 | @classmethod | ||
| 70 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, | ||
| 71 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 72 | rootfs_dir, native_sysroot): | ||
| 73 | """ | ||
| 74 | Called to do the actual content population for a partition i.e. it | ||
| 75 | 'prepares' the partition to be incorporated into the image. | ||
| 76 | """ | ||
| 77 | if not kernel_dir: | ||
| 78 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") | ||
| 79 | if not kernel_dir: | ||
| 80 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") | ||
| 81 | |||
| 82 | logger.debug('Kernel dir: %s', kernel_dir) | ||
| 83 | |||
| 84 | if 'file' not in source_params: | ||
| 85 | raise WicError("No file specified") | ||
| 86 | |||
| 87 | if 'unpack' in source_params: | ||
| 88 | img = os.path.join(kernel_dir, source_params['file']) | ||
| 89 | src = os.path.join(cr_workdir, os.path.splitext(source_params['file'])[0]) | ||
| 90 | RawCopyPlugin.do_image_uncompression(img, src, cr_workdir) | ||
| 91 | else: | ||
| 92 | src = os.path.join(kernel_dir, source_params['file']) | ||
| 93 | |||
| 94 | dst = os.path.join(cr_workdir, "%s.%s" % (os.path.basename(source_params['file']), part.lineno)) | ||
| 95 | |||
| 96 | if not os.path.exists(os.path.dirname(dst)): | ||
| 97 | os.makedirs(os.path.dirname(dst)) | ||
| 98 | |||
| 99 | if 'skip' in source_params: | ||
| 100 | sparse_copy(src, dst, skip=int(source_params['skip'])) | ||
| 101 | else: | ||
| 102 | sparse_copy(src, dst) | ||
| 103 | |||
| 104 | # get the size in the right units for kickstart (kB) | ||
| 105 | du_cmd = "du -Lbks %s" % dst | ||
| 106 | out = exec_cmd(du_cmd) | ||
| 107 | filesize = int(out.split()[0]) | ||
| 108 | |||
| 109 | if filesize > part.size: | ||
| 110 | part.size = filesize | ||
| 111 | |||
| 112 | if part.label: | ||
| 113 | RawCopyPlugin.do_image_label(part.fstype, dst, part.label) | ||
| 114 | |||
| 115 | part.source_file = dst | ||
diff --git a/scripts/lib/wic/plugins/source/rootfs.py b/scripts/lib/wic/plugins/source/rootfs.py deleted file mode 100644 index 06fce06bb1..0000000000 --- a/scripts/lib/wic/plugins/source/rootfs.py +++ /dev/null | |||
| @@ -1,236 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright (c) 2014, Intel Corporation. | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | # DESCRIPTION | ||
| 7 | # This implements the 'rootfs' source plugin class for 'wic' | ||
| 8 | # | ||
| 9 | # AUTHORS | ||
| 10 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> | ||
| 11 | # Joao Henrique Ferreira de Freitas <joaohf (at] gmail.com> | ||
| 12 | # | ||
| 13 | |||
| 14 | import logging | ||
| 15 | import os | ||
| 16 | import shutil | ||
| 17 | import sys | ||
| 18 | |||
| 19 | from oe.path import copyhardlinktree | ||
| 20 | from pathlib import Path | ||
| 21 | |||
| 22 | from wic import WicError | ||
| 23 | from wic.pluginbase import SourcePlugin | ||
| 24 | from wic.misc import get_bitbake_var, exec_native_cmd | ||
| 25 | |||
| 26 | logger = logging.getLogger('wic') | ||
| 27 | |||
| 28 | class RootfsPlugin(SourcePlugin): | ||
| 29 | """ | ||
| 30 | Populate partition content from a rootfs directory. | ||
| 31 | """ | ||
| 32 | |||
| 33 | name = 'rootfs' | ||
| 34 | |||
| 35 | @staticmethod | ||
| 36 | def __validate_path(cmd, rootfs_dir, path): | ||
| 37 | if os.path.isabs(path): | ||
| 38 | logger.error("%s: Must be relative: %s" % (cmd, path)) | ||
| 39 | sys.exit(1) | ||
| 40 | |||
| 41 | # Disallow climbing outside of parent directory using '..', | ||
| 42 | # because doing so could be quite disastrous (we will delete the | ||
| 43 | # directory, or modify a directory outside OpenEmbedded). | ||
| 44 | full_path = os.path.abspath(os.path.join(rootfs_dir, path)) | ||
| 45 | if not full_path.startswith(os.path.realpath(rootfs_dir)): | ||
| 46 | logger.error("%s: Must point inside the rootfs: %s" % (cmd, path)) | ||
| 47 | sys.exit(1) | ||
| 48 | |||
| 49 | return full_path | ||
| 50 | |||
| 51 | @staticmethod | ||
| 52 | def __get_rootfs_dir(rootfs_dir): | ||
| 53 | if rootfs_dir and os.path.isdir(rootfs_dir): | ||
| 54 | return os.path.realpath(rootfs_dir) | ||
| 55 | |||
| 56 | image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir) | ||
| 57 | if not os.path.isdir(image_rootfs_dir): | ||
| 58 | raise WicError("No valid artifact IMAGE_ROOTFS from image " | ||
| 59 | "named %s has been found at %s, exiting." % | ||
| 60 | (rootfs_dir, image_rootfs_dir)) | ||
| 61 | |||
| 62 | return os.path.realpath(image_rootfs_dir) | ||
| 63 | |||
| 64 | @staticmethod | ||
| 65 | def __get_pseudo(native_sysroot, rootfs, pseudo_dir): | ||
| 66 | pseudo = "export PSEUDO_PREFIX=%s/usr;" % native_sysroot | ||
| 67 | pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir | ||
| 68 | pseudo += "export PSEUDO_PASSWD=%s;" % rootfs | ||
| 69 | pseudo += "export PSEUDO_NOSYMLINKEXP=1;" | ||
| 70 | pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") | ||
| 71 | return pseudo | ||
| 72 | |||
| 73 | @classmethod | ||
| 74 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, | ||
| 75 | oe_builddir, bootimg_dir, kernel_dir, | ||
| 76 | krootfs_dir, native_sysroot): | ||
| 77 | """ | ||
| 78 | Called to do the actual content population for a partition i.e. it | ||
| 79 | 'prepares' the partition to be incorporated into the image. | ||
| 80 | In this case, prepare content for legacy bios boot partition. | ||
| 81 | """ | ||
| 82 | if part.rootfs_dir is None: | ||
| 83 | if not 'ROOTFS_DIR' in krootfs_dir: | ||
| 84 | raise WicError("Couldn't find --rootfs-dir, exiting") | ||
| 85 | |||
| 86 | rootfs_dir = krootfs_dir['ROOTFS_DIR'] | ||
| 87 | else: | ||
| 88 | if part.rootfs_dir in krootfs_dir: | ||
| 89 | rootfs_dir = krootfs_dir[part.rootfs_dir] | ||
| 90 | elif part.rootfs_dir: | ||
| 91 | rootfs_dir = part.rootfs_dir | ||
| 92 | else: | ||
| 93 | raise WicError("Couldn't find --rootfs-dir=%s connection or " | ||
| 94 | "it is not a valid path, exiting" % part.rootfs_dir) | ||
| 95 | |||
| 96 | part.rootfs_dir = cls.__get_rootfs_dir(rootfs_dir) | ||
| 97 | part.has_fstab = os.path.exists(os.path.join(part.rootfs_dir, "etc/fstab")) | ||
| 98 | pseudo_dir = os.path.join(part.rootfs_dir, "../pseudo") | ||
| 99 | if not os.path.lexists(pseudo_dir): | ||
| 100 | pseudo_dir = os.path.join(cls.__get_rootfs_dir(None), '../pseudo') | ||
| 101 | |||
| 102 | if not os.path.lexists(pseudo_dir): | ||
| 103 | logger.warn("%s folder does not exist. " | ||
| 104 | "Usernames and permissions will be invalid " % pseudo_dir) | ||
| 105 | pseudo_dir = None | ||
| 106 | |||
| 107 | new_rootfs = None | ||
| 108 | new_pseudo = None | ||
| 109 | # Handle excluded paths. | ||
| 110 | if part.exclude_path or part.include_path or part.change_directory or part.update_fstab_in_rootfs: | ||
| 111 | # We need a new rootfs directory we can safely modify without | ||
| 112 | # interfering with other tasks. Copy to workdir. | ||
| 113 | new_rootfs = os.path.realpath(os.path.join(cr_workdir, "rootfs%d" % part.lineno)) | ||
| 114 | |||
| 115 | if os.path.lexists(new_rootfs): | ||
| 116 | shutil.rmtree(os.path.join(new_rootfs)) | ||
| 117 | |||
| 118 | if part.change_directory: | ||
| 119 | cd = part.change_directory | ||
| 120 | if cd[-1] == '/': | ||
| 121 | cd = cd[:-1] | ||
| 122 | orig_dir = cls.__validate_path("--change-directory", part.rootfs_dir, cd) | ||
| 123 | else: | ||
| 124 | orig_dir = part.rootfs_dir | ||
| 125 | copyhardlinktree(orig_dir, new_rootfs) | ||
| 126 | |||
| 127 | # Convert the pseudo directory to its new location | ||
| 128 | if (pseudo_dir): | ||
| 129 | new_pseudo = os.path.realpath( | ||
| 130 | os.path.join(cr_workdir, "pseudo%d" % part.lineno)) | ||
| 131 | if os.path.lexists(new_pseudo): | ||
| 132 | shutil.rmtree(new_pseudo) | ||
| 133 | os.mkdir(new_pseudo) | ||
| 134 | shutil.copy(os.path.join(pseudo_dir, "files.db"), | ||
| 135 | os.path.join(new_pseudo, "files.db")) | ||
| 136 | |||
| 137 | pseudo_cmd = "%s -B -m %s -M %s" % (cls.__get_pseudo(native_sysroot, | ||
| 138 | new_rootfs, | ||
| 139 | new_pseudo), | ||
| 140 | orig_dir, new_rootfs) | ||
| 141 | exec_native_cmd(pseudo_cmd, native_sysroot) | ||
| 142 | |||
| 143 | for in_path in part.include_path or []: | ||
| 144 | #parse arguments | ||
| 145 | include_path = in_path[0] | ||
| 146 | if len(in_path) > 2: | ||
| 147 | logger.error("'Invalid number of arguments for include-path") | ||
| 148 | sys.exit(1) | ||
| 149 | if len(in_path) == 2: | ||
| 150 | path = in_path[1] | ||
| 151 | else: | ||
| 152 | path = None | ||
| 153 | |||
| 154 | # Pack files to be included into a tar file. | ||
| 155 | # We need to create a tar file, because that way we can keep the | ||
| 156 | # permissions from the files even when they belong to different | ||
| 157 | # pseudo enviroments. | ||
| 158 | # If we simply copy files using copyhardlinktree/copytree... the | ||
| 159 | # copied files will belong to the user running wic. | ||
| 160 | tar_file = os.path.realpath( | ||
| 161 | os.path.join(cr_workdir, "include-path%d.tar" % part.lineno)) | ||
| 162 | if os.path.isfile(include_path): | ||
| 163 | parent = os.path.dirname(os.path.realpath(include_path)) | ||
| 164 | tar_cmd = "tar c --owner=root --group=root -f %s -C %s %s" % ( | ||
| 165 | tar_file, parent, os.path.relpath(include_path, parent)) | ||
| 166 | exec_native_cmd(tar_cmd, native_sysroot) | ||
| 167 | else: | ||
| 168 | if include_path in krootfs_dir: | ||
| 169 | include_path = krootfs_dir[include_path] | ||
| 170 | include_path = cls.__get_rootfs_dir(include_path) | ||
| 171 | include_pseudo = os.path.join(include_path, "../pseudo") | ||
| 172 | if os.path.lexists(include_pseudo): | ||
| 173 | pseudo = cls.__get_pseudo(native_sysroot, include_path, | ||
| 174 | include_pseudo) | ||
| 175 | tar_cmd = "tar cf %s -C %s ." % (tar_file, include_path) | ||
| 176 | else: | ||
| 177 | pseudo = None | ||
| 178 | tar_cmd = "tar c --owner=root --group=root -f %s -C %s ." % ( | ||
| 179 | tar_file, include_path) | ||
| 180 | exec_native_cmd(tar_cmd, native_sysroot, pseudo) | ||
| 181 | |||
| 182 | #create destination | ||
| 183 | if path: | ||
| 184 | destination = cls.__validate_path("--include-path", new_rootfs, path) | ||
| 185 | Path(destination).mkdir(parents=True, exist_ok=True) | ||
| 186 | else: | ||
| 187 | destination = new_rootfs | ||
| 188 | |||
| 189 | #extract destination | ||
| 190 | untar_cmd = "tar xf %s -C %s" % (tar_file, destination) | ||
| 191 | if new_pseudo: | ||
| 192 | pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo) | ||
| 193 | else: | ||
| 194 | pseudo = None | ||
| 195 | exec_native_cmd(untar_cmd, native_sysroot, pseudo) | ||
| 196 | os.remove(tar_file) | ||
| 197 | |||
| 198 | for orig_path in part.exclude_path or []: | ||
| 199 | path = orig_path | ||
| 200 | |||
| 201 | full_path = cls.__validate_path("--exclude-path", new_rootfs, path) | ||
| 202 | |||
| 203 | if not os.path.lexists(full_path): | ||
| 204 | continue | ||
| 205 | |||
| 206 | if new_pseudo: | ||
| 207 | pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo) | ||
| 208 | else: | ||
| 209 | pseudo = None | ||
| 210 | if path.endswith(os.sep): | ||
| 211 | # Delete content only. | ||
| 212 | for entry in os.listdir(full_path): | ||
| 213 | full_entry = os.path.join(full_path, entry) | ||
| 214 | rm_cmd = "rm -rf %s" % (full_entry) | ||
| 215 | exec_native_cmd(rm_cmd, native_sysroot, pseudo) | ||
| 216 | else: | ||
| 217 | # Delete whole directory. | ||
| 218 | rm_cmd = "rm -rf %s" % (full_path) | ||
| 219 | exec_native_cmd(rm_cmd, native_sysroot, pseudo) | ||
| 220 | |||
| 221 | # Update part.has_fstab here as fstab may have been added or | ||
| 222 | # removed by the above modifications. | ||
| 223 | part.has_fstab = os.path.exists(os.path.join(new_rootfs, "etc/fstab")) | ||
| 224 | if part.update_fstab_in_rootfs and part.has_fstab and not part.no_fstab_update: | ||
| 225 | fstab_path = os.path.join(new_rootfs, "etc/fstab") | ||
| 226 | # Assume that fstab should always be owned by root with fixed permissions | ||
| 227 | install_cmd = "install -m 0644 -p %s %s" % (part.updated_fstab_path, fstab_path) | ||
| 228 | if new_pseudo: | ||
| 229 | pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo) | ||
| 230 | else: | ||
| 231 | pseudo = None | ||
| 232 | exec_native_cmd(install_cmd, native_sysroot, pseudo) | ||
| 233 | |||
| 234 | part.prepare_rootfs(cr_workdir, oe_builddir, | ||
| 235 | new_rootfs or part.rootfs_dir, native_sysroot, | ||
| 236 | pseudo_dir = new_pseudo or pseudo_dir) | ||
