Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In Win7 you can attach a VHD as a drive, however after a reboot the VHD is no longer attached. Is there anyway to make it permanent?

share|improve this question
add comment

8 Answers

up vote 11 down vote accepted

I don't see a way to re-attach it at boot "natively". You could use a "startup script" with the DISKPART commands to re-attach it, or deploy a script in the "Startup" program group to re-attach it after logon. That's disappointing and hackish, but at least it would give you what you wanted.

@echo off
SET TEMPFILE="%TEMP%\%RANDOM%.TXT"
echo SELECT VDISK FILE=(vdisk filename here)>%TEMPFILE%
echo ATTACH VDISK>>%TEMPFILE%
DISKPART /s %TEMPFILE%
del %TEMPFILE%

It's sad, but it's the best I can see to do for now...

share|improve this answer
    
This works great from an elevated command prompt. It doesn't work from the startup program group however. Something about the permissions makes it not execute the DISKPART correctly. –  rein Jul 10 '09 at 13:55
1  
Yeah... UAC is a pain. It should work fine from a startup script, since that runs as SYSTEM and is, by definition, elevated. –  Evan Anderson Jul 10 '09 at 13:57
    
Yes thanks, putting into the group policy startup script works for me. –  rein Jul 10 '09 at 14:08
    
Does anyone successful implement this ? I've got "The arguments specified for this command are not valid. For more information on the command type: HELP SELECT VDISK" message. –  Funky81 Sep 4 '09 at 1:16
2  
+1 for %RANDOM%. Just because it's new to me. :-) –  mpbloch Apr 13 '10 at 23:08
show 3 more comments

Another way is to attach on service level. You need instsrv.exe and srvany.exe from Windows Resource Kit Tools. Put them to any folder with the following files:

disk-W-install-permanently.cmd

@echo off
echo RUN THIS AS ADMINISTRATOR!
pause
cd %~dp0
copy srvany.* c:\windows\system32\
copy srvany-attach-disk-W c:\windows\system32\
instsrv disk_W_attach "c:\windows\system32\srvany.exe"
regedit disk-W-install-permanently.reg
pause

disk-W-install-permanently.reg

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\disk_W_attach\Parameters]
"AppEnvironment"=""
"Application"="c:\\windows\\system32\\cmd.exe"
"AppParameters"="/c srvany.bat"
"AppDirectory"="C:\\Windows\\System32"

srvany.bat

@echo on 
set log=%temp%\srvany.log

echo log: Running at %date% %time% >>%log%
diskpart /s srvany-attach-disk_W >>%log%
net stop disk_W_attach

srvany-attach-disk-W

select vdisk file=”D:\private\vm\HDD\disk_W.vhd"
attach vdisk
exit

dir /b

disk-W-install-permanently.cmd
disk-W-install-permanently.reg
instsrv.exe
srvany.bat
srvany.exe
srvany-attach-disk-W

Now edit script srvany-attach-disk_W to point to your VHD, execute disk-W-install-permanently.cmd with admin privileges and reboot. That's all!

share|improve this answer
1  
The built-in "SC" will do what "INSTSRV" does in your text above. Running a service to start a batch file seems a little dodgy to me. I'd just run a startup script and be done with it. –  Evan Anderson Sep 4 '09 at 1:22
add comment

You can try using VHD attach. It has option to attach VHD after each boot. Since one part runs as service, no UAC prompt will bother you.

share|improve this answer
add comment

VHD attach program worked for me. It attaches VHD for all the users. No need to create a start up script or scheduled task. The program attaches VHD itself. you just need to install the program. Attach your VHD file once or go to options -> Add the VHD file and select Auto Attach. Thanks Josip. You are awesome

share|improve this answer
add comment

I made a program for this, BMVHDloader.com, Free and still updated all the time. It will be a service on the next release. And its free to distribute since i needed it and the above mentioned was not available. I figure if i needed it and it was not around then i would let everyone have it for free. The one mentioned above (" VHD Attach" ) i believe was made with VB Also this month i am releasing a version for server 2003 and XP. :) Please pass to everyone that needs it and check back for updates on my site. Happy VHD'ing

share|improve this answer
add comment

There is no native way to do this - Windows does not currently persist attached VHDs across a reboot. A system startup script is going to be your best bet.

share|improve this answer
add comment

I created a batch file that uses diskpart /s and specifies a script to use that attaches my various VHD files. Then I created a scheduled task to execute during startup.

Here is a page I created with a step by step process to do it.

http://coding.infoconex.com/post/2009/11/26/Automatically-attaching-VHD-files-in-Windows-7-and-Windows-2008.aspx

share|improve this answer
add comment

Put the same diskpart /s command into task sheduler with 'highest privileges' checked and 'run it on computer startup'. It works for me (not for boot disk), mounts vhd even before user logs on.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.