Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 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 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | import os |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 9 | import signal |
| 10 | import subprocess |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 12 | from wic import WicError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | from wic.pluginbase import SourcePlugin |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 14 | from wic.misc import exec_cmd, get_bitbake_var |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | from wic.filemap import sparse_copy |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 17 | logger = logging.getLogger('wic') |
| 18 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | class RawCopyPlugin(SourcePlugin): |
| 20 | """ |
| 21 | Populate partition content from raw image file. |
| 22 | """ |
| 23 | |
| 24 | name = 'rawcopy' |
| 25 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 26 | @staticmethod |
| 27 | def do_image_label(fstype, dst, label): |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 28 | # don't create label when fstype is none |
| 29 | if fstype == 'none': |
| 30 | return |
| 31 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 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) |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 40 | elif fstype in ('squashfs', 'erofs'): |
| 41 | raise WicError("It's not possible to update a %s " |
| 42 | "filesystem label '%s'" % (fstype, label)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 43 | else: |
| 44 | raise WicError("Cannot update filesystem label: " |
| 45 | "Unknown fstype: '%s'" % (fstype)) |
| 46 | |
| 47 | exec_cmd(cmd) |
| 48 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 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 | }.get(extension) |
| 63 | if not decompressor: |
| 64 | raise WicError("Not supported compressor filename extension: %s" % extension) |
| 65 | cmd = "%s -dc %s > %s" % (decompressor, src, dst) |
| 66 | subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=workdir) |
| 67 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | @classmethod |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, |
| 70 | oe_builddir, bootimg_dir, kernel_dir, |
| 71 | rootfs_dir, native_sysroot): |
| 72 | """ |
| 73 | Called to do the actual content population for a partition i.e. it |
| 74 | 'prepares' the partition to be incorporated into the image. |
| 75 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 76 | if not kernel_dir: |
| 77 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") |
| 78 | if not kernel_dir: |
| 79 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 81 | logger.debug('Kernel dir: %s', kernel_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | |
| 83 | if 'file' not in source_params: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 84 | raise WicError("No file specified") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 86 | if 'unpack' in source_params: |
| 87 | img = os.path.join(kernel_dir, source_params['file']) |
| 88 | src = os.path.join(cr_workdir, os.path.splitext(source_params['file'])[0]) |
| 89 | RawCopyPlugin.do_image_uncompression(img, src, cr_workdir) |
| 90 | else: |
| 91 | src = os.path.join(kernel_dir, source_params['file']) |
| 92 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 93 | dst = os.path.join(cr_workdir, "%s.%s" % (os.path.basename(source_params['file']), part.lineno)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 95 | if not os.path.exists(os.path.dirname(dst)): |
| 96 | os.makedirs(os.path.dirname(dst)) |
| 97 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | if 'skip' in source_params: |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 99 | sparse_copy(src, dst, skip=int(source_params['skip'])) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 101 | sparse_copy(src, dst) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | |
| 103 | # get the size in the right units for kickstart (kB) |
| 104 | du_cmd = "du -Lbks %s" % dst |
| 105 | out = exec_cmd(du_cmd) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 106 | filesize = int(out.split()[0]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 108 | if filesize > part.size: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | part.size = filesize |
| 110 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 111 | if part.label: |
| 112 | RawCopyPlugin.do_image_label(part.fstype, dst, part.label) |
| 113 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | part.source_file = dst |