Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | # |
| 4 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 5 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | import os |
| 7 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 8 | from wic import WicError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | from wic.pluginbase import SourcePlugin |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 10 | from wic.misc import exec_cmd, get_bitbake_var |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 11 | from wic.filemap import sparse_copy |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 13 | logger = logging.getLogger('wic') |
| 14 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | class RawCopyPlugin(SourcePlugin): |
| 16 | """ |
| 17 | Populate partition content from raw image file. |
| 18 | """ |
| 19 | |
| 20 | name = 'rawcopy' |
| 21 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 22 | @staticmethod |
| 23 | def do_image_label(fstype, dst, label): |
| 24 | if fstype.startswith('ext'): |
| 25 | cmd = 'tune2fs -L %s %s' % (label, dst) |
| 26 | elif fstype in ('msdos', 'vfat'): |
| 27 | cmd = 'dosfslabel %s %s' % (dst, label) |
| 28 | elif fstype == 'btrfs': |
| 29 | cmd = 'btrfs filesystem label %s %s' % (dst, label) |
| 30 | elif fstype == 'swap': |
| 31 | cmd = 'mkswap -L %s %s' % (label, dst) |
| 32 | elif fstype == 'squashfs': |
| 33 | raise WicError("It's not possible to update a squashfs " |
| 34 | "filesystem label '%s'" % (label)) |
| 35 | else: |
| 36 | raise WicError("Cannot update filesystem label: " |
| 37 | "Unknown fstype: '%s'" % (fstype)) |
| 38 | |
| 39 | exec_cmd(cmd) |
| 40 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | @classmethod |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, |
| 43 | oe_builddir, bootimg_dir, kernel_dir, |
| 44 | rootfs_dir, native_sysroot): |
| 45 | """ |
| 46 | Called to do the actual content population for a partition i.e. it |
| 47 | 'prepares' the partition to be incorporated into the image. |
| 48 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 49 | if not kernel_dir: |
| 50 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") |
| 51 | if not kernel_dir: |
| 52 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 54 | logger.debug('Kernel dir: %s', kernel_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | |
| 56 | if 'file' not in source_params: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 57 | raise WicError("No file specified") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 59 | src = os.path.join(kernel_dir, source_params['file']) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 60 | dst = os.path.join(cr_workdir, "%s.%s" % (source_params['file'], part.lineno)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame^] | 62 | if not os.path.exists(os.path.dirname(dst)): |
| 63 | os.makedirs(os.path.dirname(dst)) |
| 64 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | if 'skip' in source_params: |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 66 | sparse_copy(src, dst, skip=int(source_params['skip'])) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 67 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 68 | sparse_copy(src, dst) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | |
| 70 | # get the size in the right units for kickstart (kB) |
| 71 | du_cmd = "du -Lbks %s" % dst |
| 72 | out = exec_cmd(du_cmd) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 73 | filesize = int(out.split()[0]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 75 | if filesize > part.size: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | part.size = filesize |
| 77 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 78 | if part.label: |
| 79 | RawCopyPlugin.do_image_label(part.fstype, dst, part.label) |
| 80 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | part.source_file = dst |