1# 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
18import logging
19import os
20
21from wic import WicError
22from wic.pluginbase import SourcePlugin
23from wic.misc import exec_cmd, get_bitbake_var
24from wic.filemap import sparse_copy
25
26logger = logging.getLogger('wic')
27
28class RawCopyPlugin(SourcePlugin):
29    """
30    Populate partition content from raw image file.
31    """
32
33    name = 'rawcopy'
34
35    @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
54    @classmethod
55    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        """
62        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")
66
67        logger.debug('Kernel dir: %s', kernel_dir)
68
69        if 'file' not in source_params:
70            raise WicError("No file specified")
71
72        src = os.path.join(kernel_dir, source_params['file'])
73        dst = os.path.join(cr_workdir, "%s.%s" % (source_params['file'], part.lineno))
74
75        if 'skip' in source_params:
76            sparse_copy(src, dst, skip=int(source_params['skip']))
77        else:
78            sparse_copy(src, dst)
79
80        # get the size in the right units for kickstart (kB)
81        du_cmd = "du -Lbks %s" % dst
82        out = exec_cmd(du_cmd)
83        filesize = int(out.split()[0])
84
85        if filesize > part.size:
86            part.size = filesize
87
88        if part.label:
89            RawCopyPlugin.do_image_label(part.fstype, dst, part.label)
90
91        part.source_file = dst
92