-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild_kernel.sh
executable file
·183 lines (147 loc) · 5.13 KB
/
build_kernel.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
#!/usr/bin/env bash
# Build new kernel from current kernel config
# tuan t. pham
# Default values
BUILD_OUTPUT=${BUILD_OUTPUT:-/tmp/build}
ARTIFACT=${ARTIFACT:-~/Downloads/kernel}
OLD_CONFIG=${OLD_CONFIG:-"/boot/config-$(uname -r)"}
KERNEL_SRC=${KERNEL_SRC:-"$(pwd)"}
CPUS=${CPUS:-$(nproc)}
DEBUG_INFO=${DEBUG_INFO:-"n"}
FRAME_POINTER=${FRAME_POINTER:-"n"}
UNWINDER_FRAME_POINTER=${UNWINDER_FRAME_POINTER:-"n"}
KGDB=${KGDB:-"n"}
KGDB_SERIAL_CONSOLE=${KGDB_SERIAL_CONSOLE:-"n"}
SYSTEM_TRUSTED_KEYS=${SYSTEM_TRUSTED_KEYS:-""}
# Colors
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
END_COLOR='\033[0m'
# Help message
HELP_MSG="\n${YELLOW}Usage:${END_COLOR} $0
Build new kernel from the current config file
${YELLOW}Required Debian/Ubuntu packages:${END_COLOR}
* build-essential yacc bison libssl-dev bc
-d: Dump environment variables
-h|--help: This help messages
${YELLOW}Default Environment Variables:${END_COLOR}
${YELLOW}BUILD_OUTPUT${END_COLOR}=${BUILD_OUTPUT}
Output directory for object files. One level up is where
the .deb files are created.
${YELLOW}ARTIFACT${END_COLOR}=$ARTIFACT
Directory where the .deb files are copied to.
${YELLOW}OLD_CONFIG${END_COLOR}=$OLD_CONFIG
Old config file.
${YELLOW}KERNEL_SRC${END_COLOR}=$KERNEL_SRC
Directory of linux kernel source code.
${YELLOW}CPUS${END_COLOR}=${CPUS}
Number of CPU cores to be used.
${YELLOW}Examples:${END_COLOR}
0) Use the default settings
$ $0
1) Set KERNEL_SRC
$ KERNEL_SRC=/opt/src/linux $0
${GREEN}Author:${END_COLOR} tuan t. pham"
# Function to print help message
print_help() {
echo -e "$HELP_MSG"
}
# Function to dump environment variables
function dump_vars() {
declare -a VAR_LIST=("BUILD_OUTPUT" "ARTIFACT" "OLD_CONFIG" "KERNEL_SRC" "CPUS" "KERNEL_VERSION" "OUTPUT_DIR")
for var in "${VAR_LIST[@]}"; do
printf "%s=%s\n" "$var" "${!var}"
done
}
# Function to pause execution
function pause() {
read -r -p "$*" response
response=${response#"${response%%[![:space:]]*}"}
response=${response%"${response##*[![:space:]]}"}
}
# Function to set custom environment
function set_custom_env() {
config_opts=(
"DEBUG_INFO=n"
"SYSTEM_TRUSTED_KEYS="
"KGDB=n"
"KGDB_SERIAL_CONSOLE=n"
"FRAME_POINTER=n"
"UNWINDER_FRAME_POINTER=n"
)
for opt in "${config_opts[@]}"; do
key=$(cut -d '=' -f 1 <<< "$opt")
value=${opt#*=}
if [[ $value == "n" ]]; then
scripts/config --file "$BUILD_OUTPUT/.config" --disable "$key"
elif [[ $value == "y" ]]; then
scripts/config --file "$BUILD_OUTPUT/.config" --enable "$key"
elif [[ -n $value ]]; then
scripts/config --file "$BUILD_OUTPUT/.config" --set-str "$key" "$value"
fi
done
}
# Function to set up environment
function setup_env() {
echo -e "${YELLOW}Setting the environment${END_COLOR}"
[ -d "${BUILD_OUTPUT}" ] || mkdir -p ${BUILD_OUTPUT}
[ -d "$ARTIFACT" ] || mkdir -p $ARTIFACT
export KBUILD_OUTPUT=${BUILD_OUTPUT}
cp ${OLD_CONFIG} ${BUILD_OUTPUT}/.config
cd ${KERNEL_SRC} || exit 1
echo -e "${YELLOW}Creating the new config file based on ${OLD_CONFIG}${END_COLOR}"
make -s olddefconfig
set_custom_env
KERNEL_VERSION=$(make -s kernelversion)
OUTPUT_DIR=${ARTIFACT}/${KERNEL_VERSION}
mkdir -p ${OUTPUT_DIR}
}
# Function to build kernel
build_kernel() {
echo -e "${YELLOW}Building kernel version ${KERNEL_VERSION}...${END_COLOR}"
# Build the new kernel
make -s -j"${CPUS}"
# Build binary debian packages
make -s -j"${CPUS}" bindeb-pkg
echo -e "${GREEN}Kernel build complete!${END_COLOR}"
}
# Function to copy artifacts
copy_artifacts() {
echo -e "${YELLOW}Copying artifacts...${END_COLOR}"
# Copy .deb and .changes files to OUTPUT_DIR
cp ${BUILD_OUTPUT}/../linux-*${KERNEL_VERSION}*.changes "${OUTPUT_DIR}"
cp ${BUILD_OUTPUT}/../linux-*${KERNEL_VERSION}*.buildinfo "${OUTPUT_DIR}"
cp ${BUILD_OUTPUT}/../linux-{headers,image}-${KERNEL_VERSION}*.deb "${OUTPUT_DIR}"
cp ${BUILD_OUTPUT}/../linux-libc-dev_${KERNEL_VERSION}*.deb "${OUTPUT_DIR}"
cp ${BUILD_OUTPUT}/.config "${OUTPUT_DIR}/config-${KERNEL_VERSION}"
echo -e "${GREEN}Artifacts copied!${END_COLOR}"
}
# Main function
main() {
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
print_help
exit 0
fi
if [ "$1" = "-d" ]; then
dump_vars
exit 0
fi
# Set up environment
setup_env || {
echo -e "${RED}Error: Failed to set up environment${END_COLOR}"
exit 1
}
# Build kernel
build_kernel || {
echo -e "${RED}Error: Failed to build kernel${END_COLOR}"
exit 1
}
# Copy artifacts
copy_artifacts || {
echo -e "${RED}Error: Failed to copy artifacts${END_COLOR}"
exit 1
}
echo -e "${GREEN}Kernel build and copy complete!${END_COLOR}"
}
main "$@"