PDA

View Full Version : HOWTO: Moving/Copying Windows XP to another Partition


habicht
September 10th, 2008, 04:25 PM
The following is mainly based on Michael Dominok's tutorial on how to fork an XP installation with Linux [1] who gives more details than I do here. Details on how to move a partition using Windows can be found in [2].

PROBLEM

I wanted to copy my entire system, including both a Ubuntu and a Windows XP partition to a new, bigger harddrive and change the order of partitions. I used my old harddrive in an external USB case (/dev/sdb), the new one was installed in my laptop.

PROCEDURE

There are many tutorials on how to copy a Linux partition (e.g. with live CD Ubuntu or Knoppix). Summary: create partition with fdisk or gparted, copy partition using "dd if=/dev/sdb1 of=/dev/sda2" (sdb1=source, sda2=destination), use "resize2fs /dev/sda2" to adapt the partition to its new size (if target partition is bigger than source), adapt "/boot/grup/menu.lst" on target partition and (re-)install grub properly.

To move a Windows XP installation is a little more work but still easy if you know what to do. My XP partition was to be moved from /dev/sdb1 to /dev/sda2. A prerequisite for the following steps is that the target partition has been created already, e.g. using fdisk or gparted. You can perform the following steps by using the already installed Linux.


1. Prepare Windows XP

You NEED to reset Window's information about mounted partitions before you move it! Use "regedit" and delete all entries in
"HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices". This does not harm Windows since Windows rebuilds these entries on the next startup.

Since I forgot to do this at first, I had to do this as a last step. I exchanged the harddisks again and booted the "old" Windows installation. Here, I did the regestry editing as described above. Then I exchanged the harddisks again and copied the relevant registry file from the old to the new partition, which is C:\Windows\system32\config\system. You might want to do a backup of the old "system" file, just in case.

In case you forgot the above step:

Method 1 - the Windows way:
Since I forgot to do this at first, I had to do this as a last step. I exchanged the harddisks again and booted the "old" Windows installation. Here, I did the registry editing as described above. Then I exchanged the harddisks again and copied the relevant registry file from the old to the new partition, which is C:\Windows\system32\config\system. You might want to do a backup of the old "system" file, just in case.

Method 2 - the Linux way:
I could not find a way how to modify the registry by using "wine", but you can use "chntpw" (apt-get install chntpw) which is a simple opensource command line registry editor ("?" gives help output):
chntpw <PATH-TO-WINDOWS>/system32/config/system
ls
cd MountedDevices
ls
delallv
q

I could not find a way how to modify the registry by using "wine".


2. Copy the Windows Partition

In [1], ntfsclone is used. Alternatively, you can use gparted. I simply used "dd". This takes a while and doesn't produce any output, so don't wonder:

dd if=/dev/sdb1 of=/dev/sda2


3. Modify Windows Boot sectors

Taken from [1]:
Now, the boot-sectors of the newly cloned XP-partition have to be
modified. The partition(s) in front of the new ones have to be
"skipped". This is done by inserting an offset into the partitions
boot-sector. But first we've got to determine where they start. "fdisk
-ul /dev/hda" shows:

Device Boot Start End Blocks Id System
/dev/hda1 * 63 40965749 20482843+ 7 HPFS/NTFS
/dev/hda2 40965750 81979694 20506972+ 7 HPFS/NTFS
/dev/hda3 81979695 122993639 20506972+ 7 HPFS/NTFS
/dev/hda4 122993640 181582694 29294527+ 5 Extended
/dev/hda5 122993703 181582694 29294496 83 Linux

The interesting values are listed in the "Start"-column but have to be
converted into hexadecimal and rearranged in order. "printf "0x%llx\n"
40965750" printfs 40965750 in hexadecimal format "0x2711676"
"printf "%x" 40965750" would have done, too.
The hexadecimal value "2711676" has to be rearranged further. The digits
have to skewed by pairs following this method: "0xABCDEFGH => GH EF CD AB"
For "2711676" this results in "76167102"
Since we've got 4 pairs to skew but only 7 digits available we simply
add a leading 0. This is as neutral in the hexadecimal system as it is
in the more familiar decimal system.
Now "76167102" has to be inserted into hda2s boot-sector. That's done
with "hexedit /dev/hda2"
Move the cursor to position "0x1c" and type in "76 16 71 02", then
save&quit with "<STRG>-X"

If you copied the source partition to a bigger destination partition, a resizing of the file system is necessary. This is described further down in point 6.


4. Adapt Boot Manager "grub"

If the partition number has changed (e.g. you copied from partition number 3 to 2 (counting from 1, independent of the disk), edit /boot/grub/menu.lst and add or set the right partition for the windows installation, which is (h0,1) for /dev/sda2 (disk 0, partition 1), in my case:

title Microsoft Windows XP Professional
root (hd0,1)
savedefault
makeactive
chainloader +1


5. Adapt Window's BOOT.INI

What has been done for the grub menu needs to be done for Window's boot loader as well. Mount the new Windows partition (/dev/sda2 in my case) and edit c:\BOOT.INI to use the right partition number which is "partition(1)" in my case (for /dev/hda2, [1] uses partition(2) though):

[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOW S
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="XP on sda2" /fastdetect
/NoExecute=OptIn


6. Adapting the file system size to the partition (resizing)

ntfsresize is an opensource tool to resize ntfs partition in Linux. This should NOT be used at this point. Using this tool can render your windows partition unusable. I don't know the exact reason, but when I applied it, a user session was immediately logged out after logging in to windows. Several attempts to solve this problem did not succeed (replacing userinit.exe, checking userinit.exe registry entry, renaming the disk ID with hexedit ("disk has been before"-effect)). I cannot tell whether this only happend because I forced the ntfsresize (-f -b), although ntfsresize gave warnings about bad sectors.

The windows partition should be able to boot by now. The last step is to boot windows and execute:
chkdsk /f /r"
Windows will tell you that it needs to reboot to execute this step. chkdsk removes all existing ntfs inconsistencies and resizes your file system to the partition size.


7. Done


Enjoy! I hope it works for you as nicely as it worked for me.



[1] Michael Dominok: "How to fork a XP-installation", 2005, www.dominok.net/en/it/en.it.clonexp.html
[2] "Move an entire Windows installation", http://winhlp.com/node/66

Predator106
September 10th, 2008, 06:46 PM
:Request move to Tutorials/Tips section, it's not meant to be here:

Nice tutorial, however.

cyrylski
August 6th, 2009, 06:27 AM
Worked for me, thank you very much :)

However, I had to skip step 3 as I could not locate line 0x1C in the editor for some reason.

jmbneaf
October 7th, 2009, 12:45 PM
Awesome - just what I needed - works great!!

Be sure to read the directions carefully on step #3. I got hung up on this and once I did it correctly - no problems!

~J

petermck
October 25th, 2009, 06:52 PM
Hi,
Great tutorial. I encountered the login/logout loop problem you describe in (6), but used the chntpw code in (1) to fix it.
What I did (before seeing this tutorial) was create a new ntfs partition on a different HDD using Partition Editor in Ubuntu 9.04 and then simply dd it across. I modified the Grub menu.1st file accordingly and booted into windows. Everything worked fine, so back in Ubuntu I used Partition Editor to delete the old windows boot partition (C: in windows), resized and moved the other ntfs patition (D: in windows) to tidy things up and re-booted.
This all worked without a hitch. Windows would boot and present a login screen, but when I logged in it would flash that it was applying user settings and then log off. Safe Mode was the same, but using the code for chntpw in (1) fixed it.
Thanks heaps. I would be interested to know if someone could shed some light on why this happens. It seems a bit weird that windows would boot, but because the volume information in the MountedDevices key in regedit is out of date it won't log in.

oramd
October 25th, 2009, 07:04 PM
ya great tips... it works i applied .. i forgot some steps in starting .but after that that it worked ..thanks for sharing