-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgenimage.bbclass
223 lines (184 loc) · 7.57 KB
/
genimage.bbclass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# genimage.bbclass
#
# Class to generate file system and disk images using the `genimage` tool.
#
# In order to build images with this class, you need to create a dedicated disk
# image recipe! This must inherit the genimage class and have a valid genimage
# configuration file in SRC_URI (named `genimage.config` by default):
#
# inherit genimage
#
# SRC_URI += "file://genimage.config"
#
# You also need to depend on all recipes creating artifacts used by
# genimage to build the final (disk) image, e.g.:
#
# do_genimage[depends] += "virtual/bootloader:do_deploy core-image-minimal:do_image_complete"
#
# The main purpose of genimage is to create an entire SD, eMMC, NAND, or UBI
# image with multiple partitions based on different images (kernel,
# bootloader, rootfs, ...)
#
# The name of the resulting image is named the same way normal images are
# named. You can customize output with the variables `GENIMAGE_IMAGE_NAME` and
# `GENIMAGE_IMAGE_SUFFIX`.
#
# Note that you should also make your genimage image recipe depend on the set
# of host tools required for building, e.g.
#
# DEPENDS += "e2fsprogs-native"
#
# You can also use genimage to split up a created rootfs into different
# partition images. Consider a yocto-created rootfs, for example.
# You can put all content of the /home directory in a 'data' partition while
# putting all content of /etc in a config partition and the rest ('/') in the
# final rootfs partition, then pack all them together to an SD image.
#
# In order to do this, you have to provide the name of the image recipe you
# intend to split up the data for:
#
# GENIMAGE_ROOTFS_IMAGE = "my-production-image"
#
# The image recipe must build an archive, either `tar.bz2` (default) or the
# type matching the extension you set with `GENIMAGE_ROOTFS_IMAGE_FSTYPE`:
#
# GENIMAGE_ROOTFS_IMAGE_FSTYPE = "tar.xz"
#
# Since nanbield the IMAGE_NAME also contains the IMAGE_NAME_SUFFIX, with it
# being set inside the image-artifact-names.bbclass. This is either '${IMAGE_NAME_SUFFIX}'
# (default) or the name matching the suffix you set with
# `GENIMAGE_ROOTFS_IMAGE_SUFFIX`:
#
# GENIMAGE_ROOTFS_IMAGE_SUFFIX = ".rootfs"
#
# The split-up is controlled by your genimage config file, using the
# 'mountpoint' options:
#
# datafs {
# [...]
# mountpoint = "/home"
# }
#
# rootfs {
# [...]
# mountpoint = "/"
# }
#
# Most common variables for customization from image recipe:
#
# GENIMAGE_CONFIG - config passed to genimage --config (default: 'genimage.config')
# GENIMAGE_IMAGE_SUFFIX - file extension suffix for created image (default: 'img')
# GENIMAGE_ROOTFS_IMAGE - input rootfs image to generate file system images from
# GENIMAGE_ROOTFS_IMAGE_FSTYPE - input roofs FSTYPE to use (default: 'tar.bz2')
# GENIMAGE_ROOTFS_IMAGE_SUFFIX - IMAGE_NAME_SUFFIX to use (default: '${IMAGE_NAME_SUFFIX}')
# GENIMAGE_VARIABLES[VAR] - replace @VAR@ in config with variable flag value
# GENIMAGE_COMPRESSION - compress the generated image. Allowed values
# are 'none' for no compression (the default), 'gzip' and 'xz'.
inherit nopackages image-artifact-names deploy
LICENSE ?= "MIT"
PACKAGES = ""
PACKAGE_ARCH = "${MACHINE_ARCH}"
python () {
if bb.data.inherits_class('image', d):
bb.fatal("genimage.bbclass is not designed to be inherited by a rootfs image recipe!")
}
S = "${WORKDIR}/sources"
UNPACKDIR = "${S}"
B = "${WORKDIR}/genimage-${PN}"
INHIBIT_DEFAULT_DEPS = "1"
DEPENDS += "genimage-native"
GENIMAGE_CONFIG ?= "genimage.config"
GENIMAGE_IMAGE_SUFFIX ?= "img"
GENIMAGE_IMAGE_NAME = "${IMAGE_BASENAME}-${MACHINE}-${DATETIME}"
# Don't include the DATETIME variable in the sstate package signature
GENIMAGE_IMAGE_NAME[vardepsexclude] = "DATETIME"
GENIMAGE_IMAGE_LINK_NAME = "${IMAGE_BASENAME}-${MACHINE}"
GENIMAGE_IMAGE_FULLNAME ?= "${GENIMAGE_IMAGE_NAME}.${GENIMAGE_IMAGE_SUFFIX}"
GENIMAGE_IMAGE_LINK_FULLNAME ?= "${GENIMAGE_IMAGE_LINK_NAME}.${GENIMAGE_IMAGE_SUFFIX}"
def get_default_fstype(d):
fstypes = d.getVar('IMAGE_FSTYPES' or '').split()
for x in fstypes:
if "tar" in x:
return x
return "tar.bz2"
GENIMAGE_ROOTFS_IMAGE ?= ""
GENIMAGE_ROOTFS_IMAGE_FSTYPE ?= "${@get_default_fstype(d)}"
GENIMAGE_ROOTFS_IMAGE_SUFFIX ?= "${IMAGE_NAME_SUFFIX}"
GENIMAGE_VARIABLES[IMAGE] = "${GENIMAGE_IMAGE_FULLNAME}"
do_genimage[vardeps] += "GENIMAGE_VARIABLES"
do_genimage[depends] += "${@'${GENIMAGE_ROOTFS_IMAGE}:do_image_complete' if '${GENIMAGE_ROOTFS_IMAGE}' else ''}"
GENIMAGE_CREATE_BMAP ?= "0"
do_genimage[depends] += "${@'bmaptool-native:do_populate_sysroot' if d.getVar('GENIMAGE_CREATE_BMAP') == '1' else ''}"
GENIMAGE_COMPRESSION ??= "none"
GENIMAGE_COMPRESS_DEPENDS[none] = ""
GENIMAGE_COMPRESS_DEPENDS[gzip] = "pigz-native:do_populate_sysroot"
GENIMAGE_COMPRESS_DEPENDS[xz] = "xz-native:do_populate_sysroot"
do_genimage[depends] += "${@d.getVarFlag('GENIMAGE_COMPRESS_DEPENDS', '${GENIMAGE_COMPRESSION}')}"
GENIMAGE_COMPRESS_CMD[none] = ":"
GENIMAGE_COMPRESS_CMD[gzip] = "gzip -f -9 -n"
GENIMAGE_COMPRESS_CMD[xz] = "xz -f"
GENIMAGE_COMPRESS_CMD = "${@d.getVarFlag('GENIMAGE_COMPRESS_CMD', '${GENIMAGE_COMPRESSION}')}"
GENIMAGE_TMPDIR = "${WORKDIR}/genimage-tmp"
GENIMAGE_ROOTDIR = "${WORKDIR}/root"
GENIMAGE_OPTS ??= ""
do_genimage_preprocess[cleandirs] = "${GENIMAGE_TMPDIR} ${GENIMAGE_ROOTDIR} ${B}"
do_configure () {
if ! grep -q "@IMAGE@" ${S}/${GENIMAGE_CONFIG}; then
bbnote "${GENIMAGE_CONFIG} does not contain @IMAGE@ marker"
fi
}
do_genimage_preprocess[dirs] = "${B}"
python do_genimage_preprocess () {
def expandvar(m):
val = d.getVarFlag('GENIMAGE_VARIABLES', m.group(1), expand=True)
if val is None:
bb.error("cannot expand variable @%s@ from GENIMAGE_VARIABLES" % m.group(1))
return val
import re
infile = d.getVar('S') + "/" + d.getVar('GENIMAGE_CONFIG')
outfile = d.getVar('B') + "/.config"
with open(infile, "r+") as input:
expansion = re.sub(r"@([^{}@\n\t :]+)@", expandvar, input.read())
with open(outfile, "w") as output:
output.write(expansion)
}
fakeroot do_genimage () {
# unpack input rootfs image if given
if [ "x${GENIMAGE_ROOTFS_IMAGE}" != "x" ]; then
bbnote "Unpacking ${DEPLOY_DIR_IMAGE}/${GENIMAGE_ROOTFS_IMAGE}-${MACHINE}${GENIMAGE_ROOTFS_IMAGE_SUFFIX}.${GENIMAGE_ROOTFS_IMAGE_FSTYPE} to ${GENIMAGE_ROOTDIR}"
tar -xf ${DEPLOY_DIR_IMAGE}/${GENIMAGE_ROOTFS_IMAGE}-${MACHINE}${GENIMAGE_ROOTFS_IMAGE_SUFFIX}.${GENIMAGE_ROOTFS_IMAGE_FSTYPE} -C ${GENIMAGE_ROOTDIR}
fi
genimage \
--loglevel 2 \
--config ${B}/.config \
--tmppath ${GENIMAGE_TMPDIR} \
--inputpath ${DEPLOY_DIR_IMAGE} \
--includepath ${S} \
--outputpath ${B} \
--rootpath ${GENIMAGE_ROOTDIR} \
${GENIMAGE_OPTS}
if [ "${GENIMAGE_CREATE_BMAP}" = 1 ] ; then
bmaptool create -o ${B}/${GENIMAGE_IMAGE_FULLNAME}.bmap ${B}/${GENIMAGE_IMAGE_FULLNAME}
fi
${GENIMAGE_COMPRESS_CMD} ${B}/${GENIMAGE_IMAGE_FULLNAME}
rm ${B}/.config
}
do_genimage[depends] += "virtual/fakeroot-native:do_populate_sysroot"
do_genimage[prefuncs] += "do_genimage_preprocess"
SSTATE_SKIP_CREATION:task-genimage = '1'
addtask genimage after do_configure
do_deploy () {
install -m 0644 ${B}/* ${DEPLOYDIR}/
for img in ${B}/*; do
img=$(basename "${img}")
case "$img" in *"${GENIMAGE_IMAGE_FULLNAME}"*)
ln -sf ${img} \
${DEPLOYDIR}/$(echo "${img}" | sed "s/${GENIMAGE_IMAGE_FULLNAME}/${GENIMAGE_IMAGE_LINK_FULLNAME}/")
esac
done
}
addtask deploy after do_genimage before do_build
do_patch[noexec] = "1"
do_compile[noexec] = "1"
do_install[noexec] = "1"
deltask do_populate_sysroot