blob: bde58ad6cd3670986d616201095708b32fd22f63 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001
2# Zap the root password if debug-tweaks feature is not enabled
3ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'empty-root-password' ], "", "zap_empty_root_password ; ",d)}'
4
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08005# Allow dropbear/openssh to accept logins from accounts with an empty password string if debug-tweaks or allow-empty-password is enabled
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05006ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-empty-password' ], "ssh_allow_empty_password; ", "",d)}'
7
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08008# Allow dropbear/openssh to accept root logins if debug-tweaks or allow-root-login is enabled
9ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-root-login' ], "ssh_allow_root_login; ", "",d)}'
10
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050011# Enable postinst logging if debug-tweaks is enabled
12ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'post-install-logging' ], "postinst_enable_logging; ", "",d)}'
13
14# Create /etc/timestamp during image construction to give a reasonably sane default time setting
15ROOTFS_POSTPROCESS_COMMAND += "rootfs_update_timestamp ; "
16
17# Tweak the mount options for rootfs in /etc/fstab if read-only-rootfs is enabled
18ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", "read_only_rootfs_hook; ", "",d)}'
19
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020# We also need to do the same for the kernel boot parameters,
21# otherwise kernel or initramfs end up mounting the rootfs read/write
22# (the default) if supported by the underlying storage.
23#
24# We do this with _append because the default value might get set later with ?=
25# and we don't want to disable such a default that by setting a value here.
26APPEND_append = '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", " ro", "", d)}'
27
Brad Bishop6e60e8b2018-02-01 10:27:11 -050028# Generates test data file with data store variables expanded in json format
29ROOTFS_POSTPROCESS_COMMAND += "write_image_test_data ; "
30
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050031# Write manifest
Patrick Williamsc0f7c042017-02-23 20:41:17 -060032IMAGE_MANIFEST = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.manifest"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050033ROOTFS_POSTUNINSTALL_COMMAND =+ "write_image_manifest ; "
34# Set default postinst log file
35POSTINST_LOGFILE ?= "${localstatedir}/log/postinstall.log"
36# Set default target for systemd images
37SYSTEMD_DEFAULT_TARGET ?= '${@bb.utils.contains("IMAGE_FEATURES", "x11-base", "graphical.target", "multi-user.target", d)}'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060038ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "set_systemd_default_target; systemd_create_users;", "", d)}'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050039
40ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile;'
41
Brad Bishop6e60e8b2018-02-01 10:27:11 -050042# Sort the user and group entries in /etc by ID in order to make the content
43# deterministic. Package installs are not deterministic, causing the ordering
44# of entries to change between builds. In case that this isn't desired,
45# the command can be overridden.
46#
47# Note that useradd-staticids.bbclass has to be used to ensure that
48# the numeric IDs of dynamically created entries remain stable.
49#
50# We want this to run as late as possible, in particular after
51# systemd_sysusers_create and set_user_group. Using _append is not
52# enough for that, set_user_group is added that way and would end
53# up running after us.
54SORT_PASSWD_POSTPROCESS_COMMAND ??= " sort_passwd; "
55python () {
56 d.appendVar('ROOTFS_POSTPROCESS_COMMAND', '${SORT_PASSWD_POSTPROCESS_COMMAND}')
Brad Bishop316dfdd2018-06-25 12:45:53 -040057 d.appendVar('ROOTFS_POSTPROCESS_COMMAND', 'rootfs_reproducible;')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050058}
59
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060systemd_create_users () {
61 for conffile in ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd.conf ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd-remote.conf; do
62 [ -e $conffile ] || continue
63 grep -v "^#" $conffile | sed -e '/^$/d' | while read type name id comment; do
64 if [ "$type" = "u" ]; then
65 useradd_params="--shell /sbin/nologin"
66 [ "$id" != "-" ] && useradd_params="$useradd_params --uid $id"
67 [ "$comment" != "-" ] && useradd_params="$useradd_params --comment $comment"
68 useradd_params="$useradd_params --system $name"
69 eval useradd --root ${IMAGE_ROOTFS} $useradd_params || true
70 elif [ "$type" = "g" ]; then
71 groupadd_params=""
72 [ "$id" != "-" ] && groupadd_params="$groupadd_params --gid $id"
73 groupadd_params="$groupadd_params --system $name"
74 eval groupadd --root ${IMAGE_ROOTFS} $groupadd_params || true
75 elif [ "$type" = "m" ]; then
76 group=$id
77 if [ ! `grep -q "^${group}:" ${IMAGE_ROOTFS}${sysconfdir}/group` ]; then
78 eval groupadd --root ${IMAGE_ROOTFS} --system $group
79 fi
80 if [ ! `grep -q "^${name}:" ${IMAGE_ROOTFS}${sysconfdir}/passwd` ]; then
81 eval useradd --root ${IMAGE_ROOTFS} --shell /sbin/nologin --system $name
82 fi
83 eval usermod --root ${IMAGE_ROOTFS} -a -G $group $name
84 fi
85 done
86 done
87}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088
89#
90# A hook function to support read-only-rootfs IMAGE_FEATURES
91#
92read_only_rootfs_hook () {
93 # Tweak the mount option and fs_passno for rootfs in fstab
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094 if [ -f ${IMAGE_ROOTFS}/etc/fstab ]; then
95 sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab
96 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050097
98 # If we're using openssh and the /etc/ssh directory has no pre-generated keys,
99 # we should configure openssh to use the configuration file /etc/ssh/sshd_config_readonly
100 # and the keys under /var/run/ssh.
101 if [ -d ${IMAGE_ROOTFS}/etc/ssh ]; then
102 if [ -e ${IMAGE_ROOTFS}/etc/ssh/ssh_host_rsa_key ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 echo "SYSCONFDIR=\${SYSCONFDIR:-/etc/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500104 echo "SSHD_OPTS=" >> ${IMAGE_ROOTFS}/etc/default/ssh
105 else
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 echo "SYSCONFDIR=\${SYSCONFDIR:-/var/run/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 echo "SSHD_OPTS='-f /etc/ssh/sshd_config_readonly'" >> ${IMAGE_ROOTFS}/etc/default/ssh
108 fi
109 fi
110
111 # Also tweak the key location for dropbear in the same way.
112 if [ -d ${IMAGE_ROOTFS}/etc/dropbear ]; then
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700113 if [ ! -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500114 echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
115 fi
116 fi
117
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500118 if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
119 # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
120 if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
121 sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
122 fi
123 # Run populate-volatile.sh at rootfs time to set up basic files
124 # and directories to support read-only rootfs.
125 if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
126 ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
127 fi
128 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500129}
130
131#
132# This function is intended to disallow empty root password if 'debug-tweaks' is not in IMAGE_FEATURES.
133#
134zap_empty_root_password () {
135 if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then
136 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow
137 fi
138 if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then
139 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd
140 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500141}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500142
143#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800144# allow dropbear/openssh to accept logins from accounts with an empty password string
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500145#
146ssh_allow_empty_password () {
147 for config in sshd_config sshd_config_readonly; do
148 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149 sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
150 fi
151 done
152
153 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
154 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
155 if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then
156 sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
157 fi
158 else
159 printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
160 fi
161 fi
162
163 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 for f in `find ${IMAGE_ROOTFS}${sysconfdir}/pam.d/* -type f -exec test -e {} \; -print`
165 do
166 sed -i 's/nullok_secure/nullok/' $f
167 done
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168 fi
169}
170
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800171#
172# allow dropbear/openssh to accept root logins
173#
174ssh_allow_root_login () {
175 for config in sshd_config sshd_config_readonly; do
176 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
177 sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
178 fi
179 done
180
181 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
182 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
183 sed -i '/^DROPBEAR_EXTRA_ARGS=/ s/-w//' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
184 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500185 fi
186}
187
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500188python sort_passwd () {
189 import rootfspostcommands
190 rootfspostcommands.sort_passwd(d.expand('${IMAGE_ROOTFS}${sysconfdir}'))
191}
192
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500193#
194# Enable postinst logging if debug-tweaks is enabled
195#
196postinst_enable_logging () {
197 mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
198 echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
199 echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
200}
201
202#
203# Modify systemd default target
204#
205set_systemd_default_target () {
206 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ]; then
207 ln -sf ${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target
208 fi
209}
210
211# If /var/volatile is not empty, we have seen problems where programs such as the
212# journal make assumptions based on the contents of /var/volatile. The journal
213# would then write to /var/volatile before it was mounted, thus hiding the
214# items previously written.
215#
216# This change is to attempt to fix those types of issues in a way that doesn't
217# affect users that may not be using /var/volatile.
218empty_var_volatile () {
219 if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then
220 match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null`
221 if [ -n "$match" ]; then
222 find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete
223 fi
224 fi
225}
226
227# Turn any symbolic /sbin/init link into a file
228remove_init_link () {
229 if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
230 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
231 rm ${IMAGE_ROOTFS}/sbin/init
232 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
233 fi
234}
235
236make_zimage_symlink_relative () {
237 if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
238 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
239 fi
240}
241
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500242python write_image_manifest () {
243 from oe.rootfs import image_list_installed_packages
244 from oe.utils import format_pkg_list
245
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500246 deploy_dir = d.getVar('IMGDEPLOYDIR')
247 link_name = d.getVar('IMAGE_LINK_NAME')
248 manifest_name = d.getVar('IMAGE_MANIFEST')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500249
250 if not manifest_name:
251 return
252
253 pkgs = image_list_installed_packages(d)
254 with open(manifest_name, 'w+') as image_manifest:
255 image_manifest.write(format_pkg_list(pkgs, "ver"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500256
257 if os.path.exists(manifest_name):
258 manifest_link = deploy_dir + "/" + link_name + ".manifest"
259 if os.path.lexists(manifest_link):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500260 os.remove(manifest_link)
261 os.symlink(os.path.basename(manifest_name), manifest_link)
262}
263
Brad Bishop316dfdd2018-06-25 12:45:53 -0400264# Can be used to create /etc/timestamp during image construction to give a reasonably
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500265# sane default time setting
266rootfs_update_timestamp () {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400267 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
268 # Convert UTC into %4Y%2m%2d%2H%2M%2S
269 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
270 else
271 sformatted=`date -u +%4Y%2m%2d%2H%2M%2S`
272 fi
273 echo $sformatted > ${IMAGE_ROOTFS}/etc/timestamp
274 bbnote "rootfs_update_timestamp: set /etc/timestamp to $sformatted"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500275}
276
277# Prevent X from being started
278rootfs_no_x_startup () {
279 if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
280 chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm
281 fi
282}
283
284rootfs_trim_schemas () {
285 for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas
286 do
287 # Need this in case no files exist
288 if [ -e $schema ]; then
289 oe-trim-schemas $schema > $schema.new
290 mv $schema.new $schema
291 fi
292 done
293}
294
295rootfs_check_host_user_contaminated () {
296 contaminated="${WORKDIR}/host-user-contaminated.txt"
297 HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)"
298 HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)"
299
300 find "${IMAGE_ROOTFS}" -wholename "${IMAGE_ROOTFS}/home" -prune \
301 -user "$HOST_USER_UID" -o -group "$HOST_USER_GID" >"$contaminated"
302
303 if [ -s "$contaminated" ]; then
304 echo "WARNING: Paths in the rootfs are owned by the same user or group as the user running bitbake. See the logfile for the specific paths."
305 cat "$contaminated" | sed "s,^, ,"
306 fi
307}
308
309# Make any absolute links in a sysroot relative
310rootfs_sysroot_relativelinks () {
311 sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT}
312}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500313
314# Generated test data json file
315python write_image_test_data() {
316 from oe.data import export2json
317
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800318 deploy_dir = d.getVar('IMGDEPLOYDIR')
319 link_name = d.getVar('IMAGE_LINK_NAME')
320 testdata_name = os.path.join(deploy_dir, "%s.testdata.json" % d.getVar('IMAGE_NAME'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500321
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500322 searchString = "%s/"%(d.getVar("TOPDIR")).replace("//","/")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800323 export2json(d, testdata_name, searchString=searchString, replaceString="")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500324
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800325 if os.path.exists(testdata_name):
326 testdata_link = os.path.join(deploy_dir, "%s.testdata.json" % link_name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500327 if os.path.lexists(testdata_link):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800328 os.remove(testdata_link)
329 os.symlink(os.path.basename(testdata_name), testdata_link)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500330}
331write_image_test_data[vardepsexclude] += "TOPDIR"
332
333# Check for unsatisfied recommendations (RRECOMMENDS)
334python rootfs_log_check_recommends() {
335 log_path = d.expand("${T}/log.do_rootfs")
336 with open(log_path, 'r') as log:
337 for line in log:
338 if 'log_check' in line:
339 continue
340
341 if 'unsatisfied recommendation for' in line:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500342 bb.warn('[log_check] %s: %s' % (d.getVar('PN'), line))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500343}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400344
345# Perform any additional adjustments needed to make rootf binary reproducible
346rootfs_reproducible () {
347 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
348 # Convert UTC into %4Y%2m%2d%2H%2M%2S
349 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
350 echo $sformatted > ${IMAGE_ROOTFS}/etc/version
351 bbnote "rootfs_reproducible: set /etc/version to $sformatted"
352
353 find ${IMAGE_ROOTFS}/etc/gconf -name '%gconf.xml' -print0 | xargs -0r \
354 sed -i -e 's@\bmtime="[0-9][0-9]*"@mtime="'${REPRODUCIBLE_TIMESTAMP_ROOTFS}'"@g'
355 fi
356}