//
you're reading...
OpenVZ

VE Create Script

Script I use to create VE’s, it assumes you have a LVM volume group called vg_vz, it will create logical volumes for VE disks.
It also added NFS option so the VE’s can mount NFS drives, you can disable that if not used.

WARNING: I’ve had issues with the br0 interface taking on the MAC of the last added VE, this causes issues if you delete the VE since its interface will go bye bye. Make sure the MAC on br0 is the same as eth0 (or what interface you are bridging against). Probably can add a line at the end of this script to set the bridge MAC back to eth0, haven’t tested that yet.

#!/bin/bash

# Set to bridged eth interface MAC
ETH_MAC='00:01:02:03:04:05'

TEMPL='centos-6-x86_64'
NET_BASE='10.1.1'
NET_GW='10.1.1.1'
NET_DNS='10.1.1.12'
NET_DOMAIN='domain.local'

if [ "$1" == '-h' ] || [ "$1" == '--help' ]; then echo "usage: ./create.vz.sh (NAME) (ID) (CPU) (MEM)[G|M] (DISK)[G|M]"; exit 0; fi

NAME=$1
if [ -z "$NAME" ]; then echo -n "Name: "; read NAME; fi

CN=$2
if [ -z "$CN" ]; then echo -n "ID (used for both vz and last octet of IP): "; read CN; fi

CPU=$3
if [ -z "$CPU" ]; then echo -n "CPUs: "; read CPU; fi

MEM=$4
if [ -z "$MEM" ]; then echo -n "Mem (add G or M to the end): "; read MEM; fi

DISK="$5"
if [ -z "$DISK" ]; then echo -n "Disk (add G or M to the end): "; read DISK; fi

echo "Name is $NAME"
echo "CN is $CN"
echo "CPU is $CPU"
echo "MEM is $MEM"
echo "DISK is $DISK"
echo -n "Create server? (y/n): "
read YN
if [ "$YN" != 'y' ]; then echo "Exiting..."; exit 1; fi

TEST_LV=`lvdisplay | grep -i name |grep -q $NAME; echo $?`
if [ "$TEST_LV" -eq 0 ]; then
    echo -n "LV $NAME exists, reuse? (y/n): "
    read YN_LV
    if [ "$YN_LV" != 'y' ]; then 
	echo "Exiting..."
	exit 1
    fi
else
    lvcreate -L$DISK -n lv_$NAME vg_vz
    mkfs.ext4 /dev/mapper/vg_vz-lv_$NAME
fi
TEST_LV=`lvdisplay | grep -i name |grep -q $NAME; echo $?`
if [ "$TEST_LV" -ne 0 ]; then echo "LV lv_$NAME not found"; exit 1; fi

if [ ! -d "/vz/servers" ]; then mkdir /vz/servers; fi
if [ ! -d "/vz/servers/$NAME" ]; then mkdir /vz/servers/$NAME; fi

mount /dev/mapper/vg_vz-lv_$NAME /vz/servers/$NAME
TEST_MOUNT=`mount |grep -q $NAME; echo $?`
if [ "$TEST_MOUNT" -ne 0 ]; then echo "Server dir not mounted"; exit 1; fi

TEST_FSTAB=`cat /etc/fstab |grep -q $NAME; echo $?`
if [ "$TEST_FSTAB" -ne 0 ]; then
    echo "Adding fstab entry"
    echo "/dev/mapper/vg_vz-lv_$NAME 	/vz/servers/$NAME	ext4	defaults	0 0" >> /etc/fstab
fi

vzctl create $CN --ostemplate $TEMPL --hostname $NAME --name $NAME --root /vz/servers/$NAME/root --private /vz/servers/$NAME/private
vzctl set $CN --ipdel all --save
vzctl set $CN --netif_add eth0 --save
vzctl set $CN --cpus $CPU --save
vzctl set $CN --ram $MEM --save
vzctl set $CN --diskspace $DISK --save
vzctl set $CN --features "nfs:on" --save

cat >/etc/sysconfig/network-scripts/ifcfg-veth${CN}.0<<EOF
DEVICE=veth${CN}.0
BRDIGE="br0"
ONBOOT=yes
EOF

cat >/vz/servers/${NAME}/private/etc/sysconfig/network-scripts/ifcfg-eth0<<EOF
DEVICE=eth0
BOOTPROTO=static
IPADDR=$NET_BASE.$CN
NETMASK=255.255.255.0
GATEWAY=$NET_GW
DNS1=$NET_DNS

DOMAIN=$NET_DOMAIN
ONBOOT=yes
EOF

cat >>/vz/servers/${NAME}/private/etc/rc.local<<EOF
/sbin/ifconfig eth0 0
/sbin/ifup eth0
/bin/ping -c1 $NET_GW
EOF

vzctl start $CN

echo 1 > /proc/sys/net/ipv4/conf/veth${CN}.0/forwarding
echo 1 > /proc/sys/net/ipv4/conf/veth${CN}.0/proxy_arp

brctl addif br0 veth${CN}.0

ip link set br0 address $ETH_MAC

echo "NOTE: To remove this server, after running 'vzctl destroy $NAME' you will need to remove the entry in /etc/fstab and unmount and remove the logical volume"
Advertisements

Discussion

One thought on “VE Create Script

  1. Re: WARNING – I added the ip link line to help with the creation of a VE changing the MAC on the bridge, this helps but there’s still issues when rebooting or shutting down the VE’s.

    I believe specifying the MAC in the ifcfg-br0 helps, but need to test some more to see why it’s failing.

    On reboots, or shutdowns, the veth will not be on the bridge anymore, this is easy to fix by doing:

    brctl addif br0 vethX.X

    Can be checked with:

    brctl show

    Posted by koaps | June 12, 2013, 7:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s