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