nix-tools/bin/basestrap.in

143 lines
3.9 KiB
Text
Raw Normal View History

2014-10-04 00:46:40 +02: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
#
2014-10-08 13:14:28 +02:00
version=@version@
2014-10-04 00:46:40 +02:00
shopt -s extglob
PKGDATADIR='@pkgdatadir@'
2014-11-13 16:31:15 +01:00
[[ -r @libdir@/util-msg.sh ]] && source @libdir@/util-msg.sh
[[ -r @libdir@/util-mount.sh ]] && source @libdir@/util-mount.sh
2014-10-04 00:46:40 +02:00
fix_dbus(){
# enable to have more debug info
#msg "machine-id (etc): $(cat $newroot/etc/machine-id)"
#[[ -e $newroot/var/lib/dbus/machine-id ]] && msg "machine-id (lib): $(cat $newroot/var/lib/dbus/machine-id)"
#msg "running processes: "
#lsof | grep $newroot
local PREFIX="$1" LINK PID NAME
for ROOT in /proc/*/root; do
LINK=$(readlink $ROOT)
if [ "x$LINK" != "x" ]; then
if [ "x${LINK:0:${#PREFIX}}" = "x$PREFIX" ]; then
# this process is in the chroot...
PID=$(basename $(dirname "$ROOT"))
NAME=$(ps -p $PID -o comm=)
msg3 "Killing chroot process: $NAME ($PID)"
kill -9 "$PID"
fi
fi
done
}
2014-10-04 00:46:40 +02:00
create_min_fs(){
msg 'Creating install root at %s' "$1"
mkdir -m 0755 -p $1/var/{cache/pacman/pkg,lib/pacman,log} $1/{dev,run,etc}
mkdir -m 1777 -p $1/tmp
mkdir -m 0555 -p $1/{sys,proc}
}
2014-10-04 00:46:40 +02:00
newroot=/mnt
2014-10-04 00:46:40 +02:00
hostcache=false
copykeyring=true
copymirrorlist=true
2014-10-04 00:46:40 +02:00
usage() {
echo "usage: ${0##*/} [options] root [packages...]"
echo " -C config Use an alternate config file for pacman"
echo " -c Use the package cache on the host, rather than the target"
echo " -d Allow installation to a non-mountpoint directory"
echo " -G Avoid copying the host's pacman keyring to the target"
echo " -i Avoid auto-confirmation of package selections"
echo " -M Avoid copying the host's mirrorlist to the target"
echo " -h Print this help message"
echo ''
echo ' basestrap installs packages to the specified new root directory.'
echo ' If no packages are given, basestrap defaults to the "base" group.'
echo ''
echo ''
exit $1
2014-10-04 00:46:40 +02:00
}
# if [[ -z $1 || $1 = @(-h|--help) ]]; then
# usage
# exit $(( $# ? 0 : 1 ))
# fi
#
# (( EUID == 0 )) || die 'This script must be run with root privileges'
2014-10-04 00:46:40 +02:00
orig_argv=("$@")
2014-10-04 00:46:40 +02:00
while getopts ':C:cdGiM' flag; do
case ${arg} in
C) pacman_config=$OPTARG ;;
B) branch=$OPTARG ;;
d) directory=true ;;
c) hostcache=true ;;
i) interactive=true ;;
G) copykeyring=false ;;
M) copymirrorlist=false ;;
*) echo "invalid argument '${arg}'"; usage 1;;
?) usage 0 ;;
esac
2014-10-04 00:46:40 +02:00
done
shift $(( OPTIND - 1 ))
check_root "$0" "${orig_argv[@]}"
2014-10-04 00:46:40 +02:00
(( $# )) || die "No root directory specified"
newroot=$1; shift
pacman_args=("${@:-base}")
${hostcache} || pacman_args+=(--cachedir="$newroot/var/cache/pacman/pkg")
2014-10-04 00:46:40 +02:00
${interactive} || pacman_args+=(--noconfirm)
2014-10-04 00:46:40 +02:00
[[ $pacman_config ]] && pacman_args+=(--config="$pacman_config")
2014-10-04 00:46:40 +02:00
[[ -d $newroot ]] || die "%s is not a directory" "$newroot"
if ! mountpoint -q "$newroot" && ! ${directory}; then
2014-10-04 00:46:40 +02:00
die '%s is not a mountpoint!' "$newroot"
fi
# create obligatory directories
create_min_fs "$newroot"
2014-10-04 00:46:40 +02:00
# mount API filesystems
2015-01-13 04:45:52 +01:00
chroot_api_mount "$newroot" || die "failed to setup API filesystems in new root"
2014-10-04 00:46:40 +02:00
2015-05-15 16:59:48 +02:00
msg2 'Installing packages to %s' "$newroot"
2014-10-04 00:46:40 +02:00
if ! pacman -r "$newroot" -Sy "${pacman_args[@]}"; then
die 'Failed to install packages to new root'
2014-10-04 00:46:40 +02:00
fi
2015-01-11 10:23:21 +01:00
# kill chroot process if needed (TODO: check if needed at all)
fix_dbus "$newroot"
if ${copykeyring}; then
# 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
2014-10-04 00:46:40 +02:00
fi
if ${copymirrorlist}; then
# install the host's mirrorlist onto the new root
#cp -a /etc/pacman.d/mirrorlist "$newroot/etc/pacman.d/"
cp -a ${PKGDATADIR}/pacman-mirrors-${branch}.conf "$newroot/etc" #/pacman.d/"
pacman-mirrors -g -b ${branch}
2014-10-04 00:46:40 +02:00
fi
# vim: et ts=2 sw=2 ft=sh: