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
Turbocharge PuTTY with 12 Powerful Add-Ons – Software for Geeks #3
Viewed 10977 times since Sun, Sep 30, 2018
RHEL: Bonding network interfaces
Viewed 1871 times since Sat, Jun 2, 2018
12 Tcpdump Commands – A Network Sniffer Tool
Viewed 7400 times since Fri, Jul 27, 2018
How to Synchronize Directories Using Lsyncd in Linux
Viewed 9949 times since Wed, Oct 31, 2018
Get UUID of Hard Disks [Update]
Viewed 1179 times since Tue, Jul 17, 2018
LVM: Reduce root PV/VG
Viewed 3416 times since Sat, Jun 2, 2018
systemctl Use systemd to Start a Linux Service at Boot
Viewed 3994 times since Mon, Dec 7, 2020
Red Hat 8 How to Set Up Automatic Updates for CentOS 8
Viewed 1519 times since Fri, Sep 25, 2020
RHEL: Scan and configure new SAN (fibre channel) LUNs
Viewed 5576 times since Sun, May 27, 2018
Prosty skaner portów TCP w bash
Viewed 1660 times since Thu, May 24, 2018