blob: 82970ce51bc1912702754ad9ff32e9d03cea3b38 [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
7
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008from wic import WicError
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050010from wic.misc import exec_cmd, get_bitbake_var
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011from wic.filemap import sparse_copy
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013logger = logging.getLogger('wic')
14
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015class RawCopyPlugin(SourcePlugin):
16 """
17 Populate partition content from raw image file.
18 """
19
20 name = 'rawcopy'
21
Brad Bishopd7bf8c12018-02-25 22:55:05 -050022 @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 Williamsc124f4f2015-09-15 14:41:29 -050041 @classmethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042 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 Bishop6e60e8b2018-02-01 10:27:11 -050049 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 Williamsc124f4f2015-09-15 14:41:29 -050053
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054 logger.debug('Kernel dir: %s', kernel_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055
56 if 'file' not in source_params:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050057 raise WicError("No file specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058
Brad Bishop6e60e8b2018-02-01 10:27:11 -050059 src = os.path.join(kernel_dir, source_params['file'])
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060 dst = os.path.join(cr_workdir, "%s.%s" % (source_params['file'], part.lineno))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061
Brad Bishop64c979e2019-11-04 13:55:29 -050062 if not os.path.exists(os.path.dirname(dst)):
63 os.makedirs(os.path.dirname(dst))
64
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 if 'skip' in source_params:
Brad Bishop37a0e4d2017-12-04 01:01:44 -050066 sparse_copy(src, dst, skip=int(source_params['skip']))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050067 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060068 sparse_copy(src, dst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069
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 Bishop37a0e4d2017-12-04 01:01:44 -050073 filesize = int(out.split()[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
Brad Bishop37a0e4d2017-12-04 01:01:44 -050075 if filesize > part.size:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 part.size = filesize
77
Brad Bishopd7bf8c12018-02-25 22:55:05 -050078 if part.label:
79 RawCopyPlugin.do_image_label(part.fstype, dst, part.label)
80
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 part.source_file = dst