Hard drive cloning Linux

In this article I show to clone a hard drive in Linux. You will be able to create a mirror of your hard drive, allowing in example to boot operating system from the cloned drive. All you need are two hard drives (or SSD drives) and Linux/Unix operating system. No extra hardware required.

Cloning hard drive theory

Cloning a hard drive is a very simple operation. All you need to do, is to copy every single byte from the beginning to the end of one hard drive to another. By doing this, you will not just copy files that are stored on the hard drive, but also partition information, boot sector and everything else your hard drive contains.

By having a hard drive cloned in such way, you will be able to boot it and use it as your original hard drive. I am currently writing this article using a computer with SSD that I cloned from my old HDD a week ago.

Preparation

Before we start, all you need is a Linux operating system (or any other system with dd tool from GNU Core Utils available), a hard drive you want to clone (or SSD), and a hard drive (or SSD) you want to clone to. The hard drive you want to clone to must be at least the same size as your original hard drive. Both hard drives must be connected to your computer and available in the system.

You need to know paths to your hard drives in your system (ie. /dev/sda). There are many ways to find out the paths:

lsscsi

If you know manufacturers of your hard drives, you can use lsscsi command to find the paths. Here is example output:

[ascheller@office ~]$ lsscsi
[0:0:0:0] cd/dvd TSSTcorp DVD-ROM SH-118AB SB00 /dev/sr0
[1:0:0:0] disk ATA SanDisk SDSSDHP0 200 /dev/sda
[5:0:0:0] disk ATA Samsung SSD 850 2B6Q /dev/sdb

This is probably the fastest method to get paths to your hard drives. We can easily see that my SanDisk SSD has path /dev/sda, while my Samsung SSD has path /dev/sdb. However, if you have two the same drives connected to your computer, finding out what is what would be a little more difficult with this command.

df

df is a utility that shows disk usage space and a few other useful information, like mount points and paths to devices. If you want to clone your live hard drive to a new one, this command is the most helpful. Example output of df:

[ascheller@office ~]$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 30627688 9468444 19580392 33% /
devtmpfs 16438520 0 16438520 0% /dev
tmpfs 16453028 217932 16235096 2% /dev/shm
tmpfs 16453028 9148 16443880 1% /run
tmpfs 16453028 0 16453028 0% /sys/fs/cgroup
/dev/sda1 249588 9640 239948 4% /boot/efi
/dev/sdb1 240230012 16908756 211095108 8% /home
tmpfs 3290608 64 3290544 1% /run/user/1000

Here we can see three partitions that are mounted in my system. With output like this, we can easily identify that /dev/sda is the hard drive on which my live system runs, because it contains the partition on which the root filesystem is mounted.

Now you can use lsscsi to identify the other hard drive or use dmesg described under the next header.

dmesg

If you are using hot-pluggable hard drive (ie. over USB or special controller) you can consult with dmesg to find what device file given hard drive receives.

Disconnect your hard drive from your computer, and then plug it in again. Execute dmesg command, and on the very end you will see your device. Here is an example output after plugging in a USB storage:

[ascheller@office ~]$ dmesg
(…)
[25997.777031] usb 2-3: new SuperSpeed USB device number 2 using xhci_hcd
[25997.788866] usb 2-3: New USB device found, idVendor=0781, idProduct=5583
[25997.788874] usb 2-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[25997.788879] usb 2-3: Product: Ultra Fit
[25997.788883] usb 2-3: Manufacturer: SanDisk
[25997.788886] usb 2-3: SerialNumber: 4C530123310922115462
[25997.865478] usb-storage 2-3:1.0: USB Mass Storage device detected
[25997.866883] scsi host6: usb-storage 2-3:1.0
[25997.867078] usbcore: registered new interface driver usb-storage
[25998.870018] scsi 6:0:0:0: Direct-Access SanDisk Ultra Fit 1.00 PQ: 0 ANSI: 6
[25998.870828] sd 6:0:0:0: [sdc] 31266816 512-byte logical blocks: (16.0 GB/14.9 GiB)
[25998.871690] sd 6:0:0:0: [sdc] Write Protect is off
[25998.871697] sd 6:0:0:0: [sdc] Mode Sense: 43 00 00 00
[25998.871876] sd 6:0:0:0: Attached scsi generic sg3 type 0
[25998.872178] sd 6:0:0:0: [sdc] Write cache: disabled, read cache: enabled, doesn’t support DPO or FUA
[25998.894395] sdc: sdc1
[25998.896291] sd 6:0:0:0: [sdc] Attached SCSI removable disk

From this dmesg output we can easily notice that my SanDisk USB storage received device file /dev/sdc.

Cloning hard drive in action

When you have the dd tool available and you know your hard drive device files, you can proceed to cloning hard drives. If your hard drives have mounted partitions it would be good to unmount them before proceeding. If you cannot unmount a partition (ie. because it is your root partition), try to minimize any writes to it as possible. Close all unnecessary applications and services. You don’t want to have a file overwritten in the middle of copying it to the other hard drive.

The magic command to perform hard drive cloning is following:

dd if=/dev/sdA of=/dev/sdB bs=128K conv=noerror,sync

dd parameters explained:

if=/dev/sdA defines source (input) hard drive (the one you want to clone)
of=/dev/sdB defines target (output) hard drive (the one you want to copy files to)
bs=128K defines block size which is used during copying data, 128K should provide a good efficiency of data copying
conv=noerror,sync tells dd to ignore all errors, and place zeros in places where read errors occur

Executing this command will not show any output. It will bring you back to the shell after it is done.

Tracking process of hard drive cloning

If you want to see the progress of hard drive cloning operation, follow the steps below.

First, open a new terminal and find PID of your dd process with following command:

[ascheller@office ~]$ ps aux | grep “dd if”
aschell+ 4103 0.0 0.0 107940 620 pts/0 S+ 21:37 0:00 dd if=/dev/sdA of=/dev/sdB bs=128K conv=noerror,sync
aschell+ 4161 0.0 0.0 112648 960 pts/1 S+ 21:38 0:00 grep –color=auto dd if

In the output above we can see that in my case the PID of running dd is 4103.

Second, then the USR1 signal to your dd process:

[ascheller@office ~]$ kill -USR1 4103
[ascheller@office ~]$

Remember to replace 4103 with your PID! The statistics output will be not available in the terminal where dd is running.

Third, if you want to have the statistics automatically displayed in regular time periods, use the watch command:

[ascheller@office ~]$ watch -n 120 kill -USR1 4103

This command will make the dd process to print its statistics every 120 seconds.

The output should be similar to this (I take this photo when I was cloning my old HDD to new SSD last week):

Clone hard drive Linux

Hard drive cloning questions?

I you have any questions regarding hard drive cloning in Linux, let me know in the comments below.