Copying Windows to a new drive, using linux - How-to/Guide
Written 20 Mar 2005 by Ed Anderson. Updated 18 May 2009 (NTFS
Updates; device names)
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.
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.
If you're not familiar with this naming scheme for
disks/partitions, read the Linux Partition HOWTO.
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:
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.
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/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 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.
Start qtparted from the command line.
Select the new disk (/dev/sda)
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.
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
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/sda, and run
# mkfs.vfat -F 32 /dev/sda1
mkfs.vfat 2.10 (22 Sep 2003)
# mkdir old new
# mount /dev/sdb1 /mnt/old
# mount /dev/sda1 /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 com