QEMU KVM Libvirt手冊(9): network

來源:互聯網
上載者:User

標籤:

虛擬網卡由-net nic定義

# qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntutest.img -vnc :19 -net nic

如果我們在monitor中查看info network

可以看到下面的

有時候,我們看到的是VLAN 0,這裡的VLAN和802.1.q一點關係都沒有,就是virtual hub的概念,在新的版本裡面已經改了。

我們可以在monitor裡面

set_link e1000.0 off

set_link e1000.0 on

來啟停網卡

-net nic [, vlan = n] [, macaddr = mac] [, model = type] [, Name = str] [, addr = str] [, vectors = v]

vlan參數表示串連在哪個virtual hub上

可以指定mac addr

model有多種

# qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntutest.img -vnc :19 -net nic,model=?
qemu: Supported NIC models: ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio

可以指定名字

addr是指在pci匯流排上的地址

我們可以添加多個網卡

# qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntutest.img -vnc :19 -net nic,vlan=3,model=ne2k_pci -net user,vlan=3 -net nic,vlan=2 -net user,vlan=2

可以建立使用者態的network

# qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntu-14.04.img -vnc :19 -net nic -net user

預設分配的IP是從10.0.2.15開始的

但是從外面無法ssh進去

Port-redirects from the host system (hostfwd)

# qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntu-14.04.img -vnc :19 -net nic -net user,hostfwd=tcp::12345-:22

會將在host機器上到連接埠12345的包轉寄給guest機器的22連接埠

我們可以從host上ssh

# ssh -p 12345 [email protected]
The authenticity of host ‘[localhost]:12345 ([127.0.0.1]:12345)‘ can‘t be established.
ECDSA key fingerprint is f2:ef:bc:8f:d8:bd:66:97:48:53:4d:0d:c1:d8:6d:c3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘[localhost]:12345‘ (ECDSA) to the list of known hosts.
[email protected]‘s password: 
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

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

  System information as of Sun Jul 13 14:12:34 EDT 2014

  System load: 0.0               Memory usage: 2%   Processes:       71
  Usage of /:  39.9% of 2.89GB   Swap usage:   0%   Users logged in: 0

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

74 packages can be updated.
32 updates are security updates.

Last login: Sun Jul 13 13:03:03 2014
[email protected]:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe12:3456/64 scope link 
       valid_lft forever preferred_lft forever

-net user, hostfwd = [tcp | udp]: [hostaddr]: host-port [guestaddr]: guest port

在monitor中也可以用下面的命令添加和刪除hostfwd

(Qemu) hostfwd_add [VLAN_ID name] [tcp | udp]: [hostaddr]: host-port [guestaddr]: guest port

(Qemu) hostfwd_remove [VLAN_ID name] [tcp | udp]: [hostaddr]: host port

Redirects from the guest system (guestfwd)

Forward guest TCP connections to the IP address server on port port to the character device dev. This option can be given multiple times.

# qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntu-14.04.img -vnc :19 -net nic -net user,guestfwd=tcp:10.0.2.1:80-stdio
GET /index.html HTTP/1.1
User-Agent: Wget/1.15 (linux-gnu)
Accept: */*
Host: 10.0.2.1
Connection: Keep-Alive

 

 

 

出現下面的輸出,是在guest裡面運行wget http://10.0.2.1/index.html的時候

在上面的網路設定中,我們發現,guest system和host system的網路是完全隔離的,如果需要通訊,則需要進行fwd功能,如果想全方位通訊,則比較困難。

於是有了TUN/TAP network interfaces

它會將guest system的網路和host system的網路連在一起。

通過TUN/TAP adapter,會產生一個在host system上的虛擬網卡tap,而tun建立了point to point的網路裝置,使得guest system的網卡和tap虛擬網卡成為一對,從而guest system的所有網路包,host system都能收到。

This driver basically allows a user-space application to obtain a file descriptor which is connected to a network device. Any frames sent to the kernel over the file descriptor will be received by the device and any frames transmitted via the device will be received by the application.

建立的過程如下

(1) 在Host機器上建立bridge br0

brctl addbr br0

(2) 將br0設為up

ip link set br0 up

(3) 建立tap device

# tunctl -b
tap0

(4) 將tap0設為up

ip link set tap0 up

(5) 將tap0加入到br0上

brctl addif br0 tap0

(6) 啟動虛擬機器

qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntu-14.04.img -boot c -vnc :19 -net nic,model=virtio -net tap,ifname=tap0,script=no,downscript=no

虛擬機器連線tap0,tap0串連br0

(7) 虛擬機器啟動後,網卡沒有配置,所以無法串連外網,先給br0設定一個ip

ifconfig br0 192.168.57.1/24

(8) 在虛擬機器裡面,給網卡設定地址

ifconfig eth0 192.168.57.2/24

這個時候在虛擬機器裡面可以ping的通192.168.57.1了,但是還是無法訪問外網

(9) 在Host上設定NAT,並且enable ip forwarding

# sysctl -p
net.ipv4.ip_forward = 1

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

(10) 在虛擬機器裡面設定預設閘道

route add –net default gw 192.168.57.1

這個時候,可以ping的通外網網關了

ping 16.158.164.1

(11) 然後在虛擬機器裡面設定dns,則可以進行apt-get

# cat /etc/resolv.conf 
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 16.110.135.52
nameserver 16.110.135.51

Multiple instances with sockets networking

通過-net socket參數,可以使得多個instance之間互連

TCP socket,一個監聽一個connect,兩者相連,相互連信

Host ~ $ qemu-system-x86_64 plate01.img -net nic,macaddr=52:54:00:12:34:56 -net socket, listen=:1234

Host ~ $ qemu-system-x86_64 plate02.img -net nic,macaddr=52:54:00:12:34:57 -net socket, connect = 127.0.0.1:1234

屬於不同vlan的instance也可以相互串連

UDP multicast socket

Host ~ $ qemu-system-x86_64 plate01.img -net nic,macaddr = 52:54:00:12:34:56 -net socket, mcast = 230.0.0.1:1234

Host ~ $ qemu-system-x86_64 plate02.img -net nic,macaddr = 52:54:00:12:34:57 -net socket, mcast = 230.0.0.1:1234

Host ~ $ qemu-system-x86_64 plate03.img -net nic,macaddr = 52:54:00:12:34:58 -net socket, mcast = 230.0.0.1:1234

同一個multicast group可以相互連信

Bridging and Switching

可以使用brctl (bridge-utils), 也可以使用openvswitch

netdev參數

-netdev [user | tap | vde | socket], id = str [, option] [, option] [, ...]

Network protocol analysis

-net dump [,vlan=n][,file=f][,len=n]

qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntu-14.04.img -vnc :19 -net nic -net user -net dump

會產生一個檔案

qemu-vlan0.pcap

需要用tcpdump查看

QEMU KVM Libvirt手冊(9): network

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.