KVM change guest hostname of the image-d automatically on boot

kvm-virtualization

I have created an image off the shut down VM.
From new XML template I was able to create a new VM using virsh create newvm.xml.
basically the image was created by copying the image of a shut down VM which was built from Ubuntu OS image – obviously during the install I had to specify the hostname.
Now I shut down the VM and copied the image to use it as a base image for my new VM guests. The problem is that I dont know how to change the damn hostname automatically.
Now eache newly created VM is starting with the same hostname as the machine from which the image was created. Is there any way how to handle this ?

   <domain type='kvm' id='10'>
  <name>sensu.gc.example.com</name>
  <uuid>3d638021-1fd5-96c4-5b7b-a5c11d69c314</uuid>
  <memory>1048576</memory>
  <currentMemory>1048576</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-1.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/kvm/sensu/tmpZ2yf6n.qcow2'/>
      <target dev='hda' bus='ide'/>
      <alias name='ide0-0-0'/>
      <address type='drive' controller='0' bus='0' unit='0'/>
    </disk>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:50:89:7b'/>
      <source bridge='br0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/0'/>
      <target port='0'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/0'>
      <source path='/dev/pts/0'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5900' autoport='yes' listen='127.0.0.1'>
      <listen type='address' address='127.0.0.1'/>
    </graphics>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='apparmor' relabel='yes'>
    <label>libvirt-3d638021-1fd5-96c4-5b7b-a5c11d69c314</label>
    <imagelabel>libvirt-3d638021-1fd5-96c4-5b7b-a5c11d69c314</imagelabel>
  </seclabel>
</domain>

I thought that by specifying hostname in a tag it would be changed automatically on VM creation. Is there any way how I can achieve what I'm trying to ?
Cheers.

Best Answer

You cannot change the hostname from the XML file of the VM, nor from KVM. This XML file list only the properties of the VM, the hostname is not one such property. The hostname is set in the VM OS (Ubuntu in your case) during the boot process.

To change the hostname you should look at the /etc/hostname file in your Ubuntu server (see detailed examples).

If you're using dhcp to set up the IP address of your servers (which I'd recommend), dhcp-client can use some hooks to run a script when it receives an IP address. That way you can dynamically change the hostname during the boot process. Here is an example on how to do it.

Another option, like suggested by pedro, is to set the MAC address of the server in each new XML file (which you can do, as it is a VM property) and have a script running at boot time that will assign an IP address and a hostname derived from this MAC address.

Related Topic