summaryrefslogtreecommitdiffstats
path: root/meta/classes/useradd.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes/useradd.bbclass')
-rw-r--r--meta/classes/useradd.bbclass211
1 files changed, 211 insertions, 0 deletions
diff --git a/meta/classes/useradd.bbclass b/meta/classes/useradd.bbclass
new file mode 100644
index 0000000..3dd7a61
--- /dev/null
+++ b/meta/classes/useradd.bbclass
@@ -0,0 +1,211 @@
1inherit useradd_base
2
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
6DEPENDS_append = "${USERADDDEPENDS}"
7USERADDDEPENDS = " base-passwd shadow-native shadow-sysroot shadow"
8USERADDDEPENDS_class-cross = ""
9USERADDDEPENDS_class-native = ""
10USERADDDEPENDS_class-nativesdk = ""
11
12# This preinstall function can be run in four different contexts:
13#
14# a) Before do_install
15# b) At do_populate_sysroot_setscene when installing from sstate packages
16# c) As the preinst script in the target package at do_rootfs time
17# d) As the preinst script in the target package on device as a package upgrade
18#
19useradd_preinst () {
20OPT=""
21SYSROOT=""
22
23if test "x$D" != "x"; then
24 # Installing into a sysroot
25 SYSROOT="$D"
26 OPT="--root $D"
27fi
28
29# If we're not doing a special SSTATE/SYSROOT install
30# then set the values, otherwise use the environment
31if test "x$UA_SYSROOT" = "x"; then
32 # Installing onto a target
33 # Add groups and users defined only for this package
34 GROUPADD_PARAM="${GROUPADD_PARAM}"
35 USERADD_PARAM="${USERADD_PARAM}"
36 GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
37fi
38
39# Perform group additions first, since user additions may depend
40# on these groups existing
41if test "x$GROUPADD_PARAM" != "x"; then
42 echo "Running groupadd commands..."
43 # Invoke multiple instances of groupadd for parameter lists
44 # separated by ';'
45 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1`
46 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-`
47 while test "x$opts" != "x"; do
48 perform_groupadd "$SYSROOT" "$OPT $opts" 10
49 if test "x$opts" = "x$remaining"; then
50 break
51 fi
52 opts=`echo "$remaining" | cut -d ';' -f 1`
53 remaining=`echo "$remaining" | cut -d ';' -f 2-`
54 done
55fi
56
57if test "x$USERADD_PARAM" != "x"; then
58 echo "Running useradd commands..."
59 # Invoke multiple instances of useradd for parameter lists
60 # separated by ';'
61 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1`
62 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-`
63 while test "x$opts" != "x"; do
64 perform_useradd "$SYSROOT" "$OPT $opts" 10
65 if test "x$opts" = "x$remaining"; then
66 break
67 fi
68 opts=`echo "$remaining" | cut -d ';' -f 1`
69 remaining=`echo "$remaining" | cut -d ';' -f 2-`
70 done
71fi
72
73if test "x$GROUPMEMS_PARAM" != "x"; then
74 echo "Running groupmems commands..."
75 # Invoke multiple instances of groupmems for parameter lists
76 # separated by ';'
77 opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1`
78 remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2-`
79 while test "x$opts" != "x"; do
80 perform_groupmems "$SYSROOT" "$OPT $opts" 10
81 if test "x$opts" = "x$remaining"; then
82 break
83 fi
84 opts=`echo "$remaining" | cut -d ';' -f 1`
85 remaining=`echo "$remaining" | cut -d ';' -f 2-`
86 done
87fi
88}
89
90useradd_sysroot () {
91 # Pseudo may (do_install) or may not (do_populate_sysroot_setscene) be running
92 # at this point so we're explicit about the environment so pseudo can load if
93 # not already present.
94 export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir}/pseudo"
95
96 # Explicitly set $D since it isn't set to anything
97 # before do_install
98 D=${STAGING_DIR_TARGET}
99
100 # Add groups and users defined for all recipe packages
101 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
102 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
103 GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}"
104
105 # Tell the system to use the environment vars
106 UA_SYSROOT=1
107
108 useradd_preinst
109}
110
111useradd_sysroot_sstate () {
112 if [ "${BB_CURRENTTASK}" = "package_setscene" -o "${BB_CURRENTTASK}" = "populate_sysroot_setscene" ]
113 then
114 useradd_sysroot
115 fi
116}
117
118do_install[prefuncs] += "${SYSROOTFUNC}"
119SYSROOTFUNC = "useradd_sysroot"
120SYSROOTFUNC_class-cross = ""
121SYSROOTFUNC_class-native = ""
122SYSROOTFUNC_class-nativesdk = ""
123SSTATEPREINSTFUNCS += "${SYSROOTPOSTFUNC}"
124SYSROOTPOSTFUNC = "useradd_sysroot_sstate"
125SYSROOTPOSTFUNC_class-cross = ""
126SYSROOTPOSTFUNC_class-native = ""
127SYSROOTPOSTFUNC_class-nativesdk = ""
128
129USERADDSETSCENEDEPS = "${MLPREFIX}base-passwd:do_populate_sysroot_setscene shadow-native:do_populate_sysroot_setscene ${MLPREFIX}shadow-sysroot:do_populate_sysroot_setscene"
130USERADDSETSCENEDEPS_class-cross = ""
131USERADDSETSCENEDEPS_class-native = ""
132USERADDSETSCENEDEPS_class-nativesdk = ""
133do_package_setscene[depends] += "${USERADDSETSCENEDEPS}"
134do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}"
135
136# Recipe parse-time sanity checks
137def update_useradd_after_parse(d):
138 useradd_packages = d.getVar('USERADD_PACKAGES', True)
139
140 if not useradd_packages:
141 raise bb.build.FuncFailed("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE'))
142
143 for pkg in useradd_packages.split():
144 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):
145 bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE'), pkg))
146
147python __anonymous() {
148 if not bb.data.inherits_class('nativesdk', d) \
149 and not bb.data.inherits_class('native', d):
150 update_useradd_after_parse(d)
151}
152
153# Return a single [GROUP|USER]ADD_PARAM formatted string which includes the
154# [group|user]add parameters for all USERADD_PACKAGES in this recipe
155def get_all_cmd_params(d, cmd_type):
156 import string
157
158 param_type = cmd_type.upper() + "_PARAM_%s"
159 params = []
160
161 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
162 for pkg in useradd_packages.split():
163 param = d.getVar(param_type % pkg, True)
164 if param:
165 params.append(param)
166
167 return "; ".join(params)
168
169# Adds the preinst script into generated packages
170fakeroot python populate_packages_prepend () {
171 def update_useradd_package(pkg):
172 bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
173
174 """
175 useradd preinst is appended here because pkg_preinst may be
176 required to execute on the target. Not doing so may cause
177 useradd preinst to be invoked twice, causing unwanted warnings.
178 """
179 preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
180 if not preinst:
181 preinst = '#!/bin/sh\n'
182 preinst += 'bbnote () {\n%s}\n' % d.getVar('bbnote', True)
183 preinst += 'bbwarn () {\n%s}\n' % d.getVar('bbwarn', True)
184 preinst += 'bbfatal () {\n%s}\n' % d.getVar('bbfatal', True)
185 preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd', True)
186 preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd', True)
187 preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems', True)
188 preinst += d.getVar('useradd_preinst', True)
189 d.setVar('pkg_preinst_%s' % pkg, preinst)
190
191 # RDEPENDS setup
192 rdepends = d.getVar("RDEPENDS_%s" % pkg, True) or ""
193 rdepends += ' ' + d.getVar('MLPREFIX') + 'base-passwd'
194 rdepends += ' ' + d.getVar('MLPREFIX') + 'shadow'
195 # base-files is where the default /etc/skel is packaged
196 rdepends += ' ' + d.getVar('MLPREFIX') + 'base-files'
197 d.setVar("RDEPENDS_%s" % pkg, rdepends)
198
199 # Add the user/group preinstall scripts and RDEPENDS requirements
200 # to packages specified by USERADD_PACKAGES
201 if not bb.data.inherits_class('nativesdk', d) \
202 and not bb.data.inherits_class('native', d):
203 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
204 for pkg in useradd_packages.split():
205 update_useradd_package(pkg)
206}
207
208# Use the following to extend the useradd with custom functions
209USERADDEXTENSION ?= ""
210
211inherit ${USERADDEXTENSION}