Use well-known passwd constants if user root
If the intended user is root, use well-known constants for UID, GID,
and home directory, to avoid wrongly using those of the normal user
running this script.
Using -f flag for groupadd, to avoid erroring out if group name
already exists for this user name.
Tested: No longer fails with weird groupadd errors if
run-unit-test-docker.sh command line contains USER="root" setting.
Signed-off-by: Josh Lehan <krellan@google.com>
Change-Id: Idc6158386ce96a6e2fa97dd0eb56b928f18e7a35
diff --git a/scripts/build-unit-test-docker b/scripts/build-unit-test-docker
index 4d5fb1b..b8337f8 100755
--- a/scripts/build-unit-test-docker
+++ b/scripts/build-unit-test-docker
@@ -679,6 +679,12 @@
gid = os.getgid()
uid = os.getuid()
+# Use well-known constants if user is root
+if username == "root":
+ homedir = "/root"
+ gid = 0
+ uid = 0
+
# Determine the architecture for Docker.
arch = uname("-m").strip()
if arch == "ppc64le":
@@ -880,7 +886,7 @@
RUN echo '{Package.depcache()}' > /tmp/depcache
# Final configuration for the workspace
-RUN grep -q {gid} /etc/group || groupadd -g {gid} {username}
+RUN grep -q {gid} /etc/group || groupadd -f -g {gid} {username}
RUN mkdir -p "{os.path.dirname(homedir)}"
RUN grep -q {uid} /etc/passwd || useradd -d {homedir} -m -u {uid} -g {gid} {username}
RUN sed -i '1iDefaults umask=000' /etc/sudoers