前言

在編譯Debian Package 時, 開發者常使用 pbuilder 建立乾淨的root filesystem, 並且在其中編譯,如此才不會有相依性不完整, 或是少檔案等在原始開發環境難以發現的錯誤 (因為開發環境會安裝所需的套件)

而 cowbuilder 與 pbuilder 的不同為 cowbuilder 並不會將建立好的 root filesystem 壓縮, 因此在編譯套件時可以省下解壓縮 root filesystem的時間。

安裝相關套件

user@host# sudo apt-get install cowbuilder debootstrap devscripts

將下面內容寫入 .pbuilderrc

export CCACHE_DIR="/var/cache/ccache"
export PATH="/usr/lib/ccache:${PATH}"

EXTRAPACKAGES="${EXTRAPACKAGES} ccache pbuilder cowdancer cowbuilder devscripts gnupg patchutils vim-tiny"
BINDMOUNTS="${BINDMOUNTS} ${CCACHE_DIR}"
COMPONENTS="main restricted universe multiverse"
PDEBUILD_PBUILDER=cowbuilder

UNSTABLE_CODENAME="sid"
TESTING_CODENAME="wheezy"
STABLE_CODENAME="squeeze"
STABLE_BACKPORTS_SUITE="$STABLE_CODENAME-backports"

# List of Debian suites.
DEBIAN_SUITES=($UNSTABLE_CODENAME $TESTING_CODENAME $STABLE_CODENAME
"unstable" "testing" "stable")

# List of Ubuntu suites. Update these when needed.
UBUNTU_SUITES=("natty" "maverick" "lucid" "karmic" "jaunty" "hardy")

# Mirrors to use. Update these to your preferred mirror.
DEBIAN_MIRROR="ftp.tw.debian.org"
UBUNTU_MIRROR="mirrors.kernel.org"

# Optionally use the changelog of a package to determine the suite to use if
# none set.
if [ -z "${DIST}" ] && [ -r "debian/changelog" ]; then
DIST=$(dpkg-parsechangelog | awk ‘/^Distribution: / {print $2}’)
# Use the unstable suite for certain suite values.
if $(echo "experimental UNRELEASED" | grep -q $DIST); then
DIST="$UNSTABLE_CODENAME"
fi
fi

# Optionally set a default distribution if none is used. Note that you can set
# your own default (i.e. ${DIST:="unstable"}).
: ${DIST:="$(lsb_release –short –codename)"}

# Optionally change Debian release states in $DIST to their names.
case "$DIST" in
unstable)
DIST="$UNSTABLE_CODENAME"
;;
testing)
DIST="$TESTING_CODENAME"
;;
stable)
DIST="$STABLE_CODENAME"
;;
esac

# Optionally set the architecture to the host architecture if none set. Note
# that you can set your own default (i.e. ${ARCH:="i386″}).
: ${ARCH:="$(dpkg –print-architecture)"}

NAME="$DIST"
if [ -n "${ARCH}" ]; then
NAME="$NAME-$ARCH"
DEBOOTSTRAPOPTS=("–arch" "$ARCH" "${DEBOOTSTRAPOPTS[@]}")
fi
#BASETGZ="/var/cache/pbuilder/$NAME-base.tgz"
# Optionally, set BASEPATH (and not BASETGZ) if using cowbuilder
BASEPATH="/var/cache/pbuilder/$NAME-base.cow/"
DISTRIBUTION="$DIST"
BUILDRESULT="/var/cache/pbuilder/$NAME/result/"
APTCACHE="/var/cache/pbuilder/$NAME/aptcache/"
BUILDPLACE="/var/cache/pbuilder/build/"

if $(echo ${DEBIAN_SUITES[@]} | grep -q $DIST); then
# Debian configuration
MIRRORSITE="http://$DEBIAN_MIRROR/debian/"
COMPONENTS="main contrib non-free"
DEBOOTSTRAPOPTS=("${DEBOOTSTRAPOPTS[@]}" "–keyring=/usr/share/keyrings/debian-archive-keyring.gpg")
# This is for enabling backports for the Debian stable suite.
if $(echo "$STABLE_CODENAME stable" | grep -q $DIST); then
EXTRAPACKAGES="$EXTRAPACKAGES debian-backports-keyring"
OTHERMIRROR="$OTHERMIRROR | deb http://www.backports.org/debian $STABLE_BACKPORTS_SUITE $COMPONENTS"
fi
elif $(echo ${UBUNTU_SUITES[@]} | grep -q $DIST); then
# Ubuntu configuration
MIRRORSITE="http://$UBUNTU_MIRROR/ubuntu/"
COMPONENTS="main restricted universe multiverse"
DEBOOTSTRAPOPTS=("${DEBOOTSTRAPOPTS[@]}" "–keyring=/usr/share/keyrings/ubuntu-archive-keyring.gpg")
else
echo "Unknown distribution: $DIST"
exit 1
fi

當 ubuntu 或 debian 有新的release時需要修改的變數

UNSTABLE_CODENAME="sid"
TESTING_CODENAME="wheezy"
STABLE_CODENAME="squeeze"
# List of Ubuntu suites. Update these when needed.
UBUNTU_SUITES=("natty" "maverick" "lucid" "karmic" "jaunty" "hardy")

設定 sudo

由於sudo後環境變數並不會延用, 因此我們要設定DIST, ARCH sudo後仍然維持原值, 修改 /etc/sudoers

8 #Defaults->>>>>>env_reset
9 Defaults env_reset,env_keep="DIST ARCH"

使用方式

環境變數 DIST 代表要使用哪個serias, 例如我要建立debian unstable 的chroot

# 若要建立的chroot是debian, 需要安裝keyring
user@host# sudo apt-get install debian-archive-keyring
user@host# sudo DIST=unstable cowbuilder –create
# 產生好的chroot 為 /var/cache/pbuilder/sid-i386-base.cow

編譯 package

# 使用 pdebuild
user@host# sudo DIST=unstable pdebuild
#也可使用 git-buildpackage 編譯 package (個人推薦使用git-buildpackage管理debian package)
user@host$ sudo DIST=unstable git-buildpackage –git-upstream-tag=$ver –git-debian-branch=debian –git-
builder=pdebuilder
# 編好的套件在 /var/cache/pbuilder/sid-i386/result/

另外也可使用環境變數 ARCH 來指定要用amd64 or arm等其他架構,預設值為你電腦的架構

延伸閱讀