正在查看旧版本。 查看 当前版本.

与当前比较 查看页面历史记录

版本 1 当前 »

简介

Linux 内核编译选项非常多。在虚拟机环境下执行编译可以对照参考 https://wiki.waringid.me/x/cAC1B 中内核编译的部分。以 debian 系统环境为例先下载基础的前置组件。

sudo apt-get install libncurses5-dev build-essential kernel-package ibdw-dev dwarves zstd libssl-dev libelf-dev

接下来下载对应的内核文件。例如 6.13.4,以下是下载内核文件的链接(列出了内核镜像的网站)

内核编译

X86 内核

tar xvf linux-6.13.4.tar.xz
cd linux-6.13.4
make mrproper
make defconfig
make menuconfig
make 
make modules_install

虚拟机环境下,还需选择下面的选项,否则将会导致vfs unable to mount root fs on unknown-block(0 0)的提示,将会导致LFS无法启动。

Device Drivers --->
   Generic Driver Options --->
      [*] Maintain a devtmpfs filesystem to mount at /dev
   [*]Network device support --->
      [*]Ethernet Driver support --->
         [*] AMD PCnet32 PCI support
   [*]Fusion MPT device support --->
      <*> Fusion MPT ScsiHost drivers for SAS
      <*> Fusion MPT misc device (ioctl) driver  
      [*] Fusion MPT logging facility 
   SCSI device support --->
      [*] SCSI low-level drivers
File Systems --->
    [*] Ext3 Journaling file system support

完成编译后会有内核文件的生成提示。

Kernel: arch/x86/boot/bzImage is ready  (#3)

交叉编译 ARM64

编译过程有少许的不同,先从 ARM64 路径下复制默认配置,然后在原来的编译命令上,添加 ARCH 来指定目标指令集,生成的内核从 bzImage 变成了 Image,编译后在根目录下会产生 vmlinux*,arch/arm64/boot/ 下会产生 Image 和 Image.gz


cp ./arch/arm64/configs/defconfig  .config
make menuconfig ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
make Image -j8 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-


构建内存文件系统(initrd)

可以使用 busybox 来构建一个简单的文件系统,在 https://busybox.net/ 可以找到 busybox 的最新版本,以 BusyBox 1.35.0 为例

1、下载源码并执行配置

wget https://busybox.net/downloads/busybox-1.35.0.tar.bz2
tar -xvf busybox-1.35.0.tar.bz2
cd busybox-1.35.0
make menuconfig
make
make install
  • 在 Setttings 选中 Build static binary (no shared libs),将 busybox 编译为静态链接的文件
  • 在 Linux System Utilities 中取消选中 Support mounting NFS file systems on Linux < 2.6.23 (NEW),关闭网络文件系统
  • 在 Networking Utilities 中取消选中 inetd,关闭 Internet 超级服务器

在源码根目录下就会有一个 _install 目录,下面存放着编译好的文件

BusyBox 交叉编译

对于 busybox 来说,在 menuconfig 中就可以直接配置交叉编译,具体位置在 Settings -> Cross compiler prefix,可以将其设置为 aarch64-linux-gnu- (对于编译 aarch64 来说)

配置后和原来一样编译就可以得到一份 aarch64 架构的 busybox

配置文件系统

  1. 添加启动文件,并且加上可执行权限
  2. 新增 init 文件
  3. 打包文件系统


cd _install
mkdir -p  proc sys dev etc/init.d
gedit etc/init.d/rcS
chmod +x etc/init.d/rcS
cat > /etc/init << EOF
#!/bin/sh
echo "INIT SCRIPT"
mkdir /tmp
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mount -t debugfs none /sys/kernel/debug
mount -t tmpfs none /tmp
echo -e "Boot took $(cut -d' ' -f1 /proc/uptime) seconds"
setsid /bin/cttyhack setuidgid 1000 /bin/sh
EOF
find . | cpio -o --format=newc > ../rootfs.img

Buildroot

下载解压并编译 Buildroot。https://buildroot.org/download.html

wget https://buildroot.org/downloads/buildroot-2022.02.tar.xz
tar -xvf buildroot-2022.02.tar.x
cd buildroot-2022.02
make menuconfig
mak

Target options  ---> 选择目标板架构特性。
Build options  ---> 配置编译选项。
Toolchain  ---> 配置交叉工具链,使用buildroot工具链还是外部提供。
System configuration  ---> 配置生成的根文件系统中所需功能
Kernel  ---> 配置kernel是否编译以及编译选项
Target packages  ---> 配置生成的根文件系统中的工具以及库
Filesystem images  ---> 配置生成的根文件系统的格式,是ext2还是其他
Bootloaders  ---> 配置使用哪种bootloader以及编译选项,uboot只是其中一种
Host utilities  ---> 主机使用功能
Legacy config options  ---> 以前遗留的配置选项

交叉编译

  • Target option ---> Target Architecture 为 AArch64 (little endian)
  • Toolchain ---> Toolchain type 为 External toolchain,这时我们可以看到 Toolchain ---> Toolchain 的值为 Arm AArch64 xxxx.xx

设置密码

  • System configuration ---> Enable root login with password 开启
  • System configuration ---> Root password 为 xxxx(任意的你喜欢的密码)

交叉编译

  • System configuration ---> Run a getty (login prompt) after boot ---> TTY port 的值为 ttyAMA0(这一条非常重要,不然虚拟机可能启动不了,如果出现 can’t open /dev/ttyAMA0: No such file or directory,就改回 console)
  • Target packages ---> Show packages that are also provided by busybox 开启
  • Target packages ---> Debugging, profiling and benchmark ---> strace 开启
  • Filesystem images ---> cpio the root filesystem 开启。

启动欢迎语

  • System configuration ---> System hostname
    System configuration ---> System banner

配置网卡 DHCP

  • System configuration ---> Network interface to configure through DHCP


  • 无标签