建立自訂 Windows BYOL 映像檔


如要在 Google Cloud上建立 Windows VM,您必須使用預先安裝 Windows 或 Windows Server 的 VM 映像檔。 Google Cloud 提供常用 Windows Server 版本的公開映像檔,但這些映像檔僅適用於隨選授權。如要自備 Windows 授權 (BYOL),您必須匯入現有映像檔,或建立自訂映像檔。

本指南說明如何使用 Google Cloud 建立公開映像檔時使用的工具和程序,建立自訂映像檔。

如要完成本指南,您需要:

  • 包含 Windows 或 Windows Server 安裝媒體的 ISO 檔案。
  • 視需要,一或多個 Windows 更新套件 (以 .msu 格式),用於套用至映像檔。

事前準備

  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    1. After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    2. Set a default region and zone.

瞭解建構程序

如要從頭安裝 Windows,常見做法是透過含有 Windows 安裝檔案的 DVD 或 ISO 檔案啟動電腦。與部分內部虛擬機器不同,Compute Engine 不允許您從 ISO 檔案啟動。

因此,如要從頭安裝 Windows,您必須採用不同的方法,並執行下列步驟:

  1. 建立新磁碟。
  2. 將 Windows 映像檔 (安裝媒體中的 install.wim) 擷取至磁碟。
  3. 新增必要的驅動程式、設定 Windows 安裝程式以便自動執行,以及讓磁碟可啟動。
  4. 從新磁碟啟動,執行 Windows 設定程序。
  5. 安裝其他軟體,包括訪客作業系統代理程式。
  6. 透過磁碟建立映像檔。

本指南將說明如何使用 Cloud Builddaisy 工具,以及GitHub 上的參考工作流程,自動執行這項程序,而非手動執行這些步驟。

daisy開放原始碼指令列工具,可讓您執行工作流程。工作流程會以 JSON 檔案的形式編寫,並包含一系列步驟。每個步驟都會說明 Compute Engine 作業,例如建立磁碟或關閉 VM 執行個體。因此,Daisy 工作流程適合用於自動執行從頭建立 Windows 映像檔所需的步驟。

建構自訂 Windows 映像檔的 Daisy 工作流程會建立兩個臨時 VM 執行個體。第一個 VM 執行個體 (前置字串為 bootstrap) 會執行建立可啟動磁碟的必要步驟。第二個 VM 執行個體 (前置字串為 install) 會執行 Windows 設定,並執行所有剩餘步驟。

準備建構映像檔的專案

為避免 Daisy 工具干擾現有的 VM 執行個體或基礎架構,請建立專用專案來建構映像檔:

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine API, Cloud Build API, and Cloud Storage API APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Compute Engine API, Cloud Build API, and Cloud Storage API APIs.

    Enable the APIs

接下來的步驟會因您在本機電腦上使用 Windows 或 Linux 而有所不同:

Windows

  1. 在本機電腦上開啟 Windows PowerShell 視窗。
  2. 初始化變數:

    $PROJECT_ID = "PROJECT_ID"
    

    其中 PROJECT_ID 是您在上一節中建立的 Google Cloud 專案專案 ID。

  3. 初始化另一個變數,讓該變數包含專案的專案編號:

    $PROJECT_NUMBER = gcloud projects describe $PROJECT_ID --format=value`(projectNumber`)
    

Linux

  1. 在本機電腦上開啟終端機視窗。
  2. 初始化變數:

    PROJECT_ID=PROJECT_ID
    

    其中 PROJECT_ID 是您在上一節中建立的 Google Cloud 專案專案 ID。

  3. 初始化另一個變數,讓該變數包含專案的專案編號:

    PROJECT_NUMBER=`gcloud projects describe $PROJECT_ID --format=value\(projectNumber\)`
    

上傳安裝檔案

您現在可以收集自訂映像檔所需的所有安裝檔案,並上傳至 Cloud Storage 值區。將檔案儲存在 Cloud Storage 值區中,可確保 daisydaisy 用於建構映像檔的暫時性 VM 執行個體都能存取這些檔案。

  1. 在本機電腦上下載必要的安裝套件:

  2. 建立新的 Cloud Storage 值區以儲存安裝檔案:

    gcloud storage buckets create gs://$PROJECT_ID-media --project=$PROJECT_ID
    
  3. Storage 物件檢視者角色授予 Cloud Build,讓 Cloud Build 能夠讀取安裝檔案:

    gcloud storage buckets add-iam-policy-binding gs://$PROJECT_ID-media --member=serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com --role=roles/storage.objectViewer
    
  4. 上傳 PowerShell 安裝套件:

    gcloud storage cp POWERSHELL_PACKAGE gs://$PROJECT_ID-media/PowerShell.msi
    

    其中 POWERSHELL_PACKAGE 是 PowerShell 安裝套件的路徑。

  5. 上傳 .NET Framework 安裝套件:

    gcloud storage cp DOTNET_PACKAGE gs://$PROJECT_ID-media/dotnet-sdk.exe
    

    其中 DOTNET_PACKAGE 是 .NET Framework 安裝套件的路徑。

  6. 上傳 gcloud CLI 安裝套件:

    gcloud storage cp CLOUDSDK_PACKAGE gs://$PROJECT_ID-media/GoogleCloudSDKInstaller.exe
    

    其中 CLOUDSDK_PACKAGE 是 gcloud CLI 安裝套件的路徑。

  7. 上傳內含 Windows 安裝媒體的 ISO 檔案:

    gcloud storage cp ISO gs://$PROJECT_ID-media/
    

    其中 ISO 是 ISO 檔案的名稱。

  8. 視需要上傳其他更新套件:

    gcloud storage cp UPDATE_DIR/*.msu gs://$PROJECT_ID-media/updates/
    

    其中 UPDATE_DIR 是包含更新套件的目錄。

您現在可以開始建構自訂映像檔。

建構映像檔

執行 daisy 工作流程來建構自訂映像檔最多需要四小時。您現在可以建立 Cloud Build 設定,讓 Cloud Build 在背景執行工作流程,而非在本機執行 daisy

  1. 在本機電腦上,複製包含 daisy 工作流程的 Git 存放區,以便建構 Windows 映像檔:

    git clone https://github.com/GoogleCloudPlatform/compute-image-tools.git
    
  2. 切換至 windows 目錄:

    cd compute-image-tools/daisy_workflows/image_build/windows/
    
  3. windows 目錄中,您會看到選取的檔案帶有 .wf.json 字尾。這些檔案包含常用 Windows 版本的 Daisy 工作流程定義:

    Windows 版本 工作流程檔案
    Windows Server Core 2022 (64 位元) windows-server-2022-dc-core-uefi-byol.wf.json
    Windows Server 2019 (64 位元) windows-server-2019-dc-uefi-byol.wf.json
    Windows Server Core 2019 (64 位元) windows-server-2019-dc-core-uefi-byol.wf.json
    Windows Server 2016 (64 位元) windows-server-2016-dc-uefi-byol.wf.json
    Windows Server Core 2016 (64 位元) windows-server-2016-dc-core-uefi-byol.wf.json
    Windows 11 21H2 (64 位元) windows-11-21h2-ent-x64-uefi.wf.json
    Windows 11 22H2 (64 位元) windows-11-22h2-ent-x64-uefi.wf.json
    Windows 11 23H2 (64 位元) windows-11-23h2-ent-x64-uefi.wf.json
    Windows 10 21H2 (64 位元) windows-10-21h2-ent-x64-uefi.wf.json
    Windows 10 22H2 (64 位元) windows-10-22h2-ent-x64-uefi.wf.json

    開啟最符合您要安裝的 Windows 版本的工作流程檔案。如有需要,請變更工作流程檔案中的 Windows 版本 (edition) 和授權金鑰 (product_key) 設定,以便與安裝媒體相符。

    如果您不確定正確的版本名稱,請開啟提升權限的 PowerShell 提示,然後執行下列指令,列出安裝媒體支援的所有版本:

    $IsoFile = "ISO"
    
    $Mount = Mount-DiskImage -ImagePath (Resolve-Path $IsoFile)
    
    $DriveLetter = ($Mount | Get-Volume).DriveLetter
    Get-WindowsImage -ImagePath "$($DriveLetter):\sources\install.wim" | select ImageName
    
    Dismount-DiskImage -InputObject $Mount | Out-Null
    

    ISO 替換為 ISO 映像檔的本機路徑。

  4. windows 目錄中建立名為 cloudbuild.yaml 的新檔案,然後貼上以下程式碼:

    timeout: 14400s  # 4 hour timeout for entire build
    steps:
    - name: 'gcr.io/compute-image-tools/daisy'
      timeout: 14400s  # 4 hour timeout for build step
      waitFor: ['-']
      args:
        - -project=$PROJECT_ID
        - -zone=us-central1-a
        - -var:updates=gs://$PROJECT_ID-media/updates/
        - -var:pwsh=gs://$PROJECT_ID-media/PowerShell.msi
        - -var:dotnet48=gs://$PROJECT_ID-media/dotnet-sdk.exe
        - -var:cloudsdk=gs://$PROJECT_ID-media/GoogleCloudSDKInstaller.exe
        - -var:media=gs://$PROJECT_ID-media/ISO
        - WORKFLOW
    

    取代:

    • ISO:Cloud Storage 中的 ISO 檔案名稱。
    • WORKFLOW:對應您使用的 Windows 版本的工作流程檔案名稱。
  5. 將建構提交至 Cloud Build:

    gcloud builds submit --project $PROJECT_ID --async
    

    建構作業最多需要四小時才能完成。您可以在 Google Cloud 控制台的「Cloud Build」>「History」(歷史記錄) 下,追蹤版本的狀態

    Cloud Build 記錄

使用自訂映像檔

建構作業完成後,您可以在 Google Cloud 控制台的「Compute Engine」>「Images」下方找到自訂 BYOL 映像檔。

為了區分同一張圖片的多個版本,建構程序會將時間戳記嵌入圖片名稱中,例如 windows-server-2019-dc-v1613488342。此外,這項程序會將圖片與自訂圖片系列建立關聯,例如 windows-server-2019

如要建立使用自訂 BYOL 映像檔的 VM 執行個體,您必須在單一用戶群節點上佈建 VM 執行個體

疑難排解

如果您懷疑建構程序失敗或沒有進行,請使用下列方法診斷情況:

  • 確認您已上傳正確的安裝套件和 ISO 檔案。
  • 確認您選取的 workflow 與 ISO 檔案的 Windows 版本相符。
  • 查看 Cloud Build 中的建構記錄 ,檢查是否有任何錯誤訊息。
  • 如果建構作業似乎卡住,請查看建構作業建立的 VM 執行個體的序列埠輸出,並檢查是否有錯誤訊息。

後續步驟