LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Gparted Error/HD Fix (https://www.linuxquestions.org/questions/linux-general-1/gparted-error-hd-fix-885835/)

Ritzbitts 06-11-2011 11:54 PM

Gparted Error/HD Fix
 
I recently deleted several partitions on my HD accidentally, and restored them with Testdisk. Now, Gparted does not load correctly and prints the error "Can't have a partition outside the disk!"

This is the result of sudo fdisk -lu:
Code:

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders, total 312581808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe567c063

  Device Boot      Start        End      Blocks  Id  System
/dev/sda1  *        2048    61442047    30720000    7  HPFS/NTFS
/dev/sda2        61448625    81915432    10233404  83  Linux
/dev/sda3        81915435    86011986    2048276  82  Linux swap / Solaris
/dev/sda4        86011987  312592769  113290391+  f  W95 Ext'd (LBA)
/dev/sda5        86012010  312576697  113282344  83  Linux

And this is the result of sudo fsdisk -d:
Code:

Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=    2048, size= 61440000, Id= 7, bootable
/dev/sda2 : start= 61448625, size= 20466808, Id=83
/dev/sda3 : start= 81915435, size=  4096552, Id=82
/dev/sda4 : start= 86011987, size=226580783, Id= f
/dev/sda5 : start= 86012010, size=226564688, Id=83

So clearly, my extended partition is a little screwy. There's a similar thread at the Ubuntu forums here, but no explanation for the steps is given, and I don't want to damage my data by guessing at the math.

Can anyone help me correct this problem so that I can run Gparted? My computer runs fine otherwise, but this is preventing me from getting certain things done!

Thanks!

ajlewis2 06-12-2011 06:00 AM

You are able to show the disk using fdisk; so maybe you can fix it using fdisk. First of all, remember that linux fdisk does not reformat like windows fdisk does; so deleting and remaking a partition with linux fdisk does not remove data. I've done this years ago and I'm pretty sure the way fdisk works has not changed.

First make a hard copy of that printout you have of the fdisk -l results. You will be using the data from the top (fdisk -lu).

I don't think you can do this with the partition mounted; so you will need to use a live cd of some sort. If you have one, good. If not, I'd get one of the rescue disks that smaller than a regular distro.

Boot the disk and go to a terminal and get to root. Run fdisk -l again to be sure the numbers are the same as what you already have. Then run fdisk /dev/sda. If you haven't used it before, look at the list of commands. You will need to delete sda5 and sda4 and then remake sda4 using the end as the same number as the total sectors at the top. That gets rid of the error, hopefully. Then make sda5 using the exact cylinder numbers on your printout. Save and close fdisk. Run fdisk -l again to be sure sda5 has those same numbers as before. Then boot your machine to Linux.

And yes, I'd be afraid to do this now. The way drives work may have changed a lot in the 10 years or so since I did this. I'd back up my data and have a really good reason to want to fix this. In my case, the partition table got hosed and I just happened to have a printed copy of the table like the one you posted here. I went in and remade the partitions and all was well. With fdisk it is the partition table that you change, not anything else on the disk.

PS: Congratulations on being able to use testdisk to restore your partitions!

Ritzbitts 06-12-2011 01:00 PM

Solved
 
Quoted from my thread at the Ubuntu forums:
Quote:

Originally Posted by coffeecat (Post 10930973)
Testdisk mistakenly defining the end sector of an extended partition seems to be a fairly common issue. Fortunately fixable with forum member srs5694's fixparts application:

http://www.rodsbooks.com/fixparts/

Just for the record, here is an earlier page on this topic written before he developed fixparts:

http://www.rodsbooks.com/missing-parts/index.html

With a thread title such as you have chosen, I wouldn't be surprised if he doesn't post himself!

Thankyou so much! I had issues running fixparts (won't go into them here, no point), so I decided to do things manually, as per the second link. Piece of cake! For others having this issue, start by booting from a different disk (live CD, USB install). In my case I booted from a Back|Track 5 USB.

Start by typing the command:
Code:

sfdisk -d /dev/sda > parts.txt
Where sda is the device name. Sfdisk will give you a warning and write your partition table to "parts.txt" in your Home folder. When you open the file, you'll see something like this:
Code:

# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=    2048, size= 61440000, Id= 7, bootable
/dev/sda2 : start= 61448625, size= 20466808, Id=83
/dev/sda3 : start= 81915435, size=  4096552, Id=82
/dev/sda4 : start= 86011987, size=226580783, Id= f
/dev/sda5 : start= 86012010, size=226564688, Id=83

Next, run fdisk -lu. You'll see something like this:
Code:

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders, total 312581808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe567c063

  Device Boot      Start        End      Blocks  Id  System
/dev/sda1  *        2048    61442047    30720000    7  HPFS/NTFS
/dev/sda2        61448625    81915432    10233404  83  Linux
/dev/sda3        81915435    86011986    2048276  82  Linux swap / Solaris
/dev/sda4        86011987  312592769  113290391+  f  W95 Ext'd (LBA)
/dev/sda5        86012010  312576697  113282344  83  Linux

Now for the magic. Subtract the start value for the problem partition from parts.txt ( 86011987 ) from the total number of sectors ( 312581808 ). Double check your math and copy paste if you can:

312581808 - 86011987 = 226569821

Now, replace the size of the problem partition in parts.txt (226580783) with the new number (226569821). Done right, this number should be smaller than that which it replaces. Save as a different file name (I used "part.txt") so that you have the original backed up before making changes. Now run this command:
Code:

sfdisk /dev/sda < part.txt
Where sda is your device name and part.txt is the new partition table file you just made. Sfdisk gave me an error, apparently it's fussy even when partition tables are valid. So, I triple checked all the math and ran this command:
Code:

sfdisk --force /dev/sda < part.txt
Which went off without a hitch. Everything works fine now, and I can use Gparted without incident.

Remember, this only works if the problem is caused by an extended partition overlapping the disk boundary. This is not the solution if the extended partition is cutting into a logical partition, although similar steps can be taken to rectify it, and may not work if the problem is related to a primary partition.

I hope my instructions are clear and this helps someone in the future! Big thanks to srs5694 and coffeecat!

ajlewis2 06-12-2011 03:24 PM

Quote:

Originally Posted by Ritzbitts (Post 4383491)

Now, replace the size of the problem partition in parts.txt (226580783) with the new number (226569821). Done right, this number should be smaller than that which it replaces. Save as a different file name (I used "part.txt") so that you have the original backed up before making changes. Now run this command:
Code:

sfdisk /dev/sda < part.txt
Where sda is your device name and part.txt is the new partition table file you just made. Sfdisk gave me an error, apparently it's fussy even when partition tables are valid. So, I triple checked all the math and ran this command:
Code:

sfdisk --force /dev/sda < part.txt

I think you will find that the end cylinder for sda4 is now 312581808 which is the beginning cylinder (86011987) plus the new number (226569821). sfdisk has been fed the new number for the partition table which it then rewrote. So this shows that the same thing would have been achieved by rewriting the table with fdisk using the number for the total sectors as the ending cylinder for sda4, the extended partition.

Code:

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders, total 312581808 sectors

It's doing the same thing with a different program.

Ritzbitts 06-13-2011 07:34 AM

I see. Would you say one technique is preferable to the other? Sfdisk was pretty fast and straightforward, but then, I've never rewritten a partition table with fdisk, so I can't compare.

ajlewis2 06-13-2011 09:55 AM

Quote:

Originally Posted by Ritzbitts (Post 4384106)
I see. Would you say one technique is preferable to the other? Sfdisk was pretty fast and straightforward, but then, I've never rewritten a partition table with fdisk, so I can't compare.

I'm not as familiar with sfdisk as I am fdisk; so fdisk is preferable to me, because I'd be more likely to get it right. I'd say it is best to use the way in which the person would most likely get the numbers correct. Also, the sfdisk way does look easier once you work out the math. The fdisk way does not require any math. You just poke in the total sector number into the end cylinder for sda4. But then you have to do all that typing of D and N and getting the right partition types and saving it and so on. Your way is probably easier, but I really didn't know your way; so I gave you my way. I'm glad you got it fixed.

Oh, come to think of it, the best technique would have been to have had that partition table on paper so that you could have just remade it correctly to start with. :-) But, I admire anyone who can come up with one when they don't have those numbers.

sputnikeee 12-31-2011 03:18 AM

Well I do realize this thread is six months old, but I do feel it is relevant & timely to "bump" it as Ritzbitts solution helped me much and other seekers should have access to this info.
I had the same problem, apparently caused by the testdisk bug? Anyhow I had to jiggle around with the values somewhat, the simple subtraction didn't get me there, but closely looking at the errors I would get in GParted finally led me to the correct number to "feed" back to the drive.
What finally solved it for me was formatting the drive as Fat32. The ext formats would just not give me enough info somehow to solve it. By making an intentionally slightly smaller Fat32 partition I got it to format ok. Then the real clincher was to run check in GParted, the Fat32 partition somehow automatically decided it should fill all the free space as well, but the sector number I had used was one sector too large and it showed that error clearly in the Gparted details. Then I knew, simply changed my "part.txt" file to one less sector and Bob's your Uncle (even if you're American).
Incidentally, another helpful thing was that although sfdisk says it is checking to see if anyone's using the drive, I would leave GParted running while doing "feed" updates, it didn't mind. Easier than restarting GParted over and over. However, be sure and do "refresh drives" from the top left "GParted" menu.
Thanks Ritzbitts! You kept me going, I had given up. And a very special THANK YOU to Rod Smith for his very unique fixit stuff.
OMG, I just spilled a glass of water all over the drive while powered and now it won't spin up! Hopefully it'll dry. It's just a tiny old drive anyhow, but I did kinda need it. At least I did want to document this here for others. :o

Ritzbitts 01-01-2012 01:46 AM

Glad I was able to help. Hope the HD dries!


All times are GMT -5. The time now is 04:26 AM.