Opennebula自定义VM 实现方法-Contextualizing Virtual Machines 2.2

from:http://archives.opennebula.org/documentation:archives:rel2.2:cong

There are two contextualization mechanisms available in OpenNebula: the automatic 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 the /etc/init.d directory in the VM root file system.
  • Execute the script at boot time before starting any network service, usually runlevel 2 should 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 would scan the available network interfaces, extract their MAC addresses, make the MAC to IP conversion and construct a /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 included by default. You have to specify the values that will be written inside context.sh and the files that will be included in the image.

Defining Context

In VM description file you can tell OpenNebula to create a contextualization image and to fill it with values using CONTEXTparameter. 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 added to context.sh file 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 OpenNebula
hostname="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 included 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 should 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 should 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 file context.sh is compatible with bash 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 outline the CONTEXT 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 a /vms_configuration folder with:

  • 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.sh
fi
 
umount /mnt
 
exit 0      

We use an indirection (rc.local calls 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.sh
fi
 
hostname $HOSTNAME
ifconfig eth0 $IP_PUBLIC
 
useradd -m $USERNAME
 
mkdir -p ~$USERNAME/.ssh
cat /mnt/id_rsa.pub >> ~$USERNAME/.ssh/authorized_keys
 
chown -R $USERNAME /home/$USERNAME

Opennebula自定义VM 实现方法-Contextualizing Virtual Machines 2.2

时间: 2024-11-10 08:27:08

Opennebula自定义VM 实现方法-Contextualizing Virtual Machines 2.2的相关文章

PatentTips - Enhancing the usability of virtual machines

BACKGROUND Virtualization technology enables a single host computer running a virtual machine monitor ("VMM") to present multiple abstractions and/or views of the host, such that the underlying hardware of the host appears as one or more indepen

Methods and systems to control virtual machines

Methods and systems are provided to control the execution of a virtual machine (VM). A VM Monitor (VMM) accesses VM Control Structures (VMCS) indirectly through access instructions passed to a processor. In one embodiment, the access instructions inc

C#事件的理解以及自定义事件的方法

事件的理解: 在skyline项目的开发中,遇到了一个新的知识:事件. 在程序中,我希望实现一个功能,当视点坐标移动的时候可以实时的得到视点的坐标.这里就需要使用事件这个概念:当坐标发生移动,则触发了一个特定的事件,他可以发出一个信号,而用户可以自定义一个函数(参数必须与事件委托的参数相同,这个后面解释),当他发出一个信号,我就可以执行这个函数. 比方说:甲和乙是朋友,上午见面了,乙和甲说,今天中午吃完饭叫我一声,我带你去网吧玩. 在这个情景中,甲吃饭这件事情是乙没有办法控制的.他只可以等甲吃完

PatentTips - Method for network interface sharing among multiple virtual machines

BACKGROUND Many computing systems include a network interface card (NIC) to provide for communications with other systems and devices over a network. In a computing system running multiple operating systems (OSs) on multiple virtual machines, each OS

PatentTips - Transparent unification of virtual machines

BACKGROUND Virtualization technology enables a single host computer running a virtual machine monitor ("VMM") to present multiple abstractions and/or views of the host, such that the underlying hardware of the host appears as one or more indepen

[SQL in Azure] Getting Started with SQL Server in Azure Virtual Machines

This topic provides guidelines on how to sign up for SQL Server on a Azure virtual machine and how to get started creating SQL Server databases in Microsoft public cloud environment. With SQL Server in Azure Virtual Machines, you get the full benefit

PatentTips - Method to manage memory in a platform with virtual machines

BACKGROUND INFORMATION Various mechanisms exist for managing memory in a virtual machine environment. A virtual machine platform typically executes an underlying layer of software called a virtual machine monitor (VMM) which hosts one to many operati

自定义类工厂方法

1.自定义工厂方法 什么是工厂方法(快速创建方法) 类工厂方法是一种用于分配.初始化实例并返回一个它自己的实例的类方法.类工厂方法很方便,因为它们允许您只使用一个步骤(而不是两个步骤)就能创建对象. 例如new 自定义类工厂方法的规范 (1)一定是+号开头 (2)返回值一般是instancetype类型 (3)方法名称以类名开头,首字母小写 示例 + (id)person; + (id)person { return [[Person alloc]init]; } + (id)personWit

iOS开发中自定义字体的方法

http://www.cnblogs.com/iyou/archive/2014/05/25/3751669.html 1. 首先下载你想要设置的字体库,例如设置方正启体简体 2. 添加到工程,一定要注意勾选红色框框处,默认是不勾选的  添加以后 3.在plist文件中添加 4.现在已经添加成功了,但是要使用就必须知道FontName,用以下代码可查到 NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyName