image: Create separate tasks for rootfs construction
RFC PATCH 3/3 image: Create separate tasks for rootfs construction
I've heard complaints from people trying to create more interesting image types about how hard it is to understand the rootfs/image generation code and that its a pain to develop/test/debug.
Having looked at it myself, the internal construction of shell functions which then gets passed into a multiprocessing pool is rather convoluted and it places rather odd constraints on when variables are expanded. Its therefore no wonder people find it confusing/complex.
This patch splits the code in lib/oe/image into separate tasks, one per image type. This removes the need for the simple task graph code and defers to the bitbake task management code to handle this instead.
This is a good step forward in splitting up the monolithic code and starting to make it more accessible to people.
It should also make it easier for people to hook in other tasks and processes into the rootfs code.
Incidentally, the reason this code was all combined originally was due to limitations of fakeroot where if you exited the session, you lost permissions data. With pseudo this constraint was removed.
We did start to rework the rootfs/image code previously and got so far with untangling it however we did prioritise some performance tweaks over splitting into separate tasks and in hindsight, this was a mistake and should have been done the other way around. That work was suspended due to changes in the people working on the project but this split has always been intended, now is the time to finish it IMO.
ToDo:
* Handling of IMAGE_GEN_DEBUGFS needs reimplementing on the new code structure
* Some variable dependencies on do_rootfs need moving to do_image
* References to do_rootfs need auditing to see if they should now be do_image_complete
* Changes need documenting with migration notes.
Future:
* Do we want to move to a model where the image types are specific tasks rather than autogenerate things?
meta/classes/image.bbclass +196 -21
@@ -106,21 +106,6 @@ python () { d.setVarFlag(var, 'func', '1') } -def fstype_variables(d): - import oe.image - - image = oe.image.Image(d) - alltypes, fstype_groups, cimages = image._get_image_types() - fstype_vars = set() - for fstype_group in fstype_groups: - for fstype in fstype_group: - fstype_vars.add('IMAGE_CMD_' + fstype) - if fstype in cimages: - for ctype in cimages[fstype]: - fstype_vars.add('COMPRESS_CMD_' + ctype) - - return sorted(fstype_vars) - def rootfs_variables(d): from oe.rootfs import variable_depends variables = ['IMAGE_DEVICE_TABLES','BUILD_IMAGES_FROM_FEEDS','IMAGE_TYPES_MASKED','IMAGE_ROOTFS_ALIGNMENT','IMAGE_OVERHEAD_FACTOR','IMAGE_ROOTFS_SIZE','IMAGE_ROOTFS_EXTRA_SPACE',
@@ -129,7 +114,6 @@ def rootfs_variables(d): 'MULTILIB_TEMP_ROOTFS','MULTILIB_VARIANTS','MULTILIBS','ALL_MULTILIB_PACKAGE_ARCHS','MULTILIB_GLOBAL_VARIANTS','BAD_RECOMMENDATIONS','NO_RECOMMENDATIONS','PACKAGE_ARCHS', 'PACKAGE_CLASSES','TARGET_VENDOR','TARGET_VENDOR','TARGET_ARCH','TARGET_OS','OVERRIDES','BBEXTENDVARIANT','FEED_DEPLOYDIR_BASE_URI','INTERCEPT_DIR','USE_DEVFS', 'COMPRESSIONTYPES', 'IMAGE_GEN_DEBUGFS'] - variables.extend(fstype_variables(d)) variables.extend(command_variables(d)) variables.extend(variable_depends(d)) return " ".join(variables)
@@ -344,19 +328,17 @@ fakeroot python do_rootfs () { } fakeroot python do_image () { - from oe.image import create_image + #from oe.image import create_image from oe.image import Image from oe.utils import execute_pre_post_process - i = Image(d) - pre_process_cmds = d.getVar("IMAGE_PREPROCESS_COMMAND", True) execute_pre_post_process(d, pre_process_cmds) - i._remove_old_symlinks() + remove_old_symlinks(d) + write_wic_env(d) - i.create() } do_image[dirs] = "${TOPDIR}"
@@ -376,6 +358,199 @@ do_image_complete[lockfiles] += "${IMAGE_ROOTFS}.lock" do_image_complete[umask] = "022" addtask do_image_complete after do_image before do_build + +def write_wic_env(d): + """ + Write environment variables used by wic + to tmp/sysroots/<machine>/imgdata/<image>.env + """ + wicvars = d.getVar('WICVARS', True) + if not wicvars: + return + + stdir = d.getVar('STAGING_DIR_TARGET', True) + outdir = os.path.join(stdir, 'imgdata') + bb.utils.mkdirhier(outdir) + basename = d.getVar('IMAGE_BASENAME', True) + with open(os.path.join(outdir, basename) + '.env', 'w') as envf: + for var in wicvars.split(): + value = d.getVar(var, True) + if value: + envf.write('%s="%s"\n' % (var, value.strip())) + + +python () { + vardeps = set() + ctypes = d.getVar('COMPRESSIONTYPES', True).split() + old_overrides = d.getVar('OVERRIDES', 0) + + def _image_base_type(type): + if type in ["vmdk", "vdi", "qcow2", "live", "iso", "hddimg"]: + type = "ext4" + basetype = type + for ctype in ctypes: + if type.endswith("." + ctype): + basetype = type[:-len("." + ctype)] + break + + return basetype + + basetypes = {} + alltypes = d.getVar('IMAGE_FSTYPES', True).split() + typedeps = {} + + def _add_type(t): + baset = _image_base_type(t) + if baset not in basetypes: + basetypes[baset]= [] + if t not in basetypes[baset]: + basetypes[baset].append(t) + deps = (d.getVar('IMAGE_TYPEDEP_' + t, True) or "").split() + vardeps.add('IMAGE_TYPEDEP_' + t) + if baset not in typedeps: + typedeps[baset] = set() + for dep in deps: + if dep not in alltypes: + alltypes.append(dep) + _add_type(dep) + basedep = _image_base_type(dep) + typedeps[baset].add(basedep) + + for t in alltypes[:]: + _add_type(t) + + bb.warn(str(basetypes)) + + d.appendVarFlag('do_image', 'vardeps', ' '.join(vardeps)) + + for t in basetypes: + vardeps = set() + cmds = [] + subimages = [] + + localdata = bb.data.createCopy(d) + localdata.setVar('OVERRIDES', '%s:%s' % (t, old_overrides)) + bb.data.update_data(localdata) + localdata.setVar('type', t) + + image_cmd = localdata.getVar("IMAGE_CMD", True) + vardeps.add('IMAGE_CMD_' + t) + if image_cmd: + cmds.append("\t" + image_cmd) + else: + bb.fatal("No IMAGE_CMD defined for IMAGE_FSTYPES entry '%s' - possibly invalid type name or missing support class" % t) + cmds.append(localdata.expand("\tcd ${DEPLOY_DIR_IMAGE}")) + + for bt in basetypes[t]: + for ctype in ctypes: + if bt.endswith("." + ctype): + cmds.append("\t" + localdata.getVar("COMPRESS_CMD_" + ctype, True)) + vardeps.add('COMPRESS_CMD_' + ctype) + subimages.append(t + "." + ctype) + + if t not in alltypes: + cmds.append(localdata.expand("\trm ${IMAGE_NAME}.rootfs.${type}")) + else: + subimages.append(t) + + d.setVar('do_image_%s' % t, '\n'.join(cmds)) + d.setVarFlag('do_image_%s' % t, 'func', '1') + d.setVarFlag('do_image_%s' % t, 'fakeroot', '1') + d.setVarFlag('do_image_%s' % t, 'prefuncs', 'set_image_size') + d.setVarFlag('do_image_%s' % t, 'postfuncs', 'create_symlinks') + d.setVarFlag('do_image_%s' % t, 'subimages', subimages) + d.appendVarFlag('do_image_%s' % t, 'vardeps', ' '.join(vardeps)) + + #image_cmds.append((t, subimages, script_name, sprefix)) + + after = 'do_image' + for dep in typedeps[t]: + after += ' do_image_%s' % dep + + bb.warn("Adding type %s before %s, after %s" % (t, 'do_image_complete', after)) + bb.build.addtask('do_image_%s' % t, 'do_image_complete', after, d) + +} + +def _get_rootfs_size(d): + import subprocess + """compute the rootfs size""" + rootfs_alignment = int(d.getVar('IMAGE_ROOTFS_ALIGNMENT', True)) + overhead_factor = float(d.getVar('IMAGE_OVERHEAD_FACTOR', True)) + rootfs_req_size = int(d.getVar('IMAGE_ROOTFS_SIZE', True)) + rootfs_extra_space = eval(d.getVar('IMAGE_ROOTFS_EXTRA_SPACE', True)) + rootfs_maxsize = d.getVar('IMAGE_ROOTFS_MAXSIZE', True) + + output = subprocess.check_output(['du', '-ks', + d.getVar('IMAGE_ROOTFS', True)]) + size_kb = int(output.split()[0]) + base_size = size_kb * overhead_factor + base_size = (base_size, rootfs_req_size)[base_size < rootfs_req_size] + \ + rootfs_extra_space + + if base_size != int(base_size): + base_size = int(base_size + 1) + else: + base_size = int(base_size) + + base_size += rootfs_alignment - 1 + base_size -= base_size % rootfs_alignment + + # Check the rootfs size against IMAGE_ROOTFS_MAXSIZE (if set) + if rootfs_maxsize: + rootfs_maxsize_int = int(rootfs_maxsize) + if base_size > rootfs_maxsize_int: + bb.fatal("The rootfs size %d(K) overrides the max size %d(K)" % \ + (base_size, rootfs_maxsize_int)) + return base_size + +python set_image_size () { + rootfs_size = _get_rootfs_size(d) + d.setVar('ROOTFS_SIZE', str(rootfs_size)) + d.setVarFlag('ROOTFS_SIZE', 'export', '1') +} + +python create_symlinks() { + """create symlinks to the newly created image""" + deploy_dir = d.getVar('DEPLOY_DIR_IMAGE', True) + img_name = d.getVar('IMAGE_NAME', True) + link_name = d.getVar('IMAGE_LINK_NAME', True) + manifest_name = d.getVar('IMAGE_MANIFEST', True) + taskname = d.getVar("BB_CURRENTTASK", True) + subimages = d.getVarFlag("do_" + taskname, 'subimages', False) + os.chdir(deploy_dir) + + if link_name: + for type in subimages: + if os.path.exists(img_name + ".rootfs." + type): + dst = link_name + "." + type + src = img_name + ".rootfs." + type + bb.note("Creating symlink: %s -> %s" % (dst, src)) + os.symlink(src, dst) + + if manifest_name is not None and \ + os.path.exists(manifest_name) and \ + not os.path.exists(link_name + ".manifest"): + os.symlink(os.path.basename(manifest_name), + link_name + ".manifest") +} + +def remove_old_symlinks(d): + """remove the symlinks to old binaries""" + + if d.getVar('IMAGE_LINK_NAME', True): + deploy_dir = d.getVar('DEPLOY_DIR_IMAGE', True) + for img in os.listdir(deploy_dir): + if img.find(d.getVar('IMAGE_LINK_NAME', True)) == 0: + img = os.path.join(deploy_dir, img) + if os.path.islink(img): + if d.getVar('RM_OLD_IMAGE', True) == "1" and \ + os.path.exists(os.path.realpath(img)): + os.remove(os.path.realpath(img)) + + os.remove(img) + + insert_feed_uris () { echo "Building feeds for [${DISTRO}].."
meta/lib/oe/image.py +37 -387
447 lines not on this page · raw message