====== Create you own OpenVZ Template ====== OK. I need to be able to quickly setup extra nodes in my clusters so basically I copy a production container that is setup how I need for the cluster in question and then use the script below to create a template that I can use to add new cluster nodes on the fly. Be advised that this is a work in progress and the Debian portion is the only part that works at the moment. Hope to have CentOS/RHEL finished soon. I finally got a few minutes free to do up the basic CentOS support. Don't forget the warning below though. **WARNING!!!!! DO NOT USE THIS SCRIPT ON A PRODUCTION CONTAINER!!!!!!!!! IT WILL CAUSE DAMAGE!!!!!** #!/bin/bash #set -x VERSION='0.2b' usage() { cat << EOF Usage: $0 options This script creates an OpenVZ Container Template from an existing container. Options: -h Show this message -t container Type can be debian or centos (default: debian) -i container ID -n Name of new template -v Verbose -V Show version EOF } CTTYPE= CTID= TEMPLATE= VERBOSE= VZCTL=`which vzctl` VZLIST=`which vzlist` while getopts "ht:i:n:vV" OPTION do case $OPTION in h) usage exit 0 ;; t) CTTYPE=$OPTARG ;; i) CTID=$OPTARG ;; n) TEMPLATE=$OPTARG ;; v) VERBOSE=1 ;; V) echo "$0 Version: $VERSION" exit 0 ;; esac done if [[ -z $CTTYPE ]] then CTTYPE="debian" fi if [[ -z $CTID ]] || [[ -z $TEMPLATE ]] then usage exit 1 fi CTSTATUS=`$VZLIST $CTID -o status -H` CTROOT=`$VZLIST $CTID -o private -H` CTIP=`$VZLIST $CTID -o ip -H` if [[ $CTIP = "-" ]] then echo "Be sure the container has venet network access!!" echo "Exiting" exit 1 fi if [[ $CTSTATUS != "running" ]] then echo "Be sure container is running!!" echo "Exiting!" exit 1 fi if [ $CTTYPE = "debian" ]; then echo "Updating container $CTID" $VZCTL exec $CTID 'apt-get update' echo "Installing SSH server" $VZCTL exec $CTID 'apt-get install openssh-server' echo "Cleaning container" $VZCTL exec $CTID 'apt-get --purge clean' $VZCTL exec $CTID 'rm -f /etc/hostname' $VZCTL exec $CTID '> /etc/resolve.conf' $VZCTL exec $CTID 'rm -f /etc/ssh/ssh_host_*' echo "Setting up SSH Keygen startup script" cat > /tmp/ssh_gen_host_keys <<"endscript" #!/bin/sh ### BEGIN INIT INFO # Provides: Generates new ssh host keys on first boot # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: Generates new ssh host keys on first boot # Description: Generates new ssh host keys on first boot ### END INIT INFO ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N "" ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N "" /etc/init.d/ssh restart insserv -r /etc/init.d/ssh_gen_host_keys rm -f \$0 endscript cp -f /tmp/ssh_gen_host_keys $CTROOT/etc/init.d rm -f /tmp/ssh_gen_host_keys $VZCTL exec $CTID 'chmod a+x /etc/init.d/ssh_gen_host_keys' $VZCTL exec $CTID 'insserv /etc/init.d/ssh_gen_host_keys' echo "Deleting IP settings" $VZCTL set $CTID --ipdel all --save cat > /tmp/excludes <<'endexcludes' .bash_history lost+found /dev/* /mnt/* /tmp/* /proc/* /sys/* /usr/src/* /etc/ssh/ssh_host* endexcludes echo "Stopping container" $VZCTL stop $CTID echo "Creating template" cd $CTROOT tar --numeric-owner -czvf /var/lib/vz/template/cache/$TEMPLATE.tar.gz -X /tmp/excludes . elif [ $CTTYPE = "centos" ]; then echo "Updating container $CTID" $VZCTL exec $CTID 'yum check-update' echo "Installing SSH server" $VZCTL exec $CTID 'yum install openssh-server' echo "Cleaning container" $VZCTL exec $CTID 'yum clean' $VZCTL exec $CTID 'mkdir /var/lock/rpm' $VZCTL exec $CTID 'sed -i "s/root:\(.*\)\(:.*:.*:.*:.*:.*:.*:\)$/root:!!\2/" /etc/shadow' $VZCTL exec $CTID 'sed -i "/none\t\/dev\/shm\ttmpfs\tdefaults\t\t0\t0/d" /etc/fstab' $VZCTL exec $CTID '> /etc/resolve.conf' $VZCTL exec $CTID '> /etc/sysconfig/network' $VZCTL exec $CTID 'rm -f /etc/ssh/ssh_host_*' echo "Setting up SSH Keygen startup script" cat > /tmp/ssh_gen_host_keys <<'endscript' #!/bin/sh ### BEGIN INIT INFO # Provides: Generates new ssh host keys on first boot # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: Generates new ssh host keys on first boot # Description: Generates new ssh host keys on first boot ### END INIT INFO ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N "" ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N "" /etc/init.d/sshd restart insserv -r /etc/init.d/ssh_gen_host_keys rm -f \$0 endscript cp -f /tmp/ssh_gen_host_keys $CTROOT/etc/init.d rm -f /tmp/ssh_gen_host_keys $VZCTL exec $CTID 'chmod a+x /etc/init.d/ssh_gen_host_keys' $VZCTL exec $CTID 'chkconfig --add ssh_gen_host_keys' echo "Deleting IP settings" $VZCTL set $CTID --ipdel all --save cat > /tmp/excludes <<'endexcludes' .bash_history lost+found /dev/* /mnt/* /tmp/* /proc/* /sys/* /usr/src/* /etc/ssh/ssh_host* endexcludes echo "Stopping container" $VZCTL stop $CTID echo "Creating template" cd $CTROOT tar --numeric-owner -czvf /var/lib/vz/template/cache/$TEMPLATE.tar.gz -X /tmp/excludes . fi