Quantcast
Channel: Reboot.pro
Viewing all articles
Browse latest Browse all 6812

Create Native Boot VHDs

$
0
0
CREATE NATIVE BOOT BASE AND DIFFERENCING VHDs:

There is strong tendency to move towards virtualized environments that offer greater flexibility and economy of scale. Windows 7 VHDs are especially attractive due to their Native Boot support, almost eliminating performance and/or PC resources hit and "virtual hardware" use instead of real, associated with using VMs. For some ideas on using Base and Differencing VHDs see for example How to use Differencing Disks for your Advantage.

One can create a Fixed and Dynamic (expandable) Base VHD file, attach, initialize, partition and format it in Win 7 Disk Management Console, and create an expandable Differencing VHD (which contains only changes from the Base) with Diskpart. Then backup a system partition or hard drive with Windows Backup or another drive backup package to that VHD, or use Sysinternals Disk2VHD - to make full or incremental Backup depending on VHD type the easy way. There is a more involving manual way as well that also works in legacy Windows versions and equips you with relevant knowledge:

1. Generalize and Backup your Windows 7 or Server 2008 R2 hard drive to WIM image:
Boot to your Win 7 HD, download and install Windows 7 WAIK, or extract ImageX and other tools & images from WAIK remotely by using HTTPdisk. Prepare Win 7 PE with WAIK or WinBuilder, copy ImageX and other tools of your choice onto it. Open as Administrator and run in WAIK Deployment Tools Command Prompt:
sysprep /oobe /generalize /shutdown
If your OS was upgraded from a previous Windows release, you may need to delete Registry key "hkey_local_machinesystemsetup dword: upgrade" to allow sysprep to run. Reboot from Win 7 PE CD or ISO, run from its Command Prompt:
imagex /capture c: c:WIMW2KR2X64.wim "Windows Server 2008R2" /compress fast /verify
Use other Sysprep and ImageX command options if required. You can then create a Base VHD in Win 7 PE or after rebooting to Win 7 HD and opening Command Prompt as Administrator, and apply (restore) your Backup WIM to it, then make the VHD bootable on its own or add its entry to your host's Boot Menu.
2. Create a Base VHD Disk:
diskpart

create vdisk file=C:VHDW2KR2X64.VHD maximum=25600 type=expandable

select vdisk file=C:VHDW2KR2X64.VHD

attach vdisk

create partition primary size=15360

active

format fs=NTFS quick label="WinServer 2008R2 Base VHD"

assign letter=V

exit
This sequence allows to create a fixed or dynamic Base (i.e. parent) VHD file, then attach it as disk, initialize, partition, format, label and mount its volume(s) for easy access. Read more about Initializing disks with Diskpart. VHD can only be initialized as basic disk with basic volumes regardless of its file type, with min fixed empty VHD disk size 3 MB, dynamic and diff. 5 MB, max 2 TB for any disk type (127 Gb for use in Windows VPC). You might need to wait "a few minutes" when creating a large fixed VHD, while its sectors on underlying physical disk are zeroed. If deleted from the disk files data security is not an issue, one can instantly create any size fixed VHD with VHDTool. Dynamic VHDs are created small, and their sectors are zeroed as the disk grows overtime, thus slowing write access. Differencing VHDs are also dynamic with no max set size.
3. Apply Windows 7 Backup WIM to the Base VHD:
imagex /apply C:WIMW2KR2X64.WIM 1 V:
You can also use WIM2VHD tool instead of ImageX to sysprep and convert your system WIM image to VHD instead of sysprepping your main OS volume. Another option is to create and/or attach the VHD from within a Win 7 Setup ISO or DVD Command Prompt, and then install OS to it like to any other hard drive - it will reboot to the VHD to finish install. Or use GImageX (ImageX GUI) to apply Install.wim from the Setup disk to your VHD. One more way: connect a VHD as IDE drive to a virtual machine, and install Win7 to it inside the VM - then use OS migration techniques to enable the VHD to run on a real PC. You can install different OSs on several volumes of the same VHD, but only one partition can be marked active and booted from at a time.
4. Add Boot Environment & Configuration files to the Base VHD:
To boot the VHD in a virtual machine or via a bootmanager like Grub4DOS, you need to add MBR, required boot environment files and the boot menu entry to it (using /store option - see example), unless these files, including Boot folder, were added to the VHD root during Windows install or transfer to it. If you only want to boot the VHD natively from a host Win7 system via its Boot Menu, just add its boot entry to the BCD Store on the host drive. You can copy bootsect.exe suitable for OS type on the VHD or host respectively (32 or 64-bit) from Win 7 PE, Installation ISO or Win 7 WAIK.
bootsect /nt60 v: /force /mbr
Create BCD store on the Base VHD:
cd v:windowssystem32

bcdboot v:windows /s v:
5. Backup BCD and add the Base VHD entry to Host Boot Menu:
Backup Host BCD, copy a default boot entry in its Boot Menu, then insert below the {new_guid} from that BCD entry copy:
bcdedit /export c:bcdbackup

bcdedit /copy {default} /d "Windows Server 2008R2 VHD boot (locate)"

bcdedit /set {new_guid} device vhd=[locate]VHDW2K8R2x64.vhd

bcdedit /set {new_guid} osdevice vhd=[locate]VHDW2K8R2x64.vhd

bcdedit /set {new_guid} detecthal on
You can also run bcdboot v:windows /s c: to add the VHD entry to your host's Boot Menu instead of the above bcdedit commands, if the host and VHD OS versions are the same. Or use the bcdedit commands to add boot entries for each bootable volume on a multi-volume VHD, then make a desired partition active from WinPE or your system HD before booting to it.
6. Add a legacy OS entry to Win7 Boot Menu (needed to run WinXP):
bcdedit /create {ntldr} /d "Chain WinXP Bootloader"

bcdedit /set {ntldr} device partition:E

bcdedit /set {ntldr} path ntldr

bcdedit /set /displayorder {ntldr} -addlast
7. Restore original Win 7 BCD or Legacy Boot Environment (for WinXP VHD bootable with Grub4DOS):
You can easily revert to previous Boot Menu or legacy OS if needed. Win XP Boot.ini remains untouched by the above VHD setup and doesn't need to be restored.
bcdedit /import c:bcdbackup

OR

bootsect /nt52 v: /force /mbr
8. Re-specialize the Base VHD:
To boot a Win7 VHD natively or via Grub4DOS from a USB drive, you might need to enable Windows 7 native USB/VHD boot registry settings in offline OS on the VHD. Run in Command Prompt from a dir with the unpacked cdob's patch:
set_7_usb_boot.cmd v:windows

diskpart

select vdisk file=C:VHDW2KR2X64.VHD

detach vdisk

exit
Reboot to the Base VHD on a selected Win 7 PC to auto install all required device drivers. Clean up and defragment the Base VHD system volume with your favorite tools if required, redirect its Paging file to a physical HD in Windows System Properties - Advanced - Settings to keep size low. Consider installing EasyBCD to it for editing Boot Menu via GUI. If planning to add its Diff VHDs, reboot again to Win 7 HD, attach the Base VHD, deselect Contents Indexing in its Disk Properties and Set the parent disk ReadOnly (valid until detached), since its content can't be changed, defragmented or booted from once Diff VHDs are created to avoid mismatch. You can also set the parent VHD file Read Only in its File Properties, thus making it non-attachable and non-bootable. If you want to boot from the parent VHD later or Merge it with its Diff VHDs, Clear attribute ReadOnly from its file.
diskpart

select vdisk file=C:VHDW2KR2X64.VHD

attach vdisk

attributes disk set readonly

detach vdisk

exit
9. Create a Differencing VHD (if needed):
You can create multiple Diff VHDs to the same Base, and they can be chained. Detach a parent (Base or another Diff) VHD before creating or attaching its Diff (child) VHDs. Don't format or defrag volume(s) on an attached Diff VHD - it may take a long time and increase their size with little performance boost. Diff VHDs by default display Label and Drive Letter of their parent if assigned that you can change, and have auto adjusted size. Diff will transparently use its parent's BCD Store and original volume content, but it saves all changes to disk sectors' content to the Diff's volume. You may want to defrag the Diff and Base VHD files to keep them contiguous, but for native boot VHDs its not mandatory.
diskpart

create vdisk file=C:VHDW2K8R2X64-diff1.VHD parent=C:VHDW2K8R2X64.VHD

attach vdisk

list volume

select volume <volume_number_of_attached_VHD>

assign letter=W

exit

label W: "WinServer 2008R2 Diff1 VHD"
10. Add Differencing VHD and delete Base VHD boot entry from BCD store:
Change dir to the Diff VHD's system root, list all boot entries, copy the Base VHD entry and link its new GUID (device ID) to the Diff VHD, then add it to the host's BCD Store to boot natively from Windows Boot Menu. Or add the entry to the VHD's own BCD Store to boot via Grub4DOS or another bootmanager like VBoot.
cd w:windowssystem32

bcdedit /v

bcdedit /set {current_guid} description "Windows Server 2008R2 Base VHD"

bcdedit /copy {current_guid} /d "Windows Server 2008R2 Diff1 VHD"

bcdedit /delete {current_guid} /cleanup
Insert below the {new_guid} from the above BCD entry copy:
bcdedit /set {new_guid} device vhd=[locate]VHDW2K8R2X64-diff1.VHD

bcdedit /set {new_guid} osdevice vhd=[locate]VHDW2K8R2X64-diff1.VHD

bcdedit /default {new_guid}

bcdedit /set {default} detecthal on

bcdedit /v
Consider creating chained or direct Diff VHDs to the same Base to establish baselines in software testing, deployment, or run specific apps or drivers, and add their entries to your host's Boot Menu. When booting from a Diff VHD, its (chained) parent(s) must be detached and located in original folders on accessible at boot time drives. If moved, its possible to Repair relationship paths.
11. Merge Differencing and Base VHDs (if needed):
When enough updates are accumulated on your Diff VHD, it can be merged with its parent (uncheck Read Only in File Properties) after you uninstall unwanted apps from it and select merge depth - a number of chained Diffs to merge to the Base. Once merged, the Diff VHD can be deleted and replaced with a new empty one, or you can keep using it. You must "wait a few minutes" until Diskpart completes the task and shows the input prompt again - do not close Console before that! If several Diff VHDs exist from the same Base, merge all one by one to the Base and delete, since updating the Base will invalidate its multiple Diffs. Once finished, don't forget to set Base VHD file Read Only. The resulting Base file size increase can be a lot smaller than total size of merged to it Diffs. If you don't want to merge all Diffs to the Base, copy the Base VHD before merging it, then merge it with selected Diffs, rename the updated Base, and restore name of the original Base VHD copy thus leaving unmerged Diffs linked to it.
diskpart

select vdisk file=C:VHDW2K8R2X64-diff1.VHD

detach vdisk

merge vdisk depth=1

exit

Fixed VHD types are preferred for work, while dynamic and differencing - for testing to save time and disk space. If something goes wrong - just delete the Diff VHD and create a new one. At boot a dynamic VHD will try to reserve disk space up to its max set size (and a Diff VHD - to its parent size), so enough (preferably contiguous) disk space should be available on the underlying physical disk volume to avoid BSOD, unless you activate Thin Provisioning. Since a Diff VHD's parent disk must be set ReadOnly, using a Dynamic VHD as a parent instead of a larger Fixed VHD can save disk space. Don't attach a bootable non-generalized VHD on the same system, unless you switched Offline the source OS drive or modified the VHD disk signature and its OS BCD & Mounted Devices registry hives (manually or with a tool like Paragon Adjust OS or IMG_XP), as it may cause VHD signature change, blocking its future boot. Note, hibernation isn't possible when booting from a VHD. If your PC is BitLockered, place the VHDs on a non-BitLockered partition and suspend BitLocker during OS install onto VHD, remember the recovery key.

You just learned how to backup your sysprepped system drive to a Base VHD, then create its Differencing VHD and add its entry to the host's or VHD own Boot Menu, so you can natively boot it on your PC. Finally install to it and start testing new apps without risking to blow your system away. The system on a VHD is running almost as fast (on average 3% performance hit) as on a physical HD - a lot faster than inside any VM. And its running on your native PC hardware, not on fictional VM hardware! Note, all code in this and my other VHD Tutorials is given as "example" ONLY, a user might need to modify based on his system config and feedback. :good:

Viewing all articles
Browse latest Browse all 6812

Trending Articles