Further cleanups and start of install.sh within chroot

This commit is contained in:
Stefan Reimer 2014-10-07 20:26:49 -07:00
parent 56b40e082f
commit 7408869fdd

View File

@ -8,7 +8,7 @@
#=============================================================================== #===============================================================================
GENTOO_MIRROR="http://distfiles.gentoo.org" GENTOO_MIRROR="http://distfiles.gentoo.org"
LOCAL_CACHE=/var/tmp LOCAL_CACHE=/var/tmp
IMAGE_ROOT=/mnt/gentoo CHROOT=/mnt/gentoo
set -o nounset set -o nounset
@ -107,57 +107,25 @@ fetch_file() {
} }
# bootstrap ROOT_FS PROFILE ARCH # bootstrap, download stage3 and portage snapshot
bootstrap() { bootstrap() {
local ROOT_FS=$1
local PROFILE=$2
local ARCH=$3
local STAGE_PATH [ -d ${CHROOT} ] || die "${CHROOT} does not exists"
local STAGE_ARCH [ -w ${CHROOT} ] || die "${CHROOT} isn't writable"
local LATEST_STAGE_FILE
local ESELECT_PROFILE
if [ "${ARCH}" = "i686" ] ; then cd ${CHROOT}
STAGE_ARCH=${ARCH}
# Why do they use x86 here ? :(
STAGE_PATH="${GENTOO_MIRROR}/releases/x86/autobuilds"
elif [ "${ARCH}" = "x86_64" ] ; then
STAGE_ARCH="amd64"
STAGE_PATH="${GENTOO_MIRROR}/releases/${STAGE_ARCH}/autobuilds"
else
die "Unknown architecture!"
fi
if [ "${PROFILE}" = "hardened" ] ; then
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}-hardened.txt"
ESELECT_PROFILE="hardened/linux/${ARCH}"
elif [ "${PROFILE}" = "hardened-no-multilib" ] ; then
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}-hardened+nomultilib.txt"
ESELECT_PROFILE="hardened/linux/${ARCH}/no-multilib"
elif [ "${PROFILE}" = "default" ] ; then
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}.txt"
ESELECT_PROFILE="default/linux/${ARCH}/13.0"
else
die "Unknown profile!"
fi
[ -d ${ROOT_FS} ] || die "${ROOT_FS} does not exists"
[ -w ${ROOT_FS} ] || die "${ROOT_FS} isn't writable"
cd ${ROOT_FS}
# first install stage 3 # first install stage 3
if [ -d "usr" ] ; then if [ -d "usr" ] ; then
echo "There seems to be already files in ${ROOT_FS} !" echo "There seems to be already files in ${CHROOT} !"
echo "Press <Ctrl+c> to abort, or <Return> to proceed without extracting stage3 ..." echo "Press <Ctrl+c> to abort, or <Return> to proceed without extracting stage3 ..."
read -r REPLY read -r REPLY
else else
STAGE_TARBALL=${GENTOO_MIRROR}/releases/${STAGE_ARCH}/autobuilds/$(curl -s ${LATEST_STAGE_FILE} | grep -v "^#" | head -n 1) STAGE_TARBALL=${GENTOO_MIRROR}/releases/${ARCH}/autobuilds/$(curl -s ${LATEST_STAGE_FILE} | grep -v "^#" | head -n 1)
fetch_file "${STAGE_TARBALL}" "${STAGE_TARBALL}.DIGESTS" || die "Cannot get ${STAGE_TARBALL}" fetch_file "${STAGE_TARBALL}" "${STAGE_TARBALL}.DIGESTS" || die "Cannot get ${STAGE_TARBALL}"
echo "Extracting stage3 to ${ROOT_FS} ..." echo "Extracting stage3 to ${CHROOT} ..."
tar jxpf $(basename ${STAGE_TARBALL}) || die "Extracting stage3 failed" tar jxpf $(basename ${STAGE_TARBALL}) || die "Extracting stage3 failed"
rm -f $(basename ${STAGE_TARBALL}) rm -f $(basename ${STAGE_TARBALL})
@ -168,8 +136,8 @@ bootstrap() {
_PORTAGE_MOUNTED=0 _PORTAGE_MOUNTED=0
if [ "x${BIND_PORTAGE}" != "x" -a -d ${BIND_PORTAGE} ] ; then if [ "x${BIND_PORTAGE}" != "x" -a -d ${BIND_PORTAGE} ] ; then
mkdir -p ${ROOT_FS}/usr/portage mkdir -p ${CHROOT}/usr/portage
mount --bind ${BIND_PORTAGE} ${ROOT_FS}/usr/portage || die "Error mounting ${BIND_PORTAGE}" mount --bind ${BIND_PORTAGE} ${CHROOT}/usr/portage || die "Error mounting ${BIND_PORTAGE}"
# Remember we mounted portage # Remember we mounted portage
_PORTAGE_MOUNTED=1 _PORTAGE_MOUNTED=1
@ -181,42 +149,65 @@ bootstrap() {
read -r REPLY read -r REPLY
else else
fetch_file "${PORTAGE_SNAPSHOT}" "${PORTAGE_SNAPSHOT}.md5sum" fetch_file "${PORTAGE_SNAPSHOT}" "${PORTAGE_SNAPSHOT}.md5sum"
echo "Extracting latest portage snapshot to ${ROOT_FS}/usr ..." echo "Extracting latest portage snapshot to ${CHROOT}/usr ..."
tar jxf $(basename ${PORTAGE_SNAPSHOT}) -C "${ROOT_FS}/usr" || die "Extracting portage snapshot failed" tar jxf $(basename ${PORTAGE_SNAPSHOT}) -C "${CHROOT}/usr" || die "Extracting portage snapshot failed"
rm -f portage-latest.tar.bz2 rm -f portage-latest.tar.bz2
fi fi
fi fi
} }
# setup_chroot ROOT_FS # setup_chroot CHROOT
setup_chroot() { setup_chroot() {
local ROOT_FS=$1
# resolve.conf
cp -L /etc/resolv.conf ${ROOT_FS}/etc/resolv.conf || die "Can't copy resolv.conf"
# mount pseudo filesystems # mount pseudo filesystems
mount -t proc none ${ROOT_FS}/proc || die "Error mounting /proc" mount -t proc none ${CHROOT}/proc || die "Error mounting /proc"
mount --rbind /dev ${ROOT_FS}/dev || die "Error mounting /dev" mount --rbind /dev ${CHROOT}/dev || die "Error mounting /dev"
mount --rbind /sys ${ROOT_FS}/sys || die "Error mounting /sys" mount --rbind /sys ${CHROOT}/sys || die "Error mounting /sys"
} }
# Actually prepare the install script running within chroot # Actually prepare the install script running within chroot
# and run it # and run it
install_gentoo() { install_gentoo() {
local ROOT_FS=$1
if [ ${INTERACTIVE} = 1 ]; then if [ ${INTERACTIVE} = 1 ]; then
echo "Done. Entering chroot environment. Good luck..." echo "Done. Entering chroot environment. All yours..."
chroot ${ROOT_FS} /bin/bash chroot ${CHROOT} /bin/bash
else else
# Install make.conf # resolve.conf
echo "Copy resolv.conf from host"
cp -L /etc/resolv.conf ${CHROOT}/etc/resolv.conf || die "Can't copy resolv.conf"
# emerge --sync --quiet # Install make.conf
if [ "x${MAKE_CONF}" != "x" ]; then
[ -r ${MAKE_CONF} ] || die "Cannot read ${MAKE_CONF}"
echo "Using custom make.conf"
cp ${MAKE_CONF} ${CHROOT}/etc/portage/
fi
# From here we create the install script and execute it within the chroot at the end
cat << 'EOF' > ${CHROOT}/tmp/install.sh
#!/bin/bash
set -x
source /etc/profile
export PS1="(chroot) $PS1"
EOF
# Sync portage if not mounted
if [ ${_PORTAGE_MOUNTED} = 0 ]; then
cat << 'EOF' >> ${CHROOT}/tmp/install.sh
echo "Syncing portage snapshot..."
emerge -p --sync --quiet
EOF
fi
# eselect profile # eselect profile
cat << EOF >> ${CHROOT}/tmp/install.sh
echo "Setting profile to ${ESELECT_PROFILE}"
eselect profile set ${ESELECT_PROFILE}
EOF
# Set Timezone # Set Timezone
@ -226,6 +217,9 @@ install_gentoo() {
# boot + kernel + lib/modules # boot + kernel + lib/modules
# /etc/fstab # /etc/fstab
chmod 755 ${CHROOT}/tmp/install.sh
chroot ${CHROOT} /tmp/install.sh
echo "Done !" echo "Done !"
echo "Press <Return> to tear down the chroot environment once you are done." echo "Press <Return> to tear down the chroot environment once you are done."
read -r REPLY read -r REPLY
@ -236,18 +230,17 @@ install_gentoo() {
# Clean up host # Clean up host
cleanup() { cleanup() {
local ROOT_FS=$1 umount ${CHROOT}/dev/pts ${CHROOT}/dev ${CHROOT}/sys ${CHROOT}/proc
umount ${ROOT_FS}/dev/pts ${ROOT_FS}/dev ${ROOT_FS}/sys ${ROOT_FS}/proc
if [ ${_PORTAGE_MOUNTED} != 0 ]; then if [ ${_PORTAGE_MOUNTED} != 0 ]; then
umount ${ROOT_FS}/usr/portage umount ${CHROOT}/usr/portage
else else
rm -rf ${ROOT_FS}/usr/portage/distfiles/* rm -rf ${CHROOT}/usr/portage/distfiles/*
fi fi
# Clean up chroot # Clean up chroot
rm -rf ${ROOT_FS}/tmp/* rm -rf ${CHROOT}/tmp/*
rm -rf ${ROOT_FS}/var/tmp/* rm -rf ${CHROOT}/var/tmp/*
} }
@ -263,7 +256,7 @@ OPTIONS:
-a arch, either i686 or x86_64, defaults to uname -m -a arch, either i686 or x86_64, defaults to uname -m
-p profile, [ hardened | hardened-no-multilib | default *] -p profile, [ hardened | hardened-no-multilib | default *]
-t The timezone to use, default to GMT -t The timezone to use, default to GMT
-r chroot location (default $IMAGE_ROOT ) -r chroot location (default $CHROOT )
-c local cache (default $LOCAL_CACHE) -c local cache (default $LOCAL_CACHE)
-b bind mount portage tree from, instead of downloading portage snapshot -b bind mount portage tree from, instead of downloading portage snapshot
-i interactive, enter chroot only, do NOT run install script -i interactive, enter chroot only, do NOT run install script
@ -275,7 +268,7 @@ EOF
DEBUG=0 DEBUG=0
INTERACTIVE=0 INTERACTIVE=0
MAKE_CONF="/etc/portage/make.conf" MAKE_CONF=""
BIND_PORTAGE="" BIND_PORTAGE=""
while getopts ":a:p:t:r:c:m:b:dhi" OPTIONS; do while getopts ":a:p:t:r:c:m:b:dhi" OPTIONS; do
case $OPTIONS in case $OPTIONS in
@ -284,7 +277,7 @@ while getopts ":a:p:t:r:c:m:b:dhi" OPTIONS; do
t ) TIMEZONE=$OPTARG;; t ) TIMEZONE=$OPTARG;;
d ) DEBUG=1;; d ) DEBUG=1;;
b ) BIND_PORTAGE=$OPTARG;; b ) BIND_PORTAGE=$OPTARG;;
r ) IMAGE_ROOT=$OPTARG;; r ) CHROOT=$OPTARG;;
c ) LOCAL_CACHE=$OPTARG;; c ) LOCAL_CACHE=$OPTARG;;
i ) INTERACTIVE=1;; i ) INTERACTIVE=1;;
m ) MAKE_CONF=$OPTARG;; m ) MAKE_CONF=$OPTARG;;
@ -304,14 +297,40 @@ ARCH=${ARCH-"$(uname -m)"}
PROFILE=${PROFILE="default"} PROFILE=${PROFILE="default"}
TIMEZONE=${TIMEZONE-"GMT"} TIMEZONE=${TIMEZONE-"GMT"}
if [ "${ARCH}" = "i686" ] ; then
# Why do they use x86 here ? :(
STAGE_PATH="${GENTOO_MIRROR}/releases/x86/autobuilds"
elif [ "${ARCH}" = "x86_64" ] ; then
ARCH="amd64"
STAGE_PATH="${GENTOO_MIRROR}/releases/${ARCH}/autobuilds"
elif [ "${ARCH}" = "amd64" ] ; then
STAGE_PATH="${GENTOO_MIRROR}/releases/${ARCH}/autobuilds"
else
die "Unknown architecture!"
fi
if [ "${PROFILE}" = "hardened" ] ; then
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${ARCH}-hardened.txt"
ESELECT_PROFILE="hardened/linux/${ARCH}"
elif [ "${PROFILE}" = "hardened-no-multilib" ] ; then
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${ARCH}-hardened+nomultilib.txt"
ESELECT_PROFILE="hardened/linux/${ARCH}/no-multilib"
elif [ "${PROFILE}" = "default" ] ; then
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${ARCH}.txt"
ESELECT_PROFILE="default/linux/${ARCH}/13.0"
else
die "Unknown profile!"
fi
if [ ${DEBUG} -eq 1 ]; then if [ ${DEBUG} -eq 1 ]; then
set -x set -x
fi fi
bootstrap ${IMAGE_ROOT} ${PROFILE} ${ARCH} bootstrap
# From here make sure we don't leave stuff around on the host # From here make sure we don't leave stuff around on the host
trap "cleanup ${IMAGE_ROOT}" INT TERM EXIT trap "cleanup" INT TERM EXIT
setup_chroot ${IMAGE_ROOT} setup_chroot
install_gentoo ${IMAGE_ROOT}
install_gentoo