| 1 | # empbuilderlib : shell library to support building chroots for Emdebian. |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006-2008 Neil Williams <codehelp@debian.org> |
|---|
| 4 | # Copyright (C) 2001-2007 Junichi Uekawa |
|---|
| 5 | # |
|---|
| 6 | # This package is free software; you can redistribute it and/or modify |
|---|
| 7 | # it under the terms of the GNU General Public License as published by |
|---|
| 8 | # the Free Software Foundation; either version 3 of the License, or |
|---|
| 9 | # (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 18 | # |
|---|
| 19 | |
|---|
| 20 | # This shell library requires perl and pbuilder (which means bash!) ! |
|---|
| 21 | # Draws in POSIX shell functions from emrootfslib to be able to call |
|---|
| 22 | # in functions from first_stage_install within debootstrap. The only |
|---|
| 23 | # reasons to continue putting new functions in here are if: |
|---|
| 24 | # 1. the functions are only useful to create cross-building chroots OR |
|---|
| 25 | # 2. the functions need to call pbuilder code directly. |
|---|
| 26 | # There should be no need to call pbuilder code within scripts that |
|---|
| 27 | # generate a root filesystem. |
|---|
| 28 | |
|---|
| 29 | # Intended solely for use on the build machine. Do not use these functions |
|---|
| 30 | # in second_stage_install ! |
|---|
| 31 | |
|---|
| 32 | . /usr/share/emdebian-tools/emrootfslib |
|---|
| 33 | . /usr/lib/pbuilder/pbuilder-modules |
|---|
| 34 | |
|---|
| 35 | WORKPLACE="${WORKDIR}/pbuilder" |
|---|
| 36 | BASETGZ="${WORKDIR}/emdebian.tgz" |
|---|
| 37 | |
|---|
| 38 | #pbuilder base values |
|---|
| 39 | DEBIAN_BUILDARCH=$ARCH |
|---|
| 40 | # cross-building chroot is same arch as host. |
|---|
| 41 | BUILDRESULT="$WORKPLACE/result/" |
|---|
| 42 | # tidy up // to / |
|---|
| 43 | BUILDRESULT=`echo $BUILDRESULT | tr -s \/` |
|---|
| 44 | APTCACHE="$WORKPLACE/aptcache/" |
|---|
| 45 | # tidy up // to / |
|---|
| 46 | APTCACHE=`echo $APTCACHE | tr -s \/` |
|---|
| 47 | AUTO_DEBSIGN=yes |
|---|
| 48 | APTCACHEHARDLINK="no" |
|---|
| 49 | BUILDPLACE="$WORKPLACE/build" |
|---|
| 50 | |
|---|
| 51 | # the default is to add a PID in the buildplace. |
|---|
| 52 | BASEBUILDPLACE="$BUILDPLACE" |
|---|
| 53 | if [ "${PRESERVE_BUILDPLACE}" != "yes" ]; then |
|---|
| 54 | BUILDPLACE="$BUILDPLACE/$$" |
|---|
| 55 | fi |
|---|
| 56 | |
|---|
| 57 | function extractembuildplace () { |
|---|
| 58 | # after calling this function, umountproc, and cleanbuildplace |
|---|
| 59 | # needs to be called. Please trap it. |
|---|
| 60 | if [ ! \( "${PRESERVE_BUILDPLACE}" = "yes" -a -d "$BUILDPLACE" \) ]; then |
|---|
| 61 | cleanbuildplace |
|---|
| 62 | echo "Building the Emdebian build Environment" |
|---|
| 63 | if ! mkdir -p "$BUILDPLACE"; then |
|---|
| 64 | echo "E: failed to build the directory to chroot" |
|---|
| 65 | exit 1 |
|---|
| 66 | fi |
|---|
| 67 | echo " -> extracting base tarball [${BASETGZ}]" |
|---|
| 68 | if [ ! -f "$BASETGZ" ]; then |
|---|
| 69 | echo "E: failed to find $BASETGZ, have you created your base tarball yet?" |
|---|
| 70 | exit 1 |
|---|
| 71 | fi |
|---|
| 72 | if ! (cd "$BUILDPLACE" && tar xfzp "$BASETGZ"); then |
|---|
| 73 | echo "E: failed to extract $BASETGZ to $BUILDPLACE" |
|---|
| 74 | exit 1 |
|---|
| 75 | fi |
|---|
| 76 | echo " -> creating local configuration" |
|---|
| 77 | if [ $CROSS -a $CROSS != $ARCH ]; then |
|---|
| 78 | hostname -f > "$BUILDPLACE/etc/mailname" |
|---|
| 79 | else |
|---|
| 80 | echo "emdebian-$ARCH" > "$BUILDPLACE/etc/mailname" |
|---|
| 81 | fi |
|---|
| 82 | if [ ! -f "$BUILDPLACE/etc/apt/apt.conf.d/10disablerecommends" ]; then |
|---|
| 83 | disable_apt_recommends |
|---|
| 84 | fi |
|---|
| 85 | fi |
|---|
| 86 | copy_local_configuration |
|---|
| 87 | mountproc |
|---|
| 88 | mkdir -p "$BUILDPLACE/tmp/buildd" |
|---|
| 89 | chmod 1777 "$BUILDPLACE/tmp" |
|---|
| 90 | if [ "$OVERRIDE_APTLINES" = "yes" ]; then |
|---|
| 91 | installaptlines |
|---|
| 92 | fi |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | function autoclean_aptcache() { |
|---|
| 96 | if [ -n "$APTCACHE" ]; then |
|---|
| 97 | echo " -> Cleaning the cached apt archive" |
|---|
| 98 | chroot $BUILDPLACE /usr/bin/apt-get autoclean || true |
|---|
| 99 | find "$APTCACHE/" -maxdepth 1 -name \*.deb | \ |
|---|
| 100 | while read A; do |
|---|
| 101 | if [ ! -f "$BUILDPLACE/var/cache/apt/archives/"$(basename "$A") -a \ |
|---|
| 102 | -f "$A" ]; then |
|---|
| 103 | echo " -> obsolete cache content "$(basename "$A")" removed" |
|---|
| 104 | rm -f "$A" || true |
|---|
| 105 | fi |
|---|
| 106 | done |
|---|
| 107 | fi |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | # intended only for chroots |
|---|
| 111 | function copy_host_configuration () { |
|---|
| 112 | echo " -> copying local configuration" |
|---|
| 113 | if [ $CROSS -a $CROSS != $ARCH ]; then |
|---|
| 114 | for a in hosts hostname resolv.conf; do |
|---|
| 115 | sudo rm -f "$BUILDPLACE/etc/$a" |
|---|
| 116 | if [ ! -f "/etc/$a" ]; then |
|---|
| 117 | echo "E: /etc/$a does not exist, your setup is insane. fix it" >&2 |
|---|
| 118 | sudo cp $( readlink -f "/etc/$a" ) "$BUILDPLACE/etc/$a"; |
|---|
| 119 | fi |
|---|
| 120 | done |
|---|
| 121 | else |
|---|
| 122 | # sandboxes need a different hostname |
|---|
| 123 | mkdir -p "$BUILDPLACE"/etc/ |
|---|
| 124 | cat > "$BUILDPLACE"/etc/hostname << EOF |
|---|
| 125 | emdebian-$ARCH |
|---|
| 126 | EOF |
|---|
| 127 | fi |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | function createemchroot() |
|---|
| 131 | { |
|---|
| 132 | if [ $SUITE == testing ]; then |
|---|
| 133 | BASETGZ="${WORKDIR}/emdebian-testing.tgz" |
|---|
| 134 | SUITE=testing |
|---|
| 135 | . /usr/lib/emdebian-tools/embootstrap --arch $ARCH testing |
|---|
| 136 | echo "Creating an embootstrap testing chroot" |
|---|
| 137 | else |
|---|
| 138 | BASETGZ="$WORKDIR/emdebian.tgz" |
|---|
| 139 | . /usr/lib/emdebian-tools/embootstrap --arch $ARCH |
|---|
| 140 | fi |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | function checkembuilddep () { |
|---|
| 144 | # call satisfydepends |
|---|
| 145 | local BUILDOPT="--binary-all" |
|---|
| 146 | case "${BINARY_ARCH}" in |
|---|
| 147 | yes) BUILDOPT="--binary-arch";; |
|---|
| 148 | *) ;; |
|---|
| 149 | esac |
|---|
| 150 | PBUILDERSATISFYDEPENDSCMD=/usr/lib/pbuilder/pbuilder-satisfydepends |
|---|
| 151 | CONTROL=$BUILDPLACE/trunk/$SVN/debian/control |
|---|
| 152 | # this version parses debian/control, NOT the .dsc |
|---|
| 153 | # so that when Emdebian removes dependencies, the chroot does too. |
|---|
| 154 | echo "Running: $PBUILDERSATISFYDEPENDSCMD --control $CONTROL --chroot $BUILDPLACE ${BUILDOPT}" |
|---|
| 155 | if "$PBUILDERSATISFYDEPENDSCMD" --control "$CONTROL" --chroot "${BUILDPLACE}" "${BUILDOPT}" ; then |
|---|
| 156 | echo " -> installed ${HOST_ARCH} dependencies." |
|---|
| 157 | else |
|---|
| 158 | echo "E: pbuilder-satisfydepends failed." >&2 |
|---|
| 159 | exit 2 |
|---|
| 160 | fi |
|---|
| 161 | # install extra packages to the chroot |
|---|
| 162 | if [ -n "$EXTRAPACKAGES" ]; then |
|---|
| 163 | if echo "DEBIAN_FRONTEND=noninteractive usr/bin/apt-get -y --force-yes install ${EXTRAPACKAGES}" | chroot $BUILDPLACE /bin/sh; then |
|---|
| 164 | : |
|---|
| 165 | else |
|---|
| 166 | # if apt failed (maybe update needs to be run), save so far and exit cleanly |
|---|
| 167 | save_aptcache |
|---|
| 168 | umountproc |
|---|
| 169 | cleanbuildplace |
|---|
| 170 | exit 1; |
|---|
| 171 | fi |
|---|
| 172 | fi |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | # get $ARCH .deb and put into /var/cache/apt/archives to |
|---|
| 176 | # allow these to be saved between builds. |
|---|
| 177 | function save_aptcrosscache() { |
|---|
| 178 | # save the current aptcache archive |
|---|
| 179 | # it is safe to call this function several times. |
|---|
| 180 | local doit |
|---|
| 181 | if [ -n "$APTCACHE" ]; then |
|---|
| 182 | echo "Copying back the cached apt-cross archive contents" |
|---|
| 183 | mkdir -p "$APTCACHE" ; |
|---|
| 184 | if [ "$APTCACHEHARDLINK" = "yes" ]; then |
|---|
| 185 | doit=ln |
|---|
| 186 | else |
|---|
| 187 | doit=cp |
|---|
| 188 | fi |
|---|
| 189 | # apt-cross puts the archives in the chroot / |
|---|
| 190 | find "$BUILDPLACE/" -maxdepth 1 -name \*.deb | \ |
|---|
| 191 | while read A ;do |
|---|
| 192 | if [ ! -f "$APTCACHE/"$(basename "$A") -a -f "$A" ]; then |
|---|
| 193 | echo " -> new cache content "$(basename "$A")" added" |
|---|
| 194 | $doit "$A" "$APTCACHE/" || true |
|---|
| 195 | fi |
|---|
| 196 | done |
|---|
| 197 | fi |
|---|
| 198 | } |
|---|