blob: 163c7f41b112609fd01bee3abc59244e3ba09db4 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
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
Andrew Geissler5082cc72023-09-11 08:41:39 -04008ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'empty-root-password' ], "", "zap_empty_root_password ",d)}'
Patrick Williams92b42cb2022-09-03 06:53:57 -05009
10# Allow dropbear/openssh to accept logins from accounts with an empty password string if debug-tweaks or allow-empty-password is enabled
Andrew Geissler5082cc72023-09-11 08:41:39 -040011ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-empty-password' ], "ssh_allow_empty_password ", "",d)}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050012
13# Allow dropbear/openssh to accept root logins if debug-tweaks or allow-root-login is enabled
Andrew Geissler5082cc72023-09-11 08:41:39 -040014ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-root-login' ], "ssh_allow_root_login ", "",d)}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050015
16# Autologin the root user on the serial console, if empty-root-password and serial-autologin-root are active
Andrew Geissler5082cc72023-09-11 08:41:39 -040017ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", [ 'empty-root-password', 'serial-autologin-root' ], "serial_autologin_root ", "",d)}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050018
19# Enable postinst logging if debug-tweaks or post-install-logging is enabled
Andrew Geissler5082cc72023-09-11 08:41:39 -040020ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'post-install-logging' ], "postinst_enable_logging ", "",d)}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050021
22# Create /etc/timestamp during image construction to give a reasonably sane default time setting
Andrew Geissler5082cc72023-09-11 08:41:39 -040023ROOTFS_POSTPROCESS_COMMAND += "rootfs_update_timestamp "
Patrick Williams92b42cb2022-09-03 06:53:57 -050024
Andrew Geissler87f5cff2022-09-30 13:13:31 -050025# Tweak files in /etc if read-only-rootfs is enabled
Andrew Geissler5082cc72023-09-11 08:41:39 -040026ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", "read_only_rootfs_hook ", "",d)}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050027
28# We also need to do the same for the kernel boot parameters,
29# otherwise kernel or initramfs end up mounting the rootfs read/write
30# (the default) if supported by the underlying storage.
31#
32# We do this with :append because the default value might get set later with ?=
33# and we don't want to disable such a default that by setting a value here.
34APPEND:append = '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", " ro", "", d)}'
35
36# Generates test data file with data store variables expanded in json format
Andrew Geissler5082cc72023-09-11 08:41:39 -040037ROOTFS_POSTPROCESS_COMMAND += "write_image_test_data "
Patrick Williams92b42cb2022-09-03 06:53:57 -050038
39# Write manifest
Andrew Geissler8f840682023-07-21 09:09:43 -050040IMAGE_MANIFEST = "${IMGDEPLOYDIR}/${IMAGE_NAME}.manifest"
Andrew Geissler5082cc72023-09-11 08:41:39 -040041ROOTFS_POSTUNINSTALL_COMMAND =+ "write_image_manifest"
Patrick Williams92b42cb2022-09-03 06:53:57 -050042# Set default postinst log file
43POSTINST_LOGFILE ?= "${localstatedir}/log/postinstall.log"
44# Set default target for systemd images
45SYSTEMD_DEFAULT_TARGET ?= '${@bb.utils.contains_any("IMAGE_FEATURES", [ "x11-base", "weston" ], "graphical.target", "multi-user.target", d)}'
Andrew Geissler5082cc72023-09-11 08:41:39 -040046ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "set_systemd_default_target systemd_sysusers_check", "", d)}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050047
Andrew Geissler5082cc72023-09-11 08:41:39 -040048ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile'
Patrick Williams92b42cb2022-09-03 06:53:57 -050049
Andrew Geissler5082cc72023-09-11 08:41:39 -040050ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "overlayfs", "overlayfs_qa_check overlayfs_postprocess", "", d)}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050051
52inherit image-artifact-names
53
54# Sort the user and group entries in /etc by ID in order to make the content
55# deterministic. Package installs are not deterministic, causing the ordering
56# of entries to change between builds. In case that this isn't desired,
57# the command can be overridden.
Patrick Williams56b44a92024-01-19 08:49:29 -060058SORT_PASSWD_POSTPROCESS_COMMAND ??= "tidy_shadowutils_files"
59ROOTFS_POSTPROCESS_COMMAND += '${SORT_PASSWD_POSTPROCESS_COMMAND}'
60
Patrick Williams92b42cb2022-09-03 06:53:57 -050061#
62# Note that useradd-staticids.bbclass has to be used to ensure that
63# the numeric IDs of dynamically created entries remain stable.
64#
Patrick Williams56b44a92024-01-19 08:49:29 -060065ROOTFS_POSTPROCESS_COMMAND += 'rootfs_reproducible'
Patrick Williams92b42cb2022-09-03 06:53:57 -050066
Patrick Williams520786c2023-06-25 16:20:36 -050067# Resolve the ID as described in the sysusers.d(5) manual: ID can be a numeric
68# uid, a couple uid:gid or uid:groupname or it is '-' meaning leaving it
69# automatic or it can be a path. In the latter, the uid/gid matches the
70# user/group owner of that file.
71def resolve_sysusers_id(d, sid):
72 # If the id is a path, the uid/gid matchs to the target's uid/gid in the
73 # rootfs.
74 if '/' in sid:
75 try:
76 osstat = os.stat(os.path.join(d.getVar('IMAGE_ROOTFS'), sid))
77 except FileNotFoundError:
78 bb.error('sysusers.d: file %s is required but it does not exist in the rootfs', sid)
79 return ('-', '-')
80 return (osstat.st_uid, osstat.st_gid)
81 # Else it is a uid:gid or uid:groupname syntax
82 if ':' in sid:
83 return sid.split(':')
84 else:
85 return (sid, '-')
86
87# Check a user exists in the rootfs password file and return its properties
88def check_user_exists(d, uname=None, uid=None):
89 with open(os.path.join(d.getVar('IMAGE_ROOTFS'), 'etc/passwd'), 'r') as pwfile:
90 for line in pwfile:
91 (name, _, u_id, gid, comment, homedir, ushell) = line.strip().split(':')
92 if uname == name or uid == u_id:
93 return (name, u_id, gid, comment or '-', homedir or '/', ushell or '-')
94 return None
95
96# Check a group exists in the rootfs group file and return its properties
97def check_group_exists(d, gname=None, gid=None):
98 with open(os.path.join(d.getVar('IMAGE_ROOTFS'), 'etc/group'), 'r') as gfile:
99 for line in gfile:
100 (name, _, g_id, _) = line.strip().split(':')
101 if name == gname or g_id == gid:
102 return (name, g_id)
103 return None
104
105def compare_users(user, e_user):
106 # user and e_user must not have None values. Unset values must be '-'.
107 (name, uid, gid, comment, homedir, ushell) = user
108 (e_name, e_uid, e_gid, e_comment, e_homedir, e_ushell) = e_user
109 # Ignore 'uid', 'gid' or 'comment' if they are not set
110 # Ignore 'shell' and 'ushell' if one is not set
111 return name == e_name \
112 and (uid == '-' or uid == e_uid) \
113 and (gid == '-' or gid == e_gid) \
114 and (comment == '-' or e_comment == '-' or comment.lower() == e_comment.lower()) \
115 and (homedir == '-' or e_homedir == '-' or homedir == e_homedir) \
116 and (ushell == '-' or e_ushell == '-' or ushell == e_ushell)
117
118# Open sysusers.d configuration files and parse each line to check the users and
119# groups are already defined in /etc/passwd and /etc/groups with similar
120# properties. Refer to the sysusers.d(5) manual for its syntax.
121python systemd_sysusers_check() {
122 import glob
123 import re
124
125 pattern_comment = r'(-|\"[^:\"]+\")'
126 pattern_word = r'[^\s]+'
127 pattern_line = r'(' + pattern_word + r')\s+(' + pattern_word + r')\s+(' + pattern_word + r')(\s+' \
128 + pattern_comment + r')?' + r'(\s+(' + pattern_word + r'))?' + r'(\s+(' + pattern_word + r'))?'
129
130 for conffile in glob.glob(os.path.join(d.getVar('IMAGE_ROOTFS'), 'usr/lib/sysusers.d/*.conf')):
131 with open(conffile, 'r') as f:
132 for line in f:
133 line = line.strip()
134 if not len(line) or line[0] == '#': continue
135 ret = re.fullmatch(pattern_line, line.strip())
136 if not ret: continue
137 (stype, sname, sid, _, scomment, _, shomedir, _, sshell) = ret.groups()
138 if stype == 'u':
139 if sid:
140 (suid, sgid) = resolve_sysusers_id(d, sid)
141 if sgid.isalpha():
142 sgid = check_group_exists(d, gname=sgid)
143 elif sgid.isdigit():
144 check_group_exists(d, gid=sgid)
145 else:
146 sgid = '-'
147 else:
148 suid = '-'
149 sgid = '-'
150 scomment = scomment.replace('"', '') if scomment else '-'
151 shomedir = shomedir or '-'
152 sshell = sshell or '-'
153 e_user = check_user_exists(d, uname=sname)
154 if not e_user:
155 bb.warn('User %s has never been defined' % sname)
156 elif not compare_users((sname, suid, sgid, scomment, shomedir, sshell), e_user):
157 bb.warn('User %s has been defined as (%s) but sysusers.d expects it as (%s)'
158 % (sname, ', '.join(e_user),
159 ', '.join((sname, suid, sgid, scomment, shomedir, sshell))))
160 elif stype == 'g':
161 gid = sid or '-'
162 if '/' in gid:
163 (_, gid) = resolve_sysusers_id(d, sid)
164 e_group = check_group_exists(d, gname=sname)
165 if not e_group:
166 bb.warn('Group %s has never been defined' % sname)
167 elif gid != '-':
168 (_, e_gid) = e_group
169 if gid != e_gid:
170 bb.warn('Group %s has been defined with id (%s) but sysusers.d expects gid (%s)'
171 % (sname, e_gid, gid))
172 elif stype == 'm':
173 check_user_exists(d, sname)
174 check_group_exists(d, sid)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500175}
176
177#
178# A hook function to support read-only-rootfs IMAGE_FEATURES
179#
180read_only_rootfs_hook () {
181 # Tweak the mount option and fs_passno for rootfs in fstab
182 if [ -f ${IMAGE_ROOTFS}/etc/fstab ]; then
183 sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab
184 fi
185
186 # Tweak the "mount -o remount,rw /" command in busybox-inittab inittab
187 if [ -f ${IMAGE_ROOTFS}/etc/inittab ]; then
188 sed -i 's|/bin/mount -o remount,rw /|/bin/mount -o remount,ro /|' ${IMAGE_ROOTFS}/etc/inittab
189 fi
190
191 # If we're using openssh and the /etc/ssh directory has no pre-generated keys,
192 # we should configure openssh to use the configuration file /etc/ssh/sshd_config_readonly
193 # and the keys under /var/run/ssh.
Andrew Geissler87f5cff2022-09-30 13:13:31 -0500194 # If overlayfs-etc is used this is not done as /etc is treated as writable
195 # If stateless-rootfs is enabled this is always done as we don't want to save keys then
196 if ${@ 'true' if not bb.utils.contains('IMAGE_FEATURES', 'overlayfs-etc', True, False, d) or bb.utils.contains('IMAGE_FEATURES', 'stateless-rootfs', True, False, d) else 'false'}; then
197 if [ -d ${IMAGE_ROOTFS}/etc/ssh ]; then
198 if [ -e ${IMAGE_ROOTFS}/etc/ssh/ssh_host_rsa_key ]; then
199 echo "SYSCONFDIR=\${SYSCONFDIR:-/etc/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
200 echo "SSHD_OPTS=" >> ${IMAGE_ROOTFS}/etc/default/ssh
201 else
202 echo "SYSCONFDIR=\${SYSCONFDIR:-/var/run/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
203 echo "SSHD_OPTS='-f /etc/ssh/sshd_config_readonly'" >> ${IMAGE_ROOTFS}/etc/default/ssh
204 fi
Patrick Williams92b42cb2022-09-03 06:53:57 -0500205 fi
Patrick Williams92b42cb2022-09-03 06:53:57 -0500206
Andrew Geissler87f5cff2022-09-30 13:13:31 -0500207 # Also tweak the key location for dropbear in the same way.
208 if [ -d ${IMAGE_ROOTFS}/etc/dropbear ]; then
209 if [ ! -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then
210 echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
211 fi
Patrick Williams92b42cb2022-09-03 06:53:57 -0500212 fi
213 fi
214
215 if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
216 # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
217 if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
218 sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
219 fi
220 # Run populate-volatile.sh at rootfs time to set up basic files
221 # and directories to support read-only rootfs.
222 if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
223 ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
224 fi
225 fi
226
227 if ${@bb.utils.contains("DISTRO_FEATURES", "systemd", "true", "false", d)}; then
228 # Create machine-id
229 # 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
230 touch ${IMAGE_ROOTFS}${sysconfdir}/machine-id
231 fi
232}
233
234#
235# This function disallows empty root passwords
236#
237zap_empty_root_password () {
238 if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then
Patrick Williamsac13d5f2023-11-24 18:59:46 -0600239 sed --follow-symlinks -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow
Patrick Williams92b42cb2022-09-03 06:53:57 -0500240 fi
241 if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then
Patrick Williamsac13d5f2023-11-24 18:59:46 -0600242 sed --follow-symlinks -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd
Patrick Williams92b42cb2022-09-03 06:53:57 -0500243 fi
244}
245
246#
247# allow dropbear/openssh to accept logins from accounts with an empty password string
248#
249ssh_allow_empty_password () {
250 for config in sshd_config sshd_config_readonly; do
251 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
252 sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
253 fi
254 done
255
256 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
257 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
258 if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then
259 sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
260 fi
261 else
262 printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
263 fi
264 fi
265
266 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then
267 for f in `find ${IMAGE_ROOTFS}${sysconfdir}/pam.d/* -type f -exec test -e {} \; -print`
268 do
269 sed -i 's/nullok_secure/nullok/' $f
270 done
271 fi
272}
273
274#
275# allow dropbear/openssh to accept root logins
276#
277ssh_allow_root_login () {
278 for config in sshd_config sshd_config_readonly; do
279 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
280 sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
281 fi
282 done
283
284 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
285 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
286 sed -i '/^DROPBEAR_EXTRA_ARGS=/ s/-w//' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
Andrew Geissler87f5cff2022-09-30 13:13:31 -0500287 sed -i '/^# Disallow root/d' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
Patrick Williams92b42cb2022-09-03 06:53:57 -0500288 fi
289 fi
290}
291
292#
293# Autologin the 'root' user on the serial terminal,
294# if empty-root-password' AND 'serial-autologin-root are enabled
295#
296serial_autologin_root () {
297 if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
298 # add autologin option to util-linux getty only
299 sed -i 's/options="/&--autologin root /' \
300 "${IMAGE_ROOTFS}${base_bindir}/start_getty"
301 elif ${@bb.utils.contains("DISTRO_FEATURES", "systemd", "true", "false", d)}; then
302 if [ -e ${IMAGE_ROOTFS}${systemd_system_unitdir}/serial-getty@.service ]; then
303 sed -i '/^\s*ExecStart\b/ s/getty /&--autologin root /' \
304 "${IMAGE_ROOTFS}${systemd_system_unitdir}/serial-getty@.service"
305 fi
306 fi
307}
308
309python tidy_shadowutils_files () {
310 import rootfspostcommands
311 rootfspostcommands.tidy_shadowutils_files(d.expand('${IMAGE_ROOTFS}${sysconfdir}'))
312}
313
314python sort_passwd () {
315 """
316 Deprecated in the favour of tidy_shadowutils_files.
317 """
318 import rootfspostcommands
319 bb.warn('[sort_passwd] You are using a deprecated function for '
320 'SORT_PASSWD_POSTPROCESS_COMMAND. The default one is now called '
321 '"tidy_shadowutils_files".')
322 rootfspostcommands.tidy_shadowutils_files(d.expand('${IMAGE_ROOTFS}${sysconfdir}'))
323}
324
325#
326# Enable postinst logging
327#
328postinst_enable_logging () {
329 mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
330 echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
331 echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
332}
333
334#
335# Modify systemd default target
336#
337set_systemd_default_target () {
338 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_system_unitdir}/${SYSTEMD_DEFAULT_TARGET} ]; then
339 ln -sf ${systemd_system_unitdir}/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target
340 fi
341}
342
343# If /var/volatile is not empty, we have seen problems where programs such as the
344# journal make assumptions based on the contents of /var/volatile. The journal
345# would then write to /var/volatile before it was mounted, thus hiding the
346# items previously written.
347#
348# This change is to attempt to fix those types of issues in a way that doesn't
349# affect users that may not be using /var/volatile.
350empty_var_volatile () {
351 if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then
352 match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null`
353 if [ -n "$match" ]; then
354 find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete
355 fi
356 fi
357}
358
359# Turn any symbolic /sbin/init link into a file
360remove_init_link () {
361 if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
362 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
363 rm ${IMAGE_ROOTFS}/sbin/init
364 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
365 fi
366}
367
368make_zimage_symlink_relative () {
369 if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
370 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
371 fi
372}
373
374python write_image_manifest () {
375 from oe.rootfs import image_list_installed_packages
376 from oe.utils import format_pkg_list
377
378 deploy_dir = d.getVar('IMGDEPLOYDIR')
379 link_name = d.getVar('IMAGE_LINK_NAME')
380 manifest_name = d.getVar('IMAGE_MANIFEST')
381
382 if not manifest_name:
383 return
384
385 pkgs = image_list_installed_packages(d)
386 with open(manifest_name, 'w+') as image_manifest:
387 image_manifest.write(format_pkg_list(pkgs, "ver"))
388
389 if os.path.exists(manifest_name) and link_name:
390 manifest_link = deploy_dir + "/" + link_name + ".manifest"
391 if manifest_link != manifest_name:
392 if os.path.lexists(manifest_link):
393 os.remove(manifest_link)
394 os.symlink(os.path.basename(manifest_name), manifest_link)
395}
396
397# Can be used to create /etc/timestamp during image construction to give a reasonably
398# sane default time setting
399rootfs_update_timestamp () {
400 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
401 # Convert UTC into %4Y%2m%2d%2H%2M%2S
402 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
403 else
404 sformatted=`date -u +%4Y%2m%2d%2H%2M%2S`
405 fi
406 echo $sformatted > ${IMAGE_ROOTFS}/etc/timestamp
407 bbnote "rootfs_update_timestamp: set /etc/timestamp to $sformatted"
408}
409
410# Prevent X from being started
411rootfs_no_x_startup () {
412 if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
413 chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm
414 fi
415}
416
417rootfs_trim_schemas () {
418 for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas
419 do
420 # Need this in case no files exist
421 if [ -e $schema ]; then
422 oe-trim-schemas $schema > $schema.new
423 mv $schema.new $schema
424 fi
425 done
426}
427
428rootfs_check_host_user_contaminated () {
429 contaminated="${S}/host-user-contaminated.txt"
430 HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)"
431 HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)"
432
433 find "${IMAGE_ROOTFS}" -path "${IMAGE_ROOTFS}/home" -prune -o \
434 -user "$HOST_USER_UID" -print -o -group "$HOST_USER_GID" -print >"$contaminated"
435
436 sed -e "s,${IMAGE_ROOTFS},," $contaminated | while read line; do
437 bbwarn "Path in the rootfs is owned by the same user or group as the user running bitbake:" $line `ls -lan ${IMAGE_ROOTFS}/$line`
438 done
439
440 if [ -s "$contaminated" ]; then
441 bbwarn "/etc/passwd:" `cat ${IMAGE_ROOTFS}/etc/passwd`
442 bbwarn "/etc/group:" `cat ${IMAGE_ROOTFS}/etc/group`
443 fi
444}
445
446# Make any absolute links in a sysroot relative
447rootfs_sysroot_relativelinks () {
448 sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT}
449}
450
451# Generated test data json file
452python write_image_test_data() {
453 from oe.data import export2json
454
455 deploy_dir = d.getVar('IMGDEPLOYDIR')
456 link_name = d.getVar('IMAGE_LINK_NAME')
457 testdata_name = os.path.join(deploy_dir, "%s.testdata.json" % d.getVar('IMAGE_NAME'))
458
459 searchString = "%s/"%(d.getVar("TOPDIR")).replace("//","/")
460 export2json(d, testdata_name, searchString=searchString, replaceString="")
461
462 if os.path.exists(testdata_name) and link_name:
463 testdata_link = os.path.join(deploy_dir, "%s.testdata.json" % link_name)
464 if testdata_link != testdata_name:
465 if os.path.lexists(testdata_link):
466 os.remove(testdata_link)
467 os.symlink(os.path.basename(testdata_name), testdata_link)
468}
469write_image_test_data[vardepsexclude] += "TOPDIR"
470
471# Check for unsatisfied recommendations (RRECOMMENDS)
472python rootfs_log_check_recommends() {
473 log_path = d.expand("${T}/log.do_rootfs")
474 with open(log_path, 'r') as log:
475 for line in log:
476 if 'log_check' in line:
477 continue
478
479 if 'unsatisfied recommendation for' in line:
480 bb.warn('[log_check] %s: %s' % (d.getVar('PN'), line))
481}
482
483# Perform any additional adjustments needed to make rootf binary reproducible
484rootfs_reproducible () {
485 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
486 # Convert UTC into %4Y%2m%2d%2H%2M%2S
487 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
488 echo $sformatted > ${IMAGE_ROOTFS}/etc/version
489 bbnote "rootfs_reproducible: set /etc/version to $sformatted"
490
491 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/gconf ]; then
492 find ${IMAGE_ROOTFS}${sysconfdir}/gconf -name '%gconf.xml' -print0 | xargs -0r \
493 sed -i -e 's@\bmtime="[0-9][0-9]*"@mtime="'${REPRODUCIBLE_TIMESTAMP_ROOTFS}'"@g'
494 fi
495 fi
496}
497
498# Perform a dumb check for unit existence, not its validity
499python overlayfs_qa_check() {
500 from oe.overlayfs import mountUnitName
501
502 overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT") or {}
503 imagepath = d.getVar("IMAGE_ROOTFS")
504 sysconfdir = d.getVar("sysconfdir")
505 searchpaths = [oe.path.join(imagepath, sysconfdir, "systemd", "system"),
506 oe.path.join(imagepath, d.getVar("systemd_system_unitdir"))]
507 fstabpath = oe.path.join(imagepath, sysconfdir, "fstab")
508
509 if not any(os.path.exists(path) for path in [*searchpaths, fstabpath]):
510 return
511
512 fstabDevices = []
513 if os.path.isfile(fstabpath):
514 with open(fstabpath, 'r') as f:
515 for line in f:
516 if line[0] == '#':
517 continue
518 path = line.split(maxsplit=2)
519 if len(path) > 2:
520 fstabDevices.append(path[1])
521
522 allUnitExist = True;
523 for mountPoint in overlayMountPoints:
524 qaSkip = (d.getVarFlag("OVERLAYFS_QA_SKIP", mountPoint) or "").split()
525 if "mount-configured" in qaSkip:
526 continue
527
528 mountPath = d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint)
529 if mountPath in fstabDevices:
530 continue
531
532 mountUnit = mountUnitName(mountPath)
533 if any(os.path.isfile(oe.path.join(dirpath, mountUnit))
534 for dirpath in searchpaths):
535 continue
536
537 bb.warn(f'Mount path {mountPath} not found in fstab and unit '
538 f'{mountUnit} not found in systemd unit directories.')
539 bb.warn(f'Skip this check by setting OVERLAYFS_QA_SKIP[{mountPoint}] = '
540 '"mount-configured"')
541 allUnitExist = False;
542
543 if not allUnitExist:
544 bb.fatal('Not all mount paths and units are installed in the image')
545}
546
547python overlayfs_postprocess() {
548 import shutil
549
550 # install helper script
551 helperScriptName = "overlayfs-create-dirs.sh"
552 helperScriptSource = oe.path.join(d.getVar("COREBASE"), "meta/files", helperScriptName)
553 helperScriptDest = oe.path.join(d.getVar("IMAGE_ROOTFS"), "/usr/sbin/", helperScriptName)
554 shutil.copyfile(helperScriptSource, helperScriptDest)
555 os.chmod(helperScriptDest, 0o755)
556}