useradd.bbclass: Some clean-ups and simplifications
PATCH v2 1/6 useradd.bbclass: Some clean-ups and simplifications
* Improve some odd function names, e.g., groupmemsadd_sysroot and user_group_groupmems_add_sysroot. These were introduced in commit 322ef726132a47d977d2c6ee41de5358f1e85994 (useradd.bbclass: Fix order of postinst-useradd-*).
* Make common_useradd_sysroot() look for the actually used command instead of always looking for useradd.
* Simplification of common_useradd_sysroot() by using case/esac.
* A bit of whitespace clean-up.
---
PATCHv2: No changes.
meta/classes/useradd.bbclass | 60 +++++++++++++++++------------------- 1 file changed, 28 insertions(+), 32 deletions(-)
meta/classes/useradd.bbclass +28 -32
@@ -19,7 +19,7 @@ PACKAGE_WRITE_DEPS += "shadow-native" # c) As the preinst script in the target package at do_rootfs time # d) As the preinst script in the target package on device as a package upgrade # -useradd_preinst () { +useradd_preinst() { OPT="" SYSROOT=""
@@ -34,7 +34,7 @@ if test "x$D" != "x"; then # shadow package, then while performing preinsts for packages that depend on # shadow, there might only be /etc/login.def.dpkg-new there in root filesystem. if [ ! -e $D${sysconfdir}/login.defs -a -e $D${sysconfdir}/login.defs.dpkg-new ]; then - cp $D${sysconfdir}/login.defs.dpkg-new $D${sysconfdir}/login.defs + cp $D${sysconfdir}/login.defs.dpkg-new $D${sysconfdir}/login.defs fi # user/group lookups should match useradd/groupadd --prefix
@@ -102,19 +102,19 @@ if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then fi } -useradd_sysroot () { - user_group_groupmems_add_sysroot user +groupadd_sysroot() { + common_useradd_sysroot groupadd } -groupadd_sysroot () { - user_group_groupmems_add_sysroot group +useradd_sysroot() { + common_useradd_sysroot useradd } -groupmemsadd_sysroot () { - user_group_groupmems_add_sysroot groupmems +groupmems_sysroot() { + common_useradd_sysroot groupmems } -user_group_groupmems_add_sysroot () { +common_useradd_sysroot() { # Pseudo may (do_prepare_recipe_sysroot) or may not (do_populate_sysroot_setscene) be running # at this point so we're explicit about the environment so pseudo can load if # not already present.
@@ -130,28 +130,24 @@ user_group_groupmems_add_sysroot () { # Beware that in some cases we might see the fake pseudo passwd here, in which case we also must # exit. if [ ! -f $D${sysconfdir}/passwd ] || - grep -q this-is-the-pseudo-passwd $D${sysconfdir}/passwd; then + grep -q this-is-the-pseudo-passwd $D${sysconfdir}/passwd; then exit 0 fi # It is also possible we may be in a recipe which doesn't have useradd dependencies and hence the # useradd/groupadd tools are unavailable. If there is no dependency, we assume we don't want to # create users in the sysroot - if ! command -v useradd; then - bbwarn "command useradd not found!" + if ! command -v "$1"; then + bbwarn "The $1 command could not be found!" exit 0 fi # Add groups and users defined for all recipe packages - if test "$1" = "group"; then - GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}" - elif test "$1" = "user"; then - USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}" - elif test "$1" = "groupmems"; then - GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}" - elif test "x$1" = "x"; then - bbwarn "missing type of passwd db action" - fi + case "$1" in + groupadd) GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}";; + useradd) USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}";; + groupmems) GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}";; + esac # Tell the system to use the environment vars UA_SYSROOT=1
@@ -159,36 +155,36 @@ user_group_groupmems_add_sysroot () { useradd_preinst } -# The export of PSEUDO in useradd_sysroot() above contains references to +# The export of PSEUDO in common_useradd_sysroot() above contains references to # ${PSEUDO_SYSROOT} and ${PSEUDO_LOCALSTATEDIR}. Additionally, the logging # shell functions use ${LOGFIFO}. These need to be handled when restoring # postinst-useradd-${PN} from the sstate cache. EXTRA_STAGING_FIXMES += "PSEUDO_SYSROOT PSEUDO_LOCALSTATEDIR LOGFIFO" -python useradd_sysroot_sstate () { - for type, sort_prefix in [("group", "01"), ("user", "02"), ("groupmems", "03")]: +python useradd_sysroot_sstate() { + for cmd, sort_prefix in [("groupadd", "01"), ("useradd", "02"), ("groupmems", "03")]: scriptfile = None task = d.getVar("BB_CURRENTTASK") if task == "package_setscene": - bb.build.exec_func(type + "add_sysroot", d) + bb.build.exec_func(f"{cmd}_sysroot", d) elif task == "prepare_recipe_sysroot": # Used to update this recipe's own sysroot so the user/groups are available to do_install # If do_populate_sysroot is triggered and we write the file here, there would be an overlapping - # files. See usergrouptests.UserGroupTests.test_add_task_between_p_sysroot_and_package - scriptfile = d.expand("${RECIPE_SYSROOT}${bindir}/postinst-useradd-" + sort_prefix + type + "-${PN}-recipedebug") + # file. See usergrouptests.UserGroupTests.test_add_task_between_p_sysroot_and_package + scriptfile = d.expand("${RECIPE_SYSROOT}${bindir}/postinst-useradd-" + sort_prefix + cmd + "-${PN}-recipedebug") - bb.build.exec_func(type + "add_sysroot", d) + bb.build.exec_func(f"{cmd}_sysroot", d) elif task == "populate_sysroot": # Used when installed in dependent task sysroots - scriptfile = d.expand("${SYSROOT_DESTDIR}${bindir}/postinst-useradd-" + sort_prefix + type + "-${PN}") + scriptfile = d.expand("${SYSROOT_DESTDIR}${bindir}/postinst-useradd-" + sort_prefix + cmd + "-${PN}") if scriptfile: bb.utils.mkdirhier(os.path.dirname(scriptfile)) with open(scriptfile, 'w') as script: script.write("#!/bin/sh -e\n") - bb.data.emit_func(type + "add_sysroot", script, d) - script.write(type + "add_sysroot\n") + bb.data.emit_func(f"{cmd}_sysroot", script, d) + script.write(f"{cmd}_sysroot\n") os.chmod(scriptfile, 0o755) }
@@ -247,7 +243,7 @@ def get_all_cmd_params(d, cmd_type): return "; ".join(params) # Adds the preinst script into generated packages -fakeroot python populate_packages:prepend () { +fakeroot python populate_packages:prepend() { def update_useradd_package(pkg): bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
PATCH v2 2/6 useradd.bbclass: Add support for USERMOD_PARAM
The groupmems command will be removed in shadow 4.20. The same functionality as provided by groupmems can be achieved with the usermod command. Add support for USERMOD_PARAM variables to specify the options to use.
The following GROUPMEMS_PARAM:
GROUPMEMS_PARAM:${PN} = "--add user --group group1; \
--add user --group group2"would be directly converted to the following USERMOD_PARAM:
USERMOD_PARAM:${PN} = "--append --groups group1 user; \
--append --groups group2 user"It can also be simplified to:
USERMOD_PARAM:${PN} = "--append --groups group1,group2 user"
[YOCTO#16277]
---
PATCHv2: No changes.
meta/classes/useradd.bbclass | 39 ++++++++++++++++++++++++++++-------- meta/conf/documentation.conf | 1 + 2 files changed, 32 insertions(+), 8 deletions(-)
meta/classes/useradd.bbclass +31 -8
@@ -48,6 +48,7 @@ if test "x$UA_SYSROOT" = "x"; then # Add groups and users defined only for this package GROUPADD_PARAM="${GROUPADD_PARAM}" USERADD_PARAM="${USERADD_PARAM}" + USERMOD_PARAM="${USERMOD_PARAM}" GROUPMEMS_PARAM="${GROUPMEMS_PARAM}" fi
@@ -85,6 +86,22 @@ if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then done fi +if test "x`echo $USERMOD_PARAM | tr -d '[:space:]'`" != "x"; then + echo "Running usermod commands..." + # Invoke multiple instances of usermod for parameter lists + # separated by ';' + opts=`echo "$USERMOD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'` + remaining=`echo "$USERMOD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'` + while test "x$opts" != "x"; do + perform_usermod "$SYSROOT" "$OPT $opts" + if test "x$opts" = "x$remaining"; then + break + fi + opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'` + remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'` + done +fi + if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then echo "Running groupmems commands..." # Invoke multiple instances of groupmems for parameter lists
@@ -103,13 +120,17 @@ fi } groupadd_sysroot() { - common_useradd_sysroot groupadd + common_useradd_sysroot groupadd } useradd_sysroot() { common_useradd_sysroot useradd } +usermod_sysroot() { + common_useradd_sysroot usermod +} + groupmems_sysroot() { common_useradd_sysroot groupmems }
@@ -146,6 +167,7 @@ common_useradd_sysroot() { case "$1" in groupadd) GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}";; useradd) USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}";; + usermod) USERMOD_PARAM="${@get_all_cmd_params(d, 'usermod')}";; groupmems) GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}";; esac
@@ -162,7 +184,7 @@ common_useradd_sysroot() { EXTRA_STAGING_FIXMES += "PSEUDO_SYSROOT PSEUDO_LOCALSTATEDIR LOGFIFO" python useradd_sysroot_sstate() { - for cmd, sort_prefix in [("groupadd", "01"), ("useradd", "02"), ("groupmems", "03")]: + for cmd, sort_prefix in [("groupadd", "01"), ("useradd", "02"), ("usermod", "03"), ("groupmems", "04")]: scriptfile = None task = d.getVar("BB_CURRENTTASK") if task == "package_setscene":
@@ -216,9 +238,9 @@ def update_useradd_after_parse(d): bb.fatal("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE', False)) for pkg in useradd_packages.split(): - d.appendVarFlag("do_populate_sysroot", "vardeps", " USERADD_PARAM:%s GROUPADD_PARAM:%s GROUPMEMS_PARAM:%s" % (pkg, pkg, pkg)) - if not d.getVar('USERADD_PARAM:%s' % pkg) and not d.getVar('GROUPADD_PARAM:%s' % pkg) and not d.getVar('GROUPMEMS_PARAM:%s' % pkg): - bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE', False), pkg)) + d.appendVarFlag("do_populate_sysroot", "vardeps", f" USERADD_PARAM:{pkg} GROUPADD_PARAM:{pkg} USERMOD_PARAM:{pkg} GROUPMEMS_PARAM:{pkg}") + if not any(d.getVar(f"{name}_PARAM:{pkg}") for name in ["USERADD", "GROUPADD", "USERMOD", "GROUPMEMS"]): + bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM, USERMOD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE', False), pkg)) python __anonymous() { if not bb.data.inherits_class('nativesdk', d) \
@@ -226,8 +248,8 @@ python __anonymous() { update_useradd_after_parse(d) } -# Return a single [GROUP|USER]ADD_PARAM formatted string which includes the -# [group|user]add parameters for all USERADD_PACKAGES in this recipe +# Return a single (GROUPADD|USERADD|USERMOD)_PARAM formatted string which includes the +# (groupadd|useradd|usermod) parameters for all USERADD_PACKAGES in this recipe def get_all_cmd_params(d, cmd_type): import string
@@ -260,10 +282,11 @@ fakeroot python populate_packages:prepend() { preinst += 'bbfatal () {\n\techo "ERROR: $*"\n\texit 1\n}\n' preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd') preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd') + preinst += 'perform_usermod () {\n%s}\n' % d.getVar('perform_usermod') preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems') preinst += d.getVar('useradd_preinst') # Expand out the *_PARAM variables to the package specific versions - for rep in ["GROUPADD_PARAM", "USERADD_PARAM", "GROUPMEMS_PARAM"]: + for rep in ["GROUPADD_PARAM", "USERADD_PARAM", "USERMOD_PARAM", "GROUPMEMS_PARAM"]: val = d.getVar(rep + ":" + pkg) or "" preinst = preinst.replace("${" + rep + "}", val) d.setVar('pkg_preinst:%s' % pkg, preinst)
meta/conf/documentation.conf +1 -0
@@ -451,6 +451,7 @@ USERADD_PACKAGES[doc] = "When a recipe inherits the useradd class, this variable USERADD_PARAM[doc] = "When a recipe inherits the useradd class, this variable specifies for a package what parameters should be passed to the useradd command if you wish to add a user to the system when the package is installed." USERADD_UID_TABLES[doc] = "Specifies a password file to use for obtaining static user identification (uid) values when the OpenEmbedded build system adds a user to the system during package installation." USERADDEXTENSION[doc] = "When set to 'useradd-staticids', causes the OpenEmbedded build system to base all user and group additions on files listed in USERADD_UID_TABLES and USERADD_GID_TABLES." +USERMOD_PARAM[doc] = "When a recipe inherits the useradd class, this variable specifies for a package what parameters should be passed to the usermod command if you wish to modify a user when the package is installed. Typically used to add the user to one or more groups." #W
PATCH v2 3/6 package.bbclass: Add USERMOD_PARAM to PACKAGEVARS
In reply to Peter KjellerstedtThis complements the USERADD_PARAM and GROUPADD_PARAM variables.
[YOCTO#16277]
---
PATCHv2: No changes.
meta/classes-global/package.bbclass | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
meta/classes-global/package.bbclass +1 -1
@@ -438,7 +438,7 @@ python package_depchains() { # Since bitbake can't determine which variables are accessed during package # iteration, we need to list them here: -PACKAGEVARS = "FILES RDEPENDS RRECOMMENDS SUMMARY DESCRIPTION RSUGGESTS RPROVIDES RCONFLICTS PKG ALLOW_EMPTY pkg_postinst pkg_postrm pkg_postinst_ontarget INITSCRIPT_NAME INITSCRIPT_PARAMS DEBIAN_NOAUTONAME ALTERNATIVE PKGE PKGV PKGR USERADD_PARAM GROUPADD_PARAM CONFFILES SYSTEMD_SERVICE LICENSE SECTION pkg_preinst pkg_prerm RREPLACES GROUPMEMS_PARAM SYSTEMD_AUTO_ENABLE SKIP_FILEDEPS PRIVATE_LIBS PACKAGE_ADD_METADATA" +PACKAGEVARS = "FILES RDEPENDS RRECOMMENDS SUMMARY DESCRIPTION RSUGGESTS RPROVIDES RCONFLICTS PKG ALLOW_EMPTY pkg_postinst pkg_postrm pkg_postinst_ontarget INITSCRIPT_NAME INITSCRIPT_PARAMS DEBIAN_NOAUTONAME ALTERNATIVE PKGE PKGV PKGR USERADD_PARAM GROUPADD_PARAM USERMOD_PARAM CONFFILES SYSTEMD_SERVICE LICENSE SECTION pkg_preinst pkg_prerm RREPLACES GROUPMEMS_PARAM SYSTEMD_AUTO_ENABLE SKIP_FILEDEPS PRIVATE_LIBS PACKAGE_ADD_METADATA" def gen_packagevar(d, pkgvars="PACKAGEVARS"): ret = []
PATCH v2 4/6 useradd_base.bbclass: Emulate the groupmems command using usermod
In reply to Peter KjellerstedtThe groupmems command does to support the --prefix option, which is being used since commit a7b846ba7d6d63a5e59939d75d9c5fe3e4cbb0e9. However, the groupmems command can easily be emulated using the usermod command.
[YOCTO#16277]
---
PATCHv2: Fail if --add and/or --group are not specified.
meta/classes/useradd_base.bbclass | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
meta/classes/useradd_base.bbclass +10 -3
@@ -56,13 +56,20 @@ perform_groupmems () { bbnote "${PN}: Performing groupmems with [$opts]" local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'` local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'` - bbnote "${PN}: Running groupmems command with group $groupname and user $username" + local prefix=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-P" || $i == "--prefix") print $(i+1) }'` + if test "x$groupname" = "x"; then + bbfatal "${PN}: groupmems: No user specified using --add." + + elif test "x$username" = "x"; then + bbfatal "${PN}: groupmems: No group specified using --group." + fi + bbnote "${PN}: Emulating groupmems command using usermod with group $groupname and user $username" local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*$" $rootdir/etc/group || true`" if test "x$mem_exists" = "x"; then - eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmems \$opts\" || true + eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO usermod \${prefix:+--prefix \$prefix} --append --groups \$groupname \$username\" || true mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*$" $rootdir/etc/group || true`" if test "x$mem_exists" = "x"; then - bbfatal "${PN}: groupmems command did not succeed." + bbfatal "${PN}: groupmems command (emulated using usermod) did not succeed." fi else bbnote "${PN}: group $groupname already contains $username, not re-adding it"
PATCH v2 5/6 useradd.bbclass: Show a deprecation warning if GROUPMEMS_PARAM is used
In reply to Peter KjellerstedtTell the user that they should switch to using USERMOD_PARAM instead of GROUPMEMS_PARAM.
[YOCTO#16277]
---
PATCHv2: Tweaked the warning message a little.
meta/classes/useradd.bbclass | 9 +++++++++ 1 file changed, 9 insertions(+)
meta/classes/useradd.bbclass +9 -0
@@ -308,6 +308,15 @@ fakeroot python populate_packages:prepend() { update_useradd_package(pkg) } +do_recipe_qa[postfuncs] += "recipe_qa_deprecate_groupmems_param" +python recipe_qa_deprecate_groupmems_param() { + useradd_packages = d.getVar('USERADD_PACKAGES') or "" + for pkg in useradd_packages.split(): + if d.getVar(f"GROUPMEMS_PARAM:{pkg}"): + bb.warn("The GROUPMEMS_PARAM variable is deprecated. Please use USERMOD_PARAM instead.") + return +} + # Use the following to extend the useradd with custom functions USERADDEXTENSION ?= ""
PATCH v2 6/6 extrauser.bbclass: Switch from using --root to --prefix
In reply to Peter KjellerstedtThis corresponds to commit a7b846ba7d6d63a5e59939d75d9c5fe3e4cbb0e9, which updated useradd.bbclass to use the --prefix option instead of the --root option when specifying the destination directory.
---
PATCHv2: New in v2.
meta/classes/extrausers.bbclass | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
meta/classes/extrausers.bbclass +6 -6
@@ -38,25 +38,25 @@ set_user_group () { # this setting is actually a serial process. So we only retry once. case $cmd in useradd) - perform_useradd "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" + perform_useradd "${IMAGE_ROOTFS}" "--prefix ${IMAGE_ROOTFS} $opts" ;; groupadd) - perform_groupadd "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" + perform_groupadd "${IMAGE_ROOTFS}" "--prefix ${IMAGE_ROOTFS} $opts" ;; userdel) - perform_userdel "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" + perform_userdel "${IMAGE_ROOTFS}" "--prefix ${IMAGE_ROOTFS} $opts" ;; groupdel) - perform_groupdel "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" + perform_groupdel "${IMAGE_ROOTFS}" "--prefix ${IMAGE_ROOTFS} $opts" ;; usermod) - perform_usermod "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" + perform_usermod "${IMAGE_ROOTFS}" "--prefix ${IMAGE_ROOTFS} $opts" ;; passwd-expire) perform_passwd_expire "${IMAGE_ROOTFS}" "$opts" ;; groupmod) - perform_groupmod "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" + perform_groupmod "${IMAGE_ROOTFS}" "--prefix ${IMAGE_ROOTFS} $opts" ;; *) bbfatal "Invalid command in EXTRA_USERS_PARAMS: $cmd"