Monday, November 22, 2010

Booting an lvm snapshot of /

When it becomes time to upgrade Mandriva, or in fact, any other distro, I always have the same problem. I don’t want to do an upgrade because I can’t easily go back if the upgrade fails. So I usually end up creating a fresh install with a different root volume in LVM, then installing the same packages and copying configuration across, as I see fit. Good, but could be better.

I’ve also found I do keep wanting to try stuff, which would be nice to be able to roll back to a known state if things turn bad. So, recently I investigated how to create LVM snapshots of the root filesystem (/).

The procedure below works for me.. but if you don’t understand it, don’t try this on an important system.

Here is my basic howto:
The example below assumes your root volume is /dev/vg/rootlv. It assumes your /boot partition is a normal partition, not part of /.

1. Create a snapshot of / (which I usually do while it’s running, which means it’s not 100% clean). I create this with the following command.
lvcreate -n rootsnap1 -L 5G -s /dev/vg/rootlv
2. Mount the snapshot in /mnt/rootsnap1
mkdir /mnt/rootsnap1; mount /dev/vg/rootsnap1 /mnt/rootsnap1
3. Adjust snapshot to be able to boot:
perl -pi -e "s#/dev/vg/rootlv#/dev/vg/rootsnap1#" /mnt/rootsnap1/etc/fstab
rm /mnt/rootsnap1/etc/mtab
umount /mnt/rootsnap1

4. Create yourself a copy of initrd that has the new root volume in it. I do this by just editing the current one.
mkdir /boot/tt
cd /boot/tt
cat ../initrd.img | gunzip | cpio -i
perl -pi -e "s#/dev/vg/rootlv#/dev/vg/rootsnap1#" init
find . | cpio -H newc -o | gzip -9 > ../initrd-rootsnap1.img
cd /boot
rm -rf /boot/tt

5. Add the new root snapshot1 to grub’s menu.1st file, by copying your existing entry and changing the copied entry:
initrd.img to initrd-rootsnap1.img
root=/dev/vg/rootlv to root=/dev/vg/rootsnap1
vi /boot/grub/menu.1st
6. Reboot and choose new entry from grub.

1. If the snapshot gets full, you’ll loose access to it and the volume it is a snapshot of, until you fix it. You can however, make it bigger at any time before it is full. It will never use more than the size of the original volume.
2. Snapshots can be merged* (fairly new feature) back into the base for that snapshot. This means that changes on a snapshot can be tried and if successful, can be merged back into the base. See lvconvert.
(*note, you don’t want to have the snapshot, or the base mounted at the time. lvconvert will delay until next reboot if you do. Reboot twice for it to completely free up the snapshot space used.)
3. If you take multiple snapshots, it is best to not do large amounts of writes to the base for the snapshots, as every block changed on the base will first have to be copied to every snapshot which affects write performance.

Readers: Comments are welcome, however I do moderate them, so they don’t appear on my blog immediately.

Sunday, November 07, 2010

Public Mandriva 2010.1 AMI now available on Amazon EC2

After some work, I’ve created a Mandriva 2010.1 AMI instance on Amazon’s EC2 service. I’ll go into detail of how I created it, but for the impatient, you can now boot a Mandriva 2010.1 i386 Amazon virtual machine, setup with a base Mandriva install. The AMI ID is: ami-62e2150b and the name is “mandriva-2010.1-go”. You can find this image in the list of public AMI’s on Amazon if you search for mandriva. The smallest machine Amazon provide costs only around 2 cents per hour, if you just want to play with it.
Getting down to how I actually created this, is a little more interesting. Amazon now provide two things that make building your own machine on Amazon easier. The first is a think called EBS, which is basically the ability to create block devices and give them to the OS, they are persistent storage and reasonably fast. The second is that Amazon now have a thing called PVGRUB. This lets it boot any standard kernel that supports running as a Xen guest. (dom U).
To build the one I have made public, I started with an install of Mandriva inside a virtual machine I created, by using the normal installer and unselecting everything except the base and urpmi. I then booted an existing Amazon machine that was running a different version of Linux. I attached another EBS disk to the machine, made a file system and used rsync to copy all the files from my vm (which had been shutdown), onto the EBS storage at Amizon. After that, I adjusted /boot/grub/menu.1st and fstab, then switched my EBS storage devices around to boot from the Mandriva one I has just created. I then added an appropriate script to /etc/rc.d/rc.local to install the appropriate ssh key on boot when I make it public, and created a snapshot of the EBS decive. From this stapshot, I was able to create the AMI ( using ec2-register), which clones the snapshot every time someone creates a virtual machine based on the snapshot.
I can create the EBS ami with whatever size disk I want, as I setup the image to run a resize2fs on first boot to grow from the 2GB snapshot to whatever size people want. The public one I created has a 4GB disk to start with.