Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Pre-requisites
The build system of Linux kernel for RISC-V requires ARCH environment variable to be set to riscv. Let's set it first. From the root working directory, run the following commands:
echo "export ARCH=riscv" >> tech.io-env.sh
source tech.io-env.sh
Build Linux Kernel
Let's download and build Linux kernel from source code. Linux kernel v6.2 will be used for this tutorial. Run the following from the root working directory:
# Download Linux kernel source code
git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
# Checkout appropriate version
cd linux
git checkout v6.2
Linux kernel will be built for RISC-V architecture. The default configurations for several targets can be obtained by the command below:
make help | grep defconfig --color=always
The default configutation, defconfig, is good enough so let's build it:
# Generate .config file
make defconfig
make -j$(nproc)
Did it Compile for RISC-V?
Search the .config file for the presence of RISC-V:
grep --color=always -ni 'riscv' .config
n= Mention line numberi= Ignore case'riscv'= Pattern to search for.config= Name of the file to search
Observe that the RISC-V configuration option is enabled:
CONFIG_RISCV=y
This happens because of the environment variable ARCH is set to riscv. It is used when the .config file was generated. The details can be found by opening the Makefile:
editor ./Makefile
and searching for ARCH and CROSS_COMPILE.
Output Files
List the output files:
ls arch/riscv/boot -lSh
l= List detailsS= Sort by file size in descending orderh= Print the sizes in easily readable format (using K/M/G for Kilo/Mega/Giga when appropriate)
The files Image and Image.gz are the build files. Image.gz is the compressed form of Image.