Copying Windows to a new drive, using linux - How-to/Guide
Written 20 Mar 2005 by Ed Anderson. Updated 13 Oct 2005 (Suggest setting DMA before copying partitions)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.
Note: This may or may not work with NTFS because of sectors that windows may write to outside the partition table. I haven't verified this either way. This being said, please feel free to try this howto using NTFS, and tell me how it works. There's no reason not to experiment when copying to an empty drive. :)
- Gathering tools
- Physical Installation
- Preparing new partition table
- Copy the MBR
- Copy the Partition
- Resizing the Partition
- FAQ
Gathering Tools
For this project, you will need a Linux Live CD that has dd, fdisk, parted/qtparted, and ntfsresize (for NTFS partitions).
Knoppix is a Live CD that has everything you'll need. 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/hda (Primary Master) - New, empty 80G driveYour disk setup can vary, but in this guide, I will refer to the new drive as /dev/hda, and the old as /dev/hdb. Use device names according to your own setup.
/dev/hdb (Primary Slave) - Old 10G drive with all data on one NTFS partition (/dev/hdb1)
If you're not familiar with this naming scheme for disks/partitions, read the Linux Partition HOWTO.
To sum it up, IDE disks are referred to by /dev/hdX where X is corresponds to the disk order (a for the first disk, d for the last). Partitions are referred to as /dev/hdXY where Y is the partiton number. (eg. /dev/hdc2 is the 3rd disk, 2nd partition) SCSI disks are the same, but sdX instead of hdX.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/hdb Disk /dev/hdb: 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/hdb1 * 63 19968794 9984366 7 HPFS/NTFSNote 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/hda 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/hda: 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 SystemIf 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/hda: 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/hda1 63 19968794 9984366 83 LinuxBefore 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/hda: 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/hda1 * 63 19968794 9984366 7 HPFS/NTFSThe partition should now look identical to when you ran fdisk -l -u /dev/hdbNow 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, me 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:
- Boot Code (446 bytes)
- Partition Table (64 bytes)
- Boot Code Signature = 55aa (2 bytes)
# dd if=/dev/hdb of=/dev/hda 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/hda /dev/hda: setting using_dma to 1 (on) using_dma = 1 (on) # hdparm -d 1 /dev/hdb /dev/hdb: setting using_dma to 1 (on) using_dma = 1 (on)The next task is to copy the filesystem from one disk to the other. We'll do this with dd as well. This time, instead of working with the disk device (hda, hdb), we'll be using the partition device (hda1, hdb1).# dd if=/dev/hdb1 of=/dev/hda1 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 verify that it's doing "something" by running top in another terminal.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.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.
- Start qtparted from the command line.
- Select the new disk (/dev/hda)
- Right click on the partition to rezise, and click Resize.
- Change "Free Space After" to 0, and press OK. The partition should now span the disk.
- Commit the changes to disk using the the Commit option in the File menu.
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/hda 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/hda 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? iDisplay the current partition table (p), and resize the partition to the size of the drive.(parted) p Disk geometry for /dev/hda: 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
- Why can't I just copy the files, instead of the whole partition?
I haven't experimented doing this on Linux. You can't if you have NTFS partitions, since linux can't write files on an NTFS partition.On FAT32, I would be afraid of losing windows file attributes, missing files, and messing up file order. The advantage would be that no unused space on the filesystem would need to be transferred. Someone please let me know if you've tried, and I'll update this page.
If I were to try this method on a FAT32 disk, I would create a large partition on /dev/hda, and run
# mkfs.vfat -F 32 /dev/hda1 mkfs.vfat 2.10 (22 Sep 2003) # mkdir old new # mount /dev/hdb1 /mnt/old # mount /dev/hda1 /mnt/new # cd old # find | cpio -pdV ../new- Can't I create the MBR with the DOS fdisk /mbr, instead of dd?
Yes, but that would require making a boot disk, or something else that requires more effort than dd. :) Since we're in the Linux environment, it's easier to use dd.
- I have a suggestion or need some help. Can I contact you?
Yes. You can find me on IRC on irc.freenode.net #Nilbus, or contact me at nilbus AT nilbus DOT comComment Script Comments
Powered by Comment Script
rPath Linux
System Uptime: 14:53:08 up 176 days, 15:23, 9 users, load average: 0.09, 0.04, 0.01
Thanks a lot for this howto, it perfectly answered my needs (creating a ghost of a bootable NTFS windows partition). I found a typo in the howto. The dd command line to copy the MBR copies the empty hard drive's MBR to the old hard drive. Inverting hda and hdb in this type of command can be quite problematic...
Thanks again,
Matthieu
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
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+)
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 :)
Regards,
Juho Leppämäki
Again, thanks - the clear writing made it very easy.
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!