forked from a13xp0p0v/kernel-build-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_container.sh
executable file
·97 lines (83 loc) · 1.82 KB
/
start_container.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
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=$?
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
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\""
if [ ! -z "$ENV" ]; then
echo "Container environment arguments: $ENV"
fi
if [ ! -z $INTERACTIVE ]; then
echo "Gonna run docker in interactive mode"
fi
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 bash\n"
fi
# Z for setting SELinux label
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 "$@"