# 10.5.构建并安装定制内核

待保存了自定义配置文件的编辑内容，就可以按照以下步骤编译内核源代码：

**过程：构建内核**

1. 切换到此目录：

   ```sh
   # cd /usr/src
   ```
2. 通过指定自定义内核配置文件的名称来编译新内核：

   ```sh
   # make buildkernel KERNCONF=MYKERNEL
   ```
3. 安装与指定内核配置文件关联的新内核。此命令将新内核复制到 /boot/kernel/kernel 并将旧内核保存到 /boot/kernel.old/kernel：

   ```sh
   # make installkernel KERNCONF=MYKERNEL
   ```
4. 关闭系统并重启进入新内核。如果出现问题，请参考 [内核无法启动](https://docs.freebsd.org/en/books/handbook/kernelconfig/#kernelconfig-noboot)。

默认情况下，当编译自定义内核时，所有内核模块都会重新编译。要更快地更新内核或仅构建自定义模块，可以在开始构建内核之前编辑 /etc/make.conf。

例如，以下变量指定要构建的模块列表，而不是默认构建所有模块：

```ini
MODULES_OVERRIDE = linux acpi
```

或者，此变量列出要从构建过程中排除的模块：

```ini
WITHOUT_MODULES = linux acpi sound
```

`NO_MODULES` 完全跳过模块构建，仅构建内核：

```ini
NO_MODULES = yes
```

**Ports 框架**包含依赖内核接口的驱动程序和模块（如 [graphics/drm-kmod](https://cgit.freebsd.org/ports/tree/graphics/drm-kmod/)、[emulators/virtualbox-ose-kmod](https://cgit.freebsd.org/ports/tree/emulators/virtualbox-ose-kmod/)）。使用 `PORTS_MODULES` 变量，每次内核构建时也会重新构建这些 port 并同步内核接口，确保“内核模块与内核本身保持同步”。内核树和 ports 树应一起更新。例如：

```ini
PORTS_MODULES = drm-kmod virtualbox-ose-kmod
```

还可以使用其他变量。有关详细信息，请参阅 [make.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=make.conf\&sektion=5\&format=html)。


---

# 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://book.bsdcn.org/hanbook/di-10-zhang-pei-zhi-freebsd-nei-he/10.5.-bian-yi-yu-an-zhuang-ding-zhi-nei-he.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.
