From 68f0d2efc9923e0374ce2c290ed7d5a91f0e9bf3 Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Sun, 21 Dec 2025 22:14:21 +0100 Subject: [PATCH] Fixes for Apple container. --- azure-cli | 24 ++++++++++++++++++------ entrypoint.sh | 44 +++++++++++++++----------------------------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/azure-cli b/azure-cli index e58db78..2c01e47 100755 --- a/azure-cli +++ b/azure-cli @@ -2,11 +2,13 @@ # Set default values ACCOUNT_NAME="$(id -un)" -USER_NAME="ubuntu" +USER_NAME="${USER_NAME:-ubuntu}" EXTRA_ARGS=() LIST="" VOLUME_NAME="" +HOME_DIR="/home/${USER_NAME}" +# Parse command line arguments while [ $# -gt 0 ]; do case $1 in --list|-l) @@ -30,6 +32,11 @@ while [ $# -gt 0 ]; do EXTRA_ARGS+=("--mount" "type=bind,source=${VOL_SRC},target=${VOL_DST}") shift 2 ;; + --root) + USER_NAME="root" + HOME_DIR="/root" + shift + ;; --) # Stop parsing arguments shift @@ -114,9 +121,7 @@ if [ ! "FOUND" = "$_FOUND" ]; then # Volume created, initialize it echo "Initializing volume $VOLUME_NAME." - if ! $CMD run --rm -it \ - --mount "type=volume,source=$VOLUME_NAME,target=/home/${USER_NAME}" \ - skoszewski/azure-cli:latest --user "$USER_NAME"; then + if ! $CMD run --rm -it --mount "type=volume,source=$VOLUME_NAME,target=$HOME_DIR" $IMAGE_NAME; then echo "Error: Failed to initialize volume $VOLUME_NAME." >&2 exit 1 fi @@ -127,10 +132,17 @@ EXTRA_ARGS+=( "--mount" "type=volume,source=$VOLUME_NAME,target=/home/${USER_NAME}" "--mount" "type=bind,source=$(pwd),target=/workdir" "--env" "ACCOUNT_NAME=$ACCOUNT_NAME" + "--env" "USER_NAME=$USER_NAME" + "--env" "HOME_DIR=$HOME_DIR" "--name" "azure-cli-$VOLUME_NAME" - "--user" "$USER_NAME" "--workdir" "/workdir" ) +if [ "$USER_NAME" != "root" ]; then + EXTRA_ARGS+=( + "--user" "$USER_NAME" + ) +fi + # Run the container as the specified user -$CMD run --rm -it ${EXTRA_ARGS[@]} $IMAGE_NAME --user "$USER_NAME" "$@" +$CMD run --rm -it ${EXTRA_ARGS[@]} $IMAGE_NAME "$@" diff --git a/entrypoint.sh b/entrypoint.sh index 8a82c90..4486b20 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,47 +3,33 @@ set -e # Setup default values -USER_NAME="ubuntu" - -# Parse arguments -while [ $# -gt 0 ]; do - case "$1" in - --user|-u) - USER_NAME="$2" - shift 2 - ;; - --) - # Stop parsing arguments - shift - break - ;; - *) - break - ;; - esac -done +USER_NAME="${USER_NAME:-ubuntu}" +HOME_DIR="${HOMED_DIR:-/home/${USER_NAME}}" # Check, if we are running as root if [ "$(id -u)" -eq 0 ]; then # Check, if the home directory exists for the specified user - if [ ! -d "/home/${USER_NAME}" ]; then + if [ ! -d "$HOME_DIR" ]; then echo "Error: Home directory for user '${USER_NAME}' does not exist." >&2 exit 1 fi + echo "Preparing home directory for user '${USER_NAME}' at '${HOME_DIR}'." + # Check, ownership of the home directory - OWNER_UID=$(stat -c '%u' "/home/${USER_NAME}") - OWNER_GID=$(stat -c '%g' "/home/${USER_NAME}") - if [ "${OWNER_UID}" -ne 0 ] || [ "${OWNER_GID}" -ne 0 ]; then - # The home directory is not owned by the specfied user, correct it - chown "${USER_NAME}:${USER_NAME}" "/home/${USER_NAME}" + if [ "$(stat -c '%u' "$HOME_DIR")" -eq 0 ]; then + # The home directory is a fresh volume owned by root, change ownership + echo "Changing ownership of home directory to user '${USER_NAME}'." + chown "${USER_NAME}:${USER_NAME}" "$HOME_DIR" fi - # Re-initialize the contents of the home directory - su - "${USER_NAME}" -c "cp -a /etc/skel/. /home/${USER_NAME}/" + if [ "$USER_NAME " != "root" ]; then + # Re-initialize the contents of the home directory + su - "${USER_NAME}" -c "cp -a /etc/skel/. $HOME_DIR" - # We are done as root, quit. The container will be re-run as the specified user. - exit 0 + # We are done as root, quit. The container will be re-run as the specified user. + exit 0 + fi fi # Verify that we are running as the user owning the home directory