-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathyocto-build.sh
executable file
·265 lines (205 loc) · 6.93 KB
/
yocto-build.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash
# -*- mode: shell-script; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# Copyright (C) 2019 coldnew
# Authored-by: Yen-Chin, Lee <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# We should not have any trouble on running this script
# set -x
# SDIR store this script path
SDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# SNAME store the script name
SNAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
# check for directory architecture
YOCTODIR="${SDIR}"
IMAGE="coldnew/yocto-build"
CONTAINER="yocto-build"
DOCKER_ARGS=""
############################################################
#### Library for common usage functions
function INFO {
: << FUNCDOC
This function print the MSG with "INFO:" as prefix, and add newline after MSG
parameter 1: MSG -> message for info
FUNCDOC
echo -e "\x1b[92m\x1b[1mINFO\x1b[0m:"
echo -e "\e[92m\e[1mINFO\e[0m: ${1}\n"
}
function ERROR {
: << FUNCDOC
This function print the MSG with "ERROR:" as prefix, and add newline after MSG
parameter 1: MSG -> message for info
FUNCDOC
echo -e "\e[31m\e[1mERROR\e[0m: ${1}\n"
}
############################################################
function read_config {
: << FUNCDOC
This function source the ~/.yocto-build.sh to modify some variable of this script.
The variables supported are:
IMAGE
The docker image you want to use, default: coldnew/yocto-build
example:
IMAGE="coldnew/yocto-build"
CONTAINER
The docker container name you use, default: yocto-build
example:
CONTAINER="yocto-build"
DOCKER_ARGS
Extra docker args you want to pass to it when create container
example:
DOCKER_ARGS=' --volume="${HOME}:${HOME}" --volume="/tmp:/tmp" '
FUNCDOC
if [ -e "${HOME}/.yocto-build.sh" ]; then
INFO "Read config from: ${HOME}/.yocto-build.sh"
source "${HOME}/.yocto-build.sh"
if [ "$IMAGE" != "coldnew/yocto-build" ]; then
INFO "CONTAINER: $CONTAINER"
fi
if [ "$CONTAINER" != "yocto-build" ]; then
INFO "CONTAINER: $CONTAINER"
fi
if [ "$DOCKER_ARGS" != "" ]; then
INFO "DOCKER_ARGS: $DOCKER_ARGS"
fi
else
INFO "No config file ${HOME}/.yocto-build.sh to read!"
fi
}
function usage {
cat <<EOF
Usage: $0 <arguments>
Arguments:
-a, --attach : attach to current runing container
-s, --shell : spawn a new shell to current container
-w, --workdir : yocto workspace to shared with docker container
-r, --rm : remove current working container
-u, --upgrade : upgrade this script
-p, --pull : pull new docker container image
-h, --help : show this help info
Description:
The first time you run this script, you should specify yor
yocto project directory like following:
$0 --workdir /home/coldnew/poky
This script will help you to pull the docker image and mount
the /home/coldnew/poky to container's /yocto directory, and
you can build you yocto in this container.
If you want to attach current running shell, you can use:
$0 --attach
If you want to create a new shell, use:
$0 --shell
After all build done, you can remove current container by using:
$0 --rm
To upgrade this script, type:
$0 --upgrade
To pull the new docker container image, type:
$0 --pull
EOF
}
# NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have
# to install this separately
TEMP=`getopt -o uasw:rph --long upgrade,attach,shell,workdir:,rm,pull,help -- "$@"`
if [ $? != 0 ] ; then
usage
exit 1
fi
# if no argument
if [ -z "$1" ] ;then
usage
exit 1
fi
# parsing arguments
while true
do
case "$1" in
-u | --upgrade)
INFO "Upgrade script $NAME"
curl https://raw.githubusercontent.com/coldnew/docker-yocto/master/yocto-build.sh > /tmp/$SNAME
mv /tmp/$SNAME $SDIR/$SNAME
chmod +x $SDIR/$SNAME
exit $?
;;
-p | --pull)
INFO "Pull new image: $IMAGE"
docker pull $IMAGE
exit $?
;;
-h | --help)
usage; exit 0
;;
-r | --rm)
if docker inspect $CONTAINER > /dev/null 2>&1 ; then
INFO "Remove container: $CONTAINER"
docker rm $CONTAINER
else
INFO "container: $CONTAINER not exist, no need to remove"
fi
exit $?
;;
-s | --shell)
if docker inspect $CONTAINER > /dev/null 2>&1 ; then
INFO "Spawn /bin/bash for container: $CONTAINER"
docker exec -it $CONTAINER /entrypoint.sh
else
ERROR "container: $CONTAINER not exist, please use '$0 --workdir <dir to share>' first"
exit -1
fi
exit $?
;;
-a | --attach)
if docker inspect $CONTAINER > /dev/null 2>&1 ; then
INFO "Atttach to running container: $CONTAINER"
docker attach $CONTAINER
else
ERROR "container: $CONTAINER not exist, please use '$0 --workdir <dir to share>' first"
exit -1
fi
exit $?
;;
-w | --workdir)
# Try to start an existing/stopped container with thie give name $CONTAINER
# otherwise, run a new one.
YOCTODIR=$(readlink -m "$2")
if docker inspect $CONTAINER > /dev/null 2>&1 ; then
INFO "Reattaching to running container $CONTAINER"
docker start -i ${CONTAINER}
else
INFO "Creating container $CONTAINER"
USER=$(whoami)
read_config
docker run -it \
--volume="$YOCTODIR:/yocto" \
--volume="${HOME}/.ssh:/home/${USER}/.ssh" \
--volume="${HOME}/.gitconfig:/home/${USER}/.gitconfig" \
--volume="/etc/localtime:/etc/localtime:ro" \
--env="DISPLAY" \
--env="QT_X11_NO_MITSHM=1" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
--env=HOST_UID=$(id -u) \
--env=HOST_GID=$(id -g) \
--env=USER=${USER} \
$(eval echo ${DOCKER_ARGS}) \
--name=$CONTAINER \
$IMAGE
fi
exit $?
;;
*)
usage
exit $?
;;
esac
done
# bye
exit $?