#!/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/' 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 [-r ] [-w ] [-d ] " echo >&2 "" echo >&2 " -n vServer to be templatised or cloned." echo >&2 " -r Location of the repository the default is $REPOSITORY_DIR" echo >&2 " -w Working directory where intermediate files will be copied default $WORKING_DIR." echo >&2 " -d 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 "****************************************"