Kvm virtualization technology deployed on Centos6.5

What is KVM?

Name is KVM Kernel-based Virtual Machine (Kernel-based Virtual Machine), which is a  Linux  a kernel module, such that the Linux kernel module into a Hypervisor:

      It was developed by Quramnet, the company in 2008 was Red Hat acquired. 
      It supports x86 (32 and 64 bit), s390, Powerpc other CPU. 
      2.6.20 it from Linux as a module since it is included in the Linux kernel. 
      It needs to support virtualization extensions CPU. 
      It is completely open source.
KVM architecture

KVM is a full virtualization extensions based virtualization solutions (Intel VT or AMD-V) X86 hardware of open source Linux native. KVM, the virtual machine is implemented as a conventional Linux process, scheduled by the standard Linux scheduler; each virtual CPU virtual machine is implemented as a regular Linux process. This makes it possible to use the Linux kernel KMV existing functionality.

However, KVM itself does not perform any hardware simulation program space needs of customers through / dev / kvm interface to set up a client server virtual address space, providing it with analog I / O, and its video display mapped back to the host display Screen. Currently this application is QEMU.

User space on Linux, kernel space and the virtual machine:

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Guest: a client system comprising a CPU (vCPUs), memory, drives (Console, card, I / O device drivers, etc.), run in one of the KVM placed limited CPU mode. 
KVM: running in kernel space, providing a virtual CPU and memory of the stage, and the client I / O interception. Guest's I / O after being intercepted KVM, QEMU to process. 
QEMU: QEMU modified to code for the virtual machine KVM and run in user space, providing hardware I / O virtualization, the IOCTL dev / kvm device and interact with KVM /.
KVM functions

KVM Supported features include:

      Memory and super-sub CPU Support (Overcommit) 
      supported paravirtualized I / O (virtio) 
      hot-swappable (CPU, block devices, network equipment, etc.) 
      Support for symmetric multiprocessing (Symmetric Multi-Processing, abbreviated as SMP) 
      to support live migration (Live Migration) 
       support direct assignment of PCI devices and a single I / O virtualization (SR-IOV) 
      kernel support combined with the page (KSM) 
      support NUMA (non-uniform memory access, structure non-uniform memory access)
KVM common tools
      libvirt: virtualization API to operate and manage KVM virtual machine, written in C, can be called by Python, Ruby, Perl, PHP, Java and other languages. Operation may include KVM, vmware, XEN, Hyper- v, LXC like Hypervisor. 
      Virsh: libvirt based on the command -line tool (CLI) 
      Virt-Manager: libvirt-based GUI tool 
      virt-v2v: format virtual machine migration tool 
      virt- * tools include: Virt-install (KVM virtual machines to create a command -line tool), Virt -viewer (machine tool connected to the virtual screen), Virt-clone (clone virtual machine tool), virt-top like 
      sVirt: security tool
KVM installation

Before installing To see if the CPU supports virtualization

Lntel CPU:
[root@localhost ~]# cat /proc/cpuinfo |grep vmx

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

If the above operation the output, it shows CPU supports virtualization

lab environment
KVM: Centos 6.5 64-bit operating system 
memory 4GB 
hard drive 20G

Open CPU virtualization support:

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Installation Dependencies
[root@localhost ~]# yum -y install qemu-kvm qemu-kvm-tools python-virtinst.noarch qemu-img bridge-utils libvirt virt-manager

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

查看是否加载了KVM模块
[root@localhost ~]# lsmod | grep kvm
kvm_intel              54285  0
kvm                   333172  1 kvm_intel
 
在libvirt中运行KVM网络有两种方法:NAT和bridge,默认是NAT。

将以bridge(桥接模式)为例。
[root@localhost ~]# ip a
1: lo:  mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:88:85:64 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.113/24 brd 192.168.2.255 scope global eth0
    inet6 fe80::20c:29ff:fe88:8564/64 scope link
       valid_lft forever preferred_lft forever
3: pan0:  mtu 1500 qdisc noop state DOWN
link/ether c2:34:e0:1c:77:37 brd ff:ff:ff:ff:ff:ff
 
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-br0
DEVICE=br0
HWADDR=00:0c:29:88:85:64
TYPE=Bridge
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=192.168.2.113
NETMASK=255.255.255.0
 
[root@localhost network-scripts]# vim ifcfg-eth0
 
DEVICE=eth0
HWADDR=00:0c:29:88:85:64
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
BRIDGE="br0"  #桥接虚拟网卡名称
 
[root@localhost ~]# /etc/init.d/network restart

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

[root@localhost ~]# ip a

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

安装KVM
[root@localhost ~]# mkdir -pv /data_kvm/{store,iso}
mkdir: 已创建目录 "/data_kvm"
mkdir: 已创建目录 "/data_kvm/store"
mkdir: 已创建目录 "/data_kvm/iso"
进入图形界面
[root@localhost ~]# virt-manager

连接出错,重启就好了

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

重启libvirtd
[root@localhost ~]# /etc/init.d/libvirtd  start

[root@localhost ~]# virt-manager

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

添加池

双击localhost{QEMU}----存储-----“+”添加池

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

选择浏览-----找到刚才创建的目录------完成

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

创建存储卷

单机刚创建的KVM01池----新建卷

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

新建虚拟机

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

具体安装过程与安装Linux系统一样(linux系统安装教程)

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

查看KVM的配置文件存放目录
[root@localhost ~]# ls /etc/libvirt/qemu
centos6.5.xml  networks
查看虚拟机状态
[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 2     centos6.5                      running(开启)
虚拟机的关机、开机自启等操作
要保证acpid服务安装并运行
[root@localhost ~]# yum -y install acpid

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

[root@localhost ~]# /etc/init.d/acpid start
 
[root@localhost ~]# /etc/init.d/haldaemon stop
正在关闭 HAL 守护进程:                                    [确定]
 
[root@localhost ~]# /etc/init.d/acpid start
[root@localhost ~]# /etc/init.d/haldaemon start
启动 HAL 守护进程:                                        [确定]
 
[root@localhost ~]# /etc/init.d/acpid status
acpid (pid  1417) 正在运行...
 
关机KVM虚拟机
[root@localhost ~]# virsh shutdown centos6.5
域 centos6.5 被关闭
 
[root@localhost ~]# virsh destroy centos6.5
域 centos6.5 被删除

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     centos6.5                      关闭
 
开机KVM虚拟机
[root@localhost ~]# virsh start centos6.5
域 centos6.5 已开始
[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 4     centos6.5                      running
 
虚拟机伴随宿主机自动启动
[root@localhost ~]# virsh autostart centos6.5
域 centos6.5标记为自动开始
 
[root@localhost ~]# ls /etc/libvirt/qemu
autostart  centos6.5.xml  networks
 
导出虚拟机配置
[root@localhost ~]# virsh dumpxml centos6.5 > /etc/libvirt/qemu/centos6.5_bak.xml
 
删除虚拟机
[root@localhost ~]# virsh undefine centos6.5
 
修改虚拟机配置信息
[root@localhost ~]# virsh edit centos6.5
KVM文件管理,raw格式转换为qcow2格式

虚拟机磁盘文件分为raw与qcow2格式,KVM默认格式是raw裸设备。

raw好处:性能好、速度最快。缺点:不支持一些新的功能。如:镜像、zlib磁盘压缩,AES加密等

libguestfs-tools工具实现格式转换

[root@localhost ~]# yum -y install libguestfs-tools

Kvm virtualization technology deployed on Centos6.5 kvm virtualization technology deployed on Centos6.5

 [root@localhost ~]# qemu-img info /data_kvm/store/KVM011.img
image: /data_kvm/store/KVM011.img
file format: raw
virtual size: 3.9G (4194304000 bytes)
disk size: 3.9G
 
[root@localhost ~]# virsh shutdown centos6.5
域 centos6.5 被关闭
 
格式转换,需要一些时间
[root@localhost ~]# qemu-img convert -f raw -O qcow2 /data_kvm/store/KVM011.img /data_kvm/store/KVM011.qcow2
 
[root@localhost ~]# ls /data_kvm/store/
KVM011.img  KVM011.qcow2
 
[root@localhost ~]# ls /etc/libvirt/qemu
autostart  centos6.5_bak.xml  centos6.5.xml  networks
 
[root@localhost ~]# virsh edit centos6.5
编辑了域 centos6.5 XML 配置。
 
修改centos6.5的xml配置文件
 
23       
24       
 
Virt-cat命令,类似于cat,可查看虚拟机里的文件
 
查看虚拟机里的network文件,需要些时间
[root@localhost ~]# virt-cat -a /data_kvm/store/centos6.5.qcow2 /etc/sysconfig/network
 
Virt-edit命令,用于编辑文件,用法与vim基本相同
[root@localhost ~]# virt-edit -a /data_kvm/store/centos6.5.qcow2 /etc/sysconfig/networ
 
Virt-df命令用查看虚拟机磁盘信息
[root@localhost ~]# virt-df -h centos6.5
Filesystem                                Size       Used  Available  Use%
centos6.5:/dev/sda1                       484M        33M       427M    7%
centos6.5:/dev/sdb1                       4.2G       4.2G          0  100%
centos6.5:/dev/VolGroup/lv_root           3.0G       1.0G       1.8G   34%
虚拟机的克隆
[root@localhost ~]# virsh destroy centos6.5
域 centos6.5 被删除
 
[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     centos6.5                      关闭
 
从centos6.5克隆为centos6.5-clome
[root@localhost ~]# virt-clone -o centos6.5 -n centos6.5-clome -f /data_kvm/store/KVM011-clone.qcow2
 
Clone 'centos6.5-clome' created successfully.
 
[root@localhost ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     centos6.5                      关闭
 -     centos6.5-clome                关闭
虚拟机的快照
创建快照
[root@localhost ~]# virsh snapshot-create centos6.5
Domain snapshot 1535644190 created
 
1535644190:快照的版本号(距离1970年1月1日过去了多少秒)
 
查看快照信息
[root@localhost ~]# virsh snapshot-list centos6.5
 名称               Creation Time             状态
------------------------------------------------------------
 1535644190           2018-08-30 23:49:50 +0800 shutoff

 
恢复快照
[root@localhost ~]# virsh snapshot-list centos6.5
 名称               Creation Time             状态
------------------------------------------------------------
 1535644190           2018-08-30 23:49:50 +0800 shutoff
 1535644574           2018-08-30 23:56:14 +0800 shutoff
 
[root@localhost ~]# virsh snapshot-revert centos6.5 1535644190
[root@localhost ~]# virsh snapshot-current centos6.5

  1535644190
  shutoff
 
 
删除快照
[root@localhost ~]# virsh snapshot-delete centos6.5 1535644190
Domain snapshot 1535644190 deleted
 
[root@localhost ~]# virsh snapshot-list centos6.5
 名称               Creation Time             状态
------------------------------------------------------------
 1535644574           2018-08-30 23:56:14 +0800 shutoff

 

Guess you like

Origin www.cnblogs.com/linuxprobe-sarah/p/11108729.html