Infrastructure at your Service

Michael Schwalm

Scripts to duplicate an Oracle database virtual machine: step two

In January, I promised to show you the next step of a procedure to manage templates with Virtual Box. Don’t worry, I have not forgotten but I was very busy! This step, the final one, consists on customizing of a virtual machine configuration at the start. VMware does it with Templates on ESX, using for example Sysprep for MS Windows guests. I am going to show you how to do it with one simple text file.

Sorry for Windows adepts, my example is based on an Oracle Linux 6. But the scenario is the same for both environments, there are just different commands and syntaxes on Windows.

Last time, we looked at the startup step. You have one or more cloned virtual machines, all identical: same hostname, same IP, etc. The purpose here is to customize each one in order to have machines based on a single template, but with proper network configuration, without causing for instance any Oracle issues.

When you use the script to clone the template virtual machine, a parameter file (conf.ini) is generated in a dedicated shared folder for each new virtual machine, containing a new hostname, IP and netmask, and an init flag. This file will allow the virtual machine to be reset at its first startup. The init flag will prevent the script to be run at each startup:

  • 0: script is not executed
  • 1: script is executed.

Let’s see how it works. Here is an example of the parameter file:

You have to create a new script to read information on this file. The script will:

  • Create a mountpoint to the shared folder created at previous steps

mount -t vboxsf vmform /mnt/vmform

  • Parse the file conf.ini in the mount point
CONF=`cat /mnt/vmform/conf.ini`
NEW_HOSTNAME=`echo $CONF | cut -d":" -f1`
NEW_IP=`echo $CONF | cut -d":" -f2`
NEW_MASK=`echo $CONF | cut -d":" -f3`
FLAG=`echo $CONF | cut -d":" -f4`


  • Change the hostname by editing /etc/sysconfig/network file

cat /etc/sysconfig/network | sed -e “s/$HOSTNAME/$NEW_HOSTNAME/I”

  • Update the /etc/hosts file

cat /etc/hosts | sed -e “s/$HOSTNAME/$NEW_HOSTNAME/I”

  • Update Oracle configuration (tnsnames.ora and listener.ora files)
cat $TNS_ADMIN/listener.ora | sed -e "s/$HOSTNAME/$NEW_HOSTNAME/I"
cat $TNS_ADMIN/tnsnames.ora | sed -e "s/$HOSTNAME/$NEW_HOSTNAME/I"
  • Change the current IP and Netmask
IP=`ifconfig eth0 | awk '/^ *inet / {print substr($2, 6)}'`
MASK=`ifconfig eth0 | awk '/^ *inet / {print substr($3, 6)}'`
cat /etc/sysconfig/network-scripts/ifcfg-eth0 | sed -e "s/$IP/$NEW_IP/g"
cat /etc/sysconfig/network-scripts/ifcfg-eth0 | sed -e "s/$MASK/$NEW_MASK/g"


  • Delete the /etc/udev/rules.d/70-persistent-net.rules file, in order to reinitialize the network interface allias

rm -f /etc/udev/rules.d/70-persistent-net.rules

Remark: To avoid any problem, delete the HWADDR parameter from scripts of any network interfaces (/etc/sysconfig/network-scripts/ifcfg-ethX).

  • Reset the init flag to a null value, to avoid the script to be run at each startup

cat /mnt/vmform/conf.ini | sed -e “s/1$/0/”

  • Restart the virtual machine


Of course, I only give you the main commands here. You have to adapt the script to check errors, make copies before editing files, create the temporary files required by the said command, etc.

The script can be copied after creating cloned virtual machines, but it can be fastidious if you have to create a lot of VMs. My advice is to copy it on the template before the clonage. This way, the machines will automatically be customized at the first startup and restarted when script is finished.

It will be completely automatic once your template is ready. When I created a new lab for our dbi InSite workshops, I clicked on the main script (duplicate_script.cmd) and was able to go drink a coffee afterwards – a big coffee because the process is not that fast :-) When I came back, all of my virtual machines were running and completely ready for work. I was able to delete the virtual machines after the training sessions and recreate them very, very easily!

Contact me if you need help or if you have any remarks on my tool.


Leave a Reply

four + 1 =

Michael Schwalm
Michael Schwalm