blob: 7c90cd3cf82bacb06a0128b89c3165c0b168d35b [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
Brad Bishopc342db32019-05-15 21:57:59 -04002# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003#
4
Brad Bishop6e60e8b2018-02-01 10:27:11 -05005import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006import os
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00007import signal
8import subprocess
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010from wic import WicError
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012from wic.misc import exec_cmd, get_bitbake_var
Patrick Williamsc0f7c042017-02-23 20:41:17 -060013from wic.filemap import sparse_copy
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014
Brad Bishop6e60e8b2018-02-01 10:27:11 -050015logger = logging.getLogger('wic')
16
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017class RawCopyPlugin(SourcePlugin):
18 """
19 Populate partition content from raw image file.
20 """
21
22 name = 'rawcopy'
23
Brad Bishopd7bf8c12018-02-25 22:55:05 -050024 @staticmethod
25 def do_image_label(fstype, dst, label):
26 if fstype.startswith('ext'):
27 cmd = 'tune2fs -L %s %s' % (label, dst)
28 elif fstype in ('msdos', 'vfat'):
29 cmd = 'dosfslabel %s %s' % (dst, label)
30 elif fstype == 'btrfs':
31 cmd = 'btrfs filesystem label %s %s' % (dst, label)
32 elif fstype == 'swap':
33 cmd = 'mkswap -L %s %s' % (label, dst)
William A. Kennington IIIac69b482021-06-02 12:28:27 -070034 elif fstype in ('squashfs', 'erofs'):
35 raise WicError("It's not possible to update a %s "
36 "filesystem label '%s'" % (fstype, label))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050037 else:
38 raise WicError("Cannot update filesystem label: "
39 "Unknown fstype: '%s'" % (fstype))
40
41 exec_cmd(cmd)
42
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000043 @staticmethod
44 def do_image_uncompression(src, dst, workdir):
45 def subprocess_setup():
46 # Python installs a SIGPIPE handler by default. This is usually not what
47 # non-Python subprocesses expect.
48 # SIGPIPE errors are known issues with gzip/bash
49 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
50
51 extension = os.path.splitext(src)[1]
52 decompressor = {
53 ".bz2": "bzip2",
54 ".gz": "gzip",
55 ".xz": "xz"
56 }.get(extension)
57 if not decompressor:
58 raise WicError("Not supported compressor filename extension: %s" % extension)
59 cmd = "%s -dc %s > %s" % (decompressor, src, dst)
60 subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=workdir)
61
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 @classmethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 def do_prepare_partition(cls, part, source_params, cr, cr_workdir,
64 oe_builddir, bootimg_dir, kernel_dir,
65 rootfs_dir, native_sysroot):
66 """
67 Called to do the actual content population for a partition i.e. it
68 'prepares' the partition to be incorporated into the image.
69 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 if not kernel_dir:
71 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
72 if not kernel_dir:
73 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 logger.debug('Kernel dir: %s', kernel_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076
77 if 'file' not in source_params:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050078 raise WicError("No file specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000080 if 'unpack' in source_params:
81 img = os.path.join(kernel_dir, source_params['file'])
82 src = os.path.join(cr_workdir, os.path.splitext(source_params['file'])[0])
83 RawCopyPlugin.do_image_uncompression(img, src, cr_workdir)
84 else:
85 src = os.path.join(kernel_dir, source_params['file'])
86
Andrew Geissler82c905d2020-04-13 13:39:40 -050087 dst = os.path.join(cr_workdir, "%s.%s" % (os.path.basename(source_params['file']), part.lineno))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088
Brad Bishop64c979e2019-11-04 13:55:29 -050089 if not os.path.exists(os.path.dirname(dst)):
90 os.makedirs(os.path.dirname(dst))
91
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 if 'skip' in source_params:
Brad Bishop37a0e4d2017-12-04 01:01:44 -050093 sparse_copy(src, dst, skip=int(source_params['skip']))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050094 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060095 sparse_copy(src, dst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
97 # get the size in the right units for kickstart (kB)
98 du_cmd = "du -Lbks %s" % dst
99 out = exec_cmd(du_cmd)
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500100 filesize = int(out.split()[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500102 if filesize > part.size:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 part.size = filesize
104
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105 if part.label:
106 RawCopyPlugin.do_image_label(part.fstype, dst, part.label)
107
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 part.source_file = dst