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