blob: 95d28afa30b62516d63c50805428188e12f6b36e [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
17# Write manifest
18IMAGE_MANIFEST = "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.manifest"
19ROOTFS_POSTUNINSTALL_COMMAND =+ "write_image_manifest ; "
20# Set default postinst log file
21POSTINST_LOGFILE ?= "${localstatedir}/log/postinstall.log"
22# Set default target for systemd images
23SYSTEMD_DEFAULT_TARGET ?= '${@bb.utils.contains("IMAGE_FEATURES", "x11-base", "graphical.target", "multi-user.target", d)}'
24ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "set_systemd_default_target; ", "", d)}'
25
26ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile;'
27
28# Disable DNS lookups, the SSH_DISABLE_DNS_LOOKUP can be overridden to allow
29# distros to choose not to take this change
30SSH_DISABLE_DNS_LOOKUP ?= " ssh_disable_dns_lookup ; "
31ROOTFS_POSTPROCESS_COMMAND_append_qemuall = "${SSH_DISABLE_DNS_LOOKUP}"
32
33
34
35#
36# A hook function to support read-only-rootfs IMAGE_FEATURES
37#
38read_only_rootfs_hook () {
39 # Tweak the mount option and fs_passno for rootfs in fstab
40 sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab
41
42 # If we're using openssh and the /etc/ssh directory has no pre-generated keys,
43 # we should configure openssh to use the configuration file /etc/ssh/sshd_config_readonly
44 # and the keys under /var/run/ssh.
45 if [ -d ${IMAGE_ROOTFS}/etc/ssh ]; then
46 if [ -e ${IMAGE_ROOTFS}/etc/ssh/ssh_host_rsa_key ]; then
47 echo "SYSCONFDIR=/etc/ssh" >> ${IMAGE_ROOTFS}/etc/default/ssh
48 echo "SSHD_OPTS=" >> ${IMAGE_ROOTFS}/etc/default/ssh
49 else
50 echo "SYSCONFDIR=/var/run/ssh" >> ${IMAGE_ROOTFS}/etc/default/ssh
51 echo "SSHD_OPTS='-f /etc/ssh/sshd_config_readonly'" >> ${IMAGE_ROOTFS}/etc/default/ssh
52 fi
53 fi
54
55 # Also tweak the key location for dropbear in the same way.
56 if [ -d ${IMAGE_ROOTFS}/etc/dropbear ]; then
57 if [ -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then
58 echo "DROPBEAR_RSAKEY_DIR=/etc/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
59 else
60 echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
61 fi
62 fi
63
64
65 if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
66 # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
67 if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
68 sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
69 fi
70 # Run populate-volatile.sh at rootfs time to set up basic files
71 # and directories to support read-only rootfs.
72 if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
73 ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
74 fi
75 fi
76
77 if ${@bb.utils.contains("DISTRO_FEATURES", "systemd", "true", "false", d)}; then
78 # Update user database files so that services don't fail for a read-only systemd system
79 for conffile in ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd.conf ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd-remote.conf; do
80 [ -e $conffile ] || continue
81 grep -v "^#" $conffile | sed -e '/^$/d' | while read type name id comment; do
82 if [ "$type" = "u" ]; then
83 useradd_params=""
84 [ "$id" != "-" ] && useradd_params="$useradd_params --uid $id"
85 [ "$comment" != "-" ] && useradd_params="$useradd_params --comment $comment"
86 useradd_params="$useradd_params --system $name"
87 eval useradd --root ${IMAGE_ROOTFS} $useradd_params || true
88 elif [ "$type" = "g" ]; then
89 groupadd_params=""
90 [ "$id" != "-" ] && groupadd_params="$groupadd_params --gid $id"
91 groupadd_params="$groupadd_params --system $name"
92 eval groupadd --root ${IMAGE_ROOTFS} $groupadd_params || true
93 fi
94 done
95 done
96 fi
97}
98
99#
100# This function is intended to disallow empty root password if 'debug-tweaks' is not in IMAGE_FEATURES.
101#
102zap_empty_root_password () {
103 if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then
104 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow
105 fi
106 if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then
107 sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd
108 fi
109}
110
111#
112# allow dropbear/openssh to accept root logins and logins from accounts with an empty password string
113#
114ssh_allow_empty_password () {
115 for config in sshd_config sshd_config_readonly; do
116 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
117 sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
118 sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
119 fi
120 done
121
122 if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
123 if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
124 if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then
125 sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
126 fi
127 else
128 printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
129 fi
130 fi
131
132 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then
133 sed -i 's/nullok_secure/nullok/' ${IMAGE_ROOTFS}${sysconfdir}/pam.d/*
134 fi
135}
136
137ssh_disable_dns_lookup () {
138 if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config ]; then
139 sed -i -e 's:#UseDNS yes:UseDNS no:' ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config
140 fi
141}
142
143#
144# Enable postinst logging if debug-tweaks is enabled
145#
146postinst_enable_logging () {
147 mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
148 echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
149 echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
150}
151
152#
153# Modify systemd default target
154#
155set_systemd_default_target () {
156 if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ]; then
157 ln -sf ${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target
158 fi
159}
160
161# If /var/volatile is not empty, we have seen problems where programs such as the
162# journal make assumptions based on the contents of /var/volatile. The journal
163# would then write to /var/volatile before it was mounted, thus hiding the
164# items previously written.
165#
166# This change is to attempt to fix those types of issues in a way that doesn't
167# affect users that may not be using /var/volatile.
168empty_var_volatile () {
169 if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then
170 match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null`
171 if [ -n "$match" ]; then
172 find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete
173 fi
174 fi
175}
176
177# Turn any symbolic /sbin/init link into a file
178remove_init_link () {
179 if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
180 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
181 rm ${IMAGE_ROOTFS}/sbin/init
182 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
183 fi
184}
185
186make_zimage_symlink_relative () {
187 if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
188 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
189 fi
190}
191
192insert_feed_uris () {
193
194 echo "Building feeds for [${DISTRO}].."
195
196 for line in ${FEED_URIS}
197 do
198 # strip leading and trailing spaces/tabs, then split into name and uri
199 line_clean="`echo "$line"|sed 's/^[ \t]*//;s/[ \t]*$//'`"
200 feed_name="`echo "$line_clean" | sed -n 's/\(.*\)##\(.*\)/\1/p'`"
201 feed_uri="`echo "$line_clean" | sed -n 's/\(.*\)##\(.*\)/\2/p'`"
202
203 echo "Added $feed_name feed with URL $feed_uri"
204
205 # insert new feed-sources
206 echo "src/gz $feed_name $feed_uri" >> ${IMAGE_ROOTFS}/etc/opkg/${feed_name}-feed.conf
207 done
208}
209
210python write_image_manifest () {
211 from oe.rootfs import image_list_installed_packages
212 from oe.utils import format_pkg_list
213
214 deploy_dir = d.getVar('DEPLOY_DIR_IMAGE', True)
215 link_name = d.getVar('IMAGE_LINK_NAME', True)
216 manifest_name = d.getVar('IMAGE_MANIFEST', True)
217
218 if not manifest_name:
219 return
220
221 pkgs = image_list_installed_packages(d)
222 with open(manifest_name, 'w+') as image_manifest:
223 image_manifest.write(format_pkg_list(pkgs, "ver"))
224 image_manifest.write("\n")
225
226 if os.path.exists(manifest_name):
227 manifest_link = deploy_dir + "/" + link_name + ".manifest"
228 if os.path.lexists(manifest_link):
229 if d.getVar('RM_OLD_IMAGE', True) == "1" and \
230 os.path.exists(os.path.realpath(manifest_link)):
231 os.remove(os.path.realpath(manifest_link))
232 os.remove(manifest_link)
233 os.symlink(os.path.basename(manifest_name), manifest_link)
234}
235
236# Can be use to create /etc/timestamp during image construction to give a reasonably
237# sane default time setting
238rootfs_update_timestamp () {
239 date -u +%4Y%2m%2d%2H%2M%2S >${IMAGE_ROOTFS}/etc/timestamp
240}
241
242# Prevent X from being started
243rootfs_no_x_startup () {
244 if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
245 chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm
246 fi
247}
248
249rootfs_trim_schemas () {
250 for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas
251 do
252 # Need this in case no files exist
253 if [ -e $schema ]; then
254 oe-trim-schemas $schema > $schema.new
255 mv $schema.new $schema
256 fi
257 done
258}
259
260rootfs_check_host_user_contaminated () {
261 contaminated="${WORKDIR}/host-user-contaminated.txt"
262 HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)"
263 HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)"
264
265 find "${IMAGE_ROOTFS}" -wholename "${IMAGE_ROOTFS}/home" -prune \
266 -user "$HOST_USER_UID" -o -group "$HOST_USER_GID" >"$contaminated"
267
268 if [ -s "$contaminated" ]; then
269 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."
270 cat "$contaminated" | sed "s,^, ,"
271 fi
272}
273
274# Make any absolute links in a sysroot relative
275rootfs_sysroot_relativelinks () {
276 sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT}
277}