Lvm – How to restore an LVM snapshot without deleting the snapshot

backuplvmsnapshot

Steps to reproduce

  • create a snapshot of a volume using lvm

lvcreate -L 200GB -s -n snapshotname /dev/hostname-vg/root

  • make changes to the filesystem

  • restore the filesystem to the state it was in when
    the snapshot was made

lvconvert --merge /dev/hsotname-vg/snapshottorestore

  • reboot (since I'm restoring the root volume)
  • show list of snapshots lvs -a

Desired behaviour

The original snapshot still exists

Actual behaviour

The original snapshot was deleted when the system was restored to the snapshot

Question

Is there an alternative to lvconvert, or a flag I can add to it, so that the original snapshot is not deleted?

Use case

  • I take a snapshot of a working system
  • I make changes
  • The changes fail
  • I restore to the working system
  • I want to retry the changes in a slightly different way, leaving open the possibility of restoring to a working system again

Using the lvconvert command above, I need to remember to manually recreate the same snapshot again after restoring. Since this is a large volume with many changes, I need to wait half an hour before making the snapshot. (You can't take a snapshot of a volume still merging)

The volume is restored immediately. So I have a clean system I could use immediately, but I don't want to proceed without a backup. So I need to twiddle my thumbs for half an hour, then make a backup, then make the changes.

Best Answer

There is no way you can keep the snapshot unless you would implement this as a feature in LVM.

When writing to a logical volume that has a snapshot, it first copies the current block from the origin into the snapshot and marks the block as changed. (Source)

After the lvconvert merge is initiated it stops copying data over to the snapshot (Copy on Write). If it would continue to do so, it would copy the data merged from the snapshot, back into the snapshot itself.

When the lvconvert merge is finished, it calls for the deletion of the snapshot here. This is because the snapshot still contains a copy of the data that it received while data from the origin was being overwritten.

It would be new feature to mark a snapshot merge action to "keep a snapshot", which would trigger a snapshot creation after merging is finished and deletion is done.

In your case, you might already be satisfied by creating a snapshot with a certain name, then have an @reboot cronjob that will check if the snapshot exists and create one if not.