Linux menu

Wednesday, September 17, 2014

LVM snapshots with ext4 In Linux

LVM is a logical volume manager for the Linux kernel that manages disk drives and similar mass-storage devices. These logical volumes can be composed from different physical hard disks, but will appear as one partition on which a file system can be installed.
LVM snapshot can be used as a complete backup of your volume. It will allow you to restore the full volume in case something goes wrong. Centos 7 release made major changes on snaphots to make it more perfect

Creating the snapshot

You can create a new snapshot using this command:
# lvcreate –size 1G –snapshot –name nameofthesnapshot /dev/mapper/nameoflvm
For example:
lvm create
The LVM snapshots doesn’t contain any data initially, it will only store changes you make to the original volume, this will save a lot of space. Because the snapshot increases in size as the origin volume changes, it is important to monitor the percentage of the snapshot volume regularly with the lvs command to be sure it does not fill. A snapshot that is 100% full is lost completely, as a write to unchanged parts of the origin would be unable to succeed without corrupting the snapshot.
After you create the snapshot it will be displayed in the output of the lvdisplay command like this:
lvm ss1

Using the LVM snapshot

You can now use the lvm snapshot as a normal partition, you can mount it in a folder, make changes to it and if you are satisfied with the changes you can merge the changes to the original volume.
To mount it you just need to create a folder and use the mount command like this:
# mkdir /mnt/ss1
# mount /dev/centos/ss1 /mnt/ss1
Now you can test anything you wish on the /mnt/ss1 folder without changing the original system.
If you are satisfied with the changes you have made to the snapshot and wish to merge them in the original volume you can use the lvconvert with the –merge attribute like in the example below:
lvm merge
As you can see if the original volume is mounted, you will have to reboot the system for the changes to take effect.
You can also remove the snapshot if you don’t need the changes in it or don’t have any more uses for it with the following command:
lvremove /dev/centos/ss1
As you can see using LVM snapshots is a great way to test and experiment different changes to the system in total safety.

No comments: