Skip to content

Commit

Permalink
devcontainer: fixup user.sh files
Browse files Browse the repository at this point in the history
Signed-off-by: Maryam Tahhan <[email protected]>
  • Loading branch information
maryamtahhan committed Nov 25, 2024
1 parent 85c7d89 commit 5a3084e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
35 changes: 17 additions & 18 deletions .devcontainer/fedora/user.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/sh -e
#! /bin/sh
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2024 Red Hat, Inc.

Expand All @@ -8,7 +8,7 @@ username=""
userid=""

usage() {
cat >&2 <<EOF
cat <<EOF >&2
Usage: $0
-u | --user <username>
-g | --gid <userid>
Expand All @@ -17,21 +17,20 @@ EOF
}

# Parse command-line arguments
args=$(getopt -o u:g: --long user:,gid: -n "$0" -- "$@")
if [ $? -ne 0 ]; then usage; fi
args=$(getopt -o u:g: --long user:,gid: -n "$0" -- "$@") || usage

eval set -- "$args"
while [ $# -gt 0 ]; do
case $1 in
case "$1" in
-h | --help)
usage
;;
-u | --user)
username=$2
username="$2"
shift 2
;;
-g | --gid)
userid=$2
userid="$2"
shift 2
;;
--)
Expand Down Expand Up @@ -61,27 +60,27 @@ if [ "$USER_NAME" = "root" ]; then
exit 0
fi

if ! [ $(getent group $USER_NAME) ]; then
groupadd --gid $USER_GID $USER_NAME
if ! getent group "$USER_NAME" >/dev/null; then
groupadd --gid "$USER_GID" "$USER_NAME"
fi

if ! [ $(getent passwd $USER_NAME) ]; then
useradd --uid $USER_UID --gid $USER_GID -m $USER_NAME
if ! getent passwd "$USER_NAME" >/dev/null; then
useradd --uid "$USER_UID" --gid "$USER_GID" -m "$USER_NAME"
fi

# Ensure $HOME exists when starting
if [ ! -d "${HOME}" ]; then
mkdir -p "${HOME}"
if [ ! -d "${HOME_DIR}" ]; then
mkdir -p "${HOME_DIR}"
fi

# Add current (arbitrary) user to /etc/passwd and /etc/group
if [ -w /etc/passwd ]; then
echo "${USER_NAME:-user}:x:$(id -u):0:${USER_NAME:-user} user:${HOME}:/bin/bash" >> /etc/passwd
echo "${USER_NAME:-user}:x:$(id -u):0:${USER_NAME:-user}:${HOME_DIR}:/bin/bash" >> /etc/passwd
echo "${USER_NAME:-user}:x:$(id -u):" >> /etc/group
fi

# Fix up permissions
chown $USER_NAME:$USER_GID -R /home/$USER_NAME
chown $USER_NAME:$USER_GID -R /opt
mkdir -p /run/user/$USER_UID
chown $USER_NAME:$USER_GID /run/user/$USER_UID
chown "$USER_NAME:$USER_GID" -R "/home/$USER_NAME"
chown "$USER_NAME:$USER_GID" -R /opt
mkdir -p "/run/user/$USER_UID"
chown "$USER_NAME:$USER_GID" "/run/user/$USER_UID"
35 changes: 17 additions & 18 deletions .devcontainer/ubuntu/user.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/sh -e
#! /bin/sh
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2024 Red Hat, Inc.

Expand All @@ -8,7 +8,7 @@ username=""
userid=""

usage() {
cat >&2 <<EOF
cat <<EOF >&2
Usage: $0
-u | --user <username>
-g | --gid <userid>
Expand All @@ -17,21 +17,20 @@ EOF
}

# Parse command-line arguments
args=$(getopt -o u:g: --long user:,gid: -n "$0" -- "$@")
if [ $? -ne 0 ]; then usage; fi
args=$(getopt -o u:g: --long user:,gid: -n "$0" -- "$@") || usage

eval set -- "$args"
while [ $# -gt 0 ]; do
case $1 in
case "$1" in
-h | --help)
usage
;;
-u | --user)
username=$2
username="$2"
shift 2
;;
-g | --gid)
userid=$2
userid="$2"
shift 2
;;
--)
Expand Down Expand Up @@ -61,27 +60,27 @@ if [ "$USER_NAME" = "root" ]; then
exit 0
fi

if ! [ $(getent group $USER_NAME) ]; then
groupadd --gid $USER_GID $USER_NAME
if ! getent group "$USER_NAME" >/dev/null; then
groupadd --gid "$USER_GID" "$USER_NAME"
fi

if ! [ $(getent passwd $USER_NAME) ]; then
useradd --uid $USER_UID --gid $USER_GID -m $USER_NAME
if ! getent passwd "$USER_NAME" >/dev/null; then
useradd --uid "$USER_UID" --gid "$USER_GID" -m "$USER_NAME"
fi

# Ensure $HOME exists when starting
if [ ! -d "${HOME}" ]; then
mkdir -p "${HOME}"
if [ ! -d "${HOME_DIR}" ]; then
mkdir -p "${HOME_DIR}"
fi

# Add current (arbitrary) user to /etc/passwd and /etc/group
if [ -w /etc/passwd ]; then
echo "${USER_NAME:-user}:x:$(id -u):0:${USER_NAME:-user} user:${HOME}:/bin/bash" >> /etc/passwd
echo "${USER_NAME:-user}:x:$(id -u):0:${USER_NAME:-user}:${HOME_DIR}:/bin/bash" >> /etc/passwd
echo "${USER_NAME:-user}:x:$(id -u):" >> /etc/group
fi

# Fix up permissions
chown $USER_NAME:$USER_GID -R /home/$USER_NAME
chown $USER_NAME:$USER_GID -R /opt
mkdir -p /run/user/$USER_UID
chown $USER_NAME:$USER_GID /run/user/$USER_UID
chown "$USER_NAME:$USER_GID" -R "/home/$USER_NAME"
chown "$USER_NAME:$USER_GID" -R /opt
mkdir -p "/run/user/$USER_UID"
chown "$USER_NAME:$USER_GID" "/run/user/$USER_UID"

0 comments on commit 5a3084e

Please sign in to comment.