2007-11-28 16:10:41 +01:00
|
|
|
#!/bin/bash
|
2007-10-22 04:08:55 +02:00
|
|
|
# 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.
|
2007-10-04 05:19:03 +02:00
|
|
|
|
|
|
|
FORCE="n"
|
|
|
|
RUN=""
|
2009-01-24 23:52:54 +01:00
|
|
|
NOCOPY="n"
|
2007-10-04 05:19:03 +02:00
|
|
|
|
|
|
|
working_dir=""
|
2010-02-12 10:14:04 +01:00
|
|
|
cache_dir=$((grep -m 1 '^CacheDir' /etc/pacman.conf || echo 'CacheDir = /var/cache/pacman/pkg') | sed 's/CacheDir\s*=\s*//')
|
2007-10-04 05:19:03 +02:00
|
|
|
|
|
|
|
APPNAME=$(basename "${0}")
|
|
|
|
|
2007-10-11 14:36:04 +02:00
|
|
|
# usage: usage <exitvalue>
|
2007-10-04 05:19:03 +02:00
|
|
|
usage ()
|
|
|
|
{
|
2007-10-24 06:50:39 +02:00
|
|
|
echo "usage ${APPNAME} [options] working-dir [package-list | app]"
|
2007-10-04 05:19:03 +02:00
|
|
|
echo " options:"
|
2007-10-11 14:36:04 +02:00
|
|
|
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"
|
2010-02-11 11:23:41 +01:00
|
|
|
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}"
|
2007-10-11 14:36:04 +02:00
|
|
|
echo " -h This message"
|
|
|
|
exit $1
|
2007-10-04 05:19:03 +02:00
|
|
|
}
|
|
|
|
|
2010-02-11 14:41:45 +01:00
|
|
|
while getopts 'r:ufnhC:M:c:' arg; do
|
2007-10-04 05:19:03 +02:00
|
|
|
case "${arg}" in
|
2007-10-04 06:24:29 +02:00
|
|
|
r) RUN="$OPTARG" ;;
|
2007-10-24 06:50:39 +02:00
|
|
|
u) RUN="pacman -Syu" ;;
|
2007-10-04 05:19:03 +02:00
|
|
|
f) FORCE="y" ;;
|
2007-11-02 01:36:11 +01:00
|
|
|
C) pac_conf="$OPTARG" ;;
|
2007-11-22 19:50:16 +01:00
|
|
|
M) makepkg_conf="$OPTARG" ;;
|
2009-01-24 23:52:54 +01:00
|
|
|
n) NOCOPY="y" ;;
|
2010-02-11 11:23:41 +01:00
|
|
|
c) cache_dir="$OPTARG" ;;
|
2007-10-11 14:36:04 +02:00
|
|
|
h|?) usage 0 ;;
|
|
|
|
*) echo "invalid argument '${arg}'"; usage 1 ;;
|
2007-10-04 05:19:03 +02:00
|
|
|
esac
|
|
|
|
done
|
2007-10-11 14:37:23 +02:00
|
|
|
|
|
|
|
if [ "$EUID" != "0" ]; then
|
|
|
|
echo "error: this script must be run as root."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2007-10-04 05:19:03 +02:00
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
|
2007-11-28 16:10:41 +01:00
|
|
|
if [ "$RUN" = "" -a $# -lt 2 ]; then
|
2007-10-04 06:24:29 +02:00
|
|
|
echo "you must specify a directory and one or more packages"
|
2007-10-11 14:36:04 +02:00
|
|
|
usage 1
|
2007-10-04 06:24:29 +02:00
|
|
|
elif [ $# -lt 1 ]; then
|
|
|
|
echo "you must specify a directory"
|
2007-10-11 14:36:04 +02:00
|
|
|
usage 1
|
2007-10-04 06:24:29 +02:00
|
|
|
fi
|
2007-10-04 05:19:03 +02:00
|
|
|
|
|
|
|
working_dir="$(readlink -f ${1})"
|
|
|
|
shift 1
|
|
|
|
|
2007-10-11 14:36:04 +02:00
|
|
|
[ "${working_dir}" = "" ] && echo "error: please specify a working directory" && usage 1
|
2007-10-04 05:19:03 +02:00
|
|
|
|
|
|
|
# {{{ functions
|
|
|
|
|
2007-11-28 16:10:41 +01:00
|
|
|
chroot_mount ()
|
2007-10-04 05:19:03 +02:00
|
|
|
{
|
|
|
|
[ -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"
|
|
|
|
|
2010-02-11 11:23:41 +01:00
|
|
|
[ -e "${working_dir}/var/cache/pacman/pkg" ] || mkdir -p "${working_dir}/var/cache/pacman/pkg"
|
2010-02-11 23:28:02 +01:00
|
|
|
[ -e "${cache_dir}" ] || mkdir -p "${cache_dir}"
|
2010-02-11 11:23:41 +01:00
|
|
|
mount -o bind "${cache_dir}" "${working_dir}/var/cache/pacman/pkg"
|
2007-10-04 05:19:03 +02:00
|
|
|
|
2008-02-06 06:59:53 +01:00
|
|
|
trap 'chroot_umount' 0 1 2 15
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_hostconf ()
|
|
|
|
{
|
2007-11-04 23:55:42 +01:00
|
|
|
cp /etc/mtab "${working_dir}/etc/mtab"
|
2007-11-29 05:18:26 +01:00
|
|
|
cp /etc/resolv.conf "${working_dir}/etc/resolv.conf"
|
|
|
|
|
2007-10-04 05:19:03 +02:00
|
|
|
}
|
|
|
|
|
2007-11-28 16:10:41 +01:00
|
|
|
chroot_umount ()
|
2007-10-04 05:19:03 +02:00
|
|
|
{
|
|
|
|
umount "${working_dir}/proc"
|
|
|
|
umount "${working_dir}/sys"
|
|
|
|
umount "${working_dir}/dev"
|
2010-02-11 11:23:41 +01:00
|
|
|
umount "${working_dir}/var/cache/pacman/pkg"
|
2007-10-04 05:19:03 +02:00
|
|
|
}
|
|
|
|
# }}}
|
|
|
|
|
2010-02-13 07:22:54 +01:00
|
|
|
umask 0022
|
2007-10-04 05:19:03 +02:00
|
|
|
if [ "$RUN" != "" ]; then
|
2007-11-29 04:20:24 +01:00
|
|
|
# run chroot {{{
|
2007-10-04 05:19:03 +02:00
|
|
|
#Sanity check
|
|
|
|
if [ ! -f "${working_dir}/.arch-chroot" ]; then
|
2007-10-11 14:36:04 +02:00
|
|
|
echo "error: '${working_dir}' does not appear to be a Arch chroot"
|
2007-11-29 04:28:29 +01:00
|
|
|
echo " please build the image using mkarchroot"
|
2007-10-04 05:19:03 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
chroot_mount
|
2008-02-06 06:59:53 +01:00
|
|
|
copy_hostconf
|
2007-10-04 05:19:03 +02:00
|
|
|
|
2007-11-04 23:55:02 +01:00
|
|
|
chroot "${working_dir}" ${RUN}
|
2007-10-04 05:19:03 +02:00
|
|
|
|
2007-11-29 04:20:24 +01:00
|
|
|
# }}}
|
2007-10-04 05:19:03 +02:00
|
|
|
else
|
2007-11-29 04:20:24 +01:00
|
|
|
# {{{ build chroot
|
2007-10-04 05:19:03 +02:00
|
|
|
if [ -e "${working_dir}" -a "${FORCE}" = "n" ]; then
|
2007-10-11 14:36:04 +02:00
|
|
|
echo "error: working dir '${working_dir}' already exists - try using -f"
|
2007-10-04 05:19:03 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2007-10-24 06:51:15 +02:00
|
|
|
mkdir -p "${working_dir}/var/lib/pacman/"
|
2007-11-22 19:52:44 +01:00
|
|
|
mkdir -p "${working_dir}/etc/"
|
2007-10-04 05:19:03 +02:00
|
|
|
|
|
|
|
chroot_mount
|
|
|
|
|
|
|
|
pacargs="--noconfirm -v " #--noprogressbar -v
|
|
|
|
pacargs="$pacargs --root=${working_dir}"
|
2007-10-07 04:58:16 +02:00
|
|
|
# pacman takes these as relative to the given root
|
|
|
|
pacargs="$pacargs --cachedir=/var/cache/pacman/pkg"
|
2007-11-02 01:36:11 +01:00
|
|
|
if [ "$pac_conf" != "" ]; then
|
|
|
|
pacargs="$pacargs --config=${pac_conf}"
|
|
|
|
fi
|
2007-10-04 05:19:03 +02:00
|
|
|
|
2010-02-11 12:49:28 +01:00
|
|
|
if [ $# -ne 0 ]; then
|
2007-10-04 05:19:03 +02:00
|
|
|
op="-Sy"
|
2007-11-28 16:10:41 +01:00
|
|
|
if [ "$FORCE" = "y" ]; then
|
2007-10-04 05:19:03 +02:00
|
|
|
op="${op}f"
|
|
|
|
fi
|
|
|
|
if ! pacman ${op} ${pacargs} $@; then
|
2007-10-24 06:50:39 +02:00
|
|
|
echo "error: failed to install all packages"
|
2007-10-04 05:19:03 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d "${working_dir}/lib/modules" ]; then
|
|
|
|
ldconfig -r "${working_dir}"
|
|
|
|
fi
|
|
|
|
|
2009-01-24 23:52:54 +01:00
|
|
|
if [ "$pac_conf" != "" -a "$NOCOPY" = "n" ]; then
|
2007-11-03 17:57:26 +01:00
|
|
|
cp ${pac_conf} ${working_dir}/etc/pacman.conf
|
|
|
|
fi
|
|
|
|
|
2009-01-24 23:52:54 +01:00
|
|
|
if [ "$makepkg_conf" != "" -a "$NOCOPY" = "n" ]; then
|
2007-11-22 19:50:16 +01:00
|
|
|
cp ${makepkg_conf} ${working_dir}/etc/makepkg.conf
|
|
|
|
fi
|
|
|
|
|
2008-10-20 08:09:43 +02:00
|
|
|
if [ -e "${working_dir}/etc/locale.gen" ]; then
|
|
|
|
cp /etc/locale.gen "${working_dir}/etc/locale.gen"
|
|
|
|
chroot "${working_dir}" locale-gen
|
|
|
|
fi
|
2007-10-07 19:30:30 +02:00
|
|
|
|
2007-10-04 05:19:03 +02:00
|
|
|
if [ ! -e "${working_dir}/.arch-chroot" ]; then
|
|
|
|
date +%s > "${working_dir}/.arch-chroot"
|
|
|
|
fi
|
2007-11-29 04:20:24 +01:00
|
|
|
# }}}
|
2007-10-04 05:19:03 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# vim:ft=sh:ts=4:sw=4:et:
|