Skip to content
mkarchroot 4.73 KiB
Newer Older
Aaron Griffin's avatar
Aaron Griffin committed
# 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.
Aaron Griffin's avatar
Aaron Griffin committed

FORCE="n"
RUN=""
Aaron Griffin's avatar
Aaron Griffin committed

working_dir=""
cache_dir=$(grep -m 1 '^CacheDir' /etc/pacman.conf | sed 's/CacheDir\s*=\s*//')
Aaron Griffin's avatar
Aaron Griffin committed

APPNAME=$(basename "${0}")

# usage: usage <exitvalue>
Aaron Griffin's avatar
Aaron Griffin committed
usage ()
{
    echo "usage ${APPNAME} [options] working-dir [package-list | app]"
Aaron Griffin's avatar
Aaron Griffin committed
    echo " options:"
    echo "    -r <app>      Run 'app' within the context of the chroot"
    echo "    -u            Update the chroot via pacman"
    echo "    -f            Force overwrite of files in the working-dir"
    echo "    -C <file>     Location of a pacman config file"
    echo "    -M <file>     Location of a makepkg config file"
    echo "    -n            Do not copy config files into the chroot"
    echo "    -c <dir>      Set pacman cache. Default: ${cache_dir}"
    echo "    -h            This message"
    exit $1
while getopts 'r:ufnhC:M:c:' arg; do
Aaron Griffin's avatar
Aaron Griffin committed
    case "${arg}" in
        r) RUN="$OPTARG" ;;
        u) RUN="pacman -Syu" ;;
Aaron Griffin's avatar
Aaron Griffin committed
        f) FORCE="y" ;;
        C) pac_conf="$OPTARG" ;;
        M) makepkg_conf="$OPTARG" ;;
        c) cache_dir="$OPTARG" ;;
        h|?) usage 0 ;;
        *) echo "invalid argument '${arg}'"; usage 1 ;;
Aaron Griffin's avatar
Aaron Griffin committed
    esac
done

if [ "$EUID" != "0" ]; then
    echo "error: this script must be run as root."
    exit 1
fi

Aaron Griffin's avatar
Aaron Griffin committed
shift $(($OPTIND - 1))

if [ "$RUN" = "" -a $# -lt 2 ]; then
    echo "you must specify a directory and one or more packages"
elif [ $# -lt 1 ]; then
    echo "you must specify a directory"
Aaron Griffin's avatar
Aaron Griffin committed

working_dir="$(readlink -f ${1})"
shift 1

[ "${working_dir}" = "" ] && echo "error: please specify a working directory" && usage 1
Aaron Griffin's avatar
Aaron Griffin committed

# {{{ functions 

chroot_mount ()
Aaron Griffin's avatar
Aaron Griffin committed
{
    [ -e "${working_dir}/sys" ] || mkdir "${working_dir}/sys"
    mount -t sysfs sysfs "${working_dir}/sys"

    [ -e "${working_dir}/proc" ] || mkdir "${working_dir}/proc"
    mount -t proc proc "${working_dir}/proc"

    [ -e "${working_dir}/dev" ] || mkdir "${working_dir}/dev"
    mount -o bind /dev "${working_dir}/dev"

    [ -e "${working_dir}/var/cache/pacman/pkg" ] || mkdir -p "${working_dir}/var/cache/pacman/pkg"
    [ -e "${cache_dir}" ] || mkdir -p "${cache_dir}"
    mount -o bind "${cache_dir}" "${working_dir}/var/cache/pacman/pkg"
    trap 'chroot_umount' 0 1 2 15
}

copy_hostconf ()
{
    cp /etc/mtab "${working_dir}/etc/mtab"
    cp /etc/resolv.conf "${working_dir}/etc/resolv.conf"

chroot_umount ()
Aaron Griffin's avatar
Aaron Griffin committed
{
    umount "${working_dir}/proc"
    umount "${working_dir}/sys"
    umount "${working_dir}/dev"
    umount "${working_dir}/var/cache/pacman/pkg"
Aaron Griffin's avatar
Aaron Griffin committed
if [ "$RUN" != "" ]; then
    # run chroot {{{
Aaron Griffin's avatar
Aaron Griffin committed
    #Sanity check
    if [ ! -f "${working_dir}/.arch-chroot" ]; then
        echo "error: '${working_dir}' does not appear to be a Arch chroot"
        echo "  please build the image using mkarchroot"
Aaron Griffin's avatar
Aaron Griffin committed
        exit 1
    fi

    chroot_mount
    copy_hostconf
    chroot "${working_dir}" ${RUN}
Aaron Griffin's avatar
Aaron Griffin committed
else
    # {{{ build chroot
Aaron Griffin's avatar
Aaron Griffin committed
    if [ -e "${working_dir}" -a "${FORCE}" = "n" ]; then
        echo "error: working dir '${working_dir}' already exists - try using -f"
    mkdir -p "${working_dir}/var/lib/pacman/"
Aaron Griffin's avatar
Aaron Griffin committed

    chroot_mount

    pacargs="--noconfirm -v " #--noprogressbar -v
    pacargs="$pacargs --root=${working_dir}"
    # pacman takes these as relative to the given root
    pacargs="$pacargs --cachedir=/var/cache/pacman/pkg"
    if [ "$pac_conf" != "" ]; then
        pacargs="$pacargs --config=${pac_conf}"
    fi
    if [ $# -ne 0 ]; then
Aaron Griffin's avatar
Aaron Griffin committed
        op="-Sy"
        if [ "$FORCE" = "y" ]; then
Aaron Griffin's avatar
Aaron Griffin committed
            op="${op}f"
        fi
        if ! pacman ${op} ${pacargs} $@; then
            echo "error: failed to install all packages"
Aaron Griffin's avatar
Aaron Griffin committed
            exit 1
        fi
    fi

    if [ -d "${working_dir}/lib/modules" ]; then
        ldconfig -r "${working_dir}"
    fi

    if [ "$pac_conf" != "" -a "$NOCOPY" = "n" ]; then
    if [ "$makepkg_conf" != "" -a "$NOCOPY" = "n" ]; then
        cp ${makepkg_conf} ${working_dir}/etc/makepkg.conf
    fi

    if [ -e "${working_dir}/etc/locale.gen" ]; then
        cp /etc/locale.gen "${working_dir}/etc/locale.gen"
        chroot "${working_dir}" locale-gen
    fi
Aaron Griffin's avatar
Aaron Griffin committed
    if [ ! -e "${working_dir}/.arch-chroot" ]; then
        date +%s > "${working_dir}/.arch-chroot"
    fi
Aaron Griffin's avatar
Aaron Griffin committed
fi

# vim:ft=sh:ts=4:sw=4:et: