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 ".zst": "zstd -f", 63 }.get(extension) 64 if not decompressor: 65 raise WicError("Not supported compressor filename extension: %s" % extension) 66 cmd = "%s -dc %s > %s" % (decompressor, src, dst) 67 subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=workdir) 68 69 @classmethod 70 def do_prepare_partition(cls, part, source_params, cr, cr_workdir, 71 oe_builddir, bootimg_dir, kernel_dir, 72 rootfs_dir, native_sysroot): 73 """ 74 Called to do the actual content population for a partition i.e. it 75 'prepares' the partition to be incorporated into the image. 76 """ 77 if not kernel_dir: 78 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") 79 if not kernel_dir: 80 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") 81 82 logger.debug('Kernel dir: %s', kernel_dir) 83 84 if 'file' not in source_params: 85 raise WicError("No file specified") 86 87 if 'unpack' in source_params: 88 img = os.path.join(kernel_dir, source_params['file']) 89 src = os.path.join(cr_workdir, os.path.splitext(source_params['file'])[0]) 90 RawCopyPlugin.do_image_uncompression(img, src, cr_workdir) 91 else: 92 src = os.path.join(kernel_dir, source_params['file']) 93 94 dst = os.path.join(cr_workdir, "%s.%s" % (os.path.basename(source_params['file']), part.lineno)) 95 96 if not os.path.exists(os.path.dirname(dst)): 97 os.makedirs(os.path.dirname(dst)) 98 99 if 'skip' in source_params: 100 sparse_copy(src, dst, skip=int(source_params['skip'])) 101 else: 102 sparse_copy(src, dst) 103 104 # get the size in the right units for kickstart (kB) 105 du_cmd = "du -Lbks %s" % dst 106 out = exec_cmd(du_cmd) 107 filesize = int(out.split()[0]) 108 109 if filesize > part.size: 110 part.size = filesize 111 112 if part.label: 113 RawCopyPlugin.do_image_label(part.fstype, dst, part.label) 114 115 part.source_file = dst 116