编译rustforlinux内核源码(5.16.0-rc3)
⽬录
编译linux内核源码,我在中有详细介绍。本⽂针对rust for linux内核源码进⾏编译,跟主线版本的linux编译稍有不同,因为引⼊了rust,
编译rust需要安装相关⼯具,以下部分会详细介绍如何编译rust for linux。
本⽂编译环境为Fedora server 35 x86_64:
[root@fedora-yg ~]# uname -a
Linux fedora-yg 5.14.10-300.fc35.x86_64 #1 SMP Thu Oct 7 20:48:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
[root@fedora-yg ~]#
[root@fedora-yg ~]# cat /etc/os-release |head -4
NAME="Fedora Linux"
VERSION="35 (Server Edition)"
ID=fedora
VERSION_ID=35
[root@fedora-yg ~]#
[root@fedora-yg ~]# cat /etc/fedora-release
Fedora release 35 (Thirty Five)
[root@fedora-yg ~]#
[root@fedora-yg ~]# cat /proc/version
Linux version 5.14.10-300.fc35.x86_64 (mockbuild@bkernel01.) (gcc (GCC) 11.2.1 20210728 (Red Hat 11.2.1-1), GNU ld version 2.37-10. [root@fedora-yg ~]#
⼀、获取rust-for-linux内核源码
注:我这⾥⽤的commit是:
675846f923385f32310b61f0409581d89806273d
可以拉取最新版的内核源码即可。
⼆、编译构建内核源码环境部署
1、基本开发环境套件安装
[root@fedora-yg linux-rust]# dnf group install "Development Tools"
2、额外要安装的软件包
[root@fedora-yg linux-rust]# dnf install ncurses-devel zstd bison flex elfutils-libelf-devel openssl-devel
3、安装llvm/clang套件
rust代码编译依赖llvm/clang套件,故还需要安装llvm/clang套件
[root@fedora-yg linux-rust]# dnf install llvm clang lld
注:编译rust for linux内核源码需要llvm/clang版本在10.0.1及以上版本;编译内核时需要加CC=clang或者LLVM=1.
4、安装rustup
①、安装rustup⽐较简单,直接执⾏如下命令
[root@fedora-yg linux-rust]# curl --proto '=https' --tlsv1.2 -sSf sh.rustup.rs | sh
info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/root/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
/root/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
/root/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/
root/.profile
/root/.bash_profile
/root/.bashrc
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
686.2 KiB / 686.2 KiB (100 %) 127.9 KiB/s in  7s ETA:  0s
686.2 KiB / 686.2 KiB (100 %) 127.9 KiB/s in  7s ETA:  0s
info: latest update on 2021-12-02, rust version 1.57.0 (f1edd0429 2021-11-29) info: downloading component 'cargo'
6.3 MiB /  6.3 MiB (100 %)  72.1 KiB/s in  1m 17s ETA:  0s
.
.
.
stable-x86_64-unknown-linux-gnu installed - rustc 1.57.0 (f1edd0429 2021-11-29) Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, run:
source $HOME/.cargo/env
[root@fedora-yg linux-rust]#
②、配置rustup环境变量⽣效
[root@fedora-yg linux-rust]# source /root/.cargo/env
[root@fedora-yg linux-rust]#
③、安装指定版本rustc,⽬前为1.57.0
注:rustup override set 1.57.0命令要在内核源码根⽬录执⾏
[root@fedora-yg linux-rust]# rustup override set 1.57.0
info: syncing channel updates for '1.57.0-x86_64-unknown-linux-gnu'
686.2 KiB / 686.2 KiB (100 %) 223.7 KiB/s in  3s ETA:  0s
info: latest update on 2021-12-02, rust version 1.57.0 (f1edd0429 2021-11-29) info: downloading component 'cargo'
.
.
.
info: installing component 'rust-std'
24.9 MiB /  24.9 MiB (100 %)  11.2 MiB/s in  2s ETA:  0s
info: installing component 'rustc'
53.9 MiB /  53.9 MiB (100 %)  13.1 MiB/s in  4s ETA:  0s
info: installing component 'rustfmt'
info: override toolchain for '/home/linux-rust' set to '1.57.0-x86_64-unknown-linux-gnu'  1.57.0-x86_64-unknown-linux-gnu installed - rustc 1.57.0 (f1edd0429 2021-11-29) [root@fedora-yg linux-rust]#
④、下载rust标准库代码
[root@fedora-yg linux-rust]# rustup component add rust-src
info: downloading component 'rust-src'
info: installing component 'rust-src'
[root@fedora-yg linux-rust]#
⑤、安indgen
需要安装指定版本的bindgen包,⽬前为0.56:
[root@fedora-yg linux-rust]# cargo install --locked --version 0.56.0 bindgen
Updating crates.io index
Downloaded bindgen v0.56.0
Downloaded 1 crate (198.3 KB) in 3.09s
Installing bindgen v0.56.0
Downloaded glob v0.3.0
...
...
...
Compiling regex v1.4.2
Compiling clap v2.33.3
Compiling cexpr v0.4.0
Compiling env_logger v0.8.1
Finished release [optimized] target(s) in 4m 21s
Installing /root/.cargo/bin/bindgen
resized
Installed package `bindgen v0.56.0` (executable `bindgen`)
[root@fedora-yg linux-rust]#
5、磁盘容量检查及扩容
执⾏df -h查看要编译的内核源码所在分区剩余磁盘容量,如果⼤于10G的话,此步骤可以跳过。
安装Fedora server 35的时候,如果你没有⼿动修改磁盘配置,则默认只分配15G给/根⽬录,剩余可⽤就6.8GB了,这个容量编译内核是不够的,make执⾏半天会给你报个no space left on disk错误,然后终⽌编译,所以还是需要先扩容根⽬录(源码所在磁盘分区)。
[root@fedora ~]# df -h
⽂件系统容量已⽤可⽤已⽤% 挂载点
devtmpfs                        1.9G    0  1.9G    0% /dev
tmpfs                          1.9G    0  1.9G    0% /dev/shm
tmpfs                          777M  1.5M  776M    1% /run
/dev/mapper/fedora_fedora-root  15G  8.3G  6.8G  55% /
tmpfs                          1.9G  4.0K  1.9G    1% /tmp
/dev/sda1                      1014M  196M  819M  20% /boot
tmpfs                          389M    0  389M    0% /run/user/0
[root@fedora ~]#
可以看到根⽬录只有15G,但是我创建虚拟机的时候是配置了60G的:
[root@fedora ~]# fdisk -l
Disk /dev/sda:60 GiB,64424509440 字节,125829120 个扇区
磁盘型号:VMware Virtual S
单元:扇区 / 1 * 512 = 512 字节
扇区⼤⼩(逻辑/物理):512 字节 / 512 字节
I/O ⼤⼩(最⼩/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x70b5f1da
设备启动起点末尾扇区⼤⼩ Id 类型
/dev/sda1  *      2048  2099199  2097152  1G 83 Linux
/dev/sda2      2099200 125829119 123729920  59G 8e Linux LVM
Disk /dev/mapper/fedora_fedora-root:15 GiB,16106127360 字节,31457280 个扇区单元:扇区 / 1 * 512 = 512 字节
扇区⼤⼩(逻辑/物理):512 字节 / 512 字节
I/O ⼤⼩(最⼩/最佳):512 字节 / 512 字节
Disk /dev/zram0:3.79 GiB,4070572032 字节,993792 个扇区
单元:扇区 / 1 * 4096 = 4096 字节
扇区⼤⼩(逻辑/物理):4096 字节 / 4096 字节
I/O ⼤⼩(最⼩/最佳):4096 字节 / 4096 字节
[root@fedora ~]#
可以看出/dev/sda2有59G,但是只给/分配了15G。
因为是lvm管理⽅式,我们可以先vgdisplay -v查看下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。