blob: ccf332554e5d1a72f4972624dd1a1e1fa8add0de [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005#
6
Brad Bishop6e60e8b2018-02-01 10:27:11 -05007import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008import os
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00009import signal
10import subprocess
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
Brad Bishop6e60e8b2018-02-01 10:27:11 -050012from wic import WicError
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050014from wic.misc import exec_cmd, get_bitbake_var
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015from wic.filemap import sparse_copy
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017logger = logging.getLogger('wic')
18
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019class RawCopyPlugin(SourcePlugin):
20 """
21 Populate partition content from raw image file.
22 """
23
24 name = 'rawcopy'
25
Brad Bishopd7bf8c12018-02-25 22:55:05 -050026 @staticmethod
27 def do_image_label(fstype, dst, label):
Patrick Williams92b42cb2022-09-03 06:53:57 -050028 # don't create label when fstype is none
29 if fstype == 'none':
30 return
31
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032 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 IIIac69b482021-06-02 12:28:27 -070040 elif fstype in ('squashfs', 'erofs'):
41 raise WicError("It's not possible to update a %s "
42 "filesystem label '%s'" % (fstype, label))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043 else:
44 raise WicError("Cannot update filesystem label: "
45 "Unknown fstype: '%s'" % (fstype))
46
47 exec_cmd(cmd)
48
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000049 @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 Williamsc124f4f2015-09-15 14:41:29 -050068 @classmethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 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 Bishop6e60e8b2018-02-01 10:27:11 -050076 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 Williamsc124f4f2015-09-15 14:41:29 -050080
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 logger.debug('Kernel dir: %s', kernel_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082
83 if 'file' not in source_params:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 raise WicError("No file specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000086 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 Geissler82c905d2020-04-13 13:39:40 -050093 dst = os.path.join(cr_workdir, "%s.%s" % (os.path.basename(source_params['file']), part.lineno))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094
Brad Bishop64c979e2019-11-04 13:55:29 -050095 if not os.path.exists(os.path.dirname(dst)):
96 os.makedirs(os.path.dirname(dst))
97
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 if 'skip' in source_params:
Brad Bishop37a0e4d2017-12-04 01:01:44 -050099 sparse_copy(src, dst, skip=int(source_params['skip']))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500100 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600101 sparse_copy(src, dst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
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 Bishop37a0e4d2017-12-04 01:01:44 -0500106 filesize = int(out.split()[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500108 if filesize > part.size:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 part.size = filesize
110
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 if part.label:
112 RawCopyPlugin.do_image_label(part.fstype, dst, part.label)
113
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 part.source_file = dst