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