Automatic Creation of a ..

7
 Automatic Creation of a Virtual Network with VBoxManage Submitted by S teve on Wed, 18/09/2013 - 5:46pm I am using VirtualBox to create mul tiple vi rtual ma chine s running Ubuntu Linux , th en configuring them to create a vi rtual network. This virtual network is then used for test i ng and demonstrating basic networking and security concepts and technologies. The approach is to create a base virtual machine and the clone it multiple times to create nodes in the network. Each node needs minor configuration, such as setting IP addresses and routes for the network interfaces. I have detail ed instructions  (/sgordon/cr eat ing-a-virtual-network-of-linux- guests-using-virtualbox)  so that h opefull y anyone can create th e ir own virtual network. To make the creation of the virtual network easier and faster, my aim is to automatic as many s teps as po ss ibl e. The ulti mate goal i s t hat s omeone can downl oad a base virtual mac hine image, speci fy the des ir ed network topolog y, a nd t hen run a program that cre ates t hat virtual network. This articl e describes my curren t work towards this goal. Currently I am c ons tantly testing and improvi ng t he step s to auto mate the vi rtual ne twork creation. As of September 2013, the following instructions are very much a work-in-progress. The steps and scripts are poorly designed, implemented and tested. But they work for me and so may be of use to others. However I recommend anyone using these instructions be prepared to for things to go wrong - have a backup at least of your base virtual machine. Prepare the Host The VirtualBox command line interface, VBoxManage (http://www.virtualbox.org/manual/ch08.html) , and a set of Bash shell scripts (and some common Unix/Linux commands) are us ed to automate the creation of the vi rtual network. VB oxManage i s install ed when Virtual Box is i ns tal le d. To run the s cripts, Bash (http://www.cygwin.com/)  shell is needed. Bash is supported on most Unix-like operating systems, including Linux and OS X. For Windows, Bash can be run using Cygwin (http://www.cygwin.com/) . I have developed the Bash scripts using Ubuntu Linux as the host operating. Some quick testing confirmed that worked also in Cygwin on Windows 7. Further testing is needed to ensure they work successfully on other operating systems. If the scripts do not work on your host, you are stuck with using the man ual st eps (/sgordon/creating-a-virtual-network-of-linux-guests-using-virtualbox) . There are several steps to perform on your host computer before you can create a virtual network. The first steps, which apply to all host operating systems, are to: 1. Install VirtualBox  (htt ps://www.vir tualbox.org/) . For Windows and OSX you can download th e pack age from the VirtualBox  (htt ps :// www.vir t ualbox.org/)  website; in Ubuntu Linux you can install using the package manager, e.g. sudo apt-get install virtualbox . Note that as of October 2013, I have only tested using VirtualBox 4.1 and 4.2: version 4.3 was j ust released but I ha ven't yet tried it. 2. Do wnload the bas e v irtual m achine (or al tern ativel y create i t you rself using my instr uctions (/sgordon/creating-a-virtual-network-of-linux-guests-using-virtualbox) ). The file is a .tar.gz  and about 700 MB. Currently the base is up to date to SVN revision 37 (12 Feb 2014). Ch oos e from the followi ng s e rver l ocations: Thailand (http:/ /ict.si it.t u.ac .th/~sgor don/r esources/virtnet/base.tar.gz) , Japan (/virtnet/base.tar.gz) .

Transcript of Automatic Creation of a ..

7/27/2019 Automatic Creation of a ..

http://slidepdf.com/reader/full/automatic-creation-of-a- 1/6

 

Automatic Creation of a Virtual Network with

VBoxManage

Submitted by Steve on Wed, 18/09/2013 - 5:46pm

I am using VirtualBox to create multiple virtual machines running Ubuntu Linux, thenconfiguring them to create a virtual network. This virtual network is then used for testing anddemonstrating basic networking and security concepts and technologies. The approach is to

create a base virtual machine and the clone it multiple times to create nodes in the network.Each node needs minor configuration, such as setting IP addresses and routes for the networkinterfaces. I have detailed instructions (/sgordon/creating-a-virtual-network-of-linux-guests-using-virtualbox) so that hopefully anyone can create their own virtual network.

To make the creation of the virtual network easier and faster, my aim is to automatic as manysteps as poss ible. The ultimate goal is that someone can download a base virtual machineimage, specify the desired network topology, and then run a program that creates that virtual

network. This article describes my current work towards this goal.

Currently I am constantly testing and improving the steps to automate the virtual networkcreation. As of September 2013, the following instructions are very much a work-in-progress.The steps and scripts are poorly designed, implemented and tested. But they work for me and

so may be of use to others. However I recommend anyone using these instructions beprepared to for things to go wrong - have a backup at least of your base virtual machine.

Prepare the HostThe VirtualBox command line interface, VBoxManage

(http://www.virtualbox.org/manual/ch08.html), and a set of Bash shell scripts (andsome common Unix/Linux commands) are used to automate the creation of the virtualnetwork. VBoxManage is installed when VirtualBox is installed. To run the scripts, Bash

(http://www.cygwin.com/) shell is needed. Bash is supported on most Unix-like operating

systems, including Linux and OS X. For Windows, Bash can be run using Cygwin

(http://www.cygwin.com/). I have developed the Bash scripts using Ubuntu Linux as thehost operating. Some quick testing confirmed that worked also in Cygwin on Windows 7.Further testing is needed to ensure they work successfully on other operating systems. If the

scripts do not work on your host, you are stuck with using the manual steps

(/sgordon/creating-a-virtual-network-of-linux-guests-using-virtualbox).

There are several steps to perform on your host computer before you can create a virtualnetwork. The first steps, which apply to all host operating systems, are to:

1. Install VirtualBox (https://www.virtualbox.org/). For Windows and OSX you candownload the package from the VirtualBox (https://www.virtualbox.org/) website; inUbuntu Linux you can install using the package manager, e.g. sudo apt-get installvirtualbox. Note that as of October 2013, I have only tested using VirtualBox 4.1 and

4.2: version 4.3 was just released but I haven't yet tried it.2. Download the base virtual machine (or alternatively create it yourself using my instructions

(/sgordon/creating-a-virtual-network-of-linux-guests-using-virtualbox)). Thefile is a .tar.gz and about 700 MB. Currently the base is up to date to SVN revision 37

(12 Feb 2014). Choose from the following server locations: Thailand

(http://ict.siit.tu.ac.th/~sgordon/resources/virtnet/base.tar.gz), Japan

(/virtnet/base.tar.gz).

7/27/2019 Automatic Creation of a ..

http://slidepdf.com/reader/full/automatic-creation-of-a- 2/6

3. Unpack the downloaded virtual machine into a directory called base and open it inVirtualBox from the menu Machine -> Add...

Now proceed to the instructions specific to your operating system.

Ubuntu Linux

I have tested on Ubuntu Linux 12.04 LTS and Ubuntu Linux 13.04. It's highly likely other

Linux distributions will also work successfully.

1. Install Subversion (http://subversion.apache.org/), e.g. sudo apt-get installsubversion

Microsoft Windows

I have tested on Microsoft Windows 7. I hope it will also work in Windows 8.

1. Install Cygwin (http://www.cygwin.com/). This provides Linux like command lineinterface, allowing you to run the Bash scripts necessary to create the virtual network.

Install the default (minimal) set of packages plus the following (which you need to selectmanually): subversion, nano, openssh.

The next step is to setup the Subversion repository. After that is complete, there is a smallstep specific to Windows to make VBoxManage available in your path.

Mac OS X

This has been successfully tested on a 2012 MacBook Air running OSX 10.8. Subversion isneeded. If you don't already have it, then it is available via Xcode:

1. Install Xcode (https://developer.apple.com/xcode/), which provides access toSubversion.2. Make Subversion (svn) available on the command line. One option is to set your path in a

terminal:

$ PATH="${PATH}:/Applications/Xcode.app/Contents/Developer/usr/bin/"

Other options are presented on Superuser

(http://superuser.com/questions/455214/where-is-svn-on-os-x-mountain-lion).

Usernames and Passwords

If you downloaded the provided base virtual machine then the username and passwordinformation is:

Default user: network; password network

Root user: root; password network

MySQL admin password: network

If you create the base virtual machine yourself, then you may use the same passwords.

Checkout the virtnet Subversion Repository

Open a terminal and proceed with the following steps. They should work on all host operatingsystems that support Bash.

7/27/2019 Automatic Creation of a ..

http://slidepdf.com/reader/full/automatic-creation-of-a- 3/6

A set of Bash scripts and accompanying data files are used to automate the creation of thevirtual network. These are available on my virtnet Subversion repository. First create a

directory called svn in your home directory.

sgordon@host:~$ cd ~sgordon@host:~$ mkdir svnsgordon@host:~$ cd svn

Now checkout the repository:

sgordon@host:~/svn/$ svn checkout https://sandilands.info/svn/virtnet/ 

This creates a directory called virtnet inside the ~/svn directory. Enter it and browse thecontents. There are two sub-directories that are important at this moment:

~/svn/virtnet/bin/host/: contains the Bash scripts for running on the host.

~/svn/virtnet/data/topologies/: contains the interfaces files that need to be copied toeach virtual machine that is created.

There is one more change to make. The permissions of a SSH private key file must be modified

to be readable only by the user. To do so, type:

sgordon@host:~/svn/$ chmod go-rwx virtnet/data/defaults/root/.ssh/id_rsa

I have prepared some topologies that can be created automatically (you can see images of thetopologies in the directory ~/svn/virtnet/data/images/). I describe how the topologies work

and how to create your own towards the end of this article (create_topology).

Microsoft Windows (Cygwin) Only

If your host operating is Microsoft Windows, you should now setup the path to VBoxManage by

executing the following in the Cygwin terminal:

sgordon@cygwin $ cd ~/svn/virtnet/bin/hostsgordon@cygwin $ PATH=${PATH}:.sgordon@cygwin $ ln -s "/cygdrive/c/Program

Files/Oracle/VirtualBox/VBoxManage.exe" VboxManage

Create the Virtual Network

There are a number of pre-created topologies that you can choose from to quickly build anetwork. See them here (/virtnet/topologies).For this demo I'll use topology 5 which contains three nodes:

Topology 5 

7/27/2019 Automatic Creation of a ..

http://slidepdf.com/reader/full/automatic-creation-of-a- 4/6

The rectangles in the topology diagram labelled neta and netb are switches createdautomatically by VirtualBox - they are not virtual machines that we need to create. The IP

addresses in all cases start with 192.168.. For example, interface eth1 of node 1 has IPaddress 192.168.1.11.

Enter the directory containing the Bash scripts:

sgordon@host:~/svn/virtnet$ cd bin/host/

Now run vn-createtopology with the topology number as the first (and only) parameter:

sgordon@host:~/svn/virtnet/bin/host$ bash vn-createtopology 5

This is the main script, which in turn calls others. You will see some output (hopefully)

indicating that the virtual machines for the nodes are created. You may be prompted for apassword for the root user of the base virtual machine (depending on whether or not yousetup public/private keys for SSH).

If all goes well, then the new virtual machines, one for each node in the topology, will be

created in VirtualBox. You can now start them and test your network.

TroubleshootingExample Output When Successfully Creating a Node

Below is an example of the terminal output when I created topology 1 on my computer. If 

successful, yours should like very similar. There may be some differences about theknown_hosts file in the last few lines.

$ bash vn-createtopology 1vn: Starting to create new VM node1

vn: Cloning base ...

  Name: base (UUID: 79bc4595-cb8b-4606-9bcc-2f571a10802d) *

vn: VM base already has snapshot base

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

Machine has been successfully cloned as "node1"

vn: Virtual machine base cloned to node1

vn: Disconnecting cable 2 on node1vn: Disconnecting cable 3 on node1

vn: Disconnecting cable 4 on node1

vn: Configuring NIC 2 on node1

vn: Setting port forwarding on node1

Waiting for VM "node1" to power on...

VM "node1" has been successfully started.

vn: Virtual machine node1 is now running

vn: Removing SSH keys

/home/sgordon/.ssh/known_hosts updated.

Original contents retained as /home/sgordon/.ssh/known_hosts.old

vn: Setting interfaces on guest using SSH ...

vn: New VM node1 has been created

Error: Permissions for id_rsa are too open

7/27/2019 Automatic Creation of a ..

http://slidepdf.com/reader/full/automatic-creation-of-a- 5/6

Below is an example terminal output in the case that the file id_rsa does not have correctpermissions. As a result you will receive several prompts to enter the root password (which isnetwork). If you receive this error, then delete the created node(s) and set the permissions for

the file id_rsa as described in the original instructions. Then create the topology again - if successful, you will receive similar output to that above.

$ bash vn-createtopology 1vn: Starting to create new VM node1

vn: Cloning base ...

  Name: base (UUID: 79bc4595-cb8b-4606-9bcc-2f571a10802d) *

vn: VM base already has snapshot base

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

Machine has been successfully cloned as "node1"

vn: Virtual machine base cloned to node1

vn: Disconnecting cable 2 on node1

vn: Disconnecting cable 3 on node1

vn: Disconnecting cable 4 on node1

vn: Configuring NIC 2 on node1

vn: Setting port forwarding on node1

Waiting for VM "node1" to power on...

VM "node1" has been successfully started.

vn: Virtual machine node1 is now running

vn: Removing SSH keys

/home/sgordon/.ssh/known_hosts updated.

Original contents retained as /home/sgordon/.ssh/known_hosts.old

vn: Setting interfaces on guest using SSH ...

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@ WARNING: UNPROTECTED PRIVATE KEY FILE! @

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Permissions 0644 for './../../data/defaults/root/.ssh/id_rsa' are too open.It is required that your private key files are NOT accessible by others.

This private key will be ignored.

bad permissions: ignore key: ./../../data/defaults/root/.ssh/id_rsa

[email protected]'s password:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@ WARNING: UNPROTECTED PRIVATE KEY FILE! @

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Permissions 0644 for './../../data/defaults/root/.ssh/id_rsa' are too open.

It is required that your private key files are NOT accessible by others.

This private key will be ignored.

bad permissions: ignore key: ./../../data/defaults/root/.ssh/[email protected]'s password:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@ WARNING: UNPROTECTED PRIVATE KEY FILE! @

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Permissions 0644 for './../../data/defaults/root/.ssh/id_rsa' are too open.

It is required that your private key files are NOT accessible by others.

This private key will be ignored.

bad permissions: ignore key: ./../../data/defaults/root/.ssh/id_rsa

[email protected]'s password:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@ WARNING: UNPROTECTED PRIVATE KEY FILE! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Permissions 0644 for './../../data/defaults/root/.ssh/id_rsa' are too open.

It is required that your private key files are NOT accessible by others.

This private key will be ignored.

7/27/2019 Automatic Creation of a ..

http://slidepdf.com/reader/full/automatic-creation-of-a- 6/6

Content: Howto (/sgordon/taxonomy/term/212)Interest:

Ubuntu Linux (/sgordon/taxonomy/term/302) VirtualBox

(/sgordon/taxonomy/term/328) Windows (/sgordon/taxonomy/term/301)

bad permissions: ignore key: ./../../data/defaults/root/.ssh/id_rsa

[email protected]'s password:

vn: New VM node1 has been created

General

The scripts for creating the virtual network currently have several parameters hardcoded. They

assume you have followed the exact instructions for creating the base virtual machine

(/sgordon/creating-a-virtual-network-of-linux-guests-using-virtualbox). If thescripts report errors and/or the virtual machines are not created, the first step is to open theBash scripts (vn-createtopology, vn-newnode, vn-clonebase) in a text editor and check anyparameters. Edit as needed. In the future I will aim to make important parameters inputs to

the script (as opposed to hardcoded).

Workflow Summary

One approach to using these scripts for creating virtual networks is:

1. Run vn-createtopology to create the desired topology of nodes

2. Use the virtual network3. When finished with the virtual network, delete the node virtual machines4. Repeat from step 1.

You should delete the automatically created nodes before creating a new virtual network (or at

least rename them) since the new network will use the same node names, e.g. node1, node2,node3.

The base virtual machine is not used in the virtual network. It is cloned to create the nodes inthe network. Keep the base up to date (in terms of required software packages andconfiguration) so when you create the virtual network the nodes will be up to date.

Creating Your Own Topology

See the README.txt

(https://sandilands.info/viewvc/virtnet/data/topologies/README.txt?view=markup) file in the data/topologies/ directory of the SVN repository for anexplanation of how to create your own topology.