External(and Live)snapshots with libvirt

10243 ワード

http://kashyapc.com/
Raw image is a blob of data exposed directly in VM as block device,it can't snapshot.qem-i mg is able to imposed data from raw image into qcow 2 image,and save it as snapshot
Previouslogy,I posted about s snapshots here、which breieflydiscussed different types of snapshots.In this post,let’s explore how external sssshotswork. Just to quickreshsh、emamash thethethethethemamaststststststststststststininininininininine thethethethethethethethethethethethethethemamamamamamamash ssssssssssssssssssssssssssssssssssssssssence/delta(aka,the snapshot image)is stored in a new QCOW 2 file.Onece the snapshot is taken,the original disk mage will be in a‘read-only’state,which can be used as backing file for other ests.
It’s wortth mentioning here that:
  • The original disk image can be either in RAW format or QCOW 2 format.When a snapsphot is tane,the difference'will be stored in a different QCOW 2 file
  • The virtual machine has to be running、ライブ.Also with Live snapshots、no gust dontime is experienced when a snapshot is taken.
  • At this moment,external(Live)snapshots work for'disk-only'snapshots(and not VM state).Work for both disk and VM state(and also,reverting to external disky.inpshot state)insp.infont.infont.infont.infont.infont.infont.infont.infont.infont.infont.infont.ings.infont.in
    Before we go aead、here’s some version info、I’m testing on Fedora-17(host)、and the gest(named‘daisy’)is running Fedora-18(Test Compse):
    
    [root@moon ~]# rpm -q libvirt qemu-kvm ; uname -r
    libvirt-0.10.1-3.fc17.x86_64
    qemu-kvm-1.2-0.2.20120806git3e430569.fc17.x86_64
    3.5.2-3.fc17.x86_64
    [root@moon ~]# 
    
    External disk-snapshots(live)using QCOW 2 as original mage:Let’s see an illustration of external(live)disk-only snapshots.First,let’s ensure the gust is running:
    
    [root@moon qemu]# virsh list
     Id    Name                           State
    ----------------------------------------------------
     3     daisy                          running
    
    
    [root@moon qemu]# 
    
    The n、list all the block devices assicated with the gusest:
    
    [root@moon ~]# virsh domblklist daisy --details
    Type       Device     Target     Source
    ------------------------------------------------
    file       disk       vda        /export/vmimgs/daisy.qcow2
    
    [root@moon ~]# 
    
    Next、let’s create a snapshot(disk-only)of the gust this way、while the gust is running:
    
    [root@moon ~]# virsh snapshot-create-as daisy snap1-daisy "snap1 description" \
     --diskspec vda,file=/export/vmimgs/snap1-daisy.qcow2 --disk-only --atomic
    
    Some details of the flags used:-Passing a'–diskspec’parameter adds the'disk'elemens to the Snapshot XML file-'–disk-only parameter,tas the snapshot of onlythe diskes-the the the the the the magenese'jec
    Let’s check the information aout the just taken snapshot by running qem-i mg:
    
    [root@moon ~]# qemu-img info /export/vmimgs/snap1-daisy.qcow2 
    image: /export/vmimgs/snap1-daisy.qcow2
    file format: qcow2
    virtual size: 20G (21474836480 bytes)
    disk size: 2.5M
    cluster_size: 65536
    backing file: /export/vmimgs/daisy.qcow2
    [root@moon qemu]# 
    
    Appt from the above,I created 2 more snapshots(just the same sysntax as above)for illustration purpose.Now,the snapshot-tree look s like this:
    
    [root@moon ~]# virsh snapshot-list daisy --tree
    
    snap1-daisy
      |
      +- snap2-daisy
          |
          +- snap3-daisy
            
    
    [root@moon ~]# 
    
    For the above example image file chain[base]<-snap 1<-snap 3],it has to be read as–snap 3 has snap 2 as its backing file,snap 2 has snap 1 asits backing file,and snap 1 file
    
    #--------------------------------------------#
    [root@moon ~]# qemu-img info /export/vmimgs/snap3-daisy.qcow2
    image: /export/vmimgs/snap3-daisy.qcow2
    file format: qcow2
    virtual size: 20G (21474836480 bytes)
    disk size: 129M
    cluster_size: 65536
    backing file: /export/vmimgs/snap2-daisy.qcow2
    #--------------------------------------------#
    [root@moon ~]# qemu-img info /export/vmimgs/snap2-daisy.qcow2
    image: /export/vmimgs/snap2-daisy.qcow2
    file format: qcow2
    virtual size: 20G (21474836480 bytes)
    disk size: 3.6M
    cluster_size: 65536
    backing file: /export/vmimgs/snap1-daisy.qcow2
    #--------------------------------------------#
    [root@moon ~]# qemu-img info /export/vmimgs/snap1-daisy.qcow2
    image: /export/vmimgs/snap1-daisy.qcow2
    file format: qcow2
    virtual size: 20G (21474836480 bytes)
    disk size: 2.5M
    cluster_size: 65536
    backing file: /export/vmimgs/daisy.qcow2
    [root@moon ~]#
    #--------------------------------------------#
    
    Now、if we do not need snap 2 any more、and want to pull the data from snap 1 into snap 3、making snap 1 a s snap 3’s backing file、we can do a virsh blockpuration as below:
    
    #--------------------------------------------#
    [root@moon ~]# virsh blockpull --domain daisy  --path /export/vmimgs/snap3-daisy.qcow2 \
     --base /export/vmimgs/snap1-daisy.qcow2 --wait --verbose
    Block Pull: [100 %]
    Pull complete
    #--------------------------------------------#
    
    Where、–path=path to the snapshot file、and–base=path to a backing file from whihich the datato to be putled.So from abooeexample、it’s e e e e e e e e e evident that t aaaaaat the pplling the dadadadadadadadadadadadataffffrom sfffrom snapsnapppnapppnapppppppppnananapppppppppppppppnanatitititishshshshshshshshshshshshshshshshshshshshshnning qem-i mg again.Thing to note here
    
    [root@moon ~]# qemu-img info /export/vmimgs/snap3-daisy.qcow2 
    image: /export/vmimgs/snap3-daisy.qcow2
    file format: qcow2
    virtual size: 20G (21474836480 bytes)
    disk size: 145M
    cluster_size: 65536
    backing file: /export/vmimgs/snap1-daisy.qcow2
    [root@moon ~]# 
    
    A couple of things to note here、after discussion with Erric Blake(thank you):
    -If we do a listing of the snapshot tree again(now that‘snap 2-daisy.qcow 2’backing file is no more in use)、
    
    [root@moon ~]# virsh snapshot-list daisy --tree
    snap1-daisy
      |
      +- snap2-daisy
          |
          +- snap3-daisy
    [root@moon ~]#
    
    might wonder、why is s snap 3 still poininininingto snap 2?Thing to note hee is、the abobove isthe snapshot chain、which isindededededededededededededefm discaaatdisssssbacking file chain.So、the、the、the'virsrsrss the's the's the's the's the's the's the's s s s s s s s s s s s thethethe the the the the the the the the the thethethethe isisisisisisisisisisisininininininininer creating the snapshot)So,from the above snapshot tree,if we we to revert to snap 1 or snap 2(when revert-to-disk-snapshots is available)、it'd still be possible to do that,meaning:
    It’s possible to go from this state:base<-snap 123(data from snap 1,snap 2 pulled into snap 3)
    we can still revert to:
    base<-snap 1(thus undong the changes in snap 2&snap 3)
    External disk-snapshots(ライブ)using RAW as original mage:With external disk-snapshots,the backing file can be RAW as well
    A quick illustration below.The command are self-explannary.It can be noted the change(from RAW to QCOW 2)in the block disk disk the Grest,before&after Taning the disk-snapshot
    
    #-------------------------------------------------#
    [root@moon ~]# virsh list | grep f17btrfs2
     7     f17btrfs2                      running
    [root@moon ~]#
    #-------------------------------------------------#
    [root@moon ~]# qemu-img info /export/vmimgs/f17btrfs2.img                                                                                                                                                      
    image: /export/vmimgs/f17btrfs2.img
    file format: raw
    virtual size: 20G (21474836480 bytes)
    disk size: 1.5G
    [root@moon ~]# 
    #-------------------------------------------------#
    [root@moon qemu]# virsh domblklist f17btrfs2 --details
    Type       Device     Target     Source
    ------------------------------------------------
    file       disk       hda        /export/vmimgs/f17btrfs2.img
    
    [root@moon qemu]# 
    #-------------------------------------------------#
    [root@moon qemu]# virsh snapshot-create-as f17btrfs2 snap1-f17btrfs2 "snap1-f17btrfs2-description" \
    --diskspec hda,file=/export/vmimgs/snap1-f17btrfs2.qcow2 --disk-only --atomic
    Domain snapshot snap1-f17btrfs2 created
    [root@moon qemu]# 
    #-------------------------------------------------#
    [root@moon qemu]# qemu-img info /export/vmimgs/snap1-f17btrfs2.qcow2 
    image: /export/vmimgs/snap1-f17btrfs2.qcow2
    file format: qcow2
    virtual size: 20G (21474836480 bytes)
    disk size: 196K
    cluster_size: 65536
    backing file: /export/vmimgs/f17btrfs2.img
    [root@moon qemu]# 
    #-------------------------------------------------#
    [root@moon qemu]# virsh domblklist f17btrfs2 --details
    Type       Device     Target     Source
    ------------------------------------------------
    file       disk       hda        /export/vmimgs/snap1-f17btrfs2.qcow2
    [root@moon qemu]# 
    #-------------------------------------------------#
    
    Also note:All snapshot XML files,where libvirt trocs the metadata of snapshots are located under/var/libvirt/qem/snapshots/USgust name