Openebula VM cannot obtain IP address problem solved

Source: Internet
Author: User

Http://archives.opennebula.org/documentation:archives:rel2.2:cong

Contextualizing virtual machines 2.2

 

There are two contextualization mechanic ISMs available in opennebula: the automatic ic ip assignment, and a more generic way to give any file and configuration parameters. You can use any of them individually, or both.

Using Virtual Network leases within a virtual machine

With opennebula you can derive the IP address assigned to the VM from the MAC address using the mac_preffix: IP rule. in order to achieve this we provide context scripts for Debian, Ubuntu, centos and opensuse based systems. this scripts can be easily adapted for other distributions, check dev.opennebula.org.

To configure the Virtual Machine follow these steps:

 

These actions are to configure the VM, the commands refer to the VMS root file system

 

  • Copy the script$ONE_SRC_CODE_PATH/share/scripts/vmcontext.shInto/etc/init.dDirectory in the VM root file system.
  • Execute the script at boot time before starting any network service, usually runlevel 2 shoshould work.

 

$ ln /etc/init.d/vmcontext.sh /etc/rc2.d/S01vmcontext.sh

 

Having done so, whenever the VN boots it will execute this script, which in turn wocould scan the available network interfaces, extract their MAC addresses, make the Mac to IP conversion and construct/etc/network/interfacesThat will ensure the correct IP assignment to the corresponding interface.

Generic contextualization

The method we provide to give configuration parameters to a newly started virtual machine is using an ISO image (ovf recommendation ). this method is network agnostic so it can be used also to configure network interfaces. in the VM description file you can specify the contents of the ISO file (Files And Directories ), tell the device the ISO image will be accessible and specify the configuration parameters that will be written to a file for later use inside the virtual machine.

In this example we see a virtual machine with two associated disks. the disk image holds the filesystem where the operating system will run from. The ISO image has the contextualization for that VM:

  • context.sh: File that contains configuration variables, filled by opennebula with the parameters specified in the VM description file
  • init.sh: Script called by VM at start that will configure specific services for this VM instance
  • certificates: Directory that contains certificates for some service
  • service.conf: Service configuration

 

This is just an example of what a contextualization image may look like. Only context.shIs already ded by default. You have to specify the values that will be written inside context.shAnd the files that will be encoded in the image.

 

Defining Context

In VM description file you can tell opennebula to create a contextualization image and to fill it with values usingCONTEXTParameter. For example:

CONTEXT = [  hostname   = "MAINHOST",  ip_private = "$NIC[IP]",  dns        = "$NETWORK[DNS, NAME=\"Public\"]",  ip_gen     = "10.0.0.$VMID",  files      = "/service/init.sh /service/certificates /service/service.conf"]

Variables inside context section will be addedcontext.shFile inside the contextualization image. These variables can be specified in three different ways:

  • Hardcoded variables:
hostname   = "MAINHOST"
  • Using template Variables
    • $<template_variable>: Any single value variable of the VM template, like for example :\
      ip_gen     = "10.0.0.$VMID"
    • $<template_variable>[<attribute>]: Any single value contained in a multiple value variable in the VM template, like for example:
      ip_private = $NIC[IP]
    • $<template_variable>[<attribute>, <attribute2>=<value2>]: Any single value contained in a multiple value variable in the VM template, setting one atribute to discern between multiple variables called the same way, like for example:
      ip_public = "$NIC[IP, NETWORK=\"Public\"]"
  • Using Virtual Network template Variables
    • $NETWORK[<vnet_attribute>, NAME=<vnet_name>]: Any single value variable in the Virtual Network (vnet_name) template, like for example:
      dns        = "$NETWORK[DNS, NAME=\"Public\"]"

The file generated will be something like this:

# Context variables generated by OpenNebulahostname="MAINHOST"ip_private="192.168.0.5"dns="192.168.4.9"ip_gen="10.0.0.85"files="/service/init.sh /service/certificates /service/service.conf"target="sdb"

Some of the variables have special meanings, but none of them are mandatory:

Attribute Description
Files Files and directories that will be encoded in the contextualization Image
Target Device Where the contextualization image will be available to the VM instance. Please note that the proper device mapping may depend on the guest OS, E.g. Ubuntu VMS shocould use HD * as the target device

 

A default target attribute is generated automatically by opennebula as "HDB" or "SDB", depending on the default prefix set at ONED. conf. you can set here any other value, but you have to take into account the other disks defined in the VM template to avoid collisions.

 

Using Context

The VM shoshould be prepared to use the contextualization image. first of all it needs to mount the contextualization image somewhere at boot time. also a script that executes after boot will be useful to make use of the information provided.

The filecontext.shIs compatiblebashSyntax so you can easilly source it inside a shellscript to get the variables that it contains.

Example

Here we propose a way to use this contextualization data. Each Unix has their own filesystem layout and way of handling init scripts, this examples assumes a Debian-based virtual machine.

We are going to use contextualization data to set the hostname, the IP address and a user with known SSH keys.

First thing, lets outlineCONTEXTSection of the VM template:

CONTEXT = [  hostname  = "$NAME",  ip_public = "$NIC[IP, NETWORK=\"Public\"]",  username  = virtualuser  files     = "/vms_configuration/id_rsa.pub /vms_configuration/init.sh"]

The opennebula front-end will thus require/vms_configurationFolder:

  • id_rsa.pub: Public SSH key to be added to the trusted SSH keys of the new user
  • init.sh: Script that will perform the configuration. explained below.

Now we will need to configure the VM to make use of this data. We are going to place in/etc/rc.localAs:

#!/bin/sh -e mount -t iso9660 /dev/sdc /mnt if [ -f /mnt/context.sh ]; then  . /mnt/init.shfi umount /mnt exit 0      

We use an indirection (RC. Local CILS init. Sh) So changing the script means editing a file locally rather that changing it inside the VMS.

The init. Sh script will be the one actually doing the work:

#!/bin/bash if [ -f /mnt/context.sh ]; then  . /mnt/context.shfi hostname $HOSTNAMEifconfig eth0 $IP_PUBLIC useradd -m $USERNAME mkdir -p ~$USERNAME/.sshcat /mnt/id_rsa.pub >> ~$USERNAME/.ssh/authorized_keys chown -R $USERNAME /home/$USERNAME

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.