LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How do I recreate a primary partition starting at cylinder 1? (https://www.linuxquestions.org/questions/linux-general-1/how-do-i-recreate-a-primary-partition-starting-at-cylinder-1-a-695141/)

RichyAD 01-05-2009 12:03 PM

How do I recreate a primary partition starting at cylinder 1?
 
History:
I recently had to replaced the motherboard of my dual-boot system. I'm running Fedora 9 (x86_64) and Windows XP 64bit on a single 320 GB Serial ATA3 disk. After the replacement, I got Fedora running without much difficulty. Of course Windows was another matter.
Today I decided to try and see if I could get Windows up and running again. The plan was to reinstall Windows, replace Grub in the MBR and be finished before lunch time ...

My partition table (3 x primary; 1 x extended):
/dev/sda1 * 1 6527 52428096 7 HPFS/NTFS (Windows XP 64bit)
/dev/sda3 6528 6788 2096482+ b W95 FAT32 (shared space)
/dev/sda2 6789 6814 208845 83 Linux (mounted on /boot)
/dev/sda4 6815 38913 257835217+ 5 Extended
/dev/sda6 6815 38651 255730639+ 83 Linux (mounted on /)
/dev/sda5 38653 38913 2096482+ 82 Linux swap / Solaris

Mistake:
During the Windows installation, I thought that it might be a good idea to remove primary partition with the previous Windows installation, put it back (forcing windows to format), and start afresh ... Big mistake! After removing the partition, the Windows installer complained that it could not create a partition because the maximum number of partitions was reached.
So I popped in my Fedora 9 DVD, booted in rescue mode, and started fdisk to recreate the partition. It showed the following partition table:

/dev/sda1 6528 6788 2096482+ b W95 FAT32 (shared space)
/dev/sda2 6789 6814 208845 83 Linux (mounted on /boot)
/dev/sda3 6815 38913 257835217+ 5 Extended
/dev/sda4 38653 38913 2096482+ 82 Linux swap / Solaris
/dev/sda5 6815 38651 255730639+ 83 Linux (mounted on /)

But fdisk will not allow me to recreate the original Windows partition (cylinders 1-6527) either! It only provides access to the extended partition. I had a look around, but I can't seem to find any useful threads or links. I did read somewhere that (apparently) the order of deleting partitions is: logical, extended, primary. But that does not help me much anymore ...

So my questions are:
- Why can't I get access to the cylinders 1-6527? Is this possible in "expert" (x) mode of fdisk? (I did not dare try this for the moment)
- How do I get my original partitioning back without destroying the data my linux partitions? Can I remove the entire table and rebuild it up from scratch?

Any help will be appreciated.

pixellany 01-05-2009 12:46 PM

I did not know that fdisk would not do this.....you might try GParted. This comes with many "live CD" Linux distros, and is also available on a standalone bootable CD. It IS possible to do what you are asking.

(I don't currently have a system where I can do a test with fdisk.)

syg00 01-05-2009 03:20 PM

Nothing to do with fdisk - blame the crappy M$oft installer.
It appears to have converted one of the logicals (the swap partition) to a primary. What a lame piece of junk.
Your orphaned space is now unreachable as you now have 4 primaries, and Windoze will only install into a primary partition. What a lame piece of junk. I have seen other threads with this sort of partition layout, and wondered how it happened - maybe now we know.

You're lucky it's the swap - from the Linux liveCD delete it, run the 'doze install, re-add the swap (as a logical), and fix up fstab in need.

CJS 01-05-2009 06:00 PM

RichyAD, as syg00 all ready pointed out, your sda5 swap logical partition was somehow converted into the sda4 primary partition; yet the sda4 swap partition is still inside of your sda3 extended partition, so basically your partition table is corrupt at this point. I've helped a number of people easily correct partition table problems like that using a trick with "sfdisk", so if you want, I can help you fix your partition table if you can first post the output of:
Code:

fdisk -lu
sfdisk -d /dev/sda

And we can work from there if you want.

RichyAD 01-06-2009 06:36 AM

Thanks for the replies, guys.

A primary partition INSIDE an extended partition? OK ... I didn't know a partition table could get screwed up so much. I'd have to agree with syg00: what a lame piece of junk! Anyway, let's see if I can clean up this mess (with your help).

fdisk -lu /dev/sda:
Code:

omitting empty partition (5)

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x0001ed87

  Device Boot      Start        End      Blocks  Id  System
/dev/sda1      104856255  109049219    2096482+  b  W95 FAT32
/dev/sda2      109049220  109466909      208845  83  Linux
/dev/sda3      109466910  625137344  257835217+  5  Extended
/dev/sda4      620944380  625137344    2096482+  82  Linux swap / Solaris
/dev/sda5      109467036  620928314  255730639+  83  Linux

sfdisk -d /dev/sda:
Code:

# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=104856255, size=  4192965, Id= b
/dev/sda2 : start=109049220, size=  417690, Id=83
/dev/sda3 : start=109466910, size=515670435, Id= 5
/dev/sda4 : start=620944380, size=  4192965, Id=82
/dev/sda5 : start=109467036, size=511461279, Id=83


CJS 01-06-2009 06:49 AM

OK, how about copying/pasting the following into a text document:
Code:

# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=      63, size=104856192, Id= 7
/dev/sda2 : start=104856255, size=  4192965, Id= b
/dev/sda3 : start=109049220, size=  417690, Id=83
/dev/sda4 : start=109466910, size=515670435, Id= 5
/dev/sda5 : start=109467036, size=511461279, Id=83
/dev/sda6 : start=620944380, size=  4192965, Id=82

And save that text document as "partition_table.txt" on your desktop. When you copy the above text, make sure there are no spaces at the end of any of the lines. Then run the following command as root:
Code:

sfdisk --force /dev/sda < /home/<username>/Desktop/partition_table.txt
That will produce a lot of output including some warnings, so you don't need to be alarmed, but please post the output of the above command. Next reboot, and then post again the output of:
Code:

fdisk -lu
sfdisk -d /dev/sda

And we can work from there.

yancek 01-06-2009 10:02 AM

The sda4 swap partition is not in the extended it is a primary partition so as syg00 suggested, delete the swap. You will then be able to create a primary partition. In Linux, logical partitions start with 5. If you create an extended partition as the first partition on your drive, then create a logical partition, it will be sda5 not sda2.

I don't know if you will be able to access cylinders 1-6527 but it seems deleting sda4 and trying would be the simplest.

RichyAD 01-06-2009 12:04 PM

CJS, are you sure that's OK?

I wanted to have a look at the option from syg00 and yancek (without saving the new table!). So using fdisk I removed the swap partition. This indeed allowed me to create a new primary partition and gave me access to cylinder 1. However the windows partition would then run from sector 63 to 104856254 (and not 104856192) as indicated here:
Code:

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x0001ed87

  Device Boot      Start        End      Blocks  Id  System
/dev/sda1      104856255  109049219    2096482+  b  W95 FAT32
/dev/sda2      109049220  109466909      208845  83  Linux
/dev/sda3      109466910  625137344  257835217+  5  Extended
/dev/sda4  *          63  104856254    52428096    7  HPFS/NTFS
/dev/sda5      109467036  620928314  255730639+  83  Linux

As said, I haven't saved the partition table yet. I would prefer CJS's option, because it also restores the original device order (saving me from also having to change fstab, etc.). So I was planning to use a modified version of CJS's sfdisk instructions:
Code:

# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=      63, size=104856254, Id= 7
/dev/sda2 : start=104856255, size=  4192965, Id= b
/dev/sda3 : start=109049220, size=  417690, Id=83
/dev/sda4 : start=109466910, size=515670435, Id= 5
/dev/sda5 : start=109467036, size=511461279, Id=83
/dev/sda6 : start=620944380, size=  4192965, Id=82

That should work, right? Or will there be some problem with sfdisk not accepting this sector structure?

Thanks.

CJS 01-06-2009 12:10 PM

I've double-checked the partition table that I gave in my post, so to the best of my knowledge, yes I think that it is OK to use those instructions I gave. As I mentioned I've helped others correct their partition table problems with the same method, so I think you should be fine. Please see these threads if you would like some examples:

http://ubuntuforums.org/showthread.php?t=1032234
http://ubuntuforums.org/showthread.php?t=1031659
http://ubuntuforums.org/showthread.php?t=1008458
http://ubuntuforums.org/showthread.php?t=1027533

But it's of course up to you, I can understand your reluctance to trust someone you don't know. Please do whatever you feel is best. :)

EDIT: The partition table you show above I think has some problems; the size of sda1 will make sda1 extend into sda2, because:
Code:

size = stop sector - start sector + 1
Therefore the stop sector of your sda1 partition is 104856316 according to the table you posted, yet your sda2 partition should start at 104856255, which is before that value. Thus I would recommend going with the partition table I gave in my last post, but it's up to you.

RichyAD 01-06-2009 12:15 PM

CJS, just out of curiosity: how did you come up with sector 104856192 instead of 104856254. I mean, 104856254 seems to make more sense since the next partition starts at 104856255 ...

Anyway, thanks for the advice. I'll let you know if it works.

CJS 01-06-2009 12:18 PM

Please see my edit in the last post; I think the size value you are using for sda1 is not going to work. Please don't confuse the "size" number with the stop sector of the partition. :)

RichyAD 01-06-2009 12:34 PM

Oops, my bad ... I didn't realise that the format of sfdisk was different. I thought it also indicated start and STOP (instead of SIZE). I'm probably not completely sober yet after the holidays! :)

Thanks, CJS, you saved me from a lot of misery.

RichyAD 01-06-2009 02:53 PM

I used the instructions as given by CJS and (so far) all went smoothly. The new partition table is now:
Code:

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x0001ed87

  Device Boot      Start        End      Blocks  Id  System
/dev/sda1              63  104856254    52428096    7  HPFS/NTFS
/dev/sda2      104856255  109049219    2096482+  b  W95 FAT32
/dev/sda3      109049220  109466909      208845  83  Linux
/dev/sda4      109466910  625137344  257835217+  5  Extended
/dev/sda5      109467036  620928314  255730639+  83  Linux
/dev/sda6      620944380  625137344    2096482+  82  Linux swap / Solaris

I've only checked to see if I could mount the linux partition and that went OK. Tomorrow I'll try the Windows install, put Grub back in the MBR, and hopefully be finished before lunch time. ;)

One note though: I was a bit disappointed to see that even with this method I could not assign device numbers (e.g. W95 FAT32 as /dev/sda3); this was simply ignored by sfdisk. No matter, I'll edit fstab to correct for the new structure. But, in this case, the suggestion by syg00 and yancek of deleting the (primary) swap partition would have been the simpler solution.

Thanks.

CJS 01-06-2009 03:05 PM

Quote:

Originally Posted by RichyAD (Post 3399169)
One note though: I was a bit disappointed to see that even with this method I could not assign device numbers (e.g. W95 FAT32 as /dev/sda3); this was simply ignored by sfdisk. No matter, I'll edit fstab to correct for the new structure. But, in this case, the suggestion by syg00 and yancek of deleting the (primary) swap partition would have been the simpler solution.

Thanks.

You said in post #8 that using the partition table I gave would restore the original device order, so I thought that's what you wanted. You can assign device numbers using that sfdisk method, you just have to rearrange the output of the sfdisk -d accordingly. For instance, if you want the FAT32 partition to be sda3 and then make the current sda3 linux partition sda2, you could use the following table:
Code:

# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=      63, size=104856192, Id= 7
/dev/sda2 : start=109049220, size=  417690, Id=83
/dev/sda3 : start=104856255, size=  4192965, Id= b

/dev/sda4 : start=109466910, size=515670435, Id= 5
/dev/sda5 : start=109467036, size=511461279, Id=83
/dev/sda6 : start=620944380, size=  4192965, Id=82

You just have to be careful to actually swap the lines like shown above and not simply change the device numbers from sda3 to sda2 and vice versa in the sfdisk partition table. So swapping primary partitions around is not a big deal, but it is not a good idea to swap the logical partitions around, because the logical partitions should be in the physical order on the drive in order to avoid problems in some cases. But if you prefer to delete and recreate partitions with gparted/fdisk, that could work just fine too. :)

RichyAD 01-07-2009 05:21 PM

Sorry for the confusion, CJS. I only noticed the device order later in the thread. What I meant was the "original" order as mentioned at the start of post #1.

I did indeed try to change the device order by simply changing the device numbers in the sfdisk input file. That explains why it didn't work ... Another case of RTFM. Well, live and learn! Anyway, I changed the order and now everything was back to original.

And I got some extra practice because (would you believe it!) the Windows installer did it again! After I put everything back, I started installing Windows. And this time I did NOT remove the partition but simply told it to (quickly) format the partition. After it had finished I checked the partition table and my Linux swap partition had been deleted! Unbelievable! I guess it updated the partition table (incorrectly) when it installed the Windows bootloader or something.

I've now put back the table again, reinstalled Grub and everything is working properly. So for me, this is finally SOLVED.

Thanks for all the help.


All times are GMT -5. The time now is 02:57 PM.