体验rust-for-linux

编译x86 kernel + 构建文件系统

  1. Rust依赖安装

    1. 参考:Rust for linux: Quick Start
    2. 使用用cargo/rustup安装rustc + rust-src + bindgen
    3. 安装LLVM-14
      1. 参考https://apt.llvm.org/
      2. bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
  2. x86生成配置+编译内核

    1. 需要注意的选项

      1. CONFIG_SYSTEM_TRUSTED_KEYS 设置为空
      2. CONFIG_SYSTEM_REVOCATION_KEYS 设置为空
      3. MODVERSIONS + GCC_PLUGINS 这两个配置默认是y,需要关闭才能打开CONFIG_RUST
    2. 编译内核+模块

      1
      make LLVM=-14   O=../rust-kernel-build/amd64 -j 64
    3. 安装内核模块

      INSTALL_MOD_PATH 是相对于内核编译路径设置的,不是当前内核源码路径

      1
      sudo make LLVM=-14  O=../rust-kernel-build/amd64  INSTALL_MOD_PATH=../../../debian11-amd64-rootfs  modules_install
  3. 编译独立的内核模块

    1. rust-out-of-tree-module repo

    2. 改一下Makefile,加上modules_install 内容

      1
      2
      3
      4
      5
      6
      7
      8
      9
      ❯ cat Makefile
      # SPDX-License-Identifier: GPL-2.0

      KDIR ?= /lib/modules/`uname -r`/build
      INSTALL_MOD_PATH ?= /
      default:
      $(MAKE) -C $(KDIR) M=$$PWD
      modules_install:
      $(MAKE) -C $(KDIR) M=$$PWD modules_install
    3. 编译独立的模块,并安装

      1
      2
      make LLVM=-14 KDIR=../rust-kernel-build/amd64
      make LLVM=-14 KDIR=../rust-kernel-build/amd64 INSTALL_MOD_PATH=../../../debian11-arm64-rootfs modules_install
  4. 构建文件系统

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    dd  if=/dev/zero of=./debian11-amd64-rootfs.img bs=1M count=10240
    mkfs -t ext4 ./debian11-amd64-rootfs.img
    sudo mkdir debian11-amd64-rootfs
    sudo mount debian11-amd64-rootfs.img debian11-amd64-rootfs
    # 下载debian 11 rootfs
    sudo debootstrap --arch amd64 stable debian11-amd64-rootfs http://ftp.debian.org/debian
    # 根据自己习惯配置一下rootfs
    sudo chroot debian11-amd64-rootfs

    apt-get update
    apt-get install sudo vim bash-completion -y
    apt-get install net-tools ethtool ifupdown network-manager iputils-ping -y
    apt-get install rsyslog resolvconf udev -y
    apt-get install vim net-tools openssh-server curl git build-essential -y
    echo "debian" >/etc/hostname
    echo "127.0.0.1 localhost" >/etc/hosts

    dpkg-reconfigure resolvconf
    dpkg-reconfigure tzdata
    exit
  5. Qemu启动脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #!/bin/bash

    KRNL_IMGE=<kernel-build-dir>/amd64/arch/x86_64/boot/bzImage
    ROOT_FILE=debian11-amd64-rootfs.img
    QEMU_BINY=./x86_64-qemu/qemu-system-x86_64

    sudo ${QEMU_BINY} \
    -name "rust-for-linux-VM" \
    -enable-kvm \
    -cpu host \
    -smp 4 \
    -m 16G \
    -kernel ${KRNL_IMGE} \
    -hda ${ROOT_FILE} -append "console=ttyS0 root=/dev/sda rw" \
    -serial mon:stdio \
    -net user,hostfwd=tcp::38080-:22 \
    -net nic,model=virtio \
    -nographic \
    -qmp unix:./qmp-amd64-sock,server,nowait
  6. 测试独立的模块

    用新编译的内核+文件系统启动qemu

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    root@debian:~# modprobe rust_out_of_tree
    [ 149.540799] rust_out_of_tree: loading out-of-tree module taints kernel.
    [ 149.548570] rust_out_of_tree: Rust out-of-tree sample (init)
    root@debian:~# modinfo rust_out_of_tree
    filename: /lib/modules/5.19.0-rc1-156671-gabd32d188969/extra/rust_out_of_tree.ko
    author: Rust for Linux Contributors
    description: Rust out-of-tree sample
    license: GPL v2
    vermagic: 5.19.0-rc1-156671-gabd32d188969 SMP preempt mod_unload aarch64
    name: rust_out_of_tree
    depends:
    root@debian:~# rmmod rust_out_of_tree
    [ 210.345721] rust_out_of_tree: My message is on the heap!
    [ 210.345961] rust_out_of_tree: Rust out-of-tree sample (exit)

交叉编译Arm64 kernel

对于LLVM交叉编译而言,不需要像gcc一样安装不同架构的工具链,仅是需要给clang传递-target=<triple> 参数,所以执行编译内核时,设置ARCH变量即可,不需要设置CROSS_COMPILE 变量,详细的LLVM编译内核文档可以参考:Building Linux with Clang/LLVM

  1. config中打开 CONFIG_RUST 相关选项

    1
    make LLVM=-14  ARCH=arm64  O=../rust-kernel-build/arm64  menuconfig
  2. 编译内核/内核模块

    1
    make LLVM=-14  ARCH=arm64  O=../rust-kernel-build/arm64  -j 64
  3. 安装内核模块,需要指定根文件目录

    安装完成后,内核模块会被安装到 ../../debian11-arm64-rootfs/lib/modules/ 目录下

    1
    sudo make LLVM=-14  ARCH=arm64  O=../rust-kernel-build/arm64 INSTALL_MOD_PATH=../../../debian11-arm64-rootfs  modules_install

体验rust-for-linux
https://gwzlchn.github.io/202206/rust-for-linux-01/
作者
Zelin Wang
发布于
2022年6月16日
更新于
2022年10月23日
许可协议