summaryrefslogtreecommitdiff
path: root/src/chroot-tools/libremakepkg
diff options
context:
space:
mode:
Diffstat (limited to 'src/chroot-tools/libremakepkg')
-rwxr-xr-xsrc/chroot-tools/libremakepkg146
1 files changed, 102 insertions, 44 deletions
diff --git a/src/chroot-tools/libremakepkg b/src/chroot-tools/libremakepkg
index a59315b..26080bc 100755
--- a/src/chroot-tools/libremakepkg
+++ b/src/chroot-tools/libremakepkg
@@ -2,9 +2,9 @@
set -euE
# libremakepkg
-# Copyright 2010-2011 Nicolás Reynolds
-# Copyright 2011 Joshua Ismael Haase Hernández
-# Copyright 2012-2013 Luke Shumaker
+# Copyright (C) 2010-2011 Nicolás Reynolds
+# Copyright (C) 2011 Joshua Ismael Haase Hernández
+# Copyright (C) 2012-2014 Luke Shumaker
#
# This file is part of Parabola.
#
@@ -25,15 +25,18 @@ set -euE
. $(librelib messages)
. $(librelib chroot/makechrootpkg.sh)
+set -o pipefail
shopt -s nullglob
umask 0022
# Global variables:
+readonly _indent="$(librelib chroot/indent)"
readonly INCHROOT=$([[ -f /.arch-chroot ]] && echo true || echo false)
NONET=true # can be changed with the -N flag
-# {SRC,LOG,PKG}DEST set at runtime by makepkg.conf
+# {PKG,SRC,SRCPKG,LOG}DEST set at runtime by makepkg.conf
# MAKEFLAGS, PACKAGER set at runtime by makepkg.conf
# LIBREUSER, LIBREHOME are set by conf.sh
+librechroot_flags=()
# Hooks ########################################################################
@@ -47,6 +50,25 @@ hook_check_pkg=(:)
# Boring/mundane functions #####################################################
+indent() {
+ "$_indent" ' | '
+}
+
+# Usage: _check_perms_dir $directory
+# Make sure that $directory is readable and executable (searchable) by 'nobody'
+check_directory_permissions() (
+ local dir=$1
+ # `cd` to the directory, then test `.`; that way if parent
+ # directories aren't readable, we aren't testing for that. We
+ # only need the last element in `$dir`.
+ cd "$dir"
+ if ! sudo -u nobody test -r . -a -x .; then
+ error "Directory '%s' must be readable by user 'nobody'" "$dir"
+ return 1
+ fi
+ return 0
+)
+
# Usage: exit_copy $copydir $src_owner
# End immediately, but copy log files out
exit_copy() {
@@ -62,17 +84,18 @@ exit_copy() {
run_hook() {
local hookname=$1; shift
local hookvar="hook_${hookname}[@]"
+
local fails=()
- msg "Running hook: %s" "$hookname"
for hook in "${!hookvar}"; do
- msg2 'hook: %s' "$hook"
- "$hook" "$@" || { error "result: %s" $?; fails+=("$hook"); }
- done
+ "$hook" "$@" || fails+=("$hook")
+ done |& indent
+
if [[ ${#fails[@]} -gt 0 ]]; then
error "Failure(s) in %s: %s" "$hookname" "${fails[*]}"
return 1
+ else
+ return 0
fi
- return 0
}
# Usage: add_to_local_repo $copydir $pkgfiles...
@@ -88,25 +111,32 @@ add_to_local_repo() {
done
}
+hook_post_build+=('cleanup')
+cleanup() {
+ local copydir=$1
+ rm -f -- "$copydir"/chroot{prepare,build}
+}
+
build() (
local copydir=$1; shift
- local cmd=(/chrootbuild "$@")
+ local repack=$1; shift
- run_hook pre_build "$copydir"
- trap "run_hook post_build '$copydir'" EXIT
-
- local netflag=''
+ local run_ynet=()
+ local run_nnet=()
if $INCHROOT; then
- ! $NONET || netflag='-n'
- unshare $netflag -- "${cmd[@]}"
+ run_ynet=(unshare)
+ run_nnet=(unshare -n)
else
- ! $NONET || netflag='-N'
- librechroot $netflag \
- -r "$PWD:/startdir_host" \
- -r "$SRCDEST:/srcdest_host" \
- -l "$copydir" \
- run "${cmd[@]}"
+ run_ynet=(librechroot "${librechroot_flags[@]}" run)
+ run_nnet=(librechroot "${librechroot_flags[@]}" -N run)
fi
+ $NONET || run_nnet=("${run_ynet[@]}")
+
+ prepare_chroot "$copydir" "$LIBREHOME" "$repack" false
+ "${run_ynet[@]}" /chrootprepare false "$@" |& indent
+ run_hook pre_build "$copydir"
+ trap "run_hook post_build '$copydir'" EXIT
+ "${run_nnet[@]}" /chrootbuild false "$@" |& indent
)
# The main program #############################################################
@@ -118,7 +148,7 @@ usage() {
prose 'If run from outside of a chroot, command will make the following
configuration changes in the chroot:'
bullet 'whatever changes `librechroot` makes.'
- bullet 'set `PKGDEST` and `SRCDEST` in `/etc/makepkg.conf`'
+ bullet 'set `{PKG,SRC,SRCPKG,LOG}DEST` in `/etc/makepkg.conf`'
bullet 'set `PACKAGER` in `/etc/makepkg.conf` to reflect the value
outside of the chroot.'
bullet '(maybe) delete `/build/.makepkg.conf`'
@@ -134,8 +164,12 @@ usage() {
the documentation there.'
echo
print 'Options:'
+ print ' %s options:' librechroot
flag "-n <$(_ CHROOT)>" 'Name of the chroot to use'
flag "-l <$(_ COPY)>" 'Name of, or absolute path to, the chroot copy to use'
+ flag "-w <$(_ 'PATH[:PATH]')>" 'Bind mount a file or directory, read/write'
+ flag "-r <$(_ 'PATH[:PATH]')>" 'Bind mount a file or directory, read-only'
+ print ' %s options:' libremakepkg
flag '-N' "Don't disable networking during build() and
package(). PLEASE don't use this unless you
have a special reason, its use is a violation
@@ -159,10 +193,14 @@ main() {
local chroot=''
# Parse command line options ###########################################
- while getopts 'n:l:NRh' flag ; do
+ while getopts 'n:l:w:r:NRh' flag ; do
case "${flag}" in
- n) if $INCHROOT; then err_chflag "$flag"; else chroot=$OPTARG; fi;;
- l) if $INCHROOT; then err_chflag "$flag"; else copy=$OPTARG; fi;;
+ n) if $INCHROOT; then err_chflag "$flag"; else
+ chroot=$OPTARG; fi;;
+ l) if $INCHROOT; then err_chflag "$flag"; else
+ copy=$OPTARG; fi;;
+ w|r) if $INCHROOT; then err_chflag "$flag"; else
+ librechroot_flags+=(-$flag "$OPTARG"); fi;;
N) NONET=false;;
R) repack=true; makepkg_args+=(-R);;
h) usage; return 0;;
@@ -190,6 +228,15 @@ main() {
fi
unset chroot
+ # Load makepkg configuration ###########################################
+ # Note that all of these are globals
+ PKGDEST="$(get_var makepkg PKGDEST "$PWD")"
+ SRCDEST="$(get_var makepkg SRCDEST "$PWD")"
+ SRCPKGDEST="$(get_var makepkg SRCPKGDEST "$PWD")"
+ LOGDEST="$(get_var makepkg LOGDEST "$PWD")"
+ MAKEFLAGS="$(get_var makepkg MAKEFLAGS '')"
+ PACKAGER="$(get_var makepkg PACKAGER '')"
+
# Quick sanity check ###################################################
if (( EUID )); then
@@ -203,14 +250,17 @@ main() {
exit 1
fi
- # Load makepkg configuration ###########################################
- # Note that all of these are globals
- SRCDEST="$(get_conf_makepkg SRCDEST "$PWD")"
- PKGDEST="$(get_conf_makepkg PKGDEST "$PWD")"
- LOGDEST="$(get_conf_makepkg LOGDEST "$PWD")"
- mkdir -p "$SRCDEST" "$PKGDEST" "$LOGDEST"
- MAKEFLAGS="$(get_conf_makepkg MAKEFLAGS '')"
- PACKAGER="$(get_conf_makepkg PACKAGER '')"
+ # Make sure that the various *DEST directories exist
+ mkdir -p -- "$PKGDEST" "$SRCDEST" "$SRCPKGDEST" "$LOGDEST"
+ # Check the permissions for $startdir and $SRCDEST
+ (
+ declare -i ret=0
+ check_directory_permissions "$PWD" || ret=1
+ if ! [[ "$PWD" -ef "$SRCDEST" ]]; then
+ check_directory_permissions "$SRCDEST" || ret=1
+ fi
+ exit $ret
+ )
# OK, we are starting now ##############################################
@@ -218,32 +268,40 @@ main() {
lock 9 "/build/.lock" \
"Waiting for existing lock on build directory to be released"
else
+ librechroot_flags+=(
+ -r "$PWD:/startdir_host"
+ -r "$SRCDEST:/srcdest_host"
+ -n "$CHROOT"
+ -l "$copy"
+ )
+
# Obtain a lock on the chroot
lock 9 "$copydir.lock" \
"Waiting for existing lock on chroot copy to be released: [%s]" "$copy"
# Create the chroot if it does not exist
- librechroot -n "$CHROOT" -l "$copy" make
+ msg 'Initializing the chroot...'
+ librechroot "${librechroot_flags[@]}" make |& indent
fi
# Set target CARCH
# note that we waited until after locking/creating the chroot to do this
- export CARCH="$(MAKEPKG_CONF=$copydir/etc/makepkg.conf get_conf_makepkg CARCH)"
+ export CARCH="$(MAKEPKG_CONF=$copydir/etc/makepkg.conf get_var makepkg CARCH)"
# Pre-build
+ msg 'Starting pre-build activities...'
run_hook check_pkgbuild
- download_sources "$copydir" "$LIBREUSER"
- prepare_chroot "$copydir" "$LIBREHOME" "$repack" false
- clean_chroot "$copydir"
+ msg 'Downloading sources...'
+ download_sources "$copydir" "$LIBREUSER" |& indent
# Build
+ msg 'Starting to build the package...'
trap "exit_copy '$copydir' '$LIBREUSER'" EXIT
- warning 'Entering build...'
- build "$copydir" "${makepkg_args[@]}"
+ build "$copydir" "$repack" "${makepkg_args[@]}"
+
# Post-build
- warning 'Entering hook check_pkg...'
+ msg 'Starting post-build activities...'
run_hook check_pkg
- warning 'Entering add_to_local_repo ...'
- add_to_local_repo "$copydir" "$copydir"/pkgdest/*.pkg.tar*
+ add_to_local_repo "$copydir" "$copydir"/pkgdest/*.pkg.tar* |& indent
}
main "$@"