A VM Sample with Context & Network 4.4

The purpose of this section is to demonstrate how to quickly deploy a VM with OpenNebula in a few easy steps. We will assume that you have properly configured OpenNebula and that you have at least one worker node running KVM (this guide does not work with Xen for the moment).

We have prepared and contextualized a VM which is available for download here. The VM runs http://ttylinux.net/.

Preparing the Network

For this example we are going to use the simplest possible network configuration. Create a new file based on the following template and change the LEASES entries to available IPs from your network.

You should also change the BRIDGE entry if you Hypervisor is configured to use a different bridge.

$ cat small_network.net 
NAME = "small_network"
TYPE = FIXED

BRIDGE = br0
LEASES = [ IP="192.168.0.5"]
LEASES = [ IP="192.168.0.6"]
LEASES = [ IP="192.168.0.7"]

Once the file is prepared we can create the network:

<xterm> $ onevnet create small_network.net </xterm>

Preparing the Image

Create a new file based on the following template

$ cat marketplace_image.one
NAME = "ttylinux"
PATH = "http://marketplace.c12g.com/appliance/4fc76a938fb81d3517000003/download"
TYPE = OS

Once the file is prepared we can create the image:

<xterm> $ oneimage create marketplace_image.one –datastore default </xterm>

You can also use the Marketplace tab in Sunstone to import the image.

Running the VM

Create a new file based on the following template

$ cat ttylinux.one 
NAME = ttylinux

CPU    = 0.1
MEMORY = 64

DISK = [ IMAGE = "ttylinux" ]
NIC  = [ NETWORK = "small_network" ]

FEATURES = [ acpi="no" ]

We are ready to deploy the VM. To do so simply do: <xterm> $ onevm create ttylinux.one </xterm>

It will take a minute or so to copy the image to /var/lib/one and to boot up the system. In the meantime we can figure out what IP the VM will have so that we can ssh into it.

<xterm> $ onevm show ttylinux|grep IP

IP=192.168.1.6,

</xterm>

By now, the VM should be up and running: <xterm> $ onevm list

ID     USER     NAME STAT CPU     MEM        HOSTNAME        TIME
 3 oneadmin myttyser runn   0   65536       localhost 00 00:06:49

</xterm>

Note: If the STAT attribute is not runn you should read the logs to see why it did not boot. You can find these logs in /var/log/one/<id>.log (vm specific log) and /var/log/one/oned.log.

We can ssh into the VM. The user is root and the password is password: <xterm> $ ssh root@192.168.1.6 Warning: Permanently added '192.168.1.6' (RSA) to the list of known hosts. root@192.168.1.6's password:

Chop wood, carry water.

# </xterm>

You might have been wondering how did the VM get automatically configured with an IP from the pool of IPs defined by the ONE Network associated to the VM template. Basically, we developed a script that runs during the bootup procedure which configures the IP address based on the MAC address of the VM. This is more thoroughly explained here.

Running the VM Again with a CONTEXT

We have not yet used the CONTEXT feature of OpenNebula which not only provides a simple way to configure the IP of the VM, but which also allows us to configure users, public keys, the host name, and any other thing we might think of. You can read a more detailed explanation on how to contextualize here.

Create a new file with the following content in /var/tmp

$ cat /var/tmp/init.sh 
#!/bin/bash
 
if [ -f /mnt/context/context.sh ]
then
  . /mnt/context/context.sh
fi
 
 
if [ -n "$HOSTNAME" ]; then
    echo $HOSTNAME > /etc/HOSTNAME
    hostname $HOSTNAME
fi
 
if [ -n "$IP_PUBLIC" ]; then
    ifconfig eth0 $IP_PUBLIC
fi
 
if [ -n "$NETMASK" ]; then
    ifconfig eth0 netmask $NETMASK
fi
 
 
if [ -f /mnt/context/$ROOT_PUBKEY ]; then
    cat /mnt/context/$ROOT_PUBKEY >> /root/.ssh/authorized_keys
fi
 
if [ -n "$USERNAME" ]; then
    adduser -s /bin/bash -D $USERNAME
    if [ -f /mnt/context/$USER_PUBKEY ]; then
        mkdir -p /home/$USERNAME/.ssh/
        cat /mnt/context/$USER_PUBKEY >> /home/$USERNAME/.ssh/authorized_keys
        chown -R $USERNAME /home/$USERNAME/.ssh
        chmod -R 600 /home/$USERNAME/.ssh
    fi
fi

Copy your public key to a tmp directory (e.g. /var/tmp/, $HOME/public) and a context section to the ttylinux.one template.

$ cat ttylinux.one 
NAME = ttylinux

CPU    = 0.1
MEMORY = 64

DISK = [ IMAGE = "ttylinux" ]
NIC  = [ NETWORK = "small_network" ]

FEATURES = [ acpi="no" ]

CONTEXT = [
    hostname    = "$NAME",
    ip_public   = "PUBLIC_IP",
    files      = "/var/tmp/init.sh /var/tmp/id_dsa.pub",
    target      = "hdc",
    root_pubkey = "id_dsa.pub",
    username    = "opennebula",
    user_pubkey = "id_dsa.pub"
]

Now we can ssh to the VM without entering a password, since the id_dsa.pub has been copied to the authorized_keys of both root and the username account you have define in the template.

<xterm> $ ssh opennebula@192.168.0.7

Chop wood, carry water.

$ </xterm>