Monday, 17 June 2013

Logical Volume Snapshots

Objective:  Creating and restoring manual logical volume snapshots.

Environment: CentOS release 6.3 (32-bit).

Descriptions: 
By LV snapshot you will be able to freeze your logical volumes. In other words, you can easily backup and rollback to a original logical volume state. This is almost similar to VMware where you can the snap shot of the VM and revert in-case if anything goes wrong.


Concept of snapshot are just like the symbolic links, where you don't create a file, instead you only reference to it. Here, two essential parts are 
1. Metadata.

2. Data blocks.

When a snapshot is created Logical Volume Manager simply creates a copy of all Metadata pointers to a separate logical volume, snapshot volume only starts grow once you start altering data of the original logical volume.

Implementation:
Since my server was not created using LVM, I have created two partitions and changed them to LVM.

Change plan:
1. Create two partitions on /dev/sda drive.
2. Create physical volumes on the two drives.
3. Create volume group.
4. Create a single logical volume using ext3 file system.
5. Take snapshot and remove data
6. Rollback logical volume snapshot

Technical Implementation:

1. Created two physical partitions:

tux#fdisk -l | tail -2
/dev/sda5            2305        2366      497983+  8e  Linux LVM
/dev/sda6            2367        2428      497983+  8e  Linux LVM
tux#

2. Created Physical Volumes:

tux#pvcreate /dev/sda[5-6]
  Physical volume "/dev/sda5" successfully created
  Physical volume "/dev/sda6" successfully created
tux#

3. Created volume groups:
tux#vgcreate vol_grp /dev/sda5 /dev/sda6
  /dev/hdc: open failed: No medium found
  Volume group "vol_grp" successfully created
tux#

4. Created single logical volume of 100Mb with ext3 file system.

tux#lvcreate -L 100M -n lv1 vol_grp
  Logical volume "lv1" created
tux#

tux#mkfs.ext3 /dev/vol_grp/lv1
mke2fs 1.39 (29-May-2006)
.
.
tux#

Finally, we have come to the point where we can take a snapshot of our logical volume, for this we will also need some sample data on our Logical Volume, so once we revert from the snapshot we can confirm entire process by comparing original data with data recovered from the snapshot.

Create a mount directory for the logical volume and mount it.

tux#mkdir /mnt/lv1
tux#mount /dev/vol_grp/lv1 /mnt/lv1
tux#

tux:/mnt/lv1#cp -r /sbin/ .
tux:/mnt/lv1#cp -r /bin/ .
tux:/mnt/lv1#du -s .
39312   .
tux:/mnt/lv1#

5. Creating the LV snapshot.

tux:/mnt/lv1]#lvcreate -s -L 30M -n lv1_snapshot /dev/vol_grp/lv1
  Rounding up size to full physical extent 32.00 MB
  Logical volume "lv1_snapshot" created
tux:/mnt/lv1]#

Execute lvs to confirm that new volume snapshot has been created.

tux:/mnt/lv1]#lvs
  LV           VG      Attr   LSize   Origin Snap%  Move Log Copy%
  lv1          vol_grp owi-ao 100.00M
  lv1_snapshot vol_grp swi-a-  32.00M lv1      0.07
tux:/mnt/lv1]#

Since the snapshot was already created, now you can alter the data on the volume group.

tux:/mnt/lv1]#rm -rf ./bin/
tux:/mnt/lv1]#rm -rf ./sbin/
tux:/mnt/lv1]#du -s .
13      .
tux:/mnt/lv1]#

6. Rollback logical volume snapshot.

tux:/mnt/lv1]# lvconvert --merge /dev/vol_grp/lv1_snapshot
  Can't merge over open origin volume
  Merging of snapshot lv1_snapshot will start next activation.
tux:/mnt/lv1]#

tux#umount /mnt/lv1

Deactivate and activate you volume:

tux# lvchange -a n /dev/vol_grp/lv1
tux# lvchange -a y /dev/vol_grp/lv1

As a last step mount again your logical volume "lv1" and confirm that data all has been recovered:

tux# mount /dev/vol_grp/lv1 /mnt/lv1
tux:/mnt/lv1]# ls
bin  lost+found  sbin
tux:/mnt/lv1]#
tux# du -s /mnt/lv1/
39312   .

Snapshot was rolled back.

Objective successful