Debian Codegraph @

pbuilder-modules

raw
1 #! /bin/bash
2 # pbuilder -- personal Debian package builder
3 # Copyright © 2001-2009 Junichi Uekawa <[email protected]>
4 # 2015-2017 Mattia Rizzolo <[email protected]>
5 #
6 # This program 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 2 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, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 ###############################################################################
20
21 # common modules for pbuilder.
22
23 hooks=tmp/hooks
24 DEB_BUILD_ARCH_OS=$(dpkg-architecture -qDEB_BUILD_ARCH_OS)
25 SELINUX="$(grep -m 1 ^selinuxfs /proc/mounts | cut -d ' ' -f 2 || : )"
26
27 function showhelp () {
28 cat <<EOF
29 pbuilder - a personal builder
30 Copyright 2001-2007 Junichi Uekawa
31 Distributed under GNU Public License version 2 or later
32
33 pbuilder [operation] [pbuilder-options]
34 pdebuild [pdebuild-options] -- [pbuilder-options]
35
36 command lines operations:
37 pbuilder create [--basetgz base.tgz-path] [--distribution sid|experimental|...]
38 Creates a base.tgz
39
40 pbuilder update [--basetgz base.tgz-path] [--distribution sid|experimental|...]
41 Updates a base.tgz
42
43 pbuilder build [--basetgz base.tgz-path] pbuilder_2.2.0-1.dsc
44 Builds using the base.tgz. Requires a .dsc filename
45
46 pbuilder clean
47 Cleans the temporal build directory.
48
49 pbuilder login
50 Opens a shell into a throw-away chroot.
51
52 pbuilder execute -- [script] [script options]
53 Logs into the build environment and executes "script". The script is copied
54 into the chroot and invoked.
55
56 pbuilder dumpconfig
57 Dumps configuration information to stdout for debugging.
58
59 pbuilder main options:
60 --basetgz [base.tgz location]
61 --buildplace [location of build]
62 --mirror [mirror location]
63 --othermirror [other mirrors location in apt deb-line format, delimited with | signs]
64 --http-proxy [proxy]
65 --distribution [distribution (sid|experimental|...)]
66 --architecture [architecture]
67 --components [components]
68 --buildresult [location-to-copy-build-result]
69 --aptcache [location of retrieved package files]
70 --extrapackages [packages-to-add on pbuilder create]
71 --configfile [configuration file to load]
72 --hookdir [hook directory]
73 --debbuildopts [dpkg-buildpackage options]
74 --logfile [filename to output log]
75 --aptconfdir [overriding apt config dir]
76 --timeout [timeout time]
77 --override-config
78 --binary-arch
79 --preserve-buildplace
80 --bindmounts [bind-mount-point]
81 --debug
82 --debootstrapopts [debootstrap options]
83 --save-after-login/--save-after-exec
84
85 pdebuild-specific pbuilder options:
86 --auto-debsign
87 --debsign-k [keyid]
88 --pbuilder [builder]
89
90 For the full documentation see the manual pages pbuilder(8) and pbuilderrc(5).
91 EOF
92 }
93
94 # Log a message
95 # message is of a format
96 # E: error message
97 # W: warning message
98 # I: informational message
99 _log() {
100 set -u
101 term_supports_colors # defines USECOLORS if not already set
102 local color="$1" ; shift
103 local red=$'\033[0;31m'
104 local yellow=$'\033[1;33m'
105 local blue=$'\033[0;34m'
106 local reset=$'\033[0m'
107 case "$USECOLORS" in
108 yes)
109 printf "%s%s%s\n" "${!color}" "${*}" "${reset}"
110 ;;
111 no)
112 printf "%s\n" "${*}"
113 ;;
114 *)
115 printf "malformed value of USECOLORS: [%s]\n" "$USECOLORS" >&2
116 exit 1
117 ;;
118 esac
119 set +u
120 }
121 function log() {
122 set +x
123 case "$*" in
124 "E: "*)
125 _log 'red' "$*" >&2
126 ;;
127 "W: "*)
128 _log 'yellow' "$*" >&2
129 ;;
130 "I: "*)
131 _log 'reset' "$*"
132 ;;
133 "D: "*)
134 _log 'blue' "$*"
135 ;;
136 *)
137 echo "malformed log message: $*" >&2
138 exit 1
139 ;;
140 esac
141 if [ "$PBUILDER_DEBUGMODE" = "yes" ]; then
142 set -x
143 fi
144 }
145
146 log.e() {
147 case "${LOGLEVEL-I}" in
148 D|I|W|E) log "E: $*" ;;
149 esac
150 }
151 log.w() {
152 case "${LOGLEVEL-I}" in
153 D|I|W) log "W: $*" ;;
154 esac
155 }
156 log.i() {
157 case "${LOGLEVEL-I}" in
158 D|I) log "I: $*" ;;
159 esac
160 }
161 log.d() {
162 case "${LOGLEVEL-I}" in
163 D) log "D: $*" ;;
164 esac
165 }
166
167 # call like `_contains $list $value`, returns 0 if $value is in $list, 1 otherwise
168 _contains() {
169 local n=$#
170 local value=${!n}
171 for ((i=1; i < $#; i++)) {
172 if [ "${!i}" == "${value}" ]; then
173 return 0
174 fi
175 }
176 return 1
177 }
178
179 term_supports_colors() {
180 if [ -z ${USECOLORS:-} ] || [ "$USECOLORS" = "auto" ]; then
181 local tputout="$(tput colors 2> /dev/null)"
182 [ ${tputout:-0} -ge 8 ] && USECOLORS=yes || USECOLORS=no
183 fi
184 }
185
186 # test whether a directory is empty
187 # fails if "$*" exists but isn't a directory
188 # fails and outputs garbage if "$*" doesn't actually exist
189 is_empty_dir() {
190 return "$(find "$*" -maxdepth 0 -type d -empty -printf 0 -o -printf 1)"
191 }
192
193 # $1 → binary package name
194 # $2 → minimum version
195 # success if pkg has an higher version, fail otherwise
196 package_version_is_older_than() {
197 dpkg --compare-versions "$($CHROOTEXEC dpkg-query -W --showformat='${Version}' "$1")" lt "$2"
198 return $?
199 }
200
201 # sanity checks to ensure mountpoint $1 is truly unmounted in $BUILDPLACE
202 # (fails relatively often to ensure we don't "rm -rf" a bind mount)
203 function seems_truly_unmounted() {
204 local mountpoint
205 mountpoint="$1"
206 if ! [ -e "$BUILDPLACE/$mountpoint" ]; then
207 log.w "$mountpoint doesn't exist"
208 return 1
209 fi
210 if ! [ -d "$BUILDPLACE/$mountpoint" ]; then
211 log.w "$mountpoint isn't a directory"
212 return 1
213 fi
214 if [ -r "$BUILDPLACE/proc/mounts" ] && \
215 grep -q "^[^ ]* $mountpoint " "$BUILDPLACE/proc/mounts"; then
216 log.w "$mountpoint is mounted according to build place's /proc/mounts"
217 return 1
218 fi
219 if [ -r "/proc/mounts" ] && \
220 grep -q "^[^ ]* $BUILDPLACE/$mountpoint " "/proc/mounts"; then
221 log.w "$mountpoint is mounted according to system's /proc/mounts"
222 return 1
223 fi
224 if ! is_empty_dir "$BUILDPLACE/$mountpoint"; then
225 log.w "$mountpoint not empty"
226 return 1
227 fi
228 return 0
229 }
230
231 function umount_one () {
232 if [ "${IGNORE_UMOUNT}" = "yes" ]; then
233 # support ignore umount option.
234 log.i "ignoring umount of $1 filesystem"
235 return
236 fi
237 log.i "unmounting $1 filesystem"
238 local UMOUNT_OUTPUT
239 if ! UMOUNT_OUTPUT="$(LC_ALL=C umount "$BUILDPLACE/$1" 2>&1)"; then
240 log.w "Could not unmount $1: $UMOUNT_OUTPUT"
241 local ignore_umount_error="no"
242 case $UMOUNT_OUTPUT in
243 "umount:"*": not found"*|"umount:"*": not mounted"*)
244 # run an additional set of sanity checks
245 if seems_truly_unmounted "$1"; then
246 ignore_umount_error="yes"
247 else
248 log.w "Tried ignoring error in unmount, but sanity check failed: $1 might still be mounted"
249 fi
250 ;;
251 *)
252 :
253 ;;
254 esac
255 if [ "$ignore_umount_error" != "yes" ]; then
256 log.w "Retrying to unmount $1 in 5s"
257 sleep 5s
258 while ! umount "$BUILDPLACE/$1"; do
259 sleep 5s
260 cat <<EOF
261
262 Could not unmount $1, some programs might
263 still be using files in /proc (klogd?).
264 Please check and kill these processes manually
265 so that I can unmount $1. Last umount error was:
266 $UMOUNT_OUTPUT
267
268 EOF
269 chroot "$BUILDPLACE" bin/sh
270 done
271 else
272 log.w "Ignored error in unmount"
273 fi
274 fi
275 }
276
277 function umountproc () {
278 # push arguments on a stack to reverse direction.
279 local reversed
280 reversed=
281 for mnt in $BINDMOUNTS; do
282 reversed="$mnt $reversed"
283 done
284 for mnt in $reversed; do
285 umount_one "${mnt#*:}"
286 done
287 if [ -n "$SELINUX" ]; then
288 umount_one "$SELINUX"
289 fi
290 if [ "$DEB_BUILD_ARCH_OS" = "linux" ] && [ "$USEDEVPTS" = "yes" ]; then
291 if mount | grep -q -F " $(readlink -f "$BUILDPLACE")/dev/console"; then
292 umount_one "dev/console"
293 fi
294 if mount | grep -q -F " $(readlink -f "$BUILDPLACE")/dev/ptmx"; then
295 umount_one "dev/ptmx"
296 fi
297 umount_one "dev/pts"
298 fi
299 if [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ] || [ "$USEDEVFS" = "yes" ]; then
300 if [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ]; then
301 umount_one "dev/fd"
302 fi
303 umount_one "dev"
304 fi
305 if [ "$USESHM" = "yes" ]; then
306 if [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ]; then
307 umount_one "run/shm"
308 elif [ "$DEB_BUILD_ARCH_OS" != "hurd" ]; then
309 umount_one "dev/shm"
310 fi
311 fi
312 if [ "$USEPROC" = "yes" ]; then
313 if [ "$DEB_BUILD_ARCH_OS" = "linux" ] && [ -e "$BUILDPLACE/proc/sys/fs/binfmt_misc/status" ]; then
314 umount_one "proc/sys/fs/binfmt_misc"
315 fi
316 umount_one "proc"
317 fi
318 if [ "$DEB_BUILD_ARCH_OS" != "hurd" ] && [ "$USESYSFS" = "yes" ]; then
319 umount_one "sys"
320 fi
321 if [ "$DEB_BUILD_ARCH_OS" = "hurd" ]; then
322 umount_one "servers"
323 umount_one "dev"
324
325 # Workaround to remove chroot on Hurd: once /dev firmlink is
326 # removed, chroot removal either gets stuck or fails by removing
327 # some devices.
328 for dev in "$BUILDPLACE"/dev/* "$BUILDPLACE"/servers/socket/* "$BUILDPLACE"/servers/bus/*; do
329 settrans -fg "$dev"
330 done
331
332 # When running with --no-targz, leave a valid translator for
333 # /servers/socket/1 so pipes can be used within the chroot.
334 if [ "${INTERNAL_BUILD_UML}" = "yes" ]; then
335 settrans -ck "$BUILDPLACE"/servers/socket/1 /hurd/pflocal
336 fi
337 fi
338 }
339
340
341 # Mount /proc /dev/pts /dev and bind-mount points
342 # Also create a policy-rc.d script if it doesn't already exist.
343 function mountproc () {
344 local -a mounted
345 local mnt mntpoint create_mntpoint_cmd
346 local primary_shm secondary_shm
347 if [ "$USEPROC" = "yes" ]; then
348 log.i "mounting /proc filesystem"
349 mkdir -p "$BUILDPLACE/proc"
350 case "$DEB_BUILD_ARCH_OS" in
351 kfreebsd)
352 PROCFS="linprocfs"
353 ;;
354 hurd)
355 settrans -fg "$BUILDPLACE/proc"
356 PROCFS="firmlink"
357 ;;
358 *)
359 PROCFS="proc"
360 ;;
361 esac
362 mount -t "$PROCFS" /proc "$BUILDPLACE/proc"
363 ln -s ../proc/mounts "$BUILDPLACE/etc/mtab" 2> /dev/null || true
364 mounted[${#mounted[@]}]="$BUILDPLACE/proc"
365 fi
366 if [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ] || [ "$USEDEVFS" = "yes" ]; then
367 log.i "mounting /dev filesystem"
368 mkdir -p "$BUILDPLACE/dev" || true
369 mount -t devfs /dev "$BUILDPLACE/dev"
370 mounted[${#mounted[@]}]="$BUILDPLACE/dev"
371 if [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ]; then
372 log.i "applying devfs rules"
373 egrep -v '^[[:space:]]*(#|$)' "$BUILDPLACE"/etc/devfs.d/*.rules | devfs -m "$BUILDPLACE"/dev rule apply -
374 log.i "mounting /dev/fd filesystem"
375 mkdir -p "$BUILDPLACE/dev/fd" || true
376 mount -t fdescfs fdesc "$BUILDPLACE/dev/fd"
377 mounted[${#mounted[@]}]="$BUILDPLACE/dev/fd"
378 fi
379 fi
380 if [ "$DEB_BUILD_ARCH_OS" != "hurd" ] && [ "$USESYSFS" = "yes" ]; then
381 log.i "mounting /sys filesystem"
382 mkdir -p "$BUILDPLACE/sys" || true
383 case "$DEB_BUILD_ARCH_OS" in
384 kfreebsd)
385 mount -t linsysfs linsysfs "$BUILDPLACE/sys"
386 ;;
387 linux)
388 mount -t sysfs -o nosuid,nodev,noexec sysfs "$BUILDPLACE/sys"
389 ;;
390 *)
391 log.e "Unknown DEB_BUILD_ARCH_OS, can't mount /sys"
392 exit 1
393 ;;
394 esac
395 mounted[${#mounted[@]}]="$BUILDPLACE/sys"
396 fi
397 if [ "$USESHM" = "yes" ]; then
398 log.i "creating /{dev,run}/shm"
399 if [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ]; then
400 primary_shm="run/shm"
401 secondary_shm="dev/shm"
402 else
403 primary_shm="dev/shm"
404 secondary_shm="run/shm"
405 # old releases might not have /run. We create it nonetheless, can't harm
406 mkdir -p "$BUILDPLACE/run"
407 fi
408 rm -df "$BUILDPLACE/$primary_shm" "$BUILDPLACE/$secondary_shm"
409 mkdir -p "$BUILDPLACE/$primary_shm"
410 chmod 1777 "$BUILDPLACE/$primary_shm"
411 ln -s "/$primary_shm" "$BUILDPLACE/$secondary_shm"
412 # /dev gets bind-mounted on hurd. In theory this is before that, but
413 # debootstrap will already have mounted it during chroot creation.
414 # This seems to cause /hurd/tmpfs to crash, but we can just use the
415 # host's /dev/shm.
416 if [ "$DEB_BUILD_ARCH_OS" != "hurd" ]; then
417 mount -t tmpfs tmpfs "$BUILDPLACE/$primary_shm"
418 mounted[${#mounted[@]}]="$BUILDPLACE/$primary_shm"
419 fi
420 fi
421 if [ "$DEB_BUILD_ARCH_OS" = "linux" ] && [ "$USEDEVPTS" = "yes" ]; then
422 log.i "mounting /dev/pts filesystem"
423 mkdir -p "$BUILDPLACE/dev/pts" || true
424 TTYGRP=5
425 TTYMODE=620
426 [ -f /etc/default/devpts ] && . /etc/default/devpts
427 # Even wheezy has CONFIG_DEVPTS_MULTIPLE_INSTANCES=y, so no need to
428 # fall back to the old method with just /dev/ptmx.
429 mount -t devpts devpts "$BUILDPLACE/dev/pts" -o newinstance,noexec,nosuid,gid=$TTYGRP,mode=$TTYMODE,ptmxmode=0666
430 mounted[${#mounted[@]}]="$BUILDPLACE/dev/pts"
431
432 # If /dev/ptmx is a symlink, it should be /dev/ptmx -> pts/ptmx
433 # and since ptmxmode is 0666, /dev/pts/ptmx will have mode 0666.
434 # However, if it is not a symlink, then any PTYs created inside the
435 # chroot will not be visible. We could delete /dev/ptmx and replace
436 # it with a symlink, but it's safer just to bind-mount it.
437 # This is based on the recommendations in
438 # Documentation/filesystems/devpts.txt
439 # (linux.git commit 784c4d8b1b1e66f8c45e8b889613f4982f525b2b)
440 if [ ! -L "$BUILDPLACE/dev/ptmx" ]; then
441 log.i "redirecting /dev/ptmx to /dev/pts/ptmx"
442 mount --bind "$BUILDPLACE/dev/pts/ptmx" "$BUILDPLACE/dev/ptmx"
443 mounted[${#mounted[@]}]="$BUILDPLACE/dev/ptmx"
444 fi
445
446 # If there is a controlling TTY, it must be available to the chroot.
447 # The standard practice (at least by lxc and systemd-nspawn) is to
448 # bind-mount it onto /dev/console.
449 if CURRENT_TTY="$(tty)"; then
450 # We need /dev/console to exist to be able to bind-mount onto it.
451 # Might as well make the proper device node rather than a file.
452 if [ ! -e "$BUILDPLACE/dev/console" ]; then
453 log.i "creating /dev/console"
454 mknod -m 600 "$BUILDPLACE/dev/console" c 5 1
455 fi
456 log.i "mounting $CURRENT_TTY over /dev/console"
457 mount --bind "$CURRENT_TTY" "$BUILDPLACE/dev/console"
458 fi
459 fi
460 if [ -n "$SELINUX" ]; then
461 log.i "mounting selinux filesystem"
462 mkdir -p "$BUILDPLACE/$SELINUX"
463 mount --bind "$SELINUX" "$BUILDPLACE/$SELINUX"
464 mount -o remount,ro,bind "$BUILDPLACE/$SELINUX"
465 mounted[${#mounted[@]}]="$BUILDPLACE/$SELINUX"
466 fi
467 if [ "$DEB_BUILD_ARCH_OS" = "hurd" ]; then
468 # /dev and /servers might have already been mounted at
469 # debootstrap chroot creation
470 mount -t firmlink /dev "$BUILDPLACE/dev" || true
471 mounted[${#mounted[@]}]="$BUILDPLACE/dev"
472 mount -t firmlink /servers "$BUILDPLACE/servers" || true
473 mounted[${#mounted[@]}]="$BUILDPLACE/servers"
474 fi
475 MOUNTPARAMS="-obind"
476 [ "$DEB_BUILD_ARCH_OS" = "linux" ] && MOUNTPARAMS="${MOUNTPARAMS} --make-private"
477 [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ] && MOUNTPARAMS="-t nullfs"
478 for mnt in $BINDMOUNTS; do
479 mntpoint=${mnt#*:}
480 mnt=${mnt%%:*}
481 if [ "$mnt" = "$mntpoint" ]; then
482 log.i "Mounting $mnt"
483 else
484 log.i "Mounting $mnt to $mntpoint"
485 fi
486 if [ -d "$mnt" ]; then
487 create_mntpoint_cmd="mkdir -p"
488 else
489 create_mntpoint_cmd="touch"
490 fi
491 if $create_mntpoint_cmd "$BUILDPLACE/$mntpoint" &&
492 mount $MOUNTPARAMS "$mnt" "$BUILDPLACE/$mntpoint"; then
493 # successful.
494 mounted[${#mounted[@]}]="$BUILDPLACE/$mnt"
495 else
496 # this part of code is the only part which is supposed to fail.
497 # When unsuccessful, backtrack / umount and abort.
498 if [ -n "${mounted[*]}" ]; then
499 log.i "error recovery: umount successfully mounted mount-points: ${mounted[@]}"
500 for umnt in "${mounted[@]}"; do
501 log.i "umounting $umnt"
502 umount "$umnt"
503 done
504 fi
505 exit 1
506 fi
507 done
508 if [ -f "$BUILDPLACE/usr/sbin/policy-rc.d" ]; then
509 log.i "policy-rc.d already exists"
510 else
511 log.i "installing dummy policy-rc.d"
512 echo "\
513 #!/bin/sh
514
515 while true; do
516 case \"\$1\" in
517 -*) shift ;;
518 makedev) exit 0;;
519 x11-common) exit 0;;
520 *) exit 101;;
521 esac
522 done
523
524 " > "$BUILDPLACE/usr/sbin/policy-rc.d"
525 chmod a+x "$BUILDPLACE/usr/sbin/policy-rc.d"
526 fi
527 }
528
529 ## function to clean subdirs, use instead of rm -r
530 function clean_subdirectories () {
531 if [ -z "$1" ]; then
532 log.e "Fatal internal error in clean_subdirectories"
533 exit 1;
534 fi
535 if [ ! -d "$1" ]; then
536 log.w "directory $1 does not exist in clean_subdirectories"
537 return;
538 fi
539 log.i "removing directory $1 and its subdirectories"
540 find "$1" -xdev \( \! -type d \) -print0 |xargs -0 rm -f
541 find "$1" -xdev -depth -type d -print0 | \
542 (xargs -0 rmdir || true)
543 }
544
545 function cleanbuildplace () {
546 if [ "$?" -ne 0 ]; then
547 log.w "Aborting with an error";
548 fi
549 unloadhooks
550 if [ "${INTERNAL_BUILD_UML}" != "yes" ]; then
551 if [ "${USECGROUP}" = "yes" ]; then
552 local TasksCurrent
553 TasksCurrent="$(systemctl show "$SYSTEMD_SLICE" --property=TasksCurrent | tr -d '\n')"
554 TasksCurrent="${TasksCurrent#TasksCurrent=}"
555 # 18446744073709551615 => -1, not set. old systemd may report the "not set" this way.
556 if [ "$TasksCurrent" != "0" -a "$TasksCurrent" != "[not set]" -a "$TasksCurrent" != "18446744073709551615" ]; then
557 log.d "Waiting for systemd to register process exits"
558 sleep 0.1s
559 TasksCurrent="$(systemctl show "$SYSTEMD_SLICE" --property=TasksCurrent | tr -d '\n')"
560 TasksCurrent="${TasksCurrent#TasksCurrent=}"
561 if [ "$TasksCurrent" != "0" -a "$TasksCurrent" != "[not set]" -a "$TasksCurrent" != "18446744073709551615" ]; then
562 log.w "Stray processes left from build:"
563 systemctl status "$SYSTEMD_SLICE"
564 fi
565 fi
566 systemctl stop "$SYSTEMD_SLICE"
567 fi
568 if [ -d "$BUILDPLACE" ]; then
569 # A directory on the same partition as $BUILDPLACE, bind-mounted
570 # into $BUILDPLACE, will be cleaned out by clean_subdirectories
571 # (because -xdev doesn't know about bind mounts). To avoid that
572 # potential disaster (and also to avoid ugly error messages from
573 # rmdir otherwise), we want to make sure that there is *nothing*
574 # mounted under the chroot before we do our bulldozer routine.
575 #
576 # The readlink -f is a simple way to canonicalize the path for
577 # $BUILDPLACE (no dirty double slashes for *us*), so it matches
578 # what will be in the output of mount.
579 if mount |grep -q -F " $(readlink -f "$BUILDPLACE")/"; then
580 log.e "Something is still mounted under ${BUILDPLACE}; unmount and remove ${BUILDPLACE} manually"
581 else
582 log.i "cleaning the build env "
583 clean_subdirectories "$BUILDPLACE"
584 fi
585 fi;
586 fi
587 }
588
589 function umountproc_cleanbuildplace () {
590 # rolling back to abort.
591 if [ "$?" -ne 0 ]; then
592 log.w "Aborting with an error";
593 fi
594 umountproc
595 cleanbuildplace
596 }
597
598 function saveaptcache_umountproc_cleanbuildplace () {
599 # save the apt cache, and call umountproc_cleanbuildplace
600 save_aptcache
601 umountproc_cleanbuildplace
602 }
603
604 function installaptlines (){
605 log.i "Installing apt-lines"
606 rm -f "$BUILDPLACE"/etc/apt/sources.list
607 if [ -z "$DISTRIBUTION" ]; then
608 log.e "Distribution not specified, please specify"
609 exit 1
610 fi
611 if [ -n "$OTHERMIRROR" ]; then
612 echo "$OTHERMIRROR" | tr "|" "\n" >> "$BUILDPLACE"/etc/apt/sources.list
613 fi
614 if [ -n "$MIRRORSITE" ] ; then
615 cat >> "$BUILDPLACE"/etc/apt/sources.list << EOF
616 deb $MIRRORSITE $DISTRIBUTION $COMPONENTS
617 #deb-src $MIRRORSITE $DISTRIBUTION $COMPONENTS
618 EOF
619 fi
620 if [ -n "$APTCONFDIR" ]; then
621 log.i "Copy " "$APTCONFDIR"/* " to chroot"
622 cp -a "$APTCONFDIR/"* "$BUILDPLACE"/etc/apt
623 fi
624
625 if [ ! -d "$BUILDPLACE"/etc/apt/apt.conf.d ]; then
626 log.i "Create /etc/apt/apt.conf.d/ inside chroot"
627 mkdir "$BUILDPLACE"/etc/apt/apt.conf.d
628 fi
629
630 # configure /etc/apt.conf.d/15pbuilder
631 cat > "$BUILDPLACE"/etc/apt/apt.conf.d/15pbuilder <<EOF
632 APT::Get::Always-Include-Phased-Updates "true";
633 APT::Install-Recommends "false";
634 APT::AutoRemove::SuggestsImportant false;
635 APT::AutoRemove::RecommendsImportant false;
636 Acquire::Languages none;
637 EOF
638 # Do not waste time on refreshing the man-db
639 echo "man-db man-db/auto-update boolean false" | $CHROOTEXEC debconf-set-selections || \
640 log.w "Can't set debconf value for man-db/auto-update"
641 if [ -n "$EXPERIMENTAL" ]; then
642 log.i "Installing apt-lines and pinning for experimental"
643 if [ -n "$MIRRORSITE" ] ; then
644 echo "deb $MIRRORSITE experimental main" >> "$BUILDPLACE"/etc/apt/sources.list
645 echo "#deb-src $MIRRORSITE experimental main" >> "$BUILDPLACE"/etc/apt/sources.list
646 fi
647 fi
648 }
649
650 function remove_packages () {
651 # remove the packages in $@ from the chroot, with apt, so r-deps are
652 # removed too, and do it if and only if they are already installed.
653 local pkg
654 local toremove=()
655 for pkg in "$@"; do
656 if ($CHROOTEXEC dpkg -s "$pkg" 2>&1)>/dev/null ; then
657 if (_contains ${EXTRAPACKAGES[@]} "$pkg" || _contains ${EXTRAPACKAGES[@]} "${pkg}-"); then
658 log.d "not removing $pkg as it's also explicitely listed in EXTRAPACKAGES"
659 else
660 toremove+=("$pkg")
661 fi
662 else
663 log.d "not removing $pkg as not installed"
664 fi
665 done
666 if [ "${#toremove[@]}" -gt 0 ]; then
667 log.i "Removing [${toremove[@]}] from the chroot..."
668 $CHROOTEXEC apt-get -q -y "${APTGETOPT[@]}" --purge remove "${toremove[@]}"
669 fi
670 }
671
672 function copy_local_configuration () {
673 log.i "copying local configuration"
674 if [ -n "$CONFDIR" ] && [ -d "$CONFDIR" ]; then
675 log.i "copying files from $CONFDIR (if possible) instead of using the system ones"
676 fi
677 if [ "$#" -eq 0 ]; then
678 local tocopy=(hosts hostname mailname resolv.conf)
679 else local tocopy=("$@") ; fi
680 if [ "$USENETWORK" = "yes" ]; then
681 local tocopy[${#tocopy[@]}]=resolv.conf
682 fi
683 for a in "${tocopy[@]}"; do
684 if [ -n "$CONFDIR" ] && [ -f "$CONFDIR/$a" ]; then
685 rm -f "$BUILDPLACE/etc/$a"
686 cp "$( readlink -f "$CONFDIR/$a" )" "$BUILDPLACE/etc/$a"
687 elif [ -f "/etc/$a" ]; then
688 rm -f "$BUILDPLACE/etc/$a"
689 cp "$( readlink -f "/etc/$a" )" "$BUILDPLACE/etc/$a"
690 else
691 log.w "No local /etc/$a to copy, relying on $BUILDPLACE/etc/$a to be correct"
692 fi
693 done
694 }
695
696 function extractbuildplace () {
697 # after calling this function, umountproc, and cleanbuildplace
698 # needs to be called. Please trap it after calling this function.
699 local TAR=tar
700 if [ "${INTERNAL_BUILD_UML}" != "yes" -a ! \( "${PRESERVE_BUILDPLACE}" = "yes" -a -d "$BUILDPLACE" \) ]; then
701 cleanbuildplace
702 log.i "Building the build Environment"
703 if ! mkdir -p "$BUILDPLACE"; then
704 log.e "failed to build the directory to chroot"
705 exit 1
706 fi
707 log.i "extracting base tarball [${BASETGZ}]"
708 if [ ! -f "$BASETGZ" ]; then
709 log.e "failed to find $BASETGZ, have you done <pbuilder create> to create your base tarball yet?"
710 exit 1
711 fi
712 if [ "$EATMYDATA" = "yes" ]; then
713 TAR="eatmydata tar"
714 fi
715 if ! (cd "$BUILDPLACE" && $TAR -x -p -f "$BASETGZ"); then
716 log.e "failed to extract $BASETGZ to $BUILDPLACE"
717 exit 1
718 fi
719 fi
720 copy_local_configuration
721 loadhooks
722
723 # installaptlines may fail with exit 1, do it earlier than mountproc.
724 if [ "$OVERRIDE_APTLINES" = "yes" ]; then
725 installaptlines
726 else # Warn if override config is not set
727 if [ "$OVERRIDE_APTLINES_WARN" = "yes" ]; then
728 log.w "--override-config is not set; not updating apt.conf Read the manpage for details."
729 fi
730 fi
731
732 mountproc
733 # FIXME maybe add more checks here? - actually it's not even really needed,
734 # since it's created at chroot creation time too.
735 mkdir -p "${BUILDPLACE}${BUILDDIR}"
736 # XXX added in 0.216, to be deprecated in the future
737 # Add a compatibility symlink from the old BUILDDIR (/tmp/buildd) to the new
738 # one, if different. (Yes, people should just fix their scripts to use
739 # BUILDDIR, please file a bug if you need that variable to also be available
740 # elsewhere other than hook script; given that, I won't do too fancy checks)
741 if [ "$BUILDDIR" != '/tmp/buildd' ]; then
742 if [ -h "$BUILDPLACE/tmp/buildd" ] && [ "$(readlink -f "$BUILDPLACE/tmp/buildd")" = "${BUILDPLACE}$BUILDDIR" ]; then
743 rm "$BUILDPLACE/tmp/buildd"
744 fi
745 if [ -d "$BUILDPLACE/tmp/buildd" ] && [ ! -h "$BUILDPLACE/tmp/buildd" ]; then
746 if [ ! "$(ls -A "$BUILDPLACE/tmp/buildd" 2>&1)" ]; then
747 # empty /tmp/buildd, let's change it to a symlink to BUILDDIR
748 rmdir "$BUILDPLACE/tmp/buildd"
749 ln -rs "${BUILDPLACE}$BUILDDIR" "$BUILDPLACE/tmp/buildd"
750 else
751 log.w "Could not create compatibility symlink because /tmp/buildd is not empty"
752 fi
753 elif [ ! -e "$BUILDPLACE/tmp/buildd" ]; then
754 ln -rs "${BUILDPLACE}$BUILDDIR" "$BUILDPLACE/tmp/buildd"
755 else
756 log.w "Could not create compatibility symlink because /tmp/buildd exists and it is not a directory"
757 fi
758 fi
759
760 if [ "$EATMYDATA" = "yes" ]; then
761 if $CHROOTEXEC /sbin/ldconfig -p | grep -q libeatmydata && $CHROOTEXEC which eatmydata > /dev/null 2>&1 ; then
762 log.i "using eatmydata during job"
763 CHROOTEXEC="$CHROOTEXEC eatmydata"
764 else
765 log.w "eatmydata is not (yet) installed inside the chroot, not using it."
766 fi
767 fi
768
769 # finalize ARCHITECTURE and HOST_ARCH if those aren't set
770 ARCHITECTURE="${ARCHITECTURE:-"$($CHROOTEXEC dpkg --print-architecture)"}"
771 HOST_ARCH="${HOST_ARCH:-"${ARCHITECTURE}"}"
772
773 executehooks "H"
774 }
775
776 function echobacktime () {
777 log.i "Current time: $(date)"
778 log.i "pbuilder-time-stamp: $(date +%s)"
779 }
780
781 function recover_aptcache() {
782 local doit
783 # recover the aptcache archive
784 if [ -n "$APTCACHE" ]; then
785 if [ "$APTCACHEHARDLINK" = "yes" ]; then
786 doit=ln
787 else
788 doit=cp
789 fi
790 log.i "Obtaining the cached apt archive contents"
791 find "$APTCACHE" -maxdepth 1 -name \*.deb | \
792 while read A ; do
793 $doit "$A" "$BUILDPLACE/var/cache/apt/archives/" || true
794 done
795 fi
796 }
797
798 function save_aptcache() {
799 # save the current aptcache archive
800 # it is safe to call this function several times.
801 local doit
802 if [ -n "$APTCACHE" ]; then
803 log.i "Copying back the cached apt archive contents"
804 mkdir -p "$APTCACHE" ;
805 if [ "$APTCACHEHARDLINK" = "yes" ]; then
806 doit=ln
807 else
808 doit=cp
809 fi
810 find "$BUILDPLACE/var/cache/apt/archives/" -maxdepth 1 -name \*.deb | \
811 while read A ;do
812 if [ ! -f "$APTCACHE/$(basename "$A")" -a -f "$A" ]; then
813 log.i "new cache content '$(basename "$A")' added"
814 $doit "$A" "$APTCACHE/" || true
815 fi
816 done
817 fi
818 }
819
820 function create_basetgz() {
821 # don't pack the hooks in
822 unloadhooks
823 # create base.tgz
824 (
825 if ! cd "$BUILDPLACE"; then
826 log.e "unexpected error in chdir to $BUILDPLACE"
827 exit 1;
828 fi
829 while test -f "${BASETGZ}.tmp"; do
830 log.i "Someone else has lock over ${BASETGZ}.tmp, waiting"
831 sleep 10s
832 done
833 log.i "creating base tarball [${BASETGZ}]"
834 if [ -h "$BUILDPLACE/tmp/buildd" ] && [ "$(readlink -f "$BUILDPLACE/tmp/buildd")" = "${BUILDPLACE}$BUILDDIR" ]; then
835 rm "$BUILDPLACE/tmp/buildd"
836 fi
837 if ! tar -c --use-compress-program "$COMPRESSPROG" -f "${BASETGZ}.tmp" --exclude ./sys/* --exclude ./proc/* ./* ; then
838 log.e "failed building base tarball"
839 rm -f "${BASETGZ}.tmp"
840 exit 1;
841 fi
842 mv "${BASETGZ}.tmp" "${BASETGZ}"
843 )
844 }
845
846 function copyinputfile() {
847 # copy files to inside chroot, copy all files specified by INPUTFILE[] parameter.
848 TARGETDIR="$1"
849 if [ -z "$TARGETDIR" ]; then
850 log.e "Unexpected error in copyinputfile"
851 exit 1x
852 fi
853 if [ -n "$INPUTFILE" ]; then
854 log.i "copy ${INPUTFILE[*]} to target directory"
855 cp "${INPUTFILE[@]}" "${TARGETDIR}"
856 fi
857 }
858
859 # all trap hooks that should lead to 'exit'; and error exit.
860 function cleanbuildplace_trap () {
861 trap "" sigpipe sighup
862 set +e
863 cleanbuildplace
864 trap - exit sighup sigpipe
865 exit 1
866 }
867 function saveaptcache_umountproc_cleanbuildplace_trap () {
868 trap "" sigpipe sighup
869 set +e
870 saveaptcache_umountproc_cleanbuildplace
871 trap - exit sighup sigpipe
872 exit 1
873 }
874 function umountproc_cleanbuildplace_trap () {
875 trap "" sigpipe sighup
876 set +e
877 umountproc_cleanbuildplace
878 trap - exit sighup sigpipe
879 exit 1
880 }
881 function umountproc_trap () {
882 trap "" sigpipe sighup
883 set +e
884 umountproc
885 trap - exit sighup sigpipe
886 exit 1
887 }
888
889 # copy to .. if target directory is not ..
890 function conditional_cp_a() {
891 local source_file="$1"
892 # NOTE: target_dir must not end with /, which is usually the case
893 # with 'readlink -f' result, which BUILDRESULT usually is.
894 local target_dir="$2"
895 # For testability, make cp overridable.
896 local cp="${3:-cp}"
897
898 # $PWD should end with non-'/', so dirname should give us the parent dir.
899 local parent_dir=$(dirname "$PWD")
900
901 if [ "${parent_dir}" != "${target_dir}" ]; then
902 "$cp" -a "$source_file" "$target_dir"
903 else
904 log.i "file ${source_file} is already in target, not copying."
905 fi
906 }
907
908 function add_additional_aptkeyrings() {
909 # To support package verification inside the repository we may have to
910 # import additional keys.
911 local filename
912 local dest
913 local KEY
914 for KEY in "${APTKEYRINGS[@]}"; do
915 filename="$(basename "${KEY}")"
916 if [ "${filename:(-4):4}" != ".gpg" ]; then
917 log.e "apt key file does not end in .gpg"
918 exit 1
919 fi
920 dest="${BUILDPLACE}/etc/apt/trusted.gpg.d/${filename}"
921 mkdir -p "${BUILDPLACE}/etc/apt/trusted.gpg.d"
922 log.i "copying apt key file ${KEY} to ${dest}"
923 if [ -f "${dest}" ]; then
924 if ! cmp -s "${KEY}" "${dest}" ; then
925 log.w "overwriting existing and different keyring"
926 fi
927 fi
928 cp "${KEY}" "${dest}"
929 done
930 }
931
932 function install_packages_for_optional_features() {
933 # automatically add packages needed for optional features to the chroot
934 # and remove them when that feature is disabled
935 case "$(readlink -e "$PBUILDERSATISFYDEPENDSCMD")" in
936 *-aptitude)
937 EXTRAPACKAGES="$EXTRAPACKAGES aptitude"
938 ;;
939 *)
940 REMOVEPACKAGES="$REMOVEPACKAGES aptitude"
941 ;;
942 esac
943 if [ -n "$CCACHEDIR" ]; then
944 EXTRAPACKAGES="$EXTRAPACKAGES ccache"
945 else
946 REMOVEPACKAGES="$REMOVEPACKAGES ccache"
947 fi
948
949 if [ "$EATMYDATA" = "yes" ]; then
950 # the eatmydata binary is going nowhere anytime soon, whilst the libeatmydata1
951 # binary is shaped like a shared library, so the name might change, etc.
952 # Install this package instead, it's cheap enough.
953 EXTRAPACKAGES="$EXTRAPACKAGES eatmydata"
954 elif [ "$EATMYDATA" != "not-available" ]; then
955 REMOVEPACKAGES="$REMOVEPACKAGES eatmydata"
956 fi
957
958 if [ "$DEBDELTA" = "yes" ]; then
959 EXTRAPACKAGES="$EXTRAPACKAGES debdelta python-apt xdelta3"
960 else
961 REMOVEPACKAGES="$REMOVEPACKAGES debdelta"
962 fi
963 }
964
965 function get822field() {
966 local field="$1"
967 local file="$2"
968
969 sed -n '
970 # Skip PGP header
971 /^-----BEGIN PGP SIGNED MESSAGE-----$/ {
972 : pgploop
973 n
974 /^$/ b leadloop
975 b pgploop
976 }
977
978 # Skip empty lines/comments
979 : leadloop
980 /^[ \t]*$/ {
981 n
982 b leadloop
983 }
984 /^#/ {
985 n
986 b leadloop
987 }
988
989 # First line of paragraph
990 : paraloop
991 # Strip field name from line if present
992 s/^'"$field"': *//i
993 # If field present, print contents
994 # Note the h; t loop; here rather than t store; so that there is no leading
995 # newline in the hold space. This will store unmatched lines in the hold space,
996 # but that does not matter as the first matched line will overwrite it.
997 h
998 t loop
999 # Otherwise, check for end of paragraph or ignore
1000 /^[ \t]*$/q
1001 n
1002 b paraloop
1003
1004 : store
1005 H
1006 : loop
1007 $ b done
1008 n
1009 /^#/ b loop
1010 /^[ \t]/ b store
1011 : done
1012 x
1013 p
1014 n
1015 b paraloop' \
1016 "$file"
1017 }
1018
1019 function get_changes_options() {
1020 local changes_options
1021 local arg
1022 # Split and de-escape DEBBUILDOPTS. Can't iterate over it before
1023 # de-escaping, as word splitting does not take quotes in the variable into
1024 # account. Need eval as $(echo $DEBBUILDOPTS) on its own doesn't perform
1025 # quote expansion, since quote expansion operates on the *original* word.
1026 eval local args=($DEBBUILDOPTS)
1027 for arg in "${args[@]}"; do
1028 case $arg in
1029 -s[iad]|-v*|-m*|-e*|-C*)
1030 changes_options="${changes_options:+$changes_options }'$arg'"
1031 ;;
1032 --changes-option=*)
1033 changes_options="${changes_options:+$changes_options }'${arg#--changes-option=}'"
1034 ;;
1035 # dpkg-genchanges doesn't support the long form of some options
1036 --release-by=*)
1037 changes_options="${changes_options:+$changes_options }-m'${arg#--release-by=}'"
1038 ;;
1039 --build-by=*)
1040 changes_options="${changes_options:+$changes_options }-e'${arg#--build-by=}'"
1041 ;;
1042 esac
1043 done
1044 echo "${changes_options:-}"
1045 }
1046
1047 function should_clean_source() {
1048 local noclean=0
1049 local arg
1050 # Split and de-escape DEBBUILDOPTS. Can't iterate over it before
1051 # de-escaping, as word splitting does not take quotes in the variable into
1052 # account. Need eval as $(echo $DEBBUILDOPTS) on its own doesn't perform
1053 # quote expansion, since quote expansion operates on the *original* word.
1054 eval local args=($DEBBUILDOPTS)
1055 for arg in "${args[@]}"; do
1056 case $arg in
1057 --pre-clean)
1058 noclean=0
1059 ;;
1060 -nc|--no-pre-clean)
1061 noclean=1
1062 ;;
1063 esac
1064 done
1065 # Exit code, not boolean, so inverted meaning
1066 return $noclean
1067 }
1068
1069 function get_source_options() {
1070 local source_options
1071 local arg
1072 # Split and de-escape DEBBUILDOPTS. Can't iterate over it before
1073 # de-escaping, as word splitting does not take quotes in the variable into
1074 # account. Need eval as $(echo $DEBBUILDOPTS) on its own doesn't perform
1075 # quote expansion, since quote expansion operates on the *original* word.
1076 eval local args=($DEBBUILDOPTS)
1077 for arg in "${args[@]}"; do
1078 case $arg in
1079 -s[nsAkurKUR]|-z*|-Z*|-i*|-I*)
1080 source_options="${source_options:+$source_options }'$arg'"
1081 ;;
1082 --source-option=*)
1083 source_options="${source_options:+$source_options }'${arg#--source-option=}'"
1084 ;;
1085 # Pre-squeeze dpkg-source doesn't support the long forms.
1086 # Since this is only used by pdebuild outside the chroot, it should
1087 # be safe to keep the long forms, but it does no harm to keep
1088 # backwards compatibility.
1089 --compression-level=*)
1090 source_options="${source_options:+$source_options }-z'${arg#--compression-level=}'"
1091 ;;
1092 --compression=*)
1093 source_options="${source_options:+$source_options }-Z'${arg#--compression=}'"
1094 ;;
1095 --diff-ignore=*)
1096 source_options="${source_options:+$source_options }-i'${arg#--diff-ignore=}'"
1097 ;;
1098 --tar-ignore=*)
1099 source_options="${source_options:+$source_options }-I'${arg#--tar-ignore=}'"
1100 ;;
1101 esac
1102 done
1103 echo "${source_options:-}"
1104 }
1105
1106 #==========================================================================
1107 # hooks stuff
1108
1109 function loadhooks () {
1110 if [ -z "$HOOKDIR" ]; then
1111 return ;
1112 fi
1113 if [ -d "$BUILDPLACE/$hooks" ]; then
1114 rm -rf "$BUILDPLACE/$hooks"
1115 fi
1116 if [ -d "$HOOKDIR" ]; then
1117 mkdir -p "$BUILDPLACE/$hooks"
1118 if ! cp -aL "$HOOKDIR/"* "$BUILDPLACE/$hooks"; then
1119 log.w "no hooks found in the hookdir '$HOOKDIR'"
1120 fi
1121 else
1122 log.w "hookdir $HOOKDIR does not exist, skipping"
1123 fi
1124 }
1125
1126 function unloadhooks () {
1127 if [ -z "$HOOKDIR" ]; then
1128 return ;
1129 fi
1130 rm -rf "$BUILDPLACE/$hooks"
1131 }
1132
1133 function executehooks () {
1134 # Execute every script found in the chroot'd target directory.
1135 # We only test whether a file is executable since we have no idea what
1136 # the user had put in their chroots. If they want PL/1 and ADA on the
1137 # chroot or they have decided to use emacslisp for everything, it's their
1138 # problem.
1139 local prefix="$1"
1140 if [ -z "$HOOKDIR" ]; then
1141 return
1142 fi
1143 for fn in "$BUILDPLACE/$hooks/$prefix"[0-9][0-9]* ; do
1144 case "$fn" in
1145 "$BUILDPLACE/$hooks/$prefix"'[0-9][0-9]*')
1146 log.d "no hooks of type ${prefix} found -- ignoring"
1147 ;;
1148 *~)
1149 log.w "skipping an editor backup file $fn"
1150 ;;
1151 *.bak)
1152 log.w "skipping a backup file $fn"
1153 ;;
1154 *)
1155 if [ -x "$fn" ]; then
1156 log.i "user script $fn starting"
1157 BUILDDIR="$BUILDDIR" \
1158 DISTRIBUTION="$DISTRIBUTION" \
1159 BUILD_ARCH="$ARCHITECTURE" \
1160 HOST_ARCH="$HOST_ARCH" \
1161 $CHROOTEXEC "/$hooks/$(basename "$fn")"
1162 log.i "user script $fn finished"
1163 else
1164 if [ -f "$fn" ]; then
1165 filetype=$(basename "$fn" )
1166 log.w "execute priv not set on file $filetype, not executing."
1167 else
1168 # Should it reach here ? This case should be caught in the above case.
1169 log.w "no hooks of type ${prefix} found -- internal error in logic"
1170 fi
1171 fi
1172 ;;
1173 esac
1174 done
1175 }
1176
1177 #==========================================================================
1178
1179
1180 #Setting environmental variables that are really required:
1181 #required for some packages to install...
1182 export LANG=C
1183 export LC_ALL=C