Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1 | # The WICVARS variable is used to define list of bitbake variables used in wic code |
| 2 | # variables from this list is written to <image>.env file |
| 3 | WICVARS ?= "\ |
| 4 | BBLAYERS IMGDEPLOYDIR DEPLOY_DIR_IMAGE FAKEROOTCMD IMAGE_BASENAME IMAGE_BOOT_FILES \ |
| 5 | IMAGE_LINK_NAME IMAGE_ROOTFS INITRAMFS_FSTYPES INITRD INITRD_LIVE ISODIR RECIPE_SYSROOT_NATIVE \ |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 6 | ROOTFS_SIZE STAGING_DATADIR STAGING_DIR STAGING_LIBDIR TARGET_SYS \ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 7 | KERNEL_IMAGETYPE MACHINE INITRAMFS_IMAGE INITRAMFS_IMAGE_BUNDLE INITRAMFS_LINK_NAME APPEND" |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 8 | |
| 9 | inherit ${@bb.utils.contains('INITRAMFS_IMAGE_BUNDLE', '1', 'kernel-artifact-names', '', d)} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | |
| 11 | WKS_FILE ??= "${IMAGE_BASENAME}.${MACHINE}.wks" |
| 12 | WKS_FILES ?= "${WKS_FILE} ${IMAGE_BASENAME}.wks" |
| 13 | WKS_SEARCH_PATH ?= "${THISDIR}:${@':'.join('%s/wic' % p for p in '${BBPATH}'.split(':'))}:${@':'.join('%s/scripts/lib/wic/canned-wks' % l for l in '${BBPATH}:${COREBASE}'.split(':'))}" |
| 14 | WKS_FULL_PATH = "${@wks_search(d.getVar('WKS_FILES').split(), d.getVar('WKS_SEARCH_PATH')) or ''}" |
| 15 | |
| 16 | def wks_search(files, search_path): |
| 17 | for f in files: |
| 18 | if os.path.isabs(f): |
| 19 | if os.path.exists(f): |
| 20 | return f |
| 21 | else: |
| 22 | searched = bb.utils.which(search_path, f) |
| 23 | if searched: |
| 24 | return searched |
| 25 | |
| 26 | WIC_CREATE_EXTRA_ARGS ?= "" |
| 27 | |
| 28 | IMAGE_CMD_wic () { |
| 29 | out="${IMGDEPLOYDIR}/${IMAGE_NAME}" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 30 | build_wic="${WORKDIR}/build-wic" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 31 | wks="${WKS_FULL_PATH}" |
| 32 | if [ -z "$wks" ]; then |
| 33 | bbfatal "No kickstart files from WKS_FILES were found: ${WKS_FILES}. Please set WKS_FILE or WKS_FILES appropriately." |
| 34 | fi |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 35 | BUILDDIR="${TOPDIR}" PSEUDO_UNLOAD=1 wic create "$wks" --vars "${STAGING_DIR}/${MACHINE}/imgdata/" -e "${IMAGE_BASENAME}" -o "$build_wic/" ${WIC_CREATE_EXTRA_ARGS} |
| 36 | mv "$build_wic/$(basename "${wks%.wks}")"*.direct "$out${IMAGE_NAME_SUFFIX}.wic" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 37 | } |
| 38 | IMAGE_CMD_wic[vardepsexclude] = "WKS_FULL_PATH WKS_FILES TOPDIR" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 39 | do_image_wic[cleandirs] = "${WORKDIR}/build-wic" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 40 | |
| 41 | # Rebuild when the wks file or vars in WICVARS change |
| 42 | USING_WIC = "${@bb.utils.contains_any('IMAGE_FSTYPES', 'wic ' + ' '.join('wic.%s' % c for c in '${CONVERSIONTYPES}'.split()), '1', '', d)}" |
| 43 | WKS_FILE_CHECKSUM = "${@'${WKS_FULL_PATH}:%s' % os.path.exists('${WKS_FULL_PATH}') if '${USING_WIC}' else ''}" |
| 44 | do_image_wic[file-checksums] += "${WKS_FILE_CHECKSUM}" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 45 | do_image_wic[depends] += "${@' '.join('%s-native:do_populate_sysroot' % r for r in ('parted', 'gptfdisk', 'dosfstools', 'mtools'))}" |
| 46 | |
| 47 | # We ensure all artfacts are deployed (e.g virtual/bootloader) |
| 48 | do_image_wic[recrdeptask] += "do_deploy" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 49 | do_image_wic[deptask] += "do_image_complete" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 50 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 51 | WKS_FILE_DEPENDS_DEFAULT = '${@bb.utils.contains_any("BUILD_ARCH", [ 'x86_64', 'i686' ], "syslinux-native", "",d)}' |
| 52 | WKS_FILE_DEPENDS_DEFAULT += "bmap-tools-native cdrtools-native btrfs-tools-native squashfs-tools-native e2fsprogs-native" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 53 | WKS_FILE_DEPENDS_BOOTLOADERS = "" |
| 54 | WKS_FILE_DEPENDS_BOOTLOADERS_x86 = "syslinux grub-efi systemd-boot" |
| 55 | WKS_FILE_DEPENDS_BOOTLOADERS_x86-64 = "syslinux grub-efi systemd-boot" |
| 56 | WKS_FILE_DEPENDS_BOOTLOADERS_x86-x32 = "syslinux grub-efi" |
| 57 | |
| 58 | WKS_FILE_DEPENDS ??= "${WKS_FILE_DEPENDS_DEFAULT} ${WKS_FILE_DEPENDS_BOOTLOADERS}" |
| 59 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 60 | DEPENDS += "${@ '${WKS_FILE_DEPENDS}' if d.getVar('USING_WIC') else '' }" |
| 61 | |
| 62 | python do_write_wks_template () { |
| 63 | """Write out expanded template contents to WKS_FULL_PATH.""" |
| 64 | import re |
| 65 | |
| 66 | template_body = d.getVar('_WKS_TEMPLATE') |
| 67 | |
| 68 | # Remove any remnant variable references left behind by the expansion |
| 69 | # due to undefined variables |
| 70 | expand_var_regexp = re.compile(r"\${[^{}@\n\t :]+}") |
| 71 | while True: |
| 72 | new_body = re.sub(expand_var_regexp, '', template_body) |
| 73 | if new_body == template_body: |
| 74 | break |
| 75 | else: |
| 76 | template_body = new_body |
| 77 | |
| 78 | wks_file = d.getVar('WKS_FULL_PATH') |
| 79 | with open(wks_file, 'w') as f: |
| 80 | f.write(template_body) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 81 | f.close() |
| 82 | # Copy the finalized wks file to the deploy directory for later use |
| 83 | depdir = d.getVar('IMGDEPLOYDIR') |
| 84 | basename = d.getVar('IMAGE_BASENAME') |
| 85 | bb.utils.copyfile(wks_file, "%s/%s" % (depdir, basename + '-' + os.path.basename(wks_file))) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 88 | do_flush_pseudodb() { |
| 89 | ${FAKEROOTENV} ${FAKEROOTCMD} -S |
| 90 | } |
| 91 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 92 | python () { |
| 93 | if d.getVar('USING_WIC'): |
| 94 | wks_file_u = d.getVar('WKS_FULL_PATH', False) |
| 95 | wks_file = d.expand(wks_file_u) |
| 96 | base, ext = os.path.splitext(wks_file) |
| 97 | if ext == '.in' and os.path.exists(wks_file): |
| 98 | wks_out_file = os.path.join(d.getVar('WORKDIR'), os.path.basename(base)) |
| 99 | d.setVar('WKS_FULL_PATH', wks_out_file) |
| 100 | d.setVar('WKS_TEMPLATE_PATH', wks_file_u) |
| 101 | d.setVar('WKS_FILE_CHECKSUM', '${WKS_TEMPLATE_PATH}:True') |
| 102 | |
| 103 | # We need to re-parse each time the file changes, and bitbake |
| 104 | # needs to be told about that explicitly. |
| 105 | bb.parse.mark_dependency(d, wks_file) |
| 106 | |
| 107 | try: |
| 108 | with open(wks_file, 'r') as f: |
| 109 | body = f.read() |
| 110 | except (IOError, OSError) as exc: |
| 111 | pass |
| 112 | else: |
| 113 | # Previously, I used expandWithRefs to get the dependency list |
| 114 | # and add it to WICVARS, but there's no point re-parsing the |
| 115 | # file in process_wks_template as well, so just put it in |
| 116 | # a variable and let the metadata deal with the deps. |
| 117 | d.setVar('_WKS_TEMPLATE', body) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 118 | bb.build.addtask('do_write_wks_template', 'do_image_wic', 'do_image', d) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 119 | bb.build.addtask('do_image_wic', 'do_image_complete', None, d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | # |
| 123 | # Write environment variables used by wic |
| 124 | # to tmp/sysroots/<machine>/imgdata/<image>.env |
| 125 | # |
| 126 | python do_rootfs_wicenv () { |
| 127 | wicvars = d.getVar('WICVARS') |
| 128 | if not wicvars: |
| 129 | return |
| 130 | |
| 131 | stdir = d.getVar('STAGING_DIR') |
| 132 | outdir = os.path.join(stdir, d.getVar('MACHINE'), 'imgdata') |
| 133 | bb.utils.mkdirhier(outdir) |
| 134 | basename = d.getVar('IMAGE_BASENAME') |
| 135 | with open(os.path.join(outdir, basename) + '.env', 'w') as envf: |
| 136 | for var in wicvars.split(): |
| 137 | value = d.getVar(var) |
| 138 | if value: |
| 139 | envf.write('%s="%s"\n' % (var, value.strip())) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 140 | envf.close() |
| 141 | # Copy .env file to deploy directory for later use with stand alone wic |
| 142 | depdir = d.getVar('IMGDEPLOYDIR') |
| 143 | bb.utils.copyfile(os.path.join(outdir, basename) + '.env', os.path.join(depdir, basename) + '.env') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 144 | } |
Andrew Geissler | 1e34c2d | 2020-05-29 16:02:59 -0500 | [diff] [blame] | 145 | addtask do_flush_pseudodb after do_rootfs before do_image do_image_qa |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 146 | addtask do_rootfs_wicenv after do_image before do_image_wic |
| 147 | do_rootfs_wicenv[vardeps] += "${WICVARS}" |
| 148 | do_rootfs_wicenv[prefuncs] = 'set_image_size' |