From 53f09df2ff4810a6f5efe44f2fef1bea30b784c6 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 5 Jan 2015 15:14:30 -0500 Subject: librechroot: tidy up --- src/chroot-tools/librechroot | 119 +++++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 49 deletions(-) (limited to 'src/chroot-tools') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index 79f57c9..5d8a202 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -38,17 +38,48 @@ load_files chroot shopt -s nullglob umask 0022 +################################################################################ +# Wrappers for files in ${pkglibexecdir}/chroot/ # +################################################################################ + readonly _arch_nspawn="$(librelib chroot/arch-nspawn)" readonly _mkarchroot="$(librelib chroot/mkarchroot)" readonly _makechrootpkg="$(librelib chroot/makechrootpkg.sh)" -# Because the makechrootpkg.sh library functions don't work with -euE +arch_nspawn_flags=() +sysd_nspawn_flags=() + +# Usage: arch-nspawn $copydir $cmd... +arch-nspawn() { + local copydir=$1; shift + local cmd=("$@") + + set +u # if an array is empty, it counts as unbound + "$_arch_nspawn" "${arch_nspawn_flags[@]}" "$copydir" "${sysd_nspawn_flags[@]}" -- "${cmd[@]}" + set -u +} + +# Usage: mkarchroot $copydir $pkgs... +mkarchroot() { + local copydir=$1; shift + local pkgs=("$@") + set +u # if an array is empty, it counts as unbound + "$_mkarchroot" "${arch_nspawn_flags[@]}" "$copydir" "${pkgs[@]}" + set -u +} + +# Usage: _makechrootpkg $function $arguments... +# Don't load $_makechrootpkg directly because it doesn't work with -euE _makechrootpkg() ( set +euE . "$_makechrootpkg" "$@" ) +################################################################################ +# Utility functions # +################################################################################ + # Usage: make_empty_repo $copydir make_empty_repo() { local copydir=$1 @@ -70,6 +101,41 @@ chroot_add_to_local_repo() { done } +# Print code to set $rootdir and $copydir; blank them on error +calculate_directories() { + # Don't assume that CHROOTDIR or CHROOT are set, + # but assume that COPY is set. + local rootdir copydir + + if [[ -n ${CHROOTDIR:-} ]] && [[ -n ${CHROOT:-} ]]; then + rootdir="${CHROOTDIR}/${CHROOT}/root" + else + rootdir='' + fi + + if [[ ${COPY:0:1} = / ]]; then + copydir=$COPY + elif [[ -n ${CHROOTDIR:-} ]] && [[ -n ${CHROOT:-} ]]; then + copydir="${CHROOTDIR}/${CHROOT}/${COPY}" + else + copydir='' + fi + + declare -p rootdir + declare -p copydir +} + +check_mountpoint() { + local file=$1 + local mountpoint="$(df -P "$file"|sed '1d;s/.*\s//')" + local mountopts=($(LC_ALL=C mount|awk "{ if (\$3==\"$mountpoint\") { gsub(/[(,)]/, \" \", \$6); print \$6 } }")) + ! in_array nosuid "${mountopts[@]}" && ! in_array noexec "${mountopts[@]}" +} + +################################################################################ +# Main program # +################################################################################ + usage() { eval "$(calculate_directories)" print "Usage: %s [OPTIONS] COMMAND [ARGS...]" "${0##*/}" @@ -154,48 +220,6 @@ readonly commands=( run enter clean-repo help ) -# Print code to set $rootdir and $copydir; blank them on error -calculate_directories() { - # Don't assume that CHROOTDIR or CHROOT are set, - # but assume that COPY is set. - local rootdir copydir - - if [[ -n ${CHROOTDIR:-} ]] && [[ -n ${CHROOT:-} ]]; then - rootdir="${CHROOTDIR}/${CHROOT}/root" - else - rootdir='' - fi - - if [[ ${COPY:0:1} = / ]]; then - copydir=$COPY - elif [[ -n ${CHROOTDIR:-} ]] && [[ -n ${CHROOT:-} ]]; then - copydir="${CHROOTDIR}/${CHROOT}/${COPY}" - else - copydir='' - fi - - declare -p rootdir - declare -p copydir -} - -check_mountpoint() { - local file=$1 - local mountpoint="$(df -P "$file"|sed '1d;s/.*\s//')" - local mountopts=($(LC_ALL=C mount|awk "{ if (\$3==\"$mountpoint\") { gsub(/[(,)]/, \" \", \$6); print \$6 } }")) - ! in_array nosuid "${mountopts[@]}" && ! in_array noexec "${mountopts[@]}" -} - -arch_nspawn_flags=() -sysd_nspawn_flags=() -arch-nspawn() { - local copydir=$1; shift - local cmd=("$@") - - set +u # if an array is empty, it counts as unbound - "$_arch_nspawn" "${arch_nspawn_flags[@]}" "$copydir" "${sysd_nspawn_flags[@]}" -- "${cmd[@]}" - set -u -} - # Globals: $CHROOTDIR, $CHROOT, $COPY, $rootdir and $copydir main() { COPY=$LIBREUSER @@ -296,11 +320,10 @@ main() { if ! [[ -t 0 ]]; then error "Input is not a TTY" plain "https://labs.parabola.nu/issues/420" + plain "https://labs.parabola.nu/issues/431" plain "https://bugs.freedesktop.org/show_bug.cgi?id=70290" prose "Due to a bug in systemd-nspawn, redirecting stdin is not - supported. We have been able to mitigate the problems - with redirecting stdout, but until the bug is fixed, - redirecting stdin will only end in pain." >&2 + supported." >&2 return 1 fi @@ -316,9 +339,7 @@ main() { if [[ ! -d $rootdir ]]; then msg "Creating 'root' copy for chroot [%s]" "$CHROOT" - set +u # if an array is empty, it counts as unbound - "$_mkarchroot" "${arch_nspawn_flags[@]}" "$rootdir" base-devel - set -u + mkarchroot "$rootdir" base-devel make_empty_repo "$rootdir" fi -- cgit v1.2.3-54-g00ecf