Various build.sh fixes
This commit is contained in:
parent
5eb28bfcaa
commit
19078eaede
285
scripts/build.sh
285
scripts/build.sh
@ -15,157 +15,172 @@ IMAGE_ROOT=/mnt/gentoo
|
|||||||
set -o nounset
|
set -o nounset
|
||||||
|
|
||||||
die() {
|
die() {
|
||||||
echo $@
|
echo $@
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fetch_file() {
|
fetch_file() {
|
||||||
local URL=$1
|
local URL=$1
|
||||||
local DIGEST=$2
|
local DIGEST=$2
|
||||||
|
|
||||||
FILE_NAME=$(basename ${URL})
|
FILE_NAME=$(basename ${URL})
|
||||||
SHA512=$(curl -s -S ${DIGEST} | grep -A1 -e "^# SHA512 HASH" | grep -o -E -e "^[0-9a-f]{128} *${FILE_NAME}$" | awk '{print $1}')
|
SHA512=$(curl -s -S ${DIGEST} | grep -A1 -e "^# SHA512 HASH" | grep -o -E -e "^[0-9a-f]{128} *${FILE_NAME}$" | awk '{print $1}')
|
||||||
|
|
||||||
if [ -z ${SHA512} ]; then
|
# Check if local cache is usable
|
||||||
die "Unable to get checksum for ${FILE_NAME}, abort"
|
if [ ! -d ${LOCAL_CACHE} ] || [ ! -w ${LOCAL_CACHE} ]; then
|
||||||
fi
|
die "Local cache ${LOCAL_CACHE} unusable!"
|
||||||
|
fi
|
||||||
|
|
||||||
# Do we have local copy
|
if [ -z ${SHA512} ]; then
|
||||||
if [ -f ${LOCAL_CACHE}/${FILE_NAME} ]; then
|
# Let's try md5sum before giving up
|
||||||
# if we have local, correct copy use it
|
MD5=$(curl -s -S ${DIGEST} | grep -o -E -e "^[0-9a-f]{32} *${FILE_NAME}$" | awk '{print $1}')
|
||||||
if [ $(sha512sum ${LOCAL_CACHE}/${FILE_NAME} | awk '{print $1}') = ${SHA512} ]; then
|
if [ -z ${MD5} ]; then
|
||||||
cp ${LOCAL_CACHE}/${FILE_NAME} .
|
die "Unable to get checksum for ${FILE_NAME}, abort"
|
||||||
return
|
fi
|
||||||
else
|
fi
|
||||||
echo "Invalid checksum for ${LOCAL_CACHE}/${FILE_NAME}, downloading new copy..."
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
wget -nc ${URL} || die "Cannot download ${URL}!"
|
# Do we have local copy, if not download
|
||||||
if [ $(sha512sum ${FILE_NAME} | awk '{print $1}') != ${SHA512} ]; then
|
if [ ! -f ${LOCAL_CACHE}/${FILE_NAME} ]; then
|
||||||
die "Invalid checksum for ${FILE_NAME}!"
|
wget -O ${LOCAL_CACHE}/${FILE_NAME} ${URL} || die "Cannot download ${URL}!"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ ! -z ${SHA512} ]; then
|
||||||
|
if [ $(sha512sum ${LOCAL_CACHE}/${FILE_NAME} | awk '{print $1}') = ${SHA512} ]; then
|
||||||
|
cp ${LOCAL_CACHE}/${FILE_NAME} .
|
||||||
|
return
|
||||||
|
else
|
||||||
|
rm -f ${LOCAL_CACHE}/${FILE_NAME}
|
||||||
|
die "Invalid checksum for ${LOCAL_CACHE}/${FILE_NAME}, removing cached copy."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -z ${MD5} ]; then
|
||||||
|
if [ $(md5sum ${LOCAL_CACHE}/${FILE_NAME} | awk '{print $1}') = ${MD5} ]; then
|
||||||
|
cp ${LOCAL_CACHE}/${FILE_NAME} .
|
||||||
|
return
|
||||||
|
else
|
||||||
|
rm -f ${LOCAL_CACHE}/${FILE_NAME}
|
||||||
|
die "Invalid checksum for ${LOCAL_CACHE}/${FILE_NAME}, removing cached copy."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
die "No checksum available for ${FILE_NAME}!"
|
||||||
|
|
||||||
# Check if local cache is usable
|
|
||||||
if [ -d ${LOCAL_CACHE} ] && [ -w ${LOCAL_CACHE} ]; then
|
|
||||||
cp ${FILE_NAME} ${LOCAL_CACHE}
|
|
||||||
echo "Stored ${FILE_NAME} in local cache."
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# bootstrap ROOT_FS PROFILE ARCH
|
# bootstrap ROOT_FS PROFILE ARCH
|
||||||
bootstrap() {
|
bootstrap() {
|
||||||
local ROOT_FS=$1
|
local ROOT_FS=$1
|
||||||
local PROFILE=$2
|
local PROFILE=$2
|
||||||
local ARCH=$3
|
local ARCH=$3
|
||||||
|
|
||||||
local STAGE_PATH
|
local STAGE_PATH
|
||||||
local STAGE_ARCH
|
local STAGE_ARCH
|
||||||
local LATEST_STAGE_FILE
|
local LATEST_STAGE_FILE
|
||||||
local ESELECT_PROFILE
|
local ESELECT_PROFILE
|
||||||
|
|
||||||
if [ "${ARCH}" = "i686" ] ; then
|
if [ "${ARCH}" = "i686" ] ; then
|
||||||
STAGE_ARCH=${ARCH}
|
STAGE_ARCH=${ARCH}
|
||||||
# Why do they use x86 here ? :(
|
# Why do they use x86 here ? :(
|
||||||
STAGE_PATH="${GENTOO_MIRROR}/releases/x86/autobuilds"
|
STAGE_PATH="${GENTOO_MIRROR}/releases/x86/autobuilds"
|
||||||
elif [ "${ARCH}" = "x86_64" ] ; then
|
elif [ "${ARCH}" = "x86_64" ] ; then
|
||||||
STAGE_ARCH="amd64"
|
STAGE_ARCH="amd64"
|
||||||
STAGE_PATH="${GENTOO_MIRROR}/releases/${STAGE_ARCH}/autobuilds"
|
STAGE_PATH="${GENTOO_MIRROR}/releases/${STAGE_ARCH}/autobuilds"
|
||||||
else
|
else
|
||||||
die "Unknown architecture!"
|
die "Unknown architecture!"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${PROFILE}" = "hardened" ] ; then
|
if [ "${PROFILE}" = "hardened" ] ; then
|
||||||
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}-hardened.txt"
|
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}-hardened.txt"
|
||||||
ESELECT_PROFILE="hardened/linux/${ARCH}"
|
ESELECT_PROFILE="hardened/linux/${ARCH}"
|
||||||
elif [ "${PROFILE}" = "hardened-no-multilib" ] ; then
|
elif [ "${PROFILE}" = "hardened-no-multilib" ] ; then
|
||||||
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}-hardened+nomultilib.txt"
|
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}-hardened+nomultilib.txt"
|
||||||
ESELECT_PROFILE="hardened/linux/${ARCH}/no-multilib"
|
ESELECT_PROFILE="hardened/linux/${ARCH}/no-multilib"
|
||||||
elif [ "${PROFILE}" = "server" ] ; then
|
elif [ "${PROFILE}" = "server" ] ; then
|
||||||
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}.txt"
|
LATEST_STAGE_FILE="${STAGE_PATH}/latest-stage3-${STAGE_ARCH}.txt"
|
||||||
ESELECT_PROFILE="default/linux/${ARCH}/10.0/no-multilib"
|
ESELECT_PROFILE="default/linux/${ARCH}/10.0/no-multilib"
|
||||||
else
|
else
|
||||||
die "Unknown profile!"
|
die "Unknown profile!"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
STAGE_TARBALL=${GENTOO_MIRROR}/releases/${STAGE_ARCH}/autobuilds/$(curl -s ${LATEST_STAGE_FILE} | grep -v "^#" | head -n 1)
|
STAGE_TARBALL=${GENTOO_MIRROR}/releases/${STAGE_ARCH}/autobuilds/$(curl -s ${LATEST_STAGE_FILE} | grep -v "^#" | head -n 1)
|
||||||
|
|
||||||
[ -d ${ROOT_FS} ] || die "${ROOT_FS} does not exists"
|
[ -d ${ROOT_FS} ] || die "${ROOT_FS} does not exists"
|
||||||
[ -w ${ROOT_FS} ] || die "${ROOT_FS} isn't writable"
|
[ -w ${ROOT_FS} ] || die "${ROOT_FS} isn't writable"
|
||||||
|
|
||||||
# install stage 3
|
# install stage 3
|
||||||
cd ${ROOT_FS}
|
cd ${ROOT_FS}
|
||||||
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 ${ROOT_FS} !"
|
||||||
echo "Press <Ctrl+c> to abort, or <Return> to proceed with extracting stage3 ..."
|
echo "Press <Ctrl+c> to abort, or <Return> to proceed with extracting stage3 ..."
|
||||||
read
|
read
|
||||||
fi
|
fi
|
||||||
fetch_file "${STAGE_TARBALL}" "${STAGE_TARBALL}.DIGESTS"
|
fetch_file "${STAGE_TARBALL}" "${STAGE_TARBALL}.DIGESTS"
|
||||||
tar jxpf $(basename ${STAGE_TARBALL}) || die "Extracting stage file failed"
|
tar jxpf $(basename ${STAGE_TARBALL}) || die "Extracting stage file failed"
|
||||||
rm -f $(basename ${STAGE_TARBALL})
|
rm -f $(basename ${STAGE_TARBALL})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# setup_chroot ROOT_FS
|
# setup_chroot ROOT_FS
|
||||||
setup_chroot() {
|
setup_chroot() {
|
||||||
local ROOT_FS=$1
|
local ROOT_FS=$1
|
||||||
|
|
||||||
# Import certain values from host config
|
# Import certain values from host config
|
||||||
_PORTDIR=$(. /etc/portage/make.conf && echo $PORTDIR)
|
_PORTDIR=$(. ${MAKE_CONF} && echo $PORTDIR)
|
||||||
_DISTDIR=$(. /etc/portage/make.conf && echo $DISTDIR)
|
_DISTDIR=$(. ${MAKE_CONF} && echo $DISTDIR)
|
||||||
_PKGDIR=$(. /etc/portage/make.conf && echo $PKGDIR)
|
_PKGDIR=$(. ${MAKE_CONF} && echo $PKGDIR)
|
||||||
HOST_PORTDIR=${_PORTDIR:-/usr/portage}
|
HOST_PORTDIR=${_PORTDIR:-/usr/portage}
|
||||||
HOST_DISTDIR=${_DISTDIR:-/usr/portage/distfiles}
|
HOST_DISTDIR=${_DISTDIR:-/usr/portage/distfiles}
|
||||||
HOST_PKGDIR=${_PKGDIR:-/usr/portage/packages}
|
HOST_PKGDIR=${_PKGDIR:-/usr/portage/packages}
|
||||||
|
|
||||||
PORTAGE_SNAPSHOT="${GENTOO_MIRROR}/snapshots/portage-latest.tar.bz2"
|
PORTAGE_SNAPSHOT="${GENTOO_MIRROR}/snapshots/portage-latest.tar.bz2"
|
||||||
if [ ${BIND_PORTAGE} = 1 ] ; then
|
if [ ${BIND_PORTAGE} = 1 ] ; then
|
||||||
if [ -d ${ROOT_FS}/${PORTDIR} ] ; then
|
if [ -d ${ROOT_FS}/${HOST_PORTDIR} ] ; then
|
||||||
mount --bind ${HOST_PORTDIR} ${ROOT_FS}/${PORTDIR} || die "Error mounting ${PORTDIR}"
|
mount --bind ${HOST_PORTDIR} ${ROOT_FS}/${HOST_PORTDIR} || die "Error mounting ${HOST_PORTDIR}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# install latest portage snapshot
|
||||||
|
if [ ! -d "usr/portage" ] ; then
|
||||||
|
fetch_file "${PORTAGE_SNAPSHOT}" "${PORTAGE_SNAPSHOT}.md5sum"
|
||||||
|
tar jxf $(basename ${PORTAGE_SNAPSHOT}) -C "${ROOT_FS}/usr" || die "Extracting portage snapshot failed"
|
||||||
|
rm -f portage-latest.tar.bz2
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
else
|
|
||||||
# install latest portage snapshot
|
|
||||||
if [ ! -d "usr/portage" ] ; then
|
|
||||||
fetch_file "${PORTAGE_SNAPSHOT}" "${PORTAGE_SNAPSHOT}.md5sum"
|
|
||||||
tar jxf $(basename ${PORTAGE_SNAPSHOT}) -C "${ROOT_FS}/usr" || die "Extracting portage snapshot failed"
|
|
||||||
rm -f portage-latest.tar.bz2
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# resolve.conf
|
# resolve.conf
|
||||||
cp -L /etc/resolv.conf ${ROOT_FS}/etc/resolv.conf || die "Can't copy resolv.conf"
|
cp -L /etc/resolv.conf ${ROOT_FS}/etc/resolv.conf || die "Can't copy resolv.conf"
|
||||||
|
|
||||||
# Remount pseudo filesystems
|
# Remount pseudo filesystems
|
||||||
mount --bind /dev ${ROOT_FS}/dev || die "Error mounting /dev"
|
mount --bind /dev ${ROOT_FS}/dev || die "Error mounting /dev"
|
||||||
mount --bind /sys ${ROOT_FS}/sys || die "Error mounting /sys"
|
mount --bind /sys ${ROOT_FS}/sys || die "Error mounting /sys"
|
||||||
mount --bind /proc ${ROOT_FS}/proc || die "Error mounting /proc"
|
mount --bind /proc ${ROOT_FS}/proc || die "Error mounting /proc"
|
||||||
|
|
||||||
# Compile own kernel later
|
# Compile own kernel later
|
||||||
# boot + kernel + lib/modules
|
# boot + kernel + lib/modules
|
||||||
|
|
||||||
# etc/portage/*
|
# etc/portage/*
|
||||||
|
|
||||||
if [ ${INTERACTIVE} = 1 ]; then
|
if [ ${INTERACTIVE} = 1 ]; then
|
||||||
echo "Done. Entering chroot environment..."
|
echo "Done. Entering chroot environment..."
|
||||||
chroot ${ROOT_FS} /bin/bash
|
chroot ${ROOT_FS} /bin/bash
|
||||||
else
|
else
|
||||||
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
|
read
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Clean up host
|
# Clean up host
|
||||||
cleanup() {
|
cleanup() {
|
||||||
local ROOT_FS=$1
|
local ROOT_FS=$1
|
||||||
umount ${ROOT_FS}/dev ${ROOT_FS}/sys ${ROOT_FS}/proc
|
umount ${ROOT_FS}/dev ${ROOT_FS}/sys ${ROOT_FS}/proc
|
||||||
|
|
||||||
if [ ${BIND_PORTAGE} != 0 ]; then
|
if [ ${BIND_PORTAGE} != 0 ]; then
|
||||||
umount ${ROOT_FS}/${HOST_PORTAGE}
|
umount ${ROOT_FS}/${HOST_PORTDIR}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -185,6 +200,7 @@ OPTIONS:
|
|||||||
-c local cache (default $LOCAL_CACHE)
|
-c local cache (default $LOCAL_CACHE)
|
||||||
-b bind mount host portage tree
|
-b bind mount host portage tree
|
||||||
-i interactive, setting up chroot and enter it, skip extracting stage3, portage, etc.
|
-i interactive, setting up chroot and enter it, skip extracting stage3, portage, etc.
|
||||||
|
-m make.conf to source portage location,etc. defaults to /etc/portage/make.conf
|
||||||
-v Verbose
|
-v Verbose
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
@ -192,28 +208,29 @@ EOF
|
|||||||
|
|
||||||
# Do some sanity checks first
|
# Do some sanity checks first
|
||||||
if [ "$(id -u)" != "0" ]; then
|
if [ "$(id -u)" != "0" ]; then
|
||||||
die "Sorry, but we need root permissions to create DEVICE nodes etc.!"
|
die "Sorry, but we need root permissions to create DEVICE nodes etc.!"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
VERBOSE=0
|
VERBOSE=0
|
||||||
BIND_PORTAGE=0
|
BIND_PORTAGE=0
|
||||||
INTERACTIVE=0
|
INTERACTIVE=0
|
||||||
|
MAKE_CONF="/etc/portage/make.conf"
|
||||||
while getopts ":a:p:t:r:c:m:bvhi" OPTIONS; do
|
while getopts ":a:p:t:r:c:m:bvhi" OPTIONS; do
|
||||||
case $OPTIONS in
|
case $OPTIONS in
|
||||||
a ) ARCH=$OPTARG;;
|
a ) ARCH=$OPTARG;;
|
||||||
p ) PROFILE=$OPTARG;;
|
p ) PROFILE=$OPTARG;;
|
||||||
t ) TIMEZONE=$OPTARG;;
|
t ) TIMEZONE=$OPTARG;;
|
||||||
v ) VERBOSE=1;;
|
v ) VERBOSE=1;;
|
||||||
b ) BIND_PORTAGE=1;;
|
b ) BIND_PORTAGE=1;;
|
||||||
r ) IMAGE_ROOT=$OPTARG;;
|
r ) IMAGE_ROOT=$OPTARG;;
|
||||||
c ) LOCAL_CACHE=$OPTARG;;
|
c ) LOCAL_CACHE=$OPTARG;;
|
||||||
i ) INTERACTIVE=1;;
|
i ) INTERACTIVE=1;;
|
||||||
m ) MAKE_CONF=$OPTARG;;
|
m ) MAKE_CONF=$OPTARG;;
|
||||||
? )
|
? )
|
||||||
usage
|
usage
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
ARCH=${ARCH-"$(uname -m)"}
|
ARCH=${ARCH-"$(uname -m)"}
|
||||||
@ -221,16 +238,16 @@ PROFILE=${PROFILE="server"}
|
|||||||
TIMEZONE=${TIMEZONE-"GMT"}
|
TIMEZONE=${TIMEZONE-"GMT"}
|
||||||
|
|
||||||
if [ ${VERBOSE} = 1 ]; then
|
if [ ${VERBOSE} = 1 ]; then
|
||||||
set -x
|
set -x
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ${INTERACTIVE} = 0 ]; then
|
if [ ${INTERACTIVE} = 0 ]; then
|
||||||
bootstrap ${IMAGE_ROOT} ${PROFILE} ${ARCH}
|
bootstrap ${IMAGE_ROOT} ${PROFILE} ${ARCH}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -r $MAKE_CONF ]; then
|
if [ ! -r $MAKE_CONF ]; then
|
||||||
echo "Cannot find requested make.conf: $MAKE_CONF"
|
echo "Cannot find requested make.conf: $MAKE_CONF"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# From here make sure we don't leave stuff around
|
# From here make sure we don't leave stuff around
|
||||||
|
Loading…
Reference in New Issue
Block a user