Skip to content

Instantly share code, notes, and snippets.

@andriytk
Last active September 2, 2024 22:31
Show Gist options
  • Save andriytk/bd3def8c30cbd474490280436c779027 to your computer and use it in GitHub Desktop.
Save andriytk/bd3def8c30cbd474490280436c779027 to your computer and use it in GitHub Desktop.
Configure NAT-network for QEMU on macOS Mojave

Configure NAT-network for QEMU on macOS Mojave

Prerequisites:

  1. Install qemu via MacPorts.
  2. Install tuntap via brew.

Steps:

  1. Add a bridge with some Thunderbolt interface via System Preferences -> Network -> Manage Virtual interfaces:

  1. Turn On Internet Sharing with the created bridge via System Preferences -> Sharing:

Now you are ready to start qemu, just add -nic tap option for the NAT-interface. After the VM is started, tapN interface will appear in the host. Add it to the bridge:

sudo ifconfig bridge0 addm tapN

For the convenience, you can create the following two scripts which qemu will run by default for tap network interfaces for each VM. The scripts add and remove the tapN-interface to/from the bridge. Otherwise, you will have to do this manually for each VM. Here are the scripts:

$ cat /opt/local/etc/qemu-ifup 
#!/bin/sh
#
TAPDEV="$1"
BRIDGEDEV="bridge0"
#
ifconfig $BRIDGEDEV addm $TAPDEV
$ cat /opt/local/etc/qemu-ifdown 
#!/bin/sh
#
TAPDEV="$1"
BRIDGEDEV="bridge0"
#
ifconfig $BRIDGEDEV deletem $TAPDEV

The scripts should be placed as shown above, at /opt/local/etc/.

Tested on macOS Mojave 10.14.6 and qemu-5.2.0_0+cocoa+target_arm+target_i386+target_x86_64+usb, qemu-6.1.0_0+cocoa+target_arm+target_i386+target_x86_64+usb with glib2-2.58.3_1+x11, libffi-3.3_1 ports.

UPD: it doesn't work with the latest (20 Oct 2021) version of glib2-2.62.6_2+x11. Looks like it stopped working since 11 Aug 2021, when glib2 was updated from 2.58 to 2.62 version in ports. tcpdump -i bridge0 shows DHCP requests and the replies, but tcpdump -i tap0 shows only DHCP requests without replies. Filed https://trac.macports.org/ticket/63650 about it.

@Kagami
Copy link

Kagami commented Nov 30, 2022

https://gitlab.com/qemu-project/qemu/-/issues/465
Just -nic vmnet-shared in QEMU 7.1

@jheysel-r7
Copy link

https://gitlab.com/qemu-project/qemu/-/issues/465 Just -nic vmnet-shared in QEMU 7.1

Thanks @Kagami this worked for me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment