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.
xxxxxxxxxx11git 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
xxxxxxxxxx11cd <local_directory>Remove the .git directory.
xxxxxxxxxx11rm -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.
xxxxxxxxxx11git initCreate a new remote called origin located at your GitHub repository.
xxxxxxxxxx11git remote add origin <your_repository_url>Once you do this, in your push commands, you can push to
origininstead of typing out the whole URL.
Add the change in the working directory to the staging area.
xxxxxxxxxx11git add .Perform the initial commit.
xxxxxxxxxx11git commit -m "Initial commit"Push your local changes to your online repository.
xxxxxxxxxx11git push -u origin main
Install the tools necessary for kernel compilation/build.
xxxxxxxxxx51sudo 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-devPlease 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.
xxxxxxxxxx3612
3# Shell script is to configure the build environment and initiate the process of building4# the Linux kernel for the ARM64 architecture using the "aarch64-linux-gnu-" cross-compiler5
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 output10OUT_DIR="$ROOT_DIR/out"11echo "$OUT_DIR"12
13# Define variables for the kernel name and the build log file path14BUILD_LOG="$ROOT_DIR/build_log.txt"15
16# Move into the "linux" directory where the kernel source is located17cd linux18
19# Clean up any leftover files from previous kernel builds in your source directory20make mrproper21
22# Create a default configuration for building the Linux kernel targeting the "ARM64" architecture23# using the "aarch64-linux-gnu-" cross-compiler24make O=$OUT_DIR ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig25
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 settings28# 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- menuconfig31
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_LOGGive the shell script execute permission.
xxxxxxxxxx11sudo chmod +x build_kernel_aarch64.shRun the shell script.
xxxxxxxxxx11sudo ./build_kernel_aarch64.shWhen the build is complete, vmlinux will be created in out/, and Image will be generated in out/arch/arm64/boot/.