Change the number of the partition from sda1 to sda2

I just did this in an easier way:

# sfdisk -d /dev/sdb > sdb.bkp

leave a copy for safety

# cp sdb.bkp sdb.new 

now edit sdb.new changing ONLY the lines order and partition numbers, as in my case:

from

# partition table of /dev/sdb
unit: sectors

/dev/sdb1 : start=  1026048, size=975747120, Id=83
/dev/sdb2 : start=     2048, size=   204800, Id=83
/dev/sdb3 : start=   206848, size=   819200, Id= b
/dev/sdb4 : start=        0, size=        0, Id= 0

to

# partition table of /dev/sdb
unit: sectors

/dev/sdb1 : start=     2048, size=   204800, Id=83
/dev/sdb2 : start=   206848, size=   819200, Id= b
/dev/sdb3 : start=  1026048, size=975747120, Id=83
/dev/sdb4 : start=        0, size=        0, Id= 0

then throw it back to the disk partition table?

# sfdisk /dev/sdb < sdb.new

My numbering sequence was mangled after I shrank&shifted right the only partition (sdb1) to add two smaller partitions at the start of the disk using gparted.

If the last command does not work, as in my case, change it for:

# sfdisk --no-reread -f /dev/sdb < sdb.new

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.


All the existing solutions look very safe, but as a result quite complicated.

I wanted to reorder all my partitions so they'd be in order.

If you are more experienced, have non-essential or backed-up data and are happy to fix any issues as a result, you can boot from a Live CD and do it this way:

sudo fdisk /dev/sdX

then:

  • x to enter Expert Mode
  • f to fix the drive order (possibly i to ignore any warnings if required)
  • r to return to normal mode
  • w to write changes to the disk and exit

Additional

I don't recall the exact next steps as I only did it once and haven't retested, but as I was also moving the install I also mounted the partitions and copied all files:

sudo mkdir /mnt/sda4
sudo mount /dev/sda4 /mnt/sda4
sudo mkdir /mnt/sda5
sudo mount /dev/sda5 /mnt/sda5
date;cp -a /mnt/sda4/* /mnt/sda5;date

And then got the partition ids:

sudo blkid

and updated the following to reflect the new partitions UUID values:

  • /mnt/sda5/boot/grub/grub.cfg
  • /mnt/sda5/etc/fstab

I then updated GRUB2:

sudo grub-install --root-directory=/mnt/sda5 /dev/sda