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