My note for trying rust-for-linux.
Here I take arch linux as an example, and I assume you already installed rust and put ~/.cargo/bin
inside your PATH
.
cd /some/where
# Install requirements
sudo pacman -Syuu --noconfirm bc bison curl clang diffutils flex git gcc llvm libelf lld ncurses make qemu-system-x86 cpio
export MAKEFLAGS="-j32"
export LLVM="1"
# Clone rust-for-linux
git clone https://github.com/Rust-for-Linux/linux.git rust-for-linux --depth=1
cd rust-for-linux
# Install rust-bindgen
cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen
# Install other rust components
rustup override set $(scripts/min-tool-version.sh rustc)
rustup component add rust-src
# Check if the enviroment is ready
make LLVM=1 rustavailable
make allnoconfig qemu-busybox-min.config rust.config
# You can enable some sample code in the following step
# Here enable Kernel hacking > Sample kernel code > Rust samples > Echo server module
make menuconfig
make
To run the kernel, we need a initramfs(aka initrd.img).
cd /some/where/busybox
# Download busybox and unzip it
wget https://busybox.net/downloads/busybox-snapshot.tar.bz2
tar -jxvf busybox-snapshot.tar.bz2
# Build it
cd busybox
make defconfig
# Settings > Build Options > Build BusyBox as a static binary (no shared libs)
make menuconfig
make
make install
# Prepare files
cd _install
mkdir -p bin dev sbin etc/init.d proc sys var usr/bin usr/sbin usr/share/udhcpc
cp ../examples/udhcp/simple.script usr/share/udhcpc/default.script
Create etc/init.d/rcS
and chmod +x etc/init.d/rcS
:
#!bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mkdir /dev/pts
mount -t devpts none /dev/pts
ifconfig lo up
udhcpc -i eth0
echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
Create etc/inittab
:
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
Then we can pack it.
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../../initramfs.cpio.gz
After this step, we will find initramfs.cpio.gz in /some/where/busybox
qemu-system-x86_64 -nographic -kernel /some/where/rust-for-linux/arch/x86_64/boot/bzImage -initrd /some/where/busybox/initramfs.cpio.gz -nic user,model=rtl8139,hostfwd=tcp::38080-:8080 -append "console=ttyS0" -enable-kvm
Try it:
nc 127.0.0.1 38080
Type something and the words will be sent back. To implement it is easy, you can use async in it.
Code: https://github.com/Rust-for-Linux/linux/blob/rust/samples/rust/rust_echo_server.rs