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