blob: a4e627fef842ea7d0600eade007952882acbf32b [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}')
59}
60
Patrick Williamsc0f7c042017-02-23 20:41:17 -060061systemd_create_users () {
62 for conffile in ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd.conf ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd-remote.conf; do
63 [ -e $conffile ] || continue
64 grep -v "^#" $conffile | sed -e '/^$/d' | while read type name id comment; do
65 if [ "$type" = "u" ]; then
66 useradd_params="--shell /sbin/nologin"
67 [ "$id" != "-" ] && useradd_params="$useradd_params --uid $id"
68 [ "$comment" != "-" ] && useradd_params="$useradd_params --comment $comment"
69 useradd_params="$useradd_params --system $name"
70 eval useradd --root ${IMAGE_ROOTFS} $useradd_params || true
71 elif [ "$type" = "g" ]; then
72 groupadd_params=""
73 [ "$id" != "-" ] && groupadd_params="$groupadd_params --gid $id"
74 groupadd_params="$groupadd_params --system $name"
75 eval groupadd --root ${IMAGE_ROOTFS} $groupadd_params || true
76 elif [ "$type" = "m" ]; then
77 group=$id
78 if [ ! `grep -q "^${group}:" ${IMAGE_ROOTFS}${sysconfdir}/group` ]; then
79 eval groupadd --root ${IMAGE_ROOTFS} --system $group
80 fi
81 if [ ! `grep -q "^${name}:" ${IMAGE_ROOTFS}${sysconfdir}/passwd` ]; then
82 eval useradd --root ${IMAGE_ROOTFS} --shell /sbin/nologin --system $name
83 fi
84 eval usermod --root ${IMAGE_ROOTFS} -a -G $group $name
85 fi
86 done
87 done
88}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050089
90#
91# A hook function to support read-only-rootfs IMAGE_FEATURES
92#
93read_only_rootfs_hook () {
94 # Tweak the mount option and fs_passno for rootfs in fstab
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095 if [ -f ${IMAGE_ROOTFS}/etc/fstab ]; then
96 sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab
97 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050098
99 # If we're using openssh and the /etc/ssh directory has no pre-generated keys,
100 # we should configure openssh to use the configuration file /etc/ssh/sshd_config_readonly
101 # and the keys under /var/run/ssh.
102 if [ -d ${IMAGE_ROOTFS}/etc/ssh ]; then
103 if [ -e ${IMAGE_ROOTFS}/etc/ssh/ssh_host_rsa_key ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500104 echo "SYSCONFDIR=\${SYSCONFDIR:-/etc/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500105 echo "SSHD_OPTS=" >> ${IMAGE_ROOTFS}/etc/default/ssh
106 else
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 echo "SYSCONFDIR=\${SYSCONFDIR:-/var/run/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500108 echo "SSHD_OPTS='-f /etc/ssh/sshd_config_readonly'" >> ${IMAGE_ROOTFS}/etc/default/ssh
109 fi
110 fi
111
112 # Also tweak the key location for dropbear in the same way.
113 if [ -d ${IMAGE_ROOTFS}/etc/dropbear ]; then
114 if [ -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then
115 echo "DROPBEAR_RSAKEY_DIR=/etc/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
116 else
117 echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
118 fi
119 fi
120
121
122 if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
123 # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
124 if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
125 sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
126 fi
127 # Run populate-volatile.sh at rootfs time to set up basic files
128 # and directories to support read-only rootfs.
129 if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
130 ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
131 fi
132 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500133}
134
135#
136# This function is intended to disallow empty root password if 'debug-tweaks' is not in IMAGE_FEATURES.
137#
138zap_empty_root_password () {
139 if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then
140 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow
141 fi
142 if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then
143 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd
144 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146
147#
148# allow dropbear/openssh to accept root logins and logins from accounts with an empty password string
149#
150ssh_allow_empty_password () {
151 for config in sshd_config sshd_config_readonly; do
152 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
153 sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
154 sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
155 fi
156 done
157
158 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
159 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
160 if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then
161 sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
162 fi
163 else
164 printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
165 fi
166 fi
167
168 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500169 for f in `find ${IMAGE_ROOTFS}${sysconfdir}/pam.d/* -type f -exec test -e {} \; -print`
170 do
171 sed -i 's/nullok_secure/nullok/' $f
172 done
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500173 fi
174}
175
176ssh_disable_dns_lookup () {
177 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config ]; then
178 sed -i -e 's:#UseDNS yes:UseDNS no:' ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config
179 fi
180}
181
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182python sort_passwd () {
183 import rootfspostcommands
184 rootfspostcommands.sort_passwd(d.expand('${IMAGE_ROOTFS}${sysconfdir}'))
185}
186
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500187#
188# Enable postinst logging if debug-tweaks is enabled
189#
190postinst_enable_logging () {
191 mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
192 echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
193 echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
194}
195
196#
197# Modify systemd default target
198#
199set_systemd_default_target () {
200 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ]; then
201 ln -sf ${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target
202 fi
203}
204
205# If /var/volatile is not empty, we have seen problems where programs such as the
206# journal make assumptions based on the contents of /var/volatile. The journal
207# would then write to /var/volatile before it was mounted, thus hiding the
208# items previously written.
209#
210# This change is to attempt to fix those types of issues in a way that doesn't
211# affect users that may not be using /var/volatile.
212empty_var_volatile () {
213 if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then
214 match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null`
215 if [ -n "$match" ]; then
216 find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete
217 fi
218 fi
219}
220
221# Turn any symbolic /sbin/init link into a file
222remove_init_link () {
223 if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
224 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
225 rm ${IMAGE_ROOTFS}/sbin/init
226 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
227 fi
228}
229
230make_zimage_symlink_relative () {
231 if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
232 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
233 fi
234}
235
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500236python write_image_manifest () {
237 from oe.rootfs import image_list_installed_packages
238 from oe.utils import format_pkg_list
239
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500240 deploy_dir = d.getVar('IMGDEPLOYDIR')
241 link_name = d.getVar('IMAGE_LINK_NAME')
242 manifest_name = d.getVar('IMAGE_MANIFEST')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500243
244 if not manifest_name:
245 return
246
247 pkgs = image_list_installed_packages(d)
248 with open(manifest_name, 'w+') as image_manifest:
249 image_manifest.write(format_pkg_list(pkgs, "ver"))
250 image_manifest.write("\n")
251
252 if os.path.exists(manifest_name):
253 manifest_link = deploy_dir + "/" + link_name + ".manifest"
254 if os.path.lexists(manifest_link):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500255 os.remove(manifest_link)
256 os.symlink(os.path.basename(manifest_name), manifest_link)
257}
258
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500259# Can be use to create /etc/timestamp during image construction to give a reasonably
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500260# sane default time setting
261rootfs_update_timestamp () {
262 date -u +%4Y%2m%2d%2H%2M%2S >${IMAGE_ROOTFS}/etc/timestamp
263}
264
265# Prevent X from being started
266rootfs_no_x_startup () {
267 if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
268 chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm
269 fi
270}
271
272rootfs_trim_schemas () {
273 for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas
274 do
275 # Need this in case no files exist
276 if [ -e $schema ]; then
277 oe-trim-schemas $schema > $schema.new
278 mv $schema.new $schema
279 fi
280 done
281}
282
283rootfs_check_host_user_contaminated () {
284 contaminated="${WORKDIR}/host-user-contaminated.txt"
285 HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)"
286 HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)"
287
288 find "${IMAGE_ROOTFS}" -wholename "${IMAGE_ROOTFS}/home" -prune \
289 -user "$HOST_USER_UID" -o -group "$HOST_USER_GID" >"$contaminated"
290
291 if [ -s "$contaminated" ]; then
292 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."
293 cat "$contaminated" | sed "s,^, ,"
294 fi
295}
296
297# Make any absolute links in a sysroot relative
298rootfs_sysroot_relativelinks () {
299 sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT}
300}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500301
302# Generated test data json file
303python write_image_test_data() {
304 from oe.data import export2json
305
306 testdata = "%s/%s.testdata.json" % (d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('IMAGE_NAME'))
307 testdata_link = "%s/%s.testdata.json" % (d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('IMAGE_LINK_NAME'))
308
309 bb.utils.mkdirhier(os.path.dirname(testdata))
310 searchString = "%s/"%(d.getVar("TOPDIR")).replace("//","/")
311 export2json(d, testdata,searchString=searchString,replaceString="")
312
313 if testdata_link != testdata:
314 if os.path.lexists(testdata_link):
315 os.remove(testdata_link)
316 os.symlink(os.path.basename(testdata), testdata_link)
317}
318write_image_test_data[vardepsexclude] += "TOPDIR"
319
320# Check for unsatisfied recommendations (RRECOMMENDS)
321python rootfs_log_check_recommends() {
322 log_path = d.expand("${T}/log.do_rootfs")
323 with open(log_path, 'r') as log:
324 for line in log:
325 if 'log_check' in line:
326 continue
327
328 if 'unsatisfied recommendation for' in line:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500329 bb.warn('[log_check] %s: %s' % (d.getVar('PN'), line))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500330}