LVM snapshots

I’m using LVM2 on Linux for some time but only now I discovered wonders of its snapshot functions.

“A wonderful facility provided by LVM is ‘snapshots’. This allows the administrator to create a new block device which presents an exact copy of a logical volume, frozen at some point in time. Typically this would be used when some batch processing, a backup for instance, needs to be performed on the logical volume, but you don’t want to halt a live system that is changing the data.” LVM snapshot intro

more on internals

“LVM allows you to take a snapshot of your system in a much more efficient way than a traditional backup. It does this efficiently by using a COW (copy-on-write) policy. The initial snapshot you take simply contains hard-links to the inodes of your actual data. So long as your data remains unchanged, the snapshot merely contains there inode pointers and not the data itself. Whenever you modify a file or directory that the snapshot points to, LVM automatically clones the data, the old copy referenced by the snapshot, and the new copy referenced by your active system. Thus, you can snapshot a system with 35GB of data using just 2GB of free space so long as you modify less than 2GB of data.” Archlinux WiKi

Examples of use

1) you have ext3 filesystem with data directories that reside on logical volume with ext3 filesystem. Files are modified all across a filesystem, some are locked etc. Now you need to do a backup of all files which is consistent. Then you can do two things.
– One is shutdown and/ or system services and do backup of files
– the other make a snapshot of a volume which in fact is frozen state of a volume at a moment you made it. Snapshot creation is a matter of a few moments so even on production system it won’t overload your system. Of course doing backup then will load disk subsystem with data read. Beware that “special” files like databases needs special care as written here or here so they stay in consistent state and as it seems even then it might be a problem when data and db logs are not on the same volume

During all that original volume can be in use without any interruption and special care.
After you finish backup just dismount snapshot volume and dispose it with lvremove command

2) imagine you use xen for server virtualization as I do. Put system on logical volume, apply all updates available and perform its setup. And now you need to create second vm which in fact can be based on first vm. You can

– create volume, format it to ext3 for example, shutdown first system, mount both volumes and rsync first installation to second. Fine but quite a lot of work
– second and much easier method is to create a snapshot. LVM version 1 allowed to make only read only snapshot but LVM2 can do read/write snapshots. So assuming you have LVM2 on system then just make a snapshot with enough of space for modification (e.g. lvcreate -L1G -s -n dbbackup /dev/ops/databases) and you have machine ready. Now just adjust its config as IP, hostname etc. and that’s it

Nice though according to labtests I found made at M.Sc. Thesis by Bhavana Shah there is logical overhead when using persistent snapshots compared to raw disk volumes



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: