LLVM is commonly used to build the Linux kernel and other operating systems instead of using GCC. Research papers were authored on this subject a number of years ago, which are worth a read:
They utilise LLVM for building the Linux kernel or other operating systems. LLVM is also a prerequisite to use Rust in the Linux kernel.
In this article, we demonstrate the procedure to build the linux kernel with LLVM on an OCI (Oracle Cloud Infrastructure) Oracle Linux 8 instance.
Step 1. Create the instance
In this article, our Oracle Linux 8 instance is an OCI VM.Standard2.16, with 16 OCPUs (32 CPUs) and paravirtualized networking.
Step 2. Setup the environment
Please install the required packages for LLVM.
$ sudo yum install clang lld llvm
Step 3. Download source code
We use linux-6.6 in this article.
$ wget https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x/linux-6.6.tar.xz
Step 4. Build and Install
Apply some additional config options and run 32 jobs in parallel during the build (as indicated by -j32). If for instance you had 16 CPUs available, you could specify -j16. We need LLVM=1 to build the linux kernel.
$ tar xvf linux-6.6.tar.xz $ cd linux-6.6/ $ make LLVM=1 defconfig $ scripts/config --file ".config" -e CONFIG_X86_X2APIC \ -m CONFIG_SCSI_VIRTIO -m CONFIG_VIRTIO_NET \ -m CONFIG_XFS_FS -m CONFIG_ISCSI_TCP \ -m CONFIG_ISCSI_BOOT_SYSFS $ make LLVM=1 olddefconfig $ make LLVM=1 -j32 > /dev/null $ sudo make LLVM=1 modules_install $ sudo make LLVM=1 install
Step 5. Configure and Reboot
Increase the loglevel to 7 by editing /etc/default/grub, and re-generate grub. This helps diagnose if any hang/panic events occurred during kernel boot.
You may need sudo su to switch to the root user.
$ cat /etc/default/grub GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_DISABLE_RECOVERY="true" GRUB_ENABLE_BLSCFG=true GRUB_TERMINAL="console" GRUB_CMDLINE_LINUX="crashkernel=auto LANG=en_US.UTF-8 console=tty0 console=ttyS0,115200 rd.luks=0 rd.md=0 rd.dm=0 rd.lvm.vg=ocivolume rd.lvm.lv=ocivolume/root rd.net.timeout.carrier=5 netroot=iscsi:169.254.0.2:::1:iqn.2015-02.oracle.boot:uefi rd.iscsi.param=node.session.timeo.replacement_timeout=6000 net.ifnames=1 nvme_core.shutdown_timeout=10 ipmi_si.tryacpi=0 ipmi_si.trydmi=0 libiscsi.debug_libiscsi_eh=1 loglevel=7 ip=dhcp,dhcp6 rd.net.timeout.dhcp=10 crash_kexec_post_notifiers" root# grub2-mkconfig > /boot/efi/EFI/redhat/grub.cfg
Set the default boot kernel in grub to linux-6.6.
root# grubby --set-default /boot/vmlinuz-6.6.0 The default is /boot/loader/entries/c568d3603873424f80f6ef62af85b224-6.6.0.conf with index 3 and kernel /boot/vmlinuz-6.6.0
Reboot and check the kernel is now at 6.6:
$ uname -r 6.6.0
In addition, the compiler information in dmesg is for clang.
$ dmesg | grep clang [ 0.000000] Linux version 6.6.0 (opc@test-kernel) (clang version 16.0.6 (Red Hat 16.0.6-2.0.1.module+el8.9.0+90022+019b57b8), LLD 16.0.6) #1 SMP PREEMPT_DYNAMIC Wed Dec 27 02:15:00 GMT 2023
Summary
We have demonstrated the procedure to build the Linux kernel 6.6 on OCI and Oracle Linux 8 with LLVM. Developers may use OCI and Oracle Linux 8 for LLVM related development/research projects.