0

I'm starting three Qemu VMs, each with several network interfaces. The network interface config looks like this:

-nic user,hostfwd=tcp::"${ssh_ports[$machine]}"-:22,model=virtio-net-pci \
-nic bridge,br="${bridge1}",model=virtio-net-pci
...

In all machines, I find that the first network device's MAC address is 52:54:00:12:34:56, the second ...:57, the third ...:58 and so on. This leads to a conflict of MAC addresses between my VMs. How can I let Qemu randomly assign the MAC addresses?

PS:

  • Not sure if this is relevant: I have two bridge interfaces, and each VM has one network interface in the first bridge, and one network interface in the second bridge.
  • I could of course use bash to generate some random number and then explicitly set it as a parameter. I would find it nicer if Qemu would just do that itself though.
  • A quick search revealed questions from people that wanted to reach the opposite: have Qemu assign some predetermined MAC. Those questions sounded like random assignment of MAC addresses is actually the default.

1 Answer 1

1

You don't want to do that. Using random MAC addresses will inevitably end up with MAC address conflicts - that's why you're seeing those search results.

Instead, you should assign a different hard-coded MAC address to each network interface on each VM using mac=macaddr with the -nic option. For example:

  • -nic user,...,mac=52:54:00:12:34:56 & -nic bridge,...,mac=52:54:00:12:34:57 with the first VM,
  • 52:54:00:12:34:58 & 52:54:00:12:34:59 with the second,
  • and 52:54:00:12:34:60 & 52:54:00:12:34:61 with the third.

Keep notes in a text file to record which MAC addresses have already been assigned and belong to which VMs.

Or just use libvirt, which handles MAC address assignment automatically. IMO, there's not much reason to run qemu manually rather than using libvirt - there's no VM customisation that you can do with qemu that you can't also do with libvirt....and any custom scripting you might write to automate it is just a poor imitation of what libvirt already does better (I know that from personal experience because I used to DIY hideously complicated qemu command lines with my own custom config files for years before I switched to using libvirt). libvirt is an API for automating VMs, with tools/libraries for shell, perl, python, and many other languages.


BTW, one extra benefit to hard-coding the MAC addresses is that (if you are using DHCP to assign IP addresses to each VM), you can configure your dhcp daemon to assign fixed IP addresses to each MAC address, so each VM can have a static IP and a reliable hostname (in either DNS or the /etc/hosts file).

It's not uncommon to run dnsmasq (or similar software - personally, I use bind9 and ISC dhcpd because I already use them for non- VM-related purposes) to provide combined dhcp & dns for VMs. In fact, that's a fairly standard setup when using libvirt as a wrapper around qemu, kvm, etc.

1
  • I think the probability of collisions is quite low in my scenario. My VMs are ephemeral, and there are only ever around 10 network devices within one bridge. Currently I use bash to assign only the last three bytes randomly, which still gives 16777216 addresses to choose from. Even if I get a collision, this is just test code, and I can just retry. For these circumstances I prefer to avoid the overhead of maintaining a list of assigned addresses. +1 for mentioning DHCP, in that case I would use some deterministic scheme for the mac addresses.
    – jan
    Oct 2, 2022 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.