set -eux
script_dir="$( cd "$( dirname "$0" )" && pwd )"
-workdir="$(mktemp -d -t ${0##*/}.XXXXXXXXXX)"
liveboot_id="$LIVEBOOT_BUILD_NUMBER"
-assembledir="$workdir/liveboot-$liveboot_id-kernel-dbg"
+session_id=
+cleanup() {
+ if test -n "$session_id"; then
+ schroot -e -c $session_id
+ fi
+}
+
+run_in_chroot() {
+ schroot -r -c $session_id -u root -d $workdir -- "$@"
+}
cidb_query() {
local qry="$1"
exit 1
}
-mkdir -p "$assembledir"
-cd "$workdir"
+# Create a chroot session
+session_id=$(schroot -u root -c pb-${DIST} --begin-session)
+
+# trap failures
+trap cleanup INT TERM EXIT ERR KILL STOP HUP ABRT PIPE CHLD
+
+chroot_dir=$(schroot -i -c session:$session_id | sed -n 's/^ Path[ ]*\(.*\)$/\1/p')
+workdir="$(schroot -r -c $session_id -u root -d / -- mktemp -d -t ${0##*/}.XXXXXXXXXX)"
+
+assembledir="$workdir/liveboot-$liveboot_id-kernel-dbg"
+run_in_chroot mkdir -p "$assembledir"
+
+# Extent sources.list in chroot
+run_in_chroot bash -c "cat > /etc/apt/sources.list.d/additional.list" <<EOF
+deb http://repo.pb.local/pb-debian-dev squeeze-dev main contrib non-free
+deb http://repo.pb.local/pb-debian-dev squeeze-feature-autotest main contrib non-free
+EOF
-sudo aptitude update
+run_in_chroot apt-get install aptitude
+run_in_chroot aptitude update
# assemble ethoip6 debug directory
-aptitude download profitbricks-linux-source-pserver=$kernel_ver
-dpkg -x profitbricks-linux-source-pserver_$kernel_ver*.deb kernel_source
-mv kernel_source/usr/src/linux-*-pserver $assembledir/
+run_in_chroot aptitude download profitbricks-linux-source-pserver=$kernel_ver
+run_in_chroot dpkg -x profitbricks-linux-source-pserver_$kernel_ver*.deb kernel_source
+run_in_chroot mv kernel_source/usr/src/linux-*-pserver $assembledir/
-aptitude download profitbricks-linux-image-pserver-dbg=$kernel_ver
-dpkg -x profitbricks-linux-image-pserver-dbg_$kernel_ver*.deb kernel_dbg
-mkdir -p $assembledir/linux-$kernel_ver-pserver-dbg
-mv kernel_dbg/usr/lib/debug/vmlinux* kernel_dbg/usr/lib/debug/lib/modules/*-pserver $assembledir/linux-$kernel_ver-pserver-dbg
+run_in_chroot aptitude download profitbricks-linux-image-pserver-dbg=$kernel_ver
+run_in_chroot dpkg -x profitbricks-linux-image-pserver-dbg_$kernel_ver*.deb kernel_dbg
+run_in_chroot mkdir -p $assembledir/linux-$kernel_ver-pserver-dbg
+run_in_chroot mv kernel_dbg/usr/lib/debug/vmlinux* kernel_dbg/usr/lib/debug/lib/modules/*-pserver $assembledir/linux-$kernel_ver-pserver-dbg
# assemble ethoip6 debug directory
-aptitude download ethoip6-sources=$ethoip6_ver
-dpkg -x ethoip6-sources_$ethoip6_ver*.deb ethoip6_src
-mv ethoip6_src/usr/src/ethoip6* $assembledir/
+run_in_chroot aptitude download ethoip6-sources=$ethoip6_ver
+run_in_chroot dpkg -x ethoip6-sources_$ethoip6_ver*.deb ethoip6_src
+run_in_chroot mv ethoip6_src/usr/src/ethoip6* $assembledir/
-aptitude download profitbricks-ethoip6-modules-pserver-dbg=$ethoip6_ver
-dpkg -x profitbricks-ethoip6-modules-pserver-dbg_$ethoip6_ver*.deb ethoip6_dbg
-mkdir -p $assembledir/ethoip6-$ethoip6_ver-dbg
-find ethoip6_dbg -name "*.ko" -exec mv {} $assembledir/ethoip6-$ethoip6_ver-dbg/ \;
+run_in_chroot aptitude download profitbricks-ethoip6-modules-pserver-dbg=$ethoip6_ver
+run_in_chroot dpkg -x profitbricks-ethoip6-modules-pserver-dbg_$ethoip6_ver*.deb ethoip6_dbg
+run_in_chroot mkdir -p $assembledir/ethoip6-$ethoip6_ver-dbg
+run_in_chroot find ethoip6_dbg -name "*.ko" -exec mv {} $assembledir/ethoip6-$ethoip6_ver-dbg/ \;
-cp "$script_dir/README" $assembledir/
-tar --owner=root -Ipixz -cf "$resultfile" liveboot-$liveboot_id-kernel-dbg -C $workdir
+run_in_chroot tee $assembledir/README < $script_dir/README
+tar --owner=root -Ipixz -cf "$resultfile" liveboot-$liveboot_id-kernel-dbg -C $chroot_dir/$workdir
-rm -rf "$workdir"
+cleanup
+session_id=
+# remove trap again
+trap - INT TERM EXIT