Windows 應用程式的安裝媒體通常以 ISO 檔案的形式提供,但 Compute Engine 不允許您將 ISO 檔案公開為 VM 執行個體的虛擬 DVD 光碟機。
如要在單一 Windows VM 上存取 ISO 檔案內容,請執行下列任一操作:
將 ISO 檔案複製到 VM,然後在本機掛接。如果您只需要在單一 VM 執行個體上存取 ISO 檔案的內容,這個方法就非常實用。
從 ISO 檔案建立永久磁碟,然後以唯讀模式將磁碟連接至一或多個 VM 執行個體。如果多部 VM 需要存取 ISO 檔案的內容,這個方法就很實用。
本文說明如何從 ISO 檔案建立永久磁碟,並以唯讀模式將磁碟連接至一或多個 VM。
事前準備
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:
gcloud init
- Set a default region and zone.
準備 ISO 檔案
如果 ISO 檔案可透過 HTTP 公開存取,您就不必先下載 ISO 檔案。如要使用本機 ISO 檔案,請將 ISO 檔案上傳至 Cloud Storage。
HTTP 網址
在 Trusted Cloud 控制台中,按一下「啟用 Cloud Shell」
按鈕,開啟 Cloud Shell。
建立下載網址的環境變數。網址可以是 HTTP 或 HTTPS 網址,但必須可供匿名存取。
ISO_URL=https://example.com/big.iso
本機 ISO 檔案
在 Trusted Cloud 控制台中建立 Cloud Storage 值區。
-
視 ISO 檔案大小而定,上傳作業可能需要數分鐘至數小時才能完成。
在儲存空間瀏覽器中,瀏覽至上傳的物件。
在「Object details」(物件詳細資料) 頁面中,複製物件的 URI。URI 開頭為
gs://
。按一下「啟用 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
後續步驟
瞭解如何建立自訂映像檔。
瞭解如何管理自訂映像檔的存取權。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-08 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-08 (世界標準時間)。"],[[["Compute Engine cannot directly expose an ISO file as a virtual DVD drive to a VM instance, but provides two methods to access the ISO contents: locally mounting the file on a single VM or creating a Persistent Disk for read-only access by multiple VMs."],["To prepare an ISO file for use, it can either be accessed publicly via HTTP or uploaded to Cloud Storage if it's a local file."],["A temporary VM is used to copy the ISO file contents onto a new disk, which involves formatting the disk, downloading the ISO, mounting it, and copying its contents, handled by a startup script."],["After the contents are copied, the temporary VM can be shut down and deleted, and the new Persistent Disk can then be attached in read-only mode to one or more VM instances."],["To extend the availability of the ISO contents to other zones or regions, you can create a Compute Engine image from the disk."]]],[]] -