Infrastructure at your Service

Michael Schwalm

Scripts to duplicate an Oracle database virtual machine: step one

In my last posting of November 9, 2012, Manage templates manually with Oracle Virtual Box, I have presented a solution to duplicate virtual machines using Oracle/ Virtual Box. I did not show the scripts in this first publication, but I promised to tell you more about it…

Let me present the case.

In September 2012, I was asked to create a training environment consisting of five identical virtual machines, with the following constraints:

  • Virtual machines must be configured with Oracle Enterprise Linux 6.1
  • Three Oracle 11g R2 databases must be configured
  • Each virtual machine must have its own network configuration (meaning different hostnames and IP addresses) to co-exist over the network
  • Virtual machines are accessed by the participants via the network through a SSH connexion

All of these training virtual machines run on a powerful laptop which is used as a virtual host (based on Oracle Virtual Box).

The first question I asked myself: Am I going to clone five times the same virtual machine with the graphical user interface? How will I change each VM’s hostname, IP address, Oracle configuration? It is a way to do, but if we have to create new training environments in the future, changing the operating system or the Oracle release, we will have to do that again and again. The GUI is not fitting this kind of requirements well.

The best solution for me is to automate this task using scripts. I spent some time writing these scripts, but now, I am able to create as many virtual machines as I want using a properly configured template.

Remember, the solution presented in my first blog acts at two levels:

  • At host level
  • At virtual machine guest level

Today, we are going to develop the host part.

What you need on your virtual host is simple:

  • A template virtual machine
  • The configuration file
  • The duplicate script

The Template

The template consists of a virtual machine, with temporary hostname and IP address, and configured with all required softwares (OS modules, rdbms…). Once the template corresponds to your final configuration, just power it off and keep it in the Virtual Box inventory.



Be careful not to delete it!

The configuration file

This file can register all data you plan to use to customize guest virtual machines. You just have to separate each parameter with a separation character. Let’s see the example of my configuration file:



Below is an explanation of each value:

Value Definition
vmrefbr01 The name of the template to duplicate
clonebr01 The name of the new virtual machine
D:VMs The path to store new virtual machine files
srvora01 The hostname of the new virtual machine The new IP of the new virtual machine The new netmask of the new virtual machine
1 A flag used to specify the new virtual machine if it must be customized at its startup*
D:shares The path where the new virtual machine will find informations to be customized


* This flag is read by the script on the guest virtual machine and allows to run or not the customization process. If the flag is 0, the script exits. If the flag is 1, the script edits the machine configuration.

You can see that the configuration file contains as many rows as there are clones of the template to create. In our case, we have five rows, to create five virtual machines for the training.

The duplicate script

The duplicate script is written in MS-DOS batch language. It interacts with Virtual Box using VBoxManage.exe to clone, configure, and start cloned virtual machines. It is composed of a main script, which parses this configuration script, and a child script (or cloning script), run for each line of the configuration file.

Main script
The main script parses the configuration file in a loop. It must be run with the name of the configuration file as input parameter. See below:

C:> duplicate_script.cmd “configuration_file.cfg”

Below is the content of the script:

REM        ## Beginning of the script
@echo off
REM        ## Open the configuration file and run the cloning procedure ##
for /f "tokens=1-8 delims=;" %%A in ('type %1') do (
    start clone.bat %%A %%B %%C %%D %%E %%F %%G %%H %%I
 REM        ## End of the script


For each line found in the configuration file, the cloning script is run by passing the input parameters. This way to run the cloning script has two advantages:

  • All virtual machines are created at the same time (gain of time)
  • You can monitor the creation of each virtual machine in its own window


Child (or cloning) script
This script is run for each new virtual machine to create, depending of the number of line registered in the configuration file.
The first thing the script does is to initialize variables for next use.


Note that %DIRECTORY% and %SHARED_FOLDER% use respectively the name of the virtual machine and the hostname to create the complete path for virtual machine data and shared folder location.
Next, it creates the shared folder path if it does not exist.

        echo The directory %SHARED_FOLDER% does not exist and will be created.
        mkdir %SHARED_FOLDER%


Once the shared folder created, the script duplicates the template into the new virtual machine.

echo Cloning virtual machine %TEMPLATE%…
“C:Program FilesOracleVirtualBoxVBoxManage” clonevm “%TEMPLATE%” –name “%VMNAME%” –basefolder %DIRECTORY% –register

The option “–register” enables the virtual machine to appear in the Virtual Box inventory.
The shared folder and the new virtual machine are created, the shared folder must now be attached to the virtual machine.

echo Adding shared folder %SHARED_FOLDER% to %VMNAME%…
“C:Program FilesOracleVirtualBoxVBoxManage” sharedfolder add “%VMNAME%” –name “vmform” –hostpath “%SHARED_FOLDER%”

The option “–name” specifies the name of the shared folder as it is seen by the virtual machine. For exemple, with netuse on Windows or with mount on Linux.
The shared folder is intended to store a parameter file, required for the new virtual machine customization. The creation of this parameter file is done with a basic OS command:

echo Generation of the parameter file %SHARED_FOLDER%conf.ini for %VMNAME%…

You can see that the parameter file is created with a fictive fifth parameter (no value after the last separator character). It is to guarantee that Windows does not add blank space after the last parameter which is the flag. A blank space after this parameter causes troubles when customizing Windows environment…
To finish, the script starts automatically the new virtual machine inorder to start its customization without user intervention:

echo Starting %VMNAME%…
“C:Program FilesOracleVirtualBoxVBoxManage” startvm “%VMNAME%” –type headless

The option “–type headless” is facultative. It allows to start the virtual machine without opening its window in the Virtual Box interface. The virtual machine starts in background on a virtual host.

That’s it!

If you have correctly followed my advice, you are now able to clone a virtual machine from a template and to start it automatically with the free tool Oracle Virtual Box. Next time, we will see how to configure the template to allow a duplicated virtual machine to configure itself at startup.

To be continued…


One Comment

  • Ryan adams says:

    I read your blog completely which is better than first one and here is some unique stuff discussing about the oracle virtualization. template, scripts and some useful guidance so I would like to thanks for sharing it all…you did great job regularly.


Leave a Reply

1 × = two

Michael Schwalm
Michael Schwalm