天天看點

kvm虛拟機快照管理

虛拟機快照

  • 檢視虛拟機的格式(磁盤格式必須為qcow2)
[[email protected] kvm]#  qemu-img info /home/kvm/virtual-img/centos-lwq-1.qcow2 
image: /home/kvm/virtual-img/centos-lwq-1.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.2G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         init                      0 2021-03-29 17:58:52   00:00:00.000
2         statinit                  0 2021-03-29 18:02:06   00:00:00.000
3         centos-lwq-1.init111      0 2021-03-29 18:23:55   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: false

           
  • 給指定虛拟機建立快照
[[email protected] kvm]# virsh snapshot-create-as centos-lwq-1 stat
已生成域快照 stat
           
  • 檢視指定虛拟機的快照
[[email protected] kvm]# virsh snapshot-list  centos-lwq-1 
 名稱               生成時間              狀态
------------------------------------------------------------
 centos-lwq-1.init111 2021-03-29 18:23:55 +0800 shutoff
 init                 2021-03-29 17:58:51 +0800 shutoff
 stat                 2021-03-29 19:38:03 +0800 shutoff
 statinit             2021-03-29 18:02:06 +0800 shutoff
           
  • 恢複快照
  • 删除快照
[[email protected] kvm]# virsh snapshot-delete centos-lwq-1 --snapshotname stat
已删除域快照 stat
           

腳本

#!/bin/bash
## 添加快照
## zy.小強

snap_create(){
while :
do
    disk_home=/home/kvm/virtual-img/
    
    
    virsh list --all
    read -p "請輸入你要建立快照的虛拟機: " vm_name
    virsh list --all | grep  $vm_name &>/dev/null
    if [ $? -eq 0  ];then
    	stat=`virsh list --all | grep $vm_name | awk   '{print $3}'`
    	echo "目前虛拟機狀态為$stat"
    	read -p "是否繼續建立快照[y/n]" option	
    	case $option in
    	y|Y)
    		read -p "請輸入快照的名字: " snap
    		echo "正在建立快照"
    		disk_type=`qemu-img info ${disk_home}${vm_name}.qcow2 | awk -F ":[ ]" 'NR==2 {print $2}'`
    
    			if [ "$disk_type"  =  "qcow2"  ];then
    				
    				virsh snapshot-create-as  $vm_name  $snap
    				virsh snapshot-list $vm_name
    				if [ $? -eq 0  ];then
    					echo "建立成功"
    					
    				else
    					echo "建立失敗"
    					break
    			
    				fi
    			else
    				echo "類型不支援,正在退出!!!"
    				break			
    
    			fi
    	;;
    
    
    	n|N)
    		break
    	;;
    
    
    	*)
    		break
    	;;
    	esac
    else 
    	echo "虛拟機不存在,請重新輸入!"
    		
    	continue
    fi
done
}

snap_delete(){
	
     virsh list --all
    read -p "請輸入删除快照的虛拟機: " vm_name
	virsh snapshot-list $vm_name 
	read -p "請輸入删除快照的虛拟機: " vm_snap
	virsh snapshot-delete $vm_name --snapshotname $vm_snap
	virsh snapshot-list $vm_name



    


    
}


           

繼續閱讀