egscripts/egopenvz/eg-make-openvz-template

146 lines
3.5 KiB
Bash
Executable file

#!/bin/bash
# Script to create an OpenVZ template for a specific Debian / Ubuntu
# version
DEFAULT_CONFFILE=/etc/egscripts/make-openvz-template.conf
DEFAULT_CONFIGBASE=/etc/egscripts/openvz
DEFAULT_CONFIGTEMPLATE=vps.basic
DEFAULT_VZCONFDIR=/etc/vz
DEFAULT_MIRROR=http://aptcache.egressive.com:3142/ubuntu
DEFAULT_NAMESERVER=10.128.2.11
DEFAULT_VEID=666
DEFAULT_IP=10.128.2.99
#
# Any of the variables below can be overriden either as environment variables
# or in the configuration file. Either specified on the command line or from
# the default above
#
CONFFILE=${CONFFILE-$DEFAULT_CONFFILE}
MIRROR=${MIRROR-$DEFAULT_MIRROR}
NAMESERVER=${NAMESERVER-$DEFAULT_NAMESERVER}
VEID=${VEID-$DEFAULT_VEID}
IP=${IP-$DEFAULT_IP}
CONFIGBASE=${CONFIGBASE-$DEFAULT_CONFIGBASE}
CONFIGTEMPLATE=${CONFIGTEMPLATE-$DEFAULT_CONFIGTEMPLATE}
VZCONFDIR=${VZCONFDIR-$DEFAULT_VZCONFDIR}
usage() {
cat <<EOT
Usage: `basename $0` [ -c configfile ] dist
EOT
}
while getopts "c:h" OPT ; do
case $OPT in
c) $CONFFILE=$OPTARG ;;
h) usage ; exit 0;;
*) usage ; exit 1;;
esac
done
shift $(($OPTIND - 1))
if [ $# -ne 1 ] ; then
usage
exit 1
fi
SUITE=$1
# Exit on any error
set -e
if [ -f $CONFFILE ] ; then
. $CONFFILE
fi
. $VZCONFDIR/vz.conf
if [ ! -r $CONFIGBASE/${SUITE}.config ] ; then
echo "Required config file $CONFIGBASE/${SUITE}.config not found"
exit 1
fi
. $CONFIGBASE/${SUITE}.config
# Make sure a tarball doesn't already exist
if [ -f $TEMPLATE/cache/$OSTEMPLATE.tar.gz ] ; then
echo "$TEMPLATE/cache/$OSTEMPLATE.tar.gz already exists"
exit 1
fi
# Make temporary directory for deboostrap
TARGET="$VE_PRIVATE"
if [ -d $TARGET ] ; then
echo "$TARGET already exists"
exit 1
fi
# Build the initial debootstrap
debootstrap $SUITE $TARGET $MIRROR
# Configure the container configuration for this dist
vzctl set $VEID --applyconfig ${CONFIGTEMPLATE} --save
echo "OSTEMPLATE=$OSTEMPLATE" >> $VZCONFDIR/conf/$VEID.conf
vzctl set $VEID --nameserver $NAMESERVER --save
vzctl set $VEID --ipadd $IP --save
# You will probably want to create a SUITE.sources.list file in th
# config directory, otherwise the only apt source will be the $MIRROR
if [ -f $CONFIGBASE/$SUITE.sources.list ] ; then
cp $CONFIGBASE/$SUITE.sources.list $TARGET/etc/apt/sources.list
fi
vzctl start $VEID
sleep 10
vzctl exec $VEID "rm /etc/event.d/tty*"
vzctl exec $VEID "chmod 700 /root"
vzctl exec $VEID "echo $SUITE-vztemplate > /etc/hostname"
vzctl exec $VEID "aptitude update"
vzctl exec $VEID "aptitude purge -y ${PURGE_PACKAGES}"
vzctl exec $VEID "aptitude install -y ${INSTALL_PACKAGES}"
vzctl exec $VEID "ln -s /bin/true /sbin/modprobe"
vzctl exec $VEID "aptitude -y safe-upgrade"
# Init will hang on klogd if not disabled
vzctl exec $VEID "update-rc.d -f klogd remove"
# /etc/mtab must be a link to /proc/mounts
vzctl exec $VEID "rm -f /etc/mtab"
vzctl exec $VEID "ln -s /proc/mounts /etc/mtab"
vzctl exec $VEID "update-rc.d -f mtab.sh remove"
# Puppet is always required
vzctl exec $VEID "aptitude install -y puppet"
# Clean apt cache
vzctl exec $VEID "aptitude -y clean"
if [ -f $CONFIGBASE/$SUITE.puppet.conf ] ; then
cp $CONFIGBASE/$SUITE.puppet.conf $TARGET/etc/puppet/puppet.conf
fi
vzctl stop $VEID
vzctl set $VEID --ipdel all --save
if [ -f $TARGET/etc/lsb-release ] ; then
. $TARGET/etc/lsb-release
TARNAME=${DISTRIB_ID}-${DISTRIB_CODENAME}-${DISTRIB_RELEASE}
else
TARNAME=${SUITE}
fi
pushd $VE_PRIVATE
echo -n "Creating $TEMPLATE/cache/$OSTEMPLATE.tar.gz ... "
tar -zcf $TEMPLATE/cache/$OSTEMPLATE.tar.gz .
echo "done"
popd
vzctl destroy $VEID