I devised the below method/script to create a cloned VM from a running Windows machine. A VHD snapshot is created automatically each evening, which can be converted into a VM via the below process.
Target machine drives & partitions, output from diskpart command:
DISKPART> list volume Volume ### Ltr Label Fs Type Size Status Info ---------- --- ----------- ----- ---------- ------- --------- -------- Volume 0 D DVD-ROM 0 B No Media Volume 1 System Rese NTFS Partition 100 MB Healthy System Volume 2 C NTFS Partition 931 GB Healthy Boot Volume 3 E Backup NTFS Partition 298 GB Healthy
The E:\ drive is not backed up in the below procedure (space restrictions), this does make the process slightly more complex as we can’t use the “*” option to include all volumes with the disk2vhd.exe utility. We need to attach the above ‘Volume 1’ partition via the diskpart command prior to running the disk2vhd.exe command. This is done via the diskpart command taking the below diskpart_start.txt file as its input when mapping the M:\ drive.
$ cat diskpart_start.txt sel disk 0 sel part 1 assign letter=m noerr
The script produces a .VHD file at the below location, the ‘WindowsImageBackup’ folder will have the latest copy:
Old ‘WindowsImageBackup’ folders are date stamped and rotated weekly.
E: cd E:\SystemBackupsPleaseLeave REM Delete backups older than 7 days, E:\ drive too small to accommodate more. forfiles -p E:\SystemBackupsPleaseLeave\ -m WindowsImageBackup* -d -7 -c "cmd /c rmdir /s /q @path" REM Rename old E:\SystemBackupsPleaseLeave\WindowsImageBackup folder if it exists rename WindowsImageBackup "WindowsImageBackup%date:/=%-%time::=%RENAMED" REM Create E:\SystemBackupsPleaseLeave\WindowsImageBackup for each days backup mkdir E:\SystemBackupsPleaseLeave\WindowsImageBackup REM Map partition that doesn't have a drive letter (M) diskpart /s C:\cygwin\usr\local\bin\diskpart_start.txt REM Pause for 10 seconds ping -n 10 -w 1000 127.0.0.1 > nul REM start.... C:\cygwin\usr\local\bin\disk2vhd.exe -accepteula M: C: E:\SystemBackupsPleaseLeave\WindowsImageBackup\snapshot.vhd REM Pause for 30 seconds ping -n 10 -w 3000 127.0.0.1 > nul REM Unmap the partition that doesn't have a drive letter (M) diskpart /s C:\cygwin\usr\local\bin\diskpart_end.txt
Converting .VHD to VM
I reverted to the below method to work around a dated version of qemu-img on my Debian host, which wouldn’t allow me to convert the .VHD file to a RAW file. I did explore using VBoxManage on my workstation but it too didn’t manage to complete the conversion.
Install cygwin sshd onto another Windows 7 machine, start sshd. This machine will be used to mount the .VHD file and to make it available for copying:
Mount the .VHD file as a read only drive via the disk management utility.
The drive will now be accessible under the cygwin /dev directory. For example /dev/sdb.
From your linux VM host run the below command to copy the above drive (all partitions) as a sparse file. In the below command the Windows 7 machine with the mounted VHD has an IP of 192.168.1.12
ssh email@example.com 'dd if=/dev/sdb | gzip ' | gunzip | cp --sparse=always /proc/self/fd/0 /data/vm/clone/windows_machine_sparse_file
Attach the /data/vm/clone/windows_machine_sparse file to a VM on your Linux host, remember to also attach the Windows DVD.
The sequence/procedure from here down will vary slightly, I guess, if the original Windows workstation is not Windows 7.
Launching this VM (view using VNC) will result in the below error:
After selecting the DVD the boot process will commence:
Select repair your computer.
Choose ‘Command prompt’ on the below screen
Now use the DISKPART utility to select the machine’s C:\ drive, using the below commands:
source: http://www.sevenforums.com/installation-setup/88872-startup-repair-menu-doesnt-see-operating-system.html , see gregrocker post:
DISKPART LIST DISK SELECT DISK 0 (confirm from list this is Windows 7 HD #) LIST PARTITION SELECT PARTITION # (replace # with Windows 7 part #) ACTIVE EXIT
I’m not 100% sure if the below steps are necessary, nothing lost doing them, enter the below commands into the same DOS prompt.
bootrec.exe /fixmbr bootrec.exe /fixboot bootrec.exe /RebuildBcd
With the drive mounted, it should be accessible via the same DOS prompt:
Changing the Registry
The below steps will alter the registry of the machine to allow it to boot as a VM.
Run the below commands in the same DOS prompt to load the registry from the D:\ drive, this might differ for you.
reg load HKLM\VHDSYS D:\windows\system32\config\system regedit
I’m not 100% sure if both CurrentControlSet0001 & CurrentControlSet0002 need to be changed, won’t hurt.
Change the value of the “Start” parameter under the below keys. Only values with stars need changing on a KVM Linux virtual machine, check others.
Aliide = 3 Amdide = 3 Atapi = 0 Cmdide = 3 iaStorV = 3 *intelide = 0 *msahci = 3 *pciide = 3 viaide = 3
Close regedit once you have finished the above edits.
Run the below command to unload the mounted registry from memory.
reg unload HKLM\VHDSYS
Reboot the machine after these changes, using the ‘Restart’ button:
Choose the DVD as the boot device again:
Choose the ‘Repair and restart’ option.
Choose the DVD as the boot device again:
Now attempt to boot from the hard drive: