> For the complete documentation index, see [llms.txt](https://handbook.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://handbook.bsdcn.org/di-10-zhang-pei-zhi-freebsd-nei-he/10.5.-bian-yi-yu-an-zhuang-ding-zhi-nei-he.md).

# 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. 关闭系统并重启进入新内核。如果出现问题，请参考 [内核无法引导](/di-10-zhang-pei-zhi-freebsd-nei-he/10.6.-ru-guo-fa-sheng-le-yi-xie-cuo-wu.md)。

默认情况下，当编译定制内核时，所有内核模块都会重新编译。要更快地更新内核或仅构建自定义模块，可以在开始构建内核之前编辑 /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 树应一起更新以获得最大兼容性。`PORTS_MODULES` 可以添加到 /etc/make.conf，以确保你构建的所有内核也会重新构建这些模块。例如：

```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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://handbook.bsdcn.org/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.
