blob: 552220953494a843da1c85ee6ad3ad23487b8655 [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
5# Allow dropbear/openssh to accept logins from accounts with an empty password string if debug-tweaks is enabled
6ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-empty-password' ], "ssh_allow_empty_password; ", "",d)}'
7
8# Enable postinst logging if debug-tweaks is enabled
9ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'post-install-logging' ], "postinst_enable_logging; ", "",d)}'
10
11# Create /etc/timestamp during image construction to give a reasonably sane default time setting
12ROOTFS_POSTPROCESS_COMMAND += "rootfs_update_timestamp ; "
13
14# Tweak the mount options for rootfs in /etc/fstab if read-only-rootfs is enabled
15ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", "read_only_rootfs_hook; ", "",d)}'
16
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017# We also need to do the same for the kernel boot parameters,
18# otherwise kernel or initramfs end up mounting the rootfs read/write
19# (the default) if supported by the underlying storage.
20#
21# We do this with _append because the default value might get set later with ?=
22# and we don't want to disable such a default that by setting a value here.
23APPEND_append = '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", " ro", "", d)}'
24
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025# Generates test data file with data store variables expanded in json format
26ROOTFS_POSTPROCESS_COMMAND += "write_image_test_data ; "
27
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050028# Write manifest
Patrick Williamsc0f7c042017-02-23 20:41:17 -060029IMAGE_MANIFEST = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.manifest"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030ROOTFS_POSTUNINSTALL_COMMAND =+ "write_image_manifest ; "
31# Set default postinst log file
32POSTINST_LOGFILE ?= "${localstatedir}/log/postinstall.log"
33# Set default target for systemd images
34SYSTEMD_DEFAULT_TARGET ?= '${@bb.utils.contains("IMAGE_FEATURES", "x11-base", "graphical.target", "multi-user.target", d)}'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060035ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "set_systemd_default_target; systemd_create_users;", "", d)}'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050036
37ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile;'
38
39# Disable DNS lookups, the SSH_DISABLE_DNS_LOOKUP can be overridden to allow
40# distros to choose not to take this change
41SSH_DISABLE_DNS_LOOKUP ?= " ssh_disable_dns_lookup ; "
42ROOTFS_POSTPROCESS_COMMAND_append_qemuall = "${SSH_DISABLE_DNS_LOOKUP}"
43
Brad Bishop6e60e8b2018-02-01 10:27:11 -050044# Sort the user and group entries in /etc by ID in order to make the content
45# deterministic. Package installs are not deterministic, causing the ordering
46# of entries to change between builds. In case that this isn't desired,
47# the command can be overridden.
48#
49# Note that useradd-staticids.bbclass has to be used to ensure that
50# the numeric IDs of dynamically created entries remain stable.
51#
52# We want this to run as late as possible, in particular after
53# systemd_sysusers_create and set_user_group. Using _append is not
54# enough for that, set_user_group is added that way and would end
55# up running after us.
56SORT_PASSWD_POSTPROCESS_COMMAND ??= " sort_passwd; "
57python () {
58 d.appendVar('ROOTFS_POSTPROCESS_COMMAND', '${SORT_PASSWD_POSTPROCESS_COMMAND}')
Brad Bishop316dfdd2018-06-25 12:45:53 -040059 d.appendVar('ROOTFS_POSTPROCESS_COMMAND', 'rootfs_reproducible;')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060}
61
Patrick Williamsc0f7c042017-02-23 20:41:17 -060062systemd_create_users () {
63 for conffile in ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd.conf ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd-remote.conf; do
64 [ -e $conffile ] || continue
65 grep -v "^#" $conffile | sed -e '/^$/d' | while read type name id comment; do
66 if [ "$type" = "u" ]; then
67 useradd_params="--shell /sbin/nologin"
68 [ "$id" != "-" ] && useradd_params="$useradd_params --uid $id"
69 [ "$comment" != "-" ] && useradd_params="$useradd_params --comment $comment"
70 useradd_params="$useradd_params --system $name"
71 eval useradd --root ${IMAGE_ROOTFS} $useradd_params || true
72 elif [ "$type" = "g" ]; then
73 groupadd_params=""
74 [ "$id" != "-" ] && groupadd_params="$groupadd_params --gid $id"
75 groupadd_params="$groupadd_params --system $name"
76 eval groupadd --root ${IMAGE_ROOTFS} $groupadd_params || true
77 elif [ "$type" = "m" ]; then
78 group=$id
79 if [ ! `grep -q "^${group}:" ${IMAGE_ROOTFS}${sysconfdir}/group` ]; then
80 eval groupadd --root ${IMAGE_ROOTFS} --system $group
81 fi
82 if [ ! `grep -q "^${name}:" ${IMAGE_ROOTFS}${sysconfdir}/passwd` ]; then
83 eval useradd --root ${IMAGE_ROOTFS} --shell /sbin/nologin --system $name
84 fi
85 eval usermod --root ${IMAGE_ROOTFS} -a -G $group $name
86 fi
87 done
88 done
89}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050090
91#
92# A hook function to support read-only-rootfs IMAGE_FEATURES
93#
94read_only_rootfs_hook () {
95 # Tweak the mount option and fs_passno for rootfs in fstab
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096 if [ -f ${IMAGE_ROOTFS}/etc/fstab ]; then
97 sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab
98 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050099
100 # If we're using openssh and the /etc/ssh directory has no pre-generated keys,
101 # we should configure openssh to use the configuration file /etc/ssh/sshd_config_readonly
102 # and the keys under /var/run/ssh.
103 if [ -d ${IMAGE_ROOTFS}/etc/ssh ]; then
104 if [ -e ${IMAGE_ROOTFS}/etc/ssh/ssh_host_rsa_key ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 echo "SYSCONFDIR=\${SYSCONFDIR:-/etc/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500106 echo "SSHD_OPTS=" >> ${IMAGE_ROOTFS}/etc/default/ssh
107 else
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500108 echo "SYSCONFDIR=\${SYSCONFDIR:-/var/run/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500109 echo "SSHD_OPTS='-f /etc/ssh/sshd_config_readonly'" >> ${IMAGE_ROOTFS}/etc/default/ssh
110 fi
111 fi
112
113 # Also tweak the key location for dropbear in the same way.
114 if [ -d ${IMAGE_ROOTFS}/etc/dropbear ]; then
115 if [ -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then
116 echo "DROPBEAR_RSAKEY_DIR=/etc/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
117 else
118 echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
119 fi
120 fi
121
122
123 if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
124 # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
125 if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
126 sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
127 fi
128 # Run populate-volatile.sh at rootfs time to set up basic files
129 # and directories to support read-only rootfs.
130 if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
131 ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
132 fi
133 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500134}
135
136#
137# This function is intended to disallow empty root password if 'debug-tweaks' is not in IMAGE_FEATURES.
138#
139zap_empty_root_password () {
140 if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then
141 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow
142 fi
143 if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then
144 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd
145 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500147
148#
149# allow dropbear/openssh to accept root logins and logins from accounts with an empty password string
150#
151ssh_allow_empty_password () {
152 for config in sshd_config sshd_config_readonly; do
153 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
154 sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
155 sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
156 fi
157 done
158
159 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
160 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
161 if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then
162 sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
163 fi
164 else
165 printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
166 fi
167 fi
168
169 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 for f in `find ${IMAGE_ROOTFS}${sysconfdir}/pam.d/* -type f -exec test -e {} \; -print`
171 do
172 sed -i 's/nullok_secure/nullok/' $f
173 done
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500174 fi
175}
176
177ssh_disable_dns_lookup () {
178 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config ]; then
179 sed -i -e 's:#UseDNS yes:UseDNS no:' ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config
180 fi
181}
182
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500183python sort_passwd () {
184 import rootfspostcommands
185 rootfspostcommands.sort_passwd(d.expand('${IMAGE_ROOTFS}${sysconfdir}'))
186}
187
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500188#
189# Enable postinst logging if debug-tweaks is enabled
190#
191postinst_enable_logging () {
192 mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
193 echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
194 echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
195}
196
197#
198# Modify systemd default target
199#
200set_systemd_default_target () {
201 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ]; then
202 ln -sf ${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target
203 fi
204}
205
206# If /var/volatile is not empty, we have seen problems where programs such as the
207# journal make assumptions based on the contents of /var/volatile. The journal
208# would then write to /var/volatile before it was mounted, thus hiding the
209# items previously written.
210#
211# This change is to attempt to fix those types of issues in a way that doesn't
212# affect users that may not be using /var/volatile.
213empty_var_volatile () {
214 if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then
215 match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null`
216 if [ -n "$match" ]; then
217 find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete
218 fi
219 fi
220}
221
222# Turn any symbolic /sbin/init link into a file
223remove_init_link () {
224 if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
225 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
226 rm ${IMAGE_ROOTFS}/sbin/init
227 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
228 fi
229}
230
231make_zimage_symlink_relative () {
232 if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
233 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
234 fi
235}
236
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500237python write_image_manifest () {
238 from oe.rootfs import image_list_installed_packages
239 from oe.utils import format_pkg_list
240
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500241 deploy_dir = d.getVar('IMGDEPLOYDIR')
242 link_name = d.getVar('IMAGE_LINK_NAME')
243 manifest_name = d.getVar('IMAGE_MANIFEST')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500244
245 if not manifest_name:
246 return
247
248 pkgs = image_list_installed_packages(d)
249 with open(manifest_name, 'w+') as image_manifest:
250 image_manifest.write(format_pkg_list(pkgs, "ver"))
251 image_manifest.write("\n")
252
253 if os.path.exists(manifest_name):
254 manifest_link = deploy_dir + "/" + link_name + ".manifest"
255 if os.path.lexists(manifest_link):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500256 os.remove(manifest_link)
257 os.symlink(os.path.basename(manifest_name), manifest_link)
258}
259
Brad Bishop316dfdd2018-06-25 12:45:53 -0400260# Can be used to create /etc/timestamp during image construction to give a reasonably
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500261# sane default time setting
262rootfs_update_timestamp () {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400263 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
264 # Convert UTC into %4Y%2m%2d%2H%2M%2S
265 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
266 else
267 sformatted=`date -u +%4Y%2m%2d%2H%2M%2S`
268 fi
269 echo $sformatted > ${IMAGE_ROOTFS}/etc/timestamp
270 bbnote "rootfs_update_timestamp: set /etc/timestamp to $sformatted"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500271}
272
273# Prevent X from being started
274rootfs_no_x_startup () {
275 if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
276 chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm
277 fi
278}
279
280rootfs_trim_schemas () {
281 for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas
282 do
283 # Need this in case no files exist
284 if [ -e $schema ]; then
285 oe-trim-schemas $schema > $schema.new
286 mv $schema.new $schema
287 fi
288 done
289}
290
291rootfs_check_host_user_contaminated () {
292 contaminated="${WORKDIR}/host-user-contaminated.txt"
293 HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)"
294 HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)"
295
296 find "${IMAGE_ROOTFS}" -wholename "${IMAGE_ROOTFS}/home" -prune \
297 -user "$HOST_USER_UID" -o -group "$HOST_USER_GID" >"$contaminated"
298
299 if [ -s "$contaminated" ]; then
300 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."
301 cat "$contaminated" | sed "s,^, ,"
302 fi
303}
304
305# Make any absolute links in a sysroot relative
306rootfs_sysroot_relativelinks () {
307 sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT}
308}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500309
310# Generated test data json file
311python write_image_test_data() {
312 from oe.data import export2json
313
314 testdata = "%s/%s.testdata.json" % (d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('IMAGE_NAME'))
315 testdata_link = "%s/%s.testdata.json" % (d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('IMAGE_LINK_NAME'))
316
317 bb.utils.mkdirhier(os.path.dirname(testdata))
318 searchString = "%s/"%(d.getVar("TOPDIR")).replace("//","/")
319 export2json(d, testdata,searchString=searchString,replaceString="")
320
321 if testdata_link != testdata:
322 if os.path.lexists(testdata_link):
323 os.remove(testdata_link)
324 os.symlink(os.path.basename(testdata), testdata_link)
325}
326write_image_test_data[vardepsexclude] += "TOPDIR"
327
328# Check for unsatisfied recommendations (RRECOMMENDS)
329python rootfs_log_check_recommends() {
330 log_path = d.expand("${T}/log.do_rootfs")
331 with open(log_path, 'r') as log:
332 for line in log:
333 if 'log_check' in line:
334 continue
335
336 if 'unsatisfied recommendation for' in line:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500337 bb.warn('[log_check] %s: %s' % (d.getVar('PN'), line))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500338}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400339
340# Perform any additional adjustments needed to make rootf binary reproducible
341rootfs_reproducible () {
342 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
343 # Convert UTC into %4Y%2m%2d%2H%2M%2S
344 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
345 echo $sformatted > ${IMAGE_ROOTFS}/etc/version
346 bbnote "rootfs_reproducible: set /etc/version to $sformatted"
347
348 find ${IMAGE_ROOTFS}/etc/gconf -name '%gconf.xml' -print0 | xargs -0r \
349 sed -i -e 's@\bmtime="[0-9][0-9]*"@mtime="'${REPRODUCIBLE_TIMESTAMP_ROOTFS}'"@g'
350 fi
351}