Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 specify a manual order, not a automatic ordering like in fdisk -> x -> f.

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

Thanks.

The reason: I have a application that needs read data from sda1 but the data is in sda2. Change the partition table is the fast solution for this issue. The system isn't critical but I don't want halt the system for too much time.

Update: the fdisk version of OpenBSD includes that functionality.

share|improve this question
    
As it is generally a bad idea to do what you ask for, please describe why you want that. –  rozcietrzewiacz Aug 14 '11 at 23:38
    
I have backups, don't worry :) –  J.F. Aug 14 '11 at 23:49
    
Ok, you have been warned and want to play, so here goes... :) –  rozcietrzewiacz Aug 14 '11 at 23:57

6 Answers 6

up vote 11 down vote accepted

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 partitioin. 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 ok, 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 ok 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.

share|improve this answer
    
Thanks for the answer. You are right, this is scary :) . –  J.F. Aug 15 '11 at 2: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 5:44
1  
@supervacuo Testisk might help you then. Search for testdisk package in your distro's software database. –  rozcietrzewiacz Aug 20 '12 at 6:29

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
share|improve this answer

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.

share|improve this answer
    
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. –  J.F. Aug 15 '11 at 0:06

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.

share|improve this answer
    
The bootloader isn't a problem :) . Thanks for the solution but I need change the order in the partition table, not in the system. –  J.F. Aug 15 '11 at 9: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 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 1:36

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.

share|improve this answer

- 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

share|improve this answer
    
Although this is doable, I'd recommend the GParted LiveCD –  eyoung100 Sep 10 at 23:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.