KVM虚拟机总结

2019-07-30 20:01:21 浏览数 (1)

版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons)

待整理

克隆方案

代码语言:javascript复制
# virsh-clone 
# virt-clone --connect qemu:///system --original test2 --name testclone --file /var/lib/libvirt/images/newdemo.img --file /var/lib/libvirt/images/newdata.img

virt-clone --connect qemu:///system -o test2 --n testclone -f /fine1fs/virtual/vm/node25/testclone

/fine1fs/virtual/vm/node25/34c0ac46-8052-11e9-a322-fcaa14a3eb62

# 连接克隆 
# os_disk img_type == file
qemu-img create -f qcow2 -b /fine1fs/virtual/vm/node25/34c0ac46-8052-11e9-a322-fcaa14a3eb62/34c0ac46-8052-11e9-a322-fcaa14a3eb62_os.qcow2 /fine1fs/virtual/vm/node25/teset12/test12.qcow2

virsh dumpxml test2 > /fine1fs/virtual/vm/node25/teset12/test12.xml
vim /etc/libvirt/qemu/test12.xml #修改name、UUID
virsh define /etc/libvirt/qemu/test12.xml
# os_disk img_type == rbd
# rbd 使用快照
#创建一个快照
rbd snap create -p rbd_pool/foo@foo_snap

#查看快照
rbd snap list -p rbd_pool foo #注意是foo不是foo_snap

#在克隆之前,确保快照处于protected状态
rbd snap protect  rbd_pool/foo@foo_image

# 克隆 
rbd clone test_for_s3/node25_a55f772e-86a7-11e9-b731-fcaa14a3eb62_os@20190610152225.snap test_for_s3/node25_e3e0926c-8b52-11e9-abe3-fcaa14a3eb62_os

#创建一个新的pool1: 
ceph osd pool create pool1 8 8

#克隆到pool1:
rbd clone rbd_pool/foo@foo_snap pool1/newfoo

#查看快照的children: 
rbd children rbd_pool/foo@foo_snap
#pool1/newfoo

#扁平化可以断开父子间的依赖关系:
rbd flatten pool1/newfoo

虚拟机克隆

代码语言:javascript复制
# 
rbd ls test_for_s3

# 创建快照
rbd snap create test_for_s3/node25_8c8c9228-8c0f-11e9-bc32-fcaa14a3eb62_os@20190610152226.snap



# 保护快照
rbd snap protect  test_for_s3/node25_a55f772e-86a7-11e9-b731-fcaa14a3eb62_os@20190610152226.snap

# 克隆 
rbd clonetest_for_s3/node25_8c8c9228-8c0f-11e9-bc32-fcaa14a3eb62_os@20190610152226.snap test_for_s3/node25_3be0e280-8c1d-11e9-8eaa-fcaa14a3eb62_os 2>&1 | tee e

rbd clone rbd clone %s/%s@%s %s/%s 2>&1 |tee %s %s

#查看快照的children: 
rbd children rbd_pool/foo@foo_snap
#pool1/newfoo

#扁平化可以断开父子间的依赖关系:
rbd flatten test_for_s3/node25_34c0ac46-8052-11e9-a322-fcaa14a3eb62_os

# 取消保护
rbd snap unprotect  test_for_s3/node25_a55f772e-86a7-11e9-b731-fcaa14a3eb62_os@20190610152226.snap

# 删除快照
rbd snap rm test_for_s3/node25_a55f772e-86a7-11e9-b731-fcaa14a3eb62_os@20190610152226.snap

虚拟机快照

创建虚拟机快照

代码语言:javascript复制
kvm 环境 qcow2 支持快照
# 确认镜像格式
qemu-img info test.qcow2

# 创建虚拟机快照
qemu-img snapshot -c snapshot01 test.qcow2  

# 列出某个镜像的所有快照
qemu-img snapshot -l test.qcow2  

# 使用快照
qemu-img snapshot -a snapshot01 test.qcow2  

# 删除快照
qemu-img snapshot -d snapshot01 test.qcow2 

创建外部快照

代码语言:javascript复制
# 取最后一个快照
last_snap_name = dom.snapshotListNames()

# 路径获取
import os
a = "/fine1fs/virtual/vm/node25/c998baae-9199-11e9-891b-fcaa14a3eb62/c998baae-9199-11e9-891b-fcaa14a3eb62_os.snapshot2"
print(os.path.splitext(a)[0])


if dom.isActive():
	dom.managedSave()
	
snap = dom.snapshotCreateXML(snapxml,flags=148)


# 磁盘快照和内存 磁盘快照
#!/bin/bash
DOMAIN="test"
TIMESTAMP=`date  %s`
SNAPSHOT_NAME=$TIMESTAMP

VM_FOLDER="/path/to/vms"
SNAPSHOT_FOLDER="`echo $VM_FOLDER`/`echo $DOMAIN`/snapshots/`echo $TIMESTAMP`"
mkdir -p $SNAPSHOT_FOLDER

MEM_FILE="`echo $SNAPSHOT_FOLDER`/mem.qcow2"
DISK_FILE="`echo $SNAPSHOT_FOLDER`/disk.qcow2"

# Find out if running or not
STATE=`virsh dominfo $DOMAIN | grep "State" | cut -d " " -f 11`

if [ "$STATE" = "running" ]; then

  virsh snapshot-create-as 
    --domain $DOMAIN $SNAPSHOT_NAME 
    --diskspec vda,file=$DISK_FILE,snapshot=external 
    --memspec file=$MEM_FILE,snapshot=external 
    --atomic

else

  virsh snapshot-create-as 
    --domain $DOMAIN $SNAPSHOT_NAME 
    --diskspec vda,file=$DISK_FILE,snapshot=external 
    --disk-only 
    --atomic

fi

virsh snapshot-create-as  --domain snaptest snapshot1 --diskspec file="/fine1fs/virtual/vm/node25/14e9948e-9199-11e9-8eb8-fcaa14a3eb62/14e9948e-9199-11e9-8eb8-fcaa14a3eb62_os.snapshot1",snapshot=external --memspec file="/fine1fs/virtual/vm/node25/14e9948e-9199-11e9-8eb8-fcaa14a3eb62/14e9948e-9199-11e9-8eb8-fcaa14a3eb62_os.memory",snapshot=external --atomic

虚拟机快照测试

代码语言:javascript复制
# 虚拟机 running 
if dom.isActive():
    ret = dom.save(path)
    
    
dom.restore(path)


#ret = dom.snapshotCreateXML(memansp,flags=384)

文档修改要求

代码语言:javascript复制
1.虚拟机支持快照
  (1)已创建快照虚拟机不允许导出、克隆、迁移操作
  (2)磁盘快照使用外部快照,创建快照需要暂停虚拟机(是否需要手动暂停)或关闭虚拟机,支持raw和qcow2格式
  (3)只有虚拟机运行的时候,才允许创建内存快照

qita

代码语言:javascript复制
virsh start x                                 启动名字为x的非活动虚拟机

virsh create x.xml                      创建虚拟机(创建后,虚拟机立即执行,成为活动主机)

virsh suspend x                             暂停虚拟机

virsh resume x                             启动暂停的虚拟机

virsh shutdown x                    正常关闭虚拟机

virsh destroy x                              强制关闭虚拟机

virsh dominfo x                              显示虚拟机的基本信息

virsh domname 2                                   显示id号为2的虚拟机名

virsh domid x                                显示虚拟机id号

virsh domuuid x                             显示虚拟机的uuid

virsh domstate x                          显示虚拟机的当前状态
代码语言:javascript复制
virsh blockcommit --domain snap –path --base /fine1fs/virtual/vm/node14/bab1f794-93d3-11e9-8c04-0cc47a6b577c/bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot03 --top /fine1fs/virtual/vm/node14/bab1f794-93d3-11e9-8c04-0cc47a6b577c/bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot04

virsh blockcommit --domain snap hda --base /fine1fs/virtual/vm/node14/bab1f794-93d3-11e9-8c04-0cc47a6b577c/bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot01 --top /fine1fs/virtual/vm/node14/bab1f794-93d3-11e9-8c04-0cc47a6b577c/bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot04 --wait --verbose

qemu-img rebase -u -b bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot02 bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot04

代码语言:javascript复制
# 查看快照关系链
qemu-img info --backing-chain bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot05

# 合并快照
qemu-img rebase -u -b bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot02 bab1f794-93d3-11e9-8c04-0cc47a6b577c_os.snapshot04

0 人点赞