Storage Pools and Snapshots with Logical Volume Management

Logical Volume Management (LVM) on Linux: A great tool for creating pools of storage hardware that can be divided, resized, or used for snapshots.

LVM Snapshots

It’s impossible to go over all or many of the usage cases for LVM, but one that stands out is the snapshotting capability of LVM. 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.

In preparation for the snapshot you need to examine the file system to determine it’s size (“du -sh”). This will tell you the approximate size of the snapshot. For example, if the file system says it has 29G used (29 GB), then you should snapshot a little larger – perhaps 30-32 GB (always good to have a cushion). This cushion isn’t strictly necessary, but a little safety never hurts.

You can check how much space is used for the snapshot by using the command lvdisplay. This link discusses this. In addition as recommended in this link 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).

To help understand snapshots a little better Figure 2 below is a variation of Figure 1 but the /data file system is not there and instead that space is left open on the VG for snapshots. There is also a single VG to make things a little easier.

Figure 2 - LVM Concepts for Snapshots
Figure 2 - LVM Concepts for Snapshots

The 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
463M    /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 -L500M -s -n home_backup_080309 /dev/primary_vg/home_lv
  Logical volume "home_backup_080309" created

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

# lvscan
  ACTIVE   Original '/dev/primary_vg/home_lv' [450.00 GB] inherit
  ACTIVE   Snapshot '/dev/primary_vg/home_backup_080309' [500.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_080309
# mount /dev/primary_vg/home_lv/home_backup_080309 /mnt/backup/home_backup_080309
# mount
/dev/hda3 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda1 on /boot type ext2 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/mapper/primary_vg-home_lv on /home type ext3 (rw,data=ordered)
/dev/mapper/primary_vg-home_backup_080309 on /mnt/backup/home_backup_080309 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_080309/
total 20
16 lost+found   4 user5
# ls -s /mnt/backup/home_backup_080309/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

The command line tools for LVM are not too complicated but for novices it can be a bit daunting. So to help 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.

Summary

This is a quick review of LVM. For people new to LVM it presents some of the basic concepts and how they can be used. It's not intended to be an encompassing tutorial for LVM, but rather a quick overview. Further details are left to better more lengthy tutorials on the web. There are some GUI tools that can help gradually introduce LVM as previously pointed out.

In addition, 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).

LVM is one of the tools that people forget about and sometimes even ignore. Many installers for various distributions will use LVM as default (RHEL, CentOS, and Fedora are three that immediately come to mind but there are definitely more), and make things much easier for the novice. However, you still need to understand how the installer built the VG's and the LV's. Hopefully this article gave you a refresher on LVM (a quick one) or introduced you to a great tool for Linux.

Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62