LVM snapshots and non-destructive Linux upgrades

This post roughly describes what I do when I want to non-destructively upgrade my Linux system. By non-destructive means a procedure that allows me to upgrade but also to rollback if something goes wrong. As an example, I wanted to upgrade my Ubuntu system from Jaunty to Karmic. Since Karmic is now Alpha 1, the chances of the upgrade going bad or eating my data were high. Here is where LVM and LVM snapshots come into scene.

Basically, the idea consists of taking a snapshot of the root filesystem using an LVM snapshot, then reboot the system to use the filesystem from the LVM snapshot as the root filesystem, perform an in-place upgrade of Jaunty to Karmic and see what happens. Since the upgrade takes place on a system running with the root filesystem mounted from the LVM snapshot, the original root volume is kept intact. Hence, if something goes wrong I can always reboot in order to use the original root filesystem and the system would behave as if no modifications were done at all during the upgrade.

The LVM snapshot volume should be big enough to, in the worst case, store a completely new Linux installation. The average Ubuntu installation requires less than 8GB of disk, so the LVM snapshot should be about that size plus some slack required to download the packages. In my case, and since I have free enough disk space, I chose 16GB just to be on the safe side.

Resize the root filesystem and root volume

This step is only required if there is no space in the volume group to accommodate for the snapshot volume. In my case, the volume group is full so I need to shrink the root filesystem and the root volume. resize2fs does not currently allow one to shrink a filesystem online, so I booted from the Jaunty LiveCD and entered rescue mode. From there:

# e2fsck -f /dev/root/root
# resize2fs /dev/root/root 80G
# lvresize -L 80G /dev/root/root

Create the LVM snapshot

To create an 16GB LVM snapshot volume of my root volume:

# lvcreate -s -n karmic-root -L 16G /dev/root/root

Prepare the boot environment

Mount the filesystem from the LVM snapshot volume and modify /etc/fstab to replace the device name where the original root filesystem is with the device name where the snapshotted rool filesystem lives:

# mount /dev/root/karmic-root /mnt
# vi /mnt/etc/fstab

In my case, the line for the new root filesystem looks like:

/dev/root/karmic-root /  ext4 defaults 0 1

Reboot into the snapshot

Trigger the grub menu and modify the kernel entry that corresponds to the Ubuntu system. The idea is to use the device name for the LVM snapshot. This is how the new kernel in the menu looks like:

kernel /vmlinuz-2.6.28-11-generic root=/dev/root/karmic-root ro

Then press b to boot the system. The system should boot normally, but instead of using the original root filesystem it should be using the filesystem from the LVM snapshot:

$ grep /dev/mapper /proc/mounts
/dev/mapper/root-karmic--root / ext4 rw,relatime,errors=remount-ro,barrier=1,data=ordered 0 0

In-place upgrade

I won’t describe how to do an in-place upgrade of Ubuntu. There are many resources out there that describe how to do that. The point here is that the upgrade will modify the snapshot while the original root filesystem is kept intact.

Destroy the snapshot

If something goes wrong with the update, and it usually goes wrong when upgrading to an Alpha version, to bring the system back to a usable state is just a matter of rebooting the system and using the right entry listed in grub. In-place upgrades of Ubuntu will typically add a new kernel to the list of entries in grub but won’t modify the existing ones.

After rebooting into the original system, the snapshot can be removed:

# lvremove /dev/root/karmic-root

If you don’t intend to experiment with upgrades, perhaps you want to resize the root LVM volume, then the root filesystem back to their original size.


13 thoughts on “LVM snapshots and non-destructive Linux upgrades

  1. Pingback: S02E09 – The Dimensions of Time – OGG HIGH | Ubuntu Podcast from the UK LoCo team

  2. Pingback: S02E09 – The Dimensions of Time – MP3 HIGH | Ubuntu Podcast from the UK LoCo team

  3. Pingback: S02E09 – The Dimensions of Time – MP3 LOW | Ubuntu Podcast from the UK LoCo team

  4. Pingback: S02E09 – The Dimensions of Time | Ubuntu Podcast from the UK LoCo team

  5. Pingback: S02E09 – The Dimensions of Time – OGG LOW | Ubuntu Podcast from the UK LoCo team

  6. I had a problem with the /boot-partition after upgrade once so I have 2 boot-partitions and grub installed onto the partition for the mirror-system and a chainloader on the first so that Karmic can remove whatever it wants from /boot without ruining my Jaunty-installation.

  7. Let’s say I want to apply patches to a RHEL 5.4 server and if need be I can revert back in time to a good state of RHEL 5.4. What methodology would I do?

  8. In the sixties I was in Europe and carried a Top Secret Clearance during the Cold War, Cuba Missale Crisis, The Building of the Berlin Wall, etc. and I thought the World had gotten dangerous then but now it is time to realize that the biggest danger of Global Warming is not the environment. You can only put so much air in a balloon before it pops. The real danger of Global Warming is the movement of people. India can only produce enough food to feed about 16% of its people. Even the Berlin Wall was built to stop the rush of people into West Germany when the air routes were opened from the Templehoff in Berlin into West Germany which at the time had a very strong economy and so many were crossing the border that it begin to disrupt their way of life. Chinese that are starving are moving across the borders into Russia at the rate of Tens of Thousands and the Russians do not like it. At this very time Russia and China could walk into a Nuclear Exchange against each other. Then there is Pakistan and Afghanistan. Lets not forget Iran and Isreal. The biggest threat is the movement of people and this country usually only complains about the borders with Mexico and what they have done to the Calif Economy. The movement of people from Mexico and Canada into the US is a very small plate as to what is happening in other countries. Not just the US but the whole world is Bankrupt and might survive if and only if the population numbers decrease in very high numbers. I am now 70 years old so have lived from The Great Depression to the New Great Depression. Only this time there will not be just a World War but a Golbal Nuk Exchange. Hunger can cause a Government to fail and is usually the root cause of war. I hope I never have to see it but the day is coming. The baloon will pop.

  9. Dear Jani.
    You’re such an ignorant asshole. Take your medication, and get a life. Why do you have slander other people just because your own life is so meaningless? Stop living in denial, stop being an useful idiot echoing the elite, and stop massaging your pathetic little ego by imagining that your comments actually represent some kind of “witty” and “smart” form of dialogue. It’s just sad, OK? Wake the fuck up, or shut the fuck up.

  10. I have discovered that smart real estate agents all over the place are getting set to FSBO Promotion. They are seeing that it’s in addition to placing a sign post in the front property. It’s really about building associations with these sellers who someday will become buyers. So, while you give your time and efforts to supporting these retailers go it alone — the “Law involving Reciprocity” kicks in. Great blog post.

  11. Howdy would you mind letting me know which web host you’re working with? I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most. Can you recommend a good internet hosting provider at a honest price? Thanks a lot, I appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s