LVM Snapshots

This article reviews how to use LVM for creating snapshots. This is a very powerful tool that should be in the arsenal of all users and admins. It is straightforward to use but you need to make sure you understand the interactions with the specific file system(s). Just be sure you have enough space on your VG to store the snapshot (and don't forget to erase it when you are done).

If you have used LVM to create LV’s and then built file systems on those LV’s. It’s very simple to create a snapshot of the file system using LVM. The key factor in the process is that you need to make sure that you have enough space on a VG that hasn’t been allocated that can be used by an LV (the snapshot LV). You should not create an LV (Logical Volume) prior to the snapshot because during the snapshot process you will be creating the LV using the lvcreate command.

You can check how much space is used for the snapshot by using the command lvdisplay. It's a good practice to plan for the worse and allocate the same amount of space as the original file system - just to be sure. You can always move the snapshot or erase it if you need the space back.

Each file system, ext2, ext3, xfs, reiserfs, jfs, etc. has it's own unique behavior when a snapshot is taken using LVM. It is recommended that you experiment with your file system of choice on a small scale before committing it to production. For example, when taking a snapshot of an xfs file system, you should run a command, "xfs_freeze" just prior to taking the snapshot. Also, ext3 file systems like to be mounted as read-write after a snapshot so the journal can be replayed (most of the time, a snapshot is mounted read-only so that backups of the snapshot don't have to worry about inadvertent writing).


LVM Concepts for Snapshots

 

The snaA snapshot volume is a special type of volume that presents all the data that was in the volume at the time the snapshot was created

Snapshot will be taken using the remaining space in the VG. In this case, the remaining space available is the same as that allocated to /home. Also, as noted in the figure, the file system for /home is ext3.

For a simple test case, the 2.6.30 kernel has been copied to /home/user5. The space used in /home is,

# du -sh /home
200M    /home

So there is 463 MB of space used in the file system built on top of LVM. So the snapshot should be slightly larger than this.

Now, let's take the snapshot using "lvcreate".

# lvcreate -L200M -s -n home_backup_160309 /dev/diskvg/home_lv
  Logical volume "home_backup_160309" created

The option "-s" tells lvcreate to take a snapshot. The command creates a new LV, home_backup_160309, on the VG, diskvg. The snapshot is of the LV /dev/diskvg/home_lv. To see if the LV was created successfully, just use "lvscan".

# lvscan
  ACTIVE   Original '/dev/diskvg/home_lv' [200.00 GB] inherit
  ACTIVE   Snapshot '/dev/diskvg/home_backup_160309' [200.00 MB] inherit

You can see the snapshot (it's even labeled "Snapshot") and the full path to the snapshot.

If you want to make a backup of the snapshot (one of the primary reasons for making a snapshot) then you need to mount it.

# mkdir /mnt/backup/home_backup_160309
# mount /dev/diskvg/home_lv/home_backup_0160309 /mnt/backup/home_backup_160309
# mount
...
/dev/mapper/diskvg-home_lv on /home type ext3 (rw,data=ordered)
/dev/mapper/disk_vg-home_backup_080309 on /mnt/backup/home_backup_160309 type ext3 (rw)

It's always a good idea to check the mounted snapshot to make sure it's there.

# ls -s /mnt/backup/home_backup_160309/
total 20
16 lost+found   4 user5
# ls -s /mnt/backup/home_backup_160309/user5
total 74080
    4 linux-2.6.30  74076 linux-2.6.30.tar.gz

Everything is there and is ready to be backed up. If you like, you could change the file system to ext2 and mount it read-only (the ext2 type will eliminate the need to read the journal so it will be easier to mount the snapshot read-only).

GUI Tools

Here are 3 GUI tools for LVM.

It is beyond the scope of this article to review these GUI's but if you are so inclined, give them a try. EVMS, in particular, is a very powerful tool with it's own volume management capability but it can be used with LVM as well. It was developed from an IBM tool of the same name. It has some very powerful methods that make your life much easier. However, with all GUI tools it is recommended you keep detailed notes on the LVM configuration.