Skip to content

Instantly share code, notes, and snippets.

@andriytk
Last active February 2, 2024 08:44
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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.

@fgenoese
Copy link

Thanks for documenting this @andriytk
Could you add the full qemu command you are running?

@andriytk
Copy link
Author

sudo qemu-system-x86_64 -accel hvf -m 4G -smp 4 -hda centos-disk1.vmdk -nographic -nic tap

@fgenoese
Copy link

fgenoese commented Dec 25, 2021

Bummer! Thought there was more magic behind. Cannot seem to get this working for a Raspberry P. Emulation boots but has no network connection.

error:

qemu-system-aarch64: warning: netdev #net088 has no peer
qemu-system-aarch64: warning: requested NIC (anonymous, model unspecified) was not created (not supported by this machine?)

using the following qemu command:

sudo qemu-system-aarch64 
    -m 1G \
    -M raspi3b \
    -smp 4 \
    -usb \
    -device usb-mouse \
    -device usb-kbd \
    -device 'usb-net,netdev=net0' \
    -netdev 'user,id=net0,hostfwd=tcp::5022-:22' \
    -drive "file=2020-08-20-raspios-buster-armhf-lite.img,index=0,format=raw" \
    -dtb bcm2710-rpi-3-b-plus.dtb \
    -kernel kernel8.img \
    -append 'rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1' \
    -no-reboot \
    -nographic \
    -nic tap

@kx1t
Copy link

kx1t commented Apr 11, 2022

tuntap is not supported anymore -- any alternatives? (Trying to get this to run on Big Sur)
thanks!

@andriytk
Copy link
Author

Oh, that's pity...
Sorry, in this case, the only option probably would be to access your vm via ssh forwarding:

  1. qemu option -nic hostfwd=tcp:127.0.0.1:22222-:22
  2. ssh -p 22222 127.0.0.1.

@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