-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathrecompile
More file actions
executable file
·182 lines (158 loc) · 6.81 KB
/
recompile
File metadata and controls
executable file
·182 lines (158 loc) · 6.81 KB
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
#!/bin/bash
#==============================================================================================
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
#
# This file is a part of the Rebuild Armbian
# https://github.com/ophub/amlogic-s9xxx-armbian
#
# Description: Compile the kernel with Armbian Docker container
# Copyright (C) 2021~ https://www.kernel.org
# Copyright (C) 2021~ https://github.com/unifreq
# Copyright (C) 2021~ https://github.com/ophub/amlogic-s9xxx-armbian
#
# Command: sudo ./recompile
# Command optional parameters please refer to the source code repository
#
#======================================= Functions list =======================================
#
# error_msg : Output error message
#
# init_var : Initialize all variables
# install_docker_engine : Install Docker engine
# launch_docker_container : Launch the Armbian Docker container
# configure_and_compile : Configure the Docker environment and compile the kernel
#
#=============================== Set make environment variables ===============================
#
# Set config and patch paths
current_path="${PWD}"
config_path="${current_path}/compile-kernel/tools/config"
kernel_patch_path="${current_path}/compile-kernel/tools/patch"
# Set Docker host mount directory and image
docker_hostpath="${PWD}"
docker_image="ophub/armbian-trixie:arm64"
docker_container="armbian-ophub"
docker_script="/usr/sbin/armbian-kernel"
# Get host architecture and release info
arch_info="$(uname -m)"
host_id="$(cat /etc/os-release 2>/dev/null | grep '^ID=.*' | cut -d'=' -f2)"
host_release="$(cat /etc/os-release 2>/dev/null | grep '^VERSION_CODENAME=.*' | cut -d'=' -f2)"
# Set font color
STEPS="[\033[95m STEPS \033[0m]"
INFO="[\033[94m INFO \033[0m]"
SUCCESS="[\033[92m SUCCESS \033[0m]"
WARNING="[\033[93m WARNING \033[0m]"
ERROR="[\033[91m ERROR \033[0m]"
#
#==============================================================================================
error_msg() {
echo -e "${ERROR} ${1}"
exit 1
}
init_var() {
echo -e "${STEPS} Start Initializing Variables..."
# If it is followed by [ : ], it means that the option requires a parameter value
local options="k:a:n:m:p:r:t:c:d:s:z:l:f:h:i:"
parsed_args=$(getopt -o "${options}" -- "${@}")
[[ ${?} -ne 0 ]] && error_msg "Parameter parsing failed."
eval set -- "${parsed_args}"
while true; do
case "${1}" in
-h | --DockerHostpath)
docker_hostpath="${2}"
shift 2
;;
-i | --DockerImage)
docker_image="${2}"
shift 2
;;
# Ignore parameters used by the Armbian Docker container
-k | -a | -n | -m | -p | -r | -t | -c | -d | -s | -z | -l | -f)
shift 2
;;
--)
shift
break
;;
*)
[[ -n "${1}" ]] && error_msg "Invalid option [ ${1} ]!"
break
;;
esac
done
}
install_docker_engine() {
echo -e "${STEPS} Start installing Docker engine..."
curl -fsSL https://get.docker.com | sh
usermod -aG docker ${USER}
newgrp docker
}
launch_docker_container() {
echo -e "${STEPS} Start launching the Armbian Docker container..."
# Check if the Armbian Docker container is running
if [[ "$(docker ps -q -f name=${docker_container})" ]]; then
echo -e "${INFO} The docker container [ ${docker_container} ] is running."
else
# Remove the stopped Armbian Docker container
[[ "$(docker ps -aq -f status=exited -f name=${docker_container})" ]] && {
echo -e "${INFO} Start removing the stopped Docker container [ ${docker_container} ]..."
docker rm -f ${docker_container} >/dev/null 2>&1
}
# Enable QEMU emulation for multi-architecture builds
echo -e "${INFO} Enable QEMU emulation for multi-architecture builds..."
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes 2>/dev/null || true
# Set Docker mount paths
echo -e "${INFO} Setting up Docker host mount paths..."
compile_path="${docker_hostpath}/compile-kernel"
ccache_path="${docker_hostpath}/ccache"
[[ -d "${compile_path}" ]] || mkdir -p ${compile_path}
[[ -d "${ccache_path}" ]] || mkdir -p ${ccache_path}
# Pull the Armbian Docker image
echo -e "${INFO} Start pulling the Docker image: [ ${docker_image} ]..."
docker run -d --privileged \
--name ${docker_container} \
-v ${compile_path}:/opt/kernel/compile-kernel \
-v ${ccache_path}:/root/.ccache \
-v /etc/localtime:/etc/localtime:ro \
-v /etc/timezone:/etc/timezone:ro \
-e CCACHE_DIR=/root/.ccache \
--restart=always \
${docker_image}
fi
}
configure_and_compile() {
echo -e "${STEPS} Start configuring the Docker environment and compiling the kernel..."
echo -e "${INFO} Synchronize server time."
ntpdate ntp.ubuntu.com 0.pool.ntp.org || true
echo -e "${INFO} Restarting Docker container..."
docker restart ${docker_container} >/dev/null && sleep 5
echo -e "${INFO} Syncing scripts in Docker container..."
docker exec -i "${docker_container}" bash "${docker_script}" -u
echo -e "${INFO} Injecting custom configs into Docker container..."
[[ -d "${config_path}" ]] && docker cp ${config_path}/. "${docker_container}":/opt/kernel/compile-kernel/tools/config/
docker exec -i "${docker_container}" ls -Alh /opt/kernel/compile-kernel/tools/config/ || true
echo -e "${INFO} Injecting custom patches into Docker container..."
[[ -d "${kernel_patch_path}" ]] && docker cp ${kernel_patch_path}/. "${docker_container}":/opt/kernel/compile-kernel/tools/patch/
docker exec -i "${docker_container}" ls -Alh /opt/kernel/compile-kernel/tools/patch/ || true
echo -e "${INFO} Start compiling the kernel inside the Docker container..."
docker exec -i "${docker_container}" bash "${docker_script}" "${@}"
}
# Show welcome message
echo -e "${STEPS} Welcome to compile kernel"
echo -e "${INFO} The host environment [ ${host_id}: ${host_release} / ${arch_info} ]"
# Check script permission
[[ "$(id -u)" == "0" ]] || error_msg "Please run this script as root: [ sudo ./${0} ]"
# Initialize variables
init_var "${@}"
# Install Docker engine if it is not installed
[[ -x "$(command -v docker)" ]] || install_docker_engine
# Launch the Armbian Docker container
launch_docker_container
# Configure the Docker environment and compile the kernel
configure_and_compile "${@}"
# Show server end information
echo -e "${STEPS} Host space usage after compilation: \n$(df -hT ${docker_hostpath}) \n"
echo -e "${SUCCESS} All processes completed successfully"