Home | Projects | Notes > Linux Kernel Analysis > Environment Setup
Create a GitHub repository to which you want to import the Linux kernel source. (e.g., https://github.com/kyungjae-lee/linux-kernel-v6.4.2.git)
Clone the Linux kernel source repository to your local machine.
xxxxxxxxxx
11git clone --depth=1 --branch v6.4.2 <source_repository_url> <local_directory>
The last argument specifies the name of the local directory into which the Linux kernel source will be cloned.
Move into the local directory
xxxxxxxxxx
11cd <local_directory>
Remove the .git
directory.
xxxxxxxxxx
11rm -rf .git/
Restructure your local directory according to your needs. (I've moved the entire source tree into a newly created subdirectory named linux
.)
Convert the local directory to a Git repository.
xxxxxxxxxx
11git init
Create a new remote called origin
located at your GitHub repository.
xxxxxxxxxx
11git remote add origin <your_repository_url>
Once you do this, in your push commands, you can push to
origin
instead of typing out the whole URL.
Add the change in the working directory to the staging area.
xxxxxxxxxx
11git add .
Perform the initial commit.
xxxxxxxxxx
11git commit -m "Initial commit"
Push your local changes to your online repository.
xxxxxxxxxx
11git push -u origin main
Install the tools necessary for kernel compilation/build.
xxxxxxxxxx
51sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu \
2 cscope libssl-dev libncurses-dev\
3 autoconf automake autotools-dev curl libmpc-dev libmpfr-dev \
4 libgmp-dev gawk build-essential bison flex texinfo \
5 gperf libtool patchutils bc zlib1g-dev libexpat-dev
Please see https://www.kernel.org/doc/html/latest/process/changes.html for more details.
Create a shell script in the root path of the local directory.
xxxxxxxxxx
361
2
3# Shell script is to configure the build environment and initiate the process of building
4# the Linux kernel for the ARM64 architecture using the "aarch64-linux-gnu-" cross-compiler
5
6# Get the path of the directory containing the script (current working directory)
7ROOT_DIR="$( cd "$(dirname "$0")" ; pwd -P )"
8
9# Create the "out" directory to store build output
10OUT_DIR="$ROOT_DIR/out"
11echo "$OUT_DIR"
12
13# Define variables for the kernel name and the build log file path
14BUILD_LOG="$ROOT_DIR/build_log.txt"
15
16# Move into the "linux" directory where the kernel source is located
17cd linux
18
19# Clean up any leftover files from previous kernel builds in your source directory
20make mrproper
21
22# Create a default configuration for building the Linux kernel targeting the "ARM64" architecture
23# using the "aarch64-linux-gnu-" cross-compiler
24make O=$OUT_DIR ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig
25
26# Do additional configurations (Optional; Uncomment the follwoing command if necessary)
27# This command opens an interactive menu that allows you to customize the configuration settings
28# for building the Linux kernel targeting the "ARM64" architecture using the specified
29# cross-compiler (`aarch64-linux-gnu-`).
30#make O=$OUTPUT ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
31
32# Start the kernel build process for the "ARM64" architecture using the specified cross-compiler,
33# utilizing multiple cores (parallel compilation) to speed up the build process.
34# 2>&1 | tee $BUILD_LOG: Redirect both standard output and standard error to the $BUILD_LOG file,
35# while also printing them to the terminal.
36make O=$OUT_DIR ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc) 2>&1 | tee $BUILD_LOG
Give the shell script execute permission.
xxxxxxxxxx
11sudo chmod +x build_kernel_aarch64.sh
Run the shell script.
xxxxxxxxxx
11sudo ./build_kernel_aarch64.sh
When the build is complete, vmlinux
will be created in out/
, and Image
will be generated in out/arch/arm64/boot/
.