forked from jandelgado/lede-dockerbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.sh
executable file
·128 lines (107 loc) · 3.39 KB
/
builder.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
#!/bin/bash
# A Docker based LEDE image builder.
# (c) Jan Delgado 02-2017
set -e
# base Tag to use for docker imag
IMAGE_TAG=lede-imagebuilder
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# may be overridden in the config file
OUTPUT_DIR=$SCRIPT_DIR/output
ROOTFS_OVERLAY=$SCRIPT_DIR/rootfs-overlay
function usage_and_exit {
cat<<EOT
Dockerized LEDE/OpenWRT image builder.
Usage: $1 COMMAND CONFIGFILE [OPTIONS]
COMMAND is one of:
build-docker-image- just build the docker image
build - build docker image, then start container and build the LEDE image
shell - start shell in docker container
CONFIGFILE - configuraton file to use
OPTIONS:
-o OUTPUT_DIR - output directory (default $OUTPUT_DIR)
-f ROOTFS_OVERLAY - rootfs-overlay directory (default $ROOTFS_OVERLAY)
--skip-sudo - call docker directly, without sudo
command line options -o, -f, -r override config file settings.
Example:
./builder.sh build example.cfg -o output -f myrootfs
EOT
exit 0
}
# build container and pass in the actual builder to use
function build_docker_image {
echo "building container $IMAGE_TAG ..."
$SUDO docker build --build-arg BUILDER_URL="$LEDE_BUILDER_URL" \
-t $IMAGE_TAG docker
}
function run_cmd_in_container {
$SUDO docker run \
--rm \
-e GOSU_USER=`id -u`:`id -g` \
-v $(cd $ROOTFS_OVERLAY; pwd):/lede/rootfs-overlay:z \
-v $(cd $OUTPUT_DIR; pwd):/lede/output:z \
-ti --rm $IMAGE_TAG "$@"
}
# run the builder in the container.
function build_lede_image {
mkdir -p $OUTPUT_DIR
echo "building image for $LEDE_PROFILE ..."
run_cmd_in_container make image PROFILE="$LEDE_PROFILE" \
PACKAGES="$LEDE_PACKAGES" \
FILES="/lede/rootfs-overlay" \
BIN_DIR="/lede/output"
}
# run a shell in the container, useful for debugging.
function run_shell {
run_cmd_in_container bash
}
# print message and exit
function fail {
echo "ERROR: $*" >&2
exit 1
}
if [ $# -lt 2 ]; then
usage_and_exit $0
fi
COMMAND=$1; shift
CONFIG_FILE=$1; shift
SUDO=sudo
# pull in config file, making $BASEDIR_CONFIG_FILE available inside`
[ ! -f "$CONFIG_FILE" ] && fail "can not open $CONFIG_FILE"
BASEDIR_CONFIG_FILE=$( cd "$( dirname "$CONFIG_FILE" )" && pwd )
eval "$(cat "$CONFIG_FILE")"
# parse cli args, can override config file params
while [[ $# -ge 1 ]]; do
key="$1"
case $key in
-f) ROOTFS_OVERLAY="$2"; shift ;;
-o) OUTPUT_DIR="$2"; shift ;;
--skip-sudo) SUDO="" ;;
*) fail "invalid option: $key";;
esac
shift
done
[ ! -d "$OUTPUT_DIR" ] && fail "output-dir: no such directory $OUTPUT_DIR"
[ ! -d "$ROOTFS_OVERLAY" ] && fail "rootfs-overlay: no such directory $ROOTFS_OVERLAY"
IMAGE_TAG=$IMAGE_TAG:$LEDE_RELEASE-$LEDE_TARGET-$LEDE_SUBTARGET
cat<<EOT
--- configuration ------------------------------
LEDE_RELEASE......: $LEDE_RELEASE
LEDE_TARGET.......: $LEDE_TARGET
LEDE_SUBTARGET....: $LEDE_SUBTARGET
LEDE_PROFILE......: $LEDE_PROFILE
LEDE_BUILDER_URL..: $LEDE_BUILDER_URL
DOCKER_IMAGE_TAG..: $IMAGE_TAG
OUTPUT_DIR........: $OUTPUT_DIR
ROOTFS_OVERLAY....: $ROOTFS_OVERLAY
------------------------------------------------
EOT
case $COMMAND in
build)
build_docker_image
build_lede_image ;;
build-docker-image)
build_docker_image ;;
shell)
run_shell ;;
*) usage_and_exit $0
esac