Vmware中的虛擬網路 ( by quqi99 )

quqi99發表於2013-03-27


Vmware中的虛擬網路 ( by quqi99 )

作者:張華  發表於:2013-03-27
版權宣告:可以任意轉載,轉載時請務必以超連結形式標明文章原始出處和作者資訊及本版權宣告

( http://blog.csdn.net/quqi99 )

           導讀:hyper-v需要安裝上支援虛擬化的物理機上,但是沒有物理機啊。怎麼辦呢?就是通過Vmware來虛擬,更難能可貴的是,虛出一虛機也能支援硬體虛擬化,這裡可以在Vmware的虛機裡來安裝hyper-v。更一方面,想做vlan方面的實驗,得把vmware的虛擬網路的原理搞明白。

1 安裝vmware workstation 12.1.0 for linux, 

    sudo apt-get install libcanberra-gtk-module:i386
    sudo ./VMware-Workstation-Full-12.1.0-3272444.x86_64.bundle
2 用vmware建立一個虛機用於安裝windows 2012 server, 給了16G的硬碟空間, 並在此啟用hyper-v角色。網路為vmware的NAT模式。
   2.1 因為是在虛擬機器上又虛擬化,所以確保vmware虛擬機器setting選項中的processors啟用了Virtualize intel VT-x/EPT or ADM-V/RVT和Virtualize cpu performance counters這兩個選項, 參見:http://hi.baidu.com/cjp19882009/item/a72f743636b42880f4e4ad6e  或http://communities.vmware.com/docs/DOC-8970/
   2.2 再修改配置檔案/run/media/hua/E/vmwareimage/WindowsServer2012/WindowsServer2012.vmx,新增下面兩行。
        hypervisor.cpuid.v0 = "FALSE"
        mce.enable = "TRUE"
   2.3 最後,關閉windows server 2012的防火牆。
   2.4 在hyper-v管理器中建立一網橋br-int, 它報錯:The virtual machine's operating system has attempted to enable promiscuous mode on adapter Ethernet0. This is not allowed for security reasons. Please go to the Web page "http://vmware.com/info?id=161" for help enabling promiscuous mode in the virtual machine.
       所以需要人工將網路卡vmnet8(NAT模式使用的是vmnet8)設定為混雜模式,因為vmnet8網路卡是由root使用者安裝的,而vmware workstation是通過普通使用者hua啟動的。對於不使用udev而直接用/dev的linux的改動如下:
        http://blog.martinshouse.com/2011/05/vmware-on-linux-promiscuous-mode.html
    [hua@zhanghua WindowsServer2012]$ sudo groupadd vmwaregroup
    [hua@zhanghua WindowsServer2012]$ sudo usermod -a -G vmwaregroup hua
    [hua@zhanghua WindowsServer2012]$ id hua
        uid=1000(hua) gid=1000(hua) groups=1000(hua),1001(libvirtd),1002(vmwaregroup)
    [hua@zhanghua WindowsServer2012]$ sudo chgrp vmwaregroup /dev/vmnet8
    [hua@zhanghua WindowsServer2012]$ sudo chmod g+rw /dev/vmnet8
    [hua@zhanghua WindowsServer2012]$ ll /dev/vmnet8
    crw-rw---- 1 root vmwaregroup 119, 8  1月 11 20:41 /dev/vmnet8
    [hua@zhanghua WindowsServer2012]$ sudo chmod a+rw /dev/vmnet8
    [hua@zhanghua WindowsServer2012]$ ll /dev/vmnet8
    crw-rw-rw- 1 root vmwaregroup 119, 8 Mar 20 09:49 /dev/vmnet8
        sudo service vmware restart

        上述方法在系統重啟後會消失,A more permanent fix is to edit /etc/init.d/vmware on the Host, by adding the lines in red:
      # Start the virtual ethernet kernel service
       vmwareStartVmnet() {
          vmwareLoadModule $vnet
          "$BINDIR"/vmware-networks --start >> $VNETLIB_LOG 2>&1
          chgrp vmwaregroup  /dev/vmnet*
          chmod a+rw /dev/vmnet*


        在上面設定了之後,當客戶機裡使用像wireshark之類的抓包工具時,它會將客戶機的網路卡設定為混雜模式,這時候vmware也會將vmnet8自動設定成混雜模式(ifconfig vmnet8 promisc)。

        下面驗證一下,netif5確實是IFF_PROMISC的了。netif5指核心的虛擬網路卡裝置, userif17是使用者態實現的nat裝置的字元裝置介面, hub8.x是網橋中的一個埠
    [root@zhanghua vmnet]# cat /proc/vmnet/hub8.0
    connected netif5 tx 23
    [root@zhanghua vmnet]# cat /proc/vmnet/netif5
    connected hub8.0 mac 00:50:56:c0:00:08 ladrf 00:00:00:00:00:00:00:00 flags IFF_RUNNING,IFF_UP,IFF_PROMISC devvmnet8
    [root@zhanghua vmnet]# cat /proc/vmnet/hub8.1
    connected userif17 tx 0
    [root@zhanghua vmnet]# cat /proc/vmnet/userif17
    connected hub8.1 mac 00:50:56:e3:d1:e0 ladrf 00:00:00:00:00:00:00:00 flags IFF_RUNNING,IFF_UP,IFF_BROADCAST read 0 written 0 queued 0 dropped.down 0 dropped.mismatch 20 dropped.overflow 0 dropped.largePacket 0
    [root@zhanghua vmnet]# cat /proc/vmnet/hub8.2
    connected userif18 tx 0
    [root@zhanghua vmnet]# cat /proc/vmnet/userif18
    connected hub8.2 mac 00:50:56:f6:3a:6b ladrf 00:00:00:00:00:00:00:00 flags IFF_RUNNING,IFF_UP,IFF_BROADCAST,IFF_ALLMULTI read 19 written 0 queued 19 dropped.down 0 dropped.mismatch 0 dropped.overflow 0 dropped.largePacket 0


        說明一下,vmware並沒有使用核心來實現nat, 因為ipforward=0, 它是由vmnet-natd來實轉發的。
        [root@zhanghua vmnet]# ps -ef|grep vmnet-natd
        root      9921     1  0 13:25 ?        00:00:00 /usr/bin/vmnet-natd -s 12 -m /etc/vmware/vmnet8/nat.mac -c /etc/vmware/vmnet8/nat/nat.conf
        例如,VM的IP為172.16.138.128,而/etc/vmware/vmnet8/nat/nat.conf檔案定義了NAT gateway address是172.16.138.2,也可定義DNAT規則例:8080=172.16.3.128:80
        [host]
    # NAT gateway address
    ip = 172.16.138.2
    [incomingtcp]
    #8080 = 172.16.3.128:80
        很顯然,vmware是natd與VM的虛擬網路卡程式進行TCP通訊時進行的NAT轉換,也就是從/dev/vmnet8 ( vmnet8 equals br-tun) 中讀出以太幀取出目的IP和協議,然後自己和遠端通訊

       下面是命令演示如何建立vmware橋:
           vmnet-bridge -n 4 -i eth2 -d /var/run/vmnet-bridge-4.pid -1vmnet4
           mknod /dev/vmnet4  c  119 4
           vmnet-netifup -d /var/run/vmnet-netifup-vmnet4.pid /dev/vmnet4 vmnet4
           ifconfig eth2 0.0.0.0 proimsc up
   
     虛機中使用橋,將以下命令列內容新增到vmx檔案:
           ethetnet0.connectionType = "custom"
           ethernet0.vnet = "vmnet4"

        這裡老外有個建議,說是最好用bridge,而不是nat, 如下:
      you'll be able to boot your Guest VM, and use Wireshark or whatever in the Guest.  Just Remember!   Your VM Guest's Network Adapter must be set to BRIDGED (connected directly to the physical network), not NAT (used to share the host's IP address).


    2.5 關於vmware中的vlan
        vmware中的vlan有三種方式, 可參見Vmware ESX Server 3 802.1Q解決方案:http://wenku.baidu.com/view/90b76687ec3a87c24028c4b9.html
        1, VGT 即在虛機裡就打了標籤, 將埠組的vlan_id屬性設為4095(相當於埠組就設為TRUCK了), 並且在虛擬機器中執行802.1Q VLAN trunking驅動。
        2,EST 在外部交換機打標籤, 預設行為,埠組的vlan_id屬性為0,相當於disable掉了埠組的tag功能 。
        3,VST 在vmware的虛擬交換機上打標籤, vmware用了埠組的概念,所以想定義一個vlan的話就要定義一個埠組,在埠組的vlan_id屬性設值(1-4094)。然後虛機和埠組關聯。
           To configure a VLAN on the portgroup using the VMware Infrastructure/vSphere Client:
        Click the ESXi/ESX host.
        Click the Configuration tab.
        Click the Networking link.
        Click Properties.
        Click the virtual switch / portgroups in the Ports tab and click Edit.
        Click the General tab.
        Assign a VLAN number in VLAN ID (optional).
        Click the NIC Teaming tab.
        From the Load Balancing dropdown, choose Route based on originating virtual port ID.
        Verify that there is at least one network adapter listed under Active Adapters.
        Verify the VST configuration using the ping command to confirm the connection between the ESXi/ESX host and the gateway interfaces and another host on the same VLAN.

        Note: For additional information on VLAN configuration of a VirtualSwitch (vSwitch) port group, see Configuring a VLAN on a portgroup (1003825).

    To configure via the command line:
        esxcfg-vswitch -p "portgroup_name" -v VLAN_ID virtual_switch_name
          參見文章:Sample configuration of virtual switch VLAN tagging (VST Mode) (1004074)
                   http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1004074


      所以想要把esx設定為trunk模式的話(VGT),需要做兩件事:

1)配置一個埠組, vlan_id關聯為4095, 步驟如上。

2)  這種VGT模式還需要特定的虛機網路卡驅動 ( 802.1Q VLAN trunking driver is required inside the virtual machine. )

     見: Sample configuration of virtual machine (VM) VLAN Tagging (VGT Mode) in ESX (1004252)

             http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1004252

3)將物理網路卡配置成混雜模式promisc, 步驟是:

  1. Log into the ESXi/ESX host or vCenter Server using the vSphere Client.
  2. Select the ESXi/ESX host in the inventory.
  3. Click the Configuration tab.
  4. In the Hardware section, click Networking.
  5. Click Properties of the virtual switch for which you want to enable promiscuous mode.
  6. Select the virtual switch or portgroup you wish to modify and click Edit.
  7. Click the Security tab.
  8. From the Promiscuous Mode dropdown menu, click Accept.

 2013-04-05加:

使用vmware的虛機來做vlan相關的網路實驗時,注意使用e1000網路卡,它預設用的是vmnet3是一個半虛擬化的網路卡對vlan支援不好。包括兩個層面,一是vmware處要設定虛機使用e1000網路卡,二是虛機內部要安裝e1000驅動(可用lspci |grep Eth命令檢視網路卡型別)。另外,使用VGT模式,即vmware的虛擬交換機要是trunk,埠組配置的vlan_id為4095



2013-04-26,總結一下,這種VMware上的虛機做控制節點,一臺物理機做計算節點的來做vlan實驗時,要保證:

在虛機裡面打tag (這樣虛機內部的網路卡驅動得支援vlan最好用e1000,並且開啟8021q模組 modprobe 8021q),這樣的話,也即hypervisor (VMware)的虛擬網橋與虛機相連的埠要支援TRUNK。


同理,如果是一個hyper-v上的虛機做控制節點,它自己這臺物理機做計算節點來做vlan實驗的話,原理同上。

1,hyper-v中給一個虛擬網路卡的所在虛擬交換機上的埠設定TRUNK,

      Get-VMNetworkAdapter  -VMName "scem1-hvsce_0415"
      Set-VMNetworkAdapterVlan -VMName "scem1-hvsce_0415" -Trunk -NativeVlanId 1 -AllowedVlanIdList 1-4094 -VMNetworkAdapterName "SCE_DATA_NIC2"

     見:HOWTO: Fully virtualized lab using Hyper-V 3.0 and GNS3 http://forum.gns3.net/topic5735.html

2, modprobe 8021q

3, 至於hyper-v中的網路卡驅動問題,好像無法安裝e1000,

    用命令(ethtool -i eth2 &&sudo modinfo hv_netvsc )你會發現hyper-v中的虛機使用的是windows自己的hv_netvsc網路卡驅動。

    這個網址說(http://stackoverflow.com/questions/14389722/hyper-v-network-adapter-drivers)hyper-v中的網路卡分為emulated和synthetic兩類,我的理解是前者是半虛擬化網路卡,後者才是正常網路卡。但要安裝正常網路卡,還需要安裝一個額外的驅動Integration Service, windows xp已經帶了這個驅動,但對於Linux需要自己下載的安裝,最新版本是3.4,下載地址: http://www.microsoft.com/en-gb/download/details.aspx?id=34603 , 要支援vlan得安裝這個驅動。ibm的一個網頁也是這麼說的,http://pic.dhe.ibm.com/infocenter/tivihelp/v48r1/index.jsp?topic=%2Fcom.ibm.scp.doc_2.1.0%2Finstalling%2Fr_limits_hyperv.html

相關文章