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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM