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
tcpdump usage examples
Viewed 666 times since Fri, Jul 27, 2018
20 Practical Examples of RPM Commands in Linux rpm
Viewed 1454 times since Mon, Feb 18, 2019
How to create stunnel with systemd? stunnel
Viewed 1340 times since Thu, Jan 16, 2020
RHEL: XFS basic operations
Viewed 1727 times since Sat, Jun 2, 2018
Logowanie za pomocą kluczy Secure Shell
Viewed 1314 times since Thu, May 24, 2018
WatchDog watchdog.sh script for checking server running
Viewed 2383 times since Tue, Jul 31, 2018
RHEL: Force system to prompt for password in Single User mode
Viewed 2362 times since Sat, Jun 2, 2018
What is yum-cron ?
Viewed 1522 times since Fri, Oct 26, 2018
Setting up encrypted tunnel using stunnel
Viewed 667 times since Fri, Sep 28, 2018
Index » Community Contributions » System encryption using LUKS and GPG encrypted keys for arch linux
Viewed 809 times since Fri, Jul 13, 2018