Master LVM Snapshots: Step‑by‑Step Guide to Create, Restore, and Merge
This tutorial explains LVM snapshot principles, walks through preparing a logical volume, creating and mounting snapshots, handling common XFS UUID errors, restoring data manually or via snapshot merge, and provides practical commands and images for each step.
Preface
In the previous chapter we learned about LVM logical volume management and discovered that LVM can flexibly resize filesystems by adding or removing PEs. LVM also offers a crucial feature—snapshot technology—for backing up a filesystem.
Before diving into LVM, consider why a snapshot of any size completes in seconds.
LVM snapshots use copy‑on‑write (COW): at the instant a snapshot is created, the system records the current state of the logical volume, producing a blank snapshot that later stores only the blocks that change.
After creation, as the source LV changes, the altered data is copied to the snapshot. Restoring merges the snapshot with the original LV, returning the volume to its original state.
Note
Snapshot volume size must be large enough; it cannot exceed the free space of the volume group (VG).
The snapshot and the source LV must reside in the same VG, otherwise creation fails.
Creating LVM Snapshots
1. Experiment Preparation
First, create the logical volume:
1) Prepare four partitions
2) Create PV
3) Create volume group vg1
4) Create a 10G logical volume lv1
5) Format and mount to /data
6) Write test data
2. LVM Snapshot Experiment
Check current LV/VG usage:
lv1 is 10G in vg1, which has 40G total and 30G free, providing enough space for a snapshot.
1) Create snapshot
Command parameters:
-L: size of the snapshot volume
--snapshot: specify snapshot type
--name: snapshot name
/dev/vg1/lv1: target LV to snapshotA snapshot is a special logical volume created with lvcreate. lvs – list logical volumes
2) Simulate data corruption
The original data "hello world" was maliciously changed to "hello worl". Using the LVM snapshot rollback we can recover the important data.
3) Mount snapshot
Encountered an error
Troubleshooting steps:
1) Run
dmesg | tail2) The snapshot LV shares the same UUID as the original LV. XFS cannot mount two filesystems with identical UUIDs.
If the filesystem is XFS, mounting the snapshot will fail because the UUIDs clash. Resolve this by either changing the UUID or adding the nouuid option to ignore the duplicate.
3) Mount with nouuid option:
Problem solved, snapshot mounted at /data_bak
4) Restore data
When many small files have changed, copying from the snapshot becomes cumbersome. A simpler method is to merge the snapshot.
Restoring the Entire Logical Volume
1) Mount the snapshot
2) Export data from the snapshot to /opt
Export snapshot data to /opt (illustrated below)
3) Unmount, format, and remount the original LV
4) Restore the data to the original LV
Merge Snapshot (merge snapshot)
1) Simulate massive data loss that requires full LV restoration.
2) Unmount both the original LV and the snapshot LV
3) Merge the snapshot (ensure both volumes are unmounted)
Note: merging automatically deletes the snapshot volume.
4) Remount the original LV
The test file now contains "hello world", confirming that the entire volume has been restored to the state captured at snapshot creation.
Summary
Snapshots are a slightly advanced LVM topic, but the common use cases presented here are sufficient for most everyday scenarios and provide a solid foundation for further exploration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
