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