Configure Networking with DHCP

This tutorial shows how to contextualize networking parameters in a virtual machine using standard networking tools. This approach can be followed if all your virtual machines can be placed in the same broadcasting domain.

Requirements

System

  • Properly configured dhcp server, i.e., with access to XEN hosts network

Virtual Machine

  • dhcp client with configuration changes provided here.

dhcp Server Configuration

As stated in the Requirements section, to handle networking contextualization you will need a dhcp server (we are using dhcp3-server package from debian in this howto). You also need to add dynamic machines to your dns server or, alternatively, add them to the /etc/hosts file in both your dhcp server machine and all the virtual machines using this contextualization.

An example of this configuration follows:

/etc/hosts

Excerpt from /etc/hosts with dynamic machines:

# Virtual Machine names
192.168.3.201   vm01
192.168.3.202   vm02
192.168.3.203   vm03
192.168.3.204   vm04

/etc/dhcp3/dhcpd.conf

Excerpt from dhcp3-server configuration file describing machines.

host vm01 {
  hardware ethernet 00:16:3e:01:01:01;
  fixed-address vm01;
  option host-name vm01;
}

host vm02 {
  hardware ethernet 00:16:3e:01:01:02;
  fixed-address vm02;
  option host-name vm02;
}

host vm03 {
  hardware ethernet 00:16:3e:01:01:03;
  fixed-address vm03;
  option host-name vm03;
}

host vm04 {
  hardware ethernet 00:16:3e:01:01:04;
  fixed-address vm04;
  option host-name vm04;
}

Here we are using MAC addresses to distinguish each node. This MAC addresses will be used to assign different names and IP addresses to dynamically deployed machines.

Virtual Machine Configuration

In order to let VMs configure correctly their network parameters, follow these steps:

  • Configure VM network interface to get dynamic IP address using dhcp.
  • Modify dhcp client configuration so it gets host-name value using dhcp. To do this make sure that there is a line in dhcp client configuration that requires host-name, example:
request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, host-name,
        netbios-name-servers, netbios-scope, interface-mtu;
  • Add a hook for hostname configuration. Create a file in /etc/dhcp3/dhclient-enter-hooks.d named host-name that contains this line:
hostname $new_host_name
  • Add machine names and IPs to /etc/hosts as described in previous section.

Virtual Machine Description

To set the MAC address of a particular Virtual Machine you will need a line similar to the following one in the ONE VM description file:

NIC=[mac="00:16:3e:01:01:01"]

Summarizing, with the above setup, Virtual Machine with mac address 00:16:3e:01:01:01v would receive IP address 192.168.3.201 and hostname vm01 whenever it starts.