Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 7 | # This bbclass is used for image level user/group configuration. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | # Inherit this class if you want to make EXTRA_USERS_PARAMS effective. |
| 9 | |
| 10 | # Below is an example showing how to use this functionality. |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 11 | # IMAGE_CLASSES += "extrausers" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | # EXTRA_USERS_PARAMS = "\ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 13 | # useradd -p '' tester; \ |
| 14 | # groupadd developers; \ |
| 15 | # userdel nobody; \ |
| 16 | # groupdel -g video; \ |
| 17 | # groupmod -g 1020 developers; \ |
| 18 | # usermod -s /bin/sh tester; \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | # " |
| 20 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | inherit useradd_base |
| 22 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 23 | PACKAGE_INSTALL:append = " ${@['', 'base-passwd shadow'][bool(d.getVar('EXTRA_USERS_PARAMS'))]}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | |
| 25 | # Image level user / group settings |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 26 | ROOTFS_POSTPROCESS_COMMAND:append = " set_user_group" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | # Image level user / group settings |
| 29 | set_user_group () { |
| 30 | user_group_settings="${EXTRA_USERS_PARAMS}" |
| 31 | export PSEUDO="${FAKEROOTENV} ${STAGING_DIR_NATIVE}${bindir}/pseudo" |
| 32 | setting=`echo $user_group_settings | cut -d ';' -f1` |
| 33 | remaining=`echo $user_group_settings | cut -d ';' -f2-` |
| 34 | while test "x$setting" != "x"; do |
| 35 | cmd=`echo $setting | cut -d ' ' -f1` |
| 36 | opts=`echo $setting | cut -d ' ' -f2-` |
| 37 | # Different from useradd.bbclass, there's no file locking issue here, as |
| 38 | # this setting is actually a serial process. So we only retry once. |
| 39 | case $cmd in |
| 40 | useradd) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 41 | perform_useradd "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | ;; |
| 43 | groupadd) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 44 | perform_groupadd "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | ;; |
| 46 | userdel) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 47 | perform_userdel "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | ;; |
| 49 | groupdel) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 50 | perform_groupdel "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | ;; |
| 52 | usermod) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 53 | perform_usermod "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | ;; |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 55 | passwd-expire) |
| 56 | perform_passwd_expire "${IMAGE_ROOTFS}" "$opts" |
| 57 | ;; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | groupmod) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 59 | perform_groupmod "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | ;; |
| 61 | *) |
| 62 | bbfatal "Invalid command in EXTRA_USERS_PARAMS: $cmd" |
| 63 | ;; |
| 64 | esac |
| 65 | # Avoid infinite loop if the last parameter doesn't end with ';' |
| 66 | if [ "$setting" = "$remaining" ]; then |
| 67 | break |
| 68 | fi |
| 69 | # iterate to the next setting |
| 70 | setting=`echo $remaining | cut -d ';' -f1` |
| 71 | remaining=`echo $remaining | cut -d ';' -f2-` |
| 72 | done |
| 73 | } |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 74 | |
| 75 | USERADDEXTENSION ?= "" |
| 76 | |
| 77 | inherit ${USERADDEXTENSION} |