# 定制 Linux 内核：交叉编译

## 1. 准备编译环境

准备一 x86\_64 Debian 环境。

### 1.1. 安装龙芯交叉编译器

{% content-ref url="/pages/-MIIMV3PsnTZ3EVTSiNx" %}
[安装交叉 GCC 编译器](/loongbian/os/install-cross-gcc.md)
{% endcontent-ref %}

### 1.2. 安装其他构建依赖

进入 root 用户 shell，运行

```bash
$ apt install git-core bc bison flex libssl-dev make libncurses5-dev
```

## 2. 获取内核源码

```
$ git clone https://github.com/Loongbian/linux -b loongson-2k --depth 1
$ cd linux
```

## 3. 配置内核编译配置

### 3.1. 加载龙芯 2k 默认配置

```
$ make ARCH=mips CROSS_COMPILE=mips64el-linux-gnuabi64- ls2k_defconfig debian.config
```

### 3.2. 配置内核构建选项（可选）

```
$ make ARCH=mips CROSS_COMPILE=mips64el-linux-gnuabi64- menuconfig
```

## 4. 构建内核

### 4.1. 构建内核镜像 `vmlinuz`

```
$ make ARCH=mips CROSS_COMPILE=mips64el-linux-gnuabi64- vmlinuz -j6
```

构建出的内核镜像文件名为 `vmlinuz`。

### 4.2. 构建并导出内核模块（可选，但强烈建议）

```
$ make ARCH=mips CROSS_COMPILE=mips64el-linux-gnuabi64- modules -j6
$ make ARCH=mips CROSS_COMPILE=mips64el-linux-gnuabi64- INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=kernel_modules modules_install
```

构建出的内核模块将位于 `kernel_modules`文件夹下，您可以更改上述命令中的 `INSTALL_MOD_PATH` 变量来自定义内核模块导出位置。

### 4.3. 导出内核头文件（可选）

```
$ make ARCH=mips CROSS_COMPILE=mips64el-linux-gnuabi64- INSTALL_HDR_PATH=kernel_headers headers_install
```

构建出的内核头文件将位于 `kernel_headers`文件夹下，您可以更改上述命令中的 `INSTALL_HDR_PATH` 变量来自定义内核头文件导出位置。

## 5. 打包内核文件并传至龙芯派

> 第二条 `scp` 命令执行前需要构建机器和龙芯派在同一个内网里，并在龙芯派上先安装好 `openssh-server`，可以通过 root 下运行 `apt install openssh-server` 来安装。或者你可以使用 u 盘将 `linux-build.tar.gz` 文件拷贝到龙芯派上来替代 `scp` 命令。

```
$ tar zcvf linux-build.tar.gz vmlinuz kernel_headers kernel_modules
$ scp linux-build.tar.gz loongson@ls2k:
```

若你没有导出内核模块，你需要将 `tar` 命令参数中的 `kernel_modules` 删去。若你没有导出内核头文件，将 `kernel_headers` 删去。

此外，你可能需要将 `scp` 命令中的 `ls2k` 替换为龙芯派的 IP 地址。

## 6. 安装内核文件

在龙芯派上登入 root 用户。

在以下命令中我们假定内核版本为 `5.7.0-rc1+`，内核版本可能有变化。

### 6.1. 解压压缩包

```
$ mkdir kernel
$ cd kernel
$ tar xvf ~loongson/linux-build.tar.gz
```

### 6.2.  安装内核本身

```
$ mv vmlinuz /boot/vmlinuz-5.7.0-rc1+
$ chmod 644 /boot/vmlinuz-5.7.0-rc1+
```

### 6.3. 安装内核模块

如果你此前导出了内核模块，则运行，否则不用运行。

```
$ mv kernel_modules/lib/modules/5.7.0-rc1+ /lib/modules
$ chmod -R 644 /lib/modules/5.7.0-rc1+
```

### 6.4. 生成 initramfs

```
$ update-initramfs -c -k 5.7.0-rc1+
```

### 6.5. 更新 PMON 启动项

```
$ pmon-update
```

现在你可以输入 `reboot` 命令重启龙芯派。启动过程中 PMON 启动菜单应会出现刚刚编译的新内核。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mbyzhang.gitbook.io/loongbian/os/customize-linux-kernel-cross-compile.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
