建立自訂 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 進行驗證:

    選取這個頁面上的分頁,瞭解如何使用範例:

    控制台

    使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,無須設定驗證。

    gcloud

    1. 安裝 Google Cloud CLI。 完成後,執行下列指令來初始化 Google Cloud CLI:

      gcloud init

      若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  • 設定預設地區和區域

瞭解建構程序

如要從頭安裝 Windows,一般做法是從包含 Windows 安裝檔案的 DVD 或 ISO 檔案啟動電腦。與某些地端部署管理程序不同,Compute Engine 不允許您從 ISO 檔案啟動。

因此,如要從頭安裝 Windows,必須採取不同做法,包括下列步驟:

  1. 建立新磁碟。
  2. 將 Windows 映像檔 (install.wim 安裝媒體) 解壓縮至磁碟。
  3. 新增必要驅動程式、設定 Windows 安裝程式,使其在無人看管的情況下執行,以及讓磁碟可啟動。
  4. 從新磁碟開機,執行 Windows 安裝程式。
  5. 安裝額外軟體,包括客體 OS 代理程式。
  6. 從磁碟建立映像檔。

本指南將說明如何使用 Cloud Builddaisy 工具和GitHub 上提供的參考工作流程,自動執行這些步驟,不必手動操作。

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

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

準備專案以建構映像檔

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

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

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

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

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

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

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

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    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 bucket。將檔案儲存在 Cloud Storage bucket 中,可確保 daisydaisy 用於建構映像檔的暫時 VM 執行個體都能存取檔案。

在下列步驟中,請將 UNIQUE_SUFFIX 替換為一小段隨機字元,建立不重複的 bucket 名稱。例如,使用 PROJECT_ID-media-a7g19。在值區名稱後方加上隨機後置字串,有助於防範值區搶註。這項安全漏洞可能導致攻擊者預先聲明名稱可預測的值區,進而掌控您的建構程序。

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

  2. 建立新的 Cloud Storage bucket 來儲存安裝檔案:

    gcloud storage buckets create gs://$PROJECT_ID-media-UNIQUE_SUFFIX --project=$PROJECT_ID
    
  3. Storage Object Viewer 角色授予 Cloud Build,讓 Cloud Build 可以讀取安裝檔案:

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

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

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

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

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

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

  6. 上傳 gcloud CLI 安裝套件:

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

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

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

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

    其中 ISO 是 ISO 檔案的名稱。

  8. (選用) 上傳其他更新套件:

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

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

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

建構映像檔

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

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

    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-UNIQUE_SUFFIX/updates/
        - -var:pwsh=gs://$PROJECT_ID-media-UNIQUE_SUFFIX/PowerShell.msi
        - -var:dotnet48=gs://$PROJECT_ID-media-UNIQUE_SUFFIX/dotnet-sdk.exe
        - -var:cloudsdk=gs://$PROJECT_ID-media-UNIQUE_SUFFIX/GoogleCloudSDKInstaller.exe
        - -var:media=gs://$PROJECT_ID-media-UNIQUE_SUFFIX/ISO
        - WORKFLOW
    

    更改項目:

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

    gcloud builds submit --project $PROJECT_ID --async
    

    建構作業最多需要 4 小時才會完成。您可以在 Google Cloud 控制台的「Cloud Build」>「記錄」下方,追蹤建構作業的狀態。

    Cloud Build 記錄

使用自訂圖片

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

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

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

疑難排解

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

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

後續步驟