建立自訂 Windows BYOL 映像檔

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

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

如要按照本指南完成操作,您需要:

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

事前準備

  • 如果尚未設定驗證,請先完成設定。驗證可確認您的身分,以便存取 Cloud de Confiance by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列其中一個選項,向 Compute Engine 進行驗證:

    請根據您打算如何使用本頁範例,選取對應的分頁標籤:

    控制台

    使用 Cloud de Confiance 控制台存取 Cloud de Confiance by S3NS 服務和 API 時,不需要設定驗證。

    gcloud

    1. 安裝 Google Cloud CLI,然後 使用聯合身分登入 gcloud CLI。登入後,執行下列指令來初始化 Google Cloud CLI:

      gcloud init
  • 設定預設區域和可用區。

瞭解建構程序

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

因此,如要從頭開始安裝 Windows,您必須採用不同的方法,其中包含下列步驟:

  1. 建立新磁碟。
  2. 將 Windows 映像檔 (安裝媒體中的 install.wim) 擷取至磁碟。
  3. 新增必要驅動程式、設定 Windows 安裝程式,使其自動執行,並讓磁碟可啟動。
  4. 從新磁碟開機,執行 Windows 安裝程式。
  5. 安裝額外軟體,包括客體 OS 代理程式。
  6. 從磁碟建立映像檔。

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

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

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

準備專案以建構圖片

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

  1. In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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

  2. Verify that billing is enabled for your Cloud de Confiance project.

  3. Enable the Compute Engine API, Cloud Build API, and Cloud Storage API APIs, if any are not already enabled.

    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 是您在前一節建立的Cloud de Confiance 專案專屬 ID。

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

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

Linux

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

    PROJECT_ID=PROJECT_ID
    

    其中 PROJECT_ID 是您在前一節建立的Cloud de Confiance 專案專屬 ID。

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

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

上傳安裝檔案

現在請收集自訂映像檔所需的所有安裝檔案,並上傳至 Cloud Storage bucket。將檔案儲存在 Cloud Storage bucket 中,可確保 daisy 和 daisy 用於建構映像檔的暫時 VM 執行個體都能存取檔案。

在下列步驟中,請將 UNIQUE_SUFFIX 替換成一小段隨機字元,建立不重複的 bucket 名稱。例如,使用 PROJECT_ID-media-a7g19。在 bucket 名稱後方附加隨機後置字串,有助於防止bucket 搶註。如果攻擊者預先聲明 bucket 名稱可預測,就可能控制您的建構程序。

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

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

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

    gcloud storage buckets add-iam-policy-binding gs://$PROJECT_ID-media-UNIQUE_SUFFIX --member=serviceAccount:$PROJECT_NUMBER@cloudbuild.s3ns-system.iam.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. 在本機電腦上,複製包含 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-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
    

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

    Cloud Build 記錄

使用自訂映像檔

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

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

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

疑難排解

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

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

後續步驟