manjaro-tools/bin/mkchroot.in
2014-11-11 21:19:26 +01:00

162 lines
4.3 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@/build-api.sh ]];then
# source @libdir@/build-api.sh
# fi
# CMD=''
# NOCOPY='n'
chrootdir=''
branch='stable'
APPNAME=$(basename "${0}")
# usage: usage <exitvalue>
usage() {
echo "Usage: ${APPNAME} [options] working-dir [package-list | app]"
echo ' options:'
echo ' -r <app> Run "app" within the context of the chroot'
# echo ' -u Update the chroot via pacman'
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 ' -n Do not copy config files into the chroot'
echo ' -c <dir> Set pacman cache'
echo ' -b <branch> Set repository branch'
echo ' -h This message'
exit 1
}
# orig_argv=("$@")
while getopts 'hC:M:S:c:b:' arg; do
case "${arg}" in
# r) CMD="$OPTARG" ;;
# u) CMD='pacman -Syu --noconfirm' ;;
C) pac_conf="$OPTARG" ;;
M) makepkg_conf="$OPTARG" ;;
# n) NOCOPY='y' ;;
c) cache_dir="$OPTARG" ;;
b) branch="$OPTARG" ;;
S) mirrors_conf="$OPTARG" ;;
h|?) usage ;;
*) error "invalid argument '${arg}'"; usage ;;
esac
done
shift $(($OPTIND - 1))
# check_root "$0" "${orig_argv[@]}"
if (( $EUID != 0 )); then
die 'This script must be run as root.'
fi
(( $# < 2 )) && die 'You must specify a directory and one or more packages.'
# if [[ -z $CMD ]] && (( $# < 2 )); then
# die 'You must specify a directory and one or more packages.'
# elif (( $# < 1 )); then
# die 'You must specify a directory.'
# fi
chrootdir="$(readlink -f ${1})"
shift 1
[[ -z $chrootdir ]] && die 'Please specify a working directory.'
get_cache_dirs(){
local cache_dirs
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
echo ${cache_dirs[@]}
}
copy_hostconf () {
cp -a /etc/pacman.d/gnupg "${chrootdir}/etc/pacman.d"
[[ -n $pac_conf ]] && cp $pac_conf "${chrootdir}/etc/pacman.conf"
[[ -n $makepkg_conf ]] && cp $makepkg_conf "${chrootdir}/etc/makepkg.conf"
[[ -n $mirrors_conf ]] && cp ${mirrors_conf} "${chrootdir}/etc/pacman-mirrors.conf"
host_mirror=$(echo "$host_mirror" | sed -E "s#/branch/#/${branch}/#")
echo "Server = $host_mirror" >"${chrootdir}/etc/pacman.d/mirrorlist"
sed -r "s|^#?\\s*CacheDir.+|CacheDir = $(echo -n $(get_cache_dirs))|g" -i "${chrootdir}/etc/pacman.conf"
}
chroot_lock () {
# Only reopen the FD if it wasn't handed to us
if [[ $(readlink -f /dev/fd/9) != "${chrootdir}.lock" ]]; then
exec 9>"${chrootdir}.lock"
fi
# Lock the chroot. Take note of the FD number.
if ! flock -n 9; then
stat_busy "Locking chroot"
flock 9
stat_done
fi
}
mk_chroot(){
if [[ -e ${chrootdir} ]]; then
die "Working directory '${chrootdir}' already exists"
fi
mkdir -p "${chrootdir}"
if [[ "$(stat -f -c %T "${chrootdir}")" == btrfs ]]; then
rmdir "${chrootdir}"
if ! btrfs subvolume create "${chrootdir}"; then
die "Couldn't create subvolume for '${chrootdir}'"
fi
chmod 0755 "${chrootdir}"
fi
chroot_lock
pacargs=("${cache_dirs[@]/#/--cachedir=}")
if [[ -n $pac_conf ]]; then
pacargs+=("--config=${pac_conf}")
fi
if ! basestrap -GMcd "${chrootdir}" "${pacargs[@]}" "$@"; then
die 'Failed to install all packages'
fi
printf '%s.UTF-8 UTF-8\n' en_US de_DE > "${chrootdir}/etc/locale.gen"
chroot "${chrootdir}" locale-gen
echo 'LANG=C' > "${chrootdir}/etc/locale.conf"
copy_hostconf
echo "${version}" > "${chrootdir}/.manjaro-chroot"
}
umask 0022
mk_chroot