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
1 #!/bin/bash 2 3 ################################################################################ 4 # 5 # Exalogic EL X2-2 2.0.0.4 (Linux x86-64) Configuration Script. 6 # 7 # HEADER START 8 # 9 # THIS SCRIPT IS PROVIDED ON AN ?AS IS? BASIS, WITHOUT WARRANTY OF ANY KIND, 10 # EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT 11 # THE COVERED SCRIPT IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR 12 # PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 13 # OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY COVERED SOFTWARE PROVE 14 # DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER 15 # CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. 16 # NO USE OF ANY COVERED SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS 17 # DISCLAIMER. 18 # 19 # When distributing this Code, include this HEADER in each file. 20 # If applicable, add the following below this this HEADER, with the fields 21 # enclosed by brackets "[]" replaced with your own identifying information: 22 # Portions Copyright [yyyy] [name of copyright owner] 23 # 24 # HEADER END 25 # 26 # 27 # Copyright 2011 Andrew Hopkinson, Oracle Corporation UK Ltd. 28 # 29 ################################################################################ 30 31 export RUN_DATE=`date +"%Y%m%d-%H%M"` 32 33 ############################################################# 34 ## 35 ## copyServerFiles 36 ## =============== 37 ## 38 ## Copy the vServer img files to working location. 39 ## 40 ############################################################# 41 42 function copyServerFiles() { 43 echo"Copying vServer Files" 44 45 TEMPLATE_DIR=$WORKING_DIR/$VSERVER_NAME/template/BASE 46 VIRTUAL_MACHINES_DIR=$REPOSITORY_DIR/VirtualMachines 47 VIRTUAL_DISKS_DIR=$REPOSITORY_DIR/VirtualDisks 48 ROOT_IMG_FILE="" 49 50 GREP_VM_CFG=`grep "'$VSERVER_NAME'" $VIRTUAL_MACHINES_DIR/*/vm.cfg` 51 VM_CFG=${GREP_VM_CFG%%:*} 52 53 mkdir -p $TEMPLATE_DIR 54 cp $VM_CFG $TEMPLATE_DIR 55 56 DISKS=`grep disk $VM_CFG` 57 FILES=${DISKS#*:} 58 while [[ "$DISKS" != "$FILES" ]] 59 do 60 #echo "FILES = $FILES" 61 #echo "DISKS = $DISKS" 62 IMG_FILE=${FILES%%,*} 63 echo"Copying $IMG_FILE" 64 cp $IMG_FILE $TEMPLATE_DIR 65 # Set root imag we assume the first 66 if [[ "$ROOT_IMG_FILE" == "" ]] 67 then 68 SYSTEM_IMG=${IMG_FILE#*VirtualDisks/} 69 ROOT_IMG_FILE=$TEMPLATE_DIR/$SYSTEM_IMG 70 echo"Root Image $ROOT_IMG_FILE" 71 fi 72 # Shuffle line for next disk 73 DISKS=${DISKS#*:} 74 FILES=${DISKS#*:} 75 done 76 } 77 78 ############################################################# 79 ## 80 ## unconfigureVM 81 ## ============= 82 ## 83 ## Remove / edit the files that a created / modified when the 84 ## template has been used to created a vServer. 85 ## 86 ############################################################# 87 88 function unconfigureVM() { 89 echo"Unconfiguring Root Image $ROOT_IMG_FILE" 90 cd $WORKING_DIR 91 # Mount the Image file 92 export LOOP=`losetup -f` 93 # Make Temp Mount Directory 94 mkdir -p $SYSTEMIMGDIR 95 # Create Loop for the System Image 96 losetup $LOOP $ROOT_IMG_FILE 97 kpartx -a $LOOP 98 mount /dev/mapper/`basename $LOOP`p2 $SYSTEMIMGDIR 99 #Change Dir into mounted Image 100 cd $SYSTEMIMGDIR 101 102 # Unconfigure 103 cp etc/sysconfig/ovmd etc/sysconfig/ovmd.orig 104 sed's/INITIAL_CONFIG=no/INITIAL_CONFIG=yes/' etc/sysconfig/ovmd.orig > etc/sysconfig/ovmd 105 rm -v etc/sysconfig/ovmd.orig 106 107 sed -i '/.*/d' etc/resolv.conf 108 109 # Remove existing ssh information 110 rm -v root/.ssh/* 111 rm -v etc/ssh/ssh_host* 112 113 # Clean up networking 114 sed -i '/^GATEWAY/d' etc/sysconfig/network 115 116 # Clean up hosts 117 sed -i '/localhost/!d' etc/hosts 118 sed -i '/localhost/!d' etc/sysconfig/networking/profiles/default/hosts 119 120 # Remove Network scripts 121 rm -v etc/sysconfig/network-scripts/ifcfg-*eth* 122 rm -v etc/sysconfig/network-scripts/ifcfg-ib* 123 rm -v etc/sysconfig/network-scripts/ifcfg-bond* 124 125 # Remove log files 126 rm -v var/log/messages* 127 rm -v var/log/ovm-template-config.log 128 rm -v var/log/ovm-network.log 129 rm -v var/log/boot.log* 130 rm -v var/log/cron* 131 rm -v var/log/maillog* 132 rm -v var/log/rpmpkgs* 133 rm -v var/log/secure* 134 rm -v var/log/spooler* 135 rm -v var/log/yum.log* 136 137 # Remove Kernel Messages 138 139 rm -v var/log/dmesg 140 141 # Edit modprobe file 142 sed -i '/bond/d' etc/modprobe.conf 143 144 # Edit hwconf file 145 cp etc/sysconfig/hwconf etc/sysconfig/hwconf.orig 146 sed's/mlx4_en/mlx4_core/' etc/sysconfig/hwconf.orig > etc/sysconfig/hwconf 147 rm -v etc/sysconfig/hwconf.orig 148 149 # Remove Exalogic Config file 150 rm -v etc/exalogic.conf 151 152 #Remove bash history 153 rm -v root/.bash_history 154 155 # Unmount the image file 156 cd $WORKING_DIR 157 umount $SYSTEMIMGDIR 158 kpartx -d $LOOP 159 losetup -d $LOOP 160 rm -rf $SYSTEMIMGDIR 161 } 162 163 function buildTemplateTgz() { 164 echo"Creating the Template tgz file" 165 cd $TEMPLATE_DIR 166 TEMPLATE_TGZ=$DESTINATION_DIR/el_template_$VSERVER_NAME.tgz 167 tar -zcvf $TEMPLATE_TGZ * 168 echo"Template $TEMPLATE_TGZ file created" 169 } 170 171 function cleanWorkingDir() { 172 echo"Cleaning Working Directory" 173 cd $WORKING_DIR 174 rm -rfv $VSERVER_NAME 175 } 176 177 ############################################################# 178 ## 179 ## createTemplate 180 ## ============== 181 ## 182 ## High level template creation function that will call the 183 ## required processing function in the necessary sequence. 184 ## 185 ############################################################# 186 187 function createTemplate() { 188 copyServerFiles 189 unconfigureVM 190 buildTemplateTgz 191 cleanWorkingDir 192 echo"" 193 echo"" 194 echo"****************************************************" 195 echo"**" 196 echo"** $TEMPLATE_TGZ has been created from" 197 echo"** vServer $VSERVER_NAME " 198 echo"**" 199 echo"****************************************************" 200 echo"" 201 echo"" 202 } 203 204 205 function usage() { 206 echo"" 207 echo>&2 "usage: $0 -n <vServer Name> [-r <Repository Directory>] [-w <Working Directory>] [-d <Destination Directory>] " 208 echo>&2 "" 209 echo>&2 " -n <vServer Name> vServer to be templatised or cloned." 210 echo>&2 " -r <Repository Directory> Location of the repository the default is $REPOSITORY_DIR" 211 echo>&2 " -w <Working Directory> Working directory where intermediate files will be copied default $WORKING_DIR." 212 echo>&2 " -d <Destination Directory> Directory where the template tgz will be created. Default is $DESTINATION_DIR" 213 echo"" 214 exit 1 215 } 216 217 ############################################################### 218 ## 219 ## Simple start for the script that will extract the parameters 220 ## and call the appriate start function. 221 ## 222 ############################################################### 223 224 export WORKING_DIR="/u01/common/images/vServerTemplateWIP" 225 export DESTINATION_DIR="/u01/common/images/vServerTemplates" 226 export REPOSITORY_DIR="/OVS/Repositories/*" 227 export SYSTEMIMGDIR=/mnt/elsystem 228 229 while [ $# -gt 0 ] 230 do 231 case"$1" in 232 -n) VSERVER_NAME="$2"; shift;; 233 -r) REPOSITORY_DIR="$2"; shift;; 234 -d) DESTINATION_DIR="$2"; shift;; 235 -w) WORKING_DIR="$2"; shift;; 236 *) usage;; 237 *) break;; 238 esac 239 shift 240 done 241 242 243 # Processing function call 244 if [[ "$VSERVER_NAME" == "" || "$REPOSITORY_DIR" == "" ]] 245 then 246 usage 247 else 248 createTemplate 249 fi 250 251 252 253 echo"****************************************" 254 echo"***** Finished Creating Template *****" 255 echo"****************************************" 256
Originally posted on the Old Toxophilist