Windows 應用程式的安裝媒體通常會以 ISO 檔案的形式提供,但 Compute Engine 不允許您將 ISO 檔案當作虛擬 DVD 磁碟機,公開給 VM 執行個體。
如要在單一 Windows VM 上存取 ISO 檔案的內容,您可以執行下列任一操作:
將 ISO 檔案複製到 VM,並在本機掛載。如果您只需要存取單一 VM 執行個體上的 ISO 檔案內容,這個方法就很實用。
從 ISO 檔案建立永久磁碟,並以唯讀模式將磁碟連接至一或多個 VM 執行個體。如果有多個 VM 需要存取 ISO 檔案的內容,這種方法就很實用。
本文說明如何從 ISO 檔案建立永久磁碟,並以唯讀模式將磁碟連接至一或多個 VM。
事前準備
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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
-
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.
- Set a default region and zone.
-
準備 ISO 檔案
如果 ISO 檔案可透過 HTTP 公開取得,您就不需要先下載 ISO 檔案。如要使用本機 ISO 檔案,您可以將 ISO 檔案上傳至 Cloud Storage。
HTTP 網址
在 Google Cloud 控制台中,按一下「啟用 Cloud Shell」
按鈕,即可開啟 Cloud Shell。
建立下載網址的環境變數。網址可以是 HTTP 或 HTTPS 網址,但必須可匿名存取。
ISO_URL=https://example.com/big.iso
本機 ISO 檔案
在 Google Cloud 控制台中建立 Cloud Storage 值區。
-
視 ISO 檔案的大小而定,上傳作業可能需要數分鐘或數小時才能完成。
在「Object details」(物件詳細資料) 頁面上,複製物件的 URI。URI 開頭為
gs://
。按一下「Activate Cloud Shell」
按鈕,開啟 Cloud Shell。
建立下載網址的環境變數。將
URI
替換為您複製的 URI。ISO_URL=URI
建立包含 ISO 檔案內容的磁碟
如要將 ISO 檔案的內容複製到新磁碟,請建立臨時 VM,然後透過磁碟建立映像檔:
在 Cloud Shell 中,指定要指派給新磁碟的名稱:
DISK_NAME=iso
建立新磁碟,用於複製 ISO 檔案的內容:
gcloud compute disks create $DISK_NAME \ --size=10GB \ --zone=$(gcloud config get-value compute/zone)
如果 ISO 檔案大小超過 9 GB,請使用更大的磁碟大小。
為臨時 VM 建立開機指令碼。開機指令碼會執行下列動作:
- 使用 NTFS 檔案系統格式化次要磁碟。
- 從您指定的 HTTP 或 Cloud Storage 網址下載 ISO 檔案。
- 掛載 ISO 檔案,並將其內容複製到次要磁碟。
cat << "EOF" > startup.ps1 $DownloadDirectory = 'c:\download\' $ErrorActionPreference = 'Stop' $MetadataUrl = 'http://metadata.google.internal/computeMetadata/v1/instance' $DownloadUrl = (Invoke-RestMethod ` -Headers @{"Metadata-Flavor" = "Google"} ` -Uri "$MetadataUrl/attributes/iso") mkdir $DownloadDirectory\Source -Force Write-Host '== Formatting secondary disk... ===' -ForegroundColor Black -BackgroundColor Yellow Set-Disk -Number 1 -IsOffline $false Clear-Disk -Number 1 -RemoveData -Confirm:$false -ErrorAction SilentlyContinue Initialize-Disk -Number 1 -PartitionStyle MBR New-Partition -DiskNumber 1 -UseMaximumSize -DriveLetter D -IsActive | Format-Volume -FileSystem 'NTFS' -Confirm:$false Write-Host '== Downloading ISO... =============' -ForegroundColor Black -BackgroundColor Yellow if ($DownloadUrl.StartsWith('gs:')) { & gcloud storage cp $DownloadUrl "$DownloadDirectory\Source\image.iso" | Out-Default } else { Import-Module BitsTransfer Start-BitsTransfer -Source $DownloadUrl -Destination "$DownloadDirectory\Source\image.iso" } Write-Host '== Mounting ISO... ================' -ForegroundColor Black -BackgroundColor Yellow Mount-DiskImage -ImagePath "$DownloadDirectory\Source\image.iso" -StorageType ISO Write-Host '== Copying ISO contents... ========' -ForegroundColor Black -BackgroundColor Yellow Copy-Item 'e:\*' 'd:\' -Force -Recurse -PassThru ` | Where-Object { -Not $_.PSIsContainer } ` | Set-ItemProperty -Name IsReadOnly -Value $False Write-Host '== Completed. =====================' -ForegroundColor Black -BackgroundColor Yellow Invoke-RestMethod ` -Headers @{'Metadata-Flavor'='Google'} ` -Method PUT ` -Uri "$MetadataUrl/guest-attributes/vm/ready" ` -Body true EOF
建立使用開機指令碼和先前建立的磁碟的 Windows Server 2019 VM:
gcloud compute instances create iso-copier \ --machine-type=n1-standard-2 \ --image-family=windows-2019-core \ --image-project=windows-cloud \ --disk=name=$DISK_NAME,auto-delete=no \ --metadata=enable-guest-attributes=true,iso=$ISO_URL \ --metadata-from-file=windows-startup-script-ps1=startup.ps1 \ --scopes=https://www.googleapis.com/auth/devstorage.read_only
VM 需要約 2 分鐘才能啟動。視 ISO 檔案的大小而定,檔案複製作業可能需要 5 到 15 分鐘才能完成。您可以執行下列指令來觀察進度:
gcloud compute instances tail-serial-port-output iso-copier \ --zone=$(gcloud config get-value compute/zone)
等待 VM 執行完開機指令碼:
until gcloud compute instances get-guest-attributes iso-copier \ --zone=$(gcloud config get-value compute/zone) \ --query-path=vm/ready > /dev/null 2>&1 do sleep 5 && echo waiting for VM to finish... done
關閉及刪除 VM:
gcloud compute instances delete iso-copier \ --zone=$(gcloud config get-value compute/zone) \ --quiet
請注意,由於輔助磁碟是使用
auto-delete=no
參數掛載,因此不會遭到刪除。
磁碟現在已可使用。您可以在同一個區域內,將磁碟以唯讀模式連接至一或多個 VM 執行個體。
建立映像檔,跨區域和區域共用磁碟
如要在其他區域或地區使用 ISO 檔案的內容,請建立 Compute Engine 映像檔:
在 Cloud Shell 中,使用先前建立的磁碟建立映像檔:
gcloud compute images create $DISK_NAME \ --source-disk=$DISK_NAME \ --source-disk-zone=$(gcloud config get-value compute/zone)
清除所用資源
如要避免在完成此程序後繼續產生費用,您可以刪除已建立的資源:
刪除磁碟:
gcloud compute disks delete $DISK_NAME \ --zone=$(gcloud config get-value compute/zone) \ --quiet
刪除映像檔:
gcloud compute images delete $DISK_NAME
後續步驟
瞭解如何建立自訂圖片。
瞭解如何管理自訂映像檔的存取權。