qemu与宿主机通过网桥链接通讯

网桥连接通讯

网上搜了一下,用以下方法尝试失败:
使用网桥方式,可以让qemu和host主机之间直接进行网络通信

  1. 安装网桥工具
    sudo apt install bridge-utilssudo apt install uml-utilities
  2. 新建一个网桥 sudo brctl addbr br0 网桥会在重启后消失
  3. 启用此网桥 sudo ip link set br0 up
  4. 确认/etc/qemu/bridge.conf中allow br0 (我的没有这个文件,创建一个)
  5. 给帮助程序权限sudo chmod u+s /usr/lib/qemu/qemu-bridge-helper
  6. qemu 启动时增加-netdev tap,helper=/usr/lib/qemu/qemu-bridge-helper,id=hn0 -device virtio-net-pci,netdev=hn0,id=nic1
  7. qemu 启动后会自动在host主机上新建一个tap0的网卡
  8. 使用brctl show查看br0和tap0已经关联上了
  9. 把host主机的一个网卡也和br0关联起来,主机wifi的网卡由于是dhcp获取的ip,无法与br0绑定,需要使用有线网卡绑定sudo brctl addif br0 enp5s0
bridge name	bridge id		STP enabled	interfaces
br0		8000.3860773ac46e	no		enp5s0
							tap0
  1. host设置各个网卡和网桥的ip,此处需要注意先设置br0的ip和tap0的ip,再设置host网卡的ip,否则guest里面无法ping外部主机的ip,最终使br0的mac和tap0的mac地址相同,具体原因还没来及查
sudo ifconfig br0 192.168.43.210 netmask 255.255.255.0
sudo ifconfig tap0 192.168.43.51 netmask 255.255.255.0
sudo ifconfig enp5s0 192.168.43.50 netmask 255.255.255.0
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.210  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::1429:b3ff:fe07:5f92  prefixlen 64  scopeid 0x20<link>
        ether fe:16:30:37:22:4f  txqueuelen 1000  (Ethernet)

tap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.51  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::fc16:30ff:fe37:224f  prefixlen 64  scopeid 0x20<link>
        ether fe:16:30:37:22:4f  txqueuelen 1000  (Ethernet)

enp5s0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.43.50  netmask 255.255.255.0  broadcast 192.168.43.255
        ether 38:xx:xx:xx:xx:xx  txqueuelen 1000  (Ethernet)
  1. guest设置eth0的ip 与br0的ip在一个网段内 例如 192.168.43.202
    qemu-bridge-helper使用/etc/qemu-ifup和/etc/qemu-ifdown来控制虚拟虚拟机网卡tap0启动

    如果想使用其他定义的网桥, /etc/qemu/bridge.conf中添加allow qemubr0
    qemu linux.img
    -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0 -device virtio-net-pci,netdev=hn0,id=nic1

尝试失败,可能是网卡绑定错了;原文链接 ->>.

我的另一种尝试:

另一种是有两个脚本实现,原理差不多,都是通过网桥通信的:

  1. 添加网桥br0
  2. 添加虚拟网卡tap100
  3. 给网桥配ip、网卡配全ip
  4. 绑定网卡和网桥
  5. 启用ipv4
    qemu_start.sh:
#!/bin/sh

flash="./flash"
rootfs="./rootfs.cpio"
liteos="./vmlinux"
qemu="qemu-system-mips"
config="./qemu-ifup"


#if [ ! -f "$flash" ];then
#    echo "There is no flash here"
#    exit
#fi

if [ ! -f "$rootfs" ];then
    echo "There is no rootfs.img here"
    exit
fi

if [ ! -f "$liteos" ];then
    echo "There is no liteos.bin here"
    exit
fi

if [ ! -f "$config" ];then
    echo "There is no qemu-ifup here"
    exit
fi

#if [ ! -f "$qemu" ];then
#    echo "There is no qemu-system-arm here"
#    exit
#fi

if [ ! -x "$config" ];then
    sudo chmod 777 $config
fi



./qemu-ifup
qemu-system-mips -M malta -cpu mips32r6-generic -initrd ./rootfs.cpio -kernel ./vmlinux -nographic -net nic,model=e1000 -net tap,ifname=tap100  -append 'console=ttyS0 root=/dev/ram oops=panic panic=1' -monitor /dev/null

qemu-ifup文件:

#!/bin/sh

brctl addbr br0

tunctl -t tap100

ifconfig tap100 0.0.0.0 up

ifconfig br0 192.168.1.1 up

brctl addif br0 tap100

echo 1 > /proc/sys/net/ipv4/ip_forward

执行./qemu_start.sh,qemu启动会提示

udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending discover
udhcpc: no lease, failing
FAIL

这个是dhcp自动分配IP失败,我们可以手动分配和网卡相同网段的ip

ifconfig eth0 192.168.1.2 up

此方法成功通信:

PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.591 ms
64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.256 ms
64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.245 ms
^C
--- 192.168.1.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2032ms
rtt min/avg/max/mdev = 0.245/0.364/0.591/0.160 ms

对比两种方法似乎就只差了echo 1 > /proc/sys/net/ipv4/ip_forward这个命令。验证后并不是,现就用脚本的方法吧。当前只是本机和qemu虚拟机通讯,qemu并不能访问网络。

gdbserver 问题

Remote debugging using 192.168.1.2:9000
warning: while parsing target description (at line 10): Target description specified unknown architecture "mips"
warning: Could not load XML target description; ignoring
Reading /root/ezcoder from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /root/ezcoder from remote target...
Reading symbols from target:/root/ezcoder...(no debugging symbols found)...done.
Reply contains invalid hex digit 59

显示本地gdb识别不了mips架构,原因应该是gdbserver和本地的架构不同,在本地安装一个支持mips的gdb,由于编译gdb源码出现很多问题,这里宿主机gdb用gdb-multiarch来设置各种平台架构,这里它支持184种架构

  1. 安装:apt-get install gdb-multiarch
  2. 使用命令gdb-multiarch filename -q启动GDB
  3. 首先使用命令set architecture [Arch-name]设置架构
  4. target remote localhost:[port] 链接远程端口
    gdb远没有在x86_64下好用,错误也是一堆,一些命令也不能用。
Exception occurred: context: Invalid option (CS_ERR_OPTION) (<class 'capstone.CsError'>)
For more info invoke `set exception-verbose on` and rerun the command
or debug it by yourself with `set exception-debugger on`

n/s命令不能用,到用户输入就执行不下去了。舍弃这种做法,选择高版本gdb,比如gdb版本>8.2的gdb编译 使用 --enable-tartgets=all支持所有架构,即可替代gdb-multiarch。它不能用的原因就是apt install gdb-multiarch的时候它自带的gdb是7.11版本,版本低会有bug。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值