Saturday, October 8, 2011

Recovery of a deleted partition ( Part - 2 )

                Starting from where I left in my previous post 
(http://meandmyubuntulinux.blogspot.com/2011/10/recovery-of-deleted-partition.html  )  , Testdisk has done wonderful job in revealing my HD's partitions and thus correcting the partition table. But..... still something was missing out there as I was still not able to boot into the system. It simply displayed a cursor which keeps on blinking forever and no other error message. 

               So, I had to again boot with the help of the Lucid Lynx ( Ubuntu 10.04 LTS ) live usb to figure out what is happening. Interesting or say luckily, when I booted the live system, It easily discovered all my partitions and and I was also able to access all of my data !!!


             This was quite confusing situation as the Ubuntu live disk was able to detect partitions properly but still my system was not able to boot at all. So, I checked with GParted Partition editor to see if there is still any problem in the partition table. Yes there was.......... it says “A disk cannot have partitions outside the disk”.


So, I dumped my partition table to a file called PT.txt for editing :

                                 sudo sfdisk -d /dev/sda > PT.txt


The output was :


--------------------------------------------------------------------------------------------
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 63, size= 62926542, Id= 7, bootable
/dev/sda2 : start= 62926605, size=425481525, Id= f
/dev/sda3 : start= 0, size= 0, Id= 0
/dev/sda4 : start= 0, size= 0, Id= 0
/dev/sda5 : start= 62926668, size= 31471272, Id= 7
/dev/sda6 : start= 94398003, size=136323528, Id=83
/dev/sda7 : start=230723584, size= 41947136, Id= 7
/dev/sda8 : start=272671308, size=104872257, Id= 7
/dev/sda9 : start=377544704, size=107634360, Id=83
/dev/sda10: start=485179128, size= 3215608, Id=82
---------------------------------------------------------------------------------------------

              Now let me explain the above output. The 1st column containing /dev/sd? are the partitions. If sfdisk doesn't find a partition corresponding to any number it simply put start, size and id = 0 for that partition. Its not an error. Start and size given here are in units of sectors. A particular Id corresponds to a particular partition type. For example, here, Id=7 is NTFS , Id=f is extended partition , Id=83 is EXT4 and Id=82 is Linux Swap partition.






Now time for some maths.......



As per the link in the reference post link that I gave above :

  1. There is a 63 sector gap between the start of an extended partition and the start of a logical partition.
  2. There is a 63 sector gap between the end of a logical partition and the start of the next logical partition.
  3. There is no gap at all between primary partitions.
  4. It is possible to have larger gaps between partitions, but never smaller than 63 sectors for logical partitions, (and obviously) never smaller than 0 for primary partitions.
  5. Partitions must not overlap.

         In my case, /dev/sda1 is a primary partition, /dev/sda2 is a primary extended partition and /dev/sda5 to /dev/sda10 are logical partitions.

               Now, checking the above conditions with my partition table.

  1. /dev/sda1 is ok as it starts at sector 63. No problem in that.
  2. Considering /dev/sda1 end range is ok, /dev/sda2 ( being a primary partition ) should start at sector=63+62926542=62926605 and so is the case . So, the start range of /dev/sda2 is also ok. Leaving aside the end range of /dev/sda2 as that will be the end of disk.
  3. Now, for /dev/sda5 ( which is a logical partition ), the start range ( as per condition 1) should be sector=62926605+63=62926668 and therefore , /dev/sda5 is also ok.
  4. For /dev/sda6 start sector= 62926668 + 31471272 + 63 = 94398003 implies that /dev/sda6 is ok.
  5. Similarly checking start sector of all the partitions are found ok.
  6. Now, time for the last check i.e. end sector of last partition i.e. /dev/sda10 and end sector of the extended partition /dev/sda2. Both should be same.
    End sector of /dev/sda2 will be sector = 62926605 + 425481525 =488408130
    End sector of /dev/sda10 will be sector = 485179128+ 3215608 = 488394736

                 So, here is a difference. So, either of two range is wrong. So, had to go with hit and trial with both the possibilities as I couldn't find a way to determine the total sector size of my HD :-( .

At first backing up the dump of original partition table.

                                            cp PT.txt PT.txt_bkp


and transferred it to the external hard disk for backup.


                 Now, trying to go with lesser of two range. So, end of /dev/sda10 will be ok but end sector of /dev/sda2 will have to be changed.


New Size of /dev/sda2 will be=end sector of /dev/sda10–start sector of /dev/sda2
                                               = 488394736 - 62926605

                                               = 425468131


                  So, editing the PT.txt file using vi editor and using the obtained value , the new partition table looked like following :

-------------------------------------------------------------------------------------------
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 63, size= 62926542, Id= 7, bootable
/dev/sda2 : start= 62926605, size=425468131, Id= f
/dev/sda3 : start= 0, size= 0, Id= 0
/dev/sda4 : start= 0, size= 0, Id= 0
/dev/sda5 : start= 62926668, size= 31471272, Id= 7
/dev/sda6 : start= 94398003, size=136323528, Id=83
/dev/sda7 : start=230723584, size= 41947136, Id= 7
/dev/sda8 : start=272671308, size=104872257, Id= 7
/dev/sda9 : start=377544704, size=107634360, Id=83
/dev/sda10: start=485179128, size= 3215608, Id=82
------------------------------------------------------------------------------------------


Please note the change in size of /dev/sda2.



                      Now time to write the new partition table using the new file PT.txt . It can be done by :


                     sudo sfdisk --no-reread -f /dev/sda -O PT.save < PT.txt


here, "--no-reread" means don't check if disk is unmounted

           -f force

          "-O PT.save" means save a backup of original partition table in PT.save.


( PT.save is in binary format. To restore the partition table using PT.save:


                           sudo sfdisk --force -I PT.save /dev/sda )



                           Now lets reboot and see if the problem is resolved. Still no luck as the cursor still blinks forever. Now at least , the GParted doesn't detect any error in the partition table and all the error messages are gone and I am able to easily access my all data using the Live USB disk.



                            So, will I have to re-install my operating systems all over again or still something can be done ???



To be continued............