How to: Change Partition Numbers In Linux

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
chopsuwe
Posts: 8
Joined: Sun 31 Jan 2010, 19:23

How to: Change Partition Numbers In Linux

#1 Post by chopsuwe »

Change Partition Numbers in Linux

After installing and removing various Linux versions, adding, deleting and moving partitions the numbering is all over the place and and I want to get it back in order again. The drive is now partitioned like this:

|-SDA4 Windows-|-unallocated space-|-SDA3-Data-|

And I want it to look like this:

|-SDA1 Windows-|-unallocated space-|-SDA2-Data-|

There are ways of convincing Linux to swap the partition numbers after booting but they may not be suitable for various reasons and some of us just want things neat and tidy. This method will delete the partition table and then recreate it with the correct numbers. It is very likely the drive will not be bootable after the procedure but that's reasonably easy to fix later. No changes will be made to the disk until we write them in the last step. If you make a mistake at any time up until that point you can simply quit and start again.

WARNING: Although the procedure is reasonably safe we are about to delete the partition table. If you make a mistake here it is easy to lose all your data. Make a backup first!

Make a backup or all your data.
Unplug any other drives so you can't accidentally mess them up.
Open the terminal, start fdisk and tell it the drive you want to work with. You may get a warning like I did but it's doesn't seem to be a problem.

Code: Select all

# fdisk /dev/sda

The number of cylinders for this disk is set to 30401.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
(Depending on which version of Linux you are using you may need to type "sudo fdisk /dev/sda". Puppy Linux doesn't need it.)

Print out the partition table, change to sectors as the unit and print it again. You should use sectors as unit, it's the actually written place on the disk.

Code: Select all

Command (m for help): p

Disk /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda4   *           1        4088    32833536    7  HPFS/NTFS
/dev/sda3            4088       30401   211361472+   7  HPFS/NTFS

Command (m for help): u
Changing display/entry units to sectors

Command (m for help): p

Disk /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda4   *        2048    65669119    32833536    7  HPFS/NTFS
/dev/sda3        65669120   488392064   211361472+   7  HPFS/NTFS
Make a note of the sector where each partition begins and where it ends. This is very important as we need these numbers to create the new partition. If you get them wrong you will lose data. Either write them down, take a screen cap or photo.

We have all the information we need. Delete all partitions using the "d" command (Partition number 1 = SDA1, partition 2 = SDA2, etc)

Code: Select all

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

Command (m for help): d
Selected partition 3

Command (m for help): d
No partition is defined yet!
All the partitions have been marked to be deleted. Don't worry even if you write the empty partition table to the disk now your data is still on the drive, it's just the partition table that tells the computer where to find the data that has been deleted. You can find a partition undelete program and you should be get all your data back.

It's time to recreate the partition table with the correct partition numbers. Start with the first partition. I'm going to make it a primary partition and call it SDA1
Copy the starting and ending sectors from the second print out we did above.

Code: Select all

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (63-488397167, default 63): 2048
Last sector or +size or +sizeM or +sizeK (1234-488397167, default 488397167): 65669119
Continue with the remaining partitions. Make sure you get the start and end sector numbers correct.

Code: Select all

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (63-488397167, default 63): 65669120
Last sector or +size or +sizeM or +sizeK (7890-488397167, default 488397167): 488392064
Fdisk is a Linux program so it assumes we want EXT3 partitions by default. If your partitions were EXT3 you can skip this step. Mine are Windows NTFS so I need to change then from type 83 (EXT3) to 7 (HPFS/NTFS)

Code: Select all

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 7
Changed system type of partition 3 to 7 (HPFS/NTFS)

Command (m for help): t   
Partition number (1-4): 2
Hex code (type L to list codes): 7
Changed system type of partition 3 to 7 (HPFS/NTFS)

Up until this point we have told Fdisk the changes to be made but we haven't written them to the disk yet. If you are happy you haven't made any mistakes press write them to the disk now.

Code: Select all

Command (m for help): w
Fdisk will show a confirmation message and then you can quit the program (Q). I haven't shown that step as my drive is now fixed and I don't want to mess with it any more.

Use Gparted or any other program to check the partition numbers are correct. You may find your OS will no longer boot. I had no problem with Vista but you may need to fix the MBR with bootrec.exe /fixmbr and bootrec /fixboot. Google for more instructions. If you are using Linux you may need to update the boot manager to point at the right partition. Again Google it because I don't know how.

chopsuwe
Posts: 8
Joined: Sun 31 Jan 2010, 19:23

#2 Post by chopsuwe »

I realise this is not specifically for Puppy Linux but Puppy is my favourite distro for fixing Windows problems.

hal8000
Posts: 6
Joined: Tue 05 Mar 2013, 18:22

#3 Post by hal8000 »

Good writeup.
I would add two things though.
Changing entries in the partition table does not overwrite the mbr, so your windows system should still be able to boot. The bootloader (whether grub, burg or standard windows bootloader is the first 464 bytes, the partition table (modified by fdisk is the next 64 bytes).

Windows requires the partition to be marked active, and this caan be checked
with :

fdisk /dev/sdx

where x is the drive letter usually "a" if you have one hard drive. An asterisk
should be displayed against the active partition, if not toggle the boot flag by pressing a in fdisk.

Finally you dont have to return to windows to write a windows bootloader.
With puppy (or any linux with ms-sys) just type

ms-sys -m /dev/sda

This will create a standard Windows/XP/2003 bootloader (the first 464 bytes) without altering your partition table.

Post Reply