Monday, 25 March 2013

RHCSA: Creating Logical Volume Snapshots

The concept of logical volumes was a bit tough for me to wrap my head around initially, but the same as with many things in this world, all it takes is a little practice. I've blogged previously about the concepts of logical volume management here, here's a quick how-to explaining how we can create a snapshot of a logical volume. Snapshot logical volumes are a useful feature of LVM storage. An LVM snapshot is connected to another volume, temporarily preserving changed data. The snapshot provides a self-consistent previous image of the original volume so that its data can be backed up in a consistent state.

To start off with, let's take a look and make sure we have enough space to create this snapshot volume. Let's use the pvdisplay command:

 [root@centos ~]# pvdisplay   
  --- Physical volume ---  
  PV Name        /dev/sda2  
  VG Name        vg_centos  
  PV Size        100.00 GiB / not usable 4.00 MiB  
  Allocatable      yes   
  PE Size        4.00 MiB  
  Total PE       25599  
  Free PE        14335  
  Allocated PE     11264  
  PV UUID        SZ8KN0-OQ8Z-CHcM-R0SH-k1eO-jQAX-1bbNKa  

On this system we only have a single physical volume. This is where all of our volume groups, the container for our logical volumes, sits on top of. This is broken up into 25599 * 4MB chunks, known as physical extents (PE) . We can see we have 11264 PE used up already but we still have plenty left. We could make a new volume group here if we need to. Let's take a look at our volume groups now, this time using the vgdisplay command:

 [root@centos ~]# vgdisplay   
  --- Volume group ---  
  VG Name        vg_centos  
  System ID         
  Format        lvm2  
  Metadata Areas    1  
  Metadata Sequence No 4  
  VG Access       read/write  
  VG Status       resizable  
  MAX LV        0  
  Cur LV        3  
  Open LV        3  
  Max PV        0  
  Cur PV        1  
  Act PV        1  
  VG Size        100.00 GiB  
  PE Size        4.00 MiB  
  Total PE       25599  
  Alloc PE / Size    11264 / 44.00 GiB  
  Free PE / Size    14335 / 56.00 GiB  
  VG UUID        AhQMcj-dHAa-zd6a-Dq4c-ymOl-qBOx-4XxDI8  

Looking again at the "Free PE" we can see we have 14335 of those 4MB physical extents available to us, ~56GB. Let's check out the logical volume for the /home directory, using lvdisplay:

 [root@centos ~]# lvdisplay /dev/vg_centos/lv_home  
  --- Logical volume ---  
  LV Path        /dev/vg_centos/lv_home  
  LV Name        lv_home  
  VG Name        vg_centos  
  LV UUID        a19hzZ-llnX-3hdb-dCQM-NTPR-n4gZ-T90ANE  
  LV Write Access    read/write  
  LV Creation host, time centos, 2013-03-23 23:39:38 +0000  
  LV Status       available  
  # open         1  
  LV Size        20.00 GiB  
  Current LE       5120  
  Segments        1  
  Allocation       inherit  
  Read ahead sectors   auto  
  - currently set to   256  
  Block device      253:2  

We seem to have more than enough space left in our volume group for our snapshot requirements. The whole of the lv_home logical volume is 20GB. Red Hat's official documentation suggests around a snapshot of ~3-5% of an origin whch has rarely changing data. In this case, I'm creating a snapshot for the volume that houses the /home directories of my users. I would expect that to change fairly frequently. I've got plenty of space in the volume group so I'm going to go for 25% and create a  5GB snapshot.

Let's create our 5GB snapshot of the lv_home logical volume:

 [root@centos ~]# lvcreate -s -n lv_home_snap -L 5G /dev/vg_centos/lv_home  
  Logical volume "lv_home_snap" created  

Let's take a good look at this command. We create a new logical volume, using the lvcreate command. We use the -s option to make it a snapshot. We name our new logical volume with the  -n option, here I've called it lv_home_snap. I've chosen to specify the size I want the snapshot logical volume to be here with the -L option, stating 5G for 5 gigabytes. Finally, I've specifed the logical volume that I want to use at the origin of my snapshot volume.

Let's check to see if it has been created to our requirements, with the lvs command:

 [root@centos mugwriter]# lvs  
  LV      VG    Attr   LSize Pool Origin Data% Move Log Cpy%Sync Convert  
  lv_home   vg_centos owi-aos-- 20.00g                         
  lv_home_snap vg_centos swi-a-s-- 5.00g   lv_home  0.21               
  lv_root   vg_centos -wi-ao--- 20.00g                         
  lv_swap   vg_centos -wi-ao--- 4.00g      

There it is on the second line. Apologies for the formatting here, in the terminal it shows lv_home under the "Origin" column so you can see that it is a snapshot of the lv_home logical volume.

Let's test our snapshot to make sure its working. I've created a copy of the /etc directory and dropped it in my home folder:
 [root@centos mugwriter]# ls -l  
 total 8156  
 drwxr-xr-x. 107 root root  12288 Mar 24 22:45 etc  
 -rw-r--r--.  1 root root 8338615 Mar 24 23:14 tar.etc.tgz  
 [root@centos mugwriter]#   

If our snapshot is working correctly, we should be able to delete any of these. But, oops, I deleted the whole  of my normal user's home directory!

 [root@centos home]# ls  
 lost+found mugrwriter  
 [root@centos home]# rm -rf mugwriter/  

Luckily, as the systems administator here I can restore this from my snapshot volume.

 [root@centos home]#mkdir /snapmount  
 [root@centos home]# mount /dev/vg_centos/lv_home_snap /snapmount/  
 root@centos home]# ls -l /snapmount/  
 total 20  
 drwx------. 2 root root 16384 Mar 23 23:39 lost+found  
 drwx------. 4 mugwriter mugwriter 4096 Mar 24 09:32 mugwriter  
 [root@centos home]# cp -r /snapmount/mugwriter/ /home  
 [root@centos home]# ls -l /home
 total 20  
 drwx------. 2 root root 16384 Mar 23 23:39 lost+found  
 drwx------. 4 root root 4096 Mar 25 00:11 mugwriter 

So that's how we create a snapshot logical volume. Hopefully you won't ever accidentally delete a user's entire home directory, but if you do you can restore it before somebody notices.

1 comment:

  1. An interesting first foray into snapshots. You need to look deeper into what you're doing though as you've very much broken mr mugwriters home directory here. In your last code snippet, look at the file and directory permissions. Good luck logging in.

    Also, to complete your snapshot knowledge, what would happen if a user came in and created a 15GB file in their home directory?