Help needed: Recover partition table with /proc/diskstates

I have accidentally deleted the root partition table, but didn’t restart yet, so the kernel doesn’t know this yet, how can I recover it if /proc/diskstates still shows my original partition table?

Write down the numbers, in particular the cylinder start and end numbers for each partition, and use this information to recreate the partition table using a rescue CD. Your data will still be there as long as you don’t format the partitions. The partition table is just a map showing where the partitions start and end. I assume you know the types of the various partitions. If they are all Linux, you only need to mark the swap partition 82 and the others 83. The exact type, ext2/3, reiserfs, etc are all 83 and distinguished in the partition data.

So if I fdisk again, and recreate my partition table manually using the cylinder information provided my /proc/diskstats, then all should be good?

Where do I get these numbers from? (/proc/diskstats should be one place, but I don’t know it’s syntax)

BTW, I think it’s /proc/partitions you want, not diskstats. diskstats as the name suggests, contains I/O statistics. partitions shows the number of blocks in each partition.

For anybody reading this thread, it’s a good idea to save the output of fdisk -l while the system is running, somewhere say offline, so that you can rebuild the partition table if you have this kind of accident.

I’m a little confused here, what I think I need is cylinders, but I assume that /proc/partitions shows the first block number, but I don’t really understand the relation between blocks and cylinders, and don’t know if partition one starts at block 1 and partition two starts at block 100 than, partition one starts from block 1 and ends at block 100? or this is not that simple?

It doesn’t matter these days, OSes and disks actually use LBA. Cylinders, heads and sectors are fictions in modern disks. The modern disk doesn’t have 255 surfaces inside or whatever it claims, it’s all mapped to LBA internally. As long as you get the block numbers right, it will be ok. Fdisk can be told to switch to block number entry and display. One thing though, by convention the first “cylinder” is dedicated to bootloaders.

But you may wish to divide the block count * 2 by the number of blocks per cylinder (which fdisk will tell you) to get back the cylinder count for double checking. If you get it wrong, no harm done, it won’t mount, then you go back and fix up the cylinder number.