diff options
| author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2025-11-07 13:31:53 +0000 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2025-11-07 13:31:53 +0000 |
| commit | 8c22ff0d8b70d9b12f0487ef696a7e915b9e3173 (patch) | |
| tree | efdc32587159d0050a69009bdf2330a531727d95 /scripts/lib/wic/engine.py | |
| parent | d412d2747595c1cc4a5e3ca975e3adc31b2f7891 (diff) | |
| download | poky-8c22ff0d8b70d9b12f0487ef696a7e915b9e3173.tar.gz | |
The poky repository master branch is no longer being updated.
You can either:
a) switch to individual clones of bitbake, openembedded-core, meta-yocto and yocto-docs
b) use the new bitbake-setup
You can find information about either approach in our documentation:
https://docs.yoctoproject.org/
Note that "poky" the distro setting is still available in meta-yocto as
before and we continue to use and maintain that.
Long live Poky!
Some further information on the background of this change can be found
in: https://lists.openembedded.org/g/openembedded-architecture/message/2179
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic/engine.py')
| -rw-r--r-- | scripts/lib/wic/engine.py | 646 |
1 files changed, 0 insertions, 646 deletions
diff --git a/scripts/lib/wic/engine.py b/scripts/lib/wic/engine.py deleted file mode 100644 index b9e60cbe4e..0000000000 --- a/scripts/lib/wic/engine.py +++ /dev/null | |||
| @@ -1,646 +0,0 @@ | |||
| 1 | # | ||
| 2 | # Copyright (c) 2013, Intel Corporation. | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | # DESCRIPTION | ||
| 7 | |||
| 8 | # This module implements the image creation engine used by 'wic' to | ||
| 9 | # create images. The engine parses through the OpenEmbedded kickstart | ||
| 10 | # (wks) file specified and generates images that can then be directly | ||
| 11 | # written onto media. | ||
| 12 | # | ||
| 13 | # AUTHORS | ||
| 14 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> | ||
| 15 | # | ||
| 16 | |||
| 17 | import logging | ||
| 18 | import os | ||
| 19 | import tempfile | ||
| 20 | import json | ||
| 21 | import subprocess | ||
| 22 | import shutil | ||
| 23 | import re | ||
| 24 | |||
| 25 | from collections import namedtuple, OrderedDict | ||
| 26 | |||
| 27 | from wic import WicError | ||
| 28 | from wic.filemap import sparse_copy | ||
| 29 | from wic.pluginbase import PluginMgr | ||
| 30 | from wic.misc import get_bitbake_var, exec_cmd | ||
| 31 | |||
| 32 | logger = logging.getLogger('wic') | ||
| 33 | |||
| 34 | def verify_build_env(): | ||
| 35 | """ | ||
| 36 | Verify that the build environment is sane. | ||
| 37 | |||
| 38 | Returns True if it is, false otherwise | ||
| 39 | """ | ||
| 40 | if not os.environ.get("BUILDDIR"): | ||
| 41 | raise WicError("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)") | ||
| 42 | |||
| 43 | return True | ||
| 44 | |||
| 45 | |||
| 46 | CANNED_IMAGE_DIR = "lib/wic/canned-wks" # relative to scripts | ||
| 47 | SCRIPTS_CANNED_IMAGE_DIR = "scripts/" + CANNED_IMAGE_DIR | ||
| 48 | WIC_DIR = "wic" | ||
| 49 | |||
| 50 | def build_canned_image_list(path): | ||
| 51 | layers_path = get_bitbake_var("BBLAYERS") | ||
| 52 | canned_wks_layer_dirs = [] | ||
| 53 | |||
| 54 | if layers_path is not None: | ||
| 55 | for layer_path in layers_path.split(): | ||
| 56 | for wks_path in (WIC_DIR, SCRIPTS_CANNED_IMAGE_DIR): | ||
| 57 | cpath = os.path.join(layer_path, wks_path) | ||
| 58 | if os.path.isdir(cpath): | ||
| 59 | canned_wks_layer_dirs.append(cpath) | ||
| 60 | |||
| 61 | cpath = os.path.join(path, CANNED_IMAGE_DIR) | ||
| 62 | canned_wks_layer_dirs.append(cpath) | ||
| 63 | |||
| 64 | return canned_wks_layer_dirs | ||
| 65 | |||
| 66 | def find_canned_image(scripts_path, wks_file): | ||
| 67 | """ | ||
| 68 | Find a .wks file with the given name in the canned files dir. | ||
| 69 | |||
| 70 | Return False if not found | ||
| 71 | """ | ||
| 72 | layers_canned_wks_dir = build_canned_image_list(scripts_path) | ||
| 73 | |||
| 74 | for canned_wks_dir in layers_canned_wks_dir: | ||
| 75 | for root, dirs, files in os.walk(canned_wks_dir): | ||
| 76 | for fname in files: | ||
| 77 | if fname.endswith("~") or fname.endswith("#"): | ||
| 78 | continue | ||
| 79 | if ((fname.endswith(".wks") and wks_file + ".wks" == fname) or \ | ||
| 80 | (fname.endswith(".wks.in") and wks_file + ".wks.in" == fname)): | ||
| 81 | fullpath = os.path.join(canned_wks_dir, fname) | ||
| 82 | return fullpath | ||
| 83 | return None | ||
| 84 | |||
| 85 | |||
| 86 | def list_canned_images(scripts_path): | ||
| 87 | """ | ||
| 88 | List the .wks files in the canned image dir, minus the extension. | ||
| 89 | """ | ||
| 90 | layers_canned_wks_dir = build_canned_image_list(scripts_path) | ||
| 91 | |||
| 92 | for canned_wks_dir in layers_canned_wks_dir: | ||
| 93 | for root, dirs, files in os.walk(canned_wks_dir): | ||
| 94 | for fname in files: | ||
| 95 | if fname.endswith("~") or fname.endswith("#"): | ||
| 96 | continue | ||
| 97 | if fname.endswith(".wks") or fname.endswith(".wks.in"): | ||
| 98 | fullpath = os.path.join(canned_wks_dir, fname) | ||
| 99 | with open(fullpath) as wks: | ||
| 100 | for line in wks: | ||
| 101 | desc = "" | ||
| 102 | idx = line.find("short-description:") | ||
| 103 | if idx != -1: | ||
| 104 | desc = line[idx + len("short-description:"):].strip() | ||
| 105 | break | ||
| 106 | basename = fname.split('.')[0] | ||
| 107 | print(" %s\t\t%s" % (basename.ljust(30), desc)) | ||
| 108 | |||
| 109 | |||
| 110 | def list_canned_image_help(scripts_path, fullpath): | ||
| 111 | """ | ||
| 112 | List the help and params in the specified canned image. | ||
| 113 | """ | ||
| 114 | found = False | ||
| 115 | with open(fullpath) as wks: | ||
| 116 | for line in wks: | ||
| 117 | if not found: | ||
| 118 | idx = line.find("long-description:") | ||
| 119 | if idx != -1: | ||
| 120 | print() | ||
| 121 | print(line[idx + len("long-description:"):].strip()) | ||
| 122 | found = True | ||
| 123 | continue | ||
| 124 | if not line.strip(): | ||
| 125 | break | ||
| 126 | idx = line.find("#") | ||
| 127 | if idx != -1: | ||
| 128 | print(line[idx + len("#:"):].rstrip()) | ||
| 129 | else: | ||
| 130 | break | ||
| 131 | |||
| 132 | |||
| 133 | def list_source_plugins(): | ||
| 134 | """ | ||
| 135 | List the available source plugins i.e. plugins available for --source. | ||
| 136 | """ | ||
| 137 | plugins = PluginMgr.get_plugins('source') | ||
| 138 | |||
| 139 | for plugin in plugins: | ||
| 140 | print(" %s" % plugin) | ||
| 141 | |||
| 142 | |||
| 143 | def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir, | ||
| 144 | native_sysroot, options): | ||
| 145 | """ | ||
| 146 | Create image | ||
| 147 | |||
| 148 | wks_file - user-defined OE kickstart file | ||
| 149 | rootfs_dir - absolute path to the build's /rootfs dir | ||
| 150 | bootimg_dir - absolute path to the build's boot artifacts directory | ||
| 151 | kernel_dir - absolute path to the build's kernel directory | ||
| 152 | native_sysroot - absolute path to the build's native sysroots dir | ||
| 153 | image_output_dir - dirname to create for image | ||
| 154 | options - wic command line options (debug, bmap, etc) | ||
| 155 | |||
| 156 | Normally, the values for the build artifacts values are determined | ||
| 157 | by 'wic -e' from the output of the 'bitbake -e' command given an | ||
| 158 | image name e.g. 'core-image-minimal' and a given machine set in | ||
| 159 | local.conf. If that's the case, the variables get the following | ||
| 160 | values from the output of 'bitbake -e': | ||
| 161 | |||
| 162 | rootfs_dir: IMAGE_ROOTFS | ||
| 163 | kernel_dir: DEPLOY_DIR_IMAGE | ||
| 164 | native_sysroot: STAGING_DIR_NATIVE | ||
| 165 | |||
| 166 | In the above case, bootimg_dir remains unset and the | ||
| 167 | plugin-specific image creation code is responsible for finding the | ||
| 168 | bootimg artifacts. | ||
| 169 | |||
| 170 | In the case where the values are passed in explicitly i.e 'wic -e' | ||
| 171 | is not used but rather the individual 'wic' options are used to | ||
| 172 | explicitly specify these values. | ||
| 173 | """ | ||
| 174 | try: | ||
| 175 | oe_builddir = os.environ["BUILDDIR"] | ||
| 176 | except KeyError: | ||
| 177 | raise WicError("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)") | ||
| 178 | |||
| 179 | if not os.path.exists(options.outdir): | ||
| 180 | os.makedirs(options.outdir) | ||
| 181 | |||
| 182 | pname = options.imager | ||
| 183 | # Don't support '-' in plugin names | ||
| 184 | pname = pname.replace("-", "_") | ||
| 185 | plugin_class = PluginMgr.get_plugins('imager').get(pname) | ||
| 186 | if not plugin_class: | ||
| 187 | raise WicError('Unknown plugin: %s' % pname) | ||
| 188 | |||
| 189 | plugin = plugin_class(wks_file, rootfs_dir, bootimg_dir, kernel_dir, | ||
| 190 | native_sysroot, oe_builddir, options) | ||
| 191 | |||
| 192 | plugin.do_create() | ||
| 193 | |||
| 194 | logger.info("The image(s) were created using OE kickstart file:\n %s", wks_file) | ||
| 195 | |||
| 196 | |||
| 197 | def wic_list(args, scripts_path): | ||
| 198 | """ | ||
| 199 | Print the list of images or source plugins. | ||
| 200 | """ | ||
| 201 | if args.list_type is None: | ||
| 202 | return False | ||
| 203 | |||
| 204 | if args.list_type == "images": | ||
| 205 | |||
| 206 | list_canned_images(scripts_path) | ||
| 207 | return True | ||
| 208 | elif args.list_type == "source-plugins": | ||
| 209 | list_source_plugins() | ||
| 210 | return True | ||
| 211 | elif len(args.help_for) == 1 and args.help_for[0] == 'help': | ||
| 212 | wks_file = args.list_type | ||
| 213 | fullpath = find_canned_image(scripts_path, wks_file) | ||
| 214 | if not fullpath: | ||
| 215 | raise WicError("No image named %s found, exiting. " | ||
| 216 | "(Use 'wic list images' to list available images, " | ||
| 217 | "or specify a fully-qualified OE kickstart (.wks) " | ||
| 218 | "filename)" % wks_file) | ||
| 219 | |||
| 220 | list_canned_image_help(scripts_path, fullpath) | ||
| 221 | return True | ||
| 222 | |||
| 223 | return False | ||
| 224 | |||
| 225 | |||
| 226 | class Disk: | ||
| 227 | def __init__(self, imagepath, native_sysroot, fstypes=('fat', 'ext')): | ||
| 228 | self.imagepath = imagepath | ||
| 229 | self.native_sysroot = native_sysroot | ||
| 230 | self.fstypes = fstypes | ||
| 231 | self._partitions = None | ||
| 232 | self._partimages = {} | ||
| 233 | self._lsector_size = None | ||
| 234 | self._psector_size = None | ||
| 235 | self._ptable_format = None | ||
| 236 | |||
| 237 | # define sector size | ||
| 238 | sector_size_str = get_bitbake_var('WIC_SECTOR_SIZE') | ||
| 239 | if sector_size_str is not None: | ||
| 240 | try: | ||
| 241 | self.sector_size = int(sector_size_str) | ||
| 242 | except ValueError: | ||
| 243 | self.sector_size = None | ||
| 244 | else: | ||
| 245 | self.sector_size = None | ||
| 246 | |||
| 247 | # find parted | ||
| 248 | # read paths from $PATH environment variable | ||
| 249 | # if it fails, use hardcoded paths | ||
| 250 | pathlist = "/bin:/usr/bin:/usr/sbin:/sbin/" | ||
| 251 | try: | ||
| 252 | self.paths = os.environ['PATH'] + ":" + pathlist | ||
| 253 | except KeyError: | ||
| 254 | self.paths = pathlist | ||
| 255 | |||
| 256 | if native_sysroot: | ||
| 257 | for path in pathlist.split(':'): | ||
| 258 | self.paths = "%s%s:%s" % (native_sysroot, path, self.paths) | ||
| 259 | |||
| 260 | self.parted = shutil.which("parted", path=self.paths) | ||
| 261 | if not self.parted: | ||
| 262 | raise WicError("Can't find executable parted") | ||
| 263 | |||
| 264 | self.partitions = self.get_partitions() | ||
| 265 | |||
| 266 | def __del__(self): | ||
| 267 | for path in self._partimages.values(): | ||
| 268 | os.unlink(path) | ||
| 269 | |||
| 270 | def get_partitions(self): | ||
| 271 | if self._partitions is None: | ||
| 272 | self._partitions = OrderedDict() | ||
| 273 | |||
| 274 | if self.sector_size is not None: | ||
| 275 | out = exec_cmd("export PARTED_SECTOR_SIZE=%d; %s -sm %s unit B print" % \ | ||
| 276 | (self.sector_size, self.parted, self.imagepath), True) | ||
| 277 | else: | ||
| 278 | out = exec_cmd("%s -sm %s unit B print" % (self.parted, self.imagepath)) | ||
| 279 | |||
| 280 | parttype = namedtuple("Part", "pnum start end size fstype") | ||
| 281 | splitted = out.splitlines() | ||
| 282 | # skip over possible errors in exec_cmd output | ||
| 283 | try: | ||
| 284 | idx =splitted.index("BYT;") | ||
| 285 | except ValueError: | ||
| 286 | raise WicError("Error getting partition information from %s" % (self.parted)) | ||
| 287 | lsector_size, psector_size, self._ptable_format = splitted[idx + 1].split(":")[3:6] | ||
| 288 | self._lsector_size = int(lsector_size) | ||
| 289 | self._psector_size = int(psector_size) | ||
| 290 | for line in splitted[idx + 2:]: | ||
| 291 | pnum, start, end, size, fstype = line.split(':')[:5] | ||
| 292 | partition = parttype(int(pnum), int(start[:-1]), int(end[:-1]), | ||
| 293 | int(size[:-1]), fstype) | ||
| 294 | self._partitions[pnum] = partition | ||
| 295 | |||
| 296 | return self._partitions | ||
| 297 | |||
| 298 | def __getattr__(self, name): | ||
| 299 | """Get path to the executable in a lazy way.""" | ||
| 300 | if name in ("mdir", "mcopy", "mdel", "mdeltree", "sfdisk", "e2fsck", | ||
| 301 | "resize2fs", "mkswap", "mkdosfs", "debugfs","blkid"): | ||
| 302 | aname = "_%s" % name | ||
| 303 | if aname not in self.__dict__: | ||
| 304 | setattr(self, aname, shutil.which(name, path=self.paths)) | ||
| 305 | if aname not in self.__dict__ or self.__dict__[aname] is None: | ||
| 306 | raise WicError("Can't find executable '{}'".format(name)) | ||
| 307 | return self.__dict__[aname] | ||
| 308 | return self.__dict__[name] | ||
| 309 | |||
| 310 | def _get_part_image(self, pnum): | ||
| 311 | if pnum not in self.partitions: | ||
| 312 | raise WicError("Partition %s is not in the image" % pnum) | ||
| 313 | part = self.partitions[pnum] | ||
| 314 | # check if fstype is supported | ||
| 315 | for fstype in self.fstypes: | ||
| 316 | if part.fstype.startswith(fstype): | ||
| 317 | break | ||
| 318 | else: | ||
| 319 | raise WicError("Not supported fstype: {}".format(part.fstype)) | ||
| 320 | if pnum not in self._partimages: | ||
| 321 | tmpf = tempfile.NamedTemporaryFile(prefix="wic-part") | ||
| 322 | dst_fname = tmpf.name | ||
| 323 | tmpf.close() | ||
| 324 | sparse_copy(self.imagepath, dst_fname, skip=part.start, length=part.size) | ||
| 325 | self._partimages[pnum] = dst_fname | ||
| 326 | |||
| 327 | return self._partimages[pnum] | ||
| 328 | |||
| 329 | def _put_part_image(self, pnum): | ||
| 330 | """Put partition image into partitioned image.""" | ||
| 331 | sparse_copy(self._partimages[pnum], self.imagepath, | ||
| 332 | seek=self.partitions[pnum].start) | ||
| 333 | |||
| 334 | def dir(self, pnum, path): | ||
| 335 | if pnum not in self.partitions: | ||
| 336 | raise WicError("Partition %s is not in the image" % pnum) | ||
| 337 | |||
| 338 | if self.partitions[pnum].fstype.startswith('ext'): | ||
| 339 | return exec_cmd("{} {} -R 'ls -l {}'".format(self.debugfs, | ||
| 340 | self._get_part_image(pnum), | ||
| 341 | path), as_shell=True) | ||
| 342 | else: # fat | ||
| 343 | return exec_cmd("{} -i {} ::{}".format(self.mdir, | ||
| 344 | self._get_part_image(pnum), | ||
| 345 | path)) | ||
| 346 | |||
| 347 | def copy(self, src, dest): | ||
| 348 | """Copy partition image into wic image.""" | ||
| 349 | pnum = dest.part if isinstance(src, str) else src.part | ||
| 350 | |||
| 351 | if self.partitions[pnum].fstype.startswith('ext'): | ||
| 352 | if isinstance(src, str): | ||
| 353 | cmd = "printf 'cd {}\nwrite {} {}\n' | {} -w {}".\ | ||
| 354 | format(os.path.dirname(dest.path), src, os.path.basename(src), | ||
| 355 | self.debugfs, self._get_part_image(pnum)) | ||
| 356 | else: # copy from wic | ||
| 357 | # run both dump and rdump to support both files and directory | ||
| 358 | cmd = "printf 'cd {}\ndump /{} {}\nrdump /{} {}\n' | {} {}".\ | ||
| 359 | format(os.path.dirname(src.path), src.path, | ||
| 360 | dest, src.path, dest, self.debugfs, | ||
| 361 | self._get_part_image(pnum)) | ||
| 362 | else: # fat | ||
| 363 | if isinstance(src, str): | ||
| 364 | cmd = "{} -i {} -snop {} ::{}".format(self.mcopy, | ||
| 365 | self._get_part_image(pnum), | ||
| 366 | src, dest.path) | ||
| 367 | else: | ||
| 368 | cmd = "{} -i {} -snop ::{} {}".format(self.mcopy, | ||
| 369 | self._get_part_image(pnum), | ||
| 370 | src.path, dest) | ||
| 371 | |||
| 372 | exec_cmd(cmd, as_shell=True) | ||
| 373 | self._put_part_image(pnum) | ||
| 374 | |||
| 375 | def remove_ext(self, pnum, path, recursive): | ||
| 376 | """ | ||
| 377 | Remove files/dirs and their contents from the partition. | ||
| 378 | This only applies to ext* partition. | ||
| 379 | """ | ||
| 380 | abs_path = re.sub(r'\/\/+', '/', path) | ||
| 381 | cmd = "{} {} -wR 'rm \"{}\"'".format(self.debugfs, | ||
| 382 | self._get_part_image(pnum), | ||
| 383 | abs_path) | ||
| 384 | out = exec_cmd(cmd , as_shell=True) | ||
| 385 | for line in out.splitlines(): | ||
| 386 | if line.startswith("rm:"): | ||
| 387 | if "file is a directory" in line: | ||
| 388 | if recursive: | ||
| 389 | # loop through content and delete them one by one if | ||
| 390 | # flaged with -r | ||
| 391 | subdirs = iter(self.dir(pnum, abs_path).splitlines()) | ||
| 392 | next(subdirs) | ||
| 393 | for subdir in subdirs: | ||
| 394 | dir = subdir.split(':')[1].split(" ", 1)[1] | ||
| 395 | if not dir == "." and not dir == "..": | ||
| 396 | self.remove_ext(pnum, "%s/%s" % (abs_path, dir), recursive) | ||
| 397 | |||
| 398 | rmdir_out = exec_cmd("{} {} -wR 'rmdir \"{}\"'".format(self.debugfs, | ||
| 399 | self._get_part_image(pnum), | ||
| 400 | abs_path.rstrip('/')) | ||
| 401 | , as_shell=True) | ||
| 402 | |||
| 403 | for rmdir_line in rmdir_out.splitlines(): | ||
| 404 | if "directory not empty" in rmdir_line: | ||
| 405 | raise WicError("Could not complete operation: \n%s \n" | ||
| 406 | "use -r to remove non-empty directory" % rmdir_line) | ||
| 407 | if rmdir_line.startswith("rmdir:"): | ||
| 408 | raise WicError("Could not complete operation: \n%s " | ||
| 409 | "\n%s" % (str(line), rmdir_line)) | ||
| 410 | |||
| 411 | else: | ||
| 412 | raise WicError("Could not complete operation: \n%s " | ||
| 413 | "\nUnable to remove %s" % (str(line), abs_path)) | ||
| 414 | |||
| 415 | def remove(self, pnum, path, recursive): | ||
| 416 | """Remove files/dirs from the partition.""" | ||
| 417 | partimg = self._get_part_image(pnum) | ||
| 418 | if self.partitions[pnum].fstype.startswith('ext'): | ||
| 419 | self.remove_ext(pnum, path, recursive) | ||
| 420 | |||
| 421 | else: # fat | ||
| 422 | cmd = "{} -i {} ::{}".format(self.mdel, partimg, path) | ||
| 423 | try: | ||
| 424 | exec_cmd(cmd) | ||
| 425 | except WicError as err: | ||
| 426 | if "not found" in str(err) or "non empty" in str(err): | ||
| 427 | # mdel outputs 'File ... not found' or 'directory .. non empty" | ||
| 428 | # try to use mdeltree as path could be a directory | ||
| 429 | cmd = "{} -i {} ::{}".format(self.mdeltree, | ||
| 430 | partimg, path) | ||
| 431 | exec_cmd(cmd) | ||
| 432 | else: | ||
| 433 | raise err | ||
| 434 | self._put_part_image(pnum) | ||
| 435 | |||
| 436 | def write(self, target, expand): | ||
| 437 | """Write disk image to the media or file.""" | ||
| 438 | def write_sfdisk_script(outf, parts): | ||
| 439 | for key, val in parts['partitiontable'].items(): | ||
| 440 | if key in ("partitions", "device", "firstlba", "lastlba"): | ||
| 441 | continue | ||
| 442 | if key == "id": | ||
| 443 | key = "label-id" | ||
| 444 | outf.write("{}: {}\n".format(key, val)) | ||
| 445 | outf.write("\n") | ||
| 446 | for part in parts['partitiontable']['partitions']: | ||
| 447 | line = '' | ||
| 448 | for name in ('attrs', 'name', 'size', 'type', 'uuid'): | ||
| 449 | if name == 'size' and part['type'] == 'f': | ||
| 450 | # don't write size for extended partition | ||
| 451 | continue | ||
| 452 | val = part.get(name) | ||
| 453 | if val: | ||
| 454 | line += '{}={}, '.format(name, val) | ||
| 455 | if line: | ||
| 456 | line = line[:-2] # strip ', ' | ||
| 457 | if part.get('bootable'): | ||
| 458 | line += ' ,bootable' | ||
| 459 | outf.write("{}\n".format(line)) | ||
| 460 | outf.flush() | ||
| 461 | |||
| 462 | def read_ptable(path): | ||
| 463 | out = exec_cmd("{} -J {}".format(self.sfdisk, path)) | ||
| 464 | return json.loads(out) | ||
| 465 | |||
| 466 | def write_ptable(parts, target): | ||
| 467 | with tempfile.NamedTemporaryFile(prefix="wic-sfdisk-", mode='w') as outf: | ||
| 468 | write_sfdisk_script(outf, parts) | ||
| 469 | cmd = "{} --no-reread {} < {} ".format(self.sfdisk, target, outf.name) | ||
| 470 | exec_cmd(cmd, as_shell=True) | ||
| 471 | |||
| 472 | if expand is None: | ||
| 473 | sparse_copy(self.imagepath, target) | ||
| 474 | else: | ||
| 475 | # copy first sectors that may contain bootloader | ||
| 476 | sparse_copy(self.imagepath, target, length=2048 * self._lsector_size) | ||
| 477 | |||
| 478 | # copy source partition table to the target | ||
| 479 | parts = read_ptable(self.imagepath) | ||
| 480 | write_ptable(parts, target) | ||
| 481 | |||
| 482 | # get size of unpartitioned space | ||
| 483 | free = None | ||
| 484 | for line in exec_cmd("{} -F {}".format(self.sfdisk, target)).splitlines(): | ||
| 485 | if line.startswith("Unpartitioned space ") and line.endswith("sectors"): | ||
| 486 | free = int(line.split()[-2]) | ||
| 487 | # Align free space to a 2048 sector boundary. YOCTO #12840. | ||
| 488 | free = free - (free % 2048) | ||
| 489 | if free is None: | ||
| 490 | raise WicError("Can't get size of unpartitioned space") | ||
| 491 | |||
| 492 | # calculate expanded partitions sizes | ||
| 493 | sizes = {} | ||
| 494 | num_auto_resize = 0 | ||
| 495 | for num, part in enumerate(parts['partitiontable']['partitions'], 1): | ||
| 496 | if num in expand: | ||
| 497 | if expand[num] != 0: # don't resize partition if size is set to 0 | ||
| 498 | sectors = expand[num] // self._lsector_size | ||
| 499 | free -= sectors - part['size'] | ||
| 500 | part['size'] = sectors | ||
| 501 | sizes[num] = sectors | ||
| 502 | elif part['type'] != 'f': | ||
| 503 | sizes[num] = -1 | ||
| 504 | num_auto_resize += 1 | ||
| 505 | |||
| 506 | for num, part in enumerate(parts['partitiontable']['partitions'], 1): | ||
| 507 | if sizes.get(num) == -1: | ||
| 508 | part['size'] += free // num_auto_resize | ||
| 509 | |||
| 510 | # write resized partition table to the target | ||
| 511 | write_ptable(parts, target) | ||
| 512 | |||
| 513 | # read resized partition table | ||
| 514 | parts = read_ptable(target) | ||
| 515 | |||
| 516 | # copy partitions content | ||
| 517 | for num, part in enumerate(parts['partitiontable']['partitions'], 1): | ||
| 518 | pnum = str(num) | ||
| 519 | fstype = self.partitions[pnum].fstype | ||
| 520 | |||
| 521 | # copy unchanged partition | ||
| 522 | if part['size'] == self.partitions[pnum].size // self._lsector_size: | ||
| 523 | logger.info("copying unchanged partition {}".format(pnum)) | ||
| 524 | sparse_copy(self._get_part_image(pnum), target, seek=part['start'] * self._lsector_size) | ||
| 525 | continue | ||
| 526 | |||
| 527 | # resize or re-create partitions | ||
| 528 | if fstype.startswith('ext') or fstype.startswith('fat') or \ | ||
| 529 | fstype.startswith('linux-swap'): | ||
| 530 | |||
| 531 | partfname = None | ||
| 532 | with tempfile.NamedTemporaryFile(prefix="wic-part{}-".format(pnum)) as partf: | ||
| 533 | partfname = partf.name | ||
| 534 | |||
| 535 | if fstype.startswith('ext'): | ||
| 536 | logger.info("resizing ext partition {}".format(pnum)) | ||
| 537 | partimg = self._get_part_image(pnum) | ||
| 538 | sparse_copy(partimg, partfname) | ||
| 539 | exec_cmd("{} -pf {}".format(self.e2fsck, partfname)) | ||
| 540 | exec_cmd("{} {} {}s".format(\ | ||
| 541 | self.resize2fs, partfname, part['size'])) | ||
| 542 | elif fstype.startswith('fat'): | ||
| 543 | logger.info("copying content of the fat partition {}".format(pnum)) | ||
| 544 | with tempfile.TemporaryDirectory(prefix='wic-fatdir-') as tmpdir: | ||
| 545 | # copy content to the temporary directory | ||
| 546 | cmd = "{} -snompi {} :: {}".format(self.mcopy, | ||
| 547 | self._get_part_image(pnum), | ||
| 548 | tmpdir) | ||
| 549 | exec_cmd(cmd) | ||
| 550 | # create new msdos partition | ||
| 551 | label = part.get("name") | ||
| 552 | label_str = "-n {}".format(label) if label else '' | ||
| 553 | |||
| 554 | cmd = "{} {} -C {} {}".format(self.mkdosfs, label_str, partfname, | ||
| 555 | part['size']) | ||
| 556 | exec_cmd(cmd) | ||
| 557 | # copy content from the temporary directory to the new partition | ||
| 558 | cmd = "{} -snompi {} {}/* ::".format(self.mcopy, partfname, tmpdir) | ||
| 559 | exec_cmd(cmd, as_shell=True) | ||
| 560 | elif fstype.startswith('linux-swap'): | ||
| 561 | logger.info("creating swap partition {}".format(pnum)) | ||
| 562 | label = part.get("name") | ||
| 563 | label_str = "-L {}".format(label) if label else '' | ||
| 564 | out = exec_cmd("{} --probe {}".format(self.blkid, self._get_part_image(pnum))) | ||
| 565 | uuid = out[out.index("UUID=\"")+6:out.index("UUID=\"")+42] | ||
| 566 | uuid_str = "-U {}".format(uuid) if uuid else '' | ||
| 567 | with open(partfname, 'w') as sparse: | ||
| 568 | os.ftruncate(sparse.fileno(), part['size'] * self._lsector_size) | ||
| 569 | exec_cmd("{} {} {} {}".format(self.mkswap, label_str, uuid_str, partfname)) | ||
| 570 | sparse_copy(partfname, target, seek=part['start'] * self._lsector_size) | ||
| 571 | os.unlink(partfname) | ||
| 572 | elif part['type'] != 'f': | ||
| 573 | logger.warning("skipping partition {}: unsupported fstype {}".format(pnum, fstype)) | ||
| 574 | |||
| 575 | def wic_ls(args, native_sysroot): | ||
| 576 | """List contents of partitioned image or vfat partition.""" | ||
| 577 | disk = Disk(args.path.image, native_sysroot) | ||
| 578 | if not args.path.part: | ||
| 579 | if disk.partitions: | ||
| 580 | print('Num Start End Size Fstype') | ||
| 581 | for part in disk.partitions.values(): | ||
| 582 | print("{:2d} {:12d} {:12d} {:12d} {}".format(\ | ||
| 583 | part.pnum, part.start, part.end, | ||
| 584 | part.size, part.fstype)) | ||
| 585 | else: | ||
| 586 | path = args.path.path or '/' | ||
| 587 | print(disk.dir(args.path.part, path)) | ||
| 588 | |||
| 589 | def wic_cp(args, native_sysroot): | ||
| 590 | """ | ||
| 591 | Copy file or directory to/from the vfat/ext partition of | ||
| 592 | partitioned image. | ||
| 593 | """ | ||
| 594 | if isinstance(args.dest, str): | ||
| 595 | disk = Disk(args.src.image, native_sysroot) | ||
| 596 | else: | ||
| 597 | disk = Disk(args.dest.image, native_sysroot) | ||
| 598 | disk.copy(args.src, args.dest) | ||
| 599 | |||
| 600 | |||
| 601 | def wic_rm(args, native_sysroot): | ||
| 602 | """ | ||
| 603 | Remove files or directories from the vfat partition of | ||
| 604 | partitioned image. | ||
| 605 | """ | ||
| 606 | disk = Disk(args.path.image, native_sysroot) | ||
| 607 | disk.remove(args.path.part, args.path.path, args.recursive_delete) | ||
| 608 | |||
| 609 | def wic_write(args, native_sysroot): | ||
| 610 | """ | ||
| 611 | Write image to a target device. | ||
| 612 | """ | ||
| 613 | disk = Disk(args.image, native_sysroot, ('fat', 'ext', 'linux-swap')) | ||
| 614 | disk.write(args.target, args.expand) | ||
| 615 | |||
| 616 | def find_canned(scripts_path, file_name): | ||
| 617 | """ | ||
| 618 | Find a file either by its path or by name in the canned files dir. | ||
| 619 | |||
| 620 | Return None if not found | ||
| 621 | """ | ||
| 622 | if os.path.exists(file_name): | ||
| 623 | return file_name | ||
| 624 | |||
| 625 | layers_canned_wks_dir = build_canned_image_list(scripts_path) | ||
| 626 | for canned_wks_dir in layers_canned_wks_dir: | ||
| 627 | for root, dirs, files in os.walk(canned_wks_dir): | ||
| 628 | for fname in files: | ||
| 629 | if fname == file_name: | ||
| 630 | fullpath = os.path.join(canned_wks_dir, fname) | ||
| 631 | return fullpath | ||
| 632 | |||
| 633 | def get_custom_config(boot_file): | ||
| 634 | """ | ||
| 635 | Get the custom configuration to be used for the bootloader. | ||
| 636 | |||
| 637 | Return None if the file can't be found. | ||
| 638 | """ | ||
| 639 | # Get the scripts path of poky | ||
| 640 | scripts_path = os.path.abspath("%s/../.." % os.path.dirname(__file__)) | ||
| 641 | |||
| 642 | cfg_file = find_canned(scripts_path, boot_file) | ||
| 643 | if cfg_file: | ||
| 644 | with open(cfg_file, "r") as f: | ||
| 645 | config = f.read() | ||
| 646 | return config | ||
