Linux – PXEBoot WinPE (windows ADK)

Loading

WinPE 或是 Windows PE 都是指 Windows Preinstallation Environment , 它就像是 Windows 版本的 LiveCD (可以透過 USB / CD / DVD 或是 PXE 等) 來啟動一個精簡的 Windows 系統.

通常利用 RIS / WDS – https://benjr.tw/279 , WDS – https://benjr.tw/100848 就可以讓 WinPE 透過網路開機,但試過 ARM WinPE 版本卻顯示不支援,這時候可以利用 Linux PXEBoot,以前在 Windows XP 時代做過一次,透過 Linux 環境架設 PXEBoot 讓 Windows PE 來開機,請參考 https://benjr.tw/275 .

WinPE 映像檔

測試環境為 Windows 10 x86_64 (虛擬機)
建立 WinPE 映像檔
這次使用的 WinPE 版本為 10.0.17134.1 ( April 2018 update version) Built from Windows 10 (1803) code base ,可以在 https://docs.microsoft.com/zh-tw/windows-hardware/get-started/adk-install#winADK 下載,參考 https://docs.microsoft.com/zh-tw/windows/deployment/configure-a-pxe-server-to-load-windows-pe 說明步驟來架設.

第一步需要先透過指令 copype 建立一個 WinPE 工作環境 (平台可以選擇 x86 , amd64 , arm , arm64, 路徑 c:\WinPE 自訂),在程式集 Windows kits 點選 Deployment and Imaging Tools Environment .

C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools>copype amd64 c:\WinPE
...
C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\Media\zh-tw\bootmgr.efi.mui
155 File(s) copied
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
Success

關於更多客製化 WinPE 的方式,請參考 https://benjr.tw/170

WinPE 開機映像檔 boot.wim
DHCP Client 會下載 PXEboot.n12 開始網路開機,並從 TFTP 下載 bootmgr.exe (UEFI 的檔案名稱則是 Bootmgfw.efi) 與 Boot\BCD 檔案,Bootmgr.exe 會依據 BCD 設定位置來下載 Windows PE 映像檔 Boot\boot.wim.也會依據 BCD 設定位置執行 \windows\system32\winload.exe 來啟動 Windows PE.載入 Windows PE 後會執行 wpeinit.exe 來進行初始化.

所需檔案都可以在 boot.wim 找到.
掛載 boot.wim

C:\Windows\system32>Dism /Mount-Image /ImageFile:"C:\WinPE\media\sources\boot.wim" /index:1 /MountDir:"C:\WinPE\mount"

主要檔案位置如下:

  • PXEboot.n12 – c:\WinPE\mount\Windows\Boot\PXE\
  • bootmgr.exe – c:\WinPE\mount\Windows\Boot\PXE\
  • BCD – 需要自行建立(後面介紹).
  • boot.wim – C:\winpe\media\sources\
C:\Windows\system32>md c:\boot
C:\Windows\system32>copy c:\winpe\mount\windows\boot\pxe\*.* c:\boot
C:\Windows\system32>copy C:\winpe\media\boot\boot.sdi c:\boot
C:\Windows\system32>copy C:\winpe\media\sources\boot.wim c:\boot
C:\Windows\system32>mkdir c:\boot\Fonts
C:\Windows\system32>copy C:\winpe\media\Boot\Fonts\* c:\boot\Fonts\

卸載 boot.wim

C:\Windows\system32>Dism /Unmount-Image /MountDir:"C:\WinPE\mount" /commit

BCD
boot.wim , winload.exe 檔案相對應位置須參考 BCD 的設定,透過 BCDEdit (Boot Configuration Data Store Editor) 來編輯,關於 BCD 請慘考 – https://benjr.tw/3599 .

C:\Windows\system32>bcdedit /createstore c:\BCD
The operation completed successfully.

新增關於 Setup Ramdisk Options 資料.

C:\Windows\system32>bcdedit /store c:\BCD /create {ramdiskoptions} /d "Ramdisk options"
The entry {ramdiskoptions} was successfully created.
 
C:\Windows\system32>bcdedit /store c:\BCD /set {ramdiskoptions} ramdisksdidevice boot
The operation completed successfully.
 
C:\Windows\system32>bcdedit /store c:\BCD /set {ramdiskoptions} ramdisksdipath \boot\boot.sdi
The operation completed successfully.

新增關於 Windows Boot Loader – winpe boot image 資料,會產生新 Entry 的 GUID ,同一個 Entry 都需要填寫該 GUID.

C:\Windows\system32>bcdedit /store c:\BCD /create /d "winpe boot image" /application osloader
The entry {5be46902-c1f4-11e8-8b98-005056fc91fb} was successfully created.

C:\Windows\system32>bcdedit /store c:\BCD /set {5be46902-c1f4-11e8-8b98-005056fc91fb} device ramdisk=[boot]\boot\boot.wim,{ramdiskoptions}
The operation completed successfully.

C:\Windows\system32>bcdedit /store c:\BCD /set {5be46902-c1f4-11e8-8b98-005056fc91fb} path \windows\system32\winload.exe 
The operation completed successfully.
 
C:\Windows\system32>bcdedit /store c:\BCD /set {5be46902-c1f4-11e8-8b98-005056fc91fb} osdevice ramdisk=[boot]\boot\boot.wim,{ramdiskoptions}
The operation completed successfully.
 
C:\Windows\system32>bcdedit /store c:\BCD /set {5be46902-c1f4-11e8-8b98-005056fc91fb} systemroot \windows
The operation completed successfully.
 
C:\Windows\system32>bcdedit /store c:\BCD /set {5be46902-c1f4-11e8-8b98-005056fc91fb} detecthal Yes
The operation completed successfully.
 
C:\Windows\system32>bcdedit /store c:\BCD /set {5be46902-c1f4-11e8-8b98-005056fc91fb} winpe Yes
The operation completed successfully.

新增關於 Windows Boot Manager 資料.

C:\Windows\system32>bcdedit /store c:\BCD /create {bootmgr} /d "boot manager"
The entry {bootmgr} was successfully created.
 
C:\Windows\system32>bcdedit /store c:\BCD /set {bootmgr} timeout 30
The operation completed successfully.
 
C:\Windows\system32>bcdedit /store c:\BCD -displayorder {5be46902-c1f4-11e8-8b98-005056fc91fb} -addlast
The operation completed successfully.
C:\Windows\system32>bcdedit /store C:\BCD /enum all
Windows Boot Manager
--------------------
identifier              {bootmgr}
description             boot manager
displayorder            {5be46902-c1f4-11e8-8b98-005056fc91fb}
timeout                 30

Windows Boot Loader
-------------------
identifier              {5be46902-c1f4-11e8-8b98-005056fc91fb}
device                  ramdisk=[boot]\boot\boot.wim,{ramdiskoptions}
path                    \windows\system32\winload.exe
description             winpe boot image
osdevice                ramdisk=[boot]\boot\boot.wim,{ramdiskoptions}
systemroot              \windows
detecthal               Yes
winpe                   Yes

Setup Ramdisk Options
---------------------
identifier              {ramdiskoptions}
description             Ramdisk options
ramdisksdidevice        boot
ramdisksdipath          \boot\boot.sdi

C:\Windows\system32>copy C:\BCD c:\boot

Linux PXEBoot

測試環境為 CentOS6.8 x86_64 (虛擬機)
DHCP
DHCP Client (Port 68 , 使用廣播封包 broadcast ) 與 DHCP Server (Port 67) 透過四個動作 ( DHCPDISCOVER , DHCPOFFER , DHCPREQUEST , DHCPACK ) 來獲得 IP (使用 UDP),並從 TFTP 下載 PXEboot.n12 來進行網路開機.

[root@localhost ~]# cat /etc/dhcp/dhcpd.conf
#ddns-update-style interim;
ddns-update-style none;
ignore client-updates;
 
allow booting;
allow bootp;
 
subnet 192.8.1.0 netmask 255.255.255.0 {
        range 192.8.1.50 192.8.1.250;
        option broadcast-address 192.8.1.255;
        option subnet-mask 255.255.255.0;
        option domain-name-servers 192.8.1.1;
 
          class "pxeclients" {
                  match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
                  next-server 192.8.1.1;
                  filename "linux-install/PXEboot.n12";
          }
}

如果要支援 EFI + Legacy 多種環境的 DHCP PXE Boot 請參考 https://benjr.tw/16194

TFTP
Windows 路徑表示方式 (\) 與 Linux (/) 不同,需要設定 tftp remap ,如果採用 SystemD (Ubuntu16.04) 設定請參考 – https://benjr.tw/98738 .

確認一下這個本版的 tftp 有支援 remap.

[root@localhost ~]# /usr/sbin/in.tftpd -V
tftp-hpa 0.49, with remap, with tcpwrappers

預設是沒有啟動 remap 功能,需要自行新增 -m /var/lib/tftpboot/tftpd.remap .並啟動 tftp 服務 disable = no .

[root@localhost ~]# vi /etc/xinetd.d/tftp 
# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
{
        disable = no
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot -m /var/lib/tftpboot/tftpd.remap -v 
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

參數說明:
–secure, -s : Change root directory on startup.
–verbose, -v : Increase the logging verbosity of tftpd,最多可以設定到 -vvv .
–mapfile , -m remap-file : Specify the use of filename remapping.

tftp 預設目錄為 /var/lib/tftpboot ,所以 remap 檔案放在 /var/lib/tftpboot/tftpd.remap

[root@localhost ~]# vi /var/lib/tftpboot/tftpd.remap
rg \\ /

參數說明:

  • r – Replace the substring matched by regex by the replacement pattern.
  • g – Repeat this rule until it no longer matches. This is always used with r.
    用 rg 來表示所有的資料都需要修改.
  • \\ – Literal backslash.
rg \\ /

在 Linux 下路徑的表示方式為 /boot 但是 windows 的路徑表示為 \boot ,\\ / 就是要把 \ 換成 /.

[root@localhost ~]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]

WinPE 開機所需檔案
檔案需要複製到 tftp 的根目錄 (/var/lib/tftpboot)

[root@localhost ~]# mkdir /var/lib/tftpboot/Boot/
[root@localhost ~]# cp /media/usb/boot/* /var/lib/tftpboot/Boot

PXEboot.n12 與 bootmgr.exe 需要在 tftpboot 根目錄.

[root@localhost ~]# mv /var/lib/tftpboot/Boot/PXEboot.n12 /var/lib/tftpboot/
[root@localhost ~]# mv /var/lib/tftpboot/Boot/bootmgr.exe /var/lib/tftpboot/

boot.sdi , boot.wim 需要在 boot 目錄有分身.

[root@localhost ~]# mkdir /var/lib/tftpboot/boot/
[root@localhost ~]# cp /var/lib/tftpboot/Boot/boot.sdi /var/lib/tftpboot/boot/
[root@localhost ~]# cp /var/lib/tftpboot/Boot/boot.wim /var/lib/tftpboot/boot/

支援 UEFI PXE Boot

bootx64.efi – 檔案位於 C:\WinPE\media\EFI\Boot , 用以取代 PXEboot.n12
bootmgfw.efi – 檔案位於 C:\WinPE\mount\Windows\Boot\EFI , 用以取代 bootmgr.exe
只需要取代這兩個檔案,其餘不變,但我失敗了,目前還找不出原因.

PXE Boot from WinPE

正確就會看到如下的開機畫面.

常見的錯誤:

PXE 通常會利用 tcpdump – https://benjr.tw/96904 來檢查哪一個環節出錯了,下面有兩種錯誤發生.

10:11:33.191880 IP 192.6.1.120.9081 > 192.6.1.1.tftp:  26 RRQ "\Boot\BCD" octet tsize 0 
10:11:33.192593 IP 192.6.1.1.49187 > 192.6.1.120.9081: UDP, length 19
10:11:33.192686 IP 192.6.1.120.9082 > 192.6.1.1.tftp:  42 RRQ "\Boot\Fonts\wgl4_boot.ttf" octet tsize 0 
10:11:33.193248 IP 192.6.1.1.46264 > 192.6.1.120.9082: UDP, length 19
10:11:33.193369 IP 192.6.1.120.9083 > 192.6.1.1.tftp:  42 RRQ "\Boot\Fonts\wgl4_boot.ttf" octet tsize 0 
10:11:33.193925 IP 192.6.1.1.33097 > 192.6.1.120.9083: UDP, length 19
10:11:33.197568 IP 192.6.1.120.aurora > 192.6.1.1.tftp:  57 RRQ "\EFI\Microsoft\Boot\SecureBootPolicy.p7b" octet tsize 0 
  • octet tsize 0
    通常是路徑錯誤 (Windows 不分大小寫),找不到檔案只會顯示 tsize 為 0 .
  • “\Boot\BCD”
    Windows 路徑表示方式 (\) 與 Linux (/) 不同,需要透過 tftp remap 來修正.
沒有解決問題,試試搜尋本站其他內容

4 thoughts on “Linux – PXEBoot WinPE (windows ADK)

  1. Hi,
    最近想處理 UEFI 的 PXE boot (bootmgr.exe + pxeboot.n12 這個組合我是過沒問題), 但我遇到找不到一堆 certificate files 以至於出現開機失敗的錯誤結果 (0xc000025), 目前還沒有證據顯示這個錯誤與 p7b 檔案有直接關聯, 但我試圖補足這些檔案, 不過 ADK 裡頭都沒有, 請問你有遇到這方面的問題嗎?
    謝謝

    TFTP log:
    [29-Oct-19 17:31:38] Client 10.17.63.24:1691 /volume1/PXE/bootx64.efi, Error 8 at Client, User aborted the transfer
    [29-Oct-19 17:31:38] Client 10.17.63.24:1692 /volume1/PXE/bootx64.efi, 869 Blocks Served
    [29-Oct-19 17:31:38] Client 10.17.63.24:9597 /volume1/PXE/Boot/BCD, Error 0 at Client, TFTP Aborted
    [29-Oct-19 17:31:38] Client 10.17.63.24:9598 /volume1/PXE/Boot/BCD, 10 Blocks Served
    [29-Oct-19 17:31:38] Client 10.17.63.24:9599 /volume1/PXE/EFI/Microsoft/Boot/Policies/SbcpFlightToken.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9600 /volume1/PXE/EFI/Microsoft/Boot/SecureBootPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9601 /volume1/PXE/EFI/Microsoft/Boot/SkuSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9602 /volume1/PXE/EFI/Microsoft/Boot/SiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9603 /volume1/PXE/EFI/Microsoft/Boot/UpdateSkuSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9604 /volume1/PXE/EFI/Microsoft/Boot/UpdateSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9605 /volume1/PXE/EFI/Microsoft/Boot/WinSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9606 /volume1/PXE/EFI/Microsoft/Boot/UpdateWinSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9607 /volume1/PXE/EFI/Microsoft/Boot/ATPSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9608 /volume1/PXE/EFI/Microsoft/Boot/UpdateATPSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9609 /volume1/PXE/EFI/Microsoft/Boot/EntRevokeSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9610 /volume1/PXE/EFI/Microsoft/Boot/UpdateEntRevokeSiPolicy.p7b, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9611 /volume1/PXE/EFI/Microsoft/Boot/Fonts/wgl4_boot.ttf, No Such File/No Access
    [29-Oct-19 17:31:38] Client 10.17.63.24:9612 /volume1/PXE/EFI/Microsoft/Boot/Fonts/wgl4_boot.ttf, No Such File/No Access

  2. 先前研究用Linux PXE Server去Provide WinPE的方式
    default檔案內的內容如下:
    label winpe2012r2
    kernel DOS/memdisk
    Append initrd=win/w2012r2/WinPE_amd64.iso iso raw
    不過UEFI我就沒試出來可行的方法了。
    如果能像WDS那樣,進到WinPE後,能有選單可以選擇,使用起來會更方便一些

  3. 您好

    最近在研究PXE Server啟動安裝OS
    我PXE Server,想同時服務CentOS和Ubuntu desktop和Windows的安裝
    在PXE Server的DHCP設定檔及tftpboot/pxelinux.cfg/default的boot menu
    要如何設定呢?
    目前預到的困難點是,根據你這篇文章,在dhcp設定檔裏,要去呼叫pxeboot.n12這個檔案,然後就能順利啟動WinPE
    但這樣子,又沒辦法帶出boot menu,也就等於沒辦法安裝CentOS及Ubuntu Desktop
    所以想請教,該如何修改DHCP及default這兩個檔案呢?
    謝謝了

    1. CentOS 和 Ubuntu desktop 使用相同的前導程式 pxelinux.0 (x86 Legacy BIOS) ,設定請參考 – http://benjr.tw/83 ,Windows 使用 pxeboot.n12 當前導程式,這兩者無法同時使用在同一台 PXE 上,除非這兩者的 Architecture Type 不同,請參考 – http://benjr.tw/16137

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料