Installing GNU / Linux on another HDD without installation disk

Today we will be looking at how to migrate all the data from one hard drive to another, work that could be useful if for some reason we need to replace the current hard drive of our computer with another (of the same or different capacity).

A few days ago, on a casual visit to a friend's house (who coincidentally migrated to GNU / Linux a few months ago after my persistence), he very gladly showed me a new Hard Drive that he had been given (a 500 Gb HDD that Although to many it may seem like a small capacity device, here in Jurassic Cuba it is still a great step towards large data storage) and that needed to replace the already obsolete and half failed 160 GB disk.

His proposal itself was to remove the old disk and put the 500 GB disk in its place, then install Debian, which was the distro that I had installed on the old disk, and take a while to restore all its settings.

While this was happening, a cousin of my friend was waiting for the culmination of this work to take the 80 GB disk, which in turn would replace a 40 GB disk in a much older computer.

The detail of all this is that I did not even walk with an installation disk of Debian, and less with the repositories at hand. So the idea of ​​installing the system and applications was not within our reach, unless I went to my house to find what was necessary, but moving from one end of the city of Santiago de Cuba to another was not going to be an alternative for me. , in a city where the official transport (private motorcycles) has doubled its rate in this new year (the grace was going to cost me no less than 40 pesos).

There the spark was lit: to clone the album. I know that this cloning the disk would have been the first thing that would have occurred to many of you, but to me, quasi-alphabet In these tasks, it seemed like a sudden alternative sent directly by the Gods of the Open Olympus.

After counting the necessary introduction, we go to the technical aspects of the case in question. Initially we have an 80 GB Hard Drive with the following partitioning system:

/ dev / sda1 / / dev / sda5 swap / dev / sda6 / home

And the new 500 GB Hard Drive that we will partition as follows:

/ dev / sdb1 / / dev / sdb2 swap / dev / sdb3 / home

Many have surely noticed that in the case of the first Hard Drive there is a jump from sda1 to sda5, this is because when defining the partitions, there is a bootable primary partition that is sda1, and then an extended partition that is divide in two: sda5 ysda6.

Also, at some point I think I had to delete a partition where my friend used to house the banished Windows 7.

The 500 Gb hard drive is partitioned so that the partition numbers are found consecutively. This Disc is named / Dev / sdb because by installing it in the SATA port, you are simultaneously sharing the system with the pre-existing disk, / Dev / sda.

Formatting the partitions

To create the partitions we can use some visual tool like gparted, or some application from console like cfdisk. Remember that in this step, we are from the operating system on the first Hard Drive, the 80 GB one.

That is, from this, we create the three partitions of the new Hard Drive. Once the partitions are created, they must be formatted:

mkfs.ext4 / dev / sdb1 mkfs.ext4 / dev / sdb3 mkswap / dev / sdb2

What we just did is format / dev / sdb1 and / dev / sdb2 as ext4 and / dev / sdb2 as SWAP.

The order mkfs.ext4 is similar to mkfs -t ext4. We must take into account that when restarting on the new hard drive, we must tell the system that it has a new swap partition (swap partition):

swapon / dev / sda2

We use sda2 and not sdb2, because when finished, we will remove the old disk.

Cloning partitions

We are already falling into it chicken rice chicken. There is no definitive recipe for cloning a partition. In our case, we have to make an exact copy of the content of / home, for this we will do the following (as root):

cd / media mkdir sdb3 mmount -t ext4 / dev / sdb3 / media / sdb3 rsync -a / home / myfriend media / sdb3

Relax, I explain:

Inside / media we have created a directory with the name sdb3 (which must coincide with the name of the partition of the new disk), so that when mounting it does not create confusion.

Then we are going to use the rsync command to synchronize the files and folders from / home into / media / sdb3, since the sdb3 partition will be / home.

The flag -a It will help us to maintain the permissions, the owner, the date and the directories.

We have intelligently used / home / myfriend and not / home / myfriend /, because if I had put the slash at the end of my friend, I would exclusively copy the files and folders inside / home / my friend. What we did is equivalent to putting:

rsync -a / home / / media / sdb3

As in the case of my friend, he only has one user in home, it does not matter to put one or another command line.

It was time to clone the root / partition, which, of course. It is a critical and delicate moment, since some failure can leave the partition without booting, and therefore a hard disk without a system.

A tip before the next step is to do some cleaning in our root partition in order to save space and reduce the traffic of unnecessary data from one hard drive to another.

It is good to previously empty the recycle bin (even before cloning home), delete the packages that are not being used and specific packages that we do not need:

dpkg -l | grep ^ rc dpkg --purge package

We can also make sure to remove the packages from the local repository: the ones that we have downloaded in each update or installation from the repositories, and have been cached:

apt-get clean

By doing the steps above you will surely find that they can free up a few GB of space. Well, let's clone our root /.

In the case of the root partition, it is advisable to copy the data bit by bit. It will take less time than home because it is obviously less accumulation of information and doing it bit by bit the tendency to failures is almost nil and there will be no errors with special permissions.
The command line in this case is:

dd if = / dev / sda1 of = / dev / sdb1

In this case we have not had to mount anything. The time to complete the operation will be a bit longer, but it will be worth it.

Preparing the restart

At this point, already the partition / dev / sdb1 it is a clone of the root system installed on our old disk. The point has come to make sure that when we remove the 80 GB disk, our computer recognizes and boots from the 500 GB disk.

It's time to touch the well-known fstab file (the one on the 500 disk found in / media / sdb1 / etc / fstab).

nano / media / sdb1 / etc / fstab

And we will get something similar to this:

# / etc / fstab: static file system information. # # proc / proc proc nodev, noexec, nosuid 0 0 #Entry for / dev / sda1: UUID = 6b192eef-e188-4e07-94de-14c95e02de78 / ext4 errors = remount-ro 0 $ #Entry for / dev / sda2: UUID = 3bd60ec0 -92f3-4ea6-a4d3-aaaf27dd8b8e none swap sw 0 0 #Entry for / dev / sda3: UUID = 3828f973-3b20-4019-9fe2-8296c755be31 / home ext4 defaults 0 2

Now we need to change the old UUIDs (the ones in that fstab file are from the 80GB disk) to the new UUIDs (from the new 500GB disk). To do this, what you have to do is find the devices by their UUID, and this is done in several ways:

One option can be with the command:

ls -l / dev / disk / by-uuid

And also using as root:

BLKI

Now we only have to replace the UUID of the fstab with the correct ones.

Guaranteeing the start. Installing Grub2

El debian wheezy installed on my friend's old Hard Drive uses Grub2 as system boot, so in the MBR (Master Boot Record) of the new Hard Drive (the 500 GB one) we will have to install a new GRUB.

It is true that the configuration files are already written in the / boot directory, but the MBR (the first sector (“sector zero”) of the hard disk.) Is blank, so as it is, it would not be bootable .

The configuration is saved within /boot/grub/grub.cfg, but this file is created by grub-mkconfig, so it would not be advisable to edit it by hand.

The best thing is, once the new partition is mounted (remember that the newly cloned root partition is not mounted, dd clone without mounting partition):

mount -t ext4 / dev / sdb1 / media / sdb1

Now we have to load GRUB2 in the MBR by doing the following:

grub-install / dev / sdb

And voila, that simple, we already have GRUB configured in the MBR of the 500 GB Hard Drive.

Final steps

Now we turn off the computer, remove my friend's 80 Gb Hard Drive, give it to the cousin (see initial story), turn on the computer again, cross our fingers and ...

Source: http://swlx.cubava.cu


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.   Gabriel said

    Friend, I don't know if I'm wrong but I think - I think - that before using the 'dd' command the destination partition must be exactly the same size as the source partition. If not, a thousand apologies.

    1.    Nebuchadnezzar said

      It's the same as I think
      dd will clone the partition and the surplus will leave it unallocated, so as it requires more space to install new applications, it will not allow it.

    2.    John edisson ortiz said

      It doesn't matter if the destination partition is larger than the source partition, then the filesystem can be told to use the extra size.

  2.   eliotime3000 said

    That is epic. Let's see if I can install Debian but Arch style (pure commands).

  3.   RudaMale said

    You could have used dd to clone the MBR:

    dd if = / dev / sda of = / dev / sdb bs = 512 count = 1

    I do not think it is necessary that the partitions are exactly the same, at least in the arch wiki it does not mention it, we would have to try. Regards.

    1.    Outdated said

      That looks good, but I don't understand why doing a dd if = / dev / sda of = / dev / sdb skips the first 512 bytes?

      1.    Outdated said

        Ok, I have understood, the difference is that at first only the partition is cloned and that is why the 512 bytes of the MBR are skipped. Thank you very much for the advice RudaMacho.

    2.    The Pelu said

      Hello, do you plan to update your system? I say it for windows xp

  4.   adr14n said

    Hello elav, I think that using clonezilla the task would have been much simpler, but it's good that you experimented in this way, after all it is the «hacker» spirit

    Regards!

  5.   vidagnu said

    In Slackware I have used tar to make the complete copy of files between disks, which preserves the permissions as well as I take the opportunity to make a backup, the fstab file is much simpler so it is not necessary to modify uuid, it is enough that you have created the partitions In the same order, as lilo is used it is easier to create the boot ...

  6.   peterczech said

    Very good 😀

  7.   mitcoes said

    In my experience gparted copy - clone - partitions faster

    And if you need to enlarge or cut them, you can also

  8.   Outdated said

    Magnificent elav guide, thank you very much for sharing.

    I have a question: is it necessary to use the swapon command for the system to detect the swap partition, or would it be enough to change the UUID in fstab?

  9.   moony said

    6 months ago I was bored with debian stable and kde 4.8. I wanted the latest KDE !! I had no installation disk or usb memory .. so I clone my stable with almost the same methods that elav describes and upgrade to sid.
    I recommend this practice for all Linux users out there, our system does not need more than 15 GB. by root partition. and twice or a little less is not much.

  10.   Victor challa said

    Excellent contribution would never have been so easy than with such a plain and clear explanation ... Congratulations bro, you are a teacher in gnu / linux!

  11.   YoeLoco said

    Very Good Tutorial. Very clear!
    There is also the command "dd"