[QEMU网络配置] 通过网桥使宿主机和虚拟机互通网络

更新3 另一种更为简单的方式(缺点是:局域网其他机器无法访问该虚拟机)

宿主需要有samba/配置好samba的用户名和共享目录
smb.conf

[share]
path=/home/work/Myproj/vms/share_dir
public=no
guest ok=yes
writable=yes
browseable=yes
valid users=samba1

宿主机操作

sudo useradd samba1
sudo passwd samba1
sudo smbpasswd -a samba1
测试下:
smbclient //127.0.0.1/share -U samba1%123

虚拟机启动脚本

qemu-system-x86_64 --enable-kvm -m 2048 -smp 2 -hda vm1.raw \
-netdev user,id=ndev.0,hostfwd=tcp::5555-:22 \
-device e1000,netdev=ndev.0 \
-smb /home/work/Myproj/vms/share_dir

宿主机访问该虚拟机
ssh 127.0.0.1 -p 5555

虚拟机访问该共享目录
smbclient -L //10.0.2.2 -U samba1%123

虚拟机挂载该目录
sudo mount -t cifs //10.0.2.2/share ~/mnt_host -o username=samba1,password=123

更新: 2个issue

    1. 宿主机缺失2个网卡脚本/etc/qemu-ifdown /etc/qemu-ifup,自己写个就行.
[work  ~  ]$cat /etc/qemu-ifup
#!/bin/bash
#This is a qemu-ifup script for bridging.
#You can use it when starting a KVM guest with bridge mode network.
#set your bridge name
switch=br0
if [ -n "$1" ]; then
    #create a TAP interface; qemu will handle it automatically.
    #tunctl -u $(whoami) -t $1
    #start up the TAP interface
    ip link set $1 up
    sleep 1
    #add TAP interface to the bridge
    brctl addif ${switch} $1
    exit 0
else
    echo "Error: no interface specified"
    exit 1
fi
[work  ~  ]$cat /etc/qemu-ifdown 
#! /bin/sh  
switch=br0  
brctl delif ${switch} $1
    1. 同一局域网多个虚拟机有可能产生MAC冲突,造成ssh没有响应.
      qemu启动脚本中修改MAC地址即可.
-net nic,macaddr=xx:xx:xx:xx:xx:xx

宿主机操作

  • 内核支持:
Device driver --> Network device support -->Universal TUN/TAP device driver support
  • 用户态软件:
apt-get install uml-utilities bridge-utils
  • 修改网络配置:增加tap0br0
$cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
+auto tap0
+iface tap0 inet manual
+up ifconfig $IFACE 0.0.0.0 up
+down ifconfig $IFACE down
+tunctl_user nmt
+auto br0
+iface br0 inet dhcp
+bridge_ports all tap0
  • 启动这俩网络设备
sudo /sbin/ifup tap0
sudo /sbin/ifup br0

*启动qemu

sudo qemu-system-x86_64 --enable-kvm -m 4G -smp 2 -hda ./VMs/vm1.raw -net nic -net tap

至于虚拟机的网路配置

(啥也不做,都是默认配置)

  • 链接成功
$ssh work@192.168.1.102
work@192.168.1.102's password: 
Welcome to Ubuntu 14.04.4 LTS (GNU/Linux 4.2.0-27-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Wed Dec 14 20:28:01 CST 2016

  System load:  0.0               Processes:           84
  Usage of /:   6.7% of 18.33GB   Users logged in:     1
  Memory usage: 1%                IP address for eth0: 192.168.1.102
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

New release '16.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Last login: Wed Dec 14 20:28:01 2016 from 192.168.1.176
work@vm1:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 52:54:00:12:34:56  
          inet addr:192.168.1.102  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fe12:3456/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:168 errors:0 dropped:0 overruns:0 frame:0
          TX packets:139 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:22868 (22.8 KB)  TX bytes:21206 (21.2 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

work@vm1:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp
    原文作者:Quasars
    原文地址: https://www.jianshu.com/p/50484dd0c0bb
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞