Centos6.5安裝kvm虛擬化

Centos6.5安裝kvm虛擬化

KVM簡介:
1.KVM是開源軟件,全稱是kernel-based virtual machine(基於內核的虛擬機)。
2.是x86架構且硬件支持虛擬化技術(如 intel VT 或 AMD-V)的Linux全虛擬化解決方案。
3.KVM能在不改變linux或windows鏡像的情況下同時運行多個虛擬機,(它的意思是多個虛擬機使用同一鏡像)併爲每一個虛擬機配置個性化硬件環境(網卡、磁盤、圖形適配器……)

一、設置虛擬機支持虛擬化
Centos6.5安裝kvm虛擬化

二、先關閉防火牆和selinux

[root@cany ~]# service iptables stop    關閉防火牆
[root@cany ~]# setenforce 0                關閉selinux(臨時生效)
setenforce: SELinux is disabled

永久生效方法

[root@cany ~]# vim /etc/sysconfig/selinux   SELINUX=enforcing 改爲disabled 並reboot生效

三、檢查服務器是否支持虛擬化

[root@cany ~]# grep -E -o 'vmx|svm' /proc/cpuinfo
vmx    如果有輸出VMX就支持虛擬化

四、安裝KVM所需組件,VM使用橋接網絡,使用yum安裝方式(配置好yum源)

[root@cany ~]# yum install -y kvm virt-* libvirts bridge-utils qemu-img

五、加載kvm模塊,查看kvm模塊是否被加載

[root@cany ~]# modprobe kvm-intel
[root@cany ~]# lsmod | grep kvm
kvm_intel             54285  0 
kvm                    333172  1 kvm_intel     成功加載!

六、配置網卡,具體參數如下
創建橋接網卡命令 : virsh iface-bridge eth0 br0 或者直接編輯網卡配置

[root@cany ~]# virsh iface-bridge eth0 br0     出現下面的錯誤 重啓一下libvirt服務就好了
error: Failed to reconnect to the hypervisor
error: no valid connection
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory
[root@cany ~]# /etc/init.d/libvirtd restart
Stopping libvirtd daemon:                                  [  OK  ]
Starting libvirtd daemon:                                  [  OK  ]
[root@cany ~]# virsh iface-bridge eth0 br0
Created bridge br0 with attached device eth0
Bridge interface br0 started                              成功橋接
[root@cany ~]# service network restart           重啓網卡

此時的網卡信息

Centos6.5安裝kvm虛擬化
注意 :因爲eth0網卡因爲與br0 網卡進行橋接,所以eth0不顯示ip是正常的

七、查看目前所有的網橋接口

[root@cany ~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.000c29eb8697   yes     eth0
virbr0      8000.525400941201   yes     virbr0-nic

八、修改VNC配置

[root@cany ~]#  vim /etc/libvirt/qemu.conf
    #vnc_listen = "0.0.0.0"  去掉  # 號並wq保存

九、重啓libvirtd和messagebus服務

[root@cany ~]# service libvirtd restart
Stopping libvirtd daemon:                                  [  OK  ]
Starting libvirtd daemon:                                  [  OK  ]
[root@cany ~]# service messagebus restart
Stopping system message bus:                               [  OK  ]
Starting system message bus:                               [  OK  ]

十、創建、安裝LVM虛擬機

[root@cany ~]# mkdir /abc         創建鏡像存放目錄
[root@cany ~]# mkdir -p /data/kvm     創建虛擬磁盤存放目錄
[root@cany ~]# dd if=/dev/cdrom of=/abc/Centos6.iso     把Centos6.5系統的鏡像文件拷貝到abc目錄下
8726528+0 records in
8726528+0 records out
4467982336 bytes (4.5 GB) copied, 157.25 s, 28.4 MB/s
[root@cany libvirt]# cd /data/kvm/    進入虛擬磁盤目錄創建img
[root@cany kvm]# qemu-img create -f qcow2 -o preallocation=metadata kvm_cany.img 5G
Formatting 'kvm_cany.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 preallocation='metadata' 
[root@cany kvm]# ls
kvm_cany.img

安裝虛擬機

[root@cany kvm]# virt-install --name=kvm_cany --ram 512 --vcpus=1 -f /data/kvm/kvm_cany.img --cdrom=/abc/Centos6.iso --graphics vnc,listen=0.0.0.0,port=7788 --force --autostart

Starting install...
Creating domain...                                                                            |    0 B     00:00     
Cannot open display: 
Run 'virt-viewer --help' to see a full list of available command line options
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

如果遇到安裝失敗  請對虛擬磁盤存放目錄權限  chmod -R 777 /data/kvm

VNC客戶端連接KVM虛擬機
軟件:VNC Viewer
Centos6.5安裝kvm虛擬化
Centos6.5安裝kvm虛擬化
VNC連上了之後,安裝系統就ok了!安裝結束後,vnc會自動掛斷,因爲此時虛擬機並未啓動!!!

使用 virsh控制檯爲 KVM 系統管理若干任務 下面就讓我們來手動開啓剛剛安裝的KVM虛擬機

[root@cany kvm]# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # list --all
 Id    Name                           State
----------------------------------------------------
 -     kvm_cany                       shut off

virsh # start kvm_cany
Domain kvm_cany started

重新使用VNC客戶端進行連接 並對其網卡進行配置
Centos6.5安裝kvm虛擬化

注意:出現網卡重啓失敗的話 將eth0網卡配置文件中的 HWADDR修改成MACADDR

rm -rf /etc/udev/rules.d/70-persistent-net.rules

在虛擬機上查看KVM虛擬機運行狀態

Centos6.5安裝kvm虛擬化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章