diff options
| author | Chen Qi <Qi.Chen@windriver.com> | 2013-07-19 09:52:25 +0800 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2013-07-24 11:35:35 +0100 |
| commit | 50d2548f74322c130129cb2e7bb0b763c8774968 (patch) | |
| tree | 6d4b2dfc7a6a7008f72287af08e0e6791ca639be /meta/classes/useradd_base.bbclass | |
| parent | 18f39e545674d7405cbdf4486a911c577177706f (diff) | |
| download | poky-50d2548f74322c130129cb2e7bb0b763c8774968.tar.gz | |
useradd.bbclass: add a new base class and code refactor
Add a new base class, useradd_base.bbclass, which is mainly a
collection of basic functions for user/group settings.
The useradd_base.bbclass is intended to be inherited by useradd.bbclass
and the extrausers.bbclass to avoid code cuplication.
[YOCTO #4074]
(From OE-Core rev: 2a57bb7e9a7e154578aa7cb9aeebdf398a54ec00)
Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
Signed-off-by: Saul Wold <sgw@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/classes/useradd_base.bbclass')
| -rw-r--r-- | meta/classes/useradd_base.bbclass | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/meta/classes/useradd_base.bbclass b/meta/classes/useradd_base.bbclass new file mode 100644 index 0000000000..7aafe29a4a --- /dev/null +++ b/meta/classes/useradd_base.bbclass | |||
| @@ -0,0 +1,230 @@ | |||
| 1 | # This bbclass provides basic functionality for user/group settings. | ||
| 2 | # This bbclass is intended to be inherited by useradd.bbclass and | ||
| 3 | # extrausers.bbclass. | ||
| 4 | |||
| 5 | # The following functions basically have similar logic. | ||
| 6 | # *) Perform necessary checks before invoking the actual command | ||
| 7 | # *) Invoke the actual command, make retries if necessary | ||
| 8 | # *) Error out if an error occurs. | ||
| 9 | |||
| 10 | # Note that before invoking these functions, make sure the global variable | ||
| 11 | # PSEUDO is set up correctly. | ||
| 12 | |||
| 13 | perform_groupadd () { | ||
| 14 | local rootdir="$1" | ||
| 15 | local opts="$2" | ||
| 16 | local retries="$3" | ||
| 17 | bbnote "Performing groupadd with [$opts] and $retries times of retry" | ||
| 18 | local groupname=`echo "$opts" | awk '{ print $NF }'` | ||
| 19 | local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" | ||
| 20 | if test "x$group_exists" = "x"; then | ||
| 21 | local count=0 | ||
| 22 | while true; do | ||
| 23 | eval $PSEUDO groupadd $opts || true | ||
| 24 | group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" | ||
| 25 | if test "x$group_exists" = "x"; then | ||
| 26 | bbwarn "groupadd command did not succeed. Retrying..." | ||
| 27 | sleep 1 | ||
| 28 | else | ||
| 29 | break | ||
| 30 | fi | ||
| 31 | count=`expr $count + 1` | ||
| 32 | if test $count = $retries; then | ||
| 33 | bbfatal "Tried running groupadd command $retries times without scucess, giving up" | ||
| 34 | fi | ||
| 35 | done | ||
| 36 | else | ||
| 37 | bbwarn "group $groupname already exists, not re-creating it" | ||
| 38 | fi | ||
| 39 | } | ||
| 40 | |||
| 41 | perform_useradd () { | ||
| 42 | local rootdir="$1" | ||
| 43 | local opts="$2" | ||
| 44 | local retries="$3" | ||
| 45 | bbnote "Performing useradd with [$opts] and $retries times of retry" | ||
| 46 | local username=`echo "$opts" | awk '{ print $NF }'` | ||
| 47 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" | ||
| 48 | if test "x$user_exists" = "x"; then | ||
| 49 | local count=0 | ||
| 50 | while true; do | ||
| 51 | eval $PSEUDO useradd $opts || true | ||
| 52 | user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" | ||
| 53 | if test "x$user_exists" = "x"; then | ||
| 54 | bbwarn "useradd command did not succeed. Retrying..." | ||
| 55 | sleep 1 | ||
| 56 | else | ||
| 57 | break | ||
| 58 | fi | ||
| 59 | count=`expr $count + 1` | ||
| 60 | if test $count = $retries; then | ||
| 61 | bbfatal "Tried running useradd command $retries times without scucess, giving up" | ||
| 62 | fi | ||
| 63 | done | ||
| 64 | else | ||
| 65 | bbwarn "user $username already exists, not re-creating it" | ||
| 66 | fi | ||
| 67 | } | ||
| 68 | |||
| 69 | perform_groupmems () { | ||
| 70 | local rootdir="$1" | ||
| 71 | local opts="$2" | ||
| 72 | local retries="$3" | ||
| 73 | bbnote "Performing groupmems with [$opts] and $retries times of retry" | ||
| 74 | local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'` | ||
| 75 | local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'` | ||
| 76 | bbnote "Running groupmems command with group $groupname and user $username" | ||
| 77 | # groupmems fails if /etc/gshadow does not exist | ||
| 78 | local gshadow="" | ||
| 79 | if [ -f $rootdir${sysconfdir}/gshadow ]; then | ||
| 80 | gshadow="yes" | ||
| 81 | else | ||
| 82 | gshadow="no" | ||
| 83 | touch $rootdir${sysconfdir}/gshadow | ||
| 84 | fi | ||
| 85 | local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`" | ||
| 86 | if test "x$mem_exists" = "x"; then | ||
| 87 | local count=0 | ||
| 88 | while true; do | ||
| 89 | eval $PSEUDO groupmems $opts || true | ||
| 90 | mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`" | ||
| 91 | if test "x$mem_exists" = "x"; then | ||
| 92 | bbwarn "groupmems command did not succeed. Retrying..." | ||
| 93 | sleep 1 | ||
| 94 | else | ||
| 95 | break | ||
| 96 | fi | ||
| 97 | count=`expr $count + 1` | ||
| 98 | if test $count = $retries; then | ||
| 99 | if test "x$gshadow" = "xno"; then | ||
| 100 | rm -f $rootdir${sysconfdir}/gshadow | ||
| 101 | rm -f $rootdir${sysconfdir}/gshadow- | ||
| 102 | fi | ||
| 103 | bbfatal "Tried running groupmems command $retries times without scucess, giving up" | ||
| 104 | fi | ||
| 105 | done | ||
| 106 | else | ||
| 107 | bbwarn "group $groupname already contains $username, not re-adding it" | ||
| 108 | fi | ||
| 109 | if test "x$gshadow" = "xno"; then | ||
| 110 | rm -f $rootdir${sysconfdir}/gshadow | ||
| 111 | rm -f $rootdir${sysconfdir}/gshadow- | ||
| 112 | fi | ||
| 113 | } | ||
| 114 | |||
| 115 | perform_groupdel () { | ||
| 116 | local rootdir="$1" | ||
| 117 | local opts="$2" | ||
| 118 | local retries="$3" | ||
| 119 | bbnote "Performing groupdel with [$opts] and $retries times of retry" | ||
| 120 | local groupname=`echo "$opts" | awk '{ print $NF }'` | ||
| 121 | local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" | ||
| 122 | if test "x$group_exists" != "x"; then | ||
| 123 | local count=0 | ||
| 124 | while true; do | ||
| 125 | eval $PSEUDO groupdel $opts || true | ||
| 126 | group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" | ||
| 127 | if test "x$group_exists" != "x"; then | ||
| 128 | bbwarn "groupdel command did not succeed. Retrying..." | ||
| 129 | sleep 1 | ||
| 130 | else | ||
| 131 | break | ||
| 132 | fi | ||
| 133 | count=`expr $count + 1` | ||
| 134 | if test $count = $retries; then | ||
| 135 | bbfatal "Tried running groupdel command $retries times without scucess, giving up" | ||
| 136 | fi | ||
| 137 | done | ||
| 138 | else | ||
| 139 | bbwarn "group $groupname doesn't exist, not removing it" | ||
| 140 | fi | ||
| 141 | } | ||
| 142 | |||
| 143 | perform_userdel () { | ||
| 144 | local rootdir="$1" | ||
| 145 | local opts="$2" | ||
| 146 | local retries="$3" | ||
| 147 | bbnote "Performing userdel with [$opts] and $retries times of retry" | ||
| 148 | local username=`echo "$opts" | awk '{ print $NF }'` | ||
| 149 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" | ||
| 150 | if test "x$user_exists" != "x"; then | ||
| 151 | local count=0 | ||
| 152 | while true; do | ||
| 153 | eval $PSEUDO userdel $opts || true | ||
| 154 | user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" | ||
| 155 | if test "x$user_exists" != "x"; then | ||
| 156 | bbwarn "userdel command did not succeed. Retrying..." | ||
| 157 | sleep 1 | ||
| 158 | else | ||
| 159 | break | ||
| 160 | fi | ||
| 161 | count=`expr $count + 1` | ||
| 162 | if test $count = $retries; then | ||
| 163 | bbfatal "Tried running userdel command $retries times without scucess, giving up" | ||
| 164 | fi | ||
| 165 | done | ||
| 166 | else | ||
| 167 | bbwarn "user $username doesn't exist, not removing it" | ||
| 168 | fi | ||
| 169 | } | ||
| 170 | |||
| 171 | perform_groupmod () { | ||
| 172 | # Other than the return value of groupmod, there's no simple way to judge whether the command | ||
| 173 | # succeeds, so we disable -e option temporarily | ||
| 174 | set +e | ||
| 175 | local rootdir="$1" | ||
| 176 | local opts="$2" | ||
| 177 | local retries="$3" | ||
| 178 | bbnote "Performing groupmod with [$opts] and $retries times of retry" | ||
| 179 | local groupname=`echo "$opts" | awk '{ print $NF }'` | ||
| 180 | local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" | ||
| 181 | if test "x$group_exists" != "x"; then | ||
| 182 | local count=0 | ||
| 183 | while true; do | ||
| 184 | eval $PSEUDO groupmod $opts | ||
| 185 | if test $? != 0; then | ||
| 186 | bbwarn "groupmod command did not succeed. Retrying..." | ||
| 187 | sleep 1 | ||
| 188 | else | ||
| 189 | break | ||
| 190 | fi | ||
| 191 | count=`expr $count + 1` | ||
| 192 | if test $count = $retries; then | ||
| 193 | bbfatal "Tried running groupmod command $retries times without scucess, giving up" | ||
| 194 | fi | ||
| 195 | done | ||
| 196 | else | ||
| 197 | bbwarn "group $groupname doesn't exist, unable to modify it" | ||
| 198 | fi | ||
| 199 | set -e | ||
| 200 | } | ||
| 201 | |||
| 202 | perform_usermod () { | ||
| 203 | # Same reason with groupmod, temporarily disable -e option | ||
| 204 | set +e | ||
| 205 | local rootdir="$1" | ||
| 206 | local opts="$2" | ||
| 207 | local retries="$3" | ||
| 208 | bbnote "Performing usermod with [$opts] and $retries times of retry" | ||
| 209 | local username=`echo "$opts" | awk '{ print $NF }'` | ||
| 210 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" | ||
| 211 | if test "x$user_exists" != "x"; then | ||
| 212 | local count=0 | ||
| 213 | while true; do | ||
| 214 | eval $PSEUDO usermod $opts | ||
| 215 | if test $? != 0; then | ||
| 216 | bbwarn "usermod command did not succeed. Retrying..." | ||
| 217 | sleep 1 | ||
| 218 | else | ||
| 219 | break | ||
| 220 | fi | ||
| 221 | count=`expr $count + 1` | ||
| 222 | if test $count = $retries; then | ||
| 223 | bbfatal "Tried running usermod command $retries times without scucess, giving up" | ||
| 224 | fi | ||
| 225 | done | ||
| 226 | else | ||
| 227 | bbwarn "user $username doesn't exist, unable to modify it" | ||
| 228 | fi | ||
| 229 | set -e | ||
| 230 | } | ||
