CentOS 7 下 KVM 使用

KVM 安装

升级系统、不升级内核

[root@demo ~]# yum -y update --exclude=kernel* --exclude=centos-release*

安装 KVM 及依赖

[root@demo ~]# yum -y install kvm virt-manager libvirt virt-install qemu-kvm xauth dejavu-lgc-sans-fonts virt-viewer
  • KVM
A full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V).
  • Virt-Manager
A desktop user interface for managing virtual machines through libvirt.
  • Libvirt
A toolkit to interact with the virtualization capabilities of recent versions of Linux.
  • Virt-Install
A command line tool for creating new KVM container guests using the "libvirt"hypervisor management library.
  • Qemu-kvm
A Linux kernel module that allows a user space program to utilize the hardware virtualization features of various processors.
  • Dejavu-lgc-sans-fonts
A font family based on the Vera Fonts.
  • virt-viewer
A minimal tool for displaying the graphical console of a virtual machine.

配置网络转发

[root@demo ~]# echo "net.ipv4.ip_forward = 1"|sudo tee /etc/sysctl.d/99-ipforward.conf
[root@demo ~]# sudo sysctl -p /etc/sysctl.d/99-ipforward.conf

修改网卡配置

物理网卡配置
[root@demo ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno1 | grep -v ^#
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
DEVICE=eno1
ONBOOT=yes
BRIDGE=br0
新增 br0 网卡配置
[root@demo ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 
DEVICE="br0"
ONBOOT="yes"
TYPE="Bridge"
BOOTPROTO=static
IPADDR=10.10.200.108
NETMASK=255.255.255.0
GATEWAY=10.10.200.1
DNS1=8.8.8.8
DEFROUTE=yes

服务启停

重启 network 服务
[root@demo ~]# systemctl restart network
关闭 NetworkManager && 禁止开机启动
[root@demo ~]# systemctl stop NetworkManager && systemctl disable NetworkManager
libvirtd 服务配置
[root@demo ~]# systemctl start libvirtd
[root@demo ~]# systemctl enable libvirtd

KVM 创建虚拟机

准备工作

存放目录创建
[root@demo ~]# mkdir -p /home/virsh_workstation
虚拟机系统磁盘准备
[root@demo ~]# qemu-img create -f qcow2 /home/virsh_workstation/test/test.qcow2 50G
Formatting '/home/virsh_workstation/test/test.qcow2', fmt=qcow2 size=53687091200 encryption=off cluster_size=65536 lazy_refcounts=off
系统镜像准备
[root@demo ~]# ll | grep CentOS
-rw-r--r--      1 root          root     8280604672 Sep  9 16:56 CentOS-7-x86_64-Everything-1611.iso
virt-install 安装
[root@demo ~]# virt-install \
> --virt-type=kvm \
> --name=virsh_host_online_keyan06 \
> --os-type=linux \
> --os-variant=rhel7 \
> --vcpus=4 \
> --ram=51200 \
> --cdrom=/home/CentOS-7-x86_64-Everything-1611.iso \
> --disk=/home/virsh_workstation/virsh_host_online_keyan06/virsh_host_online_keyan06.qcow2,format=qcow2 \
> --network bridge=br0 \
> --graphics vnc,listen=0.0.0.0 \
> --force

会调用安装窗口,通过界面进行 CentOS 7 虚拟机安装

在这里插入图片描述
在这里插入图片描述

KVM 虚拟机添加磁盘

查看虚拟机信息

查看所有虚拟机
[root@demo ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     test                            shut off
查看虚拟机磁盘信息
[root@demo ~]# virsh domblklist test 
Target     Source
------------------------------------------------
vda        /home/virsh_workstation/test/test.qcow2
hda        -

为虚拟机增加磁盘

新建磁盘文件
[root@demo ~]# qemu-img create -f qcow2 /home/virsh_workstation/test/test_data.qcow2 500G
Formatting '/home/virsh_workstation/test/test_data.qcow2', fmt=qcow2 size=536870912000 encryption=off cluster_size=65536 lazy_refcounts=off
将磁盘添加到虚拟机中
[root@demo ~]# virsh attach-disk test --source /home/virsh_workstation/test/test_data.qcow2 --target vdb --subdriver qcow2 --persistent
Disk attached successfully

删除磁盘

[root@demo ~]# virsh detach-disk test /home/virsh_workstation/test/test_data.qcow2
查看虚拟机磁盘信息
[root@demo ~]# virsh domblklist test 
Target     Source
------------------------------------------------
vda        /home/virsh_workstation/test/test.qcow2
vdb        /home/virsh_workstation/test/test_data.qcow2
hda        -
进入虚拟机查看
[root@demo ~]# fdisk -l | grep vdb
Disk /dev/vdb: 536.9 GB, 536870912000 bytes, 1048576000 sectors

KVM 虚拟机快照操作

KVM 虚拟机快照 – 查看

[root@demo ~]# virsh snapshot-list test
Name                 Creation Time             State
------------------------------------------------------------

KVM 虚拟机快照 – 创建

创建快照
[root@demo ~]# virsh snapshot-create-as --domain test --name test_snapshot --description "system clear"
Domain snapshot test_snapshot created
查看创建结果
[root@demo ~]# virsh snapshot-list virsh_host_online_keyan06 
 Name                 Creation Time             State
------------------------------------------------------------
 test_snapshot      2020-09-09 18:35:20 +0800   running
查看快照详细信息
[root@demo ~]# virsh snapshot-dumpxml --domain test --snapshotname test_snapshot
--domain:指定虚拟机
--snapshotname:指定虚拟机快照名称

KVM 虚拟机快照 – 恢复

[root@demo ~]# virsh snapshot-revert test test_snapshot
virsh snapshot-revert 虚拟机名称 虚拟机快照名称

KVM 虚拟机快照 – 删除

[root@demo ~]# virsh snapshot-delete --domain test --snapshotname test_snapshot
Domain snapshot test_snapshot deleted

KVM 常用命令

虚拟机启停

启动虚拟机
[root@demo ~]# virsh start test
停止虚拟机
[root@demo ~]# virsh shutdown test
查看虚拟机状态
[root@demo ~]# virsh list --all

参考

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐