Sunday, December 19, 2010

How to image a Windows 2008 Server using WinPE and imagex

We have a windows server 2008 R2 that we want to image to another server.
First we must create a Windows PE boot CD, that it will be used for the imaging of the server.
In a windows 7 PC, running a 64-bit Windows (a technician computer according to Microsoft documentation) we install Windows AIK. After that we open an elevated Deployment Tools Command Prompt from Windows 7 Start Menu and we enter the following commands:
DISM /Cleanup-Wim
copype.cmd amd64 c:\winpe_amd64
copy c:\winpe_amd64\winpe.wim c:\winpe_amd64\ISO\sources\boot.wim
Dism /Mount-Wim /WimFile:C:\winpe_amd64\winpe.wim /index:1 /MountDir:C:\winpe_amd64\mount
Dism /image:C:\winpe_amd64\mount /Add-Package /PackagePath:"C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs\winpe-wmi.cab"
Dism /image:C:\winpe_amd64\mount /Add-Package /PackagePath:"C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs\en-us\winpe-wmi_en-us.cab"
Dism /image:C:\winpe_amd64\mount /Add-Package /PackagePath:"C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs\winpe-scripting.cab"
Dism /image:C:\winpe_amd64\mount /Add-Package /PackagePath:"C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs\en-us\winpe-scripting_en-us.cab"
Dism /image:C:\winpe_amd64\mount /Add-Package /PackagePath:"C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs\winpe-hta.cab"
Dism /image:C:\winpe_amd64\mount /Add-Package /PackagePath:"C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs\en-us\winpe-hta_en-us.cab"
Dism /image:C:\winpe_amd64\mount /Add-Package /PackagePath:"C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs\winpe-wds-tools.cab"
Dism /image:C:\winpe_amd64\mount /Add-Package /PackagePath:"C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs\en-us\winpe-wds-tools_en-us.cab"
copy /y "C:\Program Files\Windows AIK\Tools\amd64\imagex.exe" C:\winpe_amd64\mount\Windows\System32\
copy /y "C:\Program Files\Windows AIK\Tools\amd64\bcdboot.exe" C:\winpe_amd64\mount\Windows\System32\
copy /y "C:\Program Files\Windows AIK\Tools\PETools\amd64\BootSect.exe" C:\winpe_amd64\mount\Windows\System32\
copy /y "C:\Windows\System32\bcdedit.exe" C:\winpe_amd64\mount\Windows\System32\

If we have a folder with 3rd party drivers we can inject these drivers to windows PE image using the following command:
DISM /image:C:\winpe_amd64\mount /Add-Driver /driver:C:\tmp\drv\ /recurse
After the driver injection we unmount the wim file and commit changes using this command:
Dism /Unmount-Wim /MountDir:C:\winpe_amd64\mount\ /Commit
Then we copy the wim file to the iso folder of the working folder:
copy /y c:\winpe_amd64\winpe.wim c:\winpe_amd64\ISO\sources\boot.wim
Finally we create the bootable iso image entering this command:
oscdimg -n -bc:\winpe_amd64\etfsboot.com c:\winpe_amd64\ISO c:\winpe_amd64\winpe_amd64.iso
This ISO image can be written to a blank CD  using a tool like ISO Recorder
Optionally we can create a bootable USB stick using the article How to create a bootable WinPE 2.0 USB key
Optionally we can use a tftp server to boot this WIM using these instructions:
Mount the Windows PE image to the \Mount folder of the working directory:
Dism /Mount-Wim /WimFile:C:\winpe_amd64\winpe.wim /index:1 /MountDir:C:\winpe_amd64\mount
Map a network connection to the root TFTP directory on the TFTP server and create a \Boot folder:
net use N: \\tftpServer\TFTPRoot
md N:\Boot

md N:\Boot\Fonts
Copy the PXE boot files from the mounted directory to the \Boot folder:
copy /y C:\winpe_amd64\mount\Windows\Boot\PXE\*.* N:\Boot
Copy the boot.sdi file to the PXE/TFTP server:
copy /y C:\Program Files\Windows AIK\Tools\PETools\amd64\boot\boot.sdi N:\Boot
Copy fonts files to Fonts directory:
copy /y C:\winpe_amd64\ISO\boot\fonts\*.* N:\Boot\Fonts\
Unmount the current Windows PE image and copy the bootable Windows PE image to the \Boot folder renaming this file to boot.wim:
Dism /Unmount-Wim /MountDir:C:\winpe_amd64\mount\ /Commit
copy c:\winpe_amd64\winpe.wim N:\Boot\boot.wim
Then we we must create a BCD store by using the BCDEdit tool. Using this link we create the following batch file to create the BCD store:
file name: createBCD.cmd
--------------------------------
Rem Creates BCD (boot configuration data) for Windows PE
set BCD-File=c:\tmp\BCD
del %BCD-File%
Bcdedit /createstore %BCD-File%
Bcdedit /store %BCD-File% /create {ramdiskoptions} /d "Ramdisk options"
Bcdedit /store %BCD-File% /set {ramdiskoptions} ramdisksdidevice boot
Bcdedit /store %BCD-File% /set {ramdiskoptions} ramdisksdipath \boot\boot.sdi
Bcdedit -store %BCD-File% -create {dbgsettings} /d "Debugger settings"
Bcdedit -store %BCD-File% -set {dbgsettings} debugtype serial
Bcdedit -store %BCD-File% -set {dbgsettings} baudrate 115200
Bcdedit -store %BCD-File% -set {dbgsettings} debugport 1
for /f "tokens=1-3" %%a in ('Bcdedit /store %BCD-File% /create /d "MyWinPE Boot Image" /application osloader') do set guid1=%%c
Bcdedit /store %BCD-File% /set %guid1% systemroot \Windows
Bcdedit /store %BCD-File% /set %guid1% detecthal Yes
Bcdedit /store %BCD-File% /set %guid1% winpe Yes
Bcdedit /store %BCD-File% /set %guid1% osdevice ramdisk=[boot]\boot\boot.wim,{ramdiskoptions}
Bcdedit /store %BCD-File% /set %guid1% device ramdisk=[boot]\boot\boot.wim,{ramdiskoptions}
Bcdedit /store %BCD-File% /create {bootmgr} /d "Windows BootManager"
Bcdedit /store %BCD-File% /set {bootmgr} timeout 30
Bcdedit /store %BCD-File% /set {bootmgr} displayorder %guid1%
Bcdedit /store %BCD-File% /enum all

--------------------------------
 Running this batch file the BCD file is created. After that we can copy this file to the tftp server:
copy /y c:\tmp\BCD N:\Boot
After this we can use a tftp server like this to boot our PE image. I use these settings for the tftpd configuration:

 TFTP Security: Standard
Advanced TFTP Options
Option negotiation
Translate Unix file names
Allow '\' As virtual root
Use anticipation window of 8192 bytes (optional for higher transfer rates)
tftpd settings
We must also configure a DHCP server to give the boot client an IP address and this boot file: pxeboot.n12 (DHCP options 066 and 067)
dhcp options
Using the boot CD or the bootable USB stick or a tftp server we boot the PE image we created and we enter this command to find the partitions of our server:
diskpart
DISKPART> list disk
If we have only one disk this is disk 0 so we enter these commands:
DISKPART> select disk 0
DISKPART> list part
With this command we found the partitions we want to image. Usually there is a small System partition and a bigger Windows partition. Use the diskpart command "assign letter" to assign letters to these partitions so that we can use the imagex command to image these partitions:
imagex /capture C:\ D:\SystemPartition.wim "My System Partition"
imagex /capture D:\ D:\WindowsPartition.wim "My Windows Partition"
In this example C: is the system partition and D: is the Windows Partition.
Using the net use command map a network drive to a file server so that we can store these wim files to a network folder:
net use F: \\server\share
copy /y D:\SystemPartition.wim F:\Images
copy /y D:\WindowsPartition.wim F:\Images
In this example we copied the wim files to the Images folder in our file server.
Image restoration
We can restore these image files to another server using this procedure:
Boot the new server using one of the three aforementioned methods (boot CD, boot USB or tftp server)
Enter diskpart and create the partition scheme of the old server. For example:
diskpart
DISKPART>select disk 0
DISKPART>clean
DISKPART>create partition primary size=300 
DISKPART>format quick fs=ntfs label="System"
DISKPART>assign letter="S"
DISKPART>active
DISKPART>create partition primary
DISKPART>format quick fs=ntfs label="Windows"
DISKPART>assign letter="W"
DISKPART>exit
If we are working with a UEFI based PC we use the following diskpart commands:
diskpart
DISKPART>select disk 0
DISKPART>clean
DISKPART>convert gpt
DISKPART>create partition efi size=100
DISKPART>format quick fs=fat32 label="System"
DISKPART>assign letter="S"
DISKPART>create partition msr size=128
DISKPART>create partition primary
DISKPART>format quick fs=ntfs label="Windows"
DISKPART>assign letter="W"
DISKPART>exit

For more info take a look at Technet documentation from Microsoft
We can also create a diskpart script using the above command an aply script using the command
diskpart /s
Use net use to create a network drive to map to the file server holding the the wim files:
net use F: \\server\share
After this we can apply the wim files to the new partitions:
imagex /apply F:\Images\SystemPartition.wim 1 S:\
imagex /apply F:\Images\WindowsPartition.wim 1 W:\
Finally use the BCDboot tool to copy common system partition files and to initialize boot configuration data:
W:\Windows\System32\bcdboot W:\Windows /l en-US
After this command we can reboot our new server to windows 2008 and make any customizations we want to do.

No comments:

Post a Comment