CAS KVM 虚拟机的保护与恢复

目录

  • 目录
  • 方式一
  • 方式二

方式一

思路:以 QCOW2 格式来备份和恢复被保护的 KVM 虚拟机

  • Step1:centos7_0(base qcow2) 以 qcow2 格式写入到 iSCSI 设备
root@h3cas-e306:/vms/images# virsh list --all
 Id    Name                           State
----------------------------------------------------
 33    centos7                        running
root@h3cas-e306:/vms/images# virsh domblklist centos7
Target     Source
------------------------------------------------
vda        /vms/images/centos7_0
hda        /vms/isos/CentOS-7-x86_64-Minimal-1511.iso
root@h3cas-e306:/vms/images# qemu-img convert -f qcow2 /vms/images/centos7_0 -O qcow2 /dev/sdd
root@h3cas-e306:/vms/images# qemu-img info /dev/sdd
image: /dev/sdd
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 0
cluster_size: 262144
Format specific information:
    compat: 1.1
    lazy refcounts: false
  • Step2:尝试使用 iSCSI 设备手动启动 KVM 虚拟机
# 创建 XML 文件
root@h3cas-e306:/etc/libvirt/qemu# vim centos7_q_0.xml

<domain type='kvm'>
  <name>centos7_q_0</name>
  <uuid>d668e699-22a4-464b-99c4-1ffdcd6ad4e1</uuid>
  <osha>0</osha>
  <timesync>0</timesync>
  <automem>0</automem>
  <memory unit='KiB'>4194304</memory>
  <currentMemory unit='KiB'>4194304</currentMemory>
  <memorySlots>10</memorySlots>
  <maxMemory unit='KiB'>34359738368</maxMemory>
  <blkiotune>
    <weight>300</weight>
  </blkiotune>
  <memtune>
    <priority>1</priority>
  </memtune>
  <memoryBacking>
    <locked>0</locked>
  </memoryBacking>
  <vcpu placement='static' current='2'>24</vcpu>
  <cputune>
    <shares>512</shares>
    <period>1000000</period>
    <quota>-1</quota>
  </cputune>
  <os>
    <type arch='x86_64' machine='pc-i440fx-2.1'>hvm</type>
    <system>linux</system>
    <boot dev='hd'/>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <cpu>
    <topology sockets='24' cores='1' threads='1'/>
    <numa>
      <cell cpus='0-1' memory='4194304'/>
    </numa>
    <gurantee unit='MHz'>0</gurantee>
  </cpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <tools upgrade='auto'/>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='directsync' io='native'/>
      <source file='/dev/sdd'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw' cache='none'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </controller>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <source mode='bind' path='/var/lib/libvirt/qemu/centos7.agent'/>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </memballoon>
  </devices>

root@h3cas-e306:/etc/libvirt/qemu# virsh define centos7_q_0.xml
Domain centos7_q_0 defined from centos7_q_0.xml

root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 33    centos7                        running
 -     centos7_q_0                    shut off

root@h3cas-e306:/etc/libvirt/qemu# virsh start centos7_q_0
Domain centos7_q_0 started

root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 33    centos7                        running
 34    centos7_q_0                    running
  • Step3:对 centos7_0 做快照得到增量快照数据 centos7_1(increment qcow2)
root@h3cas-e306:/vms/images# virsh snapshot-create-as --domain centos7 snap01 snap01-desc --disk-only --diskspec vda,snapshot=external,file=/vms/images/centos7_1 --atomic
Domain snapshot snap01 created
root@h3cas-e306:/vms/images# l
centos7_0  centos7_1
root@h3cas-e306:/vms/images# qemu-img info centos7_1
image: centos7_1
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 772K
cluster_size: 262144
backing file: /vms/images/centos7_0
backing file format: qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/vms/images# virsh domblklist centos7
Target     Source
------------------------------------------------
vda        /vms/images/centos7_1
hda        /vms/isos/CentOS-7-x86_64-Minimal-1511.iso

NOTE: 现在虚拟机的数据会写入到 centos7_1 中, centos7_0 理论上应该是只读的.

  • Step4:Copy centos7_1 的副本到备份目录下
root@h3cas-e306:/vms/images# cp centos7_1 /kvm_backup/
root@h3cas-e306:/vms/images# ll /kvm_backup/
total 2572
drwxr-xr-x  2 root root    4096 Apr 18 23:39 ./
drwxr-xr-x 33 root root    4096 Apr 18 23:22 ../
-rw-------  1 root root 2883584 Apr 18 23:39 centos7_1
  • Step5:rebase 并 commit centos7_1 到 iSCSI 设备中
root@h3cas-e306:/etc/libvirt/qemu# virsh destroy 34
Domain 34 destroyed
root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 33    centos7                        running
 -     centos7_q_0                    shut off
root@h3cas-e306:/vms/images# cd /kvm_backup/
root@h3cas-e306:/kvm_backup# ls
centos7_1
root@h3cas-e306:/kvm_backup# qemu-img info centos7_1
image: centos7_1
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 2.5M
cluster_size: 262144
backing file: /vms/images/centos7_0
backing file format: qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/kvm_backup# qemu-img info /dev/sdd
image: /dev/sdd
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 0
cluster_size: 262144
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/kvm_backup# qemu-img rebase -b /dev/sdd -F qcow2 centos7_1
root@h3cas-e306:/kvm_backup# qemu-img info centos7_1
image: centos7_1
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 15M
cluster_size: 262144
backing file: /dev/sdd
backing file format: qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/kvm_backup# qemu-img info /dev/sdd
image: /dev/sdd
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 0
cluster_size: 262144
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/kvm_backup# qemu-img commit -f qcow2 centos7_1
Image committed.
root@h3cas-e306:/kvm_backup# qemu-img info centos7_1
image: centos7_1
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 15M
cluster_size: 262144
backing file: /dev/sdd
backing file format: qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/kvm_backup# qemu-img info /dev/sdd
image: /dev/sdd
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 0
cluster_size: 262144
Format specific information:
    compat: 1.1
    lazy refcounts: false
  • Step6:使用 commit 后的 iSCSI 设备启动虚拟机
root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 33    centos7                        running
 -     centos7_q_0                    shut off

root@h3cas-e306:/etc/libvirt/qemu# virsh start centos7_q_0
Domain centos7_q_0 started

root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 33    centos7                        running
 35    centos7_q_0                    running

NOTE: 虚拟机启动成功, 证明 QCOW2 格式的虚拟机增量快照文件是能够合并到虚拟机 Base 数据文件中的。

方式二

思路:以 RAW 格式来备份和恢复被保护的 KVM 虚拟机

  • Step1:centos7(base qcow2) 以 raw 格式写入到 iSCSI 设备
root@h3cas-e306:/vms/images# qemu-img info centos7
image: centos7
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.0G
cluster_size: 262144
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/vms/images# qemu-img convert -f qcow2 /vms/images/centos7 -O raw /dev/sdd
root@h3cas-e306:/vms/images# qemu-img info /dev/sdd
image: /dev/sdd
file format: raw
virtual size: 23G (24696061952 bytes)
disk size: 0
  • Step2:使用 iSCSI 设备手动启动 KVM 虚拟机
root@h3cas-e306:/etc/libvirt/qemu# virsh define centos7_r_0.xml
Domain centos7_r_0 defined from centos7_r_0.xml
root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 36    centos7                        running
 -     centos7_q_0                    shut off
 -     centos7_r_0                    shut off
root@h3cas-e306:/etc/libvirt/qemu# virsh start centos7_r_0
Domain centos7_r_0 started
root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 36    centos7                        running
 37    centos7_r_0                    running
 -     centos7_q_0                    shut off
  • Step3:对 centos7 做快照得到增量快照数据 centos7_1(increment qcow2)
root@h3cas-e306:/vms/images# virsh snapshot-create-as --domain centos7 snap01 snap01-desc --disk-only --diskspec vda,snapshot=external,file=/vms/images/centos7_1 --atomic
Domain snapshot snap01 created
root@h3cas-e306:/vms/images# ls
centos7  centos7_1
root@h3cas-e306:/vms/images# qemu-img info centos7_1
image: centos7_1
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 2.0M
cluster_size: 262144
backing file: /vms/images/centos7
backing file format: qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false
  • Step4: Copy centos7_1 的副本到备份目录下
root@h3cas-e306:/vms/images# cp centos7_1 /kvm_backup/
root@h3cas-e306:/vms/images# cd /kvm_backup/
root@h3cas-e306:/kvm_backup# ls
centos7_1
root@h3cas-e306:/kvm_backup#  qemu-img info  /kvm_backup/centos7_1
image: /kvm_backup/centos7_1
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 2.5M
cluster_size: 262144
backing file: /vms/images/centos7
backing file format: qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/kvm_backup# qemu-img info /dev/sdd
image: /dev/sdd
file format: raw
virtual size: 23G (24696061952 bytes)
disk size: 0
  • Step5:以 raw 的格式 rebase 并 commit centos7_1 到 iSCSI 设备中
root@h3cas-e306:/kvm_backup# qemu-img rebase -b /dev/sdd -F raw centos7_1
root@h3cas-e306:/kvm_backup# qemu-img info centos7_1
image: centos7_1
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 25M
cluster_size: 262144
backing file: /dev/sdd
backing file format: raw
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@h3cas-e306:/kvm_backup# qemu-img info /dev/sdd
image: /dev/sdd
file format: raw
virtual size: 23G (24696061952 bytes)
disk size: 0
root@h3cas-e306:/kvm_backup# qemu-img commit -f qcow2 centos7_1
Image committed.
root@h3cas-e306:~# qemu-img info /dev/sdd
image: /dev/sdd
file format: raw
virtual size: 23G (24696061952 bytes)
disk size: 0
  • Step6: 使用 commit 后的 iSCSI 设备再次启动虚拟机
root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 38    centos7                        running
 -     centos7_q_0                    shut off
 -     centos7_r_0                    shut off

root@h3cas-e306:/etc/libvirt/qemu# virsh start centos7_r_0
Domain centos7_r_0 started

root@h3cas-e306:/etc/libvirt/qemu# virsh list --all
 Id    Name                           State
----------------------------------------------------
 38    centos7                        running
 39    centos7_r_0                    running
 -     centos7_q_0                    shut off

NOTE:尚未测试虚拟机在备份和恢复的过程中虚拟机的应用业务是否被中断。

时间: 2025-01-21 12:17:41

CAS KVM 虚拟机的保护与恢复的相关文章

KVM快照的创建与恢复

因为要做有关KVM虚拟机的实验,所以需要虚拟机生成快照.查询相关资料,说KVM可以使用两种方法生成虚拟机的快照. 方法一.使用qemu-img snapshot命令进行生成快照 方法二.使用virsh snapshot-create-as命令进行生成快照 其实经过我的测试,发现使用qemu-img snapshot命令生成快照后,即使恢复快照也是没有用的.虚拟机还是当前的状态,而不是快照之前的状态.不知道为什么,而且还不稳定,经常报错. 注意要使用KVM的快照功能,虚拟机的硬盘一定要是qcow2

Linux中如何克隆KVM虚拟机

Linux中如何克隆KVM虚拟机 作者 digoal 日期 2016-11-11 标签 Linux , KVM , 虚拟化 , 克隆 背景 当需要批量部署虚拟机时,通常有几种做法,使用模板重新安装. 或者使用已有的虚拟机克隆. 使用模板安装可以参考kickstart脚本的编写方法. <install kvm hosts use kickstart in CentOS 6 in text mode> 本文介绍一下在已经安装好的虚拟机上,克隆虚拟机的方法. 从0开始安装一个虚拟机 在服务器安装必要

kvm虚拟化学习笔记(十一)之kvm虚拟机扩展磁盘空间

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://koumm.blog.51cto.com/703525/1292146 KVM虚拟化学习笔记系列文章列表 ---------------------------------------- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51cto.com/703525/1288795 kvm虚拟化学习笔记(二)之linux kvm虚拟机安装 h

KVM虚拟机克隆

上一篇文章介绍了有关KVM虚拟机快照的创建与恢复,这篇文章我们来介绍有关KVM虚拟机克隆. KVM虚拟机的克隆,我们可以分以下几步: 1. 使用virt-clone命令进行克隆 2. 通过virsh edit命令修改克隆后虚拟机的VNC连接端口 3. 配置克隆后虚拟机的网卡 KVM虚拟机的克隆,我们可以使用virt-clone命令进行.有关virt-clone命令的使用,我们可以通过帮助文档进行查看,如下图: 强烈建议,在克隆虚拟机之前,原来的虚拟机一定要处于关机状态. 现在我们可以使用如下命令

kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://koumm.blog.51cto.com/703525/1290269 KVM虚拟化学习笔记系列文章列表 ---------------------------------------- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51cto.com/703525/1288795 kvm虚拟化学习笔记(二)之linux kvm虚拟机安装 h

在KVM虚拟机中如何添加和使用硬件随机数产生器

很多软件和应用都需要随机数,从纸牌游戏中纸牌的分发到 SSL 安全协议中密钥的产生,到处都有随机数的身影.随机数至少具备两个条件: 数字序列在统计上是随机的 不能通过已知序列推算后面的序列 自从计算机诞生起,寻求用计算机产生高质量的随机数序列的研究就一直是研究者长期关注的课题.一般情况下,使用计算机程序产生一个真正的随机数是很难的,因为程序的行为是可预测的,计算机利用设计好的算法结合用户提供的种子产生的随机数序列通常是"伪随机数"(pseudo-random number),伪随机数就

kvm虚拟化学习笔记(十)之kvm虚拟机快照备份

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://koumm.blog.51cto.com/703525/1291893 KVM虚拟化学习笔记系列文章列表 ---------------------------------------- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51cto.com/703525/1288795 kvm虚拟化学习笔记(二)之linux kvm虚拟机安装 h

KVM虚拟机技术学习总结_Kvm

最近在学习KVM,进程不算太快,近期整理了一下KVM虚拟机技术学习笔记,现在就分享给大家,也给大家做个参考.有需要的朋友可以来了解一下. KVM虚拟机的管理主要是通过virsh命令对虚拟机进行管理. 1.  查看KVM虚拟机配置文件及运行状态 (1) KVM虚拟机默认配置文件位置: /etc/libvirt/qemu/ autostart目录是配置kvm虚拟机开机自启动目录.   (2) virsh命令帮助 # virsh -help 或直接virsh命令和,再执行子命令.如下所示. [root

KVM虚拟机windows系统增加硬盘

前一篇文章介绍了有关linux系统添加硬盘的方法,这次我们来介绍有关windows系统添加的相关步骤. 其实linux和windows添加的硬盘的方法都是一样的,也是可以通过命令行和修改配置文件来添加. 下面主要介绍通过virsh edit命令编辑虚拟机配置文件,来添加硬盘的方法. 注意windows系统添加硬盘需要分硬盘接口,IDE接口和virtio接口. 首先创建一个新的硬盘,使用qemu-img create命令,如下: qemu-img create -f qcow2 testwin.i