Cloning a hard disc

From NewbieDOC


Chris Lale
chrislale AT users DOT berlios DOT de

Go to NewbieDOC index

Revision History


Version 1.0 6th July 2005 Revised by Chris Lale
Initial release

Version 1.1 7th February 2006 Revised by Chris Lale
Modified for NewbieDOC wiki by converting source to wikitext. Added a note about ddrescue.

Version 1.2 15th November 2007 Revised by Chris Lale
Moved the section about compiling Pcopy to an appendix now that there is a Debian "pcopy" package in Etch.

Version 1.3 26th November 2007 Revised by Chris Lale
Reformatted for newbiedoc package; Moved section on documentation of self-compiled version of Pcopy to Appendix B.


Image:Newbiedoc-32.png Stable document
You can comment on this document by clicking on the discussion tab.

You can edit the live version of this document: Cloning a hard disc


Abstract

This HOWTO article explains how to make a clone of a hard disc. If the original hard disc is a failing disc, it must still be working and readable. The new disc must be at least as the same capacity as the original disc. These instructions have been tested on a Debian 3.1 (Sarge) system with IDE (ATA) hard discs.

Contents


1 Introduction

If you are unlucky, your hard disc begins to fail after a few years. If you are lucky, it gives you warning that it is on the way out. The drive produces clunking noises when it spins up, or fsck reports damaged sectors. This is the time to backup important files and buy a new disc. If the failing hard disc is your primary disc, you can put the new disc in the same machine alongside the failing one and make it an exact clone of the original. After cloning, you can remove the failing disc and replace it with the cloned one. If all has gone well, your master boot record (MBR), Linux partitions and other partitions all function exactly as before.

1.1 Assumed hardware setup

For IDE (ATA) discs, this article assumes that the original IDE hard disc is the primary disc with the jumper set to master (/dev/hda). The new IDE hard disc is the secondary disk with its jumper set to cable select (CS) or slave (/dev/hdb).

For SCSI discs and SATA discs the device names would be /dev/sda and /dev/sdb.

2 Installing the Debian packages

Install these packages.

  • util-linux (provides sfdisk)
  • gcc (needed to complile pcopy)
  • make (needed to complile pcopy)

3 Installing Pcopy

pcopy is a tool for large disk to disk copying. It is intended to be used when doing large disk (partition) to disk (partition) copying where dd is just too slow (and error prone). It also displays a progress counter while doing the copying. It is available as a Debian package in Etch. If you have an earlier version of Debian, you can compile and install it by following the instructions in Appendix A.

 
Note
Note: 
It may be possible to use ddrescue instead of pcopy, although this has not been tested. ddrescue is available as a Debian package.
 

As user root:

# aptitude install pcopy

4 Preparing to clone the disc

Before you go any further you would be wise to take these precautions.

  • Backup important files..
  • Backup the master boot record (MBR). Eg to backup to a floppy disc:
# dd if=/dev/hda of=/media/floppy/mbr.backup bs=512 count=1
  • Clean up existing partitions on the old disc.
    • Run Scandisk on MS Windows systems.
    • Run fsck on unmounted Linux partitions using the -f switch to force a check:
# fsck -Cvf /dev/hda1
  • Print a list of existing partitioning information on the old disc. You may need this data to recover data if things go wrong.
# fdisk -l /dev/hda | lpr
  • Stop the network.
# /etc/init.d/networking stop
  • Change to single user mode before starting to copy the disk. This will shut down the system daemons and preserve the state of the logs. It will also prevent users from logging in on networked systems.
# /sbin/telinit 1

5 Copying the partition table

The new disc must be large enough to accept all the partitions. If the new disc is larger than the old one you will be all right.

Use sfdisk to dump the partitions of the old disk (/dev/hda) and pipe the output to another instance of sfdisk to recreate the partitions on the new disk. This creates the new partitions but not the data on them. Remember to switch user to root before you use sfdisk. This example shows the screen output for a disk with FAT, NTFS and Linux partitions. The bootable partition /dev/hda1 contained a proprietory boot manager. Even non-Linux partitions and data can be copied successfully

$ su
Password:

# sfdisk -d /dev/hda | sfdisk /dev/hdb
Checking that no-one is using this disk right now ...
OK

Disk /dev/hdb: 19457 cylinders, 255 heads, 63 sectors/track
Old situation:
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

Device Boot Start End #cyls #blocks Id System
/dev/hdb1 0 - 0 0 0 Empty
/dev/hdb2 0 - 0 0 0 Empty
/dev/hdb3 0 - 0 0 0 Empty
/dev/hdb4 0 - 0 0 0 Empty
New situation:
Units = sectors of 512 bytes, counting from 0

Device Boot Start End #sectors Id System
/dev/hdb1 * 63 4176899 4176837 6 FAT16
/dev/hdb2 4369680 116262404 111892725 f W95 Ext'd (LBA)
/dev/hdb3 4176900 4369679 192780 16 Hidden FAT16
/dev/hdb4 0 - 0 0 Empty
/dev/hdb5 4369743 8594774 4225032 7 HPFS/NTFS
/dev/hdb6 8594838 9655064 1060227 82 Linux swap
/dev/hdb7 9655128 10281599 626472 83 Linux
/dev/hdb8 10281663 18683594 8401932 83 Linux
/dev/hdb9 18683658 28933064 10249407 83 Linux
/dev/hdb10 28933128 35085959 6152832 83 Linux
/dev/hdb11 35086023 36114119 1028097 83 Linux
/dev/hdb12 36114183 37961594 1847412 6 FAT16
/dev/hdb13 37961658 82734749 44773092 83 Linux
/dev/hdb14 82734813 86638544 3903732 83 Linux
/dev/hdb15 86638608 87618509 979902 82 Linux swap
/dev/hdb16 87618573 91843604 4225032 7 HPFS/NTFS
/dev/hdb17 91843668 99747584 7903917 83 Linux
/dev/hdb18 99747648 103972679 4225032 7 HPFS/NTFS
/dev/hdb19 103972743 110495069 6522327 b W95 FAT32
/dev/hdb20 110495133 112744169 2249037 83 Linux
/dev/hdb21 112744233 116262404 3518172 83 Linux
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7 say, then use dd(1) to zero the first 512 bytes:

# dd if=/dev/zero of=/dev/foo7 bs=512 count=1

(See fdisk(8).)

6 Copying the partitions

Copy the contents of each of the partitions from the old disc to the new disc using pcopy. The -d switch shows progress as a series of dots. This example shows the screen output for the copying of partition /dev/hda3.

# pcopy -d /dev/hda3 /dev/hdb3
Copying from /dev/hda3 to /dev/hdb3 (start in 3 seconds)...
...............................................................................................
/dev/hda3: Done. Copied 94 MB in 5 seconds (18 MB/s)
#

Repeat the copy process for each partition.

7 Testing the new disc

Shut down the system.

# shutdown now -P

Unplug the computer from mains power. Remove the old disk. Remove the new disk and reposition the jumper to set it to master. Replace it using the cable connections of the old disc. Reconnect mains power, switch on and watch the computer boot. Everything should work identically to the original system.

8 Appendix A: Installing Pcopy from source

This is how you can compile Pcopy if your distribution does not have a debian "pcopy" package.

8.1 Downloading Pcopy

Download the Pcopy source from ftp://ftp.lysator.liu.se/pub/unix/pcopy. If this link is broken, search for pcopy with a search engine. Save the source tarball in your home directory eg /home/chris/download/pcopy/.

8.2 Extracting the source files

Open a terminal or a terminal window as a normal user. Move to the directory where you saved the tarball. For example, if you downloaded the Pcopy archive to /home/chris/download/pcopy/:

$ cd /home/chris/download/pcopy/

Extract the source files.

$ tar -xzvf pcopy-1.5.tar.gz

This creates a new subdirectory, pcopy-1.5, containing the source files.

8.3 Configuring the source code for your system

Move to the source directory and run the configure script.

$ cd pcopy-1.5
$ ./configure
loading cache ./config.cache
checking for gcc... (cached) gcc
checking whether the C compiler (gcc ) works... yes
checking whether the C compiler (gcc ) is a cross-compiler... no
checking whether we are using GNU C... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for a BSD compatible install... (cached) /usr/bin/install -c
checking whether make sets ${MAKE}... (cached) yes
checking size of long... (cached) 4
checking for lseek64... (cached) yes
checking for llseek... (cached) yes
checking for thr_create in -lthread... (cached) no
checking for pthread_attr_init in -lpthread... (cached) yes
creating ./config.status
creating Makefile
creating config.h
$

8.4 Compiling the source files

Run Make to select and compile the appropriate files.

$make
gcc -g -O2 -c -o pcopy.o pcopy.c
gcc -o pcopy pcopy.o -lpthread
$

8.5 Installing the binary file

You must become user root to do install pcopy, because the binary is installed in /usr/local/bin. Still in the source directory, switch user to root using su. Run make install. When the installation is complete, return to being a normal user by entering exit or typing ctrl-D.

$ su
Password:

# make install
/usr/bin/install -c -m 755 pcopy /usr/local/bin/
athlon:/home/chris/download/pcopy/pcopy-1.5

# exit
$

8.6 Documentation

8.6.1 Pcopy details

http://directory.fsf.org/sysadmin/Backup/pcopy.html

8.6.2 Manual pages

Man pages are available after you have installed the Debian packages.

  • sfdisk (8)
  • cfdisk (8)
  • tar (1)

pcopy does not have a man page, but you can get a list of options by typing:

$ pcopy -h


9 Appendix B: Licence

Copyright (c) 2005-2007 Chris Lale. chrislale AT users DOT berlios DOT de.

GNU FDL Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License."

Go to NewbieDOC index

Content is available under GNU Free Documentation License 1.2, unless otherwise stated.