Introduction#
For those who are involved in Linux system development, compiling a Linux kernel should be a familiar task. However, for beginners like me, it is still quite challenging. This article shares my experience and the steps on how to compile the Linux kernel version 6.3.0+.
Why I Compile the Linux Kernel#
I am participating in the Secure System Lab in NTU CSIE, and I am interested in Rust for Linux kind of stuff. To have a basic knowledge of how Rust for Linux works, I refer to our lab’s previous project (it’s a private repository, so I won’t link it here). Thus, I have to compile the Linux kernel with Rust support, along with the driver module developed by our lab.
Environment Setup#
Since I am working on a Fedora 42 workstation, I used virt manager to create a virtual machine. As the project specified, I first tried to install Ubuntu 20.04 LTS as the host machine; however, I encountered a lot of issues during the compilation process.
Disk Space#
Note that the Linux kernel 6.3.0+ with Rust support requires about 29GB of disk space. Thus, I recommended allocating at least 64GB for disk space to avoid running out of space, that would be awful.
Package loss#
Some packages are missing, or the versions are too old. As an example, the clang version in Ubuntu 20.04 is 12 (installed via apt), but the least version required for the kernel version 6.3.0+ is 15. I have to install that specific version with apt install clang-15.
Non-LTS version#
At the beginning, I followed the project instructions to use Ubuntu 23.04 as the VM inside my Ubuntu 20.04. However, I found that there was almost no mirror remain for Ubuntu 23.04. After searching, I finally found it from old-releases.ubuntu.com. But still, the script provided by the project was not working because the script was still missing from my Ubuntu 20.04 host. Finally, since we are not using any specific features from Ubuntu 23.04, I decided to switch to Ubuntu 22.04 LTS.
Switch to Ubuntu 24.04#
After several trials and errors (even with Gemini 2.5 pro), I finally used Ubuntu 24.04 VM as the host, and Ubuntu 22.04 VM as the guest. Even though these modifications were not directly gave me a successful compilation, at least it finally worked.
Dependencies Installation#
Before compiling the kernel, we need to install some libraries and tools. The following are some common dependencies.
apt-get update
apt-get upgrade -y
# Build tools and libraries
apt-get install -y \
build-essential \
git \
curl \
wget \
make \
gcc \
flex \
bison \
libncurses-dev \
libelf-dev \
libslang2-dev \
cpio \
bc \
dwarves \
pkg-config
apt-get install -y libssl-dev
# Build with LLVM
apt-get install -y \
clang-15 \
llvm-15 \
lld-15 \
libclang-15-dev
# Run VM
apt-get install -y \
qemu-kvm \
libvirt-daemon-system \
libvirt-clients \
bridge-utils \
gdb \
openssh-server \
lvm2 \
mdadm
There are also some Rust-related dependencies.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
rustup override set 1.66.0
The Linux Repository#
Next, we need to clone the Linux kernel repository. I got it as a submodule from our lab’s repository. If you are using git submodule, the following can help you.
git submodule update --init --recursive
It took me about two hours due to the network flow restriction in our dormitory. Then you can change to the Linux kernel directory.
cd linux
cp ../.config ./
make LLVM=1 rustavailable
cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen-cli
The .config file can be generated by make menuconfig or copied from elsewhere. The rustavailable target checks if the Rust toolchain is properly set up. In this case, we need the 1.66.0 version. The bindgen is a tool that generates Rust FFI (Foreign Function Interface) bindings with C libraries in the kernel.
Config for Rust#
To enable Rust support in the kernel, you can use make menuconfig to open the configuration menu. Inside the menu, navigate to General setup -> Rust support and press Y to enable it. You can find this option only when the Rust toolchain is correctly set up.
Compile the Kernel#
Now we can finally compile the kernel with make.
make LLVM=-15 -j8
The LLVM=-15 flag tells the make to use LLVM version 15. In fact, if your default LLVM version is 15, you can also use LLVM=1 as an alternative.
The -j8 flag specifies the number of jobs that can run in parallel. You can adjust it according to your CPU (or vCPU) number for the host VM. It took me around 1 hour to finish the compilation.
The make will ask you some LLVM-related questions at the beginning. I used the default options.
Install the compiled Kernel#
Although I did not do this step on my own, because our lab has written a script to utilize the new kernel in VM. Nonetheless, if you want to try your new kernel on your own machine, you can follow these steps.
make module_install
make install
Then, reboot your machine, and enter the boot menu Advanced Options for Ubuntu to select the new kernel.
Conclusion#
Although the process so far seems not that difficult, I spent almost 30 hours to figure out the correct steps and environment. I hope this can help those who want to compile the Linux kernel with Rust support, especially for beginners like me.
