Setting up Rust Environment for Linux Kernel Development

Hey 👋

It’s me again, still writing about Rust before actually going beyond the hello world part of the documentation, but I will let you know why.

I am currently going through Chris Simmonds’ terrific book “Mastering Embedded Linux Programming, and while reading through the chapter covering the kernel configuration and compilation , I stumbled upon a nice youtube video from the linux foundation covering the same topic but using the Rust fork as the codebase.

So, since I have an interest in Rust too, I said why not combining both ? Which was actually a good choice because both the video and the book covered each other gaps.

While watching the video, I took a note of most of the commands in a github gist which I think should be pretty useful for my future self and and other people too. I thought also of taking the notes here because why not ..

#
# Instructions for the video https://www.youtube.com/watch?v=tPs1uRqOnlk excluding setting up the env ( e.g. installing Rust and llvm)
#


#
# Kernel
#

$ git clone --depth=1 https://github.com/Rust-for-Linux/linux.git
$ cd linux/
$ make allnoconfig qemu-busybox-min.config
$ make allnoconfig qemu-busybox-min.config rust.config
$ make LLVM=1 rustavailable # should say Rust is available if you have all dependencies
$ make LLVM=1 -j4

#
# Busybox
#
$ cd busybox
$ git clone --depth=1 https://github.com/mirror/busybox.git
$ make defconfig
$ make menuconfig # then from settings select use static libraries 
# make -j4
$ cd ./_install/
$ find . | cpio -H newc -o | gzip > ../ramdisk.img


#
# running qemu
#
$ cd linux/
$ qemu-system-x86_64 -nographic -kernel vmlinux # should panic
$ qemu-system-x86_64 -nographic -kernel vmlinux -initrd ../busybox/ramdisk.img # should start


#
# adding proc filesystem to enable ps
#
$ cd busybox/_install/
$ vim etc/init.d/rcS 
# then add this content inside 
# mkdir -p /proc 
# mount -t proc none /proc
$ find . | cpio -H newc -o | gzip > ../ramdisk.img
$ qemu-system-x86_64 -nographic -kernel vmlinux -initrd ../busybox/ramdisk.img # ps should work


#
# enabling a rust sample kernel module
#
$ make LLVM=1 menuconfig
# then select the samples from kernel hacking --> samples --> rust samples


#
# enable networking
#
$ cd busybox/_install/
$ vim etc/init.d/rcS 
# then add this content inside to enable loopback interface 
# ifconfig lo up
# udhcpc -i eth0
$ mkdir -p usr/share/udhcpc
$ cp ../examples/udhcp/simple.script usr/share/udhcpc/default.script
$ qemu-system-x86_64 -nographic -kernel vmlinux -initrd ../busybox/ramdisk.img -nic user,model=rtl8139
$ qemu-system-x86_64 -nographic -kernel vmlinux -initrd ../busybox/ramdisk.img -nic user,model=rtl8139,hostfwd=tcp::5556-:8080

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: