Move the root partition to another disk

In today's tutorial, I am going to explain how you can move the Root partition of our Linux distribution to another partition (whether on the same hard drive or not). This need came to me in the middle of last year, when I was still using Chakra, and since then it is a process that I have used on more than one occasion with satisfactory results and zero problems.

If the steps are followed to the letter, it is a 100% safe, relatively fast and totally reversible operation. We will only need a Live CD of any distro that we have out there (one of Ubuntu, for example, will serve our purpose), and correctly identify which is the source and destination partition.

For such information, we can turn to GParted or the KDE Partition Editor. When we execute them, we will see a window similar to the one in the screenshot below. There, we must look for our original root partition and look at what disk it belongs to (sda, sdb, sdc ...), what number it has (sda2, sdb1, sdj5, etc.) and what is its UUID (an alphanumeric code that you will find in the section of "advanced information"). Obviously, if we are going to move a partition we will need a destination, so we have to previously create a hole in the hard disk to carry out the move, and write down the corresponding data.

Before continuing, I want to state that in this tutorial I only refer to Grub2; if you use another bootloader some steps or commands may vary - in fact, it is much easier with Grub Legacy-. So, with the information from before written down on a piece of paper, we get to work:

1) We start the computer with the Live CD and we wait for the desktop to load.

2) In a terminal we put the following two commands:

sudo mkdir / mnt / old

sudo mkdir / mnt / new

3) Followed, we type the following commands:

sudo mount / dev / sdaX / mnt / old (where sdaX is the original root partition).

sudo mount / dev / sdbX / mnt / new (where sdbX is the new root partition).

4) After you have mounted each partition, we proceed to copy the files using two commands (one for normal files and one for hidden data). Maybe the second is not strictly necessary, but I run it in case the flies. This part will take a few minutes:

sudo cp -rav / mnt / old / * / mnt / new
sudo cp -rav /mnt/old/.* / mnt / new

5) We unmount the old partition and type a couple more commands:

sudo umount / mnt / old
sudo mount -o bind / dev / mnt / new / dev
sudo mount -t proc none / mnt / new / proc

6) now we chroot the new partition in order to reinstall Grub2. The installation command changes depending on the LiveCD you have, since each distro has its own ways of managing packages. Chakra and Arch use sudo pacman -S grub, but Debian derivatives do like this:

sudo chroot / mnt / new / bin / bash

sudo grub-install / dev / sdb (where sdb is the hard drive where we have the new root partition, and we don't have to put a number on it or anything like that).
7) Now, Before restarting, we have to adjust a few small details of the fstab and grub.cfg. To do this, we edit the grub.cfg with our preferred text editor (kate, gedit, nano ...):
sudo kate /boot/grub/grub.cfg

As you can see in the image, I have highlighted the most important parts that we have to look at, but there may be more (look for them and modify them following the same methodology). With the data from our brand new root partition (UUID and company), we proceed to replace the old references with the new ones:
  • Where you put (hdX, Y), we change the figures of X and Y according to the following:

X: indicates the hard disk number. If the disk is sda, X is equal to 0. If the disk is sdb, X is equal to 1. If the disk is sdc, X is equal to 2, and so on.
Y: indicates the partition number. 1,2,3… Example: second partition of the first disk (hd0,2); second partition of the third disk (hd2,2)… Do you get the idea?

  • The second field to modify is the UUID (the code of numbers and letters so long), which still points to the old partition. We change it to the UUID of the new partition (remember that you can check this in GParted, for example). Check the data well!
  • The third modification, and one of the most important, is related to the small red rectangle that is under the UUID, and that it says in the image "sdb2". This is where you have to indicate the new partition of your root which, logically, has to correspond to (hdX, Y). Examples: (hd0,1) -> sda1 // (hd2,3) -> sdc3

Keep in mind that these changes, in principle, must be repeated depending on the number of entries of our operating system present in Grub. I have three Chakra entries, therefore I have to change that data 3 times. However, I advise you to change only the first entry and, once you see that everything starts correctly, proceed to modify the rest, from your real operating system.

8) Solved the issue of Grub, we went to fstab.
sudo kate / etc / fstab
We look for the UUID of / and we change it for the new one, as we did in the previous step. We save.

9) We can now restart and check that everything is in order. If the operating system works well, we can proceed to replace the data that we left unchanged in the remaining entries of the grub.cfg file, as well as delete the old root partition -if that is our desire-.

That's all for today.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Ellery said

    This was looking for XD. thanks

  2.   Saints said

    It is a very safe procedure, I have used it many times years ago, and the good thing is that the new / partition will not contain file fragmentation ...

    Although not long ago I tried to change the root partition with the variant of also changing the file system (from reiserfs to ext3), but it was impossible for me to do it due to more attempts and turns than I gave, somehow at system startup the detection failed de / due to the fact that it was looking for a reiserfs system / partition when the new one was formatted with ext3. Entering maintenance mode and mounting manually / as ext3 the system worked correctly, but the next start it failed again for the same reason. There was no editing of grub or fstab that worked ... always looking for a / partition with reiserfs, can't find the solution ...

    1.    Wolf said

      That looks like there was some file pointing to the reiserfs partition. Perhaps an inconspicuous line from the bootloader or something like that, otherwise ext3 should have loaded correctly.

      1.    Saints said

        If that's what I thought ... and spent hours looking for something but found nothing ... not even searching the Internet.
        Anyway, I cannot assure you that in the past I have done the procedure without problems, it is that 6 or 7 years have passed since I used to do it almost for pleasure ... the last time I tried to do it with Debian Lenny, probably the biggest difference is the kernel, before maybe you used a distro with linux 2.4.x.
        Anyway, if you go to find the solution, I hope you share it ...

  3.   ren434 said

    Good tip, ... if I had known before.

    Greetings.

  4.   Merlin The Debianite said

    Yes, very good tip, it looks a lot like the procedure to change the disk / home, but I didn't know what it could do with root.

    Very good info although I may not use it, it is good to know the flies. 🙂

    1.    Wolf said

      Yes, with / home it is much easier, because you do not have to reinstall Grub or modify its configuration file. Copying everything you need and editing the fstab is enough.

      1.    Merlin The Debianite said

        Well of course we are talking about / home, it is obvious that / root requires more attention.

        It's not like / Home, which is generally almost just cut and paste or, failing that, copy and paste.

  5.   Keopety said

    very good manual, friend, thank you very much, I would like to know if the pdf version or any other can be downloaded from somewhere, greetings

    1.    Wolf said
      1.    Keopety said

        thanks friend, this very well

  6.   Rayonant said

    Thanks a lot! I was looking for something similar and what had occurred to me was to make images of the partitions and then restore them but of course, more things were missing such as mount points etc. So it suits me like a glove!

  7.   TheSandman86 said

    Very good info, it is always useful to have these things on hand just in case. Thanks a lot.

  8.   Krim said

    If you are using Grub2, wouldn't it be grub2-install?

    Be careful when you make these manuals that you put anyone in a mess as long as you don't put the commands right.

    1.    blacksheepx said

      In Arch the old version of grub was renamed to grub-legacy and grub 2 was left as grub only so it is correct but in the same way it is advisable to read the documentation of your distribution before making an important move like this to be sure of the names of packages

      and thanks to the author I was looking for the detailed process and this served me well

  9.   Bill said

    The couple of commands more than point 5 have not worked for me, better this:
    sudo su
    mkdir / media / kk (where the root of the installed system is mounted)
    mount -t ext4 -o rw / dev / sda / media / kk
    mount –bind / proc / media / kk / proc
    mount –bind / dev / media / kk / dev
    mount –bind / sys / media / kk / sys
    chroot / media / kk
    update-grub
    grub-install / dev / sda (or sdb,…)

  10.   Alengoan said

    Thank you very much it helped me a lot, as an alternative after copying everything to the new partition you can move the grub installation with the boot-repair tool, thus avoiding having to do steps 5 onwards

    sudo add-apt-repository ppa: yannubuntu / boot-repair
    sudo apt-get update
    sudo apt-get install boot-repair

    the graphifa application is executed and advanced options are activated; grub location and the new partition is chosen for the grub installation.