How to fix partition table ordering?

4

2

I was going to install a completely fresh Linux distribution (Mageia) and I saw it as an opportunity to reorganize my partitions. I wanted all my non-Linux (Windows, restore, etc) partitions at the beginning and my Linux ones at the end so I can more easily create new Linux partitions if I want to test some more distributions.

I used GParted to delete the existing Linux partitions (I backed up the data wanted to preserve) and moved the remaining ones to the front. I would later create the Linux partitions in the Mageia installer.

It took some time but in end GParted finished without problems. Mageia was installed without a hitch too.

When booting into Mageia I had some (unrelated?) issue with the ATI Radeon video card (still unresolved) that leaves me with just the console and when rebooting I saw Error: Can't have overlapping partitions zipping by.

No problem, I thought, I'll just boot GParted again and fix it. Alas, GParted shows the whole disk as unallocated. OK, back to Linux. Starting parted from command line gives me

[root@localhost ~]# parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: Can't have overlapping partitions.

fdisk should give me more info and it did:

[root@localhost ~]# fdisk -l -u /dev/sda

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 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: 0x2bd2c32a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      206847      102400    7  HPFS/NTFS
/dev/sda2          208845  1953520064   976655610    5  Extended
/dev/sda3       950740992  1013655551    31457280    7  HPFS/NTFS
/dev/sda4      1013655552  1015754751     1049600   12  Compaq diagnostics
/dev/sda5          208896   409808895   204800000    7  HPFS/NTFS
/dev/sda6       409810944   950740991   270465024    7  HPFS/NTFS
/dev/sda7      1015757883  1034184374     9213246   82  Linux swap / Solaris
/dev/sda8      1034184438  1953520064   459667813+  83  Linux

If I sort by partition start it seems that /dev/sda5 and /dev/sda6 should be called /dev/sda3 and /dev/sda4 and vice versa.

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      206847      102400    7  HPFS/NTFS
/dev/sda2          208845  1953520064   976655610    5  Extended
/dev/sda5          208896   409808895   204800000    7  HPFS/NTFS
/dev/sda6       409810944   950740991   270465024    7  HPFS/NTFS
/dev/sda3       950740992  1013655551    31457280    7  HPFS/NTFS
/dev/sda4      1013655552  1015754751     1049600   12  Compaq diagnostics
/dev/sda7      1015757883  1034184374     9213246   82  Linux swap / Solaris
/dev/sda8      1034184438  1953520064   459667813+  83  Linux

From googling I found that testdisk can fix this.

  1. How do I use testdisk? The interface is quite confusing.
  2. Can I run it from Linux/Windows or should I boot a live version?

Thanks in advance,

Nick


I followed the instructions in the answer below but it doesn't work.

These partitions I have to re-create:

/dev/sda3       950740992  1013655551    31457280    7  HPFS/NTFS
/dev/sda4      1013655552  1015754751     1049600   12  Compaq diagnostics

This is what happens:

[root@localhost ~]# fdisk /dev/sda

Command (m for help): d
Partition number (1-8): 3

Command (m for help): d
Partition number (1-8): 4

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First sector (950743040-1953520064, default 950743040): 950740992
Value out of range.
First sector (950743040-1953520064, default 950743040):

The 1st partition needs to start at 950740992 but the new one may only start at 950743040.

How do I proceed?

Koohoolinn

Posted 2012-01-22T11:38:21.430

Reputation: 141

It seems possible to migrate a question. How do I do that? – Koohoolinn – 2012-01-29T11:15:09.167

You can either flag it for moderator attention and ask for migration or post to the "Vote to close" chat channel - a migration needs five migration votes from higher-reputation users. – None – 2012-01-29T17:39:01.070

Answers

2

The best solution I found for fixing the partition order:

Enter into the disk partition place: 
fdisk /dev/sda

Command (m for help):   m  
Command action
  ...
   l   list known partition types
   m   print this menu
   p   print the partition table
   q   quit without saving changes
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help):   x

Expert command (m for help):   m
Command action
...
   f   fix partition order
   m   print this menu
   p   print the partition table
   q   quit without saving changes
   r   return to main menu
   v   verify the partition table
   w   write table to disk and exit

Expert command (m for help):   f
Done.

Expert command (m for help):   w

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Done!

jfajunior

Posted 2012-01-22T11:38:21.430

Reputation: 121

2

The overlapping partition problem arises partly due to the oddities of PC partitioning.

The partition table has space for exactly 4 "primary partition" entries. If you need to have more than 4 partitions, there is a mechanism called "extended" partitions - it actually creates a large partition (in your case partition #2) which in turn has an own sub-partition table, allowing for additional entries called "logical" partitions.

What you have done is creating an extended partition spanning from sector 208845 to 1953520064, but also additional primary partitions 3 and 4 overlapping the disk space reserved for the "extended" partition.

The most easy way to resolve matters would be as follows:

  • delete partitions 3 and 4 using Linux fdisk
  • re-create them (again using fdisk) as logical partitions within the extended partition using the same sector start/end values as before

If you would like the logical partition order to reflect their "physical" location on the disk, fdisk can help as well. Just press "x" to activate extended functionality and use the f ix partition order function. You might need to adjust your fstabs for Linux systems after that - Windows has GUID-based partition identification and should not care about the order.

syneticon-dj

Posted 2012-01-22T11:38:21.430

Reputation: 621

1

Be warned: I tried the advanced fix option in fdisk and rebooted my system. This had disastrous effects in that my /dev/sda8 became /dev/sda5 in my case and the /boot/grub does not change as a result.

So the obvious fix to this problem to avoid the mangling is:

$ fdisk /dev/sda
x
f
w
$ update-grub
$ grub-install

but first make sure you have grub commands (above) are installed since it seems to be deprecated in some systems.

Eamonn Kenny

Posted 2012-01-22T11:38:21.430

Reputation: 141