extrausers.bbclass 2.2 KB

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