image_types_wic.bbclass 6.5 KB

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