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