1. 程式人生 > 實用技巧 >centos7 kvm安裝使用

centos7 kvm安裝使用

centos7 kvm安裝使用

kvm簡介

KVM 全稱是 Kernel-Based Virtual Machine。也就是說 KVM 是基於 Linux 核心實現的。
KVM有一個核心模組叫 kvm.ko,只用於管理虛擬 CPU 和記憶體。

那 IO 的虛擬化,比如儲存和網路裝置由誰實現呢?
這個就交給 Linux 核心和Qemu來實現。

說白了,作為一個 Hypervisor,KVM 本身只關注虛擬機器排程和記憶體管理這兩個方面。IO 外設的任務交給 Linux 核心和 Qemu

kvm安裝

檢視cpu是否支援虛擬化

[root@localhost ~]# grep -E '(vmx|svm)' /proc/cpuinfo **

安裝qemu-kvm(使用者態管理工具),libvirt(命令列管理工具),virt-install(安裝kvm工具),bridge-utils(橋接裝置管理工具)

[root@localhost ~]# yum install -y qemu-kvm libvirt virt-install bridge-utils

確保載入kvm模組

[root@localhost ~]# lsmod |grep kvm
kvm_intel             174841  0 
kvm                   578518  1 kvm_intel
irqbypass              13503  1 kvm
#如果沒有載入,執行一下命令
[root@localhost ~]# modprobe kvm
[root@localhost ~]# modprobe kvm-intel

啟動libvirtd服務

[root@localhost ~]# systemctl enable libvirtd
[root@localhost ~]# systemctl start  libvirtd
[root@localhost ~]# systemctl status  libvirtd

配置kvm橋接模式

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens32 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-br0
NAME=br0
DEVICE=br0
ONBOOT=yes
NETBOOT=yes
IPV6INIT=no
BOOTPROTO=static
NM_CONTROLLED=no
TYPE=Bridge
IPADDR=192.168.0.127
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=8.8.8.8

[root@localhost network-scripts]# vim ifcfg-ens32 
NAME=ens32
DEVICE=ens32
BOOTPROTO=none
NM_CONTROLLED=no
ONBOOT=yes
BRIDGE=br0

檢視網橋

[root@localhost ~]# brctl show
bridge name    bridge id        STP enabled    interfaces
br0        8000.000c29d1267b    no        ens32
virbr0        8000.52540063d8f4    yes        virbr0-nic

刪除virbr0

[root@localhost ~]# brctl show
bridge name    bridge id        STP enabled    interfaces
br0        8000.000c29d1267b    no        ens32
virbr0        8000.52540063d8f4    yes        virbr0-nic
[root@localhost ~]# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

[root@localhost ~]# virsh net-destroy default
Network default destroyed

[root@localhost ~]# virsh net-undefine default
Network default has been undefined

[root@localhost ~]# systemctl restart libvirtd.service
[root@localhost ~]# brctl show
bridge name    bridge id        STP enabled    interfaces
br0        8000.000c29d1267b    no        ens32

使用virt-manager管理kvm

由於要用virt-manager圖形化安裝虛擬機器,所以還需要安裝桌面

[root@localhost ~]# yum grouplist
[root@localhost ~]# yum groupinstall "GNOME Desktop" -y

配置中文桌面

grep -E "^[ \t]*export[ \t]+LANG[ \t]*=" /etc/profile&& y="yes" || y="no"
if [[ "$y" = "yes" ]]; then
sed -r -i -e '/^[ \t]*export[ \t]+LANG[ \t]*=/c\export LANG="zh_CN.UTF-8"' /etc/profile
else
echo 'export LANG="zh_CN.UTF-8"' >>/etc/profile
fi
source /etc/profile

安裝virt-manager

[root@localhost ~]# yum -y install virt-manager

xshell連結

[root@localhost ~]# virt-manager

提前將ISO系統映象存放到伺服器的一個目錄裡,比如/data/iso

[root@localhost ~]# mkdir -p /data/{iso,kvmstorage}
[root@localhost ~]# cd /data/iso/
[root@localhost iso]# ll
總用量 4217748
-rw-r--r-- 1 root root  950009856 11月 22 15:24 CentOS-7-x86_64-Minimal-1804.iso
-rw-r--r-- 1 root root 3368962048 3月  23 2012 cn_windows_server_2008_r2.iso

新建虛擬機器

kvm常用命令

KVM虛擬機器的管理主要是通過virsh命令對虛擬機器進行管理

命令幫助
[root@localhost ~]# virsh --help
檢視虛擬機器狀態
[root@localhost ~]# virsh list --all
 Id    名稱                         狀態
----------------------------------------------------
 4     win2k8r2                       running

關機
[root@localhost ~]# virsh shutdown win2k8r2

強制關閉電源
[root@localhost ~]# virsh destroy win2k8r2

通過配置檔案建立虛擬機器
[root@localhost ~]# virsh create /etc/libvirt/qemu/win2k8r2.xml

設定虛擬機器開機自啟
[root@localhost ~]# virsh autostart win2k8r2
[root@localhost ~]# ll /etc/libvirt/qemu/autostart/
總用量 0
lrwxrwxrwx 1 root root 30 1月  24 13:06 win2k8r2.xml -> /etc/libvirt/qemu/win2k8r2.xml

到處虛擬機器配置檔案
[root@localhost ~]# virsh dumpxml win2k8r2 > /etc/libvirt/qemu/win2k8r2_bak.xml

刪除虛擬機器(該命令只刪除配置檔案,並不刪除磁碟檔案)
[root@localhost ~]# virsh undefine win2k8r2

通過匯出備份的配置檔案恢復原KVM虛擬機器的定義,並重新定義虛擬機器。
[root@localhost ~]# mv /etc/libvirt/qemu/win2k8r2_bak.xml /etc/libvirt/qemu/win2k8r2.xml
[root@localhost ~]# virsh define /etc/libvirt/qemu/win2k8r2.xml

編輯配置檔案
[root@localhost ~]# virsh edit win2k8r2

掛起
[root@localhost ~]# virsh suspend win2k8r2

恢復
[root@localhost ~]# virsh resume win2k8r2

其他命令

建立儲存卷

[root@localhost ~]# qemu-img create -f qcow2 /data/kvmstorage/centos7.qcow2 20G
Formatting '/data/kvmstorage/centos7.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@localhost ~]# ll //data/kvmstorage
總用量 7437168
-rw-r--r-- 1 root root      197120 1月  24 13:21 centos7.qcow2
-rw------- 1 qemu qemu 42956488704 1月  24 13:21 win2k8r2.qcow2

生成虛擬機器

[root@localhost ~]# virt-install --virt-type kvm --name centos --ram 1024 \
  --disk /data/kvmstorage/centos7.qcow2,format=qcow2 \
  --network bridge=br0 \
  --graphics vnc,listen=0.0.0.0 --noautoconsole \
  --os-type=linux --os-variant=rhel7 \
  --location=/data/iso/CentOS-7-x86_64-Minimal-1804.iso

[root@localhost ~]# virsh list --all
 Id    名稱                         狀態
----------------------------------------------------
 5     win2k8r2                       running
 7     centos                         running