How to assign VM hostname by definition in libvirt virtual network interface

kvm-virtualizationlibvirtlinux-networking

Fedora 30 Workstation Host, Fedora 30 Server (netinstall) Guests.
I am not an expert in all this super-crazy Linux networking stuff and reading a ton of materials there and there.
So I have libvirt virtual network interface:

<network>
  <name>ocp-cluster</name>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr-ocpc' stp='on' delay='0'/>
  <mac address='52:54:00:2c:01:00'/>
  <domain name='ocp.domain.local' localOnly='no'/>
  <dns>
    <forwarder addr='192.168.130.10'/>
  </dns>
  <ip address='192.168.131.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.131.50' end='192.168.131.254'/>
      <host mac='52:54:00:2c:01:10' name='lb-1.ocp.domain.local' ip='192.168.131.10'/>
      <host mac='52:54:00:2c:01:11' name='bootstrap.ocp.domain.local' ip='192.168.131.11'/>
      <host mac='52:54:00:2c:01:12' name='master-1.ocp.domain.local' ip='192.168.131.12'/>
      <host mac='52:54:00:2c:01:13' name='worker-1.ocp.domain.local' ip='192.168.131.13'/>
    </dhcp>
  </ip>
</network>

but when i create new VMs and assign correct MAC address to network interface (manually or in kickstart --mac 52:54:00:2c:01:10 \), hostname assigned to that MAC address is no set for VM.
But if I understood right from libvirt documentation – it should be assigned.
Is it related to <dns><forwarder ardr...>?
In my case 192.168.130.10 is a address of DNSmasq VM which uses separate NAT bridge.
Sure, I can assign hostname in kickstart file network --hostname=lb-1.ocp.domain.local, but i want to understand, how this <host name...> thing works.
IP address for VM is assigned correctly.

Best Answer

The name attribute on the host members is not what you want. Confusingly, the right way to do this is to use the DNS section instead of the IP section. Adding the name attribute makes the match too restrictive (though I haven't dug into why exactly this happens), which is why it didn't apply to your machines. Try something like this instead:

<network>
  ...
  <dns>
    <host ip='192.168.131.10'>
      <hostname>lb-1.ocp.domain.local</hostname>
    </host>
    <host ip='192.168.131.11'>
      <hostname>bootstrap.ocp.domain.local</hostname>
    </host>
    <host ip='192.168.131.12'>
      <hostname>master-1.ocp.domain.local</hostname>
    </host>
    <host ip='192.168.131.13'>
      <hostname>worker-1.ocp.domain.local</hostname>
    </host>
  </dns>
  <ip address='192.168.131.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.131.50' end='192.168.131.254'/>
      <host mac='52:54:00:2c:01:10' ip='192.168.131.10'/>
      <host mac='52:54:00:2c:01:11' ip='192.168.131.11'/>
      <host mac='52:54:00:2c:01:12' ip='192.168.131.12'/>
      <host mac='52:54:00:2c:01:13' ip='192.168.131.13'/>
    </dhcp>
  </ip>
</network>

And since I can see that you are trying to install OpenShift, I'll also throw in that you don't need to do this setup manually. The installer supports libvirt - you just have to compile it from source with the right flag. This is covered in the docs (that link is to a specific commit in the history - future readers will want to look at the latest revision of those docs).