一. 环境配置
先在资源池的计算节点配置rust编译环境,可以直接在物理机上编译。每个内核目录之间不冲突。以6.1版本的linux内核为例。具体步骤如下:
1. linux安装包准备。
获取linux内核6.1.83压缩包,可以在kernel官网(kernel.org/)下载,再采用tar命令解压压缩包inux-6.1.83.tar.xz。
2. root用户进入自己的内核源码目录:
[root@gzinf-computer-55e235e17e24 rust_kernel]# cd linux-6.1.83
3. 已经对rust相关的依赖和包等进行封装,无需再单独安装各个依赖和包,直接执行对应版本的命令,6.1版本的linux内核对应执行r61,例如:
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# r61
info: override toolchain for '/var/***/rust_kernel/linux-6.1.83' set to '1.62.0-x86_64-unknown-linux-gnu'
4. 上述命令执行完后,可以执行make LLVM=1 rustavailable检查rust编译环境的状态是否ok。打印rust可用,表示成功。
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# make LLVM=1 rustavailable
Rust is available!
5. 第3步和第4步封装成了一个函数r,所以也可以跳过第3步和第4步,直接执行该步。
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# r
info: override toolchain for '/var/***/rust_kernel/linux-6.1.83' set to '1.62.0-x86_64-unknown-linux-gnu'
info: component 'rust-src' is up to date
info: component 'rustfmt' for target 'x86_64-unknown-linux-gnu' is up to date
info: component 'clippy' for target 'x86_64-unknown-linux-gnu' is up to date
Rust is available!
一. 编译流程
1. 在编译之前,需要生成.config配置文件。
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# make LLVM=1 defconfig
2. 检查.config配置文件是否生成成功,采用ls –a命令,如果看到当前目录下生成了一个.config配置文件,即表示生成成功。
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# ls -a
3. 修改.config配置文件,支持rust。首先打开图形化的内核配置。 [root@gzinf-computer-55e235e17e24 linux-6.1.83]# make menuconfig
l 找到“Enable loadable module support”,设置里面的“Module versioning support”为空,即“[ ] Module versioning support” 。
l 找到“General setup”,设置里面的“Rust support”为选中(点击y键),即“[*] Rust support”。
l 找到“Kernel hacking”,设置里面的“Sample kernel code”为选中,即“[*] Sample kernel code”。点击enter键进入,设置“[*] Rust samples”,继续点击enter键进入,设置“<M> Minimal”和“[*] Host programs”。一直退出,直到提示“Do you wish to save your new configuration?”时,选择“Yes”即可。
4.模块编译(注:第4步的模块编译是可选的)。在模块编译之前,先prepare观察一下。当提示选择时,直接回车,选择默认即可。模块编译完成后可以ls观察一下,比如生成了rust_minimal.ko文件,即模块编译成功。
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# make LLVM=1 modules_prepare
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# make LLVM=1 M=samples/rust
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# ls samples/rust
5.完整内核编译。编译的命令是make LLVM=1,多核编译的命令是make LLVM=1 -j `nproc`。
[root@gzinf-computer-55e235e17e24 linux-6.1.83]# make LLVM=1 -j `nproc`