Friday, 22 February 2013

Exalogic Virtual Tea Break Snippets - Scripted Template Generation from an existing vServer

As part of your Exalogic Virtual environment you may want to build vServer that will be used, going forwards, as a template for future vServers. Currently the "Exalogic Elastic Cloud Administrator's Guide" has an appendix describing how this can be achieved using the OVMM interface. Based on internal A-Team work it is now possible to achieve this directly from a compute nodes command-line without accessing OVMM.

As a result of this I have built the script below that will take the files associated with a "Stopped" vServer and converts them to a template.

For this templating process to work the script will need to be executed on a machine with access to the /OVS/Repositories/* directories and this means running directly on one of the Compute Nodes (I generally run it on Compute Node 1).

Because of the space and resource limitations of the Compute Node (minimal OS) we will need to create a and mount a Share from the internal ZFS to save the working files and ultimately the Template. To this end the script will take a number of parameters that will specification of these directories. If these are not specified the script assumes we have the ZFS /export/common/images mounted on /u01/common/images.

As can been seen from the Usage section below the script only mandates the Name of the vServer to be copied but assumes that the user has stopped the vServer previously. Once the template has been created, or post copy, the vServer can be restarted.


CreateTemplateFromVServer.sh

Usage

usage: ./CreateTemplateFromVServer.sh -n <vServer Name> [-r <Repository Directory>] [-w <Working Directory>] [-d <Destination Directory>]

 -n <vServer Name> vServer to be templatised or cloned.
 -r <Repository Directory> Location of the repository the default is /OVS/Repositories/*
 -w <Working Directory> Working directory where intermediate files will be copied default /u01/common/images/vServerTemplateWIP.
 -d <Destination Directory> Directory where the template tgz will be created. Default is /u01/common/images/vServerTemplates
 -h This information.

Script


Download

#!/bin/bash

################################################################################
#
#  Exalogic EL X2-2 2.0.0.4 (Linux x86-64) Configuration Script.
#
#  HEADER START
# 
#  THIS SCRIPT IS PROVIDED ON AN ?AS IS? BASIS, WITHOUT WARRANTY OF ANY KIND, 
#  EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT 
#  THE COVERED SCRIPT IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR 
#  PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 
#  OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY COVERED SOFTWARE PROVE 
#  DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER 
#  CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION.
#  NO USE OF ANY COVERED SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS 
#  DISCLAIMER.
#
#  When distributing this Code, include this HEADER in each file.
#  If applicable, add the following below this this HEADER, with the fields
#  enclosed by brackets "[]" replaced with your own identifying information:
#       Portions Copyright [yyyy] [name of copyright owner]
# 
#  HEADER END
# 
# 
#       Copyright 2011 Andrew Hopkinson, Oracle Corporation UK Ltd.
#
################################################################################

export RUN_DATE=`date +"%Y%m%d-%H%M"`

#############################################################
##
## copyServerFiles
## ===============
##
## Copy the vServer img files to working location.
##
#############################################################

function copyServerFiles() {
 echo "Copying vServer Files"
 
 TEMPLATE_DIR=$WORKING_DIR/$VSERVER_NAME/template/BASE
 VIRTUAL_MACHINES_DIR=$REPOSITORY_DIR/VirtualMachines
 VIRTUAL_DISKS_DIR=$REPOSITORY_DIR/VirtualDisks
 ROOT_IMG_FILE=""
 
 GREP_VM_CFG=`grep "'$VSERVER_NAME'" $VIRTUAL_MACHINES_DIR/*/vm.cfg`
 VM_CFG=${GREP_VM_CFG%%:*}
 
 mkdir -p $TEMPLATE_DIR
 cp $VM_CFG $TEMPLATE_DIR
 
 DISKS=`grep disk $VM_CFG`
 FILES=${DISKS#*:}
 while [[ "$DISKS" != "$FILES" ]]
 do
  #echo "FILES = $FILES"
  #echo "DISKS = $DISKS"
  IMG_FILE=${FILES%%,*}
  echo "Copying $IMG_FILE"
  cp $IMG_FILE $TEMPLATE_DIR
  # Set root imag we assume the first
  if [[ "$ROOT_IMG_FILE" == "" ]]
  then
   SYSTEM_IMG=${IMG_FILE#*VirtualDisks/}
   ROOT_IMG_FILE=$TEMPLATE_DIR/$SYSTEM_IMG
   echo "Root Image $ROOT_IMG_FILE"
  fi
  # Shuffle line for next disk
  DISKS=${DISKS#*:}
  FILES=${DISKS#*:}
 done
}

#############################################################
##
## unconfigureVM
## =============
##
## Remove / edit the files that a created / modified when the
## template has been used to created a vServer.
##
#############################################################

function unconfigureVM() {
 echo "Unconfiguring Root Image $ROOT_IMG_FILE"
 cd $WORKING_DIR
 # Mount the Image file
 export LOOP=`losetup -f`
 # Make Temp Mount Directory
 mkdir -p $SYSTEMIMGDIR
 # Create Loop for the System Image
 losetup $LOOP $ROOT_IMG_FILE
 kpartx -a $LOOP
 mount /dev/mapper/`basename $LOOP`p2 $SYSTEMIMGDIR
 #Change Dir into mounted Image
 cd $SYSTEMIMGDIR
 
 # Unconfigure
 cp etc/sysconfig/ovmd etc/sysconfig/ovmd.orig
 sed 's/INITIAL_CONFIG=no/INITIAL_CONFIG=yes/g' etc/sysconfig/ovmd.orig > etc/sysconfig/ovmd
 rm -v etc/sysconfig/ovmd.orig
 
 sed -i '/.*/d' etc/resolv.conf
 
 # Remove existing ssh information
 rm -v root/.ssh/*
 rm -v etc/ssh/ssh_host*
 
 # Clean up networking
 sed -i '/^GATEWAY/d' etc/sysconfig/network
 
 # Clean up hosts
 sed -i '/localhost/!d' etc/hosts
 sed -i '/localhost/!d' etc/sysconfig/networking/profiles/default/hosts
 
 # Remove Network scripts
 rm -v etc/sysconfig/network-scripts/ifcfg-*eth*
 rm -v etc/sysconfig/network-scripts/ifcfg-ib*
 rm -v etc/sysconfig/network-scripts/ifcfg-bond*
 
 # Remove log files
 rm -v var/log/messages*
 rm -v var/log/ovm-template-config.log
 rm -v var/log/ovm-network.log
 rm -v var/log/boot.log*
 rm -v var/log/cron*
 rm -v var/log/maillog*
 rm -v var/log/rpmpkgs*
 rm -v var/log/secure*
 rm -v var/log/spooler*
 rm -v var/log/yum.log*
 
 # Remove Kernel Messages
 
 rm -v var/log/dmesg
 
 # Edit modprobe file
 sed -i '/bond/d' etc/modprobe.conf
 
 # Edit hwconf file
 cp etc/sysconfig/hwconf etc/sysconfig/hwconf.orig
 sed 's/mlx4_en/mlx4_core/' etc/sysconfig/hwconf.orig > etc/sysconfig/hwconf
 rm -v etc/sysconfig/hwconf.orig
 
 # Remove Exalogic Config file
 rm -v etc/exalogic.conf
 
 #Remove bash history
 rm -v root/.bash_history
 
 # Unmount the image file
 cd $WORKING_DIR
 umount $SYSTEMIMGDIR
 kpartx -d $LOOP
 losetup -d $LOOP
 rm -rf $SYSTEMIMGDIR
}

function buildTemplateTgz() {
 echo "Creating the Template tgz file"
 mkdir -p $DESTINATION_DIR
 cd $TEMPLATE_DIR
 TEMPLATE_TGZ=$DESTINATION_DIR/el_template_$VSERVER_NAME.tgz
 tar -zcvf $TEMPLATE_TGZ *
 echo "Template $TEMPLATE_TGZ file created"
}

function cleanWorkingDir() {
 echo "Cleaning Working Directory"
 cd $WORKING_DIR
 rm -rfv $VSERVER_NAME
}

#############################################################
##
## createTemplate
## ==============
##
## High level template creation function that will call the 
## required processing function in the necessary sequence.
##
#############################################################

function createTemplate() {
 copyServerFiles
 unconfigureVM
 buildTemplateTgz
 cleanWorkingDir
 echo ""
 echo ""
 echo "****************************************************"
 echo "**"
 echo "** $TEMPLATE_TGZ has been created from"
 echo "** vServer $VSERVER_NAME "
 echo "**"
 echo "****************************************************"
 echo ""
 echo ""
}


function usage() {
 echo ""
 echo >&2 "usage: $0 -n <vServer Name> [-r <Repository Directory>] [-w <Working Directory>] [-d <Destination Directory>] "
 echo >&2 ""
 echo >&2 "          -n <vServer Name> vServer to be templatised or cloned."
 echo >&2 "          -r <Repository Directory> Location of the repository the default is $REPOSITORY_DIR"
 echo >&2 "          -w <Working Directory> Working directory where intermediate files will be copied default $WORKING_DIR."
 echo >&2 "          -d <Destination Directory> Directory where the template tgz will be created. Default is $DESTINATION_DIR"
 echo ""
 exit 1
}

###############################################################
##
## Simple start for the script that will extract the parameters
## and call the appriate start function.
##
###############################################################

export WORKING_DIR="/u01/common/images/vServerTemplateWIP"
export DESTINATION_DIR="/u01/common/images/vServerTemplates"
export REPOSITORY_DIR="/OVS/Repositories/*"
export SYSTEMIMGDIR=/mnt/elsystem

while [ $# -gt 0 ]
do
 case "$1" in 
  -n) VSERVER_NAME="$2"; shift;;
  -r) REPOSITORY_DIR="$2"; shift;;
  -d) DESTINATION_DIR="$2"; shift;;
  -w) WORKING_DIR="$2"; shift;;
  *) usage;;
  *) break;;
 esac
 shift
done


# Processing function call
if [[ "$VSERVER_NAME" == "" || "$REPOSITORY_DIR" == "" ]]
then
 usage
else
 createTemplate
fi



echo "****************************************"
echo "*****  Finished Creating Template  *****"
echo "****************************************"

1 comment:

  1. Hi, I keep trying to create a template from a server, however whenever I do and build a server from that template, the vserver still retains the original hostname of my old server.

    ReplyDelete