LVM snapshots are a simple and fast way to "snapshot" the current file system. They are (probably) mainly used to create backups from the snapshot LV or to use the snapshot as backup itself.
But it can also be a huge time saver! I recently wanted to replace a manually installed Tomcat through the Debian packages. I knew that would change a lot of the current system (e.g. openjdk being used instead of Oracle JDK), I wanted to make sure I have a rollback.
The machine on which the Tomcat is installed is an LXC running on its own dedicated LVM file system. Before installing the tomcat7 packages, I took a LV snapshot:
lvcreate -L10g -s -n lxc12snapshot /dev/vgdata/lxc12
Logical volume "lxc12snapshot" created
I then installed the packages in the LXC... to then realize that it breaks a lot of the current installation. I tried to remove all the packages with "apt-get remove" and "apt-get purge" but some configurations still remained:
apt-get purge tomcat7 tomcat7-admin tomcat7-common tomcat7-user openjdk-7-jdk
grep tomcat /etc/passwd
Instead of trying to manually clean up everything, I can use my fallback! Yay!
After halting the LXC in question, I first checked out the status of the current LV's:
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
lxc12snapshot vgdata swi-a-s- 10.00g lxc12 4.80
lxc12 vgdata owi-aos- 20.00g
Before the revert to the snapshot, the LV needs to be unmounted in case it is:
And then the rollback can happen (the snapshot will be merged with the current LV):
lvconvert --merge /dev/vgdata/lxc12snapshot
Merging of volume lxc12snapshot started.
lxc12: Merged: 4.8%
lxc12: Merged: 3.4%
lxc12: Merged: 1.4%
lxc12: Merged: 0.0%
lxc12: Merged: 0.0%
Merge of snapshot into logical volume lxc12 has finished.
Logical volume "lxc12snapshot" successfully removed
This took about 2 minutes.
I then remounted the LV and started the container.
mount /dev/mapper/vgdata-lxc12 /var/lib/lxc/lxc12/rootfs
lxc-start -n lxc12 -d -c /var/lib/lxc/lxc12/console.out
Voilà. Easy rollback which took only a couple of minutes.