xref: /openbmc/openbmc/poky/meta/classes-recipe/image.bbclass (revision c9537f57ab488bf5d90132917b0184e2527970a5)
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7IMAGE_CLASSES ??= ""
8
9# rootfs bootstrap install
10# warning -  image-container resets this
11ROOTFS_BOOTSTRAP_INSTALL = "run-postinsts"
12
13# Handle inherits of any of the image classes we need
14IMGCLASSES = "rootfs_${IMAGE_PKGTYPE} image_types ${IMAGE_CLASSES}"
15# Only Linux SDKs support populate_sdk_ext, fall back to populate_sdk_base
16# in the non-Linux SDK_OS case, such as mingw32
17inherit populate_sdk_base
18IMGCLASSES += "${@['', 'populate_sdk_ext']['linux' in d.getVar("SDK_OS")]}"
19IMGCLASSES += "${@bb.utils.contains_any('IMAGE_FSTYPES', 'live iso hddimg', 'image-live', '', d)}"
20IMGCLASSES += "${@bb.utils.contains('IMAGE_FSTYPES', 'container', 'image-container', '', d)}"
21IMGCLASSES += "image_types_wic"
22IMGCLASSES += "rootfs-postcommands"
23IMGCLASSES += "image-postinst-intercepts"
24IMGCLASSES += "overlayfs-etc"
25inherit_defer ${IMGCLASSES}
26
27TOOLCHAIN_TARGET_TASK += "${PACKAGE_INSTALL}"
28TOOLCHAIN_TARGET_TASK_ATTEMPTONLY += "${PACKAGE_INSTALL_ATTEMPTONLY}"
29POPULATE_SDK_POST_TARGET_COMMAND += "rootfs_sysroot_relativelinks"
30
31LICENSE ?= "MIT"
32PACKAGES = ""
33DEPENDS += "depmodwrapper-cross cross-localedef-native"
34RDEPENDS += "${PACKAGE_INSTALL} ${LINGUAS_INSTALL} ${IMAGE_INSTALL_DEBUGFS}"
35RRECOMMENDS += "${PACKAGE_INSTALL_ATTEMPTONLY}"
36PATH:prepend = "${@":".join(all_multilib_tune_values(d, 'STAGING_BINDIR_CROSS').split())}:"
37
38INHIBIT_DEFAULT_DEPS = "1"
39
40# IMAGE_FEATURES may contain any available package group
41IMAGE_FEATURES ?= ""
42IMAGE_FEATURES[type] = "list"
43IMAGE_FEATURES[validitems] += "read-only-rootfs read-only-rootfs-delayed-postinsts stateless-rootfs empty-root-password allow-empty-password allow-root-login serial-autologin-root post-install-logging overlayfs-etc"
44
45# Generate companion debugfs?
46IMAGE_GEN_DEBUGFS ?= "0"
47
48# These packages will be installed as additional into debug rootfs
49IMAGE_INSTALL_DEBUGFS ?= ""
50
51# These packages will be removed from a read-only rootfs after all other
52# packages have been installed
53ROOTFS_RO_UNNEEDED ??= "update-rc.d base-passwd shadow ${VIRTUAL-RUNTIME_update-alternatives} ${ROOTFS_BOOTSTRAP_INSTALL}"
54
55# packages to install from features
56FEATURE_INSTALL = "${@' '.join(oe.packagegroup.required_packages(oe.data.typed_value('IMAGE_FEATURES', d), d))}"
57FEATURE_INSTALL[vardepvalue] = "${FEATURE_INSTALL}"
58FEATURE_INSTALL_OPTIONAL = "${@' '.join(oe.packagegroup.optional_packages(oe.data.typed_value('IMAGE_FEATURES', d), d))}"
59FEATURE_INSTALL_OPTIONAL[vardepvalue] = "${FEATURE_INSTALL_OPTIONAL}"
60
61# Define some very basic feature package groups
62FEATURE_PACKAGES_package-management = "${ROOTFS_PKGMANAGE}"
63SPLASH ?= "${@bb.utils.contains("MACHINE_FEATURES", "screen", "psplash", "", d)}"
64FEATURE_PACKAGES_splash = "${SPLASH}"
65
66IMAGE_INSTALL_COMPLEMENTARY = '${@complementary_globs("IMAGE_FEATURES", d)}'
67
68def check_image_features(d):
69    valid_features = (d.getVarFlag('IMAGE_FEATURES', 'validitems') or "").split()
70    valid_features += d.getVarFlags('COMPLEMENTARY_GLOB').keys()
71    for var in d:
72       if var.startswith("FEATURE_PACKAGES_"):
73           valid_features.append(var[17:])
74    valid_features.sort()
75
76    features = set(oe.data.typed_value('IMAGE_FEATURES', d))
77    for feature in features:
78        if feature not in valid_features:
79            if bb.utils.contains('EXTRA_IMAGE_FEATURES', feature, True, False, d):
80                raise bb.parse.SkipRecipe("'%s' in IMAGE_FEATURES (added via EXTRA_IMAGE_FEATURES) is not a valid image feature. Valid features: %s" % (feature, ' '.join(valid_features)))
81            else:
82                raise bb.parse.SkipRecipe("'%s' in IMAGE_FEATURES is not a valid image feature. Valid features: %s" % (feature, ' '.join(valid_features)))
83
84IMAGE_INSTALL ?= ""
85IMAGE_INSTALL[type] = "list"
86export PACKAGE_INSTALL ?= "${IMAGE_INSTALL} ${ROOTFS_BOOTSTRAP_INSTALL} ${FEATURE_INSTALL}"
87PACKAGE_INSTALL_ATTEMPTONLY ?= "${FEATURE_INSTALL_OPTIONAL}"
88
89IMGDEPLOYDIR = "${WORKDIR}/deploy-${PN}-image-complete"
90
91IMGMANIFESTDIR = "${WORKDIR}/image-task-manifest"
92
93IMAGE_OUTPUT_MANIFEST_DIR = "${WORKDIR}/deploy-image-output-manifest"
94IMAGE_OUTPUT_MANIFEST = "${IMAGE_OUTPUT_MANIFEST_DIR}/manifest.json"
95
96# Images are generally built explicitly, do not need to be part of world.
97EXCLUDE_FROM_WORLD = "1"
98
99USE_DEVFS ?= "1"
100USE_DEPMOD ?= "1"
101
102PID = "${@os.getpid()}"
103
104PACKAGE_ARCH = "${MACHINE_ARCH}"
105SSTATE_ARCHS_TUNEPKG = "${@all_multilib_tune_values(d, 'TUNE_PKGARCH')}"
106
107LDCONFIGDEPEND ?= "ldconfig-native:do_populate_sysroot"
108LDCONFIGDEPEND:libc-musl = ""
109
110# This is needed to have depmod data in PKGDATA_DIR,
111# but if you're building small initramfs image
112# e.g. to include it in your kernel, you probably
113# don't want this dependency, which is causing dependency loop
114KERNELDEPMODDEPEND ?= "virtual/kernel:do_packagedata"
115
116do_rootfs[depends] += " \
117    makedevs-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot ${LDCONFIGDEPEND} \
118    virtual/update-alternatives-native:do_populate_sysroot update-rc.d-native:do_populate_sysroot \
119    ${KERNELDEPMODDEPEND} \
120"
121do_rootfs[recrdeptask] += "do_packagedata"
122
123def rootfs_command_variables(d):
124    return ['ROOTFS_POSTPROCESS_COMMAND','ROOTFS_PREPROCESS_COMMAND','ROOTFS_POSTINSTALL_COMMAND','ROOTFS_POSTUNINSTALL_COMMAND','OPKG_PREPROCESS_COMMANDS','OPKG_POSTPROCESS_COMMANDS','IMAGE_POSTPROCESS_COMMAND',
125            'IMAGE_PREPROCESS_COMMAND','RPM_PREPROCESS_COMMANDS','RPM_POSTPROCESS_COMMANDS','DEB_PREPROCESS_COMMANDS','DEB_POSTPROCESS_COMMANDS']
126
127python () {
128    variables = rootfs_command_variables(d)
129    for var in variables:
130        d.setVarFlag(var, 'vardeps', d.getVar(var))
131}
132
133def rootfs_variables(d):
134    from oe.rootfs import variable_depends
135    variables = ['IMAGE_DEVICE_TABLE','IMAGE_DEVICE_TABLES','BUILD_IMAGES_FROM_FEEDS','IMAGE_TYPES_MASKED','IMAGE_ROOTFS_ALIGNMENT','IMAGE_OVERHEAD_FACTOR','IMAGE_ROOTFS_SIZE','IMAGE_ROOTFS_EXTRA_SPACE',
136                 'IMAGE_ROOTFS_MAXSIZE','IMAGE_NAME','IMAGE_LINK_NAME','IMAGE_MANIFEST','DEPLOY_DIR_IMAGE','IMAGE_FSTYPES','IMAGE_INSTALL_COMPLEMENTARY','IMAGE_LINGUAS', 'IMAGE_LINGUAS_COMPLEMENTARY', 'IMAGE_LOCALES_ARCHIVE',
137                 'MULTILIBRE_ALLOW_REP','MULTILIB_TEMP_ROOTFS','MULTILIB_VARIANTS','MULTILIBS','ALL_MULTILIB_PACKAGE_ARCHS','MULTILIB_GLOBAL_VARIANTS','BAD_RECOMMENDATIONS','NO_RECOMMENDATIONS',
138                 'PACKAGE_ARCHS','PACKAGE_CLASSES','TARGET_VENDOR','TARGET_ARCH','TARGET_OS','OVERRIDES','BBEXTENDVARIANT','FEED_DEPLOYDIR_BASE_URI','INTERCEPT_DIR','USE_DEVFS',
139                 'CONVERSIONTYPES', 'IMAGE_GEN_DEBUGFS', 'ROOTFS_RO_UNNEEDED', 'IMGDEPLOYDIR', 'PACKAGE_EXCLUDE_COMPLEMENTARY', 'REPRODUCIBLE_TIMESTAMP_ROOTFS', 'IMAGE_INSTALL_DEBUGFS']
140    variables.extend(rootfs_command_variables(d))
141    variables.extend(variable_depends(d))
142    return " ".join(variables)
143
144do_rootfs[vardeps] += "${@rootfs_variables(d)}"
145
146# This is needed to have kernel image in DEPLOY_DIR.
147# This follows many common usecases and user expectations.
148# But if you are building an image which doesn't need the kernel image at all,
149# you can unset this variable manually.
150KERNEL_DEPLOY_DEPEND ?= "virtual/kernel:do_deploy"
151do_build[depends] += "${KERNEL_DEPLOY_DEPEND}"
152
153
154python () {
155    def extraimage_getdepends(task):
156        deps = ""
157        for dep in (d.getVar('EXTRA_IMAGEDEPENDS') or "").split():
158            if ":" in dep:
159                deps += " %s " % (dep)
160            else:
161                deps += " %s:%s" % (dep, task)
162        return deps
163
164    d.appendVarFlag('do_image_complete', 'depends', extraimage_getdepends('do_populate_sysroot'))
165
166    deps = " " + imagetypes_getdepends(d)
167    d.appendVarFlag('do_rootfs', 'depends', deps)
168
169    #process IMAGE_FEATURES, we must do this before runtime_mapping_rename
170    #Check for replaces image features
171    features = set(oe.data.typed_value('IMAGE_FEATURES', d))
172    remain_features = features.copy()
173    for feature in features:
174        replaces = set((d.getVar("IMAGE_FEATURES_REPLACES_%s" % feature) or "").split())
175        remain_features -= replaces
176
177    #Check for conflict image features
178    for feature in remain_features:
179        conflicts = set((d.getVar("IMAGE_FEATURES_CONFLICTS_%s" % feature) or "").split())
180        temp = conflicts & remain_features
181        if temp:
182            bb.fatal("%s contains conflicting IMAGE_FEATURES %s %s" % (d.getVar('PN'), feature, ' '.join(list(temp))))
183
184    d.setVar('IMAGE_FEATURES', ' '.join(sorted(list(remain_features))))
185
186    check_image_features(d)
187}
188
189IMAGE_POSTPROCESS_COMMAND ?= ""
190
191IMAGE_LINGUAS ??= ""
192
193LINGUAS_INSTALL ?= "${@" ".join(map(lambda s: "locale-base-%s" % s, d.getVar('IMAGE_LINGUAS').split()))}"
194
195# per default create a locale archive
196IMAGE_LOCALES_ARCHIVE ?= '1'
197
198# Prefer image, but use the fallback files for lookups if the image ones
199# aren't yet available.
200PSEUDO_PASSWD = "${IMAGE_ROOTFS}:${STAGING_DIR_NATIVE}"
201
202PACKAGE_EXCLUDE ??= ""
203PACKAGE_EXCLUDE[type] = "list"
204
205fakeroot python do_rootfs () {
206    from oe.rootfs import create_rootfs
207    from oe.manifest import create_manifest
208    import logging
209    import oe.packagedata
210
211    logger = d.getVar('BB_TASK_LOGGER', False)
212    if logger:
213        logcatcher = bb.utils.LogCatcher()
214        logger.addHandler(logcatcher)
215    else:
216        logcatcher = None
217
218    # NOTE: if you add, remove or significantly refactor the stages of this
219    # process then you should recalculate the weightings here. This is quite
220    # easy to do - just change the MultiStageProgressReporter line temporarily
221    # to pass debug=True as the last parameter and you'll get a printout of
222    # the weightings as well as a map to the lines where next_stage() was
223    # called. Of course this isn't critical, but it helps to keep the progress
224    # reporting accurate.
225    stage_weights = [1, 203, 354, 186, 65, 4228, 1, 353, 49, 330, 382, 23, 1]
226    progress_reporter = bb.progress.MultiStageProgressReporter(d, stage_weights)
227    progress_reporter.next_stage()
228
229    # Handle package exclusions
230    excl_pkgs = d.getVar("PACKAGE_EXCLUDE").split()
231    inst_pkgs = d.getVar("PACKAGE_INSTALL").split()
232    inst_attempt_pkgs = d.getVar("PACKAGE_INSTALL_ATTEMPTONLY").split()
233
234    d.setVar('PACKAGE_INSTALL_ORIG', ' '.join(inst_pkgs))
235    d.setVar('PACKAGE_INSTALL_ATTEMPTONLY', ' '.join(inst_attempt_pkgs))
236
237    for pkg in excl_pkgs:
238        if pkg in inst_pkgs:
239            bb.warn("Package %s, set to be excluded, is in %s PACKAGE_INSTALL (%s).  It will be removed from the list." % (pkg, d.getVar('PN'), inst_pkgs))
240            inst_pkgs.remove(pkg)
241
242        if pkg in inst_attempt_pkgs:
243            bb.warn("Package %s, set to be excluded, is in %s PACKAGE_INSTALL_ATTEMPTONLY (%s).  It will be removed from the list." % (pkg, d.getVar('PN'), inst_pkgs))
244            inst_attempt_pkgs.remove(pkg)
245
246    d.setVar("PACKAGE_INSTALL", ' '.join(inst_pkgs))
247    d.setVar("PACKAGE_INSTALL_ATTEMPTONLY", ' '.join(inst_attempt_pkgs))
248
249    # Ensure we handle package name remapping
250    # We have to delay the runtime_mapping_rename until just before rootfs runs
251    # otherwise, the multilib renaming could step in and squash any fixups that
252    # may have occurred.
253    pn = d.getVar('PN')
254    oe.packagedata.runtime_mapping_rename("PACKAGE_INSTALL", pn, d)
255    oe.packagedata.runtime_mapping_rename("PACKAGE_INSTALL_ATTEMPTONLY", pn, d)
256    oe.packagedata.runtime_mapping_rename("BAD_RECOMMENDATIONS", pn, d)
257
258    # Generate the initial manifest
259    create_manifest(d)
260
261    progress_reporter.next_stage()
262
263    # generate rootfs
264    d.setVarFlag('REPRODUCIBLE_TIMESTAMP_ROOTFS', 'export', '1')
265    create_rootfs(d, progress_reporter=progress_reporter, logcatcher=logcatcher)
266
267    progress_reporter.finish()
268}
269do_rootfs[dirs] = "${TOPDIR}"
270do_rootfs[cleandirs] += "${IMAGE_ROOTFS} ${IMGDEPLOYDIR} ${S}"
271do_rootfs[file-checksums] += "${POSTINST_INTERCEPT_CHECKSUMS}"
272addtask rootfs after do_prepare_recipe_sysroot
273
274fakeroot python do_image () {
275    from oe.utils import execute_pre_post_process
276
277    d.setVarFlag('REPRODUCIBLE_TIMESTAMP_ROOTFS', 'export', '1')
278    pre_process_cmds = d.getVar("IMAGE_PREPROCESS_COMMAND")
279
280    execute_pre_post_process(d, pre_process_cmds)
281}
282do_image[dirs] = "${TOPDIR}"
283do_image[cleandirs] += "${IMGMANIFESTDIR}"
284addtask do_image after do_rootfs
285
286fakeroot python do_image_complete () {
287    from oe.utils import execute_pre_post_process
288    from pathlib import Path
289    import json
290
291    post_process_cmds = d.getVar("IMAGE_POSTPROCESS_COMMAND")
292
293    execute_pre_post_process(d, post_process_cmds)
294
295    image_manifest_dir = Path(d.getVar('IMGMANIFESTDIR'))
296
297    data = []
298
299    for manifest_path in image_manifest_dir.glob("*.json"):
300        with manifest_path.open("r") as f:
301            data.extend(json.load(f))
302
303    with open(d.getVar("IMAGE_OUTPUT_MANIFEST"), "w") as f:
304        json.dump(data, f)
305}
306do_image_complete[dirs] = "${TOPDIR}"
307SSTATETASKS += "do_image_complete"
308SSTATE_SKIP_CREATION:task-image-complete = '1'
309do_image_complete[sstate-inputdirs] = "${IMGDEPLOYDIR}"
310do_image_complete[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
311do_image_complete[stamp-extra-info] = "${MACHINE_ARCH}"
312do_image_complete[sstate-plaindirs] += "${IMAGE_OUTPUT_MANIFEST_DIR}"
313do_image_complete[dirs] += "${IMAGE_OUTPUT_MANIFEST_DIR}"
314addtask do_image_complete after do_image before do_build
315python do_image_complete_setscene () {
316    sstate_setscene(d)
317}
318addtask do_image_complete_setscene
319
320# Add image-level QA/sanity checks to IMAGE_QA_COMMANDS
321#
322# IMAGE_QA_COMMANDS += " \
323#     image_check_everything_ok \
324# "
325#
326# This task runs all functions in IMAGE_QA_COMMANDS after the rootfs
327# construction has completed in order to validate the resulting image.
328#
329# The functions should use ${IMAGE_ROOTFS} to find the unpacked rootfs
330# directory, which if QA passes will be the basis for the images.
331#
332# The functions are expected to call oe.qa.handle_error() to report any
333# problems.
334fakeroot python do_image_qa () {
335    qa_cmds = (d.getVar('IMAGE_QA_COMMANDS') or '').split()
336
337    for cmd in qa_cmds:
338        bb.build.exec_func(cmd, d)
339
340    oe.qa.exit_if_errors(d)
341}
342addtask do_image_qa after do_rootfs before do_image
343
344SSTATETASKS += "do_image_qa"
345SSTATE_SKIP_CREATION:task-image-qa = '1'
346do_image_qa[sstate-inputdirs] = ""
347do_image_qa[sstate-outputdirs] = ""
348python do_image_qa_setscene () {
349    sstate_setscene(d)
350}
351addtask do_image_qa_setscene
352
353def setup_debugfs_variables(d):
354    d.appendVar('IMAGE_ROOTFS', '-dbg')
355    if d.getVar('IMAGE_LINK_NAME'):
356        d.appendVar('IMAGE_LINK_NAME', '-dbg')
357    d.appendVar('IMAGE_NAME','-dbg')
358    d.setVar('IMAGE_BUILDING_DEBUGFS', 'true')
359    debugfs_image_fstypes = d.getVar('IMAGE_FSTYPES_DEBUGFS')
360    if debugfs_image_fstypes:
361        d.setVar('IMAGE_FSTYPES', debugfs_image_fstypes)
362
363python setup_debugfs () {
364    setup_debugfs_variables(d)
365}
366
367python () {
368    vardeps = set()
369    # We allow CONVERSIONTYPES to have duplicates. That avoids breaking
370    # derived distros when OE-core or some other layer independently adds
371    # the same type. There is still only one command for each type, but
372    # presumably the commands will do the same when the type is the same,
373    # even when added in different places.
374    #
375    # Without de-duplication, gen_conversion_cmds() below
376    # would create the same compression command multiple times.
377    ctypes = set(d.getVar('CONVERSIONTYPES').split())
378    old_overrides = d.getVar('OVERRIDES', False)
379
380    def _image_base_type(type):
381        basetype = type
382        for ctype in ctypes:
383            if type.endswith("." + ctype):
384                basetype = type[:-len("." + ctype)]
385                break
386
387        if basetype != type:
388            # New base type itself might be generated by a conversion command.
389            basetype = _image_base_type(basetype)
390
391        return basetype
392
393    basetypes = {}
394    alltypes = d.getVar('IMAGE_FSTYPES').split()
395    typedeps = {}
396
397    if d.getVar('IMAGE_GEN_DEBUGFS') == "1":
398        debugfs_fstypes = d.getVar('IMAGE_FSTYPES_DEBUGFS').split()
399        for t in debugfs_fstypes:
400            alltypes.append("debugfs_" + t)
401
402    def _add_type(t):
403        baset = _image_base_type(t)
404        input_t = t
405        if baset not in basetypes:
406            basetypes[baset]= []
407        if t not in basetypes[baset]:
408            basetypes[baset].append(t)
409        debug = ""
410        if t.startswith("debugfs_"):
411            t = t[8:]
412            debug = "debugfs_"
413        deps = (d.getVar('IMAGE_TYPEDEP:' + t) or "").split()
414        vardeps.add('IMAGE_TYPEDEP:' + t)
415        if baset not in typedeps:
416            typedeps[baset] = set()
417        deps = [debug + dep for dep in deps]
418        for dep in deps:
419            if dep not in alltypes:
420                alltypes.append(dep)
421            _add_type(dep)
422            basedep = _image_base_type(dep)
423            typedeps[baset].add(basedep)
424
425        if baset != input_t:
426            _add_type(baset)
427
428    for t in alltypes[:]:
429        _add_type(t)
430
431    d.appendVarFlag('do_image', 'vardeps', ' '.join(vardeps))
432
433    maskedtypes = (d.getVar('IMAGE_TYPES_MASKED') or "").split()
434    maskedtypes = [dbg + t for t in maskedtypes for dbg in ("", "debugfs_")]
435
436    for t in basetypes:
437        vardeps = set()
438        cmds = []
439        subimages = []
440        realt = t
441
442        if t in maskedtypes:
443            continue
444
445        localdata = bb.data.createCopy(d)
446        debug = ""
447        if t.startswith("debugfs_"):
448            setup_debugfs_variables(localdata)
449            debug = "setup_debugfs "
450            realt = t[8:]
451        localdata.setVar('OVERRIDES', '%s:%s' % (realt, old_overrides))
452        localdata.setVar('type', realt)
453        # Delete DATETIME so we don't expand any references to it now
454        # This means the task's hash can be stable rather than having hardcoded
455        # date/time values. It will get expanded at execution time.
456        # Similarly TMPDIR since otherwise we see QA stamp comparision problems
457        # Expand PV else it can trigger get_srcrev which can fail due to these variables being unset
458        localdata.setVar('PV', d.getVar('PV'))
459        localdata.delVar('DATETIME')
460        localdata.delVar('DATE')
461        localdata.delVar('TMPDIR')
462        localdata.delVar('IMAGE_VERSION_SUFFIX')
463        vardepsexclude = (d.getVarFlag('IMAGE_CMD:' + realt, 'vardepsexclude') or '').split()
464        for dep in vardepsexclude:
465            localdata.delVar(dep)
466
467        image_cmd = localdata.getVar("IMAGE_CMD")
468        vardeps.add('IMAGE_CMD:' + realt)
469        if image_cmd:
470            cmds.append("\t" + image_cmd)
471        else:
472            bb.fatal("No IMAGE_CMD defined for IMAGE_FSTYPES entry '%s' - possibly invalid type name or missing support class" % t)
473        cmds.append(localdata.expand("\tcd ${IMGDEPLOYDIR}"))
474
475        # Since a copy of IMAGE_CMD:xxx will be inlined within do_image_xxx,
476        # prevent a redundant copy of IMAGE_CMD:xxx being emitted as a function.
477        d.delVarFlag('IMAGE_CMD:' + realt, 'func')
478
479        rm_tmp_images = set()
480        def gen_conversion_cmds(bt):
481            for ctype in sorted(ctypes):
482                if bt.endswith("." + ctype):
483                    type = bt[0:-len(ctype) - 1]
484                    if type.startswith("debugfs_"):
485                        type = type[8:]
486                    # Create input image first.
487                    gen_conversion_cmds(type)
488                    localdata.setVar('type', type)
489                    cmd = "\t" + localdata.getVar("CONVERSION_CMD:" + ctype)
490                    if cmd not in cmds:
491                        cmds.append(cmd)
492                    vardeps.add('CONVERSION_CMD:' + ctype)
493                    subimage = type + "." + ctype
494                    if subimage not in subimages:
495                        subimages.append(subimage)
496                    if type not in alltypes:
497                        rm_tmp_images.add(localdata.expand("${IMAGE_NAME}.${type}"))
498
499        for bt in basetypes[t]:
500            gen_conversion_cmds(bt)
501
502        localdata.setVar('type', realt)
503        if t not in alltypes:
504            rm_tmp_images.add(localdata.expand("${IMAGE_NAME}.${type}"))
505        else:
506            subimages.append(realt)
507
508        # Clean up after applying all conversion commands. Some of them might
509        # use the same input, therefore we cannot delete sooner without applying
510        # some complex dependency analysis.
511        for image in sorted(rm_tmp_images):
512            cmds.append("\trm " + image)
513
514        after = 'do_image'
515        for dep in typedeps[t]:
516            after += ' do_image_%s' % dep.replace("-", "_").replace(".", "_")
517
518        task = "do_image_%s" % t.replace("-", "_").replace(".", "_")
519
520        d.setVar(task, '\n'.join(cmds))
521        d.setVarFlag(task, 'func', '1')
522        d.setVarFlag(task, 'fakeroot', '1')
523        d.setVarFlag(task, 'imagetype', t)
524
525        d.appendVarFlag(task, 'prefuncs', ' ' + debug + ' set_image_size')
526        d.prependVarFlag(task, 'postfuncs', 'create_symlinks ')
527        d.appendVarFlag(task, 'subimages', ' ' + ' '.join(subimages))
528        d.appendVarFlag(task, 'vardeps', ' ' + ' '.join(vardeps))
529        d.appendVarFlag(task, 'vardepsexclude', ' DATETIME DATE ' + ' '.join(vardepsexclude))
530        d.appendVarFlag(task, 'postfuncs', ' write_image_output_manifest')
531
532        bb.debug(2, "Adding task %s before %s, after %s" % (task, 'do_image_complete', after))
533        bb.build.addtask(task, 'do_image_complete', after, d)
534}
535
536#
537# Compute the rootfs size
538#
539def get_rootfs_size(d):
540    import subprocess, oe.utils
541
542    rootfs_alignment = int(d.getVar('IMAGE_ROOTFS_ALIGNMENT'))
543    overhead_factor = float(d.getVar('IMAGE_OVERHEAD_FACTOR'))
544    rootfs_req_size = int(d.getVar('IMAGE_ROOTFS_SIZE'))
545    rootfs_extra_space = eval(d.getVar('IMAGE_ROOTFS_EXTRA_SPACE'))
546    rootfs_maxsize = d.getVar('IMAGE_ROOTFS_MAXSIZE')
547    image_fstypes = d.getVar('IMAGE_FSTYPES') or ''
548    initramfs_fstypes = d.getVar('INITRAMFS_FSTYPES') or ''
549    initramfs_maxsize = d.getVar('INITRAMFS_MAXSIZE')
550
551    size_kb = oe.utils.directory_size(d.getVar("IMAGE_ROOTFS")) / 1024
552
553    base_size = size_kb * overhead_factor
554    bb.debug(1, '%f = %d * %f' % (base_size, size_kb, overhead_factor))
555    base_size2 = max(base_size, rootfs_req_size) + rootfs_extra_space
556    bb.debug(1, '%f = max(%f, %d)[%f] + %d' % (base_size2, base_size, rootfs_req_size, max(base_size, rootfs_req_size), rootfs_extra_space))
557
558    base_size = base_size2
559    if base_size != int(base_size):
560        base_size = int(base_size + 1)
561    else:
562        base_size = int(base_size)
563    bb.debug(1, '%f = int(%f)' % (base_size, base_size2))
564
565    base_size_saved = base_size
566    base_size += rootfs_alignment - 1
567    base_size -= base_size % rootfs_alignment
568    bb.debug(1, '%d = aligned(%d)' % (base_size, base_size_saved))
569
570    # Do not check image size of the debugfs image. This is not supposed
571    # to be deployed, etc. so it doesn't make sense to limit the size
572    # of the debug.
573    if (d.getVar('IMAGE_BUILDING_DEBUGFS') or "") == "true":
574        bb.debug(1, 'returning debugfs size %d' % (base_size))
575        return base_size
576
577    # Check the rootfs size against IMAGE_ROOTFS_MAXSIZE (if set)
578    if rootfs_maxsize:
579        rootfs_maxsize_int = int(rootfs_maxsize)
580        if base_size > rootfs_maxsize_int:
581            bb.fatal("The rootfs size %d(K) exceeds IMAGE_ROOTFS_MAXSIZE: %d(K)" % \
582                (base_size, rootfs_maxsize_int))
583
584    # Check the initramfs size against INITRAMFS_MAXSIZE (if set)
585    if image_fstypes == initramfs_fstypes != ''  and initramfs_maxsize:
586        initramfs_maxsize_int = int(initramfs_maxsize)
587        if base_size > initramfs_maxsize_int:
588            bb.error("The initramfs size %d(K) exceeds INITRAMFS_MAXSIZE: %d(K)" % \
589                (base_size, initramfs_maxsize_int))
590            bb.error("You can set INITRAMFS_MAXSIZE a larger value. Usually, it should")
591            bb.fatal("be less than 1/2 of ram size, or you may fail to boot it.\n")
592
593    bb.debug(1, 'returning %d' % (base_size))
594    return base_size
595
596python set_image_size () {
597        rootfs_size = get_rootfs_size(d)
598        d.setVar('ROOTFS_SIZE', str(rootfs_size))
599        d.setVarFlag('ROOTFS_SIZE', 'export', '1')
600}
601
602#
603# Create symlinks to the newly created image
604#
605python create_symlinks() {
606
607    deploy_dir = d.getVar('IMGDEPLOYDIR')
608    img_name = d.getVar('IMAGE_NAME')
609    link_name = d.getVar('IMAGE_LINK_NAME')
610    manifest_name = d.getVar('IMAGE_MANIFEST')
611    taskname = d.getVar("BB_CURRENTTASK")
612    subimages = (d.getVarFlag("do_" + taskname, 'subimages', False) or "").split()
613
614    if not link_name:
615        return
616    for type in subimages:
617        dst = os.path.join(deploy_dir, link_name + "." + type)
618        src = img_name + "." + type
619        if os.path.exists(os.path.join(deploy_dir, src)):
620            bb.note("Creating symlink: %s -> %s" % (dst, src))
621            if os.path.islink(dst):
622                os.remove(dst)
623            os.symlink(src, dst)
624        else:
625            bb.note("Skipping symlink, source does not exist: %s -> %s" % (dst, src))
626}
627
628python write_image_output_manifest() {
629    import json
630    from pathlib import Path
631
632    taskname = d.getVar("BB_CURRENTTASK")
633    image_deploy_dir = Path(d.getVar('IMGDEPLOYDIR'))
634    image_manifest_dir = Path(d.getVar('IMGMANIFESTDIR'))
635    manifest_path = image_manifest_dir / ("do_" + d.getVar("BB_CURRENTTASK") + ".json")
636
637    image_name = d.getVar("IMAGE_NAME")
638    image_basename = d.getVar("IMAGE_BASENAME")
639    machine = d.getVar("MACHINE")
640
641    subimages = (d.getVarFlag("do_" + taskname, 'subimages', False) or "").split()
642    imagetype = d.getVarFlag("do_" + taskname, 'imagetype', False)
643
644    data = {
645        "taskname": taskname,
646        "imagetype": imagetype,
647        "images": []
648    }
649
650    for type in subimages:
651        image_filename = image_name + "." + type
652        image_path = image_deploy_dir / image_filename
653        if not image_path.exists():
654            continue
655        data["images"].append({
656            "filename": image_filename,
657        })
658
659    with manifest_path.open("w") as f:
660        json.dump([data], f)
661}
662
663MULTILIBRE_ALLOW_REP += "${base_bindir} ${base_sbindir} ${bindir} ${sbindir} ${libexecdir} ${sysconfdir} ${nonarch_base_libdir}/udev /lib/modules/[^/]*/modules.*"
664MULTILIB_CHECK_FILE = "${WORKDIR}/multilib_check.py"
665MULTILIB_TEMP_ROOTFS = "${WORKDIR}/multilib"
666
667PSEUDO_INCLUDE_PATHS .= ",${MULTILIB_TEMP_ROOTFS}"
668
669do_fetch[noexec] = "1"
670do_unpack[noexec] = "1"
671do_patch[noexec] = "1"
672do_configure[noexec] = "1"
673do_compile[noexec] = "1"
674do_install[noexec] = "1"
675deltask do_populate_lic
676deltask do_populate_sysroot
677do_package[noexec] = "1"
678deltask do_package_qa
679deltask do_packagedata
680deltask do_package_write_ipk
681deltask do_package_write_deb
682deltask do_package_write_rpm
683
684create_merged_usr_symlinks_rootfs() {
685    create_merged_usr_symlinks ${IMAGE_ROOTFS}
686}
687
688ROOTFS_PREPROCESS_COMMAND += "${@bb.utils.contains('DISTRO_FEATURES', 'usrmerge', 'create_merged_usr_symlinks_rootfs', '',d)}"
689
690reproducible_final_image_task () {
691    if [ "$REPRODUCIBLE_TIMESTAMP_ROOTFS" = "" ]; then
692        REPRODUCIBLE_TIMESTAMP_ROOTFS=`git -C "${COREBASE}" log -1 --pretty=%ct 2>/dev/null` || true
693        if [ "$REPRODUCIBLE_TIMESTAMP_ROOTFS" = "" ]; then
694            REPRODUCIBLE_TIMESTAMP_ROOTFS=`stat -c%Y ${@bb.utils.which(d.getVar("BBPATH"), "conf/bitbake.conf")}`
695        fi
696    fi
697    # Set mtime of all files to a reproducible value
698    bbnote "reproducible_final_image_task: mtime set to $REPRODUCIBLE_TIMESTAMP_ROOTFS"
699    find  ${IMAGE_ROOTFS} -print0 | xargs -0 touch -h  --date=@$REPRODUCIBLE_TIMESTAMP_ROOTFS
700}
701
702IMAGE_PREPROCESS_COMMAND:append = " reproducible_final_image_task "
703
704CVE_PRODUCT = ""
705