Centos

qemu-kvm/virsh:使用橋接網路時沒有網路連接

  • November 22, 2011

我在通過 libvirt/qemu-kvm 執行虛擬化時遇到問題,其中我的橋接網路設置似乎無法正常工作。我已經按照我能找到的每一個教程,並在論壇上花費了數小時和數小時,但目前無濟於事。

主機和訪客都在使用 Centos6。

這是主機的設置:

[root@node2 ~]# ifconfig -a
bridge0   Link encap:Ethernet  HWaddr BC:30:5B:E8:C0:4F  
         inet addr:10.59.190.253  Bcast:10.59.190.255  Mask:255.255.255.0
         inet6 addr: fe80::be30:5bff:fee8:c04f/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:5315 errors:0 dropped:0 overruns:0 frame:0
         TX packets:3350 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:316961 (309.5 KiB)  TX bytes:3943630 (3.7 MiB)

eth0      Link encap:Ethernet  HWaddr BC:30:5B:E8:C0:4F  
         inet6 addr: fe80::be30:5bff:fee8:c04f/64 Scope:Link
         UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
         RX packets:5286 errors:0 dropped:0 overruns:0 frame:0
         TX packets:5325 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:409939 (400.3 KiB)  TX bytes:4095006 (3.9 MiB)
         Interrupt:16 Memory:c0000000-c0012800 

eth1      Link encap:Ethernet  HWaddr BC:30:5B:E8:C0:50  
         UP BROADCAST MULTICAST  MTU:1500  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:1000 
         RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
         Interrupt:17 Memory:c2000000-c2012800 

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:16436  Metric:1
         RX packets:28 errors:0 dropped:0 overruns:0 frame:0
         TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:2832 (2.7 KiB)  TX bytes:2832 (2.7 KiB)

sit0      Link encap:IPv6-in-IPv4  
         NOARP  MTU:1480  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)

vnet0     Link encap:Ethernet  HWaddr FE:54:00:3A:C5:C9  
         inet6 addr: fe80::fc54:ff:fe3a:c5c9/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
         TX packets:492 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:500 
         RX bytes:0 (0.0 b)  TX bytes:29516 (28.8 KiB)

我有兩個物理適配器,eth0 和 eth1。eth1 目前已拔出,未在此設置中使用。eth0 被橋接到 bridge0。bridge0 正在使用 DHCP 獲取其網路配置。適配器使用以下配置:

[root@node2 network-scripts]# cat ifcfg-eth0
DEVICE="eth0"
NM_CONTROLLED="no"
ONBOOT=yes
HWADDR=BC:30:5B:E8:C0:4F
BRIDGE=bridge0
[root@node2 network-scripts]# cat ifcfg-bridge0
DEVICE=bridge0
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=dhcp
DELAY=0
NM_CONTROLLED=no

…和 ​​iptables 設置如下:

[root@node2 network-scripts]# iptables --list-rules
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -p icmp -j ACCEPT 
-A INPUT -i lo -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5900 -j ACCEPT 
-A INPUT -j REJECT --reject-with icmp-host-prohibited 
-A FORWARD -m physdev --physdev-is-bridged -j ACCEPT 
-A FORWARD -m physdev --physdev-is-bridged -j ACCEPT 
-A FORWARD -j REJECT --reject-with icmp-host-prohibited

所以,就是這樣安排的。然後我使用以下命令創建和安裝 Centos6 來賓映像:

virt-install --name=centos-bridge-test --disk path=/var/lib/libvirt/images/centos6-minimal,size=5 --vnc --ram=512 --cdrom=/var/lib/libvirt/images/CentOS-6.0-x86_64-minimal.iso --vnclisten=0.0.0.0 -b bridge0  

我的理解是,這種設置應該足以讓橋接網路“正常工作”,但是,當我使用virsh命令(virsh start centos-bridge-test)啟動來賓時,它沒有網路連接。

這是 libvirt 域的 xml 轉儲:

[root@node2 network-scripts]# virsh dumpxml centos-bridge-test
<domain type='kvm' id='7'>
 <name>centos-bridge-test</name>
 <uuid>5d611267-2feb-c6f7-7a48-29f9695a4a75</uuid>
 <memory>524288</memory>
 <currentMemory>524288</currentMemory>
 <vcpu>1</vcpu>
 <os>
   <type arch='x86_64' machine='rhel6.0.0'>hvm</type>
   <boot dev='hd'/>
 </os>
 <features>
   <acpi/>
   <apic/>
   <pae/>
 </features>
 <clock offset='utc'/>
 <on_poweroff>destroy</on_poweroff>
 <on_reboot>restart</on_reboot>
 <on_crash>restart</on_crash>
 <devices>
   <emulator>/usr/libexec/qemu-kvm</emulator>
   <disk type='file' device='disk'>
     <driver name='qemu' type='raw' cache='none'/>
     <source file='/var/lib/libvirt/images/centos6-minimal'/>
     <target dev='hda' bus='ide'/>
     <alias name='ide0-0-0'/>
     <address type='drive' controller='0' bus='0' unit='0'/>
   </disk>
   <disk type='block' device='cdrom'>
     <driver name='qemu' type='raw'/>
     <target dev='hdc' bus='ide'/>
     <readonly/>
     <alias name='ide0-1-0'/>
     <address type='drive' controller='0' bus='1' unit='0'/>
   </disk>
   <controller type='ide' index='0'>
     <alias name='ide0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
   </controller>
   <interface type='bridge'>
     <mac address='52:54:00:3a:c5:c9'/>
     <source bridge='bridge0'/>
     <target dev='vnet0'/>
     <alias name='net0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
   </interface>
   <serial type='pty'>
     <source path='/dev/pts/1'/>
     <target port='0'/>
     <alias name='serial0'/>
   </serial>
   <console type='pty' tty='/dev/pts/1'>
     <source path='/dev/pts/1'/>
     <target port='0'/>
     <alias name='serial0'/>
   </console>
   <input type='mouse' bus='ps2'/>
   <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-gb'/>
   <video>
     <model type='cirrus' vram='9216' heads='1'/>
     <alias name='video0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
   </video>
   <memballoon model='virtio'>
     <alias name='balloon0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
   </memballoon>
 </devices>
 <seclabel type='dynamic' model='selinux'>
   <label>system_u:system_r:svirt_t:s0:c384,c608</label>
   <imagelabel>system_u:object_r:svirt_image_t:s0:c384,c608</imagelabel>
 </seclabel>
</domain>

界面似乎設置正確(通過查看 libvirt 文件)。

因此,每當我啟動來賓時,都沒有網路連接。

[root@localhost ~]# ifconfig -a
eth0       Link encap:Ehternet  HWaddr 52:54:00:3A:C5:C9
          BROADCAST MULTICAST  MTU:1500  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:1000 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:10 Base address:0x6000

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:16436  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)

**編輯:**請注意 eth0 不在此處,因此我一直在執行ifup eth0以使適配器啟動,然後提供以下輸出:

[root@localhost ~]# ifconfig -a
eth0       Link encap:Ehternet  HWaddr 52:54:00:3A:C5:C9
          inet6 addr: fe80::5054:ff:fe3a:c5c9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets65 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3896 (3.8 KiB)  TX bytes:398 (398.0 b)

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:16436  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)

…當我嘗試訪問網路以獲取內部或外部地址時,我得到了這個:

[root@localhost ~]# ping 10.59.190.253
connect: Network is unreachable
[root@localhost ~]# ping www.google.com
ping: unknown host www.google.com
[root@localhost ~]# ping 173.194.66.99
connect: Network is unreachable

**附加資訊:**以下是對此問題的評論要求的一些附加資訊。這些命令是從目前執行來賓的主機上執行的:

[root@node2 network-scripts]# brctl show
bridge name         bridge id               STP enabled     interfaces
bridge0             8000.bc305be8c04f       no              eth0
                                                           vnet0
[root@node2 network-scripts]# ps -ef | grep qemu
qemu      1597     1  0 11:28 ?        00:00:25 /usr/libexec/qemu-kvm -S -M rhel6.0.0 -enable-kvm -m 512 -smp 1,sockets=1,cores=1,threads=1 -name centos-bridge-test -uuid 5d611267-2feb-c6f7-7a48-29f9695a4a75 -nodefconfig -nodefaults -chardev socket,id=monitor,path=/var/lib/libvirt/qemu/centos-bridge-test.monitor,server,nowait -mon chardev=monitor,mode=control -rtc base=utc -boot c -drive file=/var/lib/libvirt/images/centos6-minimal,if=none,id=drive-ide0-0-0,boot=on,format=raw,cache=none -device ide-drive,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive if=none,media=cdrom,id=drive-ide0-1-0,readonly=on,format=raw -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -netdev tap,fd=20,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=52:54:00:3a:c5:c9,bus=pci.0,addr=0x3 -chardev pty,id=serial0 -device isa-serial,chardev=serial0 -usb -vnc 0.0.0.0:0 -k en-gb -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x4
root      2815 32057  0 14:31 pts/0    00:00:00 grep qemu

我真的很感激此刻可能的任何幫助!如果您需要我提供任何進一步的配置等,請告訴我。

**編輯:**我使用的是 Centos6 的最小安裝,所以我沒有安裝任何視窗管理器,所以我試圖通過 virsh 和 virsh-install 來做所有事情,並且想盡量避免使用 virsh-manager。謝謝。

這個問題的答案是,因為我在來賓上完成了 Centos 6 minimum 的非圖形安裝,所以網路介面沒有自動設置為工作。我缺乏對虛擬化和 Centos 的了解,這意味著我認為這會為我解決,但我的這種假設是錯誤的。

修復:我需要進入來賓上的ifcfg-eth0並打開ONBOOT=yesBOOTPROTO=dhcp ,然後通過ifdown eth0然後ifup eth0重新啟動網路適配器。完成此操作後,客人收到了 IP 地址,一切都按預期工作。

引用自:https://serverfault.com/questions/333173