跳转到内容
来自 Arch Linux 中文维基

本文或本节需要翻译。要贡献翻译,请访问简体中文翻译团队

附注: #传统方法#处理大规模重构 需要翻译(在 Talk:创建一个干净的 chroot# 中讨论)

使用 clean chroot 的意义

clean chroot 可以直译为 干净/洁净的 chroot。在 clean chroot 环境中构建可以避免软件包依赖缺失问题。不然可能会出现意外的动态链接或是在 PKGBUILD 依赖数组 depends 中出现未声明的依赖。此外,它还允许用户参与 core-testingextra-testing 测试仓库情况下,为稳定仓库 (coreextra) 构建软件包。


便捷方式

为了快速在 clean chroot 环境中构建软件包而无需复杂配置,你可以使用 devtools 中的辅助脚本来帮助你。

这些辅助脚本应在 PKGBUILD 的同一目录中调用,就像使用 makepkg 时一样。例如,extra-x86_64-build 将会自动从 /var/lib/archbuild 的 clean chroot 模板中创建 chroot 环境,更新它,并为 extra 仓库构建软件包。对于 multilib 构建则使用无架构参数的 multilib-build。请参考下表了解不同目标仓库和架构对应的构建脚本。

-c参数用于重置chroot模板,这在环境损坏时非常有用。在建立全新的 clean chroot 时不需要此参数。

注意:
  • core 仓库被省略,因为这些软件包必须先通过 core-testing 仓库测试才能进入正式仓库
  • 如果目标是为本地使用构建 core 仓库的软件包,建议直接使用稳定仓库而非测试仓库。此时可以直接使用extra构建脚本
目标仓库 架构 构建脚本 使用的 Pacman 配置文件
extra x86_64 extra-x86_64-build /usr/share/devtools/pacman.conf.d/extra.conf
core-testing / extra-testing x86_64 extra-testing-x86_64-build /usr/share/devtools/pacman.conf.d/extra-testing.conf
core-staging / extra-staging x86_64 extra-staging-x86_64-build /usr/share/devtools/pacman.conf.d/extra-staging.conf
multilib x86_64 multilib-build /usr/share/devtools/pacman.conf.d/multilib.conf
multilib-testing x86_64 multilib-testing-build /usr/share/devtools/pacman.conf.d/multilib-testing.conf
multilib-staging x86_64 multilib-staging-build /usr/share/devtools/pacman.conf.d/multilib-staging.conf
提示:pkgctl-build(1)会自动选择正确的构建脚本在 clean chroot 中构建

传统方法

设置 chroot 环境

devtools 提供了用于创建 clean chroot 并在其中构建软件包的功能,请确保您已安装该软件包。

之后,为创建 clean chroot,请创建一个新文件夹用于存放 chroot,比如 $HOME/chroot/

$ mkdir ~/chroot

之后定义 CHROOT 变量:

$ CHROOT=$HOME/chroot

现在就可以开始创建 clean chroot 环境了:

注意:chroot 需要 root 文件夹的访问权限,因为 $CHROOT 目录需要复制其中的必要文件来创建一个干净的工作环境
$ mkarchroot $CHROOT/root base-devel
注意:
  • 若您经常使用 clean chroot 构建软件包,在 $HOME/.bashrc 里添加 export 命令以引入 CHROOT 变量是一个不错的选择
  • btrfs 文件系统中,chroot 会以分卷(subvolume)的形式被创建,因此您必须以 root 用户的身份运行 btrfs subvolume delete $CHROOT/root 才能删除这个分卷。

所有相关信息,比如打包者名称、makeflags 等都在 ~/.makepkg.conf 中,毋庸置疑,您需要编辑 ~/.makepkg.conf 以确保其被正常构建。别忘了修改 $CHROOT/root/etc/pacman.d/mirrorlist 以更换镜像源

如果需要,您也可以在 $CHROOT/root/etc/pacman.conf 中启用 testing 软件仓库以在 chroot 中获取测试版本的软件包。

注意:~$HOME 变量在"makechrootpkg"构建脚本中会被自动定义为 /root/

自定义 pacman.conf

Alternatively, provide a custom pacman.conf and makepkg.conf with the following:

$ mkarchroot -C <pacman.conf> -M <makepkg.conf> $CHROOT/root base-devel
警告:Using a custom pacman.conf or makepkg.conf during the initial creation of clean chroot can result in unintended custom adjustments to the chroot environment. Use with caution.

在 chroot 里构建

Firstly, make sure the base chroot ($CHROOT/root) is up to date:

$ arch-nspawn $CHROOT/root pacman -Syu

Then, build a package by calling makechrootpkg in the directory containing its PKGBUILD:

$ makechrootpkg -c -r $CHROOT
注意:Passing the -c flag to makechrootpkg ensures that the working chroot ($CHROOT/$USER) is cleaned before building.

预装必要依赖包

To build a package with dependencies unavailable from the repositories enabled in $CHROOT/root/pacman.conf, pre-install them to the working chroot with -I package:

$ makechrootpkg -c -r $CHROOT -I build-dependency-1.0-1-x86_64.pkg.tar.xz -I required-package-2.0-2-x86_64.pkg.tar.xz

向 makepkg 传递参数

To pass arguments to makepkg, list them after an end-of-options marker; e.g., to force a check():

$ makechrootpkg -c -r $CHROOT -- --check

处理大规模重构

The cleanest way to handle a major rebuild is to use the staging repositories. Build the first package against extra and push it to staging. Then rebuild all following packages against staging and push them there.

If you cannot use staging, you can build against custom packages using a command like this:

# extra-x86_64-build -- -I ~/packages/foobar/foobar-2-1-any.pkg.tar.xz

You can specify more than one package to be installed using multiple -I arguments.

A simpler, but dirtier way to handle a major rebuild is to install all built packages in the chroot, never cleaning it. Build the first package using:

# extra-x86_64-build

And build all following packages using:

# makechrootpkg -n -r /var/lib/archbuild/extra-x86_64

Running namcap (the -n argument) implies installing the package in the chroot. *-build also does this by default.

小提示和小技巧

在tmpfs中构建

若系统拥有足够内存,可为 devtools 构建脚本指定 tmpfs 文件系统。

# mount --mkdir -t tmpfs -o defaults,size=20G tmpfs /mnt/chroots/arch
# extra-x86_64-build -c -r /mnt/chroots/arch