LVM: Managing snapshots

LVM: Managing snapshots

# Tested on RHEL 6 & 7

# LVM allows to take read/write snapshots of a logical volume. An snapshot consists of a
# point-in-time copy of a logical volume that can be written to independently from the
# original volume.

# They can be used for backup purposes or test changes, for example.

# When a snapshot is created it points to the same block on disk as the original but when
# a change is made to either the original or the snapshot, it will be copied and snapshot
# will point to the copy instead.

# To store changes a snapshot is given an initial space. Once it has used all the
# available space it will be turned 'inactive'. To avoid this, we can configure snapshot
# to automatically extend the space available when a defined threshold is reached.

# We can define following variables in /etc/lvm/lvm.conf configuration file:
#     snapshot_autoextend_threshold
#     snapshot_autoextend_percent
#
# When usage goes above 'snapshot_autoextend_threshold' percent, it will be grown by
# 'snapshot_autoextend_percent' of the original size. Once snapshot has reached the same
# size as the original logical volume it will stop being extended.
#
# lvm2-monitor service has to be restarted for changes to take effect


# Creating the snapshot of an existing logical volume

LV=lv_test         # Our existing logical volume
SNAP=lv_snapshot  
# Snapshot
VG=datavg          # Volume group our logical volume belongs to
SIZE=256M          # Desired size for the snapshot - do your calculations before ;)

lvcreate -s -n $SNAP -L $SIZE /dev/$VG/$LV


# To check how much space has been used in a snapshot, we can use lvs

lvs
  LV          VG     Attr      LSize     Pool   Origin    Data%  Move Log Cpy%Sync Convert
  [...]
  lv_snapshot rootvg swi-a-s-- 256.00m          lv_test   0.01



# Snapshot cat be mounted to make modifications

mount /dev/$VG/$SNAP /mnt


# Once modifications done, we can merge changes to origin logical volume (both original
# lv and snapshot should be unmounted, if not next time lv will be de-activated and
# re-activated it will be synchronized). If changes may be discarded we can 'lvremove'
# snapshot

# NOTE: Use snapshot merging carefully; merging will discard any change done to original
# logical volume after taking the snapshot
# Apart from that, one has to take into account that merging logical volumes will
# definitely destroy snapshot.

umount /dev/$VG/$LV
umount /mnt

lvconvert --merge /dev/$VG/$SNAP
  Merging of volume lv_snapshot started.
  lv_test: Merged: 100.0%
  lv_test: Merged: 100.0%
  Merge of snapshot into logical volume lv_test has finished.
  Logical volume "lv_snapshot" successfully removed


# We may run merge in the background ('-b'). If the original volume is in use, the merge
# will be started next time the original volume is activated. Obviously merging a snapshot
# of the root file system requires a reboot of the server




# Example of snapshot
# ------------------------------------------------------------------------------------------

# lvs rootvg/lv_test
  LV      VG     Attr      LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_test rootvg -wi-ao--- 256.00m

# ll /test/
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found

# lvcreate -s -n lv_snapshot -L 32M /dev/rootvg/lv_test
  Logical volume "lv_snapshot" created

# mount /dev/rootvg/lv_snapshot /mnt

# ll /mnt/
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found

# touch /mnt/3.txt
# touch /mnt/4.txt

# touch /test/5.txt

# ll /test/
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 5.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found

# ll /mnt/
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 3.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 4.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found

# lvs rootvg/lv_test rootvg/lv_snapshot
  LV          VG     Attr      LSize   Pool Origin  Data%  Move Log Cpy%Sync Convert
  lv_snapshot rootvg swi-aos--  32.00m      lv_test   0.12
  lv_test     rootvg owi-aos-- 256.00m

# umount /test
# umount /mnt

# lvconvert --merge /dev/rootvg/lv_snapshot
  Merging of volume lv_snapshot started.
  lv_test: Merged: 100.0%
  Merge of snapshot into logical volume lv_test has finished.
  Logical volume "lv_snapshot" successfully removed

# lvs rootvg/lv_test rootvg/lv_snapshot
  One or more specified logical volume(s) not found.
  LV      VG     Attr      LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_test rootvg -wi-a---- 256.00m

# mount /dev/rootvg/lv_test /test

# ll /test
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 3.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 4.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found


# Files 3.txt and 4.txt that were created on snapshot have been merged to original LV.
# On the other hand file 5.txt, that was created on original LV after having taken the
# snapshot, has been discarded during merge operation.
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Get UUID of Hard Disks [Update]
Viewed 2278 times since Tue, Jul 17, 2018
CentOS / RHEL : Configure yum automatic updates with yum-cron service
Viewed 3583 times since Fri, Oct 26, 2018
RHCS6: Clustered LVM
Viewed 2333 times since Sun, Jun 3, 2018
linux-training.be gives you books for free to study Linux
Viewed 5006 times since Sat, Jun 2, 2018
How to accurately determine when the system was booted
Viewed 2377 times since Wed, Oct 3, 2018
RHEL: Remove existing SAN LUNs
Viewed 14182 times since Sat, Jun 2, 2018
ubuntu How to Reset Forgotten Passwords in Ubuntu 16.04
Viewed 3466 times since Tue, Dec 8, 2020
WatchDog watchdog.sh script for checking server running
Viewed 5468 times since Tue, Jul 31, 2018
Build a simple RPM that packages a single file
Viewed 8627 times since Sat, Jun 2, 2018
LVM: Remove a Filesystem / Logical Volume
Viewed 2654 times since Sat, Jun 2, 2018