manjaro-tools/bin/chroot-run.in
2014-11-12 04:15:40 +01:00

130 lines
3.8 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@
if [[ -r @libdir@/messages.sh ]];then
source @libdir@/messages.sh
fi
if [[ -r @libdir@/mount-api.sh ]];then
source @libdir@/mount-api.sh
fi
if [[ -r @libdir@/utils.sh ]];then
source @libdir@/utils.sh
fi
working_dir=''
usage() {
echo "Usage: ${0##*/} [options] working-dir [systemd-nspawn arguments]"
echo "A wrapper around systemd-nspawn. 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 ' -p <mount> Set PKGDEST mountpoint'
echo ' -s <mount> Set PKGSRC mountpoint'
echo ' -h This message'
exit 1
}
orig_argv=("$@")
while getopts 'hC:M:S:c:p:s:' arg; do
case "$arg" in
C) pac_conf="$OPTARG" ;;
M) makepkg_conf="$OPTARG" ;;
S) mirrors_conf="$OPTARG" ;;
c) cache_dir="$OPTARG" ;;
p) pkgdest="$OPTARG" ;;
s) pkgsrc="$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 extra/devtools 2>/dev/null | sed -E "s#(.*/)(.*/)extra/.*#\1branch/\$repo/\$arch#")
[[ $host_mirror == *file://* ]] && host_mirror_path=$(echo "$host_mirror" | sed -r 's#file://(/.*)/\$repo/os/\$arch#\1#g')
# {{{ functions
build_mount_args() {
declare -g mount_args=()
if [[ -n $host_mirror_path ]]; then
mount_args+=(--bind-ro="$host_mirror_path")
fi
mount_args+=(--bind="${cache_dirs[0]}")
for cache_dir in ${cache_dirs[@]:1}; do
mount_args+=(--bind-ro="$cache_dir")
done
}
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')
host_mirror=$(echo "$host_mirror" | sed -E "s#/branch/#/${branch}/#")
echo "Server = $host_mirror" >"$working_dir/etc/pacman.d/mirrorlist"
sed -r "s|^#?\\s*CacheDir.+|CacheDir = $(echo -n ${cache_dirs[@]})|g" -i "$working_dir/etc/pacman.conf"
}
# }}}
umask 0022
# Sanity check
if [[ ! -f "$working_dir/.manjaro-chroot" ]]; then
die "'%s' does not appear to be a Manjaro chroot." "$working_dir"
elif [[ $(cat "$working_dir/.manjaro-chroot") != $version ]]; then
die "chroot '%s' is not at version %s. Please rebuild." "$working_dir" "$version"
fi
api_fs_mount "${working_dir}"
track_mount /etc/resolv.conf "${working_dir}/etc/resolv.conf" -Br
track_mount ${cache_dirs[0]} "${working_dir}/${cache_dirs[0]}" -Br
if [[ -n $pkgdest ]]; then
track_mount $pkgdest "${working_dir}/$pkgdest" -Br
fi
if [[ -n $pkgsrc ]]; then
track_mount $pkgsrc "${working_dir}/$pkgsrc" -Br
fi
copy_hostconf
eval $(grep '^CARCH=' "$working_dir/etc/makepkg.conf")
exec ${CARCH:+setarch "$CARCH"} chroot "${working_dir}" "$@"