37

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.

7 Answers7

52

I just did this in an easier way:

# sfdisk --dump /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 --force /dev/sdb < sdb.new
  • This worked for me and I didn't end up needing that 2nd command. – Aaron Franke Nov 06 '16 at 23:50
  • For me as well, thanks ;) ... Note make sure all your partition are unmounted before commands – intika Aug 30 '18 at 05:50
  • This worked perfectly for my USB. I use Rufus to boot FAT32 partition to NTFS partition with EFI so I can reinstall Windows 10 if there are issues. However, I have an extra partition for getting files from my PS4. This allowed me to set that partition as sdb1 so PS4 sees it as the first option. The only thing I can nitpick about is replacing -d with --dump argument just to make it clear what the command is doing. Apart from that, I believe this should be the best answer for it's simplicity. – Nova Mar 06 '20 at 14:26
  • Hi, I'm from the far future. Linux works more or less exactly the same way, and this worked for me even though I did it with a live, mounted drive on a headless Raspberry Pi, like an idiot would do. It was still fine. – Kevin Teljeur Jun 08 '20 at 22:38
  • Works like a charm! After cloning ESXi OS I got the following error: "Bank 5: not found Bank 6: invalid configuration". After doing some research I found out that ESXi expects the two 250.0 MB partitions to be numbered 5 and 6. I've applied your method using a LiveCD and the problem was solved in less than 5 minutes! – Radu May 18 '21 at 06:36
22

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.

  • Thanks for the answer. You are right, this is scary :) . – Rufo El Magufo Aug 15 '11 at 02:27
  • Wonderfully evil. What happens if the numbers aren't the same? Give up and repartition? (That's what I did) – supervacuo Aug 19 '12 at 05:44
  • 1
    @supervacuo Testisk might help you then. Search for testdisk package in your distro's software database. – rozcietrzewiacz Aug 20 '12 at 06:29
  • @supervacuo If the numbers aren't the same then your new partitions aren't in the same place as the old partitions and you will potentially corrupt them. If you don't do anything else to the disk then you can restore the partition table that you hopefully backed up before starting and try again. If not then you'd better hope that the correct numbers are still on your console somewhere, or that you can remember the exact size and location of your partitions and work the numbers out. – micheal65536 May 21 '16 at 07:21
15

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
SharpC
  • 359
4

I just discovered a surprisingly easy solution to this problem. Say you have only one partition left named /dev/sda3. You want it to be called /dev/sda1. Open gdisk, convert the partition table to GPT, write, and exit. Then open it again, press sort (this will change the GPT number to 1), and then convert the GPT back to MBR via the expert options.

erch
  • 5,030
  • This seems dangerous. gdisk warns against this: converting MBR to GPT format in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE!. Moreover, it is not clear which expert option converts from GPT to MBR. – Luis A. Florit Feb 14 '21 at 23:19
3

You would have to rewrite the drive partition table to reverse the order, which is a bit risky to do. If I were you I would fix the problem that makes you want to swap the order of device names and leave the partitions alone.

For instance if you have a script that needs to run on both machines and you want it to access the same device, then create a link in a different directory (not /dev) which is the same name on both servers, but links to /dev/sda1 on one, and /dev/sda2 on the other.

You can use any partition editor (fdisk or cfdisk) to edit the partitions but you need to either write down the current settings or take a photo of the screen. Delete the two partitions, and then create them manually, specifying all the correct values. Boot the system with a LiveCD to do this.

  • I can't change anything. My system is a Xen DomU and the problem is in the Dom0. I could recreate the order of partitions but is more complex in my situation. But anyway, I would like know how change the order. – Rufo El Magufo Aug 15 '11 at 00:06
2

First off, this whole thing is an exercise in trying your best to break things, but anyway, here's another approach you can try.

Note: You didn't mention where you boot from, and this solution, and no other one at the time I am writing this, are addressing what you would have to do to get LILO or GRUB to work properly.

/dev/sda1 and /dev/sda2 are block devices (8,1) and (8,2). You can try using mknod to simply swap the two.

telinit 1
(...)
rm -f /dev/sda{1,2}; mknod /dev/sda1 b 8 2; mknod /dev/sda2 b 8 1
chmod 640 /dev/sda{1,2}; chown root:disk /dev/sda{1,2}
reboot

Just remember that this swapping will happen only after your partition containing /dev/ is loaded upon boot, so the initrd will still have the "correct" ones unless you fix it there too (exercise for the reader - will mkinitrd copy the strangeness?) This also doesn't address SELinux contexts.

  • The bootloader isn't a problem :) . Thanks for the solution but I need change the order in the partition table, not in the system. – Rufo El Magufo Aug 15 '11 at 09:50
  • 1
    That's not going to work on any system that uses udev, and it would only help if the application read from a file called /dev/sda (as opposed to reading the disk directly). – Gilles 'SO- stop being evil' Aug 15 '11 at 18:30
  • Both - agreed. The whole question IMHO is an exercise in pain. I wasn't sure about the udev stuff; apparently the first system I sshd into didn't have it. – Aaron D. Marasco Aug 16 '11 at 01:36
1

- Editing partitions /dev/sda3 to /dev/sda2 in terminal as root -

  1. Backup partition table

    sfdisk -d /dev/sda > sda.bkp

  2. Copy partition table backup to the new partition table

    cp sda.bkp sda.new

  3. Edit new partition table

    gedit sda.new

enter image description here

Save, close

  1. Reinstall the new partition table

    sfdisk --no-reread -f /dev/sda < sda.new

  2. Reboot computer