blob: 018815b966882c939ed4b45d844abf223e215712 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Copyright (c) 2013, Intel Corporation.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005#
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 Bishop6e60e8b2018-02-01 10:27:11 -050017import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018import os
Brad Bishopd7bf8c12018-02-25 22:55:05 -050019import tempfile
20import json
21import subprocess
Brad Bishop6dbb3162019-11-25 09:41:34 -050022import re
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023
24from collections import namedtuple, OrderedDict
25from distutils.spawn import find_executable
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027from wic import WicError
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028from wic.filemap import sparse_copy
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029from wic.pluginbase import PluginMgr
Brad Bishopd7bf8c12018-02-25 22:55:05 -050030from wic.misc import get_bitbake_var, exec_cmd
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032logger = logging.getLogger('wic')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
34def 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"):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041 raise WicError("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
43 return True
44
45
46CANNED_IMAGE_DIR = "lib/wic/canned-wks" # relative to scripts
47SCRIPTS_CANNED_IMAGE_DIR = "scripts/" + CANNED_IMAGE_DIR
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048WIC_DIR = "wic"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049
50def build_canned_image_list(path):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 layers_path = get_bitbake_var("BBLAYERS")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 canned_wks_layer_dirs = []
53
54 if layers_path is not None:
55 for layer_path in layers_path.split():
Brad Bishop6e60e8b2018-02-01 10:27:11 -050056 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)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060
61 cpath = os.path.join(path, CANNED_IMAGE_DIR)
62 canned_wks_layer_dirs.append(cpath)
63
64 return canned_wks_layer_dirs
65
66def 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
Brad Bishop15ae2502019-06-18 21:44:24 -040079 if ((fname.endswith(".wks") and wks_file + ".wks" == fname) or \
80 (fname.endswith(".wks.in") and wks_file + ".wks.in" == fname)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 fullpath = os.path.join(canned_wks_dir, fname)
82 return fullpath
83 return None
84
85
86def 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
Brad Bishop15ae2502019-06-18 21:44:24 -040097 if fname.endswith(".wks") or fname.endswith(".wks.in"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 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
Brad Bishop15ae2502019-06-18 21:44:24 -0400106 basename = fname.split('.')[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600107 print(" %s\t\t%s" % (basename.ljust(30), desc))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108
109
110def 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:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600120 print()
121 print(line[idx + len("long-description:"):].strip())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 found = True
123 continue
124 if not line.strip():
125 break
126 idx = line.find("#")
127 if idx != -1:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600128 print(line[idx + len("#:"):].rstrip())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129 else:
130 break
131
132
133def list_source_plugins():
134 """
135 List the available source plugins i.e. plugins available for --source.
136 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 plugins = PluginMgr.get_plugins('source')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138
139 for plugin in plugins:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600140 print(" %s" % plugin)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141
142
143def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500144 native_sysroot, options):
145 """
146 Create image
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147
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
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 image_output_dir - dirname to create for image
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154 options - wic command line options (debug, bmap, etc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155
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:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 raise WicError("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500178
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500179 if not os.path.exists(options.outdir):
180 os.makedirs(options.outdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800182 pname = options.imager
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500183 plugin_class = PluginMgr.get_plugins('imager').get(pname)
184 if not plugin_class:
185 raise WicError('Unknown plugin: %s' % pname)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500187 plugin = plugin_class(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
188 native_sysroot, oe_builddir, options)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500190 plugin.do_create()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600191
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500192 logger.info("The image(s) were created using OE kickstart file:\n %s", wks_file)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193
194
195def wic_list(args, scripts_path):
196 """
197 Print the list of images or source plugins.
198 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500199 if args.list_type is None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200 return False
201
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500202 if args.list_type == "images":
203
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204 list_canned_images(scripts_path)
205 return True
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500206 elif args.list_type == "source-plugins":
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500207 list_source_plugins()
208 return True
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500209 elif len(args.help_for) == 1 and args.help_for[0] == 'help':
210 wks_file = args.list_type
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211 fullpath = find_canned_image(scripts_path, wks_file)
212 if not fullpath:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500213 raise WicError("No image named %s found, exiting. "
214 "(Use 'wic list images' to list available images, "
215 "or specify a fully-qualified OE kickstart (.wks) "
216 "filename)" % wks_file)
217
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218 list_canned_image_help(scripts_path, fullpath)
219 return True
220
221 return False
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500222
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500223
224class Disk:
225 def __init__(self, imagepath, native_sysroot, fstypes=('fat', 'ext')):
226 self.imagepath = imagepath
227 self.native_sysroot = native_sysroot
228 self.fstypes = fstypes
229 self._partitions = None
230 self._partimages = {}
231 self._lsector_size = None
232 self._psector_size = None
233 self._ptable_format = None
234
235 # find parted
Andrew Geissler99467da2019-02-25 18:54:23 -0600236 # read paths from $PATH environment variable
237 # if it fails, use hardcoded paths
238 pathlist = "/bin:/usr/bin:/usr/sbin:/sbin/"
239 try:
240 self.paths = os.environ['PATH'] + ":" + pathlist
241 except KeyError:
242 self.paths = pathlist
243
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500244 if native_sysroot:
Andrew Geissler99467da2019-02-25 18:54:23 -0600245 for path in pathlist.split(':'):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500246 self.paths = "%s%s:%s" % (native_sysroot, path, self.paths)
247
248 self.parted = find_executable("parted", self.paths)
249 if not self.parted:
250 raise WicError("Can't find executable parted")
251
252 self.partitions = self.get_partitions()
253
254 def __del__(self):
255 for path in self._partimages.values():
256 os.unlink(path)
257
258 def get_partitions(self):
259 if self._partitions is None:
260 self._partitions = OrderedDict()
261 out = exec_cmd("%s -sm %s unit B print" % (self.parted, self.imagepath))
262 parttype = namedtuple("Part", "pnum start end size fstype")
263 splitted = out.splitlines()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800264 # skip over possible errors in exec_cmd output
265 try:
266 idx =splitted.index("BYT;")
267 except ValueError:
268 raise WicError("Error getting partition information from %s" % (self.parted))
269 lsector_size, psector_size, self._ptable_format = splitted[idx + 1].split(":")[3:6]
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500270 self._lsector_size = int(lsector_size)
271 self._psector_size = int(psector_size)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800272 for line in splitted[idx + 2:]:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500273 pnum, start, end, size, fstype = line.split(':')[:5]
274 partition = parttype(int(pnum), int(start[:-1]), int(end[:-1]),
275 int(size[:-1]), fstype)
276 self._partitions[pnum] = partition
277
278 return self._partitions
279
280 def __getattr__(self, name):
281 """Get path to the executable in a lazy way."""
282 if name in ("mdir", "mcopy", "mdel", "mdeltree", "sfdisk", "e2fsck",
Andrew Geissler82c905d2020-04-13 13:39:40 -0500283 "resize2fs", "mkswap", "mkdosfs", "debugfs","blkid"):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500284 aname = "_%s" % name
285 if aname not in self.__dict__:
286 setattr(self, aname, find_executable(name, self.paths))
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700287 if aname not in self.__dict__ or self.__dict__[aname] is None:
288 raise WicError("Can't find executable '{}'".format(name))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500289 return self.__dict__[aname]
290 return self.__dict__[name]
291
292 def _get_part_image(self, pnum):
293 if pnum not in self.partitions:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500294 raise WicError("Partition %s is not in the image" % pnum)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500295 part = self.partitions[pnum]
296 # check if fstype is supported
297 for fstype in self.fstypes:
298 if part.fstype.startswith(fstype):
299 break
300 else:
301 raise WicError("Not supported fstype: {}".format(part.fstype))
302 if pnum not in self._partimages:
303 tmpf = tempfile.NamedTemporaryFile(prefix="wic-part")
304 dst_fname = tmpf.name
305 tmpf.close()
306 sparse_copy(self.imagepath, dst_fname, skip=part.start, length=part.size)
307 self._partimages[pnum] = dst_fname
308
309 return self._partimages[pnum]
310
311 def _put_part_image(self, pnum):
312 """Put partition image into partitioned image."""
313 sparse_copy(self._partimages[pnum], self.imagepath,
314 seek=self.partitions[pnum].start)
315
316 def dir(self, pnum, path):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500317 if pnum not in self.partitions:
318 raise WicError("Partition %s is not in the image" % pnum)
319
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500320 if self.partitions[pnum].fstype.startswith('ext'):
321 return exec_cmd("{} {} -R 'ls -l {}'".format(self.debugfs,
322 self._get_part_image(pnum),
323 path), as_shell=True)
324 else: # fat
325 return exec_cmd("{} -i {} ::{}".format(self.mdir,
326 self._get_part_image(pnum),
327 path))
328
Andrew Geissler82c905d2020-04-13 13:39:40 -0500329 def copy(self, src, dest):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500330 """Copy partition image into wic image."""
Andrew Geissler82c905d2020-04-13 13:39:40 -0500331 pnum = dest.part if isinstance(src, str) else src.part
332
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500333 if self.partitions[pnum].fstype.startswith('ext'):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500334 if isinstance(src, str):
335 cmd = "printf 'cd {}\nwrite {} {}\n' | {} -w {}".\
336 format(os.path.dirname(dest.path), src, os.path.basename(src),
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500337 self.debugfs, self._get_part_image(pnum))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500338 else: # copy from wic
339 # run both dump and rdump to support both files and directory
340 cmd = "printf 'cd {}\ndump /{} {}\nrdump /{} {}\n' | {} {}".\
341 format(os.path.dirname(src.path), src.path,
342 dest, src.path, dest, self.debugfs,
343 self._get_part_image(pnum))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500344 else: # fat
Andrew Geissler82c905d2020-04-13 13:39:40 -0500345 if isinstance(src, str):
346 cmd = "{} -i {} -snop {} ::{}".format(self.mcopy,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500347 self._get_part_image(pnum),
Andrew Geissler82c905d2020-04-13 13:39:40 -0500348 src, dest.path)
349 else:
350 cmd = "{} -i {} -snop ::{} {}".format(self.mcopy,
351 self._get_part_image(pnum),
352 src.path, dest)
353
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500354 exec_cmd(cmd, as_shell=True)
355 self._put_part_image(pnum)
356
Brad Bishop6dbb3162019-11-25 09:41:34 -0500357 def remove_ext(self, pnum, path, recursive):
358 """
359 Remove files/dirs and their contents from the partition.
360 This only applies to ext* partition.
361 """
362 abs_path = re.sub('\/\/+', '/', path)
363 cmd = "{} {} -wR 'rm \"{}\"'".format(self.debugfs,
364 self._get_part_image(pnum),
365 abs_path)
366 out = exec_cmd(cmd , as_shell=True)
367 for line in out.splitlines():
368 if line.startswith("rm:"):
369 if "file is a directory" in line:
370 if recursive:
371 # loop through content and delete them one by one if
372 # flaged with -r
373 subdirs = iter(self.dir(pnum, abs_path).splitlines())
374 next(subdirs)
375 for subdir in subdirs:
376 dir = subdir.split(':')[1].split(" ", 1)[1]
377 if not dir == "." and not dir == "..":
378 self.remove_ext(pnum, "%s/%s" % (abs_path, dir), recursive)
379
380 rmdir_out = exec_cmd("{} {} -wR 'rmdir \"{}\"'".format(self.debugfs,
381 self._get_part_image(pnum),
382 abs_path.rstrip('/'))
383 , as_shell=True)
384
385 for rmdir_line in rmdir_out.splitlines():
386 if "directory not empty" in rmdir_line:
387 raise WicError("Could not complete operation: \n%s \n"
388 "use -r to remove non-empty directory" % rmdir_line)
389 if rmdir_line.startswith("rmdir:"):
390 raise WicError("Could not complete operation: \n%s "
391 "\n%s" % (str(line), rmdir_line))
392
393 else:
394 raise WicError("Could not complete operation: \n%s "
395 "\nUnable to remove %s" % (str(line), abs_path))
396
397 def remove(self, pnum, path, recursive):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500398 """Remove files/dirs from the partition."""
399 partimg = self._get_part_image(pnum)
400 if self.partitions[pnum].fstype.startswith('ext'):
Brad Bishop6dbb3162019-11-25 09:41:34 -0500401 self.remove_ext(pnum, path, recursive)
402
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500403 else: # fat
404 cmd = "{} -i {} ::{}".format(self.mdel, partimg, path)
405 try:
406 exec_cmd(cmd)
407 except WicError as err:
408 if "not found" in str(err) or "non empty" in str(err):
409 # mdel outputs 'File ... not found' or 'directory .. non empty"
410 # try to use mdeltree as path could be a directory
411 cmd = "{} -i {} ::{}".format(self.mdeltree,
412 partimg, path)
413 exec_cmd(cmd)
414 else:
415 raise err
416 self._put_part_image(pnum)
417
418 def write(self, target, expand):
419 """Write disk image to the media or file."""
420 def write_sfdisk_script(outf, parts):
421 for key, val in parts['partitiontable'].items():
422 if key in ("partitions", "device", "firstlba", "lastlba"):
423 continue
424 if key == "id":
425 key = "label-id"
426 outf.write("{}: {}\n".format(key, val))
427 outf.write("\n")
428 for part in parts['partitiontable']['partitions']:
429 line = ''
430 for name in ('attrs', 'name', 'size', 'type', 'uuid'):
431 if name == 'size' and part['type'] == 'f':
432 # don't write size for extended partition
433 continue
434 val = part.get(name)
435 if val:
436 line += '{}={}, '.format(name, val)
437 if line:
438 line = line[:-2] # strip ', '
439 if part.get('bootable'):
440 line += ' ,bootable'
441 outf.write("{}\n".format(line))
442 outf.flush()
443
444 def read_ptable(path):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500445 out = exec_cmd("{} -J {}".format(self.sfdisk, path))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500446 return json.loads(out)
447
448 def write_ptable(parts, target):
449 with tempfile.NamedTemporaryFile(prefix="wic-sfdisk-", mode='w') as outf:
450 write_sfdisk_script(outf, parts)
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700451 cmd = "{} --no-reread {} < {} ".format(self.sfdisk, target, outf.name)
452 exec_cmd(cmd, as_shell=True)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500453
454 if expand is None:
455 sparse_copy(self.imagepath, target)
456 else:
457 # copy first sectors that may contain bootloader
458 sparse_copy(self.imagepath, target, length=2048 * self._lsector_size)
459
460 # copy source partition table to the target
461 parts = read_ptable(self.imagepath)
462 write_ptable(parts, target)
463
464 # get size of unpartitioned space
465 free = None
466 for line in exec_cmd("{} -F {}".format(self.sfdisk, target)).splitlines():
467 if line.startswith("Unpartitioned space ") and line.endswith("sectors"):
468 free = int(line.split()[-2])
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700469 # Align free space to a 2048 sector boundary. YOCTO #12840.
470 free = free - (free % 2048)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500471 if free is None:
472 raise WicError("Can't get size of unpartitioned space")
473
474 # calculate expanded partitions sizes
475 sizes = {}
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700476 num_auto_resize = 0
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500477 for num, part in enumerate(parts['partitiontable']['partitions'], 1):
478 if num in expand:
479 if expand[num] != 0: # don't resize partition if size is set to 0
480 sectors = expand[num] // self._lsector_size
481 free -= sectors - part['size']
482 part['size'] = sectors
483 sizes[num] = sectors
484 elif part['type'] != 'f':
485 sizes[num] = -1
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700486 num_auto_resize += 1
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500487
488 for num, part in enumerate(parts['partitiontable']['partitions'], 1):
489 if sizes.get(num) == -1:
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700490 part['size'] += free // num_auto_resize
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500491
492 # write resized partition table to the target
493 write_ptable(parts, target)
494
495 # read resized partition table
496 parts = read_ptable(target)
497
498 # copy partitions content
499 for num, part in enumerate(parts['partitiontable']['partitions'], 1):
500 pnum = str(num)
501 fstype = self.partitions[pnum].fstype
502
503 # copy unchanged partition
504 if part['size'] == self.partitions[pnum].size // self._lsector_size:
505 logger.info("copying unchanged partition {}".format(pnum))
506 sparse_copy(self._get_part_image(pnum), target, seek=part['start'] * self._lsector_size)
507 continue
508
509 # resize or re-create partitions
510 if fstype.startswith('ext') or fstype.startswith('fat') or \
511 fstype.startswith('linux-swap'):
512
513 partfname = None
514 with tempfile.NamedTemporaryFile(prefix="wic-part{}-".format(pnum)) as partf:
515 partfname = partf.name
516
517 if fstype.startswith('ext'):
518 logger.info("resizing ext partition {}".format(pnum))
519 partimg = self._get_part_image(pnum)
520 sparse_copy(partimg, partfname)
521 exec_cmd("{} -pf {}".format(self.e2fsck, partfname))
522 exec_cmd("{} {} {}s".format(\
523 self.resize2fs, partfname, part['size']))
524 elif fstype.startswith('fat'):
525 logger.info("copying content of the fat partition {}".format(pnum))
526 with tempfile.TemporaryDirectory(prefix='wic-fatdir-') as tmpdir:
527 # copy content to the temporary directory
528 cmd = "{} -snompi {} :: {}".format(self.mcopy,
529 self._get_part_image(pnum),
530 tmpdir)
531 exec_cmd(cmd)
532 # create new msdos partition
533 label = part.get("name")
534 label_str = "-n {}".format(label) if label else ''
535
536 cmd = "{} {} -C {} {}".format(self.mkdosfs, label_str, partfname,
537 part['size'])
538 exec_cmd(cmd)
539 # copy content from the temporary directory to the new partition
540 cmd = "{} -snompi {} {}/* ::".format(self.mcopy, partfname, tmpdir)
541 exec_cmd(cmd, as_shell=True)
542 elif fstype.startswith('linux-swap'):
543 logger.info("creating swap partition {}".format(pnum))
544 label = part.get("name")
545 label_str = "-L {}".format(label) if label else ''
Andrew Geissler82c905d2020-04-13 13:39:40 -0500546 out = exec_cmd("{} --probe {}".format(self.blkid, self._get_part_image(pnum)))
547 uuid = out[out.index("UUID=\"")+6:out.index("UUID=\"")+42]
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500548 uuid_str = "-U {}".format(uuid) if uuid else ''
549 with open(partfname, 'w') as sparse:
550 os.ftruncate(sparse.fileno(), part['size'] * self._lsector_size)
551 exec_cmd("{} {} {} {}".format(self.mkswap, label_str, uuid_str, partfname))
552 sparse_copy(partfname, target, seek=part['start'] * self._lsector_size)
553 os.unlink(partfname)
554 elif part['type'] != 'f':
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800555 logger.warning("skipping partition {}: unsupported fstype {}".format(pnum, fstype))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500556
557def wic_ls(args, native_sysroot):
558 """List contents of partitioned image or vfat partition."""
559 disk = Disk(args.path.image, native_sysroot)
560 if not args.path.part:
561 if disk.partitions:
562 print('Num Start End Size Fstype')
563 for part in disk.partitions.values():
564 print("{:2d} {:12d} {:12d} {:12d} {}".format(\
565 part.pnum, part.start, part.end,
566 part.size, part.fstype))
567 else:
568 path = args.path.path or '/'
569 print(disk.dir(args.path.part, path))
570
571def wic_cp(args, native_sysroot):
572 """
Andrew Geissler82c905d2020-04-13 13:39:40 -0500573 Copy file or directory to/from the vfat/ext partition of
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500574 partitioned image.
575 """
Andrew Geissler82c905d2020-04-13 13:39:40 -0500576 if isinstance(args.dest, str):
577 disk = Disk(args.src.image, native_sysroot)
578 else:
579 disk = Disk(args.dest.image, native_sysroot)
580 disk.copy(args.src, args.dest)
581
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500582
583def wic_rm(args, native_sysroot):
584 """
585 Remove files or directories from the vfat partition of
586 partitioned image.
587 """
588 disk = Disk(args.path.image, native_sysroot)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500589 disk.remove(args.path.part, args.path.path, args.recursive_delete)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500590
591def wic_write(args, native_sysroot):
592 """
593 Write image to a target device.
594 """
Brad Bishop64c979e2019-11-04 13:55:29 -0500595 disk = Disk(args.image, native_sysroot, ('fat', 'ext', 'linux-swap'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500596 disk.write(args.target, args.expand)
597
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500598def find_canned(scripts_path, file_name):
599 """
600 Find a file either by its path or by name in the canned files dir.
601
602 Return None if not found
603 """
604 if os.path.exists(file_name):
605 return file_name
606
607 layers_canned_wks_dir = build_canned_image_list(scripts_path)
608 for canned_wks_dir in layers_canned_wks_dir:
609 for root, dirs, files in os.walk(canned_wks_dir):
610 for fname in files:
611 if fname == file_name:
612 fullpath = os.path.join(canned_wks_dir, fname)
613 return fullpath
614
615def get_custom_config(boot_file):
616 """
617 Get the custom configuration to be used for the bootloader.
618
619 Return None if the file can't be found.
620 """
621 # Get the scripts path of poky
622 scripts_path = os.path.abspath("%s/../.." % os.path.dirname(__file__))
623
624 cfg_file = find_canned(scripts_path, boot_file)
625 if cfg_file:
626 with open(cfg_file, "r") as f:
627 config = f.read()
628 return config