Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | # Zap the root password if debug-tweaks and empty-root-password features are not enabled |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 8 | ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'empty-root-password' ], "", "zap_empty_root_password ",d)}' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 9 | |
| 10 | # Allow dropbear/openssh to accept logins from accounts with an empty password string if debug-tweaks or allow-empty-password is enabled |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 11 | ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-empty-password' ], "ssh_allow_empty_password ", "",d)}' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 12 | |
| 13 | # Allow dropbear/openssh to accept root logins if debug-tweaks or allow-root-login is enabled |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 14 | ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-root-login' ], "ssh_allow_root_login ", "",d)}' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 15 | |
| 16 | # Autologin the root user on the serial console, if empty-root-password and serial-autologin-root are active |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 17 | ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", [ 'empty-root-password', 'serial-autologin-root' ], "serial_autologin_root ", "",d)}' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 18 | |
| 19 | # Enable postinst logging if debug-tweaks or post-install-logging is enabled |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 20 | ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'post-install-logging' ], "postinst_enable_logging ", "",d)}' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 21 | |
| 22 | # Create /etc/timestamp during image construction to give a reasonably sane default time setting |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 23 | ROOTFS_POSTPROCESS_COMMAND += "rootfs_update_timestamp " |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 24 | |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 25 | # Tweak files in /etc if read-only-rootfs is enabled |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 26 | ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", "read_only_rootfs_hook ", "",d)}' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 27 | |
| 28 | # We also need to do the same for the kernel boot parameters, |
| 29 | # otherwise kernel or initramfs end up mounting the rootfs read/write |
| 30 | # (the default) if supported by the underlying storage. |
| 31 | # |
| 32 | # We do this with :append because the default value might get set later with ?= |
| 33 | # and we don't want to disable such a default that by setting a value here. |
| 34 | APPEND:append = '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", " ro", "", d)}' |
| 35 | |
| 36 | # Generates test data file with data store variables expanded in json format |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 37 | ROOTFS_POSTPROCESS_COMMAND += "write_image_test_data " |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 38 | |
| 39 | # Write manifest |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 40 | IMAGE_MANIFEST = "${IMGDEPLOYDIR}/${IMAGE_NAME}.manifest" |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 41 | ROOTFS_POSTUNINSTALL_COMMAND =+ "write_image_manifest" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 42 | # Set default postinst log file |
| 43 | POSTINST_LOGFILE ?= "${localstatedir}/log/postinstall.log" |
| 44 | # Set default target for systemd images |
| 45 | SYSTEMD_DEFAULT_TARGET ?= '${@bb.utils.contains_any("IMAGE_FEATURES", [ "x11-base", "weston" ], "graphical.target", "multi-user.target", d)}' |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 46 | ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "set_systemd_default_target systemd_sysusers_check", "", d)}' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 47 | |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 48 | ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 49 | |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 50 | ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "overlayfs", "overlayfs_qa_check overlayfs_postprocess", "", d)}' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 51 | |
| 52 | inherit image-artifact-names |
| 53 | |
| 54 | # Sort the user and group entries in /etc by ID in order to make the content |
| 55 | # deterministic. Package installs are not deterministic, causing the ordering |
| 56 | # of entries to change between builds. In case that this isn't desired, |
| 57 | # the command can be overridden. |
Patrick Williams | 56b44a9 | 2024-01-19 08:49:29 -0600 | [diff] [blame] | 58 | SORT_PASSWD_POSTPROCESS_COMMAND ??= "tidy_shadowutils_files" |
| 59 | ROOTFS_POSTPROCESS_COMMAND += '${SORT_PASSWD_POSTPROCESS_COMMAND}' |
| 60 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 61 | # |
| 62 | # Note that useradd-staticids.bbclass has to be used to ensure that |
| 63 | # the numeric IDs of dynamically created entries remain stable. |
| 64 | # |
Patrick Williams | 56b44a9 | 2024-01-19 08:49:29 -0600 | [diff] [blame] | 65 | ROOTFS_POSTPROCESS_COMMAND += 'rootfs_reproducible' |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 66 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 67 | # Resolve the ID as described in the sysusers.d(5) manual: ID can be a numeric |
| 68 | # uid, a couple uid:gid or uid:groupname or it is '-' meaning leaving it |
| 69 | # automatic or it can be a path. In the latter, the uid/gid matches the |
| 70 | # user/group owner of that file. |
| 71 | def resolve_sysusers_id(d, sid): |
| 72 | # If the id is a path, the uid/gid matchs to the target's uid/gid in the |
| 73 | # rootfs. |
| 74 | if '/' in sid: |
| 75 | try: |
| 76 | osstat = os.stat(os.path.join(d.getVar('IMAGE_ROOTFS'), sid)) |
| 77 | except FileNotFoundError: |
| 78 | bb.error('sysusers.d: file %s is required but it does not exist in the rootfs', sid) |
| 79 | return ('-', '-') |
| 80 | return (osstat.st_uid, osstat.st_gid) |
| 81 | # Else it is a uid:gid or uid:groupname syntax |
| 82 | if ':' in sid: |
| 83 | return sid.split(':') |
| 84 | else: |
| 85 | return (sid, '-') |
| 86 | |
| 87 | # Check a user exists in the rootfs password file and return its properties |
| 88 | def check_user_exists(d, uname=None, uid=None): |
| 89 | with open(os.path.join(d.getVar('IMAGE_ROOTFS'), 'etc/passwd'), 'r') as pwfile: |
| 90 | for line in pwfile: |
| 91 | (name, _, u_id, gid, comment, homedir, ushell) = line.strip().split(':') |
| 92 | if uname == name or uid == u_id: |
| 93 | return (name, u_id, gid, comment or '-', homedir or '/', ushell or '-') |
| 94 | return None |
| 95 | |
| 96 | # Check a group exists in the rootfs group file and return its properties |
| 97 | def check_group_exists(d, gname=None, gid=None): |
| 98 | with open(os.path.join(d.getVar('IMAGE_ROOTFS'), 'etc/group'), 'r') as gfile: |
| 99 | for line in gfile: |
| 100 | (name, _, g_id, _) = line.strip().split(':') |
| 101 | if name == gname or g_id == gid: |
| 102 | return (name, g_id) |
| 103 | return None |
| 104 | |
| 105 | def compare_users(user, e_user): |
| 106 | # user and e_user must not have None values. Unset values must be '-'. |
| 107 | (name, uid, gid, comment, homedir, ushell) = user |
| 108 | (e_name, e_uid, e_gid, e_comment, e_homedir, e_ushell) = e_user |
Patrick Williams | f52e3dd | 2024-01-26 13:04:43 -0600 | [diff] [blame] | 109 | # Ignore 'uid', 'gid' or 'homedir' if they are not set |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 110 | # Ignore 'shell' and 'ushell' if one is not set |
| 111 | return name == e_name \ |
| 112 | and (uid == '-' or uid == e_uid) \ |
| 113 | and (gid == '-' or gid == e_gid) \ |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 114 | and (homedir == '-' or e_homedir == '-' or homedir == e_homedir) \ |
| 115 | and (ushell == '-' or e_ushell == '-' or ushell == e_ushell) |
| 116 | |
| 117 | # Open sysusers.d configuration files and parse each line to check the users and |
| 118 | # groups are already defined in /etc/passwd and /etc/groups with similar |
| 119 | # properties. Refer to the sysusers.d(5) manual for its syntax. |
| 120 | python systemd_sysusers_check() { |
| 121 | import glob |
| 122 | import re |
| 123 | |
| 124 | pattern_comment = r'(-|\"[^:\"]+\")' |
| 125 | pattern_word = r'[^\s]+' |
| 126 | pattern_line = r'(' + pattern_word + r')\s+(' + pattern_word + r')\s+(' + pattern_word + r')(\s+' \ |
| 127 | + pattern_comment + r')?' + r'(\s+(' + pattern_word + r'))?' + r'(\s+(' + pattern_word + r'))?' |
| 128 | |
| 129 | for conffile in glob.glob(os.path.join(d.getVar('IMAGE_ROOTFS'), 'usr/lib/sysusers.d/*.conf')): |
| 130 | with open(conffile, 'r') as f: |
| 131 | for line in f: |
| 132 | line = line.strip() |
| 133 | if not len(line) or line[0] == '#': continue |
| 134 | ret = re.fullmatch(pattern_line, line.strip()) |
| 135 | if not ret: continue |
| 136 | (stype, sname, sid, _, scomment, _, shomedir, _, sshell) = ret.groups() |
| 137 | if stype == 'u': |
| 138 | if sid: |
| 139 | (suid, sgid) = resolve_sysusers_id(d, sid) |
| 140 | if sgid.isalpha(): |
| 141 | sgid = check_group_exists(d, gname=sgid) |
| 142 | elif sgid.isdigit(): |
| 143 | check_group_exists(d, gid=sgid) |
| 144 | else: |
| 145 | sgid = '-' |
| 146 | else: |
| 147 | suid = '-' |
| 148 | sgid = '-' |
| 149 | scomment = scomment.replace('"', '') if scomment else '-' |
| 150 | shomedir = shomedir or '-' |
| 151 | sshell = sshell or '-' |
| 152 | e_user = check_user_exists(d, uname=sname) |
| 153 | if not e_user: |
| 154 | bb.warn('User %s has never been defined' % sname) |
| 155 | elif not compare_users((sname, suid, sgid, scomment, shomedir, sshell), e_user): |
| 156 | bb.warn('User %s has been defined as (%s) but sysusers.d expects it as (%s)' |
| 157 | % (sname, ', '.join(e_user), |
| 158 | ', '.join((sname, suid, sgid, scomment, shomedir, sshell)))) |
| 159 | elif stype == 'g': |
| 160 | gid = sid or '-' |
| 161 | if '/' in gid: |
| 162 | (_, gid) = resolve_sysusers_id(d, sid) |
| 163 | e_group = check_group_exists(d, gname=sname) |
| 164 | if not e_group: |
| 165 | bb.warn('Group %s has never been defined' % sname) |
| 166 | elif gid != '-': |
| 167 | (_, e_gid) = e_group |
| 168 | if gid != e_gid: |
| 169 | bb.warn('Group %s has been defined with id (%s) but sysusers.d expects gid (%s)' |
| 170 | % (sname, e_gid, gid)) |
| 171 | elif stype == 'm': |
| 172 | check_user_exists(d, sname) |
| 173 | check_group_exists(d, sid) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | # |
| 177 | # A hook function to support read-only-rootfs IMAGE_FEATURES |
| 178 | # |
| 179 | read_only_rootfs_hook () { |
| 180 | # Tweak the mount option and fs_passno for rootfs in fstab |
| 181 | if [ -f ${IMAGE_ROOTFS}/etc/fstab ]; then |
| 182 | sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab |
| 183 | fi |
| 184 | |
| 185 | # Tweak the "mount -o remount,rw /" command in busybox-inittab inittab |
| 186 | if [ -f ${IMAGE_ROOTFS}/etc/inittab ]; then |
| 187 | sed -i 's|/bin/mount -o remount,rw /|/bin/mount -o remount,ro /|' ${IMAGE_ROOTFS}/etc/inittab |
| 188 | fi |
| 189 | |
| 190 | # If we're using openssh and the /etc/ssh directory has no pre-generated keys, |
| 191 | # we should configure openssh to use the configuration file /etc/ssh/sshd_config_readonly |
| 192 | # and the keys under /var/run/ssh. |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 193 | # If overlayfs-etc is used this is not done as /etc is treated as writable |
| 194 | # If stateless-rootfs is enabled this is always done as we don't want to save keys then |
| 195 | if ${@ 'true' if not bb.utils.contains('IMAGE_FEATURES', 'overlayfs-etc', True, False, d) or bb.utils.contains('IMAGE_FEATURES', 'stateless-rootfs', True, False, d) else 'false'}; then |
| 196 | if [ -d ${IMAGE_ROOTFS}/etc/ssh ]; then |
| 197 | if [ -e ${IMAGE_ROOTFS}/etc/ssh/ssh_host_rsa_key ]; then |
| 198 | echo "SYSCONFDIR=\${SYSCONFDIR:-/etc/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh |
| 199 | echo "SSHD_OPTS=" >> ${IMAGE_ROOTFS}/etc/default/ssh |
| 200 | else |
| 201 | echo "SYSCONFDIR=\${SYSCONFDIR:-/var/run/ssh}" >> ${IMAGE_ROOTFS}/etc/default/ssh |
| 202 | echo "SSHD_OPTS='-f /etc/ssh/sshd_config_readonly'" >> ${IMAGE_ROOTFS}/etc/default/ssh |
| 203 | fi |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 204 | fi |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 205 | |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 206 | # Also tweak the key location for dropbear in the same way. |
| 207 | if [ -d ${IMAGE_ROOTFS}/etc/dropbear ]; then |
| 208 | if [ ! -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then |
| 209 | echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear |
| 210 | fi |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 211 | fi |
| 212 | fi |
| 213 | |
| 214 | if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then |
| 215 | # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes |
| 216 | if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then |
| 217 | sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS |
| 218 | fi |
| 219 | # Run populate-volatile.sh at rootfs time to set up basic files |
| 220 | # and directories to support read-only rootfs. |
| 221 | if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then |
| 222 | ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh |
| 223 | fi |
| 224 | fi |
| 225 | |
| 226 | if ${@bb.utils.contains("DISTRO_FEATURES", "systemd", "true", "false", d)}; then |
| 227 | # Create machine-id |
| 228 | # 20:12 < mezcalero> koen: you have three options: a) run systemd-machine-id-setup at install time, b) have / read-only and an empty file there (for stateless) and c) boot with / writable |
| 229 | touch ${IMAGE_ROOTFS}${sysconfdir}/machine-id |
| 230 | fi |
| 231 | } |
| 232 | |
| 233 | # |
| 234 | # This function disallows empty root passwords |
| 235 | # |
| 236 | zap_empty_root_password () { |
| 237 | if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 238 | sed --follow-symlinks -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 239 | fi |
| 240 | if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 241 | sed --follow-symlinks -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 242 | fi |
| 243 | } |
| 244 | |
| 245 | # |
| 246 | # allow dropbear/openssh to accept logins from accounts with an empty password string |
| 247 | # |
| 248 | ssh_allow_empty_password () { |
| 249 | for config in sshd_config sshd_config_readonly; do |
| 250 | if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then |
| 251 | sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config |
| 252 | fi |
| 253 | done |
| 254 | |
| 255 | if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then |
| 256 | if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then |
| 257 | if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then |
| 258 | sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear |
| 259 | fi |
| 260 | else |
| 261 | printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear |
| 262 | fi |
| 263 | fi |
| 264 | |
| 265 | if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then |
| 266 | for f in `find ${IMAGE_ROOTFS}${sysconfdir}/pam.d/* -type f -exec test -e {} \; -print` |
| 267 | do |
| 268 | sed -i 's/nullok_secure/nullok/' $f |
| 269 | done |
| 270 | fi |
| 271 | } |
| 272 | |
| 273 | # |
| 274 | # allow dropbear/openssh to accept root logins |
| 275 | # |
| 276 | ssh_allow_root_login () { |
| 277 | for config in sshd_config sshd_config_readonly; do |
| 278 | if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then |
| 279 | sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config |
| 280 | fi |
| 281 | done |
| 282 | |
| 283 | if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then |
| 284 | if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then |
| 285 | sed -i '/^DROPBEAR_EXTRA_ARGS=/ s/-w//' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 286 | sed -i '/^# Disallow root/d' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 287 | fi |
| 288 | fi |
| 289 | } |
| 290 | |
| 291 | # |
| 292 | # Autologin the 'root' user on the serial terminal, |
| 293 | # if empty-root-password' AND 'serial-autologin-root are enabled |
| 294 | # |
| 295 | serial_autologin_root () { |
| 296 | if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then |
| 297 | # add autologin option to util-linux getty only |
| 298 | sed -i 's/options="/&--autologin root /' \ |
| 299 | "${IMAGE_ROOTFS}${base_bindir}/start_getty" |
| 300 | elif ${@bb.utils.contains("DISTRO_FEATURES", "systemd", "true", "false", d)}; then |
| 301 | if [ -e ${IMAGE_ROOTFS}${systemd_system_unitdir}/serial-getty@.service ]; then |
| 302 | sed -i '/^\s*ExecStart\b/ s/getty /&--autologin root /' \ |
| 303 | "${IMAGE_ROOTFS}${systemd_system_unitdir}/serial-getty@.service" |
| 304 | fi |
| 305 | fi |
| 306 | } |
| 307 | |
| 308 | python tidy_shadowutils_files () { |
| 309 | import rootfspostcommands |
| 310 | rootfspostcommands.tidy_shadowutils_files(d.expand('${IMAGE_ROOTFS}${sysconfdir}')) |
| 311 | } |
| 312 | |
| 313 | python sort_passwd () { |
| 314 | """ |
| 315 | Deprecated in the favour of tidy_shadowutils_files. |
| 316 | """ |
| 317 | import rootfspostcommands |
| 318 | bb.warn('[sort_passwd] You are using a deprecated function for ' |
| 319 | 'SORT_PASSWD_POSTPROCESS_COMMAND. The default one is now called ' |
| 320 | '"tidy_shadowutils_files".') |
| 321 | rootfspostcommands.tidy_shadowutils_files(d.expand('${IMAGE_ROOTFS}${sysconfdir}')) |
| 322 | } |
| 323 | |
| 324 | # |
| 325 | # Enable postinst logging |
| 326 | # |
| 327 | postinst_enable_logging () { |
| 328 | mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default |
| 329 | echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst |
| 330 | echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst |
| 331 | } |
| 332 | |
| 333 | # |
| 334 | # Modify systemd default target |
| 335 | # |
| 336 | set_systemd_default_target () { |
| 337 | if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_system_unitdir}/${SYSTEMD_DEFAULT_TARGET} ]; then |
| 338 | ln -sf ${systemd_system_unitdir}/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target |
| 339 | fi |
| 340 | } |
| 341 | |
| 342 | # If /var/volatile is not empty, we have seen problems where programs such as the |
| 343 | # journal make assumptions based on the contents of /var/volatile. The journal |
| 344 | # would then write to /var/volatile before it was mounted, thus hiding the |
| 345 | # items previously written. |
| 346 | # |
| 347 | # This change is to attempt to fix those types of issues in a way that doesn't |
| 348 | # affect users that may not be using /var/volatile. |
| 349 | empty_var_volatile () { |
| 350 | if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then |
| 351 | match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null` |
| 352 | if [ -n "$match" ]; then |
| 353 | find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete |
| 354 | fi |
| 355 | fi |
| 356 | } |
| 357 | |
| 358 | # Turn any symbolic /sbin/init link into a file |
| 359 | remove_init_link () { |
| 360 | if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then |
| 361 | LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init` |
| 362 | rm ${IMAGE_ROOTFS}/sbin/init |
| 363 | cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init |
| 364 | fi |
| 365 | } |
| 366 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 367 | python write_image_manifest () { |
| 368 | from oe.rootfs import image_list_installed_packages |
| 369 | from oe.utils import format_pkg_list |
| 370 | |
| 371 | deploy_dir = d.getVar('IMGDEPLOYDIR') |
| 372 | link_name = d.getVar('IMAGE_LINK_NAME') |
| 373 | manifest_name = d.getVar('IMAGE_MANIFEST') |
| 374 | |
| 375 | if not manifest_name: |
| 376 | return |
| 377 | |
| 378 | pkgs = image_list_installed_packages(d) |
| 379 | with open(manifest_name, 'w+') as image_manifest: |
| 380 | image_manifest.write(format_pkg_list(pkgs, "ver")) |
| 381 | |
| 382 | if os.path.exists(manifest_name) and link_name: |
| 383 | manifest_link = deploy_dir + "/" + link_name + ".manifest" |
| 384 | if manifest_link != manifest_name: |
| 385 | if os.path.lexists(manifest_link): |
| 386 | os.remove(manifest_link) |
| 387 | os.symlink(os.path.basename(manifest_name), manifest_link) |
| 388 | } |
| 389 | |
| 390 | # Can be used to create /etc/timestamp during image construction to give a reasonably |
| 391 | # sane default time setting |
| 392 | rootfs_update_timestamp () { |
| 393 | if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then |
| 394 | # Convert UTC into %4Y%2m%2d%2H%2M%2S |
| 395 | sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S` |
| 396 | else |
| 397 | sformatted=`date -u +%4Y%2m%2d%2H%2M%2S` |
| 398 | fi |
| 399 | echo $sformatted > ${IMAGE_ROOTFS}/etc/timestamp |
| 400 | bbnote "rootfs_update_timestamp: set /etc/timestamp to $sformatted" |
| 401 | } |
| 402 | |
| 403 | # Prevent X from being started |
| 404 | rootfs_no_x_startup () { |
| 405 | if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then |
| 406 | chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm |
| 407 | fi |
| 408 | } |
| 409 | |
| 410 | rootfs_trim_schemas () { |
| 411 | for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas |
| 412 | do |
| 413 | # Need this in case no files exist |
| 414 | if [ -e $schema ]; then |
| 415 | oe-trim-schemas $schema > $schema.new |
| 416 | mv $schema.new $schema |
| 417 | fi |
| 418 | done |
| 419 | } |
| 420 | |
| 421 | rootfs_check_host_user_contaminated () { |
| 422 | contaminated="${S}/host-user-contaminated.txt" |
| 423 | HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)" |
| 424 | HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)" |
| 425 | |
| 426 | find "${IMAGE_ROOTFS}" -path "${IMAGE_ROOTFS}/home" -prune -o \ |
| 427 | -user "$HOST_USER_UID" -print -o -group "$HOST_USER_GID" -print >"$contaminated" |
| 428 | |
| 429 | sed -e "s,${IMAGE_ROOTFS},," $contaminated | while read line; do |
| 430 | bbwarn "Path in the rootfs is owned by the same user or group as the user running bitbake:" $line `ls -lan ${IMAGE_ROOTFS}/$line` |
| 431 | done |
| 432 | |
| 433 | if [ -s "$contaminated" ]; then |
| 434 | bbwarn "/etc/passwd:" `cat ${IMAGE_ROOTFS}/etc/passwd` |
| 435 | bbwarn "/etc/group:" `cat ${IMAGE_ROOTFS}/etc/group` |
| 436 | fi |
| 437 | } |
| 438 | |
| 439 | # Make any absolute links in a sysroot relative |
| 440 | rootfs_sysroot_relativelinks () { |
| 441 | sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT} |
| 442 | } |
| 443 | |
| 444 | # Generated test data json file |
| 445 | python write_image_test_data() { |
| 446 | from oe.data import export2json |
| 447 | |
| 448 | deploy_dir = d.getVar('IMGDEPLOYDIR') |
| 449 | link_name = d.getVar('IMAGE_LINK_NAME') |
| 450 | testdata_name = os.path.join(deploy_dir, "%s.testdata.json" % d.getVar('IMAGE_NAME')) |
| 451 | |
| 452 | searchString = "%s/"%(d.getVar("TOPDIR")).replace("//","/") |
| 453 | export2json(d, testdata_name, searchString=searchString, replaceString="") |
| 454 | |
| 455 | if os.path.exists(testdata_name) and link_name: |
| 456 | testdata_link = os.path.join(deploy_dir, "%s.testdata.json" % link_name) |
| 457 | if testdata_link != testdata_name: |
| 458 | if os.path.lexists(testdata_link): |
| 459 | os.remove(testdata_link) |
| 460 | os.symlink(os.path.basename(testdata_name), testdata_link) |
| 461 | } |
| 462 | write_image_test_data[vardepsexclude] += "TOPDIR" |
| 463 | |
| 464 | # Check for unsatisfied recommendations (RRECOMMENDS) |
| 465 | python rootfs_log_check_recommends() { |
| 466 | log_path = d.expand("${T}/log.do_rootfs") |
| 467 | with open(log_path, 'r') as log: |
| 468 | for line in log: |
| 469 | if 'log_check' in line: |
| 470 | continue |
| 471 | |
| 472 | if 'unsatisfied recommendation for' in line: |
| 473 | bb.warn('[log_check] %s: %s' % (d.getVar('PN'), line)) |
| 474 | } |
| 475 | |
| 476 | # Perform any additional adjustments needed to make rootf binary reproducible |
| 477 | rootfs_reproducible () { |
| 478 | if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then |
| 479 | # Convert UTC into %4Y%2m%2d%2H%2M%2S |
| 480 | sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} +%4Y%2m%2d%2H%2M%2S` |
| 481 | echo $sformatted > ${IMAGE_ROOTFS}/etc/version |
| 482 | bbnote "rootfs_reproducible: set /etc/version to $sformatted" |
| 483 | |
| 484 | if [ -d ${IMAGE_ROOTFS}${sysconfdir}/gconf ]; then |
| 485 | find ${IMAGE_ROOTFS}${sysconfdir}/gconf -name '%gconf.xml' -print0 | xargs -0r \ |
| 486 | sed -i -e 's@\bmtime="[0-9][0-9]*"@mtime="'${REPRODUCIBLE_TIMESTAMP_ROOTFS}'"@g' |
| 487 | fi |
| 488 | fi |
| 489 | } |
| 490 | |
| 491 | # Perform a dumb check for unit existence, not its validity |
| 492 | python overlayfs_qa_check() { |
| 493 | from oe.overlayfs import mountUnitName |
| 494 | |
| 495 | overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT") or {} |
| 496 | imagepath = d.getVar("IMAGE_ROOTFS") |
| 497 | sysconfdir = d.getVar("sysconfdir") |
| 498 | searchpaths = [oe.path.join(imagepath, sysconfdir, "systemd", "system"), |
| 499 | oe.path.join(imagepath, d.getVar("systemd_system_unitdir"))] |
| 500 | fstabpath = oe.path.join(imagepath, sysconfdir, "fstab") |
| 501 | |
| 502 | if not any(os.path.exists(path) for path in [*searchpaths, fstabpath]): |
| 503 | return |
| 504 | |
| 505 | fstabDevices = [] |
| 506 | if os.path.isfile(fstabpath): |
| 507 | with open(fstabpath, 'r') as f: |
| 508 | for line in f: |
| 509 | if line[0] == '#': |
| 510 | continue |
| 511 | path = line.split(maxsplit=2) |
| 512 | if len(path) > 2: |
| 513 | fstabDevices.append(path[1]) |
| 514 | |
| 515 | allUnitExist = True; |
| 516 | for mountPoint in overlayMountPoints: |
| 517 | qaSkip = (d.getVarFlag("OVERLAYFS_QA_SKIP", mountPoint) or "").split() |
| 518 | if "mount-configured" in qaSkip: |
| 519 | continue |
| 520 | |
| 521 | mountPath = d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint) |
| 522 | if mountPath in fstabDevices: |
| 523 | continue |
| 524 | |
| 525 | mountUnit = mountUnitName(mountPath) |
| 526 | if any(os.path.isfile(oe.path.join(dirpath, mountUnit)) |
| 527 | for dirpath in searchpaths): |
| 528 | continue |
| 529 | |
| 530 | bb.warn(f'Mount path {mountPath} not found in fstab and unit ' |
| 531 | f'{mountUnit} not found in systemd unit directories.') |
| 532 | bb.warn(f'Skip this check by setting OVERLAYFS_QA_SKIP[{mountPoint}] = ' |
| 533 | '"mount-configured"') |
| 534 | allUnitExist = False; |
| 535 | |
| 536 | if not allUnitExist: |
| 537 | bb.fatal('Not all mount paths and units are installed in the image') |
| 538 | } |
| 539 | |
| 540 | python overlayfs_postprocess() { |
| 541 | import shutil |
| 542 | |
| 543 | # install helper script |
| 544 | helperScriptName = "overlayfs-create-dirs.sh" |
| 545 | helperScriptSource = oe.path.join(d.getVar("COREBASE"), "meta/files", helperScriptName) |
| 546 | helperScriptDest = oe.path.join(d.getVar("IMAGE_ROOTFS"), "/usr/sbin/", helperScriptName) |
| 547 | shutil.copyfile(helperScriptSource, helperScriptDest) |
| 548 | os.chmod(helperScriptDest, 0o755) |
| 549 | } |