quickemu-project / quickemu

Quickly create and run optimised Windows, macOS and Linux virtual machines
MIT License
10.82k stars 476 forks source link

Document How to Specify Unique Persistent MAC addresses for VMs using Bridge #287

Closed erwin closed 2 years ago

erwin commented 2 years ago

Persistent MACs may be out of scope for the project, so I at least wanted to share some simple instructions in case anyone else wants to do this:

While network bridging is a great feature it is out of scope for quickemu, which is designed to be a means of taking your VMs with you on USB drive and just work on any Linux without configuration

First off, both ./quickemu and ./quickget and bash shell scripts. They're very clearly written, so if you want to make any of your own changes, it should be quite easy.

When you're using a bridged network interface to connect your VM to your network, each VM must have it's own MAC address. If you don't explicitly set this MAC address, you'll have multiple VMs sending the same MAC, and you'll be loosing packets when you run two VMs at the same time (that will try to share both the MAC address, and then the IP).

You really just need to edit one line to add mac (for MAC address) when using a bridged interface.

diff --git a/quickemu b/quickemu
index 5507580..55fb23a 100755
--- a/quickemu
+++ b/quickemu
@@ -871,7 +871,7 @@ function vm_boot() {
   if [ -n "${bridge}" ]; then
     # Enable bridge mode networking
     # shellcheck disable=SC2054,SC2206
-    args+=(-nic bridge,br=${bridge},model=virtio-net-pci)
+    args+=(-nic bridge,br=${bridge},model=virtio-net-pci,mac=${macaddr})
   else
     # shellcheck disable=SC2054,SC2206
     args+=(-device ${NET_DEVICE},netdev=nic -netdev ${NET},id=nic)
@@ -1027,6 +1027,7 @@ function display_param_check() {
 # Lowercase variables are used in the VM config file only
 boot="efi"
 bridge=""
+macaddr=""
 cpu_cores=""
 disk_img=""
 disk_size=""

Then in your .conf file for each VM, you'll want to specify both the bridge and macaddr.

guest_os="freebsd"
disk_img="freebsd-13.0/disk.qcow2"
iso="freebsd-13.0/FreeBSD-13.0-RELEASE-amd64-dvd1.iso"
bridge="br0"
macaddr="52:54:00:AB:51:AE"

Note that qemu requires that the Mac address is in the range: 52:54:00:AB:00:00 - 52:54:00:AB:FF:FF, so you can generate your own randomish MAC addresses with:

printf '52:54:00:AB:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256))

That's basically all that's required ONCE YOU'VE ALREADY GOT THE BRIDGE WORKING... These notes are ONLY about helping once you've already got the bridge working. To get the bridge itself up, if your system supports netplan, assuming your primary network interface is eno1, you would want to create /etc/netplan/01-netplan.yaml

---
network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: false
      dhcp6: false
    wlp4s0:
      dhcp4: false
      dhcp6: false

  bridges:
    br0:
      interfaces: [eno1]
      addresses: [192.168.0.10/24]
      routes:
        - to: default
          via: 192.168.0.1
      mtu: 1500
      nameservers:
        addresses: [192.168.0.1]
      parameters:
        stp: false
        forward-delay: 0
      dhcp4: false
      dhcp6: false

Then run:

sudo netplan generate
sudo netplan --debug apply

Best of luck.

github-actions[bot] commented 2 years ago

Hello there 👋 Thanks for submitting your first issue to the Quickemu project 🐛 We'll try and take a look at your issue soon ⏲

In the meantime you might want to join the Wimpys World Discord 🗣 where we have a large community of Linux 🐧 enthusiasts and passionate open source developers 🧑‍💻

You might also be interested in following Wimpys World Twitch 📡 channel where Wimpy streams let's code video, including this project, several times a week. A back catalog of past live stream and other Linux related content is available on Wimpys World YouTube 📺 channel.

ChrisMacGregor commented 2 years ago

Thanks, @erwin, this was helpful and the patch works nicely. However, I haven't quite figured out how to set up br0 (on Ubuntu 21.10) so that it actually works. Googling turned up a bunch of ways of doing it on various distros and/or in conjunction with other ways of running VMs, but it was not at all clear which method(s) were applicable in this case (qemu or quickemu on Ubuntu 21.10); I tried a number of things, but the closest I got had the VM getting an IPv6 address but not IPv4, and meanwhile br0 somehow became the preferred interface when I tried to ping or ssh to the physical host (definitely not what I wanted).

You posted the netplan info, but I don't want to do something that will interfere with the regular (systemd + NetworkManager) networking stuff that Ubuntu 21.10 provides by default; I'm trying to keep this machine as close as possible to a normal config, so I don't miss issues that my clients (on machines not configured for development) might encounter. Any tips? For instance, I was able to create a bridge (br0) easily enough, but I can't figure out what if any interfaces I should create (e.g. tun/tap) and/or attach to br0 (e.g., eth0 or equivalent), vs. what /usr/lib/qemu/qemu-bridge-helper will do automatically.

erwin commented 2 years ago

I configured bridged networking several times on CentOS/RHEL and it was very straightforward, I even scripted it for a set of virtual servers for my own mini cloud, however getting it to work on Ubuntu was a major struggle for me.

If there is one major thing to be aware of though, its that Docker (which I love almost as much as snaps...) will also mess up your networking in ways that conflict with bridging. I would completely purge docker, reboot, make sure everything is updated, then follow these instructions:

https://www.cyberciti.biz/faq/ubuntu-20-04-add-network-bridge-br0-with-nmcli-command/

After you've got it working, there's a way to get docker working to, but you have to change the way it mangles your iptables.

Personally between ever more snaps forced on me, and the networking issues, I'm definitely not an Ubuntu fan...

flexiondotorg commented 2 years ago

The extra_args (added via https://github.com/quickemu-project/quickemu/pull/280) can be used to specify a persistent MAC address.

@erwin Please consider submitting a pull request that adds a revised version of the information presented here to the Bridged Networking section of the README.md

lwbt commented 2 years ago

I'd like to get this to work using extra_args instead of modifying quickemu, but I'm obviously doing it wrong and can't figure it out after trying for over an hour and having to many tabs open in a browser session that was not supposed to have so many tabs.

extra_args="-net nic,macaddr=52:54:00:AB:7E:94" is not the way to do it, as it results in invalid option. How do I do it right? I'd love to make my first contribution, but this is quite hard.

Setting up a bridge network with netplan seems to have a few pitfalls too. I did not expect that coming from VirtualBox and so far enjoying Quickemu a lot.

erwin commented 2 years ago

You should check the value of ${SHELL_ARGS} that's echo'ed right before ${QEMU} runs...

By adding -net nic,macaddr=XX:XX:XX in extra_args my bet is that you're sending the -nic argument to qemu twice.

If it still doesn't work, maybe you can post the the arguments that the script sends to Qemu...

jbarlow83 commented 2 years ago

It needs extra_args=-net nic,macaddr=52:54:00:AB:7E:94 to proceed from the current error message to the next error message. The quotes in extra_args produce the wrong command line.

The next error message is

failed to parse default acl file `/etc/qemu/bridge.conf'
qemu-system-x86_64: -nic bridge,br=br0,model=virtio-net-pci: bridge helper failed

I decided at this point that qemu is resolutely determined to prevent the use of bridged networking at all costs and gave up. However, I am happy to provide this next piece of information in case someone else some ideas about how to compel qemu to be reasonable.

erwin commented 2 years ago

@flexiondotorg my apologies for not submitting the documentation for extra_args as asked. You know how it goes - get busy and forget...

I came back across this issue a few weeks ago, and finally dove in ready to make the asked changes only to find that you've actually added a macaddr config directly into quickemu. So cool!

https://github.com/quickemu-project/quickemu/commit/2e15b1219fc64ecd6256fd9567066712d8deb1e2

My apologies for being so slow.

I use quickemu every day. It rocks!