Jeff's Blog

Just another WordPress site

Recovering a crashed LVM setup

leave a comment

I have been a happy user of LVM for the last 3 years. Never had any problem until last week when everything disappeared from my disk.

Grub refused to boot the system and every lvm command I knew vgscan, lvscan or pvscan gave me nothing.

After some research I discovered that I should have included in my backup the /etc/lvm directory. Well, good to know for next time.

So after a few hours of research and I guess a little bit of luck I managed to recover all my data.

1. So what is in the /etc/lvm directory?

Backup files generated by LVM each time you change the structure of the disk. They look like that

creation_host = "mypc"	# Linux mypc 2.6.35-28-generic
#50-Ubuntu SMP Fri Mar 18 18:42:20 UTC 2011 x86_64
creation_time = 1303817218	# Tue Apr 26 12:26:58 201

main { ## Note that main is the name of my volume group
       ## and therefore this be diffent for you
	id = "29gZ34-vvVq-qING-irrW-oxJP-muBC-6R6x1Q"
	seqno = 13
	status = ["RESIZEABLE", "READ", "WRITE"]
	flags = []
	extent_size = 8192		# 4 Megabytes
	max_lv = 0
	max_pv = 0

	physical_volumes {

		pv0 {
			id = "IfhgDL-LpD6-cbCH-sEDp-Fsu9-la81-KlyGdC"
			device = "/dev/sda6"	# Hint only

			status = ["ALLOCATABLE"]
			flags = []
			dev_size = 321042897	# 153.085 Gigabytes
			pe_start = 384

And it goes on and on

2. How to get this file without backup?

So assuming you did not save that drive you here is a way to get it back. Boot using a rescue CD or a liveCD and once logged in as root in the shell, run the following command.

grep -a -B500 -A500 physical_volumes /dev/sda6 > results

This will search the whole lvm physical volume (sda6 in my case) for a string that we know is included in the file we are after. The -A and -B options tell grep to return 500 lines before the match and 500 lines after the match.

You might have to repeat this operation if your lvm setup was using multiple drives.

3. Get some coffee
It will take quite a while.

You might have to repeat this operation too.

4. Sort the results
The res file will contain quite a lot of garbage and most likely several copies of the file we are after. Why several copies? Remember that LVM save a copy each time you change the disk structure.

An easy way to find out which version is the latest is to use the creation_time filed that is included in the file

#grep -a creation_time\ \=\ [0-9] res
creation_time = 1229967708	# Mon Dec 22 17:41:48 2008
creation_time = 1229967855	# Mon Dec 22 17:44:15 2008
creation_time = 1286641220	# Sat Oct  9 17:20:20 2010
creation_time = 1286641265	# Sat Oct  9 17:21:05 2010
creation_time = 1238857427	# Sat Apr  4 16:03:47 2009
creation_time = 1239575936	# Sun Apr 12 23:38:56 2009
creation_time = 1229968090	# Mon Dec 22 17:48:10 2008
creation_time = 1303817218	# Tue Apr 26 12:26:58 2011
creation_time = 1286641220	# Sat Oct  9 17:20:20 2010
creation_time = 1286641265	# Sat Oct  9 17:21:05 2010

In my case the latest backup was done in April 2011. So I can use a text editor to extract the file out of the res file.

I used the timestamp 1303817218 to locate the file within the res file.

5. Reinitialize the physical volume

pvcreate -u IfhgDL-LpD6-cbCH-sEDp-Fsu9-la81-KlyGdC /dev/sda6

You will find the id of the physical volume in the file you recently recovered.

You might have to repeat this operation if you have multiples volumes in the LVM.

6. Reinitialize the volume group
vgcreate main /dev/sda6 [second drive ...]

Name is the name of my volume group, you will most likely remember what is yours but most likely it is the name before the first opening bracket in your file.

If you have multiples physical volumes, append at the end of the command line.

7. restore the lvm configuration backup
vgcfgrestore -f backup.vgc main

At that stage if you are lucky lvscan should give you the list of you logical volumes and you should be able to mount them.

Good luck with your recovery and next time don’t forget to backup the /etc/lvm directory.

Written by me

September 4th, 2011 at 1:58 pm

Posted in Linux

Are you considering to quit smoking?

leave a comment

I know that it is not the new year. But as everybody is back from their well deserved summer break it is also time to consider taking some big decisions on what to get done by the end of the year.

If “Quit smoking before year end” is at the top of your agenda, we have something that might help you. Get Me Smoke Free is a Facebook app for people like you that are considering quiting smoking. It is packed with tips on how to quit smoking (and please don’t hesitate to share your).

The application has just entered the public beta stage so don’t hesitate to let us know what you think.

Written by me

August 29th, 2011 at 9:40 pm

Posted in web

Bookmarklet that submit a form without leaving the current page

leave a comment

I recently came across the need to have a javascript bookmarklet that would allow me to the post a form using information available on the current page without actually leaving that page or opening popups. The following code does the trick by adding a 1x1px iframe to the current page and posting the form to that iframe


var iframe = document.createElement("iframe");
iframe.setAttribute("name","myiframe");
iframe.setAttribute("frameborder","0");
iframe.setAttribute("scrolling","no");
iframe.setAttribute("src","about:blank");
iframe.setAttribute("width","1");
iframe.setAttribute("height","1");
document.body.appendChild(iframe);

var form = document.createElement("form");
form.setAttribute("method", "POST");
form.setAttribute("target", "myiframe");
form.setAttribute("action", "<url here>");
var hiddenField = document.createElement("input");
form.appendChild(hiddenField);
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "url");
hiddenField.setAttribute("value", document.location);
document.body.appendChild(form);
form.submit();

Written by me

August 26th, 2011 at 12:41 pm

Posted in code

The hedge fund name generator

leave a comment

Why don’t you give a spin to the Hedge Fund Name Generator?

This one-arm bandit machine will not require you to insert any coin but might still help you win the jackpot and provide you with the name of your next hedge fund venture

Written by me

August 26th, 2011 at 12:29 pm

Posted in web