How to access Raspberry PI QEMU VM via network

ε祈祈猫儿з 提交于 2020-01-01 03:50:12

问题


I have successfully setup a Raspberry PI VM on my Mac OS X via QEMU. Now I want to access the filesystem of this VM from my Mac.

When I call ifconfig on my VM I get this.

And here the content of my /etc/network/interfaces file

On my Mac

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
    ether 3c:07:54:65:da:50 
    inet6 fe80::3e07:54ff:fe65:da50%en0 prefixlen 64 scopeid 0x4 
    inet6 fdbf:a879:6730::3e07:54ff:fe65:da50 prefixlen 64 autoconf 
    inet6 fdbf:a879:6730::401e:56f5:f2f9:a236 prefixlen 64 autoconf temporary 
    inet 192.168.1.119 netmask 0xffffff00 broadcast 192.168.1.255
    nd6 options=1<PERFORMNUD>
    media: autoselect (1000baseT <full-duplex,flow-control>)
    status: active

I think the reason is, that the VM uses some kind of Shared Network. Is there a way that the VM get the IP address from my router?


回答1:


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.




回答2:


If you are happy to use SFTP/SCP then IMO the easiest thing to do is to install openssh-server on your Pi guest:

sudo apt-get update && sudo apt-get install openssh-server

When you launch your Pi VM use the -redir switch when your launch your QEMU guest to redirect port 22 (SSH/SFTP/SCP) e.g.

qemu <other-qemu-switches-options-etc> -redir tcp:2222::22

You can then connect to your VM SFTP/SCP (or SSH) via port 2222 on your host machine. So from your host you can use localhost; from other machines on your LAN you can use :2222

[update 2019] As noted by @ChristophBimminger in a comment, in newer versions of QEMU the -redir switch has been deprecated/removed. The way to achieve this same thing is now via the -net or -netdev switches. Please check the man page, e.g. Debian testing (I don't currently have a QEMU running so can't give a definitive example).



来源:https://stackoverflow.com/questions/23106012/how-to-access-raspberry-pi-qemu-vm-via-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!