Create systemd users at build time
cover Create systemd users at build time
Systemd has a config option sysusers which creates any users or groups that systemd needs on first boot. There is a rootfs-postcommand that creates these users in case the filesystem is readonly, however it does not take into account any staicids that the user may have set.
By adding these users at build time, we allow a check for staticids, which would otherwise have been skipped.
This issue was initially raised by a fix that was added to Ostro OS. See Patrick's comments for more info on that.
[ YOCTO #9497 ]
Stephano Cetola (2):
systemd: add sysusers config option to defaults
systemd: add users/groups at build rather than runtime
meta/classes/rootfs-postcommands.bbclass | 66 +++++++++++++++++++++----------- meta/classes/useradd-staticids.bbclass | 56 +-------------------------- meta/recipes-core/systemd/systemd_229.bb | 4 +- useradd-staticids-list.bbclass | 56 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 79 deletions(-) create mode 100644 useradd-staticids-list.bbclass
Signature · 1 line
2.8.0
PATCH 1/2 systemd: add sysusers config option to defaults
Without the sysusers config option, the build will not know which users need to be added for systemd. This is especially necessary if the filesystem is readonly, as these users cannot be added at runtime.
--- meta/recipes-core/systemd/systemd_229.bb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
meta/recipes-core/systemd/systemd_229.bb +3 -1
@@ -23,6 +23,7 @@ DEPENDS = "kmod docbook-sgml-dtd-4.1-native intltool-native gperf-native acl rea SECTION = "base/shell" inherit useradd pkgconfig autotools perlnative update-rc.d update-alternatives qemu systemd ptest gettext bash-completion +USERADDEXTENSION += " useradd-staticids" SRCREV = "714c62b46379abb7558c544665522aca91691e10"
@@ -80,7 +81,8 @@ PACKAGECONFIG ??= "xz \ quotacheck \ bootchart \ hostnamed \ - ${@bb.utils.contains('TCLIBC', 'glibc', 'myhostname sysusers', '', d)} \ + sysusers \ + ${@bb.utils.contains('TCLIBC', 'glibc', 'myhostname', '', d)} \ hibernate \ timedated \ timesyncd \
Signature · 1 line
2.8.0
PATCH 2/2 systemd: add users/groups at build rather than runtime
In reply to Stephano CetolaThis fix ensures that systemd users are created at build and that any staticids for the users or groups are preserved. This functionality only existed for readonly filesystems, however it seems wise to be sure these users are added rather than relying on systemd to create them.
[ YOCTO #9497 ]
--- meta/classes/rootfs-postcommands.bbclass | 66 +++++++++++++++++++++----------- meta/classes/useradd-staticids.bbclass | 56 +-------------------------- useradd-staticids-list.bbclass | 56 +++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 78 deletions(-) create mode 100644 useradd-staticids-list.bbclass
meta/classes/rootfs-postcommands.bbclass +43 -23
@@ -1,4 +1,4 @@ - +inherit useradd-staticids-list # Zap the root password if debug-tweaks feature is not enabled ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'empty-root-password' ], "", "zap_empty_root_password ; ",d)}'
@@ -23,6 +23,9 @@ POSTINST_LOGFILE ?= "${localstatedir}/log/postinstall.log" SYSTEMD_DEFAULT_TARGET ?= '${@bb.utils.contains("IMAGE_FEATURES", "x11-base", "graphical.target", "multi-user.target", d)}' ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "set_systemd_default_target; ", "", d)}' +# Create systemd users at build rather than at runtime +ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "create_systemd_sysusers; ", "", d)}' + ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile;' # Disable DNS lookups, the SSH_DISABLE_DNS_LOOKUP can be overridden to allow
@@ -30,7 +33,45 @@ ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile;' SSH_DISABLE_DNS_LOOKUP ?= " ssh_disable_dns_lookup ; " ROOTFS_POSTPROCESS_COMMAND_append_qemuall = "${SSH_DISABLE_DNS_LOOKUP}" - +# Add systemd users checking for any staticids +python create_systemd_sysusers () { + import shlex + import subprocess + s_users = merge_files(get_passwd_list(d), 7) + s_groups = merge_files(get_group_list(d), 4) + confs = ['%s/usr/lib/sysusers.d/systemd.conf' % d.getVar('IMAGE_ROOTFS', d), '%s/usr/lib/sysusers.d/systemd-remote.conf' % d.getVar('IMAGE_ROOTFS', d)] + for conf in confs: + if os.path.isfile(conf): + with open(conf, 'r') as f: + for line in f: + if line.startswith('#'): + continue + fields = shlex.split(line.rstrip()) + if not fields: + continue + if fields[0] == 'u': + useradd_params = '' + if fields[1] in s_users: + useradd_params += '--uid %s' % s_users[fields[1]][2] + bb.warn("Changing user %s's uid to (%s), verify configuration files!" % (fields[1], s_users[fields[1]][2])) + elif fields[2] != '-': + useradd_params += '--uid %s' % fields[2] + if fields[3] != '-': + useradd_params += ' --comment "%s"' % fields[3] + useradd_params += ' --root "%s" --shell /sbin/nologin --system %s' % (d.getVar("IMAGE_ROOTFS", d), fields[1]) + code = subprocess.call('useradd %s' % useradd_params, shell=True) + elif fields[0] == 'g': + groupadd_params = "" + if fields[1] in s_groups: + groupadd_params += '--gid %s' % s_groups[fields[1]][2] + bb.warn("Changing group %s's gid to (%s), verify configuration files!" % (fields[1], s_groups[fields[1]][2])) + elif fields[2] != '-': + groupadd_params += '--gid %s' % fields[2] + if fields[3] != '-': + groupadd_params += ' --comment "%s"' % (d.getVar("IMAGE_ROOTFS", d), fields[3]) + groupadd_params += ' --root "%s" --system %s' % (d.getVar("IMAGE_ROOTFS", d), fields[1]) + code = subprocess.call('groupadd %s' % groupadd_params, shell=True) +} # # A hook function to support read-only-rootfs IMAGE_FEATURES
@@ -73,27 +114,6 @@ read_only_rootfs_hook () { ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh fi fi - - if ${@bb.utils.contains("DISTRO_FEATURES", "systemd", "true", "false", d)}; then - # Update user database files so that services don't fail for a read-only systemd system - for conffile in ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd.conf ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd-remote.conf; do - [ -e $conffile ] || continue - grep -v "^#" $conffile | sed -e '/^$/d' | while read type name id comment; do - if [ "$type" = "u" ]; then - useradd_params="" - [ "$id" != "-" ] && useradd_params="$useradd_params --uid $id" - [ "$comment" != "-" ] && useradd_params="$useradd_params --comment $comment" - useradd_params="$useradd_params --system $name" - eval useradd --root ${IMAGE_ROOTFS} $useradd_params || true - elif [ "$type" = "g" ]; then - groupadd_params="" - [ "$id" != "-" ] && groupadd_params="$groupadd_params --gid $id" - groupadd_params="$groupadd_params --system $name" - eval groupadd --root ${IMAGE_ROOTFS} $groupadd_params || true - fi - done - done - fi } #
meta/classes/useradd-staticids.bbclass +1 -55
77 lines not on this page · raw message
useradd-staticids-list.bbclass new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
2.8.0