ADS

Featured

How to clone a hard drive in linux

In this tutorial, there are some basic commands that can be executed in most of the most popular Linux distributions, since the command used is very simple and available in most versions from the most minimalist, the most popular to the most robust.

Before you begin, make sure your computer has the available ports and power connectors available for connecting the disks to be cloned.

After connecting, it is recommended that the main disk is in fact on the first port of the motherboard, so that in Linux it is identified as / dev / sda, and the second as / dev / sdb.

When running a Live CD, usually the RAM memory created by the temporary system is created after the physical disks, that is, if you have two disks, the virtual area is now called / dev / sdc, but this can vary depending on the assembly of each distribution, remembering that you can mount a volume of a device wherever you want.

In this tutorial, we will not mount a volume, we will work with it in RAW mode, that is, whatever is there on the hard disk, it will be completely copied to the other, including the binaries and sectors that exist on the disk but in the partition table no longer exists, that is, it even recovers the deleted files (using a specific hard disk scanning tool to recover the removed files that no longer exist in the file allocation table).

When starting a clone of a disk, make sure that the destination disk has the same or greater storage capacity than the first, or else data may be corrupted.

Do not interrupt the copy job, it does not seem to display anything and apparently there is no work on the hard drives but yes, it is running. It is a time consuming task, even with proper adjustments, a 2 TB disk takes about 6 to 8 hours to be fully cloned.

There are other ways to create hard disk images based only on reading the file allocation table, but in this tutorial I will explain the simplest format for making a copy of a hard disk including all data, whatever it may be, no matter whether it is FAT, NTFS, ZFS, RaiserFS, EXT4, etc. the copy mode is simply to copy sector by sector from one disk to the other in full sequential reading and writing (and for this reason the cloned hard disks do not show noise, after all they are not going to the beginning of the disk to read the file allocation table).

As I already made clear at the beginning, we are working in RAW mode, where partition, bootloader, among other information are just bytes.

After identifying the hard drives, there are the following ways to copy:
- Partition for partition.
- Complete disk to another disk (including MBR and partitions).

Partition to partition:
If both disks are already partitioned, make sure that the destination partition has the same or higher capacity for data storage.

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

if = Input File. Source. It can be from a file. Eg: /home/disk.img
of = Output File. Destiny. It can be for a file. Eg: /home/disk.img
bs = Buffer Size. Read size that enters the disk buffer for traffic. That is, you will copy the hard disk from blocks to blocks, never exactly byte by byte as the process would be very slow because for each byte the hard disk has an overhead of reading 4096 physical bytes from a given sector to convert to the operating system in 512 logical bytes per sector. So it is suggested that to take advantage of the use of the disk buffer, start by doing small tests to test the write read speed.

dd if=/dev/sda1 of=/dev/sdb1 bs=65535 count=10000

It will copy only the first 10,000 blocks of 64 KB of data, and will display the throughput of this test.

Change the blocks and see which situation has the best speed gain. In a test using a 2TB and 3TB Seagate hard drive, I got 93.3 MB / s with a buffer size (bs) of 32768.

To copy by cloning all information from one disk to the other, including partitions:

dd if=/dev/sda of=/dev/sdb bs=65535

It is noteworthy that the process takes hours to complete, but it also copies information that physically exists on the disk in which they were not overwritten and with specific software it will be possible to recover them.

And now, did you finish the process successfully? Congratulations, you have just cloned a disk completely. Did an error occur? Sometimes problems can occur during copying from a source or destination hard drive with damaged sector drives.
dd if=/dev/sda of=/dev/sdb bs=65535 conv=notrunc,noerror,sync

In this way, the above flags make the dd command even if it finds errors, keep copying the rest of the data available on the disk, corrupting only a certain file that faced the problem during access, and mark it to do its best not to corrupt the files , in addition to keeping the copy in sync so that the bits with problems also skip the destination.

With the DD command, it is also possible to make a backup of the MBR of the disk, as well as record another version of MBR as you wish. However, make sure your hard drive works with GUID or not (although the GUID scheme physically has the MBR area, but the programming code is not the same as a system with a common MBR).

To understand it a little better, MBR is the traditional scheme of a computer's traditional BIOS, which sees up to 4 data partitions. In the new model, the MBR area has a hook for the first partition that has 100 Mb of programmable areas, and for the BIOS, the rest of the disk does not have any other partitions, but for the computer startup and the programming code in the When the first partition starts to run, the GUID partitions come into existence, as they start to be recognized only by this software that manages it.

dd if=/dev/sda of=/home/mbr.img bs=512 count=1

Okay, with the following command, you made a backup of the MBR (with the disk partitions, be careful when restoring if the partitions are in other areas of the disk as this can cause data to be lost!)

It is also possible to backup only the programmable area of the MBR, keeping the partitions as they are on the disk.

First of all, know that the MBR is divided into 3 parts:
- 446 initial bytes of the bootloader (programmable boot area of your machine)
- 64 bytes for information on partitions and their locations.
- 2 bytes for identification (in hex, usually 55 AA bytes to indicate the end of the MBR area).

dd if=/dev/sda of=/home/mbr.img bs=446 count=1

To restore, just replace the "if" with the "of".

dd if=/home/mbr.img of=/dev/sda bs=446 count=1

Note: If you use an IDE hard drive, it can be recognized in some Linux distributions as / dev / hda or / dev / hdb instead.

Reference source:
https://wiki.archlinux.org/index.php/Disk_Cloning
http://www.cse.scu.edu/~tschwarz/COEN252_09/Lectures/MBR-Example.html

No comments