1# 2# Copyright OpenEmbedded Contributors 3# 4# SPDX-License-Identifier: GPL-2.0-only 5# 6 7import logging 8import os 9import signal 10import subprocess 11 12from wic import WicError 13from wic.pluginbase import SourcePlugin 14from wic.misc import exec_cmd, get_bitbake_var 15from wic.filemap import sparse_copy 16 17logger = logging.getLogger('wic') 18 19class RawCopyPlugin(SourcePlugin): 20 """ 21 Populate partition content from raw image file. 22 """ 23 24 name = 'rawcopy' 25 26 @staticmethod 27 def do_image_label(fstype, dst, label): 28 # don't create label when fstype is none 29 if fstype == 'none': 30 return 31 32 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) 40 elif fstype in ('squashfs', 'erofs'): 41 raise WicError("It's not possible to update a %s " 42 "filesystem label '%s'" % (fstype, label)) 43 else: 44 raise WicError("Cannot update filesystem label: " 45 "Unknown fstype: '%s'" % (fstype)) 46 47 exec_cmd(cmd) 48 49 @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 68 @classmethod 69 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 """ 76 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") 80 81 logger.debug('Kernel dir: %s', kernel_dir) 82 83 if 'file' not in source_params: 84 raise WicError("No file specified") 85 86 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 93 dst = os.path.join(cr_workdir, "%s.%s" % (os.path.basename(source_params['file']), part.lineno)) 94 95 if not os.path.exists(os.path.dirname(dst)): 96 os.makedirs(os.path.dirname(dst)) 97 98 if 'skip' in source_params: 99 sparse_copy(src, dst, skip=int(source_params['skip'])) 100 else: 101 sparse_copy(src, dst) 102 103 # get the size in the right units for kickstart (kB) 104 du_cmd = "du -Lbks %s" % dst 105 out = exec_cmd(du_cmd) 106 filesize = int(out.split()[0]) 107 108 if filesize > part.size: 109 part.size = filesize 110 111 if part.label: 112 RawCopyPlugin.do_image_label(part.fstype, dst, part.label) 113 114 part.source_file = dst 115