LVM Snapshots

Background Info

Snapshots enable you to to take a point-in-time "image" of the filesystem instantly. This allows you to create backups without having to turn off your server/processes in order to ensure data integrity/consistency. LVM also has this functionality, but is not a copy-on-write filesystem, so you can retain a 1:1 relationship between current file data and disk usage by removing the snapshots. It also does not utilize a large portion of your RAM like ZFS and is "safer" to use than BTRFS at this time.

LVM Snapshot Concept

LVM is able to take a snapshot of a filesystem instantly because it will just redirect all subsequent writes somewhere else (the new snapshot volume), which means the original volume is never changed. Thus, at the point of the snapshot being taken, your disk will have the exact same disk utilization level as before. However, if you were to completely replace a 2GB file with a different 2GB file, you would be "wasting" 2GB of space as there would be two copies of the file, but only that file.

The Filesystem Lies!

LVM snapshots can cause confusion because what is happening behind the scenes on the volumes is the exactly opposite to what appears to be happening when you examine the filesystems. The original filesystem may appear to grow as you add files over time, but your snapshot filesystem will always appear static. What is actually happening is, the original volume is never being touched, and the snapshot volume is growing in utilization. As far as I know, this means that excess capacity on a volume that a snapshot is taken from will never be utilized until the snapshot is deleted or invalidated due to capacity constraints. This means that it is a good idea to keep growing your logical volumes as you need to.

Getting Started With Installation

This section is for users who want to be able to snapshot their root filesystem by performing a fresh Ubuntu Server 12 installation so if you don't need to do this then skip ahead.


At the partitioning screen, it is a good idea to select "guided partitioning with LVM". You could also use encrypted LVM, it's up to you.

I spent over 30 minutes trying to figure out how to manually configure an LVM setup with the root partition being on LVM, but gave up. One day I may revisit this. This is the only time I have found CentOS much easier than Ubuntu.

The important part is ensuring that you do NOT use the default maximum allocation size for guided partitioning. In fact it is a good idea to enter the minimum that you feel is reasonable and then grow your filesystem later as you need it! It is incredibly difficult to shrink a root filesystem (has to be done "offline"), but very easy to grow it. By not using the entire disk, you are allowing yourself the ability to create other LVMs later that will act as your snapshot volume(s).

Do NOT use the default of the maximum possible size as shown on the above screenshot!

Taking a Snapshot!

Now that you have an LVM filesystem set up with some free space on your volume group, you can take a snapshot. This is done by creating a new logical volume of a specified size and name. I will refer to this as the "snapshot volume" even though in my mind, the snapshot volume is the original that is never going to be touched now.

sudo lvcreate -L[SIZE_IN_MB]m -s -n [SNAPSHOT_NAME] /dev/[VG_NAME]/[ORIGINAL_LVM]
Don't skimp on the allocation size. If the original filesystem changes by more than this amount, the volume will be invalidated and not work
The -s switch tells LVM that this is a snapshot, and the -n is for inputting the name of the snapshot

Mount The Snapshot

If you want to look at the original files, then all you need to do is mount the snapshot just like any other filesystem.

mkdir /my/mount/point
mount /dev/[VG_NAME]/[SNAPSHOT_NAME] /my/mount/point

Now if you run a tool such as df or pydf, then it will appear that you have two filesysems of near-exactly the same size, which may be a physical impossibility because it adds up to more than your drive's capacity

It is at this point that I would run my backup program (duplicity/scp/rsync etc) on that mounted volume before deleting the snapshot. This means that you can use incredibly small volumes for your snapshots as you only need enough to hold the changes that will take place whilst the backup is running.

No comments:

Post a Comment