Nilbus is love

I am a Ruby on Rails & Javascript web developer who loves making things better for people. I also study piano and play ultimate frisbee.

Copying Windows to a new drive, using linux - How-to/Guide

Written 20 Mar 2005 by Edward Anderson. Updated 20 May 2014 (warn about the non-intuitive ntfsclone argument order)

Another good resource: www.2pi.info/software/copying-windows-new-hard-drive.html

Overview

This guide will show you how to copy an existing installation of Windows (or any other OS) from one drive to another - as long as the destination drive is the same size or larger.

This is a free and relatively easy method that will create a clone of your current hard disk, without having to buy any third party software.

  1. Gathering tools
  2. Physical Installation
  3. Preparing new partition table
  4. Copy the MBR
  5. Copy the Partition
  6. Resizing the Partition

Gathering Tools

For this project, you will need a Linux Live CD that has dd, fdisk, parted/qtparted, and ntfsresize.
Knoppix is a Live CD that has everything you'll need. Many other Live CDs will work as well, such as an Ubuntu install CD.
Burn the contents of the ISO to CD to boot from later.

Physical Installation

Install both hard drives in your computer. In this guide, I will use the following disk setup as an example:

/dev/sda (Primary Master) - New, empty 80G drive
/dev/sdb (Primary Slave) - Old 10G drive with all data on one NTFS partition (/dev/sdb1)

Your disk setup can vary, but in this guide, I will refer to the new drive as /dev/sda, and the old as /dev/sdb. Use device names according to your own setup.

To sum it up, SCSI disks are referred to by /dev/sdX where X is corresponds to the disk order (a for the first disk, d for the last). Partitions are referred to as /dev/sdXY where Y is the partiton number. (eg. /dev/sdc2 is the 3rd disk, 2nd partition) IDE disks are the same, but hdX instead of sdX.

Preparing new partition table

Before you start, you may want to run scandisk on your drive to verify that there are no errors on the disk.

Boot from the Live CD. Press enter at the boot: prompt.

Open a new terminal as root. In knoppix 3.7, click on the Penguin icon, and click Root Shell. Otherwise, just open a normal terminal and run

$ sudo su
to switch to the root user.

View the partition table for the old disk:

# fdisk -l -u /dev/sdb

Disk /dev/sdb: 10.2 GB, 10240000000 bytes
255 heads, 63 sectors/track, 1244 cylinders, total 20000000 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *          63    19968794     9984366    7  HPFS/NTFS
Note that I used the -u option on fdisk. This displays the start and end units in sectors, rather than cylinders, since the cylinder size varies from disk to disk.

Record the Start and End positions, and the Id for the existing partition.

Now run fdisk on the new disk.

# fdisk /dev/sda
Device contains neither a valid DOS partition table, nor Sun, SGI or
OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 158730.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by
w(rite)

Command (m for help):
When you run fdisk on an emptry drive, it may tell you that there is no partition table, or that the disk is large. You may safely ignore these warnings.

Print the current partition table to verify that there are no partitions on the disk.

Command (m for help): p

Disk /dev/sda: 81.9 GB, 81920000000 bytes
16 heads, 63 sectors/track, 158730 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System

If there are partitions listed, and you were expecting none, make sure you ran fdisk on the correct device. You should delete the existing partitions on the new disk, only if you know that the data is not needed.

Create an identical partition, using the Start and End positions from the other disk. Be sure to change the units to Sectors.

Command (m for help): u
Changing display/entry units to sectors

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p

Partition number (1-4): 1
First sector (63-159999999, default 63): 63
Last sector or +size or +sizeM or +sizeK (63-159999999, default 159999999): 19968794

Command (m for help): p

Disk /dev/sda: 81.9 GB, 81920000000 bytes
16 heads, 63 sectors/track, 158730 cylinders, total 160000000 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              63    19968794     9984366   83  Linux
Before we're done, we must set the Boot flag and System Id. Use the same Id that was listed on your old partition table.
Command (m for help): a
Partition number (1-4): 1

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 7
Changed system type of partition 1 to 7 (HPFS/NTFS)

Command (m for help): p
Disk /dev/sda: 81.9 GB, 81920000000 bytes
16 heads, 63 sectors/track, 158730 cylinders, total 160000000 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63    19968794     9984366    7  HPFS/NTFS
The partition should now look identical to when you ran fdisk -l -u /dev/sdb

Now we must write the changes to disk.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Copy the MBR

For the new disk to boot, we must copy the boot code from the Master Boot Record (MBR) to the new disk.

The MBR is on the first sector of the disk, and is split into three parts:

  • Boot Code (446 bytes)
  • Partition Table (64 bytes)
  • Boot Code Signature = 55aa (2 bytes)
We only want to copy the boot code - the first 446 bytes. We do this with dd:
# dd if=/dev/sdb of=/dev/sda bs=446 count=1
1+0 records in
1+0 records out
446 bytes transferred in 0.026312 seconds (16950 bytes/sec)

Copying the Partition

Optional: We should try to enable DMA on both disks, to increase the transfer speed.

# hdparm -d 1 /dev/sda
/dev/sda:
 setting using_dma to 1 (on)
 using_dma    =  1 (on)

# hdparm -d 1 /dev/sdb
/dev/sdb:
 setting using_dma to 1 (on)
 using_dma    =  1 (on)
The next task is to copy the filesystem from one disk to the other. This time, instead of working with the disk device (sda, sdb), we'll be using the partition device (sda1, sdb1).

If this is an NTFS filesystem, you can use ntfsclone to copy it very efficiently.

# ntfsclone --overwrite /dev/sda1 /dev/sdb1
Add the --rescue option if there are bad sectors on the source disk.
# ntfsclone --rescue --overwrite /dev/sda1 /dev/sdb1
Be careful that your source drive (sdb) is the last argument. The argument order may be counter-intuitive, and doing this backward would be bad.

If it's not NTFS, you can use dd, which will work with any filesystem type. If your source disk has bad sectors, you should use ddrescue instead of dd.

# dd if=/dev/sdb1 of=/dev/sda1 bs=4096
2496092+0 records in
2496092+0 records out
10223990784 bytes transferred in 355.312 seconds (28774684 bytes/sec)
This may take a long time, especially if the partition size is large. You can view the progress at any time by sending SIGUSR1 to the dd process:
# pkill -SIGUSR1 ^dd$
The terminal running dd will output something like:
1261525+0 records in
1261525+0 records out
5167206400 bytes transferred, 152.312 seconds (33925143 bytes/sec)

Resizing the Partition

Next, we resize the partition to fill the disk. You should only do this section if you are copying to a larger disk, and don't want to leave unpartitioned space.

By far, the easist way is with qtparted. It is a graphical front-end to parted and ntfsresize. If you are using a Live CD without a GUI, you'll have to use parted or ntfsresize.

If Windows was not shut down properly, the partition is dirty and cannot be resized. The resizing tools will tell you if the this is the case. If needed, boot into your new Windows partition and shut down cleanly before continuing.

Method 1: qtparted

This method should work with most filesystems, including NTFS and FAT32.
  1. Start qtparted from the command line.
  2. Select the new disk (/dev/sda)
  3. Right click on the partition to rezise, and click Resize.
  4. Change "Free Space After" to 0, and press OK. The partition should now span the disk.
  5. Commit the changes to disk using the the Commit option in the File menu.
After the commit operation finishes with your disk, you can reboot onto your new hard drive, and everything should work. Since you resized your partition, be sure to run scandisk (FAT32) or chkdsk (NTFS) to remove any errors in the newly created filesystem.
chkdsk C: /f /r

Method 2: ntfsresize

On the command line, ntfsresize works great for NTFS filesystems. First, delete the old partition and create a new one in its place that matches the new disk size. Deleting a partition does not erase the data that exists there. Just make sure the new partition is at the same start position. Finally, run ntfsresize to grow the filesystem to the size of the partition it's now in.

  1. Use fdisk to recreate the partition to be resized with the new size:
    # fdisk /dev/sda
    Command (m for help): u
    Changing display/entry units to sectors
    
    Command (m for help): d
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    
    Partition number (1-4): 1
    First sector (63-159999999, default 63): 63
    Last sector or +size or +sizeM or +sizeK (63-159999999, default 159999999): 159999999
    
  2. Resize the partition:
    # ntfsresize /dev/sda1

Method 3: parted

For any non-NTFS filesystem, you can use parted. In this example, I'll resize a FAT32 partition to fill the drive.

Run parted. You may safely ignore errors about the partition alignment.

# parted /dev/sda
GNU Parted 1.6.9
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
This program is free software, covered by the GNU General Public License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Using /dev/sda
Warning: Unable to align partition properly.  This probably means that another
partitioning tool generated an incorrect partition table, because it didn't have
the correct BIOS geometry.  It is safe to ignore, but ignoring may cause
(fixable) problems with some boot loaders.
Ignore/Cancel? i
Display the current partition table (p), and resize the partition to the size of the drive.
(parted) p
Disk geometry for /dev/sda: 0.000-78125.000 megabytes
Disk label type: msdos
Minor    Start       End     Type      Filesystem  Flags
1          0.031   9750.234  primary   fat32       boot, lba
(parted) resize
Partition number? 1
Start?  [0.0308]? 0.0308
End?  [9750.2339]? 78125.000
(parted)
This may take some time, depending on the size of the partition. When it's done, you can reboot into windows. Since you modified the partition, be sure to run scandisk to remove any errors in the newly created filesystem.

Contact

I'm always glad to help if I can. Feel free to shoot me an email: