egscripts/egopenvz/eg-make-openvz-ve

81 lines
1.8 KiB
Text
Raw Permalink Normal View History

#!/bin/bash
# Script to create an OpenVZ VE for a specific hostname
# The hostname must be present in $CONFIGBASE/host.map
DEFAULT_CONFFILE=/etc/egscripts/make-openvz-ve.conf
DEFAULT_CONFIGBASE=/etc/egscripts/openvz
DEFAULT_CONFIGTEMPLATE=vps.basic
DEFAULT_VZCONFDIR=/etc/vz
DEFAULT_DOMAIN=kc
DEFAULT_NAMESERVER=10.128.2.11
#
# 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}
CONFIGBASE=${CONFIGBASE-$DEFAULT_CONFIGBASE}
CONFIGTEMPLATE=${CONFIGTEMPLATE-$DEFAULT_CONFIGTEMPLATE}
VZCONFDIR=${VZCONFDIR-$DEFAULT_VZCONFDIR}
DOMAIN=${DOMAIN-$DEFAULT_DOMAIN}
NAMESERVER=${NAMESERVER-$DEFAULT_NAMESERVER}
START=1
usage() {
cat <<EOT
Usage: `basename $0` [ -h ] [ -N ] [ -c configfile ] hostname
EOT
}
while getopts "c:hN" OPT ; do
case $OPT in
c) $CONFFILE=$OPTARG ;;
h) usage ; exit 0;;
N) START=0 ;;
*) usage ; exit 1;;
esac
done
shift $(($OPTIND - 1))
if [ $# -ne 1 ] ; then
usage
exit 1
fi
TARGETHOST=$1
# Exit on any error
set -e
if [ -f $CONFFILE ] ; then
. $CONFFILE
fi
while read HOST OSTEMPLATE VEID IP ; do
if [ "$HOST" == "$TARGETHOST" ] ; then
echo "Creating host $HOST with VEID $VEID and IP $IP"
. $VZCONFDIR/vz.conf
vzctl create $VEID --config $CONFIGTEMPLATE --ostemplate $OSTEMPLATE --hostname $HOST --ipadd $IP
vzctl set $VEID --name $HOST --save
vzctl set $VEID --features "nfs:on" --save
vzctl set $VEID --nameserver "$NAMESERVER" --save
# The apt cache has most likely been updated since the template was
# created, so do a one off update
chroot $VE_PRIVATE /usr/bin/aptitude update
if [ $START -eq 1 ] ; then
vzctl start $VEID
fi
exit 0
fi
done < $CONFIGBASE/host.map
echo "Host $TARGETHOST not found in $CONFIGBASE/host.map"