aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2019-01-07 20:55:45 +0800
committerAaron LI <aly@aaronly.me>2019-01-09 11:49:39 +0800
commit78f8a082b88816fd72e625b073404ac2d6f5c946 (patch)
tree6e5de5fed98d449c0fffc17233d98084f9b642d8
parent96c9dd06a80c981a9d9fa5c9effe72136df96660 (diff)
downloaddfly-update-78f8a082b88816fd72e625b073404ac2d6f5c946.tar.bz2
Add new users/groups according to {master.passwd,group}
Add function add_users() to add new users and groups according to /etc.hdd/{master.passwd,group} files, and call this function after install_system().
-rw-r--r--README7
-rwxr-xr-xdfly-update49
2 files changed, 53 insertions, 3 deletions
diff --git a/README b/README
index dcad080..d40e0ed 100644
--- a/README
+++ b/README
@@ -31,10 +31,11 @@ the following steps:
1. mount the system image file (*.img);
2. backup the current kernel and world (including "/etc");
3. install the kernel and world using cpdup (similar to the installer);
-4. identify the new/changed "/etc" files, rename the conflicting ones
+4. create new users and groups;
+5. identify the new/changed "/etc" files, rename the conflicting ones
with suffix ".__new__" and then copy over;
-5. get obsolete files from "Makefile_upgrade.inc" and remove them;
-6. umount, show the "*.__new__" files need manual merge.
+6. get obsolete files from "Makefile_upgrade.inc" and remove them;
+7. umount, show the "*.__new__" files need manual merge.
Usage
-----
diff --git a/dfly-update b/dfly-update
index 971b1a2..5f4d370 100755
--- a/dfly-update
+++ b/dfly-update
@@ -36,6 +36,7 @@ EC_TAR=18
EC_MTREE=19
EC_CPDUP=20
EC_NOFILE=21
+EC_PW=22
#
@@ -261,6 +262,53 @@ install_system() {
echo " => DONE!"
}
+# Add new users and groups
+add_users() {
+ [ $# -eq 0 ] ||
+ error ${EC_ARGS} "add_users: invalid arguments: $@"
+
+ local fpasswd="${MNT_DIR}/etc.hdd/master.passwd"
+ local fgroup="${MNT_DIR}/etc.hdd/group"
+ local _name _pw _uid _gid _gids item
+ local _class _change _expire _gecos _home _shell _members
+ echo "Adding new users and groups ..."
+
+ echo " => Adding new users ..."
+ _gids=""
+ grep -Ev '^(#.*|\s*)$' ${fpasswd} |
+ while IFS=':' read -r _name _pw _uid _gid _class \
+ _change _expire _gecos _home _shell; do
+ pw usershow ${_name} -q >/dev/null && continue
+
+ # NOTE: There is circular dependence: 'groupadd' requires the members
+ # already exist, while 'useradd' requires the group exists.
+ # So first assign new users to the 'nogroup' group, and make
+ # adjustments after group creation.
+ echo " * ${_name}, ${_uid}, ${_gid}, ${_gecos}, ${_home}, ${_shell}"
+ pw useradd ${_name} \
+ -u ${_uid} -g nogroup -d ${_home} -s ${_shell} \
+ -L "${_class}" -c "${_gecos}" || exit ${EC_PW}
+ _gids="${_gids} ${_name}:${_gid}"
+ done
+
+ echo " => Adding new groups ..."
+ grep -Ev '^(#.*|\s*)$' ${fgroup} |
+ while IFS=':' read -r _name _pw _gid _members; do
+ pw groupshow ${_name} -q >/dev/null && continue
+
+ echo " * ${_name}, ${_gid}, ${_members}"
+ pw groupadd ${_name} -g ${_gid} -M "${_members}" || exit ${EC_PW}
+ done
+
+ echo " => Adjusting the group of new users ..."
+ for item in ${_gids}; do
+ _name=${item%:*}
+ _gid=${item#*:}
+ echo " * ${_name}, ${_gid}"
+ pw usermod ${_name} -g ${_gid} || exit ${EC_PW}
+ done
+}
+
# Upgrade the system with new configuration files
upgrade_system() {
[ $# -eq 0 ] ||
@@ -434,6 +482,7 @@ cmd_upgrade() {
error ${EC_ARGS} "cmd_upgrade: invalid arguments: $@"
install_system
+ add_users
upgrade_system
}