summaryrefslogtreecommitdiffstats
path: root/meta/classes-recipe/rootfs-postcommands.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes-recipe/rootfs-postcommands.bbclass')
-rw-r--r--meta/classes-recipe/rootfs-postcommands.bbclass440
1 files changed, 440 insertions, 0 deletions
diff --git a/meta/classes-recipe/rootfs-postcommands.bbclass b/meta/classes-recipe/rootfs-postcommands.bbclass
new file mode 100644
index 0000000000..d40adf5f0e
--- /dev/null
+++ b/meta/classes-recipe/rootfs-postcommands.bbclass
@@ -0,0 +1,440 @@
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# Zap the root password if debug-tweaks and empty-root-password features are not enabled
8ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'empty-root-password' ], "", "zap_empty_root_password; ",d)}'
9
10# Allow dropbear/openssh to accept logins from accounts with an empty password string if debug-tweaks or allow-empty-password is enabled
11ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-empty-password' ], "ssh_allow_empty_password; ", "",d)}'
12
13# Allow dropbear/openssh to accept root logins if debug-tweaks or allow-root-login is enabled
14ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-root-login' ], "ssh_allow_root_login; ", "",d)}'
15
16# Enable postinst logging if debug-tweaks or post-install-logging is enabled
17ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'post-install-logging' ], "postinst_enable_logging; ", "",d)}'
18
19# Create /etc/timestamp during image construction to give a reasonably sane default time setting
20ROOTFS_POSTPROCESS_COMMAND += "rootfs_update_timestamp; "
21
22# Tweak the mount options for rootfs in /etc/fstab if read-only-rootfs is enabled
23ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", "read_only_rootfs_hook; ", "",d)}'
24
25# We also need to do the same for the kernel boot parameters,
26# otherwise kernel or initramfs end up mounting the rootfs read/write
27# (the default) if supported by the underlying storage.
28#
29# We do this with :append because the default value might get set later with ?=
30# and we don't want to disable such a default that by setting a value here.
31APPEND:append = '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", " ro", "", d)}'
32
33# Generates test data file with data store variables expanded in json format
34ROOTFS_POSTPROCESS_COMMAND += "write_image_test_data; "
35
36# Write manifest
37IMAGE_MANIFEST = "${IMGDEPLOYDIR}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.manifest"
38ROOTFS_POSTUNINSTALL_COMMAND =+ "write_image_manifest ; "
39# Set default postinst log file
40POSTINST_LOGFILE ?= "${localstatedir}/log/postinstall.log"
41# Set default target for systemd images
42SYSTEMD_DEFAULT_TARGET ?= '${@bb.utils.contains_any("IMAGE_FEATURES", [ "x11-base", "weston" ], "graphical.target", "multi-user.target", d)}'
43ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "set_systemd_default_target; systemd_create_users;", "", d)}'
44
45ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile;'
46
47ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "overlayfs", "overlayfs_qa_check; overlayfs_postprocess;", "", d)}'
48
49inherit image-artifact-names
50
51# Sort the user and group entries in /etc by ID in order to make the content
52# deterministic. Package installs are not deterministic, causing the ordering
53# of entries to change between builds. In case that this isn't desired,
54# the command can be overridden.
55#
56# Note that useradd-staticids.bbclass has to be used to ensure that
57# the numeric IDs of dynamically created entries remain stable.
58#
59# We want this to run as late as possible, in particular after
60# systemd_sysusers_create and set_user_group. Using :append is not
61# enough for that, set_user_group is added that way and would end
62# up running after us.
63SORT_PASSWD_POSTPROCESS_COMMAND ??= " sort_passwd; "
64python () {
65 d.appendVar('ROOTFS_POSTPROCESS_COMMAND', '${SORT_PASSWD_POSTPROCESS_COMMAND}')
66 d.appendVar('ROOTFS_POSTPROCESS_COMMAND', 'rootfs_reproducible;')
67}
68
69systemd_create_users () {
70 for conffile in ${IMAGE_ROOTFS}/usr/lib/sysusers.d/*.conf; do
71 [ -e $conffile ] || continue
72 grep -v "^#" $conffile | sed -e '/^$/d' | while read type name id comment; do
73 if [ "$type" = "u" ]; then
74 useradd_params="--shell /sbin/nologin"
75 [ "$id" != "-" ] && useradd_params="$useradd_params --uid $id"
76 [ "$comment" != "-" ] && useradd_params="$useradd_params --comment $comment"
77 useradd_params="$useradd_params --system $name"
78 eval useradd --root ${IMAGE_ROOTFS} $useradd_params || true
79 elif [ "$type" = "g" ]; then
80 groupadd_params=""
81 [ "$id" != "-" ] && groupadd_params="$groupadd_params --gid $id"
82 groupadd_params="$groupadd_params --system $name"
83 eval groupadd --root ${IMAGE_ROOTFS} $groupadd_params || true
84 elif [ "$type" = "m" ]; then
85 group=$id
86 eval groupadd --root ${IMAGE_ROOTFS} --system $group || true
87 eval useradd --root ${IMAGE_ROOTFS} --shell /sbin/nologin --system $name --no-user-group || true
88 eval usermod --root ${IMAGE_ROOTFS} -a -G $group $name
89 fi
90 done
91 done
92}
93
94#
95# A hook function to support read-only-rootfs IMAGE_FEATURES
96#
97read_only_rootfs_hook () {
98 # Tweak the mount option and fs_passno for rootfs in fstab
99 if [ -f ${IMAGE_ROOTFS}/etc/fstab ]; then
100 sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab
101 fi
102
103 # Tweak the "mount -o remount,rw /" command in busybox-inittab inittab
104 if [ -f ${IMAGE_ROOTFS}/etc/inittab ]; then
105 sed -i 's|/bin/mount -o remount,rw /|/bin/mount -o remount,ro /|' ${IMAGE_ROOTFS}/etc/inittab
106 fi
107
108 # If we're using openssh and the /etc/ssh directory has no pre-generated keys,
109 # we should configure openssh to use the configuration file /etc/ssh/sshd_config_readonly
110 # and the keys under /var/run/ssh.
111 if [ -d ${IMAGE_ROOTFS}/etc/ssh ]; then
112 if [ -e ${IMAGE_ROOTFS}/etc/ssh/ssh_host_rsa_key ]; then
113 echo "SYSCONFDIR=\${SYSCONFDIR:-/etc/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
114 echo "SSHD_OPTS=" >> ${IMAGE_ROOTFS}/etc/default/ssh
115 else
116 echo "SYSCONFDIR=\${SYSCONFDIR:-/var/run/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
117 echo "SSHD_OPTS='-f /etc/ssh/sshd_config_readonly'" >> ${IMAGE_ROOTFS}/etc/default/ssh
118 fi
119 fi
120
121 # Also tweak the key location for dropbear in the same way.
122 if [ -d ${IMAGE_ROOTFS}/etc/dropbear ]; then
123 if [ ! -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then
124 echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
125 fi
126 fi
127
128 if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
129 # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
130 if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
131 sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
132 fi
133 # Run populate-volatile.sh at rootfs time to set up basic files
134 # and directories to support read-only rootfs.
135 if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
136 ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
137 fi
138 fi
139
140 if ${@bb.utils.contains("DISTRO_FEATURES", "systemd", "true", "false", d)}; then
141 # Create machine-id
142 # 20:12 < mezcalero> koen: you have three options: a) run systemd-machine-id-setup at install time, b) have / read-only and an empty file there (for stateless) and c) boot with / writable
143 touch ${IMAGE_ROOTFS}${sysconfdir}/machine-id
144 fi
145}
146
147#
148# This function disallows empty root passwords
149#
150zap_empty_root_password () {
151 if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then
152 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow
153 fi
154 if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then
155 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd
156 fi
157}
158
159#
160# allow dropbear/openssh to accept logins from accounts with an empty password string
161#
162ssh_allow_empty_password () {
163 for config in sshd_config sshd_config_readonly; do
164 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
165 sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
166 fi
167 done
168
169 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
170 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
171 if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then
172 sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
173 fi
174 else
175 printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
176 fi
177 fi
178
179 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then
180 for f in `find ${IMAGE_ROOTFS}${sysconfdir}/pam.d/* -type f -exec test -e {} \; -print`
181 do
182 sed -i 's/nullok_secure/nullok/' $f
183 done
184 fi
185}
186
187#
188# allow dropbear/openssh to accept root logins
189#
190ssh_allow_root_login () {
191 for config in sshd_config sshd_config_readonly; do
192 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
193 sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
194 fi
195 done
196
197 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
198 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
199 sed -i '/^DROPBEAR_EXTRA_ARGS=/ s/-w//' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
200 fi
201 fi
202}
203
204python sort_passwd () {
205 import rootfspostcommands
206 rootfspostcommands.sort_passwd(d.expand('${IMAGE_ROOTFS}${sysconfdir}'))
207}
208
209#
210# Enable postinst logging
211#
212postinst_enable_logging () {
213 mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
214 echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
215 echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
216}
217
218#
219# Modify systemd default target
220#
221set_systemd_default_target () {
222 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_system_unitdir}/${SYSTEMD_DEFAULT_TARGET} ]; then
223 ln -sf ${systemd_system_unitdir}/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target
224 fi
225}
226
227# If /var/volatile is not empty, we have seen problems where programs such as the
228# journal make assumptions based on the contents of /var/volatile. The journal
229# would then write to /var/volatile before it was mounted, thus hiding the
230# items previously written.
231#
232# This change is to attempt to fix those types of issues in a way that doesn't
233# affect users that may not be using /var/volatile.
234empty_var_volatile () {
235 if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then
236 match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null`
237 if [ -n "$match" ]; then
238 find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete
239 fi
240 fi
241}
242
243# Turn any symbolic /sbin/init link into a file
244remove_init_link () {
245 if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
246 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
247 rm ${IMAGE_ROOTFS}/sbin/init
248 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
249 fi
250}
251
252make_zimage_symlink_relative () {
253 if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
254 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
255 fi
256}
257
258python write_image_manifest () {
259 from oe.rootfs import image_list_installed_packages
260 from oe.utils import format_pkg_list
261
262 deploy_dir = d.getVar('IMGDEPLOYDIR')
263 link_name = d.getVar('IMAGE_LINK_NAME')
264 manifest_name = d.getVar('IMAGE_MANIFEST')
265
266 if not manifest_name:
267 return
268
269 pkgs = image_list_installed_packages(d)
270 with open(manifest_name, 'w+') as image_manifest:
271 image_manifest.write(format_pkg_list(pkgs, "ver"))
272
273 if os.path.exists(manifest_name) and link_name:
274 manifest_link = deploy_dir + "/" + link_name + ".manifest"
275 if manifest_link != manifest_name:
276 if os.path.lexists(manifest_link):
277 os.remove(manifest_link)
278 os.symlink(os.path.basename(manifest_name), manifest_link)
279}
280
281# Can be used to create /etc/timestamp during image construction to give a reasonably
282# sane default time setting
283rootfs_update_timestamp () {
284 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
285 # Convert UTC into %4Y%2m%2d%2H%2M%2S
286 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
287 else
288 sformatted=`date -u +%4Y%2m%2d%2H%2M%2S`
289 fi
290 echo $sformatted > ${IMAGE_ROOTFS}/etc/timestamp
291 bbnote "rootfs_update_timestamp: set /etc/timestamp to $sformatted"
292}
293
294# Prevent X from being started
295rootfs_no_x_startup () {
296 if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
297 chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm
298 fi
299}
300
301rootfs_trim_schemas () {
302 for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas
303 do
304 # Need this in case no files exist
305 if [ -e $schema ]; then
306 oe-trim-schemas $schema > $schema.new
307 mv $schema.new $schema
308 fi
309 done
310}
311
312rootfs_check_host_user_contaminated () {
313 contaminated="${S}/host-user-contaminated.txt"
314 HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)"
315 HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)"
316
317 find "${IMAGE_ROOTFS}" -path "${IMAGE_ROOTFS}/home" -prune -o \
318 -user "$HOST_USER_UID" -print -o -group "$HOST_USER_GID" -print >"$contaminated"
319
320 sed -e "s,${IMAGE_ROOTFS},," $contaminated | while read line; do
321 bbwarn "Path in the rootfs is owned by the same user or group as the user running bitbake:" $line `ls -lan ${IMAGE_ROOTFS}/$line`
322 done
323
324 if [ -s "$contaminated" ]; then
325 bbwarn "/etc/passwd:" `cat ${IMAGE_ROOTFS}/etc/passwd`
326 bbwarn "/etc/group:" `cat ${IMAGE_ROOTFS}/etc/group`
327 fi
328}
329
330# Make any absolute links in a sysroot relative
331rootfs_sysroot_relativelinks () {
332 sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT}
333}
334
335# Generated test data json file
336python write_image_test_data() {
337 from oe.data import export2json
338
339 deploy_dir = d.getVar('IMGDEPLOYDIR')
340 link_name = d.getVar('IMAGE_LINK_NAME')
341 testdata_name = os.path.join(deploy_dir, "%s.testdata.json" % d.getVar('IMAGE_NAME'))
342
343 searchString = "%s/"%(d.getVar("TOPDIR")).replace("//","/")
344 export2json(d, testdata_name, searchString=searchString, replaceString="")
345
346 if os.path.exists(testdata_name) and link_name:
347 testdata_link = os.path.join(deploy_dir, "%s.testdata.json" % link_name)
348 if testdata_link != testdata_name:
349 if os.path.lexists(testdata_link):
350 os.remove(testdata_link)
351 os.symlink(os.path.basename(testdata_name), testdata_link)
352}
353write_image_test_data[vardepsexclude] += "TOPDIR"
354
355# Check for unsatisfied recommendations (RRECOMMENDS)
356python rootfs_log_check_recommends() {
357 log_path = d.expand("${T}/log.do_rootfs")
358 with open(log_path, 'r') as log:
359 for line in log:
360 if 'log_check' in line:
361 continue
362
363 if 'unsatisfied recommendation for' in line:
364 bb.warn('[log_check] %s: %s' % (d.getVar('PN'), line))
365}
366
367# Perform any additional adjustments needed to make rootf binary reproducible
368rootfs_reproducible () {
369 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
370 # Convert UTC into %4Y%2m%2d%2H%2M%2S
371 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
372 echo $sformatted > ${IMAGE_ROOTFS}/etc/version
373 bbnote "rootfs_reproducible: set /etc/version to $sformatted"
374
375 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/gconf ]; then
376 find ${IMAGE_ROOTFS}${sysconfdir}/gconf -name '%gconf.xml' -print0 | xargs -0r \
377 sed -i -e 's@\bmtime="[0-9][0-9]*"@mtime="'${REPRODUCIBLE_TIMESTAMP_ROOTFS}'"@g'
378 fi
379 fi
380}
381
382# Perform a dumb check for unit existence, not its validity
383python overlayfs_qa_check() {
384 from oe.overlayfs import mountUnitName
385
386 overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT") or {}
387 imagepath = d.getVar("IMAGE_ROOTFS")
388 sysconfdir = d.getVar("sysconfdir")
389 searchpaths = [oe.path.join(imagepath, sysconfdir, "systemd", "system"),
390 oe.path.join(imagepath, d.getVar("systemd_system_unitdir"))]
391 fstabpath = oe.path.join(imagepath, sysconfdir, "fstab")
392
393 if not any(os.path.exists(path) for path in [*searchpaths, fstabpath]):
394 return
395
396 fstabDevices = []
397 if os.path.isfile(fstabpath):
398 with open(fstabpath, 'r') as f:
399 for line in f:
400 if line[0] == '#':
401 continue
402 path = line.split(maxsplit=2)
403 if len(path) > 2:
404 fstabDevices.append(path[1])
405
406 allUnitExist = True;
407 for mountPoint in overlayMountPoints:
408 qaSkip = (d.getVarFlag("OVERLAYFS_QA_SKIP", mountPoint) or "").split()
409 if "mount-configured" in qaSkip:
410 continue
411
412 mountPath = d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint)
413 if mountPath in fstabDevices:
414 continue
415
416 mountUnit = mountUnitName(mountPath)
417 if any(os.path.isfile(oe.path.join(dirpath, mountUnit))
418 for dirpath in searchpaths):
419 continue
420
421 bb.warn(f'Mount path {mountPath} not found in fstab and unit '
422 f'{mountUnit} not found in systemd unit directories.')
423 bb.warn(f'Skip this check by setting OVERLAYFS_QA_SKIP[{mountPoint}] = '
424 '"mount-configured"')
425 allUnitExist = False;
426
427 if not allUnitExist:
428 bb.fatal('Not all mount paths and units are installed in the image')
429}
430
431python overlayfs_postprocess() {
432 import shutil
433
434 # install helper script
435 helperScriptName = "overlayfs-create-dirs.sh"
436 helperScriptSource = oe.path.join(d.getVar("COREBASE"), "meta/files", helperScriptName)
437 helperScriptDest = oe.path.join(d.getVar("IMAGE_ROOTFS"), "/usr/sbin/", helperScriptName)
438 shutil.copyfile(helperScriptSource, helperScriptDest)
439 os.chmod(helperScriptDest, 0o755)
440}