Networking

xen 使用橋接網路創建新虛擬機

  • May 3, 2014

我正在使用 CentOS 6.5 64

使用本教程設置 Bridge

[root@CentOS ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
xenbr0          8000.08002754b3fa       yes             eth0

使用命令創建一個新的虛擬機(CentOS)xm,一切正常

Xen 配置文件

kernel = "/var/lib/xen/images/vmlinuz"
ramdisk = "/var/lib/xen/images/initrd.img"
extra = "text ks=http://192.168.1.8/kickstart-minimal6-x86_64.cfg"
name = "xc63"
memory = "512"
disk = [ "file:/var/lib/xen/images/centos63.img,xvda,w","file:/var/lib/xen/images/centos63.swp,xvdb,w"]
vif = [ 'bridge=xenbr0' ]
vcpus = 1
on_reboot = "destroy"
on_crash = "destroy"

創建後

[root@CentOS ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
xenbr0          8000.08002754b3fa       yes             eth0
                                                       vif6.0

[root@CentOS ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:54:B3:FA
         inet6 addr: fe80::a00:27ff:fe54:b3fa/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:3884 errors:0 dropped:0 overruns:0 frame:0
         TX packets:2832 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000
         RX bytes:310622 (303.3 KiB)  TX bytes:412047 (402.3 KiB)

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)

vif6.0    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF
         inet6 addr: fe80::fcff:ffff:feff:ffff/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:6 errors:0 dropped:0 overruns:0 frame:0
         TX packets:18 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:32
         RX bytes:928 (928.0 b)  TX bytes:2192 (2.1 KiB)

xenbr0    Link encap:Ethernet  HWaddr 08:00:27:54:B3:FA
         inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
         inet6 addr: fe80::a00:27ff:fe54:b3fa/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:2935 errors:0 dropped:0 overruns:0 frame:0
         TX packets:1403 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:188677 (184.2 KiB)  TX bytes:271882 (265.5 KiB)

xm 控制台 xc63

在此處輸入圖像描述 這意味著我的網橋無法正常工作,因為 vm 無法呼叫192.168.1.8

獲取 MAC 地址xc63

[root@CentOS xen]# xl network-list xc63
Idx BE Mac Addr.         handle state evt-ch   tx-/rx-ring-ref BE-path
0   0  00:16:3e:21:f4:e1     0     4     10   768/769         /local/domain/0/backend/vif/3/0

並檢查 DHCP 狀態 在此處輸入圖像描述

xc63獲取ip192.168.1.118

ping

從同一個主機它工作

[root@CentOS xen]# ping 192.168.1.118
PING 192.168.1.118 (192.168.1.118) 56(84) bytes of data.
64 bytes from 192.168.1.118: icmp_seq=1 ttl=64 time=2.28 ms
64 bytes from 192.168.1.118: icmp_seq=2 ttl=64 time=0.907 ms
64 bytes from 192.168.1.118: icmp_seq=3 ttl=64 time=0.724 ms
64 bytes from 192.168.1.118: icmp_seq=4 ttl=64 time=0.809 ms
^C
--- 192.168.1.118 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3171ms
rtt min/avg/max/mdev = 0.724/1.181/2.284/0.640 ms

從本地網路上的任何設備Request timed out

C:\Users\motaz>ping 192.168.1.118

Pinging 192.168.1.118 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.168.1.118:
   Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

誰能給我一個解決這個問題的想法,我將不勝感激。

首先,如果您使用 VirtualBox 來託管 XEN 伺服器,請確保使用乙太網而不是無線網路,並將混雜模式設置為“全部允許”。

其次,為了讓一切都變得乾淨,讓我們從使用 XEN 的 CentOS 的干淨安裝開始,並在其上安裝 Bridge Network 和 CentOS VM。

假設您有外部伺服器 192.168.1.6,在 /var/www/html/centos/6.3/os/i386/ 上提取了 CentOS ISO,在 /var/www/html/centos/6.3/os/i386/ks.cfg 上提取了 kickstart 文件和 /var/www/html/centos/6.3/os/i386/repodata 與 repodata/TRANS.TBL 文件中的名稱匹配

在 XEN 伺服器 (CentOS+XEN) 上安裝以下軟體包:

yum install -y rsync wget vim-enhanced openssh-clients
yum install -y libvirt python-virtinst libvirt-daemon-xen
yum install -y bridge-utils tunctl

然後編輯 ifcfg-* 文件來創建網橋,在 CentOS 中不需要 NM_CONTROLLED=no:

echo "DEVICE=br0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes" > /etc/sysconfig/network-scripts/ifcfg-br0

echo "DEVICE=eth0
HWADDR=XX:XX:XX:XX:XX:XX
ONBOOT=yes
TYPE=Ethernet
IPV6INIT=no
USERCTL=no
BRIDGE=br0" >  /etc/sysconfig/network-scripts/ifcfg-eth0

編輯HWADDR=XX:XX:XX:XX:XX:XX行以匹配您的 MAC 地址。不要在 ssh 控制台上重新啟動,使用 VBox 控制台

reboot

重啟後,假設您有 DHCP 伺服器 XEN 伺服器將獲得一個新 IP,通過 VBox 控制台登錄以獲取新 IP

ifconfig 結果應該類似於

br0       Link encap:Ethernet  HWaddr 08:00:27:23:54:69  
         inet addr:192.168.1.105  Bcast:192.168.1.255  Mask:255.255.255.0
         inet6 addr: fe80::a00:27ff:fe23:5469/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:5063 errors:0 dropped:0 overruns:0 frame:0
         TX packets:3142 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:34251267 (32.6 MiB)  TX bytes:361205 (352.7 KiB)

eth0      Link encap:Ethernet  HWaddr 08:00:27:23:54:69  
         inet6 addr: fe80::a00:27ff:fe23:5469/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:149910 errors:0 dropped:0 overruns:0 frame:0
         TX packets:5045 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:182020057 (173.5 MiB)  TX bytes:493792 (482.2 KiB)

現在網橋已準備就緒,您可以使用 br0 的 IP 再次獲取 ssh 控制台

要在 XEN 上創建使用先前網橋的虛擬機:

cd /var/lib/xen/images/

創建虛擬磁碟:

dd if=/dev/zero of=centos_1.img bs=4K count=0 seek=1024K
qemu-img create -f raw centos_1.img 8G 

然後使用 virt-install 創建虛擬機:

virt-install -d -n TestVM1 -r 512 --vcpus=1 \
--bridge=br0 --disk /var/lib/xen/images/centos_1.img \
--nographics -p -l "http://192.168.1.6/centos/6.3/os/i386" \
--extra-args="text console=com1 utf8 console=hvc0 ks=http://192.168.1.6/centos/6.3/os/i386/ks.cfg"

現在虛擬機應該可以正常啟動並能夠從 DHCP 伺服器獲取 IP 並能夠完成無人值守的遠端安裝。

XEN 上的 ifconfig 結果應該類似於:

br0       Link encap:Ethernet  HWaddr 08:00:27:23:54:69  
         inet addr:192.168.1.105  Bcast:192.168.1.255  Mask:255.255.255.0
         inet6 addr: fe80::a00:27ff:fe23:5469/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:10247 errors:0 dropped:0 overruns:0 frame:0
         TX packets:8090 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:102264338 (97.5 MiB)  TX bytes:827859 (808.4 KiB)

eth0      Link encap:Ethernet  HWaddr 08:00:27:23:54:69  
         inet6 addr: fe80::a00:27ff:fe23:5469/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:998780 errors:0 dropped:0 overruns:0 frame:0
         TX packets:37992 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:724701715 (691.1 MiB)  TX bytes:2897912 (2.7 MiB)


vif5.0    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF  
         inet6 addr: fe80::fcff:ffff:feff:ffff/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:37 errors:0 dropped:0 overruns:0 frame:0
         TX packets:67 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:32 
         RX bytes:4381 (4.2 KiB)  TX bytes:9842 (9.6 KiB)

安裝完成後,你可以使用xen控制台獲取它的IP,然後你就可以在上面安裝ssh控制台了。

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