How to Access Raspberry Pi Qemu Vm via Network

How to access Raspberry PI QEMU VM via network

The easier method - less flexible but you don't need to muck around so much

They way you have configured qemu appears to be the default host NAT network (aka. qemu -net nic -net user configuration)

By default, your host (your Mac in this case) will be found on 10.0.2.2 when accessed from your guest. From inside your guest you can connect to services on your host (your MAC) at 10.0.2.2. But this is using NAT, so you can't go back the other way as easily.

For example, if you decide to you want to connect to the SSH service and a web server running inside your guest, you will need to start qemu with modified options like the following:

qemu -net nic -net user,hostfwd=tcp::2222-:22,hostfwd=tcp::22280-:80

What this will do is route connections from port 22280 on your host (your MAC) to port 80 inside your qemu guest, (same for port 2222 --> port 22 in the guest)

Which means you can browse to http://localhost:22280 on your Mac, to get to the web server in your virtual Raspberry Pi, etc.

FTP and SMB will be more complicated to setup this way because of the different ports used and they way they are used, etc. although if you setup FTP to use PASV mode it wont be too much of a problem.

The more complicated method

This involves doing what you suggested, configuring the virtual machine quest to be able to get an IP address from your router. In this case, you need to make a bridge from your virtual machine onto your hosts network.

This requires a lot more setup than can be quickly explained here, but essentially, you need to assign your NIC to a vlan and add a tap interface, for example:

qemu -net nic,vlan=0 -net tap,ifname=tap0

This however requires more setup on the host (initially, manual, as you figure out your own situation, but then, scriptable) to create a bridge and tap interface - which usually requires root access beyond that needed to simply run qemu. A bit of Googling brings up a variety of methods to do this, because it varies more depending on your setup. (I found an example setup script here: https://gist.github.com/EmbeddedAndroid/6572715 )

  • Note - network MAC addresses, network card models, etc. and other qemu options omitted for clarity.

The SAMBA method

Note: I have only tried this under Linux

You can enable a samba server inside qemu:

qemu -smb /path/to/files

This creates a SMB share accessible from inside the guest at \10.0.2.4\qemu mapped from /path/to/files on the host.

How to access Raspberry PI QEMU VM via network

The easier method - less flexible but you don't need to muck around so much

They way you have configured qemu appears to be the default host NAT network (aka. qemu -net nic -net user configuration)

By default, your host (your Mac in this case) will be found on 10.0.2.2 when accessed from your guest. From inside your guest you can connect to services on your host (your MAC) at 10.0.2.2. But this is using NAT, so you can't go back the other way as easily.

For example, if you decide to you want to connect to the SSH service and a web server running inside your guest, you will need to start qemu with modified options like the following:

qemu -net nic -net user,hostfwd=tcp::2222-:22,hostfwd=tcp::22280-:80

What this will do is route connections from port 22280 on your host (your MAC) to port 80 inside your qemu guest, (same for port 2222 --> port 22 in the guest)

Which means you can browse to http://localhost:22280 on your Mac, to get to the web server in your virtual Raspberry Pi, etc.

FTP and SMB will be more complicated to setup this way because of the different ports used and they way they are used, etc. although if you setup FTP to use PASV mode it wont be too much of a problem.

The more complicated method

This involves doing what you suggested, configuring the virtual machine quest to be able to get an IP address from your router. In this case, you need to make a bridge from your virtual machine onto your hosts network.

This requires a lot more setup than can be quickly explained here, but essentially, you need to assign your NIC to a vlan and add a tap interface, for example:

qemu -net nic,vlan=0 -net tap,ifname=tap0

This however requires more setup on the host (initially, manual, as you figure out your own situation, but then, scriptable) to create a bridge and tap interface - which usually requires root access beyond that needed to simply run qemu. A bit of Googling brings up a variety of methods to do this, because it varies more depending on your setup. (I found an example setup script here: https://gist.github.com/EmbeddedAndroid/6572715 )

  • Note - network MAC addresses, network card models, etc. and other qemu options omitted for clarity.

The SAMBA method

Note: I have only tried this under Linux

You can enable a samba server inside qemu:

qemu -smb /path/to/files

This creates a SMB share accessible from inside the guest at \10.0.2.4\qemu mapped from /path/to/files on the host.

Trying to run Raspberry-Pi image under QEMU, but VM memory is limited to 256MB

The problem here is that a lot of people claim to be running "raspberry pi emulation in QEMU" when they're actually just running Raspbian userspace on top of a kernel for a different machine emulation. So it's easy to be confused if you look at several different tutorials that are really describing entirely different emulation setups. Look for what machine type they pass QEMU.

The "versatilepb" machine type gets used in a lot of tutorials, especially older ones, because it has been in QEMU a long time and it is possible to get it to work with the 1176 CPU that the classic Raspberry Pi boards used. This specific machine has a 256MB maximum memory size, because the real hardware it's emulating has that restriction (it's imposed by the way the physical memory address space is designed). This machine type will never be able to support more RAM, so if you need more then you should ignore any tutorial or setup that uses it.

More recent versions of QEMU really do emulate the actual raspberry pi hardware; these are the raspi0, raspi1ap, raspi2b, raspi3ap, raspi3b machine types. These will have the same amount of RAM as the real raspi hardware they're emulating (either 512MB or 1GB). The downside of these board models is that some of the device emulation is lacking features -- so older QEMU will often not correctly boot a newer kernel, and sometimes devices you would like to use are not present. Also, because the raspi boards hang their ethernet device off the USB controller, the only way to get ethernet on these QEMU models would also be to use a USB ethernet device, eg with:

-device usb-net,netdev=eth0 -netdev user,id=eth0

This probably needs a recent QEMU version to get a working USB controller.

I don't know if there are any tutorials/recipes for running Raspbian on top of the QEMU "virt" board. If there are, this would probably be the best experience, because the virt board permits lots of memory, PCI devices, virtio devices, and is well maintained.

qemu-aarch64 networking

qemu-aarch64 just runs a single guest Linux binary. Any system calls made by the guest binary (including those dealing with asking about what ethernet interfaces are present) are simply passed to the host; there is no support for changing the answers given.

Network interfaces are entirely renamable in Linux, so (a) a correctly written guest application should not care about what the exact names are and (b) if you are willing to deal with the upheaval you could rename them on the host. I wouldn't really recommend (b) except by running everything inside a VM for this purpose, though :-)



Related Topics



Leave a reply



Submit