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