跳至內容
出自 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 環境

The devtools package provides tools for creating and building within clean chroots. Install it if not done already.

To make a clean chroot, create a directory in which the chroot will reside. For example, $HOME/chroot.

$ mkdir ~/chroot

Define the CHROOT variable:

$ CHROOT=$HOME/chroot

Now create the chroot (the sub directory root is required because the $CHROOT directory will get other sub directories for clean working copies):

$ mkarchroot $CHROOT/root base-devel
注意:
  • One can also define the CHROOT variable in $HOME/.bashrc using the export command if the location is to be repeatedly used.
  • On btrfs, the chroot is created as a subvolume, so you have to remove it by removing the subvolume by running btrfs subvolume delete $CHROOT/root as root.

Edit ~/.makepkg.conf to set the packager name and any makeflags. Also adjust the mirrorlist in $CHROOT/root/etc/pacman.d/mirrorlist and enable the testing repositories in $CHROOT/root/etc/pacman.conf, if desired.

注意:The ~ and $HOME variable are resolved to /root/ by the makechrootpkg script (described below).

自定義 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