blob: 221869e04cee4b6cb0f14ef34a1211b891f51830 [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
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700115 if [ ! -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500116 echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
117 fi
118 fi
119
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500120 if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
121 # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
122 if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
123 sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
124 fi
125 # Run populate-volatile.sh at rootfs time to set up basic files
126 # and directories to support read-only rootfs.
127 if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
128 ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
129 fi
130 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500131}
132
133#
134# This function is intended to disallow empty root password if 'debug-tweaks' is not in IMAGE_FEATURES.
135#
136zap_empty_root_password () {
137 if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then
138 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow
139 fi
140 if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then
141 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd
142 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500143}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144
145#
146# allow dropbear/openssh to accept root logins and logins from accounts with an empty password string
147#
148ssh_allow_empty_password () {
149 for config in sshd_config sshd_config_readonly; do
150 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
151 sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
152 sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
153 fi
154 done
155
156 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
157 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
158 if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then
159 sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
160 fi
161 else
162 printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
163 fi
164 fi
165
166 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500167 for f in `find ${IMAGE_ROOTFS}${sysconfdir}/pam.d/* -type f -exec test -e {} \; -print`
168 do
169 sed -i 's/nullok_secure/nullok/' $f
170 done
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500171 fi
172}
173
174ssh_disable_dns_lookup () {
175 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config ]; then
176 sed -i -e 's:#UseDNS yes:UseDNS no:' ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config
177 fi
178}
179
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500180python sort_passwd () {
181 import rootfspostcommands
182 rootfspostcommands.sort_passwd(d.expand('${IMAGE_ROOTFS}${sysconfdir}'))
183}
184
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500185#
186# Enable postinst logging if debug-tweaks is enabled
187#
188postinst_enable_logging () {
189 mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
190 echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
191 echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
192}
193
194#
195# Modify systemd default target
196#
197set_systemd_default_target () {
198 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ]; then
199 ln -sf ${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target
200 fi
201}
202
203# If /var/volatile is not empty, we have seen problems where programs such as the
204# journal make assumptions based on the contents of /var/volatile. The journal
205# would then write to /var/volatile before it was mounted, thus hiding the
206# items previously written.
207#
208# This change is to attempt to fix those types of issues in a way that doesn't
209# affect users that may not be using /var/volatile.
210empty_var_volatile () {
211 if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then
212 match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null`
213 if [ -n "$match" ]; then
214 find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete
215 fi
216 fi
217}
218
219# Turn any symbolic /sbin/init link into a file
220remove_init_link () {
221 if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
222 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
223 rm ${IMAGE_ROOTFS}/sbin/init
224 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
225 fi
226}
227
228make_zimage_symlink_relative () {
229 if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
230 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
231 fi
232}
233
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500234python write_image_manifest () {
235 from oe.rootfs import image_list_installed_packages
236 from oe.utils import format_pkg_list
237
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500238 deploy_dir = d.getVar('IMGDEPLOYDIR')
239 link_name = d.getVar('IMAGE_LINK_NAME')
240 manifest_name = d.getVar('IMAGE_MANIFEST')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500241
242 if not manifest_name:
243 return
244
245 pkgs = image_list_installed_packages(d)
246 with open(manifest_name, 'w+') as image_manifest:
247 image_manifest.write(format_pkg_list(pkgs, "ver"))
248 image_manifest.write("\n")
249
250 if os.path.exists(manifest_name):
251 manifest_link = deploy_dir + "/" + link_name + ".manifest"
252 if os.path.lexists(manifest_link):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500253 os.remove(manifest_link)
254 os.symlink(os.path.basename(manifest_name), manifest_link)
255}
256
Brad Bishop316dfdd2018-06-25 12:45:53 -0400257# Can be used to create /etc/timestamp during image construction to give a reasonably
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500258# sane default time setting
259rootfs_update_timestamp () {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400260 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
261 # Convert UTC into %4Y%2m%2d%2H%2M%2S
262 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
263 else
264 sformatted=`date -u +%4Y%2m%2d%2H%2M%2S`
265 fi
266 echo $sformatted > ${IMAGE_ROOTFS}/etc/timestamp
267 bbnote "rootfs_update_timestamp: set /etc/timestamp to $sformatted"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500268}
269
270# Prevent X from being started
271rootfs_no_x_startup () {
272 if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
273 chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm
274 fi
275}
276
277rootfs_trim_schemas () {
278 for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas
279 do
280 # Need this in case no files exist
281 if [ -e $schema ]; then
282 oe-trim-schemas $schema > $schema.new
283 mv $schema.new $schema
284 fi
285 done
286}
287
288rootfs_check_host_user_contaminated () {
289 contaminated="${WORKDIR}/host-user-contaminated.txt"
290 HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)"
291 HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)"
292
293 find "${IMAGE_ROOTFS}" -wholename "${IMAGE_ROOTFS}/home" -prune \
294 -user "$HOST_USER_UID" -o -group "$HOST_USER_GID" >"$contaminated"
295
296 if [ -s "$contaminated" ]; then
297 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."
298 cat "$contaminated" | sed "s,^, ,"
299 fi
300}
301
302# Make any absolute links in a sysroot relative
303rootfs_sysroot_relativelinks () {
304 sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT}
305}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500306
307# Generated test data json file
308python write_image_test_data() {
309 from oe.data import export2json
310
311 testdata = "%s/%s.testdata.json" % (d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('IMAGE_NAME'))
312 testdata_link = "%s/%s.testdata.json" % (d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('IMAGE_LINK_NAME'))
313
314 bb.utils.mkdirhier(os.path.dirname(testdata))
315 searchString = "%s/"%(d.getVar("TOPDIR")).replace("//","/")
316 export2json(d, testdata,searchString=searchString,replaceString="")
317
318 if testdata_link != testdata:
319 if os.path.lexists(testdata_link):
320 os.remove(testdata_link)
321 os.symlink(os.path.basename(testdata), testdata_link)
322}
323write_image_test_data[vardepsexclude] += "TOPDIR"
324
325# Check for unsatisfied recommendations (RRECOMMENDS)
326python rootfs_log_check_recommends() {
327 log_path = d.expand("${T}/log.do_rootfs")
328 with open(log_path, 'r') as log:
329 for line in log:
330 if 'log_check' in line:
331 continue
332
333 if 'unsatisfied recommendation for' in line:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500334 bb.warn('[log_check] %s: %s' % (d.getVar('PN'), line))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500335}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400336
337# Perform any additional adjustments needed to make rootf binary reproducible
338rootfs_reproducible () {
339 if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
340 # Convert UTC into %4Y%2m%2d%2H%2M%2S
341 sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S`
342 echo $sformatted > ${IMAGE_ROOTFS}/etc/version
343 bbnote "rootfs_reproducible: set /etc/version to $sformatted"
344
345 find ${IMAGE_ROOTFS}/etc/gconf -name '%gconf.xml' -print0 | xargs -0r \
346 sed -i -e 's@\bmtime="[0-9][0-9]*"@mtime="'${REPRODUCIBLE_TIMESTAMP_ROOTFS}'"@g'
347 fi
348}