Network communication issue with OpenWrt on Qemu

Hello guys,

I've been following the directions on this youtube video to install OpenWRT on Qemu. It works perfectly and I can access LuCI web interface normally. Now I would like to setup a development environment for LuCI using this guide here but the very first command "opkg update" fails.

I'm pretty sure I'm missing something very obvious here.
Sorry if this is a simple question, I'm brand new to this and I don't have much experience on it.

Thanks for all your time and patience.

Have you connected the second interface (WAN) to a network that has:

  1. DHCP; and
  2. is not

I usually execute OpenWrt in QEMU on my Desktop like that:

gunzip openwrt-x86-64-generic-squashfs-combined.img.gz
truncate -s 500M openwrt-x86-64-generic-squashfs-combined.img
sudo qemu-system-x86_64 -nographic -nic tap -hda openwrt-x86-64-generic-squashfs-combined.img

In another terminal for your desktop:

sudo ip link set dev tap0 up
sudo ip addr add dev tap0
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -I FORWARD -i tap0 -j ACCEPT
sudo iptables -I FORWARD -o tap0 -j ACCEPT
sudo iptables -t nat -I POSTROUTING -j MASQUERADING

In the booted OpenWrt console:

uci set network.lan.ipaddr=
uci set network.lan.gateway=
uci set network.lan.dns=
uci commit network
ifup lan

This should achieve internet connectivity in the OpenWrt QEMU instance.


Thanks for reaching out @lleachii
I believe I'm missing a second interface on my configuration. Check this screen capture.

How can I set this configuration right? I tried to add a second network card but I don't know how it should look like.

Hello @jow thank you for your help.
I tried to create a machine using your example and I have a couple of questions:
1 - What the difference between the image you're using ( openwrt-x86-64-generic-squashfs-combined.img) and the one that I have been using (openwrt-21.02.1-x86-64-generic-ext4-combined.img)?

2 - While configuring the network using your example, I'm getting an error at $ sudo iptables -t nat -I POSTROUTING -j MASQUERADING
All the rest goes fine, though. In the end I still can't ping anything or run a opkg update on openWRT.
Any other thoughts?

The squashfs one uses the rom filesystem + overlay approach you'll find on most OpenWrt supported devices, it can also be reset to its initial state. The ext4 image is fully writable and cannot be reset to defaults, afair it also requires manual repartitioning and ext4 resize to enlarge it while for the squashfs image it is enough to simply enlarge the image file (the truncate -s command above)

Yes, this was actually a typo since I wrote this from memory, it needs to be

$ sudo iptables -t nat -I POSTROUTING -j MASQUERADE

The various iptables and sysctl commands will essentially turn your desktop into a very simple NAT router which masquerades and forwards all traffic from/to tap0 (the virtual NIC of your QEMU OpenWrt). There are more sophisticated ways to set that up but for quickly launching QEMU instances this is close to the simplest and shortest solution possible.

1 Like

@jow All commands worked this time! used opkg update and worked fine!
For some reason my computer lost connection to the network but openWRT is still connected to the internet.
Any thoughts on why this is happening?