How to Take LVM Snapshot in Linux & Verify It

LVM Snapshots are the point-in-time copy of your logical volumes and can be used to restore the data to that particular time when the snapshot was taken. In this article, we will see how to take LVM snapshot, mount it and verify the data. This article is very easy to understand and you can follow along.

 

How to Take LVM Snapshot RHEL Linux

 

For this tutorial, we are using RHEL 7.4 (Maipo) and EXT4 filesystem instead of XFS filesystem. We have put some test files from /etc/ directory for our testing and this tutorial.

[root@master ~]# uname -a
Linux master 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@master ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@master ~]#

 

Steps To Create LVM Snapshot in Linux

 

1. First of all, let’s create an EXT4 filesystem and mount it.

 

[root@master ~]# pvcreate /dev/sdai
  Physical volume "/dev/sdai" successfully created.

[root@master ~]# vgcreate vg1 /dev/sdai
  Volume group "vg1" successfully created
[root@master ~]#
[root@master ~]# vgs
  VG             #PV #LV #SN Attr   VSize   VFree
  rhel_master   1   2   0 wz--n- <39.00g     0
  vg1              1   0   0 wz--n-  <5.00g <5.00g
[root@master ~]#
[root@master ~]# lvcreate -L 1G -n lv1 vg1
  Logical volume "lv1" created.
[root@master ~]# lvs
  LV   VG             Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel_master -wi-ao---- <35.00g
  swap rhel_master -wi-ao----   4.00g
  lv1  vg1            -wi-a-----   1.00g

[root@master ~]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
0 blocks (0.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@master ~]#
[root@master ~]# mkdir -p /test
[root@master ~]# mount /dev/mapper/vg1-lv1 /test

So, now we have created the EXT4 filesystem and mounted it on /test directory. Now, let's copy some data inside our test directory.

[root@master ~]# cd /test
[root@master test]# cp -r /etc/* .
[root@master test]#
[root@master test]# df -hP /test
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lv1  976M  378M  583M  40% /test
[root@master test]#

 

2. Now, let's create LVM snapshot of this newly created volume. Here we are going to use 'lvcreate' command with option '-s' which is for the snapshot.

We will create exactly the same size snapshot as our logical volume.

[root@master ~]# lvcreate -s -n lv1_snap -L 1G /dev/mapper/vg1-lv1
  Using default stripesize 64.00 KiB.
  Logical volume "lv1_snap" created.

[root@master ~]# ls -lh /dev/mapper/vg1-lv*
lrwxrwxrwx. 1 root root 7 Jul 19 18:27 /dev/mapper/vg1-lv1 -> ../dm-2
lrwxrwxrwx. 1 root root 7 Jul 19 18:27 /dev/mapper/vg1-lv1-real -> ../dm-3
lrwxrwxrwx. 1 root root 7 Jul 19 18:27 /dev/mapper/vg1-lv1_snap -> ../dm-5
lrwxrwxrwx. 1 root root 7 Jul 19 18:27 /dev/mapper/vg1-lv1_snap-cow -> ../dm-4

[root@master ~]# lvdisplay /dev/vg1/lv1_snap
  --- Logical volume ---
  LV Path                /dev/vg1/lv1_snap
  LV Name                lv1_snap
  VG Name                vg1
  LV UUID                Ut5H34-ETDD-bTdU-tvaG-0SYk-AGxZ-F1RtTx
  LV Write Access        read/write
  LV Creation host, time master, 2018-07-19 18:27:31 -0700
 LV snapshot status     active destination for lv1
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  COW-table size         1.00 GiB
  COW-table LE           256
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:5

[root@master ~]# lvdisplay /dev/vg1/lv1
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                XR891T-bcjI-z9AC-Eiot-FKQw-Dlc7-XllGfV
  LV Write Access        read/write
  LV Creation host, time master, 2018-07-19 18:15:52 -0700
 LV snapshot status     source of lv1_snap [active]
  LV Status              available
  # open                 1
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

Just, for now, know that when we create our LVM snapshot, four dm devices are used as shown in above output. And COW stands for copy-on-write. If you will need, we can create a separate article on COW and these 4 DM devices, just let us know through your comments.

 

Verify LVM Snapshot in Linux

 

3. This is it! We have created the snapshot of our block device, now let's mount it and verify it.

[root@master ~]# mount /dev/mapper/vg1-lv1_snap /test_snap
[root@master ~]# cd /test_snap
[root@master test_snap]# df -hP /test_snap/
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lv1_snap  976M  378M  583M  40% /test_snap
[root@master test_snap]#
[root@master test_snap]# df -hP /test
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg1-lv1  976M  378M  583M  40% /test
[root@master test_snap]#

As we can see both the mount point has the same utilization, so can say that our "lvm snapshot" is successfully created and verified.

Now, in case of any filesystem crash or loss of any file, you can copy the data from this snapshot mount point. Please note that the main objective of a snapshot is not for backup purpose and you should always use backup software like NetBackup and Avamar in your enterprise environment.

We can also use command "lvconvert" with option '- -merge' to merge this snapshot with its original volume after which this snapshot gets deleted.

LVM snapshot is only good if you are going to use them immediately if required like if you are doing OS or application upgrade or doing storage migration activity.


Hope this basic article on creating LVM snapshot of logical volumes is helpful for you and will help you to further excel in the Linux field. Do let us know what are your thoughts on this "Storage Tutorial" and what tutorial you would like to see next.

If you want us to send FREE notification on your email, do subscribe to Storage Tutorial and also follow us on social media channels.

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy Preference Center