Install Windows 7 on an USB disk

From Qontrol.nl Wiki
Revision as of 15:49, 9 November 2022 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This article will guide you through the process of creating a bootable Windows 7 USB disk. Not for installing Windows 7, but for actually running Windows 7 from the USB disk. No virtualization tricks, just a real portable Windows 7 disk.

Installing Windows 7 onto the USB device

First, you'll need to get Windows to install itself on the USB device. This is normally not possible, but there are a few hacks.

  • If you want to install Windows on an USB harddisk, you can remove the harddisk from it's USB casing and install it directly in your computer. You can then install Windows 7 on the disk.
  • Alternatively, you can use some virtualzation software (like VMWare or VirtualBox) to 'attach' the USB device as a virtual SATA disk to a virtual machine, and install Windows 7 inside the virtual machine, onto the USB device.

I'll use VirtualBox to install Windows on my portable USB harddisk. I use Ubuntu Linux on my laptop, so the commands used here may be Linux specific - but Windows equivalents exist!

  1. First, I create a 'raw disk' vmdk virtual harddisk file, using the following command: VBoxManage internalcommands createrawvmdk -filename USBDisk.vmdk -rawdisk /dev/sdb -register
    • This command needs to be run as root (Administrator for windows)!
    • USBDisk.vmdk is the filename of the virtual harddisk file we're creating, that will map to the physical device
    • /dev/sdb is the physical device we want to use. Make sure that you use the right drive here, as you might otherwise erase your existing operating system!!!
    • Windows has a different notation for the physical drive notation. It's somewhere in the VirtualBox manual. Google is your friend.
  2. Then, still as root/Administrator, we create a new virtual machine with sufficient RAM, and attach the newly created .vmdk image to the machine, and attach a Windows 7 installation media to the machine.
  3. Start the Virtual Machine and proceed the installation as usual.

Configuring Windows 7 to boot from the USB device

Normally, Windows 7 would not boot from anything it doesn't recognize. This includes USB devices. To fix this, we'll have to run a few commands, as Administrator, on our new Windows 7 installation:

  1. bcdedit /set {default} detecthal on
  2. usbbootfix.bat (see below for the contents of this file!)
  3. StorageBootStart.bat (see below, this is optional, will slow down the boot process but make it a lot more portable!)

usbbootfix.bat contents

Copy the text below into a new notepad document and save it as usbbootfix.bat. Make sure to select "All files (*.*)" for the file type!

@echo off
if "%1"=="fix" goto :fix

rem -- install task
copy /y "%~f0" "%SystemRoot%\system32\usbbootfix.bat"
SCHTASKS /Create /RU SYSTEM /SC ONEVENT /MO "*[System[Provider[@Name='Microsoft-Windows-UserPnp'] and EventID=20003]]" /EC System /TN USBBootFix /TR "'%SystemRoot%\system32\usbbootfix.bat' fix" /F
rem -- apply other settings
reg add HKLM\SYSTEM\CurrentControlSet\services\pciide /v Start /t REG_DWORD /d 0x0 /f
reg add HKLM\SYSTEM\CurrentControlSet\services\msahci /v Start /t REG_DWORD /d 0x0 /f
reg add HKLM\SYSTEM\CurrentControlSet\services\intelide /v Start /t REG_DWORD /d 0x0 /f
reg add HKLM\SYSTEM\CurrentControlSet\services\viaide /v Start /t REG_DWORD /d 0x0 /f
rem -- run :fix once after install

:fix
call :fixservice usbehci "Boot Bus Extender"
call :fixservice usbohci "Boot Bus Extender"
call :fixservice usbuhci "Boot Bus Extender"
call :fixservice usbhub  "System Bus Extender"
call :fixservice usbstor "SCSI miniport"
goto :eof

:fixservice
setlocal
set Start=
set Group=
for /f "skip=2 tokens=1,2,*" %%I in ('reg query HKLM\SYSTEM\CurrentControlSet\services\%~1') do (
  if "%%I"=="Start" set Start=%%K
  if "%%I"=="Group" set Group=%%K
)
if not "%Start%"=="0x0" reg add HKLM\SYSTEM\CurrentControlSet\services\%~1 /v Start /t REG_DWORD /d 0x0 /f
if not "%Group%"=="%~2" reg add HKLM\SYSTEM\CurrentControlSet\services\%~1 /v Group /t REG_SZ /d "%~2" /f
endlocal
goto :eof

StorageBootStart.bat

Copy the text below into a new notepad document and save it as StorageBootStart.bat. Make sure to select "All files (*.*)" for the file type!

@echo off
setlocal
set saveundo=^>^>StorageBootStart_Undo.bat echo 
%saveundo% @echo off
for /f "skip=1 tokens=1-5 delims=^&" %%I in ('reg query HKLM\SYSTEM\CurrentControlSet\Control\CriticalDeviceDatabase') do (
 if not .%%I==. call :checkcdd %%I %%J %%K %%L %%M
)
endlocal
goto :eof

:checkcdd
set key=%1
if not .%key:~0,18%==.HKEY_LOCAL_MACHINE goto :eof
if not .%2==. set key=%1^&%2
if not .%3==. set key=%1^&%2^&%3
if not .%4==. set key=%1^&%2^&%3^&%4
if not .%5==. set key=%1^&%2^&%3^&%4^&%5
set class=
set service=
for /f "skip=2 tokens=1,2,*" %%I in ('reg query "%key%"') do (
  if /i "%%I"=="ClassGUID" set class=%%K
  if /i "%%I"=="Service" set service=%%K
)
if .==.%service% goto :eof
if /i "%class%"=="{4D36E96A-E325-11CE-BFC1-08002BE10318}" goto :storage
if /i "%class%"=="{4D36E97B-E325-11CE-BFC1-08002BE10318}" goto :storage
goto :eof

:storage
set /a knowsv=knowsv_%service%
if %knowsv%==1 goto:eof
set knowsv_%service%=1
set start=
for /f "skip=2 tokens=1,2,*" %%I in ('reg query HKLM\SYSTEM\CurrentControlSet\services\%service% /v Start') do (
  if /i "%%I"=="Start" set /a start=%%K
)
echo %service% %start%
if "%start%"=="0" goto :eof
%saveundo% echo %service% %start%
%saveundo% reg add HKLM\SYSTEM\CurrentControlSet\services\%service% /v Start /t REG_DWORD /d %start% /f
reg add HKLM\SYSTEM\CurrentControlSet\services\%service% /v Start /t REG_DWORD /d 0 /f
goto :eof

Many thanks go to

After spending a few hours on google, I finally found this thread on http://www.boot-land.net/, containing everything needed to make Windows 7 boot from an USB device. To make sure that this information is not lost, and to make it more accessible, I've written this article.