Jump to content











Photo

Boot Win 7 VHD on Bare Metal PC from Empty Drive

vhd bare metal boot win 7 grub4dos

  • Please log in to reply
321 replies to this topic

Poll: Are you interested in booting Windows from VHD? (75 member(s) have cast votes)

Would you try to install OS to and boot from a single portable VHD file (virtual disk) instead of hard drive?

  1. Yes (72 votes [96.00%])

    Percentage of vote: 96.00%

  2. No (3 votes [4.00%])

    Percentage of vote: 4.00%

Would you be interested to copy that VHD file to an empty USB Thumb or HD and boot from it on real PC?

  1. Yes (72 votes [98.63%])

    Percentage of vote: 98.63%

  2. No (1 votes [1.37%])

    Percentage of vote: 1.37%

Did you try to boot OS from VHD on real PC instead of Virtual Machine?

  1. Yes, I usually boot VHDs saved on an internal hard drive (32 votes [38.55%])

    Percentage of vote: 38.55%

  2. Yes, I usually boot VHDs saved on a USB drive or thumb (10 votes [12.05%])

    Percentage of vote: 12.05%

  3. Yes, I boot VHDs saved on drives of any type (10 votes [12.05%])

    Percentage of vote: 12.05%

  4. Not yet (31 votes [37.35%])

    Percentage of vote: 37.35%

Vote Guests cannot vote

#1 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 13 November 2011 - 01:03 PM

*
POPULAR

BOOT WIN 7 VHD ON BARE METAL PC FROM EMPTY DRIVE

Have you just assembled a new clean PC without OS installed, or bought an external HDD, and want to quickly migrate your trusted system volume from an old PC with all installed apps to it, or test drive various system setups on it from VHDs delivering OS with all apps in a single compact easy to copy to a new drive or delete file? No problem... Backup the system volume you want to test onto a Base VHD as per Create Native Boot VHDs Tutorial (steps 1 - 7) or prepare your VHD by other mentioned in that Tutorial thread tools, then copy it to the new PC and make it bootable. How?

Boot from Win 7 PE or Installation DVD or ISO saved on a USB Thumb on a new Bare Metal PC. Open by Shift+F10 and Run from its Command Prompt:
1. Partition and Format the new PC's hard drive:
diskpart

list disk

select disk <disk_number_to_format>

clean

create partition primary

format quick fs=ntfs label="VHD Test Drive"

active

assign letter=c

exit
You can format and make bootable an internal or external HD, or another USB Thumb drive (with Diskpart or special tools like RMPrepUSB or Boot_USB).
2. Copy your sysprepped Win7 Base VHD from the source Thumb or Network drive to the new partition:
net use n: your_servershare_folder

md c:VHD

copy n:VHDwindows7.vhd c:VHD

3. Attach and Mount the VHD on the new PC:
diskpart

select vdisk file=C:VHDwindows7.vhd

attach vdisk

list volume

select volume <volume_number_of_attached_VHD>

assign letter=v

exit
4. Copy boot environment and configuration files from the VHD to the new partition:
cd v:windowssystem32

bootsect /nt60 c: /force /mbr

bcdboot v:windows /s c:

bcdedit /store c:bootbcd /copy {default} /d "Windows7 VHD boot (locate)"
Insert below the {new_guid} from the above BCD entry copy:
bcdedit /store c:bootbcd /set {new_guid} device vhd=[locate]windows7.vhd

bcdedit /store c:bootbcd /set {new_guid} osdevice vhd=[locate]windows7.vhd

bcdedit /store c:bootbcd /default {new_guid}

bcdedit /store c:bootbcd /set {new_guid} detecthal on
In most cases its sufficient to run only bootsect and bcdboot commands above, then reboot from the drive.
5. Re-specialize the Base VHD:
To boot a Win7 VHD from an empty USB drive, you might need to enable Windows 7 native USB/VHD boot registry settings by running the unpacked cdob's patch on Registry of the offline system volume saved on the attached VHD:
set_7_usb_boot.cmd v:windows

diskpart

select vdisk file=C:VHDwindows7.vhd

detach vdisk

exit
Then restart your system, select the drive with your saved VHD as fisrt to boot in BIOS if not yet selected, and reboot to the Bare Metal PC - it will boot straight to your Base VHD and auto install required drivers. Follow Create Native Boot VHDs Tutorial (step 8), if you want to add later Differencing VHDs.
6. Add a Differencing VHD to the new PC (if needed):
You may then add Diff VHDs as per Create Native Boot VHDs Tutorial (steps 9 - 10) on the new PC. Don't forget to delete a Base VHD's entry from the host's Boot Menu after adding its Diff VHDs entries to it. You must Detach the Base VHD disk and make its file Read-only before creating its Diff VHDs, and keep them in the same original folder.

After rebooting from Win7 PE or another Win 7 & Server 2008R2 PC on LAN, you can copy more Base VHDs to the new PC, and add their entries to the host's Boot Menu by copying an existing entry in a VHD's BCD Store to the host's BCD Store and editing it: replacing in bcdedit commands a disk identifier number {current_guid} with {new_guid} generated for the copy, and setting the new VHD as an additional boot device. Of course, only one VHD boot device can be set default.

You just learned, how to natively boot a Win7 VHD from an empty drive without any OS installed, even when its the only drive in your new PC. 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. :clap:
  • Nuno Brito, metal03326, Uvais and 1 other like this

#2 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,865 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 November 2011 - 01:20 PM

select disk 0


Are you sure that the internal disk will be disk 0 when you boot from an USB stick? :dubbio:

:cheers:
Wonko

#3 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 13 November 2011 - 01:32 PM

All code in this and my other VHD Tutorials is given as "example" ONLY, a user is expected to modify based on his system config and feedback (results of List Disk command in this step). :)

#4 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,865 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 November 2011 - 01:58 PM

All code in this and my other VHD Tutorials is given as "example" ONLY, a user is expected to modify based on his system feedback (results of List Disk command in this step). :)

Yep, only thought that it was good to highlight is as anyone at first attempt is likely to re-partition the USB stick. :ph34r:
Expecially because on most machines the USB stick will be disk 0.


Maybe something like:

diskpart

list disk

select disk0

detail disk

select disk 1

detail disk


from the result of the above you should be able to understand which device has which disk number, normally if you boot from a USB stick or USB connected hard disk, the boot order would be:
disk 0 <-USB device
disk 1 <- first internal hard disk
But on some machines/BIOS the result might be:
disk 0 <- internal hard disk
disk 1 <- USB connected device
Select the disk corresponding to the INTERNAL hard disk, in this example disk 0 in :
select disk 0

then go on:

clean

create partition primary

format quick fs=ntfs

active

assign letter=c

exit


Might be more "safe".

:cheers:
Wonko

#5 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 13 November 2011 - 02:12 PM

At the moment the described method of booting natively a VHD from an initially empty drive selected as first to boot in PC BIOS works well, when placing a Win7 VHD onto an internal IDE or SATA, or external eSATA drive. It was not yet tested or proven to boot from a LAN drive, and setting VHD to boot from an empty USB drive requires further clarification of what changes (if any) must be made to a system installed onto VHD to make it boot natively from an USB Thumb.

#6 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,865 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 November 2011 - 02:30 PM

Well, if you want to boot from it the USB Thumb cannot be really "empty".
I presume there is some kind of misunderstanding.
The internal disk might be empty but the whatever you boot from cannot be empty, this extends to the the title:

How to Boot Win 7 VHD on Bare Metal PC from Empty Drive

:unsure:
that IMHO is not such easily understandable.


In my previous post I was referring to:

Boot from Win 7 PE or Installation DVD or ISO saved on a USB Thumb on a new Bare Metal PC. Open by Shift+F10 and Run from its Command Prompt:

In such a case first disk will likely become the USB thumb, or more generally if you set the boot priority in BIOS to boot from a hard disk like device (IDE/ATA/SATA internal, USB, firewire or e-sata), it is likely that it will become disk 0.


:cheers:
Wonko

#7 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 13 November 2011 - 02:56 PM

The disk is originally empty, until Win7 BCD Store is added to it when preparing to boot a Win 7 VHD natively, as described above. What's important, its not required to install any OS or a 3rd party bootloader to an empty disk selected as first to boot in BIOS (and possibly the only disk in that PC), to natively boot a Win7 VHD from it. As mentioned above, USB thumb support for that particular task requires further investigation. :)

#8 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,865 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 November 2011 - 08:23 PM

Maybe this is useful to expand on the topic:
http://technet.micro...338(WS.10).aspx

:cheers:
Wonko

#9 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 15 November 2011 - 01:57 AM

Tested also on USB HDD and works well. To boot Win7 VHD natively or via Grub4DOS from an empty USB drive with Win7 boot environment added to it, you might need to enable Windows 7 native USB/VHD boot registry settings. :)

Grub4DOS can also be added to the drive as another boot entry. The way it worked for me - by adding NeoGrub entry in EasyBCD installed to Win7 on VHD.

Updated the Tutorial.

#10 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 15 November 2011 - 11:27 AM

Nice tute, am 3/4 of the way through, used Paragon to make a VHD of a live sysprepped Win7 x64 install, so we'll see how it goes.

It occurs to me, that this procedure - at least the very first version - could be make fairly automatic, if someone (me, I guess) were to write a small application that ran under WinPE.

I gather it's not "legal" to distribute a modified copy of WinPE? A pity, as one could just release a pre-build WinPE with everything on board. (I run my own from PXE, works quite well).

That said, it also shouldn't be hard to write an application to make a WinPE... it all depends on whether the application can get hold of those few tools it needs, without having to grab the whole AIK ISO.

What the world needs is more of this:

http://live.sysinter...om/disk2vhd.exe

live.sysinternals.com rocks my world.

#11 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 15 November 2011 - 12:11 PM

Would be really nice to see this procedure automated. I'd suggest you to read Create Native Boot VHDs thread to the end before you start coding, as there are more tools mentioned in it, and some need to be integrated for better user experience.

#12 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 17 November 2011 - 04:50 PM

Have read that particular document many many times, I think I found myself with 5 copies open while I was following *this* guide.

Speak of which, I finished up my USB bootable, as per this guide, it's a sysprepped Crysis II running off an OEM Win7 Ultimate x64. Works a treat. Boots to a playable Crysis II on any ATI or VMWare environment. Good to see those registry hacks survived the post sysprep "unboxing".

So I'm really keen to get differential VHDs working, since I can only run my USB drive one before the base VHD is "out of the box". Given the base VHD is 20gb, and windows makes maybe 350mb of changes to prep for a new machine, there's some fairly obvious gains to be had.

Per your list of programs, I offer the following comments .. and a query as to whether you've played with
ccboot.com iSCSI imaging stuff?



Disk Management Service
aka diskmgmt.msc I find it's a quick way to attach and detach VHDs, not much else.

Diskpart
Totally rocks, runs in WinPE, and lets you get real lazy, eg "sel part 1".

Windows Backup
Good for real backups, but I prefer a cool-boot to Paragon/Acronis. Cleaner images.

bcd{edit,boot,sect}
where would we be without these three. although bcdedit kills me with it's
{guid} stuff, so I have taken to using "visual bcd editor" when I can, it's
one of the few that can work with VHD files and not screw the pooch.

WIM2VHD
i love dism, and the ease you can add drivers to .wims, but when you start
mixing them up with vhds, my mind just loses the plot and refuses to
process. :P

Hyper-V Mgr
I've just installed the free Virtual PC 6.1/Win7 app, don't have any
experience with Microsoft virtualization products. I'll be trying to lay my
hands on this next.

GImageX
Is imagex really that hard to use? seriously...

DISK2VHD
http://live.sysinter...om/disk2vhd.exe - always there when you need it :)

CloneDisk
never worked right for me, i prefer acronis true image for cloning, or "dd".

Vmdk2VHD
http://www.starwinds...e.com/converter - it's free, and it works good and
if you use pre-allocated flat VMDKs they "dd" directly onto real disks.

Raw2VHD
I guess those raw disks would go into Raw2Vhd, I'll look into that.

VHD Resizer, VHD Tool
Haven't had much of chance to look into VHD tools yet, but I found this gem:

http://downloads.sou...af.exe?download
It's a portable app that can be set to zero out your slack space and other
junk. I use it because VHD Compactor required way too much effort.

IMG_XP
A big and confusing collection of programs, but useful to have them all in
one place and ready for when a guide calls for a script. although i prefer
to write my own scripts, or at least be sure i thoroughly understand what a
script is doing.

Acronis Backup & Recovery Advanced Virtual Server or Workstation
Paragon Hard Disk Manager for Virtual Server
Paragon Adjust OS

Yeah, these rock. I actually paid for Acronis TI and DD. Paragon is
seriously ahead of the game right now though, with all that cold cloning to
virtual and adjusting windows install stuff.

VHD Loader
is that seriously a program?

VBoot
downloaded the trials, just to find they ended the next day (15th november).
am quite interested in pulling the bits apart to see what they've done
though.

Grub4DOS
I'm so far into SYSLINUX for my PXE environment that it hurts me to use
grub. But nothing can map drives like grub4dos, and the work on firara and
winvdisk is awesome.

RMPrepUSB, USB_BOOT
they do what they do, i think i used one or maybe even both on my usb boot
setup they definately work. but you'd want to be doing this kind of basic
stuff from your own program, and getting windows apis to do the work for
you.

Edited by sfinktah, 17 November 2011 - 05:00 PM.


#13 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 17 November 2011 - 08:53 PM

Interesting comments. I didn't play with CCBoot package, since it requires iSCSI wide broadband network to work efficiently, but PXE Boot in general is a popular diskless network boot direction that has a separate dedicated section on this forum. :)

Using Visual BCD Editor is a good suggestion, though a bit complex for a novice. I'll add it to VHD Tools List. :thumbsup:

#14 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,865 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 18 November 2011 - 02:55 PM

JFYI, a few BCD editors were talked about here:
http://reboot.pro/7476/
I personally like jianjulin little thingy BCDTOOL:
http://www.boot-land...showtopic=10003
(BTW cannot say if now there is a 64 bit version of it, the 32 bit version works allright here)


:cheers:
Wonko

#15 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 18 November 2011 - 02:59 PM

Visual BCD Editor is an absolute head-frack for doing VHD. Not at all the fault of the utility, but due to the really really warped way Windows works with VHD.

I had to read and experiment with this for about an hour until I was satisfied I knew what was going on. This may be related to some of your tutorials, in which you seem to attach the vdisk for no reason. So I won't bother to summarize what it says, since I think you already know what the deal is.

A VHD loader is a normal Windows 7 loader with ApplicationDevice and OSDevice elements of type "LocateExDevice".
For the device element specify: Type=LocateExDevice, Drive=D: and Path=VHDw7_32.vhd for example.

In Windows7boot blog you can find the screenshots and a few explanations on BCD entries created by standard Windows 7 installation to VHD.
If you attach the VHD for which you want to create a loader entry and then create the loader entry the "Application device" and "OS Device" elements must be of type partition. The mapping is done by Windows. Later if you detach the VHD you will see that the device elements are displayed as type LocateExDevice !

Device elements are true objects not just strings or numbers as are most other elements in BCD store. This is also the reason that there are only two possible ways of editing the BCD - with bcdedit or over the WMI BCD provider interface. As far as I know all other GUI tools which can edit some parts of the BCD are achieving this by scripting bcdedit.(stand mid 2011).


Posted Image

#16 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 18 November 2011 - 03:15 PM

My goal was to boot Windows 7 without modifying my hard-drive. PXE, iSCSI, USB, ... the technology wasn't really the point. But having a good PXE setup is just so rewarding. Just press F12 during boot and you have a list of every tool you can load off an ISO or GRUB4LINUX distro.

The reason I mention CCBoot is not necessarily the iSCSI part (which is obviously the entire point of the product), but the patches and imaging they do to make it work.

I tried it on XP, put their host in a VM, and ran the client on another VM. Installed the client util, ran it in XP, and then booted up on another PC with an identical image. Sure - some of that magic comes from the MS iSCSI initiator... but there are some neat tricks and a few "hacks" going on as well, and that piqued my curiosity.

It takes the "Bare Metal Windows 7" to a new level. Not necessarily a better level for most people, but an interesting one. Having a Windows XP you can boot up from the network to do modifications to your setup is pretty neat as well. That (at least) can be done relatively easy using M$ tools and instructions.


In an unrelated question, have you tested differential VHDs with Windows 7 bare metal installs? Because I just can't get my diff VHDs to boot.

#17 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 18 November 2011 - 03:27 PM

Here's some more links for you:

In this Video Series we will dive deep into a the new virtualization feature built into Windows Server 2008 R2 and Windows 7. It is called Native Boot To VHD. This is the one virtualization platform that allows virtualization inside of the virtualized machine. Yes, you can run Hyper-V or Virtual PC from inside a Boot to VHD machine. Running Boot to VHD gives you the capability to start playing with Windows 7 or Windows Server 2008 R2 right away. Just dual boot your machine. It is very clean, as the entire OS is in a vhd file. Having the OS fully encapsulated in a VHD means no changes are needed to partitions, and there is no confusion as to what files are for what OS.and the not necessarily useful: If you make a mistake while “playing” with BCDEdit see Server 2008 R2 or Windows 7 System Will NOT Boot After Making Changes To Boot Manager Using BCDEdit.

I just noticed them while looking for a good shell script to automate the differential VHD process, so I can ensure a repeatable procedure I can debug.

#18 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 18 November 2011 - 03:43 PM

This may be related to some of your tutorials, in which you seem to attach the vdisk for no reason. So I won't bother to summarize what it says, since I think you already know what the deal is.

I always keep trying to improve Tutorials, so could you be more specific. As to Visual BCD Editor, it fixed an offline BCD Store for me, so it sounds like a comprehensive tool that needs some practice.

Pls read the Dif VHD part of Create Native Boot VHDs Tut again. It also says a lot about coordination and placement of Base and Dif VHDs. Win7 should be installed to the Base, and an app you want to test - to the Dif. Both VHDs must kept be in the same folder.

#19 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 18 November 2011 - 05:18 PM

Pls read the Dif VHD part of Create Native Boot VHDs Tut again. It also says a lot about coordination and placement of Base and Dif VHDs. Win7 should be installed to the Base, and an app you want to test - to the Dif. Both VHDs must kept be in the same folder.


Read it? I'm presently coding it :P Unfortunately, I can't literally follow your instructions, since they presume that base.vhd contains an installed working operating system. Mine is a sysprepped Win7, and I want to use the diff to preserve it between uses.

* sfinktah is dreading the <code> mode of this board...

set VhdDrive=%Available%

ECHO Will assign VHD to drive %VhdDrive%

ECHO.

set DiffVhd=diff.vhd

set ParentVhd=base.vhd

>> %TMP% ECHO  create vdisk file=%DiffVhd% parent=%ParentVhd%

>> %TMP% ECHO  attach vdisk

>> %TMP% ECHO  list volume

>> %TMP% ECHO  assign letter=%VhdDrive%

>> %TMP% ECHO  exit



ECHO This is our DISKPART script:

ECHO.

cat %TMP%

ECHO.

diskpart /s %TMP%


@wonko - 32 or 64 bit isn't of any relevance, only windows 7 and vhd support. i'm not saying i like visual bcd edit, i'm not even saying it's easy to use. but it does fully support win 7 & vhd, and i don't think many others can make that claim.

Posted Image

#20 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 18 November 2011 - 05:55 PM

OS on a Base VHD is online after booting the Diff, but all differences are kept in the Diff, hence don't see how your task is different. If you can code the process with all user options and variables into a GUI, it would be VERY nice contribution. Just make sure you update the Tut page periodically, since I make changes at times (listening to you :)).

Also try using VHD Resizer - quite often needed tool.

#21 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 18 November 2011 - 06:04 PM

I always keep trying to improve Tutorials, so could you be more specific.


I'm not sure it affects bcdedit or not... but in Visual BCD Edit (and this is a reflection of the WMI BCD provider interface, nothing to do with the program itself), you attach a mounted VHD as a Partition, and later - if that partition is unmounted - windows shows it as [C:]... or [locate].... In the case of BCDEDIT, I think it always shows it that way, but it's not the way windows is storing it (at least that is my impression).

It have a suspicion that if you dismounted the VHD before using BCDEDIT, then everything would fail.

Observe these two screen captures I just made, the first is a VHD entry I just created, to the second partition of an attached VHD (H:). (the first partition is a Recovery partition).

Posted Image

Note, that I simply tell it that I want the device to be H:

However, after I detach the VHD, and reload:

Posted Image

... windows (which must be holding some kind of pointer or link to the VHD) now says the device is vhd=[E:]VHDscrysis0.vhd. Or to quote the author, "If [..] you create a loader entry [..] the "Application device" and "OS Device" elements must be of type partition. The mapping is done by Windows. Later if you detach the VHD you will see that the device elements are displayed as type LocateExDevice"



When I first tried to create a differencing VHD, I tried to just edit the filename in bcdedit, with out bothering to do anything else. Obviously that failed, but I gather that is the reason.

I could be wrong. I'm almost defiantly irrelevant.

#22 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 18 November 2011 - 07:07 PM

Always the bridge groom, never the bride....

Will post script shortly. (n.b. it's not really Windows 8, I just had too many Vm's called Windows 7)

Posted Image

#23 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 18 November 2011 - 08:01 PM

Posted Image

@ECHO off



if "%1" == "/h" (

goto :Usage

)

if "%1" == "-h" (

goto :Usage

)

if "%1" == "--help" (

goto :Usage

)

if "%1" == "/?" (

goto :Usage

)

if "%1" == "-?" (

goto :Usage

)

if "%2" == "" (

goto :Usage

)

if "%3" == "" (

goto :Usage

)

set BcdStore=%1

set ParentVhd=%2

set DiffVhd=%3

IF NOT EXIST %BcdStore% (

	  echo Error: %BcdStore% does not exist

	  goto End

	 )

IF NOT EXIST %ParentVhd% (

	  echo Error: %ParentVhd% does not exist

	  goto End

	 )

IF	 EXIST %DiffVhd% (

	  echo Error: %DiffVhd% already exists

	  echo Try: del "%DiffVhd%"

	  goto End

	 )





set TMP="%TEMP%.diskpart.script"

del %TMP% > null 2>&1



:: Allow delayed variable expansion

SETLOCAL ENABLEDELAYEDEXPANSION

SET Available=

SET Drives=

:: The main command to list all drive letters in use is FSUTIL FSINFO DRIVES.

:: MORE /E /T0 removes the vertical tabs/single linefeeds from FSUTIL's output,

:: which is necessary to allow parsing with FOR /F.

FOR /F "tokens=1,2 delims= " %%A IN ('FSUTIL FSINFO DRIVES ^| MORE /E /T0') DO (

IF "%%B"=="" (

  SET Drives=!Drives! %%A

) ELSE (

  SET Drives=%%B

)

)

:: Display the formated result

ECHO Finding an free drive letter to assign to VHD...

:: Check which drive letters are not in use

FOR %%A IN (Q R S T U V W X Y Z) DO (

ECHO.%Drives% | %SystemRoot%system32FIND "%%A:" >NUL 2>&1

IF ERRORLEVEL 1 (

  IF "!Available!"=="" (

   SET Available=%%A:

   GOTO FOUND

  ) ELSE (

   SET Available=!Available! %%A:

  )

)

)

:FOUND

:: "Export" the 2 variables and terminate

ENDLOCAL & SET Drives=%Drives%& SET Available=%Available%

set VhdDrive=%Available%

ECHO Will assign VHD to drive %VhdDrive%

ECHO.



>> %TMP% ECHO create vdisk file=%DiffVhd% parent=%ParentVhd%

>> %TMP% ECHO select vdisk file=%DiffVhd%

>> %TMP% ECHO attach vdisk

>> %TMP% ECHO list disk

>> %TMP% ECHO select part 1

>> %TMP% ECHO list part

>> %TMP% ECHO list vol

>> %TMP% ECHO assign letter=%VhdDrive%

>> %TMP% ECHO exit



ECHO This is our DISKPART script:

ECHO.

type %TMP%

ECHO.

diskpart /s %TMP%

ECHO.

set VHDDESC=differenced VHD

set VHDFileName=%DiffVhd%

set BCDEDIT=BCDEDIT -store %BcdStore%

ECHO VHDFileName=%VHDFileName%

ECHO Creating VHD boot entry for %VHDFileName%...

ECHO.



ECHO %BCDEDIT% /copy {default} /d "%VHDDESC%"

FOR /F "tokens=7 delims=. " %%A in ('%BCDEDIT% /copy {default} /d "%VHDDESC%"') do set GUID=%%A

ECHO Got GUID: %GUID%



set BCDFName=%VHDFileName:~2%



ECHO %BCDEDIT% /set %GUID% device vhd=[locate]%BCDFName%

	 %BCDEDIT% /set %GUID% device vhd=[locate]%BCDFName%

ECHO %BCDEDIT% /set %GUID% osdevice vhd=[locate]%BCDFName%

	 %BCDEDIT% /set %GUID% osdevice vhd=[locate]%BCDFName%

ECHO %BCDEDIT% /set %GUID% detecthal on

	 %BCDEDIT% /set %GUID% detecthal on

ECHO.

Press ENTER or type command to continue

ECHO Detaching %DiffVhd%...

>  %TMP% ECHO select vdisk file=%DiffVhd%

>> %TMP% ECHO detach vdisk

ECHO.

type %TMP%

ECHO.

diskpart /s %TMP%



goto :Exit



:Usage

ECHO.

ECHO NOTE: 

ECHO  You must run the command from an elevated command prompt.

ECHO.

ECHO SYNTAX:

ECHO. %0 ^<BCD store^> ^<Base VHD^> ^<Diff VHD to create^>

ECHO.

ECHO EXAMPLE:

ECHO. %0 c:bootbcd c:vhdbase.vhd c:vhdnew_diff.vhs

ECHO.

ECHO Written by sfinktah for sambul61, feedback to http://reboot.pro/15815/

ECHO Some code written by Dan Stolts. No warranties, Use at your own risk.

ECHO.

:Exit

ECHO.

ECHO written by sfinktah for sambul61, feedback to http://reboot.pro/15815/

ECHO.

ECHO.

:End

mbp:Documents mini$ !

You have new mail in /var/mail/mini

mbp:Documents mini$ !vi

vi /Volumes/mini-1/tmp/abc.bat

CSApprox needs gui support - not loading.

  See :help |csapprox-+gui| for possible workarounds.

Press ENTER or type command to continue

mbp:Documents mini$ cat /tmp/1 | egrep -v '^REM'

@ECHO off



if "%1" == "/h" (

   goto :Usage

)

if "%1" == "-h" (

   goto :Usage

)

if "%1" == "--help" (

   goto :Usage

)

if "%1" == "/?" (

   goto :Usage

)

if "%1" == "-?" (

   goto :Usage

)

if "%2" == "" (

   goto :Usage

)

if "%3" == "" (

   goto :Usage

)

set BcdStore=%1

set ParentVhd=%2

set DiffVhd=%3

IF NOT EXIST   %BcdStore% (

				  echo Error: %BcdStore% does not exist

				  goto End

			   )

IF NOT EXIST   %ParentVhd% (

				  echo Error: %ParentVhd% does not exist

				  goto End

			   )

IF	 EXIST   %DiffVhd% (

				  echo Error: %DiffVhd% already exists

				  echo Try: del "%DiffVhd%"

				  goto End

			   )





set TMP="%TEMP%.diskpart.script"

del %TMP% > null 2>&1



:: Allow delayed variable expansion

SETLOCAL ENABLEDELAYEDEXPANSION

SET Available=

SET Drives=

:: The main command to list all drive letters in use is FSUTIL FSINFO DRIVES.

:: MORE /E /T0 removes the vertical tabs/single linefeeds from FSUTIL's output,

:: which is necessary to allow parsing with FOR /F.

FOR /F "tokens=1,2 delims= " %%A IN ('FSUTIL FSINFO DRIVES ^| MORE /E /T0') DO (

   IF "%%B"=="" (

	  SET Drives=!Drives! %%A

   ) ELSE (

	  SET Drives=%%B

   )

)

:: Display the formated result

ECHO Finding an free drive letter to assign to VHD...

:: Check which drive letters are not in use

FOR %%A IN (Q R S T U V W X Y Z) DO (

   ECHO.%Drives% | %SystemRoot%system32FIND "%%A:" >NUL 2>&1

   IF ERRORLEVEL 1 (

	  IF "!Available!"=="" (

		 SET Available=%%A:

		 GOTO FOUND

	  ) ELSE (

		 SET Available=!Available! %%A:

	  )

   )

)

:FOUND

:: "Export" the 2 variables and terminate

ENDLOCAL & SET Drives=%Drives%& SET Available=%Available%

set VhdDrive=%Available%

ECHO Will assign VHD to drive %VhdDrive%

ECHO.



>> %TMP% ECHO  create vdisk file=%DiffVhd% parent=%ParentVhd%

>> %TMP% ECHO  select vdisk file=%DiffVhd%

>> %TMP% ECHO  attach vdisk

>> %TMP% ECHO  list disk

>> %TMP% ECHO  select part 1

>> %TMP% ECHO  list part

>> %TMP% ECHO  list vol

>> %TMP% ECHO  assign letter=%VhdDrive%

>> %TMP% ECHO  exit



ECHO This is our DISKPART script:

ECHO.

type %TMP%

ECHO.

diskpart /s %TMP%

ECHO.

set VHDDESC=differenced VHD

set VHDFileName=%DiffVhd%

set BCDEDIT=BCDEDIT -store %BcdStore%

ECHO VHDFileName=%VHDFileName%

ECHO Creating VHD boot entry for %VHDFileName%...

ECHO.



ECHO %BCDEDIT% /copy {default} /d "%VHDDESC%"

FOR /F "tokens=7 delims=. " %%A in ('%BCDEDIT% /copy {default} /d "%VHDDESC%"') do set GUID=%%A

ECHO Got GUID: %GUID%



set BCDFName=%VHDFileName:~2%



ECHO %BCDEDIT% /set %GUID% device vhd=[locate]%BCDFName%

	 %BCDEDIT% /set %GUID% device vhd=[locate]%BCDFName%

ECHO %BCDEDIT% /set %GUID% osdevice vhd=[locate]%BCDFName%

	 %BCDEDIT% /set %GUID% osdevice vhd=[locate]%BCDFName%

ECHO %BCDEDIT% /set %GUID% detecthal on

	 %BCDEDIT% /set %GUID% detecthal on

ECHO.

ECHO Detaching %DiffVhd%...

>  %TMP% ECHO  select vdisk file=%DiffVhd%

>> %TMP% ECHO  detach vdisk

ECHO.

type %TMP%

ECHO.

diskpart /s %TMP%



goto :Exit



:Usage

ECHO.

ECHO NOTE: 

ECHO  You must run the command from an elevated command prompt.

ECHO.

ECHO SYNTAX:

ECHO. %0 ^<BCD store^> ^<Base VHD^> ^<Diff VHD to create^>

ECHO.

ECHO EXAMPLE:

ECHO. %0 c:bootbcd c:vhdbase.vhd c:vhdnew_diff.vhs

ECHO.

ECHO Written by sfinktah for sambul61, feedback to http://reboot.pro/15815/

ECHO Some code written by Dan Stolts. No warranties, Use at your own risk.

ECHO.

:Exit

ECHO.

ECHO written by sfinktah for sambul61, feedback to http://reboot.pro/15815/

ECHO.

ECHO.

:End


Edited by sfinktah, 18 November 2011 - 08:31 PM.


#24 sambul61

sambul61

    Gold Member

  • Advanced user
  • 1,568 posts
  •  
    American Samoa

Posted 18 November 2011 - 08:50 PM

Can you try to hide lengthy code behind BBC-Spoiler (left of Font dropdown). :)

Will test this - how about GUI? Ideally, it would incorporate Resizing, Conversion, Compression and similar VHD manipulation features by integrating some available code & tools.

#25 sfinktah

sfinktah

    Frequent Member

  • Advanced user
  • 217 posts
  • Location:Der Äther
  • Interests:/(C(++|#)|P(HP|XE)|(OS|Linu)X|8051)/
  •  
    Australia

Posted 18 November 2011 - 10:49 PM

I will check out that spoiler thing, never used it, but have seen the idea. Definately not suggesting that a batch file is the desired product... but for me just to be able to repeatedly try something, and know that it will be the same every time is just huge. vmware restore snapshot, change a line, and try again. (or in this case, just delete the created entries in visual bcd)

i think my problem might actually be that i'm not deleting the original vhd. i recall you or some-one mentioning that duplicates of identical disks can cause issues. and a diff would probably qualify as that. but i'm not really in favour of deleting things from batch files, since you never know what can go wrong.

the worst this batch file can do is leave you with some extra bcd options (i avoided setting default, since subsequent deleting of that default really messes things up - visual bcd to the rescue again)

with this particular batch file, it seems to be a bit random about selecting and assigning the drive letter. since there's no easy way to ask the user, i count on "select vdisk ... " to activate the disk, then select partition 1. some of the time that selects the volume, some of the time it doesn't.

i'm definately eager to learn about vhd compression and other advanced areas, so i'll keep on coding. i don't actually know windows batch files, i'm a php and c# programmer really, with a lot of unix behind me, but the batch files were easy enough to crib.

just found something called "MOA", which while a tad outdated, appears to offer a bootable usb with such wonders as a working copy of virtual pc. if i can leverage off that, we should be able to make a very powerful tool that doesn't require an actual copy of windows to be running.

and that i feel, is kinda a primary goal. 'bare metal tools for bare metal installs'. it's the only way... but it's a bitch to code for the WinPE environment.





Also tagged with one or more of these keywords: vhd, bare metal, boot, win 7, grub4dos