Specify the Virtual NIC Name for KVM Bridged VM’s

When working with KVM bridged interfaces, KVM will automatically name the virtual NIC that is spawned when the VM is started. This typically follows a naming convention of:

vnet0, vnet1, vnet2, ..., vnetN

The virtual NIC names are dynamically applied to each VM instance.  Thus, a spawned VM is not guaranteed to receive the same virtual NIC when it is restarted.  Generally speaking, this may not be a problem.  However, what if you *need* to have a script, or some function whereby it is important to know which virtual NIC is allocated to a specific VM?  There are ways of scripting this, but to avoid the headaches of scripting, it may be helpful to just specify a fixed, hard-coded name on the generated virtual NIC of the VM.  To do this, you must use the virsh command line utility.

To implement this, follow the steps below as a user that has rights to use the virsh command:

  1. Run the command: virsh
  2. At the virsh console, you need to type the command: edit <domain/VM Name> (substitute the name of your VM in here)
  3. This will open up a vi like interface to edit the XML entries for your VM. NOTE: I am making the assumption that you are using a standard bridged setup. I have not tested this with non-bridged setups, and especially not on libvirt managed bridged setups. Thus, your mileage may vary.
  4. Locate the XML entry for your network setup. It should look something like this:
    <interface type='bridge'>
          <mac address='00:11:22:33:44:55'/>
          <source bridge='br0'/>
          <model type='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
        </interface>
    

    You need to add a line to the interface tag that looks like this:
    <target dev='the_name_of_your_nic'/>

    Note: the name of the NIC needs to be a valid interface name. All lowercase and underscores work. As an example, I named my VM’s virtual NIC’s to something like this:
    vm1_net, vm2_net, vm3_net, ..., vmN_net

  5. Once it’s entered, it should look something like this:
    <interface type='bridge'>
          <mac address='00:11:22:33:44:55'/>
          <source bridge='br0'/>
          <target dev='vm1_net'/>
          <model type='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
        </interface>
    
  6. Save the changes and start the VM.

Once everything is set, you should see something like this if you use the ifconfig command:

vm1_net   Link encap:Ethernet  HWaddr 00:11:22:33:44:55  
          inet6 addr: fe80::fc54:ff:fec7:11/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500 
          RX bytes:468 (468.0 b)  TX bytes:468 (468.0 b)

vm2_net   Link encap:Ethernet  HWaddr 00:11:22:33:44:56  
          inet6 addr: fe80::fc54:ff:fec7:22/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500 
          RX bytes:0 (0.0 b)  TX bytes:468 (468.0 b)

This guarantees that the VM will always start with the virtual NIC name that you specify. In my case, I have VM1 using vm1_net, and VM2 using vm2_net.

Copyright © /sarc All Rights Reserved · Using modified version of Green Hope Theme by Sivan & schiy · Proudly powered by WordPress