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