Course contentsShow
← Linux
Lesson 662 of 1,8197. Storage Management and Logical VolumesFree lesson

LVM snapshot architecture

Implement point-in-time state capture for backups and development environments.

In this lesson, you will learn how to use LVM snapshots to capture a "frozen" point-in-time state of your data, allowing you to perform backups or test changes without risking your original files.

The Snapshot Concept

Imagine you have a document you are about to edit, but you’re worried you might break it. A snapshot is like taking a photographic copy of that document at an exact second. In LVM terms, a snapshot volume acts as a bridge. When you create one, LVM does not copy all the data instantly; instead, it tracks the changes made to the "original" volume.

How It Works: Copy-on-Write

LVM snapshots use a mechanism called Copy-on-Write (COW). When you first create the snapshot, it is virtually empty. As you modify the original volume, LVM detects that a block is about to be overwritten. It pauses the write operation, copies the original data to the snapshot volume, and then completes the write to the original volume.

Because of this, the snapshot stays small as long as your changes are minimal. If you don't change anything, the snapshot takes up almost no space at all.

Implementing a Snapshot

To create a snapshot, you use the lvcreate command. You must define the snapshot size—this is the amount of space reserved for tracking the changes made to the original volume.

# Create a 1GB snapshot named 'my_backup' for the 'data_vol' logical volume
lvcreate -L 1G -s -n my_backup /dev/vg0/data_vol

Once created, you can mount /dev/vg0/my_backup just like any other drive. You will see the files exactly as they existed the moment the snapshot was created. If you accidentally delete a file on your original volume, you can simply restore it from the mounted snapshot.

Crucial note: If the snapshot volume runs out of space because you changed too many files on the original volume, the snapshot will become corrupted and invalid. Always monitor your snapshot capacity with lvs.

Key Takeaway: LVM snapshots provide a space-efficient way to freeze the state of a volume by using Copy-on-Write to store only the modified data, making them perfect for safe backups and testing environments.