Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start_container: support setting environment variables #15

Merged
merged 7 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion make_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def build_kernel(arch, kconfig, src, out, compiler, make_args):
print('Going to run the container in the interactive mode (without build log)')
stdout_destination = None

start_container_cmd.extend(['make', 'O=~/out/'])
start_container_cmd.extend(['--', 'make', 'O=../out/'])

if compiler.startswith('clang'):
print('Compiling with clang requires \'CC=clang\'')
Expand Down
108 changes: 72 additions & 36 deletions start_container.sh
Original file line number Diff line number Diff line change
@@ -1,61 +1,97 @@
#!/bin/bash

function print_help {
echo "usage: $0 compiler src_dir out_dir [-n] [-e VAR] [-h] [-v] [-- cmd with args]"
echo " -n launch container in non-interactive mode"
echo " -e add environment variable in the container (may be used multiple times)"
echo " -h print this help"
echo " -v enable debug output"
echo ""
echo " If cmd is empty, we will start an interactive bash in the container."
}

groups | grep docker
NEED_SUDO=$?

if [ $NEED_SUDO -eq 1 ]
then
set -eu

if [ $NEED_SUDO -eq 1 ]; then
echo "Hey, we gonna use sudo for running docker"
SUDO_CMD="sudo"
else
echo "Hey, you are in docker group, sudo is not needed"
SUDO_CMD=""
fi

set -e

if [ $# -lt 3 ]
then
echo "usage: $0 compiler src_dir out_dir [-n] [cmd with args]"
echo " use '-n' for non-interactive session"
echo " if cmd is empty, we will start an interactive bash in the container"
if [ $# -lt 3 ]; then
print_help
exit 1
fi

COMPILER=$1
SRC="$2"
OUT="$3"
shift 3

# defaults
CIDFILE=""
ENV=""
INTERACTIVE="-it"

while [[ $# -gt 0 ]]; do
case $1 in
-n | --non-interactive)
INTERACTIVE=""
CIDFILE="--cidfile $OUT/container.id"
echo "Run docker in NON-interactive mode"
shift
;;
-e | --env)
# `set -eu` will prevent out-of-bounds access
ENV="$ENV -e $2"
shift 2
;;
-v | --verbose)
set -x
shift
;;
-h | --help)
print_help
exit 0
;;
--)
shift
break
;;
*)
echo "Unknown option $1"
print_help
exit 1
;;
esac
done

echo "Starting \"kernel-build-container:$COMPILER\""

SRC="$2"
echo "Source code directory \"$SRC\" is mounted at \"~/src\""
if [ ! -z "$ENV" ]; then
echo "Container environment arguments: $ENV"
fi

OUT="$3"
echo "Build output directory \"$OUT\" is mounted at \"~/out\""

shift
shift
shift

if [ $# -gt 0 -a "$1" = "-n" ]
then
INTERACTIVE=""
CIDFILE="--cidfile $OUT/container.id"
echo "Run docker in NON-interactive mode"
shift
else
INTERACTIVE="-it"
CIDFILE=""
echo "Run docker in interactive mode"
if [ ! -z $INTERACTIVE ]; then
echo "Gonna run docker in interactive mode"
fi

if [ $# -gt 0 ]
then
echo -e "Gonna run \"$@\"\n"
echo "Mount source code directory \"$SRC\" at \"/home/$(id -nu)/src\""
echo "Mount build output directory \"$OUT\" at \"/home/$(id -nu)/out\""

if [ $# -gt 0 ]; then
echo -e "Gonna run command \"$@\"\n"
else
echo -e "Gonna run interactive bash...\n"
echo -e "Gonna run bash\n"
fi

# Z for setting SELinux label
exec $SUDO_CMD docker run $INTERACTIVE $CIDFILE --rm \
-v $SRC:/home/$(id -nu)/src:Z \
-v $OUT:/home/$(id -nu)/out:Z \
kernel-build-container:$COMPILER "$@"
exec $SUDO_CMD docker run $ENV $INTERACTIVE $CIDFILE --rm \
-v $SRC:/home/$(id -nu)/src:Z \
-v $OUT:/home/$(id -nu)/out:Z \
kernel-build-container:$COMPILER "$@"