2012-10-18

VirtualBox, CentOS, Network and Template

I have been working with VirtualBox and CentOS recently, here are some notes about this experience.

I used VirtualBox 4.2 and CentOS 6.3, but most of this should work with other products too. I created the first headless, minimal CentOS via NetInstall.
I cover two points: create a template machine and configure the Network.

Configure the Network

We want Internet access and a LAN local to the host.
For background information read: Networking in VirtualBox by Fat Bloke on June 2012.

The easiest is to enable two Network Adapters: One will be "Host-only" and the second "Nat". In the "Preference" menu you can see the DHCP server range for the Host-only Network. So you may set fixed addresses outside this range.
Next: start the guest. There may be various results at first, depending on a lot of things. Some problem might be solved by rm -f /etc/udev/rules.d/70-persistent-net.rules and a reboot.

Anyway, configure the two interfaces (set your own IP and MAC addresses) :

/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO=none
IPADDR="192.168.56.12"
HWADDR="08:00:27:30:60:68"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="d22a3e0a-5e0d-4185-9aaa-7295e3547224"

/etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE="eth1"
BOOTPROTO="dhcp"
HWADDR="08:00:27:02:A8:46"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"

I am not sure about the UUID and HWADDR usefulness. Be careful to first check that your configuration is not inverse eth0 ~ eth1. An ifconfig -a should give an indication.

Configure /etc/hosts to add the fixed IP: 192.168.56.12 web-serv1.my-domain.

Restart the Network: /etc/init.d/network restart. Quick test: ping $(hostname). You should have Internet access try with elinks google.com or yum update. You should also be able to access from your host via SSH. On Windows you may add the host name and IP to C:\WINDOWS\system32\drivers\etc\hosts.

Prepare the machine to be a template from which other will be cloned

When cloning don't forget to set new Mac address.

This script may ease the process. You need to adapt them!
# See: http://www.cyberciti.biz/tips/vmware-linux-lost-eth0-after-cloning-image.html
rm -f /etc/udev/rules.d/70-persistent-net.rules

# Remove MAC Address
ifcfg="/etc/sysconfig/network-scripts/ifcfg-eth0"
content=$( grep -v HWADDR $ifcfg )
echo "$content"> $ifcfg

# Remove SSH keys (they will be recreated at startup)
rm -f /etc/ssh/*_host_*
After cloning, don't forget to change the hostname and fixed IP.
This script may ease the process. You may need to adapt them!
ifcfg="/etc/sysconfig/network-scripts/ifcfg-eth0"

content=$( grep -v HWADDR $ifcfg )
if [ -z "$content" ] ; then
    echo "Problem $ifcfg maybe empty !?" ;  exit 1
fi

mac=$( ifconfig eth0 | grep eth0 | awk '{print $5}' )
if [ -z "$mac" ] ; then
    echo "Problem eth0 maybe down !?" ;  exit 1
fi

echo "$content" > $ifcfg
echo "HWADDR=\"$mac\"" >> $ifcfg

Next step ...

I should have use some Puppet or Chef automation, but since I already tried without success some time ago, I was not so keen to reiterate the experience.

I also wonder if NFS mounts would allow to keep a system up-to-date. The idea would be to have a common "kernel" base re-used by all the VM via NFS. The VM would have only user space variations. But I am not so sure if it would be reliable or even considered "good practice".

2023 summary

  Life is bigger than what you can imagine.  Still using Roam  http://www.roamresearch.com/  to take notes Still using Mastodon mainly, but ...