How to Change Partition Number from sda1 to sda2

fdisklinuxpartition

I have a disk with two partitions: sda1 and sda2. I would like change the number of sda1 to sda2 and sda2 to sda1.

It's possible but I don't remember the procedure. i.e. My first partition will be sda2 and the second sda1, so I need to specify a manual order, not an automatic ordering like in fdisk -> x -> f.

How can I change the order? Links to manuals or tutorials are also welcome.

Thanks.

The reason: I have an application that needs to read data from sda1 but the data is in sda2. Changing the partition table is the fastest fix for this issue. The system isn't critical but I don't want to keep the system halted for too much time.

Update: the fdisk version of OpenBSD includes this functionality.

Best Answer

FYI, it is a bad idea and you can lose everything. If you still want to do it, here are the steps:

  1. Don't do it. If this doesn't help, then:
  2. Use the sfdisk tool: First, make a backup of the partition table using

    sfdisk -d /dev/sda > sda.out
    

    Then go for it:

    sfdisk /dev/sda -O sda-partition-sectors.save
    

    You will see something like this

    Checking that no-one is using this disk right now ...
    OK
    
    Disk /dev/sda: 1018 cylinders, 124 heads, 62 sectors/track
    Old situation:
    Units = cylinders of 3936256 bytes, blocks of 1024 bytes, counting from 0
    
       Device Boot Start     End   #cyls    #blocks   Id  System
    /dev/sda1          0+      5       6-     23063+  83  Linux
    /dev/sda2          6    1017    1012    3890128   83  Linux
    /dev/sda3          0       -       0          0    0  Empty
    /dev/sda4          0       -       0          0    0  Empty
    Input in the following format; absent fields get a default value.
    <start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>
    Usually you only need to specify <start> and <size> (and perhaps <type>).
    
    /dev/sda1 :
    

Now it is asking you to give the new details for the 'sda1' partition. So you have to give the numbers of sda2 here. So, I put '6 1012' here and press Enter:

    /dev/sda1 :6 1012
    /dev/sda1          6    1017    1012    3890128   83  Linux
    /dev/sda2 :

Now check if the numbers printed after you pressed Enter are exactly the same as those printed earlier for sda2. If it is okay, continue with giving the new numbers for sda2:

    /dev/sda2 :0
    /dev/sda2          0+      5       6-     23063+  83  Linux
    /dev/sda3 :

This time it was enough to enter "0" in my case - but you have to make sure the numbers aren't messed up in yours.

Next, continue with the other partitions in the same manner. If you already reached the end of the disk, pressing Enter is enough. Finally, check again that all the numbers are okay and save the partition table (or not). If you messed something up, have a look at man sfdisk and the descriptions of '-d', '-O' and '-I' options.

Notice also, that once you've made the crazy changes, you might need to run 'sync' so that the partitions are re-read before you try to mount them.

Related Question