From: http://archives.opennebula.org/documentation:archives:rel2.2:cong
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.sh
Into/etc/init.d
Directory 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/interfaces
That 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.sh
Is already ded by default. You have to specify the values that will be written inside
context.sh
And 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 usingCONTEXT
Parameter. 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.sh
File inside the contextualization image. These variables can be specified in three different ways:
hostname = "MAINHOST"
- Using template Variables
- Using Virtual Network template Variables
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.sh
Is compatiblebash
Syntax 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 outlineCONTEXT
Section 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_configuration
Folder:
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.local
As:
#!/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