How to Merge or Remove LVM Snapshots: Commands for lvconvert and lvremove


2 views

When working with LVM snapshots, you typically encounter two scenarios: merging changes back to the origin volume or completely discarding the snapshot. The choice depends on whether you want to preserve the delta changes or revert to the original state.

To completely remove a snapshot and discard all changes made since its creation:

lvremove /dev/vg00/snap

This command will:

  • Delete the snapshot volume immediately
  • Not affect the original volume (/dev/vg00/vm)
  • Permanently discard all changes captured in the snapshot

To merge changes from the snapshot into the original volume:

lvconvert --merge /dev/vg00/snap

Key points about merging:

  • The merge occurs automatically during the next activation of the origin volume
  • For immediate merging, you can deactivate/reactate the origin:
    lvchange -an /dev/vg00/vm
    lvchange -ay /dev/vg00/vm
  • After successful merging, the snapshot is automatically removed

Space monitoring: Always monitor snapshot space usage to prevent automatic merging when full:

lvs -o +snap_percent

Timing merges: Merging large snapshots may take significant time. Schedule during low-usage periods.

Example workflow:

# Create snapshot
lvcreate -L 10G -s -n db_snapshot /dev/vg00/prod_db

# ... perform testing ...

# Option 1: Merge changes
lvconvert --merge /dev/vg00/db_snapshot

# Option 2: Discard changes
lvremove /dev/vg00/db_snapshot

When working with LVM (Logical Volume Manager) in Linux, snapshots provide powerful capabilities for system backups and state preservation. A snapshot initially appears as an identical copy of the original logical volume, but only stores changes made after its creation using copy-on-write (COW) technology.

As shown in your example, creating a 10GB snapshot of /dev/vg00/vm would use:

lvcreate --name snap --size 10G -s /dev/vg00/vm

This creates a snapshot named 'snap' in the same volume group as the original volume.

To completely remove a snapshot and discard all changes recorded in it:

lvremove /dev/vg00/snap

This command will:

  • Immediately remove the snapshot volume
  • Free up the allocated space
  • Permanently discard any changes tracked by the snapshot

To merge the changes from the snapshot into the original volume (/dev/vg00/vm), use:

lvconvert --merge /dev/vg00/snap

Important notes about merging:

  • The merge happens automatically during the next volume activation
  • The original volume will be inaccessible during the merge process
  • After successful merge, the snapshot is automatically removed
  • For immediate merge, you might need to deactivate/reactivate the volume group

Here's a complete example demonstrating snapshot lifecycle:

# Create original volume
lvcreate -L 20G -n myvol vg00

# Create filesystem and mount
mkfs.ext4 /dev/vg00/myvol
mount /dev/vg00/myvol /mnt

# Create important files
echo "important data" > /mnt/file1.txt

# Create snapshot
lvcreate -s -n mysnap -L 5G /dev/vg00/myvol

# Make changes to original
rm /mnt/file1.txt
echo "new data" > /mnt/file2.txt

# Option 1: Discard changes using snapshot
umount /mnt
lvremove /dev/vg00/mysnap

# Option 2: Revert to snapshot state
umount /mnt
lvconvert --merge /dev/vg00/mysnap
lvchange -ay /dev/vg00/myvol
mount /dev/vg00/myvol /mnt

Before performing operations, check snapshot status:

lvs -a -o +devices

Key metrics to watch:

  • Data% - percentage of snapshot space used
  • Snap% - percentage of original volume tracked
  • Attr - status flags (e.g., 's' for snapshot, 'a' for active)

1. Snapshot full - When 100% of COW space is consumed:

# Check status
lvs /dev/vg00/snap

# Either extend snapshot
lvextend -L +2G /dev/vg00/snap

# Or merge/remove it
lvconvert --merge /dev/vg00/snap

2. Merge conflicts - Handle carefully when:

  • Original volume was modified while merge was pending
  • Multiple snapshots exist in the chain
  • Volume group has insufficient free space