-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrun.sh
executable file
·227 lines (209 loc) · 7.42 KB
/
run.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash -e
# SPDX-License-Identifier: (GPL-2.0+ OR MIT)
# Copyright 2021 Variscite Ltd.
readonly FILE_SCRIPT="$(basename "$0")"
readonly DIR_SCRIPT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
readonly GIT_COMMIT="$(git --git-dir=${DIR_SCRIPT}/.git log -1 --format=%h)"
cd ${DIR_SCRIPT}
UBUNTU_VERSIONS_SUPPORTED=("22.04" "20.04" "18.04" "16.04" "14.04")
UBUNTU_VERSION="20.04"
WORKDIR=$(pwd)
SCRIPT=""
INTERACTIVE="-it"
DOCKER_VOLUMES=""
PRIVLEGED=""
BUILD_CACHE=""
CPUS="0.000"
QUIRKS=""
build_image() {
DOCKERFILE="$1"
if [ ! -f "${DIR_SCRIPT}/${DOCKERFILE}" ]; then
echo "${DIR_SCRIPT}/${DOCKERFILE} not found"
exit -1
fi
docker build ${BUILD_CACHE} -t "variscite:${DOCKER_IMAGE}" ${DIR_SCRIPT} -f ${DOCKERFILE}
}
array_contains () {
local seeking=$1; shift
local in=1
for element; do
if [[ $element == "$seeking" ]]; then
in=0
break
fi
done
return $in
}
help() {
echo
echo "Usage: ${DIR_SCRIPT}/${FILE_SCRIPT} <options>"
echo
echo " optional:"
echo " -u --ubuntu-version Ubuntu Version: ${UBUNTU_VERSIONS_SUPPORTED[@]}"
echo " -b --build Build Docker Image, includes only changes made to Dockerfile"
echo " -f --force-build Build Docker Image with --no-cache, will include latest from Ubuntu"
echo " -e --env Docker Environment File"
echo " -n --non-interactive Run container and exit without interactive shell"
echo " -w --workdir Docker Working Directory to Mount, default is ${WORKDIR}"
echo " -v --volume Docker Volumes to Mount, e.g. -v /opt/yocto_downloads_docker:/opt/yocto_downloads -v /opt/yocto_sstate_docker:/opt/yocto_sstate"
echo " -p --privledged Run docker in privledged mode, allowing access to all devices"
echo " --host-network Run container with host network mode"
echo " -c --cpus Limit the number of CPUs available to the container, default is ${CPUS}, which will use all available CPUs"
echo " -h --help Display this Help Message"
echo " --command Run a command inside the docker container, implies -n"
echo
echo "Example - Run Interactive Shell In Current Directory:"
echo "./run.sh"
echo
echo "Example - Run Interactive Shell In Another Directory:"
echo "./run.sh -w ~/var-fslc-yocto"
echo
echo "Example - Run Interactive Shell In Another Directory, mounting directories inside Docker container"
echo "./run.sh -w ~/var-fslc-yocto -v /opt/yocto_downloads_docker:/opt/yocto_downloads -v /opt/yocto_sstate_docker:/opt/yocto_sstate"
echo
exit
}
# Add a flag to determine whether to build the image
BUILD_IMAGE_FLAG=0
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
help
;;
--command)
COMMAND="$2"
INTERACTIVE=""
shift
shift
;;
-u|--ubuntu-version)
UBUNTU_VERSION="$2"
# Verify Ubuntu Version is Supported
array_contains "${UBUNTU_VERSION}" "${UBUNTU_VERSIONS_SUPPORTED[@]}" || ( echo "Error, Ubuntu '${UBUNTU_VERSION}' not supported, use one of: ${UBUNTU_VERSIONS_SUPPORTED[@]}"; exit 1);
shift
shift
;;
-b|--build)
# Set the flag to build the image later
BUILD_IMAGE_FLAG=1
shift
;;
-f|--force-build)
BUILD_CACHE="--no-cache"
shift
;;
-n|--non-interactive)
INTERACTIVE=""
shift
;;
-w|--workdir)
WORKDIR="$2"
if [ "$WORKDIR" = "" ]; then
help
fi
if [ ! -d $WORKDIR ]; then
echo "Error: ${WORKDIR} doesn't exist"
echo "Please verify path and run:"
echo "mkdir -p ${WORKDIR}"
exit -1
fi
shift # past argument
shift # past value
;;
-v|--volume)
NEW_VOL="$2"
if [ "$NEW_VOL" = "" ]; then
help
fi
DOCKER_VOLUMES="${DOCKER_VOLUMES} -v ${NEW_VOL}"
shift # past argument
shift # past value
;;
-e|--env)
ENV_FILE=$2
if [ ! -f "${ENV_FILE}" ]; then
echo "Error: ${ENV_FILE} Not Found"
echo "Error: ${DIR_SCRIPT}/env/${ENV_FILE} Not Found either"
help
fi
ENV_FILE="--env-file=${ENV_FILE}"
shift # past argument
shift # past value
;;
-p|--privledged)
PRIVLEGED=" --privileged"
shift
;;
--host-network)
DOCKER_HOST_NETWORK=" --network host"
shift
;;
-c|--cpus)
CPUS="$2"
if ! [[ "$CPUS" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
echo "Error: CPU limit must be a valid number."
exit 1
fi
shift
shift
;;
*) # unknown option
echo "Unknown option: $1"
help
;;
esac
done
}
set_quirks() {
# Get Host OS Information
if [ -f /etc/os-release ]; then
. /etc/os-release
fi
# If Host is Ubuntu 20.04 and Container is Ubuntu 22.04
if [ "$UBUNTU_VERSION" = "22.04" ] && [ "$VERSION_ID" = "20.04" ] && [ "$ID" = "ubuntu" ]; then
# https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1321924/j721excpxevm-yocto-build-always-gives-an-error-for-gdk-pixbuf_2-42-10-bb-do_compile
QUIRKS="${QUIRKS} --security-opt seccomp=unconfined"
fi
}
parse_args "$@"
readonly DOCKER_IMAGE="yocto-${UBUNTU_VERSION}-${GIT_COMMIT}"
readonly HOSTNAME=$( echo "$DOCKER_IMAGE" | sed 's/\./-/g')
# Verify qemu-user-static is installed
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
echo "Error: Please install qemu-user-static on host, required for debian"
exit -1
fi
# Build container if the image does not exist, the cache needs to be rebuilt, or the build flag is set
if ! docker images | grep -q "${DOCKER_IMAGE}" \
|| [ -n "$BUILD_CACHE" ] \
|| [ $BUILD_IMAGE_FLAG -eq 1 ]; then
build_image "Dockerfile_${UBUNTU_VERSION}"
fi
uid=$(id -u ${USER})
gid=$(id -g ${USER})
# .gitconfig is required by repo and git
if [ ! -f ${HOME}/.gitconfig ]; then
echo "Error: Please create ${HOME}/.gitconfig on your host computer:"
echo ' $ git config --global user.email "[email protected]"'
echo ' $ git config --global user.name "Your Name"'
exit -1
fi
set_quirks
docker run ${EXTRA_ARGS} --rm -e HOST_USER_ID=$uid -e HOST_USER_GID=$gid \
-v ~/.ssh:/home/vari/.ssh \
-v ${WORKDIR}:/workdir \
-v ~/.gitconfig:/tmp/host_gitconfig \
-v /opt:/opt \
-v /usr/src:/usr/src \
-v /lib/modules:/lib/modules \
-v /linux-kernel:/linux-kernel \
--hostname ${HOSTNAME} \
${DOCKER_VOLUMES} \
${INTERACTIVE} \
${ENV_FILE} \
${PRIVLEGED} \
${DOCKER_HOST_NETWORK} \
--cpus=${CPUS} \
${QUIRKS} \
variscite:${DOCKER_IMAGE} "$COMMAND"