KVM的外部快照(External snapshot)功能比较实用,可以支持仅对disk进行快照,也支持live snapshot,很多虚拟化云方案中一般也会使用外部快照功能创建快照。不过遗憾的是在centos6的rpm默认源里安装的qemu版本过低不支持–live模式的外部快照 。目前qemu.org上的qemu的最新版本是2.0.0rc3,最新稳定版是1.7.1,而目前centos6的源中使用的qemu版本是0.12.1。

1[root@localhost test]# cd /usr/libexec/
2[root@localhost libexec]# ./qemu-kvm -version
3QEMU PC emulator version 0.12.1 (qemu-kvm-0.12.1.2), Copyright (c) 2003-2008 Fabrice Bellard

如下所示,使用–live功能时会报错:

1[root@localhost libvirt]#virsh snapshot-create-as ppd_centos snap1-centos "snap1 description"  --diskspec vda,file=/file/snap1-centos.qcow2 --disk-only --atomic
2error: Operation not supported: live disk snapshot not supported with this QEMU binary

在serverfault上有人提到了该问题,链接为:http://serverfault.com/questions/567234/how-to-replace-qemu-binary-with-newer-version-for-libvirt-live-snapshots ,同时我也尝试了使用升级qemu为最新的1.7.1测试–live,发现问题也依旧 。目前估计还会对内核版本和libvirt的版本有依赖或兼容性问题。编译步骤如下:

  1[root@localhost qemu-1.7.1]# ./configure --prefix=/usr/local/qemu
  2ERROR: glib-2.12 required to compile QEMU
  3[root@localhost qemu-1.7.1]#yum -y install glib*
  4[root@localhost qemu-1.7.1]# ./configure --prefix=/usr/local/qemu
  5Install prefix    /usr/local/qemu
  6BIOS directory    /usr/local/qemu/share/qemu
  7binary directory  /usr/local/qemu/bin
  8library directory /usr/local/qemu/lib
  9libexec directory /usr/local/qemu/libexec
 10include directory /usr/local/qemu/include
 11config directory  /usr/local/qemu/etc
 12local state directory   /usr/local/qemu/var
 13Manual directory  /usr/local/qemu/share/man
 14ELF interp prefix /usr/gnemul/qemu-%M
 15Source path       /usr/local/src/qemu-1.7.1
 16C compiler        cc
 17Host C compiler   cc
 18C++ compiler      c++
 19Objective-C compiler cc
 20ARFLAGS           rv
 21CFLAGS            -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -g
 22QEMU_CFLAGS       -fPIE -DPIE -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing  -Wendif-labels -Wmissing-include-dirs -Wempty-body -Wnested-externs -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wold-style-declaration -Wold-style-definition -Wtype-limits -fstack-protector-all -I$(SRC_PATH)/pixman/pixman -I$(BUILD_DIR)/pixman/pixman -I$(SRC_PATH)/dtc/libfdt
 23LDFLAGS           -Wl,--warn-common -Wl,-z,relro -Wl,-z,now -pie -m64 -g
 24make              make
 25install           install
 26python            python -B
 27smbd              /usr/sbin/smbd
 28host CPU          x86_64
 29host big endian   no
 30target list        alpha-softmmu arm-softmmu cris-softmmu i386-softmmu lm32-softmmu m68k-softmmu microblazeel-softmmu microblaze-softmmu mips64el-softmmu mips64-softmmu mipsel-softmmu mips-softmmu moxie-softmmu or32-softmmu ppc64-softmmu ppcemb-softmmu ppc-softmmu s390x-softmmu sh4eb-softmmu sh4-softmmu sparc64-softmmu sparc-softmmu unicore32-softmmu x86_64-softmmu xtensaeb-softmmu xtensa-softmmu alpha-linux-user armeb-linux-user arm-linux-user cris-linux-user i386-linux-user m68k-linux-user microblazeel-linux-user microblaze-linux-user mips64el-linux-user mips64-linux-user mipsel-linux-user mips-linux-user mipsn32el-linux-user mipsn32-linux-user or32-linux-user ppc64abi32-linux-user ppc64-linux-user ppc-linux-user s390x-linux-user sh4eb-linux-user sh4-linux-user sparc32plus-linux-user sparc64-linux-user sparc-linux-user unicore32-linux-user x86_64-linux-user
 31tcg debug enabled no
 32gprof enabled     no
 33sparse enabled    no
 34strip binaries    yes
 35profiler          no
 36static build      no
 37-Werror enabled   no
 38pixman            internal
 39SDL support       no
 40GTK support       no
 41curses support    no
 42curl support      no
 43mingw32 support   no
 44Audio drivers     oss
 45Block whitelist (rw)
 46Block whitelist (ro)
 47VirtFS support    no
 48VNC support       yes
 49VNC TLS support   no
 50VNC SASL support  no
 51VNC JPEG support  no
 52VNC PNG support   no
 53VNC WS support    no
 54xen support       no
 55brlapi support    no
 56bluez  support    no
 57Documentation     no
 58GUEST_BASE        yes
 59PIE               yes
 60vde support       no
 61Linux AIO support no
 62ATTR/XATTR support yes
 63Install blobs     yes
 64KVM support       yes
 65RDMA support      no
 66TCG interpreter   no
 67fdt support       yes
 68preadv support    yes
 69fdatasync         yes
 70madvise           yes
 71posix_madvise     yes
 72sigev_thread_id   yes
 73uuid support      no
 74libcap-ng support no
 75vhost-net support yes
 76vhost-scsi support yes
 77Trace backend     nop
 78Trace output file trace-<pid>
 79spice support     no (/)
 80rbd support       no
 81xfsctl support    no
 82nss used          no
 83libusb            no
 84usb net redir     no
 85GLX support       no
 86libiscsi support  no
 87build guest agent yes
 88QGA VSS support   no
 89seccomp support   no
 90coroutine backend ucontext
 91coroutine pool    yes
 92GlusterFS support no
 93virtio-blk-data-plane no
 94gcov              gcov
 95gcov enabled      no
 96TPM support       no
 97libssh2 support   no
 98TPM passthrough   no
 99QOM debugging     yes
100vhdx              no
101[root@localhost qemu-1.7.1]# make && make install

编译完成后,通过软链接替换qemu-kvm和qemu-img两个可执行文件,由于测试结果是失败的,所以这里过程也略过。在destroy关机情况下,测试的–disk外部快照功能如下:

 1[root@localhost file]# virsh destroy ppd_centos
 2Domain ppd_centos destroyed
 3[root@localhost file]# virsh snapshot-create-as ppd_centos snap1-centos "snap1 description" --disk-only --atomic
 4Domain snapshot snap1-centos created
 5[root@localhost file]# qemu-img info centos.qcow2
 6image: centos.qcow2
 7file format: qcow2
 8virtual size: 30G (32212254720 bytes)
 9disk size: 1.6G
10cluster_size: 65536
11Format specific information:
12    compat: 0.10
13[root@localhost file]# qemu-img info centos.snap1-centos
14image: centos.snap1-centos
15file format: qcow2
16virtual size: 30G (32212254720 bytes)
17disk size: 196K
18cluster_size: 65536
19backing file: /file/centos.qcow2
20backing file format: qcow2
21Format specific information:
22    compat: 0.10
23[root@localhost file]# virsh snapshot-list ppd_centos
24 Name                 Creation Time             State
25------------------------------------------------------------
26 snap1-centos         2014-04-15 18:20:54 +0800 shutoff

从上面的查询中不难看出centos.snap1-centos的backing file来自于/file/centos.qcow2 。同样,也可以利用下面的命令进行查看:

1[root@localhost libexec]# virsh domblklist  ppd_centos --details
2Type       Device     Target     Source
3------------------------------------------------
4file       disk       vda        /file/centos.snap1-centos
5block      cdrom      hdc        -

创建完外部快照后,原镜像文件会变成只读,新的变更都会写入到新的快照文件中。如下,我做完快照后,上传到一个文件到guest主机中,查看文件状态发现如下:

 1[root@localhost test]# ll /file/
 2total 39673072
 3-rwxr-xr-x. 1 root root 32212254720 Apr 14 16:39 centos.img
 4-rw-r--r--. 1 root root  1704854016 Apr 15 18:20 centos.qcow2
 5-rw-r--r--. 1 root root      197120 Apr 15 18:20 centos.snap1-centos
 6drwx------. 2 root root       16384 Apr  9 18:08 lost+found
 7drwxr-xr-x. 8 1001 1002        4096 Apr 14 09:58 ntfs-3g_ntfsprogs-2014.2.15
 8-rwxr-xr-x. 1 qemu qemu 53687091200 Apr 15 19:29 win2008.img
 9-rwxr-xr-x. 1 qemu qemu 32212254720 Apr 15 19:27 win7.img
10[root@localhost test]# ll
11total 0
12[root@localhost test]# ll /file/
13total 39772532
14-rwxr-xr-x. 1 root root 32212254720 Apr 14 16:39 centos.img
15-rw-r--r--. 1 qemu qemu  1704854016 Apr 15 18:20 centos.qcow2
16-rw-r--r--. 1 qemu qemu   102105088 Apr 15 19:33 centos.snap1-centos
17drwx------. 2 root root       16384 Apr  9 18:08 lost+found
18drwxr-xr-x. 8 1001 1002        4096 Apr 14 09:58 ntfs-3g_ntfsprogs-2014.2.15
19-rwxr-xr-x. 1 qemu qemu 53687091200 Apr 15 19:32 win2008.img
20-rwxr-xr-x. 1 qemu qemu 32212254720 Apr 15 19:32 win7.img
21[root@localhost test]#

从上面不难发现,centos.snap1-centos的大小在增大,而原centos.qcow2的大小不变。至于快照的删除、回滚和内部快照操作没有区别。不过外部快照多了一个快照合并功能:

1# virsh blockpull --domain ppd_centos --path /file/centos.snap1-centos

以上命令就可以将快照的变化写入境像文件中。

关于centos功能支持性的问题,外部快照功能测试的有点草率。回头有时间换到ubuntu、fedora或重新编译安装再做测试 。