<h1><center>KVM部署与使用</center></h1>

**作者：行癫（盗版必究）**

------

## 一：KVM安装

#### 1.普通方式安装

​		查看CPU是否支持VT技术:：

```shell
[root@xingdian ~]# cat /proc/cpuinfo | egrep 'vmx|svm'
注意：
	vmx是英特尔
	svm是AMD
```

​		安装：

```shell
[root@xingdian ~]# yum install *qemu*  *virt*  *kvm* -y
```

#### 2.组包方式安装

```shell
[root@xingdian ~]# yum -y groupinstall Virtualization Host
```

#### 3.启动服务

```shell
[root@xingdian ~]# systemctl start libvirtd
[root@xingdian ~]# systemctl enable libvirtd
```

#### 4.查看kvm模块加载

```shell
[root@xingdian ~]# lsmod | grep kvm
kvm_intel              53484  3 
kvm                   316506  1 kvm_intel
```

#### 5.安装图形化管理工具

```shell
[root@xingdian ~]# yum -y install virt-manager
```

#### 6.使用图形化管理工具

```shell
[root@xingdian ~]# virt-manager
```

<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220710224049107.png" alt="image-20220710224049107" style="zoom:50%;" />

## 二：KVM使用

#### 1.虚拟机的组成部分

​		虚拟机配置文件：

```shell
[root@xingdian ~]# ls /etc/libvirt/qemu
centos7.0.xml  centos7.xml  networks
```

​		储存虚拟机的介质：

```shell
[root@xingdian ~]# ls /var/lib/libvirt/images/
centos7.0.qcow2
```

#### 2.存储池

概念：

​		kvm必须要配置一个目录当作他存储磁盘镜像(存储卷)的目录，我们称这个目录为存储池

默认存储池：

​		/var/lib/libvirt/images/

创建基于文件夹的存储池（目录）：

```shell
[root@xingdian ~]# mkdir -p /home/vmfs
```

定义存储池与其目录：

```shell
[root@xingdian ~]# virsh pool-define-as vmdisk --type dir --target /home/vmfs/
Pool vmdisk defined
```

创建已定义的存储池：

```shell
[root@xingdian ~]# virsh pool-build vmdisk 
Pool vmdisk built
```

查看已定义的存储池，存储池不激活无法使用：

```shell
[root@xingdian ~]# virsh pool-list --all 
Name                 State      Autostart 
-------------------------------------------
default               active     yes       
vmdisk               inactive   no 
```

激活并自动启动已定义的存储池：

```shell
[root@xingdian ~]# virsh pool-start vmdisk 
Pool vmdisk started
[root@xingdian ~]# virsh pool-autostart vmdisk
Pool vmdisk marked as autostarted
```

注意：

​		这里vmdisk存储池就已经创建好了，可以直接在这个存储池中创建虚拟磁盘文件了

在存储池中创建虚拟机存储卷：

```shell
[root@xingdian ~]# virsh vol-create-as vmdisk centos7.qcow2 20G --format qcow2
Vol centos7.qcow2 created
```

根据创建的虚拟机存储卷安装虚拟机：

```shell
[root@xingdian ~]# virt-install --name=centos7 --os-variant=RHEL7 --ram 3000 --vcpus=1 --disk path=/home/vmfs/centos7.qcow2,format=qcow2,size=20,bus=virtio  --accelerate --cdrom /home/镜像/CentOS-7-x86_64-Minimal-1708.iso --vnc --vncport=5910  --vnclisten=0.0.0.0 --network=default --noautoconsole
```

注意：

​		KVM存储池主要是体现一种管理方式，可以通过挂载存储目录，lvm逻辑卷的方式创建存储池

​		KVM存储池也要用于虚拟机迁移任务

#### 3.存储池相关管理命令

在存储池中删除虚拟机存储卷：

```shell
[root@xingdian ~]# virsh vol-delete --pool vmdisk oeltest03.qcow2
```

取消激活存储池：

```shell
[root@xingdian ~]# virsh pool-destroy vmdisk
```

删除存储池定义的目录/data/vmfs：

```shell
[root@xingdian ~]# virsh pool-delete vmdisk
```

取消定义存储池：

```shell
[root@xingdian ~]# virsh pool-undefine vmdisk
```

注意：

​		存储池（Storage pools）是放置虚拟机的存储位置，可以是本地的，也可以是网络存储，本实例放置在卷（Volume）上

​		在创建卷之前，先要创建存储池，存储池可以建立在好几种方式的存储上，现在主要建立一个基于目录的存储池

#### 4.根据配置文件创建虚拟机

需要有磁盘文件：

```shell
[root@xingdian images]# cp centos7.qcow2 centos7-1.qcow2
```

需要有配置文件:配置文件需要修改必要的东西：

```shell
[root@xingdian qemu]# cp centos7.xml centos7-1.xml
```

修改配置文件：（如下）

创建虚拟机：

```shell
[root@xingdian qemu]# virsh define /etc/libvirt/qemu/centos7-1.xml
```

配置文件：

```shell
[root@xingdian qemu]# vim /etc/libvirt/qemu/centos7-1.xml
<domain type='kvm'>
  <name>vm3</name>
  <uuid>a2f62549-c6b7-4b8f-a8e2-c14edda35a78</uuid>
  <memory unit='KiB'>2099200</memory>
  <currentMemory unit='KiB'>2099200</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>Haswell-noTSX</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/vm3.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:f2:28:6f'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
  </devices>
</domain>
```

5.图形化安装虚拟机

<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220710232445811.png" alt="image-20220710232445811" style="zoom:50%;" />

![image-20220710232523493](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220710232523493.png)

![image-20220710232621518](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220710232621518.png)

![image-20220710232657633](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220710232657633.png)

![image-20220710232718965](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220710232718965.png)

![image-20220710232748416](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220710232748416.png)

![image-20220710232850844](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220710232850844.png)

#### 5.虚拟机基本命令

查看正在运行虚拟机：

```shell
[root@xingdian ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     centos7                        running
```

查看所有虚拟机：

```shell
[root@xingdian ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     centos7                        running
 -     centos7.0                      shut off
```

查看kvm虚拟机配置文件：

```shell
[root@xingdian ~]# virsh dumpxml centos7
```

将node4虚拟机的配置文件保存至node6.xml：

```shell
[root@xingdian ~]# virsh dumpxml node4 > /etc/libvirt/qemu/node6.xml
```

修改node6的配置文件：

```shell
[root@xingdian ~]# virsh edit node6
```

注意：

​		如果直接用vim编辑器修改配置文件的话，需要重启libvirtd服务

启动:

```shell
[root@xingdian ~]# virsh start vm1
Domain vm1 started
```

暂停虚拟机：

```shell
[root@xingdian ~]# virsh suspend vm_name 
```

恢复虚拟机：

```shell
[root@xingdian ~]# virsh resume vm_name
```

关闭：

```shell
[root@xingdian ~]# virsh shutdown vm1
Domain vm1 is being shutdown
```

重启：

```shell
[root@xingdian ~]# virsh reboot vm1
Domain vm1 is being reboote
```

重置：

```shell
[root@xingdian ~]# virsh reset vm1
Domain vm1 was reset
```

删除虚拟机：

```shell
[root@xingdian ~]# virsh undefine vm2
Domain vm2 has been undefined
```

注意:

​		虚拟机在开启的情况下undefine是无法删除的

#### 6.连接虚拟机的方法

使用virt-viewer图形连接已启动的虚拟机：

```shell
[root@xingdian ~]# virt-viewer vm1
```

虚拟机开机自动启动：

```shell
[root@xingdian ~]# virsh autostart vm1
```

此目录默认不存在，在有开机启动的虚拟机时自动创建：

```shell
[root@xingdian ~]# ls /etc/libvirt/qemu/autostart/ 
vm1.xml
```

取消开机启动：

```shell
[root@xingdian ~]# virsh autostart --disable vm1
[root@xingdian ~]# ls /etc/libvirt/qemu/autostart/
```

