Skip navigation

Skip secondary navigation


Support
 Introduction
 Forum
 How To
 Tutorials

Other Resources
 Documentation
 Tech. Q&A
 FAQ
 Redhat
How to clone a disk under Linux

How to clone a disk under Linux

There are several approaches to cloning a disk in Linux.  This document describes how to make a functionally equivalent  copy of a disk.   The disk from from which you want to make the copy, we'll call the source disk.  The new disk, will be the new disk or destination disk. 

This approach also assumes that "grub" is the boot loader.

Pre Setup -- What you will need

Basic pieces:
  1. New disk.  Will be used for the clone/copy.  All contents on this drive will be destroyed.
  2. Redhat installation CDROM (or DVD) and/or a Redhat recovery CDROM or diskette (need to create the "boot" record)
  3. A machine running linux, that has the source disk (i.e. the disk you want to clone)
Before installing the new disk, edit the /etc/fstab and comment out references to the /sos disk and change the labeled references to absolute references (machine specific details needed here).

Step 1 -- New disk install


Install a new disk into the machine used to clone.  For ide drives, the disks will show up as the following:

hda  (master drive on ide0 chain)
hdb (slave drive on ide0 chain)
hdc (master drive on ide1 chain)
hdd (slave drive on the ide chain)

Master slave is controlled generally by jumpers on the ide drive.  If set to cable select, then the OS/BIOS choses.  Regardless, it is important to know which drive is the new drive, as the contents will be destroyed.

Step 2 --- Collect source disk information 

Using "copy" method, determine the partition sizes from the disk that you want to copy from (the source disk).  You can generally do this with the df -m command.  Make note of the partition names and partition sizes on the source disk.  Use "swapon -s" to see if the source disk has a swap area.   Look at the /etc/fstab to see if the source disk has any ext2 filesystem labels.  It is fairly typical for the "root" partition to have a "/" label and the "/boot" partition to have a "/boot" label.

Step 3 - Fdisk the new disk


Use fdisk to partition the new disk.  Generally, the process is, delete any existing partition with fdisk "d" command.  Then  use "n" to create new partitions.    Nothing will be written or changed on the disk unless you change the label or write out the partition table.  That is, if something screws up, you can always quite _without_ writing to back to the previous state.  On single disk redhat systems the following partition mappings are typical (note the "X" below will be either, "a", "b", "c", or "d", depending on where the disk is in the IDE chain):

Filesystem
Device
System Id
Label
Size
/
/dev/hdX3
83
/
4G+
swap
/dev/hdX2
82
none
2 x memory
/boot
/dev/hdX1
83
/boot
100MB


To get going, type:

fdisk /dev/hdX

You will get some text about fdisk and then the following fdisk command prompt:

Command (m for help):

This is fdisk command line menu prompt.  The first thing to do is check for existing partitions.   Use the "p" command to print out the existing partition tables.   If partition tables exist, delete them with the "d" command.

Once the partitions are deleted, use the "n" command to create new partitions.  The "n" command will be used iteratively, until all three partitions are created. 

TypeAfter typing "n", fdisk will ask to create primary or extended.  Choose primary. 

Fdisk will ask for the number of the new partition (1-4).   This number fdisk is looking for, cooresponds to the numbers used in the Linux device name,  as in /dev/hdX"n", where "n" is either 1, 2, 3, or 4.   It makes sense to match them to your source drive.  So for example, using the above table, choose 1 for the /boot partition, that will map to the device /dev/hdX1.

Next, fdisk will ask for the start block/cylinder.  Always let fdisk choose the start block/cylinder, that is take the default given by hitting return.  Fdisk will ask you to input the partition size.  It is easiest to specify the size of the partition in megabytes.  To do this, make sure you follow the syntax for size:  "+100M" is 100Mb.  If you forget the "+", then you are inadvertently specifying a cylinder number, even if you added the "M".    The default "system id" (or filesystem type) is "Linux filesystem or 83". 

Go through each of the devices in the table above, running the "n" command three times. 

The "p" command can be used at nearly anytime, to list out the partition table.  Use the "p" command to make sure the table looks correct.

After all of the partitions are created, use the "t" command to change the swap area's system id to "82".  Type the "t" command and choose the swap area partition number and change the system id to 82.

Use the "a" command to make the "/" filesystem bootable.  I think Linux ignores this, but most bootable filesystems have this flag set.

Use "p" to print out the table one more time.  Ensure everything looks as expected.  This is the point of no return!

After the partitions are created, use the "w" command to write the table to disk.  This will take several seconds to write out the partition table.  The partition table is stored in the first 512 bytes of the disk, along with the MasterBootRecord (MBR)

Step 4 - Make filesystems, labels and swap area.

Now that the disk has a partition table, it is time to put file systems on each partition. 

The command to make filesystem in Linux is: mkfs

Mkfs will be used to make "ext3" filesystem on the relevant partitions (/ and /boot).   For example, to make an ext3 filesystem for / and boot use the following (note, X will be either a, b, c, or d):

mkfs -t ext3 /dev/hdX1    (this creates an ext3 filesystem on  "/boot" )
mkfs -t ext3 /dev/hdX3    (this creates an ext3 filesystem on "/")

Prepare the swap area for use with Linux command mkswap.  Again, using the table above, the swap are is on /dev/hda2.   Use the following command, replaced the "X" with the appropriate, 'a', 'b', 'c'.

mkswap /dev/hdX2

Almost done.  Use the e2label command to put filesystem labels on the new disk.

e2label /dev/hdX1 /boot
e2label /dev/hdX3 /

Step 5 - Copy the data


The disks are ready to use.  Now is the time to copy existing data from the source drive, to the the new drive.  The copies will proceed, one partition at a time.  The first step is to create mount points (i.e. these are just directories on the existing system) for the new drive's partitions and mount the partitions onto those directories.  "/" and "/boot" are the two partitions that need to be copied.  For this example,  /clone_boot and /clone_root (you can use any names) will be used.  The command mkdir will be used to create the mount points.

Copy existing /boot partition to the new /boot partition that will be mounted on /cloneboot.  Note the "." in the "cp -ax /boot/. command.  "." means this directory)

mkdir /clone_boot (make the directory that will be used as the mount point)
mount /dev/hdX1 /clone_boot  (mount the new file system on this new directory)
cp -ax /boot/.     /clone_boot  (copy the data from the existing, active /boot directory to the new filesystem)

Now, copy the / and everything else (this one will take the longest depending on how much data is on the system).

mkdir /clone_root
mount /dev/hdX3 /clone_root
cp -ax /.   /clone_root

Type the sync command to ensure that all data has been written to the new disk:

sync

Halt the system.

halt

Only two more steps!

Step 6 -  Swap disks around and configure  the MBR (Master Boot Record).


This is the final step.  The operating system is fully cloned and ready to use.  This step make the disk bootable using the Grub boot loader.  The Master Boot Record (MBR) contains the program that the BIOS uses to bootstrap the operating system.  The MBR needs to be updated so that it knows how to find the Linux boot programs.  The Redhat install/recovery CD-ROM is used to boot a standalone kernel and install the  MBR. 

Halt the system and remove all of the original hard disks.  Only the newly cloned disk is in the system at this point.  We remove the original disks, as this next step could be dangerous and destroy data.   Pull out the original boot disk and ensure that the jumper settings on the newly cloned disk are identical to the original.  Install the cloned disk into the IDE port that the existing boot disk was in.  That is, unplug the source disk, verify jumper settings and install the new disk in the original boot drives slot.  We remove the original source disks to avoid, unintential damage to that source drive.

Install the Redhat Disc 1 into the CDROM and power up the system.  At the boot prompt, go into Linux rescue mode.  That is, at the first installation screen, type:

linux rescue

The system will boot off the CDROM into a special Linux that runs out of RAM.  As the system boots up into this mode, it will ask questions about the preferred language, keyboard, etc.  It looks like the system is going to do a full install, but after only a few questions, the system will ask if you want a command prompt (or command shell).  Answer yes to that question.

Run "df" to ensure that the hard disk partitions have been mounted with the devices as expected (e.g. /dev/hda1 and /dev/hda3).

Also, type the command:

chroot /mnt/sysimage

At his point, we do what is called a "grub-install".   The grub-install command installs the grub boot loader into the MBR (Master Boot Record) of the boot disk.   Here it goes, run grub-install:

grub-install /dev/hdX     (again, replace /dev/hdX with /dev/hda or the appropriate boot device for this system)
sync

Note:  If this grub-install fails with, "unable to find stage2....", then try this alternative.  This assumes that the boot disk is the first disk in the system.  Run "grub", not grub-install, and type the following:

grub
grub >  install (hd0,0)/grub/stage1 (hd0) (hd0,0)/grub/stage2 p (hd0,0)/grub/grub.conf
grub > quit


Type exit:

exit

Type reboot and pop out the cdrom after the machine starts to reboot:

reboot

The system should boot normally.  Check for errors.

At this point, we can re-enable the /data drive in the /etc/fstab file.  Halt the system, reinstall the data drive, and power the system back up. The system should be a fully functional clone of the original.