-
Notifications
You must be signed in to change notification settings - Fork 2k
/
recompile
executable file
·818 lines (704 loc) · 32.6 KB
/
recompile
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#!/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: Run on the x86_64 and aarch64 platforms (ubuntu/debian), Compile the kernel.
# 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
# mount_try : Mount the image file, fail again
#
# init_var : Initialize all variables
# toolchain_check : Check and install the cross-compilation toolchain
# query_version : Query the latest kernel version
# apply_patch : Apply custom kernel patches
# get_kernel_source : Get the kernel source code
#
# chroot_armbian : Chroot into Armbian to generate initrd.img, uInitrd and make scripts
# compile_env : Set up the compile kernel environment
# compile_dtbs : Compile the dtbs
# compile_kernel : Compile the kernel
# packit_dtbs : Packit dtbs files
# packit_kernel : Packit boot, modules and header files
# compile_selection : Choose to compile dtbs or all kernels
# clean_tmp : Clear temporary files
#
# loop_recompile : Loop to compile kernel
#
#=============================== Set make environment variables ===============================
#
# Related file storage path
current_path="${PWD}"
compile_path="${current_path}/compile-kernel"
kernel_path="${compile_path}/kernel"
config_path="${compile_path}/tools/config"
script_path="${compile_path}/tools/script"
kernel_patch_path="${compile_path}/tools/patch"
armbian_path="${compile_path}/tools/armbian"
armbian_file="${armbian_path}/armbian.img"
output_path="${compile_path}/output"
chroot_path="${output_path}/chroot"
chroot_file="${chroot_path}/chroot_armbian.img"
[[ -d "${kernel_path}" ]] || mkdir -p ${kernel_path}
[[ -d "${output_path}" ]] || mkdir -p ${output_path}
# Set the system file path to be used
arch_info="$(uname -m)"
host_release="$(cat /etc/os-release | grep '^VERSION_CODENAME=.*' | cut -d'=' -f2)"
# Set the default for downloading kernel sources from github.com
repo_owner="unifreq"
repo_branch="main"
build_kernel=("6.1.y" "6.6.y")
# Set whether to use the latest kernel, options: [ true / false ]
auto_kernel="true"
# Set whether to apply custom kernel patches, options: [ true / false ]
auto_patch="false"
# Set custom signature for the kernel
custom_name="-ophub"
# Set the kernel compile object, options: [ dtbs / all ]
package_list="all"
# Set the compression format, options: [ gzip / lzma / xz / zstd ]
compress_format="xz"
# Cross compile toolchain download mirror
dev_repo="https://github.com/ophub/kernel/releases/download/dev"
# Arm GNU Toolchain source: https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads
gun_file_x86_64="arm-gnu-toolchain-13.3.rel1-x86_64-aarch64-none-linux-gnu.tar.xz"
gun_file_aarch64="arm-gnu-toolchain-13.3.rel1-aarch64-aarch64-none-elf.tar.xz"
# Armenian file source: https://github.com/ophub/amlogic-s9xxx-armbian/releases
armbian_rootfs_file="armbian.tar.xz"
# Set the toolchain path
toolchain_path="/usr/local/toolchain"
# Set the default cross-compilation toolchain: [ clang / gcc / gcc-13.2, etc. ]
toolchain_name="gcc"
# QEMU BINARY
qemu_binary_arm64="qemu-aarch64-static"
# 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
}
mount_try() {
# Check mount parameters
m_dev="${1}"
m_target="${2}"
[[ -n "${m_dev}" && -n "${m_target}" ]] || {
error_msg "Mount parameter is missing: [ ${m_dev}, ${m_target} ]"
}
t="1"
max_try="10"
while [[ "${t}" -le "${max_try}" ]]; do
mount ${m_dev} ${m_target}
if [[ "${?}" -eq "0" ]]; then
break
else
sync && sleep 3
umount -f ${m_target} 2>/dev/null
t="$((t + 1))"
fi
done
[[ "${t}" -gt "${max_try}" ]] && error_msg "[ ${t} ] attempts to mount failed."
}
init_var() {
echo -e "${STEPS} Start Initializing Variables..."
# If it is followed by [ : ], it means that the option requires a parameter value
get_all_ver="$(getopt "k:a:n:m:p:r:t:c:" "${@}")"
while [[ -n "${1}" ]]; do
case "${1}" in
-k | --Kernel)
if [[ -n "${2}" ]]; then
oldIFS="${IFS}"
IFS="_"
build_kernel=(${2})
IFS="${oldIFS}"
shift
else
error_msg "Invalid -k parameter [ ${2} ]!"
fi
;;
-a | --AutoKernel)
if [[ -n "${2}" ]]; then
auto_kernel="${2}"
shift
else
error_msg "Invalid -a parameter [ ${2} ]!"
fi
;;
-n | --customName)
if [[ -n "${2}" ]]; then
custom_name="${2// /}"
[[ "${custom_name:0:1}" != "-" ]] && custom_name="-${custom_name}"
shift
else
error_msg "Invalid -n parameter [ ${2} ]!"
fi
;;
-m | --MakePackage)
if [[ -n "${2}" ]]; then
package_list="${2}"
shift
else
error_msg "Invalid -m parameter [ ${2} ]!"
fi
;;
-p | --AutoPatch)
if [[ -n "${2}" ]]; then
auto_patch="${2}"
shift
else
error_msg "Invalid -p parameter [ ${2} ]!"
fi
;;
-r | --Repository)
if [[ -n "${2}" ]]; then
repo_owner="${2}"
shift
else
error_msg "Invalid -r parameter [ ${2} ]!"
fi
;;
-t | --Toolchain)
if [[ -n "${2}" ]]; then
toolchain_name="${2}"
shift
else
error_msg "Invalid -t parameter [ ${2} ]!"
fi
;;
-c | --Compress)
if [[ -n "${2}" ]]; then
compress_format="${2}"
shift
else
error_msg "Invalid -c parameter [ ${2} ]!"
fi
;;
*)
error_msg "Invalid option [ ${1} ]!"
;;
esac
shift
done
# Receive the value entered by the [ -r ] parameter
input_r_value="${repo_owner//https\:\/\/github\.com\//}"
code_owner="$(echo "${input_r_value}" | awk -F '@' '{print $1}' | awk -F '/' '{print $1}')"
code_repo="$(echo "${input_r_value}" | awk -F '@' '{print $1}' | awk -F '/' '{print $2}')"
code_branch="$(echo "${input_r_value}" | awk -F '@' '{print $2}')"
#
[[ -n "${code_owner}" ]] || error_msg "The [ -r ] parameter is invalid."
[[ -n "${code_branch}" ]] || code_branch="${repo_branch}"
# Set the gcc version code
[[ "${toolchain_name}" =~ ^gcc-[0-9]+.[0-9]+ ]] && {
gcc_version_code="${toolchain_name#*-}"
gun_file_x86_64="arm-gnu-toolchain-${gcc_version_code}.rel1-x86_64-aarch64-none-linux-gnu.tar.xz"
gun_file_aarch64="arm-gnu-toolchain-${gcc_version_code}.rel1-aarch64-aarch64-none-elf.tar.xz"
}
# Set cross compilation parameters
export SRC_ARCH="arm64"
export LOCALVERSION="${custom_name}"
}
toolchain_check() {
cd ${current_path}
echo -e "${STEPS} Start checking the toolchain for compiling the kernel..."
# Install dependencies for jammy
apt-get -qq update
if [[ "${arch_info}" == "x86_64" ]]; then
apt-get -qq install -y $(cat compile-kernel/tools/script/ubuntu2204-build-armbian-depends)
else
apt-get -qq install -y $(cat compile-kernel/tools/script/armbian-compile-kernel-depends)
fi
# Download armbian
if [[ ! -f "${armbian_file}" ]]; then
echo -e "${INFO} Start downloading Armbian rootfs file [ ${armbian_rootfs_file} ]..."
rm -rf ${armbian_path} && mkdir -p ${armbian_path}
# Download Armbian rootfs file. If it fails, wait 1 minute and try again, try 10 times.
for i in {1..10}; do
curl -fsSL "${dev_repo}/${armbian_rootfs_file}" -o "${armbian_path}/${armbian_rootfs_file}"
[[ "${?}" -eq "0" ]] && break || sleep 60
done
[[ "${?}" -eq "0" ]] || error_msg "Armbian file download failed"
# Decompress the Armbian rootfs file
tar -xJf ${armbian_path}/${armbian_rootfs_file} -C ${armbian_path}
# Change other names to armbian.img
[[ -f "${armbian_file}" ]] || mv -f ${armbian_path}/*.img ${armbian_file}
# Delete download file
rm -f ${armbian_path}/${armbian_rootfs_file}
[[ -f "${armbian_file}" ]] || error_msg "There is no Armbian rootfs file: [ ${armbian_file} ]"
fi
# Set the default path
path_os_variable="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
# Download the cross-compilation toolchain: [ clang / gcc ]
[[ -d "/etc/apt/sources.list.d" ]] || mkdir -p /etc/apt/sources.list.d
if [[ "${toolchain_name}" == "clang" ]]; then
# Install LLVM
echo -e "${INFO} Start installing the LLVM toolchain..."
apt-get -qq install -y lsb-release software-properties-common gnupg
curl -fsSL https://apt.llvm.org/llvm.sh | bash -s all
[[ "${?}" -eq "0" ]] || error_msg "LLVM installation failed."
# Set cross compilation parameters
export PATH="${path_os_variable}"
export CROSS_COMPILE="aarch64-linux-gnu-"
export CC="clang"
export LD="ld.lld"
export MFLAGS=" LLVM=1 LLVM_IAS=1 "
else
# Choosing Arm GNU Toolchain based on the platform
if [[ "${arch_info}" == "x86_64" ]]; then
gun_file="${gun_file_x86_64}"
gun_bin="aarch64-none-linux-gnu-"
else
gun_file="${gun_file_aarch64}"
gun_bin="aarch64-none-elf-"
fi
# Download Arm GNU Toolchain
[[ -d "${toolchain_path}" ]] || mkdir -p ${toolchain_path}
if [[ ! -d "${toolchain_path}/${gun_file//.tar.xz/}/bin" ]]; then
echo -e "${INFO} Start downloading the ARM GNU toolchain [ ${gun_file} ]..."
# Download the ARM GNU toolchain file. If it fails, wait 1 minute and try again, try 10 times.
for i in {1..10}; do
curl -fsSL "${dev_repo}/${gun_file}" -o "${toolchain_path}/${gun_file}"
[[ "${?}" -eq "0" ]] && break || sleep 60
done
[[ "${?}" -eq "0" ]] || error_msg "GNU toolchain file download failed."
# Decompress the ARM GNU toolchain file
tar -xJf ${toolchain_path}/${gun_file} -C ${toolchain_path}
rm -f ${toolchain_path}/${gun_file}
# List and check directory names, and change them all to lowercase
for dir in $(ls ${toolchain_path}); do
if [[ -d "${toolchain_path}/${dir}" && "${dir}" != "${dir,,}" ]]; then
mv -f ${toolchain_path}/${dir} ${toolchain_path}/${dir,,}
fi
done
[[ -d "${toolchain_path}/${gun_file//.tar.xz/}/bin" ]] || error_msg "The gcc is not set!"
fi
# Add ${PATH} variable
path_gcc="${toolchain_path}/${gun_file//.tar.xz/}/bin:${path_os_variable}"
export PATH="${path_gcc}"
# Set cross compilation parameters
export CROSS_COMPILE="${toolchain_path}/${gun_file//.tar.xz/}/bin/${gun_bin}"
export CC="${CROSS_COMPILE}gcc"
export LD="${CROSS_COMPILE}ld.bfd"
export MFLAGS=""
fi
}
query_version() {
cd ${current_path}
echo -e "${STEPS} Start querying the latest kernel version..."
# Set empty array
tmp_arr_kernels=()
# Query the latest kernel in a loop
i=1
for KERNEL_VAR in "${build_kernel[@]}"; do
echo -e "${INFO} (${i}) Auto query the latest kernel version of the same series for [ ${KERNEL_VAR} ]"
# Identify the kernel mainline
MAIN_LINE="$(echo ${KERNEL_VAR} | awk -F '.' '{print $1"."$2}')"
if [[ -z "${code_repo}" ]]; then linux_repo="linux-${MAIN_LINE}.y"; else linux_repo="${code_repo}"; fi
github_kernel_repo="${code_owner}/${linux_repo}/${code_branch}"
github_kernel_ver="https://raw.githubusercontent.com/${github_kernel_repo}/Makefile"
# latest_version="125"
latest_version="$(curl -s ${github_kernel_ver} | grep -oE "SUBLEVEL =.*" | head -n 1 | grep -oE '[0-9]{1,3}')"
if [[ "${?}" -eq "0" && -n "${latest_version}" ]]; then
tmp_arr_kernels[${i}]="${MAIN_LINE}.${latest_version}"
else
error_msg "Failed to query the kernel version in [ github.com/${github_kernel_repo} ]"
fi
echo -e "${INFO} (${i}) [ ${tmp_arr_kernels[$i]} ] is github.com/${github_kernel_repo} latest kernel. \n"
let i++
done
# Reset the kernel array to the latest kernel version
build_kernel=(${tmp_arr_kernels[@]})
}
apply_patch() {
cd ${current_path}
echo -e "${STEPS} Start applying custom kernel patches..."
# Apply the common kernel patches
if [[ -d "${kernel_patch_path}/common-kernel-patches" ]]; then
echo -e "${INFO} Copy common kernel patches..."
cp -vf ${kernel_patch_path}/common-kernel-patches/*.patch -t ${kernel_path}/${local_kernel_path}
cd ${kernel_path}/${local_kernel_path}
for file in *.patch; do
echo -e "${INFO} Apply kernel patch file: [ ${file} ]"
patch -p1 <"${file}"
done
rm -f *.patch
else
echo -e "${INFO} No common kernel patches, skipping."
fi
# Apply the dedicated kernel patches
if [[ -d "${kernel_patch_path}/${local_kernel_path}" ]]; then
echo -e "${INFO} Copy [ ${local_kernel_path} ] version dedicated kernel patches..."
cp -vf ${kernel_patch_path}/${local_kernel_path}/*.patch -t ${kernel_path}/${local_kernel_path}
cd ${kernel_path}/${local_kernel_path}
for file in *.patch; do
echo -e "${INFO} Apply kernel patch file: [ ${file} ]"
patch -p1 <"${file}"
done
rm -f *.patch
else
echo -e "${INFO} No [ ${local_kernel_path} ] version dedicated kernel patches, skipping."
fi
}
get_kernel_source() {
cd ${current_path}
echo -e "${STEPS} Start downloading the kernel source code..."
[[ -d "${kernel_path}" ]] || mkdir -p ${kernel_path}
# Download the kernel source code from github.com
if [[ ! -d "${kernel_path}/${local_kernel_path}" ]]; then
echo -e "${INFO} Start cloning from [ https://github.com/${server_kernel_repo} -b ${code_branch} ]"
# Clone the latest kernel source code. If it fails, wait 1 minute and try again, try 10 times.
for i in {1..10}; do
git clone -q --single-branch --depth=1 --branch=${code_branch} https://github.com/${server_kernel_repo} ${kernel_path}/${local_kernel_path}
[[ "${?}" -eq "0" ]] && break || sleep 60
done
[[ "${?}" -eq "0" ]] || error_msg "[ https://github.com/${server_kernel_repo} ] Clone failed."
else
# Get a local kernel version
local_makefile="${kernel_path}/${local_kernel_path}/Makefile"
local_makefile_version="$(cat ${local_makefile} | grep -oE "VERSION =.*" | head -n 1 | grep -oE '[0-9]{1,3}')"
local_makefile_patchlevel="$(cat ${local_makefile} | grep -oE "PATCHLEVEL =.*" | head -n 1 | grep -oE '[0-9]{1,3}')"
local_makefile_sublevel="$(cat ${local_makefile} | grep -oE "SUBLEVEL =.*" | head -n 1 | grep -oE '[0-9]{1,3}')"
# Local version and server version comparison
if [[ "${auto_kernel}" == "true" || "${auto_kernel}" == "yes" ]] && [[ "${kernel_sub}" -gt "${local_makefile_sublevel}" ]]; then
# Pull the latest source code of the server
cd ${kernel_path}/${local_kernel_path}
git checkout ${code_branch} && git reset --hard origin/${code_branch} && git pull
kernel_version="${local_makefile_version}.${local_makefile_patchlevel}.${kernel_sub}"
echo -e "${INFO} Synchronize the upstream source code, compile the kernel version [ ${kernel_version} ]."
else
# Reset to local kernel version number
kernel_version="${local_makefile_version}.${local_makefile_patchlevel}.${local_makefile_sublevel}"
echo -e "${INFO} Use local source code, compile the kernel version [ ${kernel_version} ]."
fi
fi
# Remove the local version number
rm -f ${kernel_path}/${local_kernel_path}/localversion
# Apply custom kernel patches
[[ "${auto_patch}" == "true" || "${auto_patch}" == "yes" ]] && apply_patch
}
chroot_armbian() {
cd ${current_path}
echo -e "${STEPS} Create chroot..."
rm -f ${chroot_file}
cp -f ${armbian_file} ${chroot_file}
# Mount the armbian system
tag_rootfs="${chroot_path}/root"
loop_armbian="$(losetup -P -f --show "${chroot_file}")"
[[ -n "${loop_armbian}" ]] || error_msg "losetup ${chroot_file} failed."
mount_try ${loop_armbian}p2 ${tag_rootfs}
mount_try ${loop_armbian}p1 ${tag_rootfs}/boot
ARMBIAN_PLATFORM="$(cat ${tag_rootfs}/etc/ophub-release 2>/dev/null | grep -E "^PLATFORM=.*" | cut -d"'" -f2)"
echo -e "${INFO} Armbian PLATFORM: [ ${ARMBIAN_PLATFORM} ]"
echo -e "${INFO} Copy the kernel files to the armbian system..."
# Remove current system files for /boot
rm -f ${tag_rootfs}/boot/{config-*,initrd.img-*,System.map-*,vmlinuz-*,uInitrd*,*Image}
# Copy /boot related files into armbian system
cp -f ${kernel_path}/${local_kernel_path}/System.map ${tag_rootfs}/boot/System.map-${kernel_outname}
cp -f ${kernel_path}/${local_kernel_path}/.config ${tag_rootfs}/boot/config-${kernel_outname}
cp -f ${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/Image ${tag_rootfs}/boot/vmlinuz-${kernel_outname}
if [[ "${ARMBIAN_PLATFORM}" == "rockchip" || "${ARMBIAN_PLATFORM}" == "allwinner" ]]; then
cp -f ${tag_rootfs}/boot/vmlinuz-${kernel_outname} ${tag_rootfs}/boot/Image
else
cp -f ${tag_rootfs}/boot/vmlinuz-${kernel_outname} ${tag_rootfs}/boot/zImage
fi
#echo -e "${INFO} Kernel copy results in the [ ${tag_rootfs}/boot ] directory: \n$(ls -l ${tag_rootfs}/boot) \n"
# Remove current system files for /usr/lib/modules
rm -rf ${tag_rootfs}/usr/lib/modules/*
# Copy /usr/lib/modules related files into armbian system
cp -rf ${output_path}/modules/lib/modules/${kernel_outname} -t ${tag_rootfs}/usr/lib/modules
#echo -e "${INFO} Kernel copy results in the [ ${tag_rootfs}/usr/lib/modules ] directory: \n$(ls -l ${tag_rootfs}/usr/lib/modules) \n"
cd ${kernel_path}/${local_kernel_path}
echo -e "${INFO} Copy the kernel source tree to the armbian system..."
# Set the kernel source tree path
armbian_kernel_path="${tag_rootfs}/opt/linux-kernel"
mkdir -p ${armbian_kernel_path}
# Set the git configuration and copy the kernel source tree
git config --global --add safe.directory ${PWD}
git archive --format=tar ${code_branch} | tar xf - -C ${armbian_kernel_path}
cp -af include/config "${armbian_kernel_path}/include"
cp -af include/generated "${armbian_kernel_path}/include"
cp -af arch/${SRC_ARCH}/include/generated "${armbian_kernel_path}/arch/${SRC_ARCH}/include"
cp -af .config Module.symvers ${armbian_kernel_path}
[[ "${?}" -eq "0" ]] || error_msg "Copy the kernel source tree to the armbian system failed."
cd ${current_path}
# COMPRESS: [ gzip | lzma | xz | zstd ]
[[ "${kernel_outname}" =~ ^5.4.[0-9]+ ]] && compress_format="xz"
compress_initrd_file="${tag_rootfs}/etc/initramfs-tools/initramfs.conf"
if [[ -f "${compress_initrd_file}" ]]; then
sed -i "s|^COMPRESS=.*|COMPRESS=${compress_format}|g" ${compress_initrd_file}
compress_settings="$(cat ${compress_initrd_file} | grep -E ^COMPRESS=)"
echo -e "${INFO} Set the [ ${compress_settings} ] in the initramfs.conf file."
else
error_msg "The [ ${compress_initrd_file} ] file does not exist."
fi
echo -e "${INFO} Copy the [ ${qemu_binary_arm64} ] file to the armbian system..."
[[ -f "/usr/bin/${qemu_binary_arm64}" ]] && cp -f /usr/bin/${qemu_binary_arm64} ${tag_rootfs}/usr/bin/
#echo -e "${INFO} The [ ${qemu_binary_arm64} ] file copy results: \n$(ls -l ${tag_rootfs}/usr/bin/${qemu_binary_arm64}) \n"
echo -e "${INFO} Copy the [ ubuntu_chroot_armbian.sh ] script to the armbian system..."
cp -f ${script_path}/ubuntu_chroot_armbian.sh ${tag_rootfs}/root
chmod +x ${tag_rootfs}/root/ubuntu_chroot_armbian.sh
sync && sleep 3
#echo -e "${INFO} Kernel copy results in the [ ${tag_rootfs}/root ] directory: \n$(ls -l ${tag_rootfs}/root) \n"
# Enter the armbian system to generate /boot/uInitrd-${kernel_outname} file
echo -e "${STEPS} Enter the chroot Armbian system..."
mount -t proc /proc ${tag_rootfs}/proc
mount --bind /dev ${tag_rootfs}/dev
mount --bind /dev/pts ${tag_rootfs}/dev/pts
mount --bind /proc/$$/fd ${tag_rootfs}/dev/fd
chmod 0666 ${tag_rootfs}/dev/null
chroot ${tag_rootfs} /bin/bash -c "/root/ubuntu_chroot_armbian.sh ${kernel_outname}"
[[ "${?}" -eq "0" && -f "${tag_rootfs}/boot/uInitrd-${kernel_outname}" ]] || error_msg "Create chroot uInitrd-${kernel_outname} file failed."
cd ${current_path}
echo -e "${STEPS} Start copying files from Armbian system..."
# Copy the generated uInitrd file to the current system
echo -e "${INFO} Copy the boot files from armbian [ /boot ]"
cp -rf ${tag_rootfs}/boot/*${kernel_outname} ${output_path}/boot
# Copy the generated header files to the current system
echo -e "${INFO} Copy the header files from armbian [ /opt/header ]"
cp -f ${tag_rootfs}/opt/header/header-${kernel_outname}.tar.gz ${output_path}/${kernel_version}
# Unmount the armbian system
sync && sleep 3
umount ${tag_rootfs}/dev/fd 2>/dev/null
umount ${tag_rootfs}/dev/pts 2>/dev/null
umount ${tag_rootfs}/dev 2>/dev/null
umount ${tag_rootfs}/proc 2>/dev/null
umount ${tag_rootfs}/boot 2>/dev/null
umount ${tag_rootfs} 2>/dev/null
losetup -D 2>/dev/null
}
compile_env() {
cd ${current_path}
echo -e "${STEPS} Start checking local compilation environments..."
# Get kernel output name
kernel_outname="${kernel_version}${custom_name}"
echo -e "${INFO} Compile kernel output name: [ ${kernel_outname} ]"
# Create a temp directory
echo -e "${INFO} Create a temp directory: [ ${output_path} ]"
rm -rf ${output_path}/{chroot/,boot/,dtb/,modules/,header/,${kernel_version}/}
mkdir -p ${output_path}/{chroot/{root/boot/,},boot/,dtb/{allwinner/,amlogic/,rockchip/},modules/,header/,${kernel_version}/}
cd ${kernel_path}/${local_kernel_path}
echo -e "${STEPS} Set cross compilation parameters..."
# Show variable
echo -e "${INFO} ARCH: [ ${SRC_ARCH} ]"
echo -e "${INFO} LOCALVERSION: [ ${LOCALVERSION} ]"
echo -e "${INFO} CROSS_COMPILE: [ ${CROSS_COMPILE} ]"
echo -e "${INFO} CC: [ ${CC} ]"
echo -e "${INFO} LD: [ ${LD} ]"
# Set generic make string
MAKE_SET_STRING=" ARCH=${SRC_ARCH} CROSS_COMPILE=${CROSS_COMPILE} CC=${CC} LD=${LD} ${MFLAGS} LOCALVERSION=${LOCALVERSION} "
# Make clean/mrproper
make ${MAKE_SET_STRING} mrproper
# Check .config file
if [[ ! -s ".config" ]]; then
[[ -s "${config_path}/config-${kernel_verpatch}" ]] || error_msg "Missing [ config-${kernel_verpatch} ] template!"
echo -e "${INFO} Copy [ ${config_path}/config-${kernel_verpatch} ] to [ .config ]"
cp -f ${config_path}/config-${kernel_verpatch} .config
else
echo -e "${INFO} Use the .config file in the current directory."
fi
# Clear kernel signature
sed -i "s|CONFIG_LOCALVERSION=.*|CONFIG_LOCALVERSION=\"\"|" .config
# Enable/Disabled Linux Kernel Clang LTO
[[ "${toolchain_name}" == "clang" ]] && {
kernel_x="$(echo "${kernel_version}" | cut -d '.' -f1)"
kernel_y="$(echo "${kernel_version}" | cut -d '.' -f2)"
if [[ "${kernel_x}" -ge "6" ]] || [[ "${kernel_x}" -eq "5" && "${kernel_y}" -ge "12" ]]; then
scripts/config -e LTO_CLANG_THIN
else
scripts/config -d LTO_CLANG_THIN
fi
}
# Make menuconfig
#make ${MAKE_SET_STRING} menuconfig
# Set max process
PROCESS="$(cat /proc/cpuinfo | grep "processor" | wc -l)"
[[ -z "${PROCESS}" || "${PROCESS}" -lt "1" ]] && PROCESS="1" && echo "PROCESS: 1"
}
compile_dtbs() {
cd ${kernel_path}/${local_kernel_path}
# Make dtbs
echo -e "${STEPS} Start compilation dtbs [ ${local_kernel_path} ]..."
make ${MAKE_SET_STRING} dtbs -j${PROCESS}
[[ "${?}" -eq "0" ]] && echo -e "${SUCCESS} The dtbs is compiled successfully."
}
compile_kernel() {
cd ${kernel_path}/${local_kernel_path}
# Make kernel
echo -e "${STEPS} Start compilation kernel [ ${local_kernel_path} ]..."
make ${MAKE_SET_STRING} Image modules dtbs -j${PROCESS}
[[ "${?}" -eq "0" ]] && echo -e "${SUCCESS} The kernel is compiled successfully."
# Install modules
echo -e "${STEPS} Install modules..."
make ${MAKE_SET_STRING} INSTALL_MOD_PATH=${output_path}/modules modules_install
[[ "${?}" -eq "0" ]] && echo -e "${SUCCESS} The modules is installed successfully."
# Strip debug information
STRIP="${CROSS_COMPILE}strip"
find ${output_path}/modules -name "*.ko" -print0 | xargs -0 ${STRIP} --strip-debug 2>/dev/null
[[ "${?}" -eq "0" ]] && echo -e "${SUCCESS} The modules is stripped successfully."
# Chroot into Armbian to generate initrd.img, uInitrd and make scripts
chroot_armbian
}
packit_dtbs() {
# Pack 3 dtbs files
echo -e "${STEPS} Packing the [ ${kernel_outname} ] dtbs packages..."
cd ${output_path}/dtb/allwinner
cp -f ${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/allwinner/*.dtb . 2>/dev/null
[[ "${?}" -eq "0" ]] && {
[[ -d "${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/allwinner/overlay" ]] && {
mkdir -p overlay
cp -f ${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/allwinner/overlay/*.dtbo overlay/ 2>/dev/null
}
tar -czf dtb-allwinner-${kernel_outname}.tar.gz *
mv -f *.tar.gz ${output_path}/${kernel_version}
echo -e "${SUCCESS} The [ dtb-allwinner-${kernel_outname}.tar.gz ] file is packaged."
}
cd ${output_path}/dtb/amlogic
cp -f ${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/amlogic/*.dtb . 2>/dev/null
[[ "${?}" -eq "0" ]] && {
[[ -d "${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/amlogic/overlay" ]] && {
mkdir -p overlay
cp -f ${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/amlogic/overlay/*.dtbo overlay/ 2>/dev/null
}
tar -czf dtb-amlogic-${kernel_outname}.tar.gz *
mv -f *.tar.gz ${output_path}/${kernel_version}
echo -e "${SUCCESS} The [ dtb-amlogic-${kernel_outname}.tar.gz ] file is packaged."
}
cd ${output_path}/dtb/rockchip
cp -f ${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/rockchip/*.dtb . 2>/dev/null
[[ "${?}" -eq "0" ]] && {
[[ -d "${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/rockchip/overlay" ]] && {
mkdir -p overlay
cp -f ${kernel_path}/${local_kernel_path}/arch/${SRC_ARCH}/boot/dts/rockchip/overlay/*.dtbo overlay/ 2>/dev/null
}
tar -czf dtb-rockchip-${kernel_outname}.tar.gz *
mv -f *.tar.gz ${output_path}/${kernel_version}
echo -e "${SUCCESS} The [ dtb-rockchip-${kernel_outname}.tar.gz ] file is packaged."
}
}
packit_kernel() {
# Pack 3 kernel files
echo -e "${STEPS} Packing the [ ${kernel_outname} ] boot, modules and header packages..."
cd ${output_path}/boot
rm -rf dtb-*
chmod +x *
tar -czf boot-${kernel_outname}.tar.gz *
mv -f *.tar.gz ${output_path}/${kernel_version}
echo -e "${SUCCESS} The [ boot-${kernel_outname}.tar.gz ] file is packaged."
cd ${output_path}/modules/lib/modules
tar -czf modules-${kernel_outname}.tar.gz *
mv -f *.tar.gz ${output_path}/${kernel_version}
echo -e "${SUCCESS} The [ modules-${kernel_outname}.tar.gz ] file is packaged."
cd ${output_path}/${kernel_version}
[[ -n "$(ls header-${kernel_outname}.tar.gz 2>/dev/null)" ]] && {
echo -e "${SUCCESS} The [ header-${kernel_outname}.tar.gz ] file is packaged."
}
}
compile_selection() {
# Compile by selection
if [[ "${package_list}" == "dtbs" ]]; then
compile_dtbs
packit_dtbs
else
compile_kernel
packit_dtbs
packit_kernel
fi
# Add sha256sum integrity verification file
cd ${output_path}/${kernel_version}
sha256sum * >sha256sums
echo -e "${SUCCESS} The [ sha256sums ] file has been generated"
cd ${output_path}
tar -czf ${kernel_version}.tar.gz ${kernel_version}
echo -e "${INFO} Kernel series files are stored in [ ${output_path} ]."
}
clean_tmp() {
cd ${current_path}
echo -e "${STEPS} Clear the space and update-grub..."
sync && sleep 3
rm -rf ${output_path}/{chroot/,boot/,dtb/,modules/,header/,${kernel_version}/}
update-grub 2>/dev/null
echo -e "${SUCCESS} All processes have been completed."
}
loop_recompile() {
cd ${current_path}
j="1"
for k in "${build_kernel[@]}"; do
# kernel_version, such as [ 6.1.15 ]
kernel_version="${k}"
# kernel <VERSION> and <PATCHLEVEL>, such as [ 6.1 ]
kernel_verpatch="$(echo ${kernel_version} | awk -F '.' '{print $1"."$2}')"
# kernel <SUBLEVEL>, such as [ 15 ]
kernel_sub="$(echo ${kernel_version} | awk -F '.' '{print $3}')"
# The loop variable assignment
if [[ -z "${code_repo}" ]]; then
server_kernel_repo="${code_owner}/linux-${kernel_verpatch}.y"
local_kernel_path="linux-${kernel_verpatch}.y"
else
server_kernel_repo="${code_owner}/${code_repo}"
local_kernel_path="${code_repo}-${code_branch}"
fi
# Check disk space size
echo -ne "(${j}) Start compiling the kernel [\033[92m ${kernel_version} \033[0m]. "
now_remaining_space="$(df -Tk ${kernel_path} | tail -n1 | awk '{print $5}' | echo $(($(xargs) / 1024 / 1024)))"
if [[ "${now_remaining_space}" -le "15" ]]; then
echo -e "${WARNING} Remaining space is less than 15G, exit the compilation."
break
else
echo "Remaining space is ${now_remaining_space}G."
fi
# Execute the following functions in sequence
get_kernel_source
compile_env
compile_selection
clean_tmp
let j++
done
}
# Show welcome message
echo -e "${STEPS} Welcome to compile kernel! \n"
echo -e "${INFO} Server running on Ubuntu: [ Release: ${host_release} / Host: ${arch_info} ] \n"
# Check script permission, supports running on the x86_64 and aarch64 platforms (ubuntu/debian)
[[ "$(id -u)" == "0" ]] || error_msg "Please run this script as root: [ sudo ./${0} ]"
[[ "x86_64 aarch64" == *"${arch_info}"* ]] || error_msg "This script is only supported on the x86_64 and aarch64 platforms."
# Initialize variables
init_var "${@}"
# Check and install the toolchain
toolchain_check
# Query the latest kernel version
[[ "${auto_kernel}" == "true" || "${auto_kernel}" == "yes" ]] && query_version
# Show compile settings
echo -e "${INFO} Kernel compilation toolchain: [ ${toolchain_name} ]"
echo -e "${INFO} Kernel from: [ ${code_owner} ]"
echo -e "${INFO} Kernel patch: [ ${auto_patch} ]"
echo -e "${INFO} Kernel arch: [ ${SRC_ARCH} ]"
echo -e "${INFO} Kernel Package: [ ${package_list} ]"
echo -e "${INFO} kernel signature: [ ${custom_name} ]"
echo -e "${INFO} Latest kernel version: [ ${auto_kernel} ]"
echo -e "${INFO} kernel initrd compress: [ ${compress_format} ]"
echo -e "${INFO} Kernel List: [ $(echo ${build_kernel[@]} | xargs) ] \n"
# Show server start information
echo -e "${INFO} Server space usage before starting to compile: \n$(df -hT ${kernel_path}) \n"
# Loop to compile the kernel
loop_recompile
# Show server end information
echo -e "${STEPS} Server space usage after compilation: \n$(df -hT ${kernel_path}) \n"
echo -e "${SUCCESS} All process completed successfully."
# All process completed
wait