- Disable/delete udev net and cdrom persistent rules. On boot, linux distributions scan for new hardware and discovered network and cdrom are added to a file. This process is really useful for physical machines so adding or taking out a new network card wont change the name of the rest, making the configuration we had still useful. With virtual machines this is a nuisance. A simple MAC address change will make udev create a new device for that interface and the configuration will no longer be used.
- Unconfigure network. This way the VM won’t configure the network before the OpenNebula contextualization kicks in.
- Add contextualization scripts to startup. These scripts will configure the network and will call init.sh from the context cdrom enabling us to do some magic with the context section of the VM template.
One of the changes introduced in OpenNebula 3.8 is the new contextualization packages. The new version does the same as the previous one with some changes that we hope will make people creating images happier.
Modular Contextualization Scripts
Now the script launched on VM boot has less logic:
- Mounts the context cdrom
- Exports the variables from context.sh
- Executes any script located in
- Executes init.sh from cdrom
- Unmounts the cdrom
Network configuration is now a script located in
/etc/one-context.d/00-network. Any file located in that directory will be executed on start, in alphabetical order. This way we can add any script to configure or start processes on boot. For example, we can have a script that populates authorized_keys file using a variable in the context.sh. Remember that those variables are exported to the environment and will be easily accessible by the scripts:
#!/bin/bash echo "$SSH_PUBLIC_KEY" > /root/.ssh/authorized_keys
Network Configuration Driven by Contextualization
The new network configuration scripts can still infer the network configuration from the MAC address of the VM, the same as the previous versions. The way OpenNebula generates MAC addresses by default is by setting the first 2 bytes of the MAC address to the prefix configured in
oned.conf and the rest 4 bytes to the IP assigned. This method is convenient but lacks flexibility and some interesting parameters like the network mask or gateway information.
Other way we had to configure the network was adding a script to the contextualization cdrom using the
file. This method is very flexible but most of the time we always configure the same network parameters so this script changes very rarely. Also, in new OpenNebula versions we discourage the use of contextualization
file parameter as it can lead to security problems.
Now the network configuration script will search for some predefined environment variables to configure network parameters. The parameters are:
|<DEV>_IP||IP assigned to the interface|
|<DEV>_MASK||Interface net mask|
We will substitute
<DEV> with the interface the variable refers to in uppercase, as in ETH0, ETH1, etc. As an example, we can have a network defined this way:
NAME=public NETWORK_ADDRESS=188.8.131.52 NETWORK_MASK=255.255.255.0 GATEWAY=184.108.40.206
And then in the VM contextualization those parameters for
eth0 can be expressed as:
CONTEXT=[ ETH0_IP = "$NIC[IP, NETWORK=\"public\"]", ETH0_NETWORK = "$NIC[NETWORK_ADDRESS, NETWORK=\"public\"]", ETH0_MASK = "$NIC[NETWORK_MASK, NETWORK=\"public\"]", ETH0_GATEWAY = "$NIC[GATEWAY, NETWORK=\"public\"]" ]
Generation of Custom Contextualization Packages
OpenNebula source code comes with the scripts and the files needed to generate those packages. This way you can also generate custom packages tweaking the scripts that will go inside your images or adding new scripts that will perform other duties.
The files are located in
base: files that will be in all the packages. Right now it contains empty
udevrules and the init script that will be executed on startup.
base_<type>: files specific for linux distributions. It contains the contextualization scripts for the network and comes in
debflavors. You can add here your own contextualization scripts and they will be added to the package when you run the generation script.
generate.sh: The script that generates the packages.
postinstall: This script will be executed after the package installation and will clean the network and
udevconfiguration. It will also add the init script to the started services on boot.
To generate the packages you will need:
- Ruby >= 1.8.7
- gem fpm
- dpkg utils for deb package creation
- rpm utils for rpm package creation
You can also give to the generation script some parameters using env variables to generate the packages. For example, to generate an
rpm package you will execute:
$ PACKAGE_TYPE=rpm ./generate.sh
These are the default values of the parameters, but you can change any of them the same way we did for
VERSION=3.7.80 MAINTAINER=C12G Labs <firstname.lastname@example.org> LICENSE=Apache PACKAGE_NAME=one-context VENDOR=C12G Labs DESCRIPTION=" This package prepares a VM image for OpenNebula: * Disables udev net and cd persistent rules * Deletes udev net and cd persistent rules * Unconfigures the network * Adds OpenNebula contextualization scripts to startup To get support use the OpenNebula mailing list: https://opennebula.io/community:mailinglists" PACKAGE_TYPE=deb URL=https://opennebula.io
For more information check the
README.md file from that directory.