2011-10-28 21:00:47 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# - builds a generic image from a stage3 tarball and portage snapshot
|
|
|
|
#
|
|
|
|
# based on work from JD Harrington https://github.com/psi/gentoo-aws
|
|
|
|
# and Matsuu Takuto https://gist.github.com/870789
|
|
|
|
|
|
|
|
#===============================================================================
|
2014-10-03 06:59:42 +00:00
|
|
|
GENTOO_MIRROR="http://distfiles.gentoo.org"
|
2011-10-31 22:50:15 +00:00
|
|
|
LOCAL_CACHE=/var/tmp
|
2014-10-08 03:26:49 +00:00
|
|
|
CHROOT=/mnt/gentoo
|
2012-04-11 17:14:38 +00:00
|
|
|
|
2011-10-28 21:00:47 +00:00
|
|
|
|
2011-11-21 23:54:45 +00:00
|
|
|
set -o nounset
|
2011-10-28 21:00:47 +00:00
|
|
|
|
|
|
|
die() {
|
2013-04-27 18:07:42 +00:00
|
|
|
echo $@
|
|
|
|
exit 1
|
2011-10-28 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 00:15:03 +00:00
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
verify_digest() {
|
|
|
|
local FILE_NAME=$1
|
|
|
|
local TYPE=$2
|
|
|
|
local DIGEST=$3
|
|
|
|
local PURGE=$4
|
|
|
|
|
|
|
|
case $TYPE in
|
|
|
|
sha512)
|
|
|
|
FILE_DIGEST=$(sha512sum ${FILE_NAME} | awk '{print $1}')
|
|
|
|
;;
|
|
|
|
md5)
|
|
|
|
FILE_DIGEST=$(md5sum ${FILE_NAME} | awk '{print $1}')
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown DIGEST"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2014-10-05 22:13:54 +00:00
|
|
|
if [ "${FILE_DIGEST}" = "${DIGEST}" ]; then
|
2014-10-04 20:35:46 +00:00
|
|
|
echo "${TYPE} checksum for ${FILE_NAME} verified."
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
echo "Invalid checksum for ${FILE_NAME}!"
|
|
|
|
[ "x${PURGE}" != "x" ] && { echo "Removing cached copy." ; rm -f ${FILE_NAME}; }
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-31 22:50:15 +00:00
|
|
|
fetch_file() {
|
2013-04-27 18:07:42 +00:00
|
|
|
local URL=$1
|
2014-10-04 20:35:46 +00:00
|
|
|
local DIGEST_URL=$2
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
local FILE_NAME=$(basename ${URL})
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
local DIGEST_TYPE
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
# Check cache
|
|
|
|
if [ ! -d ${LOCAL_CACHE} ] || [ ! -w ${LOCAL_CACHE} ]; then
|
|
|
|
echo "Cannot write to cache ${LOCAL_CACHE}"
|
|
|
|
return 1
|
2013-04-27 18:07:42 +00:00
|
|
|
fi
|
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
# If DIGEST requested get it
|
|
|
|
if [ ! -z ${DIGEST_URL} ]; then
|
|
|
|
DIGEST=$(curl -s -S ${DIGEST_URL} | grep -A1 -e "^# SHA512 HASH" | grep -o -E -e "^[0-9a-f]{128} *${FILE_NAME}$" | awk '{print $1}')
|
|
|
|
if [ -z ${DIGEST} ]; then
|
|
|
|
# Let's try md5sum before giving up
|
|
|
|
DIGEST=$(curl -s -S ${DIGEST_URL} | grep -o -E -e "^[0-9a-f]{32} *${FILE_NAME}$" | awk '{print $1}')
|
|
|
|
if [ -z ${DIGEST} ]; then
|
|
|
|
echo "Unable to get checksum for ${FILE_NAME}, abort"
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
DIGEST_TYPE="md5"
|
2013-04-27 18:07:42 +00:00
|
|
|
else
|
2014-10-04 20:35:46 +00:00
|
|
|
DIGEST_TYPE="sha512"
|
2013-04-27 18:07:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
# Do we have local copy
|
|
|
|
if [ -f ${LOCAL_CACHE}/${FILE_NAME} ]; then
|
|
|
|
verify_digest ${LOCAL_CACHE}/${FILE_NAME} $DIGEST_TYPE ${DIGEST} 1
|
|
|
|
if [ $? -eq 0 ]; then
|
2013-04-27 18:07:42 +00:00
|
|
|
cp ${LOCAL_CACHE}/${FILE_NAME} .
|
2014-10-04 20:35:46 +00:00
|
|
|
echo "Using cached ${LOCAL_CACHE}/${FILE_NAME}"
|
|
|
|
return 0
|
2013-04-27 18:07:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
# We we are here either we didnt have a copy or the cached file was invalid
|
|
|
|
wget -q -O ${LOCAL_CACHE}/${FILE_NAME} ${URL}
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "Downloaded ${URL} to ${LOCAL_CACHE}/${FILE_NAME}"
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
verify_digest ${LOCAL_CACHE}/${FILE_NAME} $DIGEST_TYPE ${DIGEST} 1
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "Could not get a verified version of ${FILE_NAME}"
|
|
|
|
return 3
|
|
|
|
fi
|
|
|
|
cp ${LOCAL_CACHE}/${FILE_NAME} .
|
|
|
|
else
|
|
|
|
echo "Cannot download ${URL}!"
|
|
|
|
return 4
|
|
|
|
fi
|
2011-10-31 22:50:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 00:15:03 +00:00
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
# bootstrap, download stage3 and portage snapshot
|
2011-10-28 21:00:47 +00:00
|
|
|
bootstrap() {
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
[ -d ${CHROOT} ] || die "${CHROOT} does not exists"
|
|
|
|
[ -w ${CHROOT} ] || die "${CHROOT} isn't writable"
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
cd ${CHROOT}
|
2014-10-04 20:35:46 +00:00
|
|
|
|
|
|
|
# first install stage 3
|
2013-04-27 18:07:42 +00:00
|
|
|
if [ -d "usr" ] ; then
|
2014-10-08 03:26:49 +00:00
|
|
|
echo "There seems to be already files in ${CHROOT} !"
|
2014-10-04 20:35:46 +00:00
|
|
|
echo "Press <Ctrl+c> to abort, or <Return> to proceed without extracting stage3 ..."
|
2014-10-03 06:59:42 +00:00
|
|
|
read -r REPLY
|
2014-10-04 20:35:46 +00:00
|
|
|
else
|
2014-10-08 03:26:49 +00:00
|
|
|
STAGE_TARBALL=${GENTOO_MIRROR}/releases/${ARCH}/autobuilds/$(curl -s ${LATEST_STAGE_FILE} | grep -v "^#" | head -n 1)
|
2014-10-05 22:13:54 +00:00
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
fetch_file "${STAGE_TARBALL}" "${STAGE_TARBALL}.DIGESTS" || die "Cannot get ${STAGE_TARBALL}"
|
2011-10-28 21:00:47 +00:00
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
echo "Extracting stage3 to ${CHROOT} ..."
|
2014-10-04 20:35:46 +00:00
|
|
|
tar jxpf $(basename ${STAGE_TARBALL}) || die "Extracting stage3 failed"
|
2011-10-28 21:00:47 +00:00
|
|
|
|
2014-10-04 20:35:46 +00:00
|
|
|
rm -f $(basename ${STAGE_TARBALL})
|
|
|
|
fi
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-03 06:59:42 +00:00
|
|
|
# Portage snapshot
|
|
|
|
PORTAGE_SNAPSHOT="${GENTOO_MIRROR}/releases/snapshots/current/portage-latest.tar.bz2"
|
2014-10-08 02:51:39 +00:00
|
|
|
_PORTAGE_MOUNTED=0
|
|
|
|
|
|
|
|
if [ "x${BIND_PORTAGE}" != "x" -a -d ${BIND_PORTAGE} ] ; then
|
2014-10-08 03:26:49 +00:00
|
|
|
mkdir -p ${CHROOT}/usr/portage
|
|
|
|
mount --bind ${BIND_PORTAGE} ${CHROOT}/usr/portage || die "Error mounting ${BIND_PORTAGE}"
|
2014-10-08 02:51:39 +00:00
|
|
|
|
|
|
|
# Remember we mounted portage
|
|
|
|
_PORTAGE_MOUNTED=1
|
2013-04-27 18:07:42 +00:00
|
|
|
else
|
|
|
|
# install latest portage snapshot
|
2014-10-04 20:35:46 +00:00
|
|
|
if [ -d "usr/portage" ] ; then
|
|
|
|
echo "There seems to be already portage files!"
|
|
|
|
echo "Press <Ctrl+c> to abort, or <Return> to proceed without extracting portage ..."
|
|
|
|
read -r REPLY
|
|
|
|
else
|
2013-04-27 18:07:42 +00:00
|
|
|
fetch_file "${PORTAGE_SNAPSHOT}" "${PORTAGE_SNAPSHOT}.md5sum"
|
2014-10-08 03:26:49 +00:00
|
|
|
echo "Extracting latest portage snapshot to ${CHROOT}/usr ..."
|
|
|
|
tar jxf $(basename ${PORTAGE_SNAPSHOT}) -C "${CHROOT}/usr" || die "Extracting portage snapshot failed"
|
2013-04-27 18:07:42 +00:00
|
|
|
rm -f portage-latest.tar.bz2
|
|
|
|
fi
|
|
|
|
fi
|
2014-10-03 06:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
# setup_chroot CHROOT
|
2014-10-03 06:59:42 +00:00
|
|
|
setup_chroot() {
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-05 22:13:54 +00:00
|
|
|
# mount pseudo filesystems
|
2014-10-08 03:26:49 +00:00
|
|
|
mount -t proc none ${CHROOT}/proc || die "Error mounting /proc"
|
|
|
|
mount --rbind /dev ${CHROOT}/dev || die "Error mounting /dev"
|
|
|
|
mount --rbind /sys ${CHROOT}/sys || die "Error mounting /sys"
|
2014-10-05 22:13:54 +00:00
|
|
|
}
|
2013-04-27 18:07:42 +00:00
|
|
|
|
|
|
|
|
2014-10-05 22:13:54 +00:00
|
|
|
# Actually prepare the install script running within chroot
|
|
|
|
# and run it
|
|
|
|
install_gentoo() {
|
2013-04-27 18:07:42 +00:00
|
|
|
|
|
|
|
if [ ${INTERACTIVE} = 1 ]; then
|
2014-10-08 03:26:49 +00:00
|
|
|
echo "Done. Entering chroot environment. All yours..."
|
|
|
|
chroot ${CHROOT} /bin/bash
|
2013-04-27 18:07:42 +00:00
|
|
|
else
|
2014-10-08 03:26:49 +00:00
|
|
|
# resolve.conf
|
|
|
|
echo "Copy resolv.conf from host"
|
|
|
|
cp -L /etc/resolv.conf ${CHROOT}/etc/resolv.conf || die "Can't copy resolv.conf"
|
|
|
|
|
|
|
|
# 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
|
2014-10-05 22:13:54 +00:00
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
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
|
2014-10-05 22:13:54 +00:00
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
# eselect profile
|
|
|
|
cat << EOF >> ${CHROOT}/tmp/install.sh
|
|
|
|
echo "Setting profile to ${ESELECT_PROFILE}"
|
|
|
|
eselect profile set ${ESELECT_PROFILE}
|
|
|
|
EOF
|
2014-10-05 22:13:54 +00:00
|
|
|
|
|
|
|
# Set Timezone
|
|
|
|
|
|
|
|
# Configure locales
|
|
|
|
|
|
|
|
# Compile own kernel later
|
|
|
|
# boot + kernel + lib/modules
|
|
|
|
|
|
|
|
# /etc/fstab
|
2014-10-08 03:26:49 +00:00
|
|
|
chmod 755 ${CHROOT}/tmp/install.sh
|
|
|
|
chroot ${CHROOT} /tmp/install.sh
|
|
|
|
|
2013-04-27 18:07:42 +00:00
|
|
|
echo "Done !"
|
|
|
|
echo "Press <Return> to tear down the chroot environment once you are done."
|
2014-10-03 06:59:42 +00:00
|
|
|
read -r REPLY
|
2014-10-05 22:13:54 +00:00
|
|
|
|
2012-10-30 05:18:15 +00:00
|
|
|
fi
|
2011-11-21 23:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Clean up host
|
|
|
|
cleanup() {
|
2014-10-08 03:26:49 +00:00
|
|
|
umount ${CHROOT}/dev/pts ${CHROOT}/dev ${CHROOT}/sys ${CHROOT}/proc
|
2013-04-27 18:07:42 +00:00
|
|
|
|
2014-10-08 02:51:39 +00:00
|
|
|
if [ ${_PORTAGE_MOUNTED} != 0 ]; then
|
2014-10-08 03:26:49 +00:00
|
|
|
umount ${CHROOT}/usr/portage
|
2014-10-05 22:13:54 +00:00
|
|
|
else
|
2014-10-08 03:26:49 +00:00
|
|
|
rm -rf ${CHROOT}/usr/portage/distfiles/*
|
2013-04-27 18:07:42 +00:00
|
|
|
fi
|
2014-10-05 22:13:54 +00:00
|
|
|
|
|
|
|
# Clean up chroot
|
2014-10-08 03:26:49 +00:00
|
|
|
rm -rf ${CHROOT}/tmp/*
|
|
|
|
rm -rf ${CHROOT}/var/tmp/*
|
2011-11-21 23:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# print usage
|
2011-10-28 21:00:47 +00:00
|
|
|
usage() {
|
|
|
|
cat << EOF
|
|
|
|
Usage: $0 [options]
|
|
|
|
|
2014-10-08 02:51:39 +00:00
|
|
|
This script builds a full Gentoo chroot
|
2011-10-28 21:00:47 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-h Show this message
|
|
|
|
-a arch, either i686 or x86_64, defaults to uname -m
|
2014-10-05 22:13:54 +00:00
|
|
|
-p profile, [ hardened | hardened-no-multilib | default *]
|
2011-10-28 21:00:47 +00:00
|
|
|
-t The timezone to use, default to GMT
|
2014-10-08 03:26:49 +00:00
|
|
|
-r chroot location (default $CHROOT )
|
2012-10-30 05:18:15 +00:00
|
|
|
-c local cache (default $LOCAL_CACHE)
|
2014-10-08 02:51:39 +00:00
|
|
|
-b bind mount portage tree from, instead of downloading portage snapshot
|
2014-10-05 22:13:54 +00:00
|
|
|
-i interactive, enter chroot only, do NOT run install script
|
2014-10-08 02:51:39 +00:00
|
|
|
-m use custom make.conf
|
|
|
|
-d debug (set -x)
|
2011-10-28 21:00:47 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2011-11-21 23:54:45 +00:00
|
|
|
|
2014-10-08 02:51:39 +00:00
|
|
|
DEBUG=0
|
2012-09-05 05:33:15 +00:00
|
|
|
INTERACTIVE=0
|
2014-10-08 03:26:49 +00:00
|
|
|
MAKE_CONF=""
|
2014-10-08 02:51:39 +00:00
|
|
|
BIND_PORTAGE=""
|
|
|
|
while getopts ":a:p:t:r:c:m:b:dhi" OPTIONS; do
|
2013-04-27 18:07:42 +00:00
|
|
|
case $OPTIONS in
|
|
|
|
a ) ARCH=$OPTARG;;
|
|
|
|
p ) PROFILE=$OPTARG;;
|
|
|
|
t ) TIMEZONE=$OPTARG;;
|
2014-10-08 02:51:39 +00:00
|
|
|
d ) DEBUG=1;;
|
|
|
|
b ) BIND_PORTAGE=$OPTARG;;
|
2014-10-08 03:26:49 +00:00
|
|
|
r ) CHROOT=$OPTARG;;
|
2013-04-27 18:07:42 +00:00
|
|
|
c ) LOCAL_CACHE=$OPTARG;;
|
|
|
|
i ) INTERACTIVE=1;;
|
|
|
|
m ) MAKE_CONF=$OPTARG;;
|
|
|
|
? )
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
esac
|
2011-10-28 21:00:47 +00:00
|
|
|
done
|
|
|
|
|
2014-10-05 22:13:54 +00:00
|
|
|
# Do some sanity checks first
|
|
|
|
if [ "$(id -u)" != "0" ]; then
|
|
|
|
die "Sorry, but we need root permissions to create DEVICE nodes etc.!"
|
|
|
|
fi
|
|
|
|
|
2011-10-28 21:00:47 +00:00
|
|
|
ARCH=${ARCH-"$(uname -m)"}
|
2014-10-05 22:13:54 +00:00
|
|
|
PROFILE=${PROFILE="default"}
|
2011-10-28 21:00:47 +00:00
|
|
|
TIMEZONE=${TIMEZONE-"GMT"}
|
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
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
|
|
|
|
|
2014-10-08 02:51:39 +00:00
|
|
|
if [ ${DEBUG} -eq 1 ]; then
|
2013-04-27 18:07:42 +00:00
|
|
|
set -x
|
2012-04-11 17:14:38 +00:00
|
|
|
fi
|
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
bootstrap
|
2011-11-21 23:54:45 +00:00
|
|
|
|
2014-10-05 22:13:54 +00:00
|
|
|
# From here make sure we don't leave stuff around on the host
|
2014-10-08 03:26:49 +00:00
|
|
|
trap "cleanup" INT TERM EXIT
|
|
|
|
|
|
|
|
setup_chroot
|
2011-11-21 23:54:45 +00:00
|
|
|
|
2014-10-08 03:26:49 +00:00
|
|
|
install_gentoo
|