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