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
RHCS6: Create a new Logical Volume / Global Filesystem 2 (GFS2)
Viewed 2397 times since Sun, Jun 3, 2018
What UUIDs can do for you
Viewed 1904 times since Tue, Jul 17, 2018
Create a Linux Swap File
Viewed 3170 times since Fri, Jun 8, 2018
7 Tips – Tuning Command Line History in Bash
Viewed 5600 times since Fri, Jul 5, 2019
SSH: Execute Remote Command or Script – Linux
Viewed 2553 times since Mon, Feb 18, 2019
awk printf
Viewed 15519 times since Wed, Aug 19, 2020
10 nmap Commands Every Sysadmin Should Know
Viewed 10100 times since Wed, May 22, 2019
systemd Auto-restart a crashed service in systemd
Viewed 3410 times since Fri, Jan 17, 2020
Migrate a Linux System from Red Hat Enterprise to CentOS
Viewed 10476 times since Fri, May 15, 2020
Linux: how to monitor the nofile limit
Viewed 10824 times since Wed, Jul 25, 2018