future-install-scripts/pacstrap.in

94 lines
2.3 KiB
Plaintext
Raw Normal View History

2012-06-18 03:17:10 +08:00
#!/bin/bash
#
# Assumptions:
# 1) User has partitioned, formatted, and mounted partitions on /mnt
# 2) Network is functional
# 3) Arguments passed to the script are valid pacman targets
# 4) A valid mirror appears in /etc/pacman.d/mirrorlist
#
2012-06-18 05:52:39 +08:00
shopt -s extglob
m4_include(common)
2012-06-18 03:17:10 +08:00
newroot=/mnt
hostcache=0
2012-06-18 03:17:10 +08:00
2012-06-18 05:52:39 +08:00
usage() {
cat <<EOF
usage: ${0##*/} [options] root [packages...]
2012-06-18 05:52:39 +08:00
Options:
-c Use the package cache on the host, rather than the target
-d Allow installation to a non-mountpoint directory
pacstrap installs packages to the specified new root directory. If no packages
are given, pacstrap defaults to the "base" group.
2012-06-18 05:52:39 +08:00
EOF
}
if [[ -z $1 || $1 = @(-h|--help) ]]; then
usage
exit $(( $# ? 0 : 1 ))
fi
(( EUID == 0 )) || die 'This script must be run with root privileges'
while getopts ':cd' flag; do
2012-06-18 03:17:10 +08:00
case $flag in
d)
directory=1
;;
c)
hostcache=1
;;
:)
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
;;
2012-06-18 03:17:10 +08:00
?)
die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
;;
esac
done
shift $(( OPTIND - 1 ))
(( $# )) || die "No root directory specified"
newroot=$1; shift
pacman_args=("${@:-base}")
if (( ! hostcache )); then
pacman_args+=(--cachedir="$newroot/var/cache/pacman/pkg")
2012-06-18 03:17:10 +08:00
fi
[[ -d $newroot ]] || die "%s is not a directory" "$newroot"
if ! mountpoint -q "$newroot" && (( ! directory )); then
die '%s is not a mountpoint!' "$newroot"
fi
2012-06-18 03:17:10 +08:00
# create obligatory directories
msg 'Creating install root at %s' "$newroot"
mkdir -p "$newroot"/var/{cache/pacman/pkg,lib/pacman} "$newroot"/{dev,proc,sys,run,tmp,etc}
2012-06-18 03:17:10 +08:00
# always call umount on quit after this point
trap 'api_fs_umount "$newroot" 2>/dev/null' EXIT
2012-06-18 03:17:10 +08:00
# mount API filesystems
2012-06-18 03:27:38 +08:00
api_fs_mount "$newroot" || die "failed to setup API filesystems in new root"
2012-06-18 03:17:10 +08:00
msg 'Installing packages to %s' "$newroot"
if ! pacman -r "$newroot" -Sy --noconfirm "${pacman_args[@]}"; then
die 'Failed to install packages to new root'
fi
# if there's a keyring on the host, copy it into the new root, unless it exists already
if [[ -d /etc/pacman.d/gnupg && ! -d $newroot/etc/pacman.d/gnupg ]]; then
cp -a /etc/pacman.d/gnupg "$newroot/etc/pacman.d/"
fi
2012-06-18 03:17:10 +08:00
# install the host's mirrorlist onto the new root
cp -a /etc/pacman.d/mirrorlist "$newroot/etc/pacman.d/"
2012-06-18 04:05:18 +08:00
# vim: et ts=2 sw=2 ft=sh: