manjaro-tools/bin/chroot-run.in
2015-01-08 16:19:00 +01:00

128 lines
4.1 KiB
Bash

#!/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
version=@version@
[[ -r @libdir@/util-msg.sh ]] && source @libdir@/util-msg.sh
[[ -r @libdir@/util.sh ]] && source @libdir@/util.sh
[[ -r @libdir@/util-mount.sh ]] && source @libdir@/util-mount.sh
working_dir=''
usage() {
echo "Usage: ${0##*/} [options] working-dir [run arguments]"
echo "A wrapper around chroot. Provides support for pacman."
echo
echo ' options:'
echo ' -C <file> Location of a pacman config file'
echo ' -M <file> Location of a makepkg config file'
echo ' -S <file> Location of a pacman-mirrors config file'
echo ' -c <dir> Set pacman cache'
echo ' -r <list> Bind mountargs ro'
echo ' -w <list> Bind mountargs rw'
echo ' List format [src1:target1,...,srcN:targetN]'
echo ' -h This message'
exit 1
}
orig_argv=("$@")
while getopts 'hC:M:S:c:r:w:' arg; do
case "$arg" in
C) pac_conf="$OPTARG" ;;
M) makepkg_conf="$OPTARG" ;;
S) mirrors_conf="$OPTARG" ;;
c) cache_dir="$OPTARG" ;;
r) mountargs_ro="$OPTARG" ;;
w) mountargs_rw="$OPTARG" ;;
h|?) usage ;;
*) error "invalid argument '$arg'"; usage ;;
esac
done
shift $(($OPTIND - 1))
(( $# < 1 )) && die 'You must specify a directory.'
check_root "$0" "${orig_argv[@]}"
working_dir=$(readlink -f "$1")
shift 1
[[ -z $working_dir ]] && die 'Please specify a working directory.'
if [[ -z $cache_dir ]]; then
cache_dirs=($(pacman -v 2>&1 | grep '^Cache Dirs:' | sed 's/Cache Dirs:\s*//g'))
else
cache_dirs=("$cache_dir")
fi
# host_mirror=$(pacman -Sddp community/manjaro-tools 2>/dev/null | sed -E "s#(.*/)(.*/)community/.*#\1branch/\$repo/\$arch#")
# [[ $host_mirror == *file://* ]] && host_mirror_path=$(echo "$host_mirror" | sed -r 's#file://(/.*)/\$repo/os/\$arch#\1#g')
copy_hostconf () {
cp -a /etc/pacman.d/gnupg "$working_dir/etc/pacman.d"
[[ -n $pac_conf ]] && cp $pac_conf "$working_dir/etc/pacman.conf"
[[ -n $makepkg_conf ]] && cp $makepkg_conf "$working_dir/etc/makepkg.conf"
[[ -n $mirrors_conf ]] && cp ${mirrors_conf} "${working_dir}/etc/pacman-mirrors.conf"
#local branch=$(cat "${working_dir}/etc/pacman-mirrors.conf" | grep '^Branch=' | sed 's/Branch=\s*//g')
source ${working_dir}/etc/pacman-mirrors.conf
#host_mirror=$(echo "$host_mirror" | sed -E "s#/branch/#/${branch}/#")
#echo "Server = $host_mirror" >"$working_dir/etc/pacman.d/mirrorlist"
sed -e "s|/stable|/${Branch}|g" -i "$working_dir/etc/pacman.d/mirrorlist"
sed -r "s|^#?\\s*CacheDir.+|CacheDir = $(echo -n ${cache_dirs[@]})|g" -i "$working_dir/etc/pacman.conf"
}
build_mount_args() {
track_mount "/etc/resolv.conf" "${working_dir}/etc/resolv.conf" -B
track_mount "${cache_dirs[0]}" "${working_dir}${cache_dirs[0]}" -B
# for cache_dir in ${cache_dirs[@]:1}; do
# track_mount "$cache_dir" "${working_dir}${cache_dir}" -Br
# done
if [[ -n ${mountargs_ro[@]} ]];then
local IFS=','
for m in ${mountargs_ro[@]}; do
track_mount "${m%%:*}" "${working_dir}${m##*:}" -Br
done
unset IFS
fi
if [[ -n ${mountargs_rw[@]} ]];then
local IFS=','
for m in ${mountargs_rw[@]}; do
track_mount "${m%%:*}" "${working_dir}${m##*:}" -B
done
unset IFS
fi
}
umask 0022
# Sanity check
if [[ ! -f "$working_dir/.manjaro-tools" ]]; then
die "'%s' does not appear to be a Manjaro chroot." "$working_dir"
elif [[ $(cat "$working_dir/.manjaro-tools") != $version ]]; then
die "chroot '%s' is not at version %s. Please rebuild." "$working_dir" "$version"
fi
api_fs_mount "${working_dir}" || die "failed to setup API filesystems in chroot %s" "${working_dir}"
build_mount_args
copy_hostconf
eval $(grep '^CARCH=' "$working_dir/etc/makepkg.conf")
${CARCH:+setarch "$CARCH"} chroot "${working_dir}" "$@"