Skip to content

Instantly share code, notes, and snippets.

@smoser
Last active February 14, 2024 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smoser/14df5f0cd621e10d2282d7c90345e322 to your computer and use it in GitHub Desktop.
Save smoser/14df5f0cd621e10d2282d7c90345e322 to your computer and use it in GitHub Desktop.
custom sbuild / schroot setup.
#!/bin/sh
# https://gist.github.com/smoser/14df5f0cd621e10d2282d7c90345e322
# This is /etc/schroot/setup.d/91smoser
# I use it to apply local updates to schroots.
# make sure it is executable (chmod +x).
# Things it does:
# a.) sets proxy inside. If apt proxy is configured outside, it will
# apply that inside.
# b.) uses a portion of 'apt-go-fast'
# https://gist.github.com/smoser/5823699/
# that installs eatmydata and uses eatmydata for all apt operations.
# c.) stops apt from downloading languages or translations.
# d.) disables deb-src lines so apt does not download them.
set -e
msg() { echo "$@"; }
. "$SETUP_DATA_DIR/common-data"
. "$SETUP_DATA_DIR/common-functions"
if [ -f "$SETUP_DATA_DIR/common-config" ]; then
. "$SETUP_DATA_DIR/common-config"
fi
#msg "STAGE=$STAGE CHROOT_SESSION_PURGE=${CHROOT_SESSION_PURGE}"
[ "$STAGE" = "setup-start" -o "$STAGE" = "setup-recover" ] || exit 0
[ -n "${CHROOT_PATH}" -a "${CHROOT_PATH}" != "/" ] ||
{ echo "FAIL: CHROOT_PATH NOT SET"; exit 1; }
aptconfd="${CHROOT_PATH}/etc/apt/apt.conf.d"
pfile="${aptconfd}/99sbuild-proxy"
[ ! -e "$pfile" ] || rm -f "$pfile"
for s in Acquire::HTTP:proxy Acquire::HTTPS:proxy ; do
out=$(apt-config shell x "$s") ||
fatal "failed to read apt-config shell x $s"
val=$(sh -c 'eval $1 && echo $x' -- "$out") ||
fatal "evaluating output for apt-config '$s' failed"
[ -n "$val" ] || continue
msg "copying $s host apt to ${pfile#${CHROOT_PATH}}"
printf '# from host apt config\n%s "%s";\n' "$s" "$val" >> "$pfile"
done
sources="${CHROOT_PATH}/etc/apt/sources.list"
if grep -q "^deb-src" "$sources"; then
msg "disabling deb-src lines in ${sources#${CHROOT_PATH%/}}"
sed -i -e "s,^deb-src,#deb-src," "$sources"
fi
f="${aptconfd}/99notranslations"
if [ ! -f "$f" ]; then
msg "disabling languages in ${f#${CHROOT_PATH%/}}"
echo 'Acquire::Languages "none";' > "$f"
rm -f "${CHROOT_PATH}/var/lib/apt/lists"/*Translation*
fi
apt_get="${CHROOT_PATH}/usr/bin/apt-get"
if [ ! -e "$apt_get.distrib" ]; then
msg "diverting apt-get"
chroot "${CHROOT_PATH}" \
dpkg-divert --local --rename --add /usr/bin/apt-get
msg "apt-go-fasting"
cat > "${apt_get}" <<"EOF"
#!/bin/sh
a=" $* "
emd="eatmydata"
if ! command -v eatmydata >/dev/null 2>&1; then
emd=""
if [ "$(id -u)" = "0" ] &&
[ "${a#* install }" != "$a" -o "${a#* upgrade }" != "$a" -o \
"${a#* dist-upgrade }" != "$a" ]; then
# we are root, this is install, so install emd
echo "=== first installing eatmydata ===" 1>&2
out=$(DEBIAN_FRONTEND=noninteractive "$0.distrib" install \
--quiet --assume-yes eatmydata 2>&1)
ret=$?
if [ $ret -ne 0 ]; then
echo "FAILED: $out" 1>&2;
echo "failed to install libeatmydata."
exit $ret
fi
emd="eatmydata"
fi
fi 1>&2 </dev/null
exec $emd "$0.distrib" "$@"
EOF
chmod 755 "${apt_get}"
fi

schroot and sbuild setup

new sbuild creation

For setting up sbuild with schroot, I'm now using the ubuntu package sbuild-launchpad-chroot Its quite easy to set up a new sbuild or schroot that way.

Install the package

$ apt-get install sbuild-launchpad-chroot

Build yourself sbuild chroots.

$ rel="cosmic"; arch="amd64"
$ sudo sbuild-launchpad-chroot create \
     --architecture="$arch" "--name=$rel-$arch" "--series=$rel"

Build!

$ sbuild --resolve-alternatives --dist=cosmic --arch=amd64 --arch-all my.dsc

sbuild-it

I find that sbuild cli can be painful. I've written sbuild-it to do the right thing. You feed it a dsc or a changes file and it figures out where to build it and does that.

custom schroot changes

I use the 91smoser-schroot-setup script here to apply my local customizations to the schroot. This results in no local maintenance, as the script does it all on each new chroot.

schroots for user with home mounted.

I add a 'user-$rel-$arch' schroot that has /home mounted. Note that my /etc/schroot/default/fstab file has the /home entry with rbind instead of bind. That is due to bug 769595 related to private home.

$ sbconf=/etc/schroot/chroot.d/$rel-$arch
$ userconf="/etc/schroot/chroot.d/user-$rel-$arch"
$ sudo cp "$sbconf" "$userconf"
$ sudo sed -i \
   -e "s,^\[$rel-$arch\],[user-$rel-$arch]," \
   -e "s,^profile=sbuild,profile=default," \
   -e "s,^description=,description=schroot with /home based on $rel-$arch," \
   -e "/^aliases=/d" \
   "$userconf"
#!/bin/bash
VERBOSITY=0
error() { echo "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
Usage() {
cat <<EOF
Usage: ${0##*/} [ options ] dsc [arch [ release ] ]
dsc and is required, arch and release are optional with defaults
options:
-h | --help display this message
--dry-run only report what would be done
--no-arch-all do not pass '--arch-all'
EOF
}
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
debug() {
local level=${1}; shift;
[ "${level}" -gt "${VERBOSITY}" ] && return
error "${@}"
}
rel_from_changes() {
local cfile="$1" out=""
out=$(awk '$1 == "Distribution:" { print $2 }' "$cfile") &&
[ "$(echo $out | tr '[A-Z]' '[a-z]')" != "unreleased" ] &&
_RET="${out%-proposed}" || return 1
}
short_opts="hv"
long_opts="help,dry-run,chroot:,no-arch-all,verbose"
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
bad_Usage
def_rel=""
uname_m=$(uname -m)
case "${uname_m}" in
i?86) arch="i386";;
x86_64) arch="amd64";;
ppc64le) arch="ppc64el";;
*) arch="$uname_m";;
esac
dsc=""
dry_run=0
arch_all="--arch-all"
rels=" $(ubuntu-distro-info --all | tr '\n' ' ') "
ifile=""
schanges=""
sbuild="sbuild"
chroot=""
while [ $# -ne 0 ]; do
cur=${1}; next=${2};
case "$cur" in
-h|--help) Usage ; exit 0;;
--dry-run) dry_run=1;;
--no-arch-all) arch_all="";;
--chroot) chroot=$next; shift;;
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
--) shift; break;;
esac
shift;
done
for arg in "$@"; do
if [ "${rels#* ${arg} }" != "${rels}" ]; then
rel="$arg";
continue
fi
if command -v "sbuild-$arg"; then
# handle string like 'icehouse' that has a 'sbuild-icehouse'
sbuild="sbuild-$arg"
continue
fi
case "$arg" in
*.dsc)
ifile="$arg"
dsc="$arg"
;;
*_source.changes)
[ -f "$arg" ] || fail "changes file '$arg' does not exist"
ifile="$arg"
schanges="$arg"
tmp="${arg%_source.changes}.dsc"
if [ -z "$dsc" -a -f "$tmp" ]; then
error "changed ${arg} to ${tmp} as a dsc"
dsc="$tmp"
fi
;;
i386|amd64) arch="$arg";;
*) fail "confused by $arg";;
esac
done
[ -n "$dsc" ] || { Usage 1>&2; fail "must give dsc"; }
[ -f "$dsc" ] || fail "$dsc: not a file"
if [ -z "$schanges" -a -f "${ifile%.dsc}_source.changes" ]; then
schanges="${ifile%.dsc}_source.changes"
fi
ubuntu_devel=$(ubuntu-distro-info --devel)
if [ -z "$rel" -a -f "$schanges" ]; then
rel_from_changes "$schanges" && def_rel="${_RET}" &&
error "set default release to $def_rel from changes file $schanges"
fi
if [ -z "$def_rel" ]; then
def_rel=${ubuntu_devel}
fi
rel=${rel:-${def_rel}}
arch=${arch:-amd64}
extra=""
if [ "${rel#*-}" != "$rel" ] &&
distro-info --all | grep -q "^${rel%-*}"; then
extra=${rel#*-}
rel=${rel%-*}
error "set rel to $rel, extra='$extra'"
fi
orig_src=$(awk \
'$1 ~ /^[a-f0-9]{32}$/ && $3 ~ /.orig.tar.gz$/ {print $3}' \
"$dsc")
dsc_d=$(dirname "$dsc")
if [ -f "$dsc_d/$orig_src" -o -z "$orig_src" ]; then
:
elif [ -f "$dsc_d/build-area/$orig_src" ]; then
ln -f "build-area/$orig_src" "$dsc_d" ||
fail "failed to symlink to build-area for $orig_src";
else
dlbase="https://launchpad.net/ubuntu/+archive/primary/+files/"
wget "$dlbase/${orig_src}" -O "$dsc_d/$orig_src.part" &&
mv "$dsc_d/$orig_src.part" "$dsc_d/$orig_src" ||
fail "failed to download from $dlbase/$orig_src"
fi
case "$rel" in
unstable)
rel=${ubuntu_devel}
[ -n "$rel" ] || fail "failed to get release to build with from $rel"
debug 1 "building 'unstable' on ubuntu-devel ($rel)"
;;
esac
debug 0 "$sbuild --dist=${rel} --arch=${arch}${chroot:+ --chroot=$chroot} ${arch_all:+ ${arch_all}} $dsc"
[ $dry_run -eq 0 ] || exit 0
# --resolve-alternatives is used by the buildd systems
exec $sbuild --resolve-alternatives ${chroot:+"--chroot=${chroot}"} "--dist=${rel}" "--arch=${arch}" ${arch_all} "$dsc"
# vi: ts=4 expandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment