Main: About Me | Pictures | Resume | Calendar
Useful Stuff: PuTTY | WinSCP | X-Chat
Babies: TheNinthCut | github
nilbus nilbus nilbus nilbus nilbus.com

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

Written 20 Mar 2005 by Ed Anderson. Updated 8 Apr 2010 (How to view dd progress; recommend ntfsclone)

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
  7. FAQ

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. For example, yours might be /dev/sda and /dev/sdb.

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.

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:

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. If this is an NTFS filesystem, you can use ntfsclone to copy it very efficiently. In this guide, we use dd, which will work with any filesystem type, but will be slower. This time, instead of working with the disk device (sda, sdb), we'll be using the partition device (sda1, sdb1).
# 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.

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 to remove any errors in the newly created filesystem.

Method 2: ntfsresize

(no examples yet)

Method 3: parted

If you don't have a GUI, you can use parted to resize almost any non-ntfs partition. 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 hav
e
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.

FAQ


Comments

geoff
05 Jun 2005, 14:35
Hi there,

I just wanted to let you know that I found your article extremely helpful. I just successfully moved my old drive C: to a new disk using knoppix, dd and qtparted, just as you outlined.

There was one small glitch, however. When I went to boot with the new drive, Windows 2000 was complaining about not having a paging file. Normally, I understand, Windows would attempt to create a new paging file if it can't locate the configured file. However, the issue here is that
the Windows registry didn't recognize the new drive as drive C:. This meant that many settings in the registry that pointed to drive C: (besides just the paging file) were unhappy.

To rectify the problem, I used Microsoft's knowledge base article KB223188
- http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q223188.

It might be helpful to future users to add a link.

Thanks,
-g
Wakko Warner
24 Jul 2005, 20:26
I have had fairly good luck doing this where I work. I did notice a problem with office2000 since it likes to write the 8.3 path names in the registry and if another directory gets there first, it does cause problems which I noticed recently.

I wrote most of the system in perl. I have a 2 stage boot system that loads up my perl based menu system. Stage 1 actually fits on floppy. It searches for the stage 2 which is like a mini-distro that loads into tmpfs via a tar file. Since I deal mostly with windows 2000 and xp, there are problems with hard disk geometry and the fact that the fat32 partition MUST contain the bios boot disk number. It took me several tries to realize this. Also if the heads/sectors in the fat32 boot record don't match the partition table, you will have problems as well. I have a program that installs the boot loader into fat32 for 2000/xp (same thing actually).

I came across your site because I was looking for a way to resize a fat32 partition under linux. (I have an iPod which I was forced to convert from mac hfs+ to vfat due to linux's bad support for hfs+)
Juho Leppamaki
22 Sep 2005, 03:36
Hello!

I would like to suggest adding a dma enable option to your guide. I'm using knoppix 4.0 live-dvd and the copying was really slow. I enabled dma on my old PATA-drive with another root shell, and the copying became much faster. I just typed 'hdparm -d 1 /dev/hda'.

Thanks for the great yet simple guide :)
Joshua Michael Smith
12 Nov 2005, 12:42
Just wanted to say thanks for the guide about copying Windows you have. It's really the best and clearest of its kind that I could find on the web. I was able to rescue WinXP on FAT32 for a friend, with the only problem being that their source drive was dying...which was the reason for the move in the first place.

Again, thanks - the clear writing made it very easy.
Brian Dolan-Goecke
02 Feb 2008, 13:18
Great guide.

I ran across a way to get status on the dd while it is running. (It was in the manpage for dd) If you send it a USR1 signal it will output status on standard out. So you could use these commands to do the copy and output copy stats while it is working. Be sure to use the correct partions.

dd if=/dev/hdb1 of=/dev/hda1 bs=4096& pid=$!
while true ; do kill -USR1 $pid; date; sleep 10; done

That will give you stats every 10 second from the dd command. You will have to stop the while loop by hand (with a ctrl-c in the terminal) after the dd is done. The while loop will continue, but get an error.

Have fun, thanks again.
BTW. It looks like it will take 3 hours to copy a 60GB disk connected up via USB!
magic_ninja
21 Aug 2008, 23:24
well I used this guide to copy a 12 gig partition from my old hard drive onto a new 80 gig for windows, i use linux mostly but I use windows for games, this guide was really easy as I have not used dd before, keep up the good work
Gennady
14 Oct 2008, 23:20
I've spent several hours trying to copy a bootable NTFS drive with Windows XP (6 GB) to a larger one (20 GB) using the commercial Drive Image program. I was not able to do it due to various problems. After having found your tutorial, I did the job in 20 min.! Thanks a lot.
steve
30 Dec 2008, 16:38
Very cool directions...

The only issue I had is that after resizing using qtparted and rebooting, I got a no system disk or disk error message. I had to use Partition magic to set the NTFS partition to \"active\" and then it booted right up.

These directions are a life-saver!!!!

cheers
Frank Parker
09 Jan 2009, 00:11
I successfully cloned windows ME from a 20 gig drive to a 500 gig drive. The disk was formatted in fat 32. The only issue is that I used knoppix 5.1 which demanded more memory then the computer had. I should have used an older version of knoppix.

Thanks for the help!
TexasEric
23 Jan 2009, 14:16
Great directions! I would suggest users try and use dd_rescue as opposed to the older dd. dd_rescue has the ability to grab larger and larger slices until it finds a disk error. When it does find an error, it shrinks the slices down smaller and smaller so that only the 'damaged' portions are skipped. It is a great little program.
Randall Wayth
13 Mar 2009, 21:28
Just wanted to say thanks for the excellent page on copying a windows installation to a new disk. I have just done this to a new, larger hard disk in my laptop where I went from a 10G NTFS windows to a 15G NTFS.

It might be worth updating the page regarding the caveats about NTFS. Writing to NTFS partitions has been supported for a few Ubuntu releases now. Furthermore, copying from a smaller->larger partition is super easy using ntfsresize.

Recipe is:
create new partition
copy, via dd, contents of old to new
ntfsresize new

Windows did a disk check when I booted into it after the copy/resize, but otherwise everything went fine.

Cheers
Boes
25 Jun 2009, 13:59
Thanks.

I burned a SystemRescueCd cd from www.sysresccd.org and followed your directions with succes.

The disk was partitioned with a primary partition (sda1, windows disk C) and an extended partition (sda2). In the extended partition another NTFS partition existed (sda5, windows disk D). On the new disk I created the same partitions, where the extended partition can get a size greater than or equal the extended partition on the old disk. I only needed to dd copy the MBR, sda1 and sda5 to the new harddisk.

Restarted the computer without the old harddisk and now everything is up and running!
Perlakowski
26 Jun 2009, 18:56
absolutely gorgeous. just wonderful. :),
Stephen Winnall
09 Jul 2009, 15:38
Thanks for this excellent article. It worked a treat when I copied a Vista system from a 500 GB to a 1000 GB disk.

There was one drawback though. The copy insisted that it was the E: disk when I booted from it and apparently tried to referenced stuff on the C: disk, which was not good at all when I removed the C. disk completely from the computer.

The following article helped me to resolve that problem by making the copy boot as the C: disk:

http://www.multibooters.co.uk/articles/drive_letters.html
Norman Ramsey
26 Jul 2009, 22:39
Thanks for the helpful tutorial. I used ntfsclone instead of dd, and after running qtparted I had to run ntfsresize to get not only the partition but also the filesystem up to full size. But everything's great and I'm now having windows check the new disk for bad sectors (full hardware scan).

Thanks a lot for the helpful tutorial.
Lazyfirecloud
25 Aug 2009, 18:57
Thank you so much! This tutorial has been very helpful. The MBR always ends up screwing me =)

I personally used gparted to copy and resize each partition's content over; it uses ntfsclone and ntfsresize, as well as checks the filesystems for errors before/after each operation. It's quite the tool =)
Lazyfirecloud
25 Aug 2009, 18:57
Thank you so much! This tutorial has been very helpful. The MBR always ends up screwing me =)

I personally used gparted to copy and resize each partition's content over; it uses ntfsclone and ntfsresize, as well as checks the filesystems for errors before/after each operation. It's quite the tool =)
Steve Brodie
14 Oct 2009, 16:54
Very interesting guide, and much needed since clear information on how to do this with Free and Open Source Software (FOSS) seems to be hard to find.

I am curious about the 'dd if=/dev/sdb of=/dev/sda bs=446 count=1' instruction: is the 'bs=446' specific to the windows OS, or would the same '446' number be applicable to linux systems? I ask because typically I see the number '512' used in other instructions about how to backup an MBR; maybe this number is applicable to linux?

Can anyone confirm?

Thanks,
SB
Ed Anderson
14 Oct 2009, 17:10
466 excludes the last 46 bytes, which is where the partition table is stored. Since tha partition table would differ on disks of different geometry and size, it's probably better to recreate the partition table using the right tools rather than copying the source disk's partition table.
Steve Brodie
15 Oct 2009, 10:19
Ed Anderson> 466 excludes the last 46 bytes, which is where the partition table is stored. Since tha partition table would differ on disks of different geometry and size, it's probably better to recreate the partition table using the right tools rather than copying the source disk's partition table.

So if I understand you correctly you are saying that:
1. It is better to use '446' rather than '512';
2. It is always better to do so (in this context anyway);
3. The '466' value is a constant that will always apply, regardless of what OS one is working with.

Is this correct?

Thanks,
SB
Ed Anderson
15 Oct 2009, 11:28
Sorry, I typo'd the number and then used it in my math. :P It's 446 bytes, leaving 64 bytes for the partition table and a 2 byte signature. See http://en.wikipedia.org/wiki/Master_boot_record

Yes, this is OS independent.

Following this guide, you would not want to copy all 512 bytes of the boot sector to the new disk, because it would copy the partition table from the old disk and write over the partition table you had just created on the new disk.

Different disks will usually have different disk geometry (number of cylinders, heads, sectors). Since the partition table stores partition locations using these values, I don't know what the behavior would be if you copied the partition table from one disk to another of a different size.
G. Miller
16 Jan 2010, 09:07
Thank you for the excellent guide. This is an extremely slick and easy way to copy a system. I was able to copy the system without any problems, but had some difficulty with the partition resize. I tried 4 or 5 different Linux LiveCDs, but couldn't get gparted to work (160GB SATA drive, ntfs). It kept reporting a bad sector problem that didn't seem to actually exist (ran chkdsk several times, no problems). I poked around the net and ran across EASEUS which I downloaded (free for home use) and it seemed to do the trick.

I really appreciate the work you put into this.

GM
Flyte
25 Jan 2010, 21:21
Excellent tutorial - followed it and it worked perfectly, except for the partition resizing.
I moved from a 64GB SSD to a 150GB VelociRaptor on Windows 7.
Instead of resizing the partition in Knoppix, I had to resize it within Windows, otherwise the partition would resize but the drive size in Windows Explorer would not. Quite strange!

Thanks for your article :D
Shashi
22 Feb 2010, 02:10
Excellent article.
Thanks a lot :-)

I am going to try it.

Thanks,
Shashi.
John
28 Feb 2010, 07:09
Thanks for the tutorial, it was helpful. I had to do some more research though for the case when the disk geometry was different, and for a different partition layout. That requires some hacking of the partition boot sector and the registry to get windows to boot.

This page has some details on this: http://www.2pi.info/software/copying-windows-new-hard-drive.html
marty rehder
05 Apr 2010, 20:39
I followed the instructions to get a new 500GB disk setup from an existing 200GB except for one part - I didn't use a Live linux CD but a full Fedora Linux install.

When I try to boot the new drive, the bios give me an error "invalid partition table". But under Linux, the drive mounts up just fine and all the files are there as expected.

Any clues as to what I might have done wrong?
Milo
13 Apr 2010, 12:19
Thanks for the great tutorial. My question concerns the necessity of using fdisk to partition the new drive exactly like the old one, then using dd to copy everything else. Why not just use dd to copy the old disk --> new disk, then resize? Will the old partition table be incorrect on the new, larger drive?
Ed Anderson
15 Apr 2010, 18:34
The link John posted talks about the cylinder/head/sector count that is contained in the partition table.

www.2pi.info/software/copying-windows-new-hard-drive.html

We don't copy the partition table to the new disk in case these values change. Disk geometry may differ on different disks of different size.
Micca
24 Apr 2010, 04:50
Hi there... can anyone tell me, if this method.. (or dd in particular) preserves the extended attributes of ntfs? If not, why not and is there a linux utility which can?
Ed Anderson
24 Apr 2010, 09:19
Yes, since the extended attribute information is contained within the partition, it will certainly be copied by dd. dd copies everything byte for byte, without regard for what it is.

Also, ntfsclone and ntfsresize both rely on libntfs, which is aware of extended attributes. (http://www.tuxera.com/community/ntfs-3g-advanced/extended-attributes)

Milo
24 Apr 2010, 11:50
I certainly understand the concept of different drive geometries. One example would be the advent of EIDE electronics into the newer drives. Nonetheless, differences in drive geometries should be transparent in all but a few special cases. In fact, a direct dd image onto a new drive boots and works fine both before and after resizing with gparted. So, did I just get lucky? The method above will work, but seems unnecessarily complicated.
Ed Anderson
24 Apr 2010, 15:33
You could very likely be right. I wasn't sure it would work in every case, so I wrote the guide this way to be safe.

People always say "in rare special cases." What would such a case be? If it never realistically happens, I should probably cut the tedium out of the guide.

On the other hand, dd is probably the least efficient method when compared with ntfsclone, making this a moot point.
Chris
21 Jun 2010, 05:41
Great write-up! To be safe, run CHKDSK on the original drive before starting. Even a running/bootable NTFS partition may not mount in Linux (or be resized) unless you do.

This will save you some time.
stephen
31 Jul 2010, 18:59
to resize an ntfs partition to take up all available space on sda1 under linux, do the following:

# ntfsresize /dev/sda1
dE
03 Sep 2010, 10:34
I'm trying to just copy stuff up... let's see.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
Powered by Scriptsmill Comments Script