OpenVZ cache update script

Script to update OpenVZ vz cache for any template.

Useful to upgrade to newest security releases.

#!/bin/bash
#
# updates cache of VZ
#
#

#
# Set These (or pass from command line as below)
#
#TEMPLATE=centos-5-i386-afull
VZID=5999
#
# use this from command line:
#
# ./updatevzcache centos-5-i386-afull
#
TEMPLATE=$1
# VZID=$2

#
# change these if needed
#
HOSTNAME=testvps.zoidial.com
IP=10.10.10.100
NAMESERVER=10.10.10.1

#
# create vps, add IP address, set hostname, set nameserver, start vps
#

vzctl create $VZID –ostemplate $TEMPLATE –ipadd $IP –hostname $HOSTNAME
vzctl set $VZID –nameserver $NAMESERVER –save
vzctl start $VZID
#vzctl enter $VZID

#
# run some update commands on the VPS
#

vzctl exec $VZID “yum clean all; yum upgrade -y; yum clean all; > /etc/resolv.conf”

#
# stop vps, delete ip addresses
#

vzctl stop $VZID
vzctl set $VZID –ipdel all –save

#
# zero out all logs in the vps (we want it to look fresh!)
#

for i in `ls -1 /vz/private/$VZID/var/log`; do echo “” > /vz/private/$VZID/var/log/$i; done
for i in `ls -1 /vz/private/$VZID/var/log/*/*`; do echo “” > $i; done

#
# move old vps template to a backup
#

mv /vz/template/cache/$TEMPLATE.tar.gz{,-old}

#
# Enter vps private directory, make a template from this VPS (nice 19 and ionice to limit impact of this tar)
#

cd /vz/private/$VZID
nice -19 ionice -c 2 -n 7 tar czf /vz/template/cache/$TEMPLATE.tar.gz .
chmod +rx /vz/template/cache/$TEMPLATE.tar.gz

#
# back to /root dir
#

cd /root/

#
# destroy this temporary vps and config
#

vzctl destroy $VZID
rm -f /etc/vz/conf/$VZID.conf.destroyed

#
# double check that the template looks like it’s been updated
#

ls -lh /vz/template/cache/$TEMPLATE*

Leave a Reply

Your email address will not be published. Required fields are marked *

*