blob: e86398ac8fbabf36efa4265b38c3f1e9ee8e1eb9 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License version 2 as
6# published by the Free Software Foundation.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16#
17
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019import os
20
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021from wic import WicError
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023from wic.misc import exec_cmd, get_bitbake_var
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024from wic.filemap import sparse_copy
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
Brad Bishop6e60e8b2018-02-01 10:27:11 -050026logger = logging.getLogger('wic')
27
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028class RawCopyPlugin(SourcePlugin):
29 """
30 Populate partition content from raw image file.
31 """
32
33 name = 'rawcopy'
34
Brad Bishopd7bf8c12018-02-25 22:55:05 -050035 @staticmethod
36 def do_image_label(fstype, dst, label):
37 if fstype.startswith('ext'):
38 cmd = 'tune2fs -L %s %s' % (label, dst)
39 elif fstype in ('msdos', 'vfat'):
40 cmd = 'dosfslabel %s %s' % (dst, label)
41 elif fstype == 'btrfs':
42 cmd = 'btrfs filesystem label %s %s' % (dst, label)
43 elif fstype == 'swap':
44 cmd = 'mkswap -L %s %s' % (label, dst)
45 elif fstype == 'squashfs':
46 raise WicError("It's not possible to update a squashfs "
47 "filesystem label '%s'" % (label))
48 else:
49 raise WicError("Cannot update filesystem label: "
50 "Unknown fstype: '%s'" % (fstype))
51
52 exec_cmd(cmd)
53
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054 @classmethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 def do_prepare_partition(cls, part, source_params, cr, cr_workdir,
56 oe_builddir, bootimg_dir, kernel_dir,
57 rootfs_dir, native_sysroot):
58 """
59 Called to do the actual content population for a partition i.e. it
60 'prepares' the partition to be incorporated into the image.
61 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062 if not kernel_dir:
63 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
64 if not kernel_dir:
65 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066
Brad Bishop6e60e8b2018-02-01 10:27:11 -050067 logger.debug('Kernel dir: %s', kernel_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
69 if 'file' not in source_params:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 raise WicError("No file specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071
Brad Bishop6e60e8b2018-02-01 10:27:11 -050072 src = os.path.join(kernel_dir, source_params['file'])
Patrick Williamsc0f7c042017-02-23 20:41:17 -060073 dst = os.path.join(cr_workdir, "%s.%s" % (source_params['file'], part.lineno))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
75 if 'skip' in source_params:
Brad Bishop37a0e4d2017-12-04 01:01:44 -050076 sparse_copy(src, dst, skip=int(source_params['skip']))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050077 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060078 sparse_copy(src, dst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
80 # get the size in the right units for kickstart (kB)
81 du_cmd = "du -Lbks %s" % dst
82 out = exec_cmd(du_cmd)
Brad Bishop37a0e4d2017-12-04 01:01:44 -050083 filesize = int(out.split()[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084
Brad Bishop37a0e4d2017-12-04 01:01:44 -050085 if filesize > part.size:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086 part.size = filesize
87
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 if part.label:
89 RawCopyPlugin.do_image_label(part.fstype, dst, part.label)
90
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 part.source_file = dst