> 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-4-zhang-an-zhuang-ying-yong-cheng-xu-ruan-jian-bao-he-ports/4.6.-shi-yong-poudriere-gou-jian-ruan-jian-bao.md).

# 4.6.使用 Poudriere 构建软件包

poudriere 是一个采用 `BSD` 许可证的实用工具，用于创建和测试 FreeBSD 软件包。它通过 FreeBSD jail 创建隔离的编译环境。这些 jail 可用于为与当前系统版本不同的 FreeBSD 构建软件包，也可以在 amd64 主机上为 i386 构建软件包。构建完成后，生成的软件包布局与 FreeBSD 官方镜像相同，能被 [pkg(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg\&sektion=8\&format=html) 及其他包管理工具使用。

poudriere 可通过 [ports-mgmt/poudriere](https://cgit.freebsd.org/ports/tree/ports-mgmt/poudriere/) 软件包或 port 安装。安装后会附带一个示例配置文件 `/usr/local/etc/poudriere.conf.sample`，可将其复制为 `/usr/local/etc/poudriere.conf` 并根据本地系统进行编辑。

虽然运行 poudriere 的系统不要求使用 `ZFS`，但使用 ZFS 有助于提升性能。启用 ZFS 时，需要在 `/usr/local/etc/poudriere.conf` 中设置 `ZPOOL`，并将 `FREEBSD_HOST` 设为附近的镜像站。定义 `CCACHE_DIR` 可启用 [devel/ccache](https://cgit.freebsd.org/ports/tree/devel/ccache/)，对重复编译的代码进行缓存，加快构建速度。建议将 poudriere 的数据集挂载到一个独立的目录（如 `/poudriere`）。大多数默认配置项通常已经足够。

系统会根据可用处理器核心数决定并行构建的数量。请确保系统具有足够的虚拟内存（`RAM` 或交换空间），否则 jail 会在构建时中止并被销毁，可能导致莫名其妙的错误信息。

## 4.6.1. 初始化 Jail 和 Ports 树

完成配置后，需要初始化 poudriere，使其安装所需的 FreeBSD jail 和 Ports 树。使用 `-j` 指定 jail 名称，`-v` 指定 FreeBSD 版本。在 FreeBSD/amd64 系统上，也可使用 `-a` 指定架构（`i386` 或 `amd64`），默认使用 `uname` 输出的架构：

```sh
# poudriere jail -c -j 13amd64 -v 13.1-RELEASE
[00:00:00] Creating 13amd64 fs at /poudriere/jails/13amd64... done
[00:00:00] Using pre-distributed MANIFEST for FreeBSD 13.1-RELEASE amd64
[00:00:00] Fetching base for FreeBSD 13.1-RELEASE amd64
/poudriere/jails/13amd64/fromftp/base.txz              125 MB 4110 kBps    31s
[00:00:33] Extracting base... done
[00:00:54] Fetching src for FreeBSD 13.1-RELEASE amd64
/poudriere/jails/13amd64/fromftp/src.txz               154 MB 4178 kBps    38s
[00:01:33] Extracting src... done
[00:02:31] Fetching lib32 for FreeBSD 13.1-RELEASE amd64
/poudriere/jails/13amd64/fromftp/lib32.txz              24 MB 3969 kBps    06s
[00:02:38] Extracting lib32... done
[00:02:42] Cleaning up... done
[00:02:42] Recording filesystem state for clean... done
[00:02:42] Upgrading using ftp
/etc/resolv.conf -> /poudriere/jails/13amd64/etc/resolv.conf
Looking up update.FreeBSD.org mirrors... 3 mirrors found.
Fetching public key from update4.freebsd.org... done.
Fetching metadata signature for 13.1-RELEASE from update4.freebsd.org... done.
Fetching metadata index... done.
Fetching 2 metadata files... done.
Inspecting system... done.
Preparing to download files... done.
Fetching 124 patches.....10....20....30....40....50....60....70....80....90....100....110....120.. done.
Applying patches... done.
Fetching 6 files... done.
The following files will be added as part of updating to
13.1-RELEASE-p1:
/usr/src/contrib/unbound/.github
/usr/src/contrib/unbound/.github/FUNDING.yml
/usr/src/contrib/unbound/contrib/drop2rpz
/usr/src/contrib/unbound/contrib/unbound_portable.service.in
/usr/src/contrib/unbound/services/rpz.c
/usr/src/contrib/unbound/services/rpz.h
/usr/src/lib/libc/tests/gen/spawnp_enoexec.sh
The following files will be updated as part of updating to
13.1-RELEASE-p1:
[…]
Installing updates...Scanning //usr/share/certs/blacklisted for certificates...
Scanning //usr/share/certs/trusted for certificates...
 done.
13.1-RELEASE-p1
[00:04:06] Recording filesystem state for clean... done
[00:04:07] Jail 13amd64 13.1-RELEASE-p1 amd64 is ready to be used
```

```sh
# poudriere ports -c -p local -m git+https
[00:00:00] Creating local fs at /poudriere/ports/local... done
[00:00:00] Checking out the ports tree... done
```

poudriere 支持在单机上构建多个配置的 ports，分别使用不同的 jail、ports 树和配置集合（*sets*）。关于 set 的详细内容，请参阅 [poudriere(8)](https://man.freebsd.org/cgi/man.cgi?query=poudriere\&sektion=8\&format=html) 中的 CUSTOMIZATION 部分。

以下所示的基本配置会将一个针对特定 jail、ports 树和 set 的 `make.conf` 文件放置在 `/usr/local/etc/poudriere.d` 中。此示例中的文件名由 jail 名称、ports 名称和 set 名称组合而成：`13amd64-local-workstation-make.conf`。系统的 `make.conf` 和这个新文件会在构建时合并，生成构建 jail 所使用的 `make.conf`。

要构建的软件包列表写入 `13amd64-local-workstation-pkglist` 文件（带有 [FLAVORS](https://docs.freebsd.org/en/books/porters-handbook/flavors) 的 ports 使用 @FLAVOR 指定）：

```sh
editors/emacs
devel/git
devel/php-composer2@php82
ports-mgmt/pkg
...
```

配置这些 port 的选项与依赖：

```sh
# poudriere options -j 13amd64 -p local -z workstation -f 13amd64-local-workstation-pkglist
```

开始构建并生成软件包仓库：

```sh
# poudriere bulk -j 13amd64 -p local -z workstation -f 13amd64-local-workstation-pkglist
```

构建过程中按下 **Ctrl**+**t** 可查看当前构建状态。构建日志会保存于 `/poudriere/logs/bulk/jailname`，可通过 Web 服务器查看构建详情。

构建完成后，新生成的软件包可通过 poudriere 提供的软件仓库进行安装。

如需了解更多内容，请参阅 [poudriere(8)](https://man.freebsd.org/cgi/man.cgi?query=poudriere\&sektion=8\&format=html) 和 poudriere 官方页面 <https://github.com/freebsd/poudriere/wiki>。

## 4.6.2. 配置 pkg 客户端使用 poudriere 仓库

虽然可以同时使用自定义软件包仓库与官方仓库，但有时更希望禁用官方仓库。这可以通过创建一个覆盖官方配置的文件来实现。创建 `/usr/local/etc/pkg/repos/FreeBSD.conf`，内容如下：

```sh
FreeBSD: {
	enabled: no
}
```

通常最方便的做法是通过 HTTP 将 poudriere 软件包仓库提供给客户端访问。可以通过 Web 服务器提供仓库目录，例如 `/usr/local/poudriere/data/packages/13amd64`，其中 `13amd64` 是构建名。

假设软件包仓库的 URL 是 [`https://pkg.example.com/13amd64`](https://pkg.example.com/13amd64)，那么应在 `/usr/local/etc/pkg/repos/custom.conf` 中创建如下配置文件：

```sh
custom: {
	url: "https://pkg.example.com/13amd64",
	enabled: yes,
}
```

如果不希望将软件包仓库暴露在互联网中，可以使用 `file://` 协议直接指向本地路径，例如：

```sh
custom: {
	url: "file:///usr/local/poudriere/data/packages/11amd64",
	enabled: yes,
}
```


---

# 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-4-zhang-an-zhuang-ying-yong-cheng-xu-ruan-jian-bao-he-ports/4.6.-shi-yong-poudriere-gou-jian-ruan-jian-bao.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.
