Two birds with one home — cloned Vagrant multi-machines!

Paul Guerin
Oracle Developers
Published in
5 min readFeb 6, 2020

Vagrant makes it easy to create a single virtual machine. This is great — but what if you want multiple virtual machines?

Fortunately, multiple virtual machines using Vagrant is easy with just an edit of the Vagrant file.

Additionally, Vagrant can create multiple virtual machines that are each a clone of a single master virtual machine.

Vagrant is compatible with a variety of hypervisors, but for our purposes I’m going to use Oracle VirtualBox to create 2 virtual machine clones.

The OEL8 Vagrant Box has been released recently in the Oracle repository of Github. Download the whole repository, including the OEL8 component as follows:

> git clone https://github.com/oracle/vagrant-boxes

Out-of-the-box, the Vagrant file present in the \OracleLinux\8 directory will configure a single virtual host using the latest OEL8.

However, to get multi-machines, the Vagrant file needs to be edited. So all the code blocks will be placed within the main block.

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
..
<put_virtual_machine_configurations_in_here>
..
end

For the first code block, you may want to create a resource limit for each virtual machine. Let’s start with each virtual machine receiving a maximum of 1GB of memory, and just 1 CPU core.

  # hardware bound
config.vm.provider "virtualbox" do |vb|
vb.memory = 1024
vb.cpus = 1
# create a master VM before creating the linked clones
vb.linked_clone = true
end

At the end of the first code block I have configure the ‘linked_clone = true’.

    # create a master VM before creating the linked clones
vb.linked_clone = true

From the Vagrant documentation, the ‘linked_clone = true’ does the following:

By default new machines are created by importing the base box. For large boxes this produces a large overhead in terms of time (the import operation) and space (the new machine contains a copy of the base box’s image). Using linked clones can drastically reduce this overhead.

Linked clones are based on a master VM, which is generated by importing the base box only once the first time it is required. For the linked clones only differencing disk images are created where the parent disk image belongs to the master VM.

Now let’s configure the attributes of the first virtual machine. We’ll assign a host name and static IP address.

  # create a linked clone of the master VM
config.vm.define "email-MTA" do |mta|
mta.vm.box_check_update = false
mta.vm.box = "ol8-latest"
mta.vm.box_url = "https://yum.oracle.com/boxes/oraclelinux/latest/ol8-latest.box"
mta.vm.define "ol8-smtp-mta" # name of the virtual machine instance
mta.vm.hostname = "smtp-mta.host.com"
# assign a static IP
mta.vm.network "private_network", ip: "192.168.1.9"
end

Now configure the second virtual machine.

# create a linked clone of the master VM
config.vm.define "email-MDA" do |mda|
mda.vm.box_check_update = false
mda.vm.box = "ol8-latest"
mda.vm.box_url = "https://yum.oracle.com/boxes/oraclelinux/latest/ol8-latest.box"
mda.vm.define "ol8-smtp-mda" # name of the virtual machine instance
mda.vm.hostname = "smtp-mda.host.com"
# assign a static IP
mda.vm.network "private_network", ip: "192.168.1.10"
end

Now we are good to go, so start the virtual machines with Vagrant.

> vagrant up

The latest Vagrant box of OEL8 will be downloaded, and Vagrant can tell us which Vagrant boxes are downloaded so far.

> cd "C:\Program Files\Oracle\VirtualBox"> vagrant box list
ol7-latest (virtualbox, 0)
ol8-latest (virtualbox, 0)
>

After start up, to determine the status of all your virtual machines associated with Vagrant, just use the ‘global-status’ clause.

> vagrant global-status
id name provider state directory
------------------------------------------------------------------------------------------------------
ece1d42 email-MTA virtualbox running C:/Users/oracle/vagrant-boxes/OracleLinux/8
28e35ce email-MDA virtualbox running C:/Users/oracle/vagrant-boxes/OracleLinux/8
The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"
>

Also the Oracle VirtualBox CLI tells us which virtual machines are running.

> VBoxManage list runningvms
"email_email-MTA_1580297020948_19483" {d65d7083-fc34-4d1b-9c75-31b9a0de35c3}
"email_email-MDA_1580297795595_41309" {5dac821c-69fd-4638-986a-98a21bef643b}
>

The Oracle VirtualBox GUI also shows that there are 2 virtual machines running.

However the base virtual machine, that the 2 running virtual machines were cloned from, is turned off as expected.

Now we can confirm login in a single virtual machine.

> vagrant ssh email-MTAWelcome to Oracle Linux Server release 8.0 (GNU/Linux package kernel-uek is not installed)The Oracle Linux End-User License Agreement can be viewed here:* /usr/share/eula/eula.en_USFor additional packages, updates, documentation and community help, see:* http://yum.oracle.com/[vagrant@smtp-mta ~]$

Now login to the other virtual machine.

> vagrant ssh email-MDAWelcome to Oracle Linux Server release 8.0 (GNU/Linux package kernel-uek is not installed)The Oracle Linux End-User License Agreement can be viewed here:* /usr/share/eula/eula.en_USFor additional packages, updates, documentation and community help, see:* http://yum.oracle.com/[vagrant@smtp-mda ~]$

Shutdown of both virtual machines is the same as for a single Vagrant virtual machine.

> vagrant halt
==> email-MDA: Attempting graceful shutdown of VM...
==> email-MTA: Attempting graceful shutdown of VM...
>

Remember we used the special ‘linked_clone = true’ configuration in the Vagrant file.

    # create a master VM before creating the linked clones
vb.linked_clone = true

We can now see the effect of using a base, and clone virtual machine combination on storage requirements. The size of the base virtual machine is:

> dir "OL8U0_x86_64_1574143390111_30607"|find "File"4 File(s)  1,689,990,382 bytes>

So the base virtual machine for the clones is around 1.5 GB.

The size of the 2 clone virtual machines are determined here:

> dir "email_email-MDA_1580297795595_41309"|find "File"2 File(s)         11,430 bytes> dir "email_email-MTA_1580297020948_19483"|find "File"2 File(s)         11,430 bytes>

Consequently, we actually have 3 virtual machines:

  • single base virtual machine of 1.6 GB.
  • 2 clone virtual machines of just KB in total.

Without the ‘linked_clone = true’ configuration, we would have 2 virtual machines of 1.6 GB each for a total of 3.2 GB.

Instead, we have a base virtual machine of 1.6 GB and a couple of clones that are much smaller, for a total that is much less than 3.2 GB.

Paul Guerin is an international consultant that specialises in Oracle database. Paul is based from a global delivery center in South East Asia, but has clients from Australia, Europe, Asia, and North America. Moreover, he has presented at some of the world’s leading Oracle conferences, including Oracle Open World 2013. Since 2015, his work has been featured in the IOUG Best Practices Tip Booklet, and in publications from AUSOUG, Oracle Technology Network, and Oracle Developers (Medium). In 2019, he was awarded as a most valued contributor for the My Oracle Support Community. He is a DBA OCP, and continues to be a participant of the Oracle ACE program.

--

--

Oracle Developers
Oracle Developers

Published in Oracle Developers

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, Oracle ACEs, and Java Champions on all things Oracle technology. The views expressed are those of the authors and not necessarily of Oracle.