fbpixel

VM Cloning that works

Tommaso Doninelli}
Tommaso Doninelli
Share:

How to conigre vCenter templates to create VM clones with custom hostname and ip address.

To clone or not to clone?

Having a VM to clone is handy: no need to install packages or dependencies, ssh certs and users are already configured - of course, we can use cloud-init (which is not really well integrated in vCenter) or chef/puppet to provision and configure our servers, but in most cases we only need to spin up a vm that is exactly the same as the others.

Cloning a vm seems a good way, but it comes with 2 main issues:

  • how to change the hostname to make it match the vm name?
  • why the clone VM has same IP address of its parent?

The problem is that the cloned vm is, well, a clone. It is exactly equal to the parent. Some configurations are automatically managed by vCenter (mac address), but it is not enough: the clone hostname will be the same of its parent, and unfortunately, also its ip address! Why? scroll down…

sbambolino bam bam
Un tizio che guarda una valle da paura

Configuring hostname with VM Customization

Naming things is hard. Usually, the vm hostname is configured to match the vm name in vCenter. Usually the DHCP offer the possibility to register a DNS entry with the hostname of the vm, very handy.

Setting the hostname equal to the vm name is easy in vCenter: all we need to do is to create a VM Customization Specification. In the vCenter menu, select VM Policies and Profile and create a new item.

In the vCenter menu, select VM Policies and Profile
Create a new VM Customization Configuration

In the vCenter menu, select VM Policies and Profile

Under computer name, select Use the virtual machine name

In the vCenter menu, select VM Policies and ProfileSet the hostname to the vm name
Customizing cloned vm hostname

That’s it! Now we can customize the os of the vm setting the hostname equal to the vm name.

My cloned vm get the same ip address of its parent

This is something related to systemd: dhclient by default uses the link-layer address (MAC address), while systemd-networkd uses the contents of /etc/machine-id.

Since the VMs were cloned, they have the same machine-id and the DHCP server returns the same IP for both. We want to setup a new machine-id on the first boot of the cloned vm.

To do so, we must:

  • update the parent vm with a script to generate the machine-id at the first startup
  • delete any configured id
  • power off the vm and convert it to a template

Create the machine-id at startup

We can use systemd to run this command online at the first power on: let’s create a new unit file, systemd-firstboot.service with the following contents

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[Unit]
Description=First Boot Wizard
Documentation=man:systemd-firstboot(1)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-remount-fs.service
Before=systemd-sysusers.service sysinit.target shutdown.target
ConditionPathIsReadWrite=/etc
ConditionPathExists=!/etc/machine-id    

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/etc/vmware-config.sh
StandardOutput=tty
StandardInput=tty
StandardError=tty

[Install]
WantedBy=sysinit.target

This service file will

  • run as soon as possible, WantedBy=sysinit.target
  • run only if the machine-id is not set : ConditionPathExists=!/etc/machine-id

The executed script, /etc/vmware-config.sh, is simple - it must only generate a machine-id :)

1
2
3
#!/bin/bash
dbus-uuidgen --ensure=/etc/machine-id
dbus-uuidgen --ensure

Clean the parent vm

We have to remove 2 files:

1
2
rm -f /etc/machine-id
rm /var/lib/dbus/machine-id

Create a VM Template

The last step is to power off the parent vm, and export to a template.

Done! Now you can create a new vm from the template, and it will have its hostname equal to the vm name and a unique machine-id!

Tommaso Doninelli

CEO @ HakunaCloud

10 years as CTO, former Software Engineer at Amazon AWS, Cloud Solution Architect with projects in US, Europe and United Arab Emirates.

"I am a DevOps and automation advocate; you can test, deploy, analyze and improve even your grandma recipes. "