3 # base-passwd-cross provides the default passwd and group files in the
4 # target sysroot, and shadow -native and -sysroot provide the utilities
5 # and support files needed to add and modify user and group accounts
6 DEPENDS_append_class-target = " base-files shadow-native shadow-sysroot shadow"
8 # This preinstall function can be run in four different contexts:
10 # a) Before do_install
11 # b) At do_populate_sysroot_setscene when installing from sstate packages
12 # c) As the preinst script in the target package at do_rootfs time
13 # d) As the preinst script in the target package on device as a package upgrade
19 if test "x$D" != "x"; then
20 # Installing into a sysroot
24 # Make sure login.defs is there, this is to make debian package backend work
25 # correctly while doing rootfs.
26 # The problem here is that if /etc/login.defs is treated as a config file for
27 # shadow package, then while performing preinsts for packages that depend on
28 # shadow, there might only be /etc/login.def.dpkg-new there in root filesystem.
29 if [ ! -e $D${sysconfdir}/login.defs -a -e $D${sysconfdir}/login.defs.dpkg-new ]; then
30 cp $D${sysconfdir}/login.defs.dpkg-new $D${sysconfdir}/login.defs
33 # user/group lookups should match useradd/groupadd --root
34 export PSEUDO_PASSWD="$SYSROOT:${STAGING_DIR_NATIVE}"
37 # If we're not doing a special SSTATE/SYSROOT install
38 # then set the values, otherwise use the environment
39 if test "x$UA_SYSROOT" = "x"; then
40 # Installing onto a target
41 # Add groups and users defined only for this package
42 GROUPADD_PARAM="${GROUPADD_PARAM}"
43 USERADD_PARAM="${USERADD_PARAM}"
44 GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
47 # Perform group additions first, since user additions may depend
48 # on these groups existing
49 if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then
50 echo "Running groupadd commands..."
51 # Invoke multiple instances of groupadd for parameter lists
53 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
54 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
55 while test "x$opts" != "x"; do
56 perform_groupadd "$SYSROOT" "$OPT $opts"
57 if test "x$opts" = "x$remaining"; then
60 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
61 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
65 if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then
66 echo "Running useradd commands..."
67 # Invoke multiple instances of useradd for parameter lists
69 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
70 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
71 while test "x$opts" != "x"; do
72 perform_useradd "$SYSROOT" "$OPT $opts"
73 if test "x$opts" = "x$remaining"; then
76 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
77 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
81 if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then
82 echo "Running groupmems commands..."
83 # Invoke multiple instances of groupmems for parameter lists
85 opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
86 remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
87 while test "x$opts" != "x"; do
88 perform_groupmems "$SYSROOT" "$OPT $opts"
89 if test "x$opts" = "x$remaining"; then
92 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
93 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
99 # Pseudo may (do_install) or may not (do_populate_sysroot_setscene) be running
100 # at this point so we're explicit about the environment so pseudo can load if
101 # not already present.
102 export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir_native}/pseudo"
104 # Explicitly set $D since it isn't set to anything
106 D=${STAGING_DIR_TARGET}
108 # Add groups and users defined for all recipe packages
109 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
110 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
111 GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}"
113 # Tell the system to use the environment vars
119 useradd_sysroot_sstate () {
120 if [ "${BB_CURRENTTASK}" = "package_setscene" -o "${BB_CURRENTTASK}" = "populate_sysroot_setscene" ]
126 userdel_sysroot_sstate () {
127 if test "x${STAGING_DIR_TARGET}" != "x"; then
128 if [ "${BB_CURRENTTASK}" = "clean" ]; then
129 export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir_native}/pseudo"
130 OPT="--root ${STAGING_DIR_TARGET}"
132 # Remove groups and users defined for package
133 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
134 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
136 user=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | awk '{ print $NF }'`
137 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
138 while test "x$user" != "x"; do
139 perform_userdel "${STAGING_DIR_TARGET}" "$OPT $user"
140 user=`echo "$remaining" | cut -d ';' -f 1 | awk '{ print $NF }'`
141 remaining=`echo "$remaining" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
144 user=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | awk '{ print $NF }'`
145 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
146 while test "x$user" != "x"; do
147 perform_groupdel "${STAGING_DIR_TARGET}" "$OPT $user"
148 user=`echo "$remaining" | cut -d ';' -f 1 | awk '{ print $NF }'`
149 remaining=`echo "$remaining" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
156 SSTATECLEANFUNCS_append_class-target = " userdel_sysroot_sstate"
158 do_install[prefuncs] += "${SYSROOTFUNC}"
159 SYSROOTFUNC_class-target = "useradd_sysroot"
162 SSTATEPREINSTFUNCS_append_class-target = " useradd_sysroot_sstate"
164 do_package_setscene[depends] += "${USERADDSETSCENEDEPS}"
165 do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}"
166 USERADDSETSCENEDEPS_class-target = "${MLPREFIX}base-passwd:do_populate_sysroot_setscene pseudo-native:do_populate_sysroot_setscene shadow-native:do_populate_sysroot_setscene ${MLPREFIX}shadow-sysroot:do_populate_sysroot_setscene"
167 USERADDSETSCENEDEPS = ""
169 # Recipe parse-time sanity checks
170 def update_useradd_after_parse(d):
171 useradd_packages = d.getVar('USERADD_PACKAGES', True)
173 if not useradd_packages:
174 raise bb.build.FuncFailed("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE', False))
176 for pkg in useradd_packages.split():
177 if not d.getVar('USERADD_PARAM_%s' % pkg, True) and not d.getVar('GROUPADD_PARAM_%s' % pkg, True) and not d.getVar('GROUPMEMS_PARAM_%s' % pkg, True):
178 bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE', False), pkg))
180 python __anonymous() {
181 if not bb.data.inherits_class('nativesdk', d) \
182 and not bb.data.inherits_class('native', d):
183 update_useradd_after_parse(d)
186 # Return a single [GROUP|USER]ADD_PARAM formatted string which includes the
187 # [group|user]add parameters for all USERADD_PACKAGES in this recipe
188 def get_all_cmd_params(d, cmd_type):
191 param_type = cmd_type.upper() + "_PARAM_%s"
194 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
195 for pkg in useradd_packages.split():
196 param = d.getVar(param_type % pkg, True)
198 params.append(param.rstrip(" ;"))
200 return "; ".join(params)
202 # Adds the preinst script into generated packages
203 fakeroot python populate_packages_prepend () {
204 def update_useradd_package(pkg):
205 bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
208 useradd preinst is appended here because pkg_preinst may be
209 required to execute on the target. Not doing so may cause
210 useradd preinst to be invoked twice, causing unwanted warnings.
212 preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
214 preinst = '#!/bin/sh\n'
215 preinst += 'bbnote () {\n\techo "NOTE: $*"\n}\n'
216 preinst += 'bbwarn () {\n\techo "WARNING: $*"\n}\n'
217 preinst += 'bbfatal () {\n\techo "ERROR: $*"\n\texit 1\n}\n'
218 preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd', True)
219 preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd', True)
220 preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems', True)
221 preinst += d.getVar('useradd_preinst', True)
222 d.setVar('pkg_preinst_%s' % pkg, preinst)
225 rdepends = d.getVar("RDEPENDS_%s" % pkg, True) or ""
226 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-passwd'
227 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'shadow'
228 # base-files is where the default /etc/skel is packaged
229 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-files'
230 d.setVar("RDEPENDS_%s" % pkg, rdepends)
232 # Add the user/group preinstall scripts and RDEPENDS requirements
233 # to packages specified by USERADD_PACKAGES
234 if not bb.data.inherits_class('nativesdk', d) \
235 and not bb.data.inherits_class('native', d):
236 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
237 for pkg in useradd_packages.split():
238 update_useradd_package(pkg)
241 # Use the following to extend the useradd with custom functions
242 USERADDEXTENSION ?= ""
244 inherit ${USERADDEXTENSION}