使用機器映像檔,為 Compute Engine 中運作的 VM 執行個體儲存多個磁碟中的所有設定、中繼資料、權限和資料。用來建立機器映像檔的 VM 執行個體稱為「來源 VM 執行個體」。
如要瞭解使用機器映像檔的時機和方式,請參閱機器映像檔。
本文說明如何從來源 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.
- 每 60 分鐘最多可為特定執行個體建立 6 次機器映像檔。
- 您可以使用 Cloud Key Management Service (Cloud KMS) 或客戶提供的加密金鑰 (CSEK) 保護機器映像檔。加密金鑰可保護磁碟內容。但中繼資料不會受到保護。
- 如果您使用 CSEK 保護機器映像檔,系統會在產生機器映像檔時,儲存所有磁碟資料的完整副本。在所有其他情況下,磁碟資料會儲存在差異副本中。
您無法從下列來源 VM 建立機器映像檔:
- 已連結的磁碟大小超過 200 TB。
- 沒有任何附加磁碟。
- 已連結任何 Hyperdisk 磁碟區。
- 使用 C3D、H3、A3 或 Z3 機型。
disks.architecture
disks.description
disks.labels
disks.locked
disks.multiWriter
disks.onUpdateAction
disks.provisionedIops
disks.shieldedInstanceInitialState
disks.sourceImage
disks.sourceImageEncryptionKey
disks.sourceInstantSnapshot
disks.sourceSnapshot
disks.sourceSnapshotEncryptionKey
privateIpv6GoogleAccess
resourceManagerTags
resourcePolicies
secure_tag
shieldedInstanceConfig
- 要建立的機器映像檔名稱。
- 來源 VM 執行個體的名稱。
- 來源 VM 執行個體所在的可用區。
- 選填說明。
- 選用的
storageLocation
。 如果未指定位置,預設儲存位置為來源 VM 執行個體的多區域 Cloud Storage 位置。 - 選用的加密金鑰。您可以選擇 Google-owned and Google-managed encryption key、Cloud Key Management Service (Cloud KMS) 金鑰或客戶提供的加密 (CSEK) 金鑰。如未指定加密金鑰,系統會使用 Google-owned and Google-managed encryption key加密機器映像檔。
如要使用機器映像檔進行執行個體複製和快照,請先移除執行個體專屬的 OS 和應用程式資訊,再從執行個體產生機器映像檔。舉例來說,如果是 Windows VM 執行個體,請使用
GCESysprep
準備系統進行複製。MACHINE_IMAGE_NAME
:要建立的機器映像檔名稱。SOURCE_VM_NAME
:要建立映像檔的來源 VM 執行個體名稱。PROJECT_ID
: 專案 ID。MACHINE_IMAGE_NAME
:要建立的機器映像檔名稱。SOURCE_INSTANCE_URL
:您要用來建立機器映像檔的來源 VM 執行個體完整或部分網址。舉例來說,如果您在名為myProject
的專案中,有一個名為my-instance
的來源執行個體,下列網址有效:https://www.googleapis.com/compute/v1/projects/myProject/global/instances/my-instance
projects/myProject/global/instances/my-instance
global/instances/my-instance
- 進一步瞭解機器映像檔
- 從機器映像檔建立 VM 執行個體
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
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.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
限制和規定
機器映像檔不支援的執行個體和磁碟屬性
從 VM 執行個體建立機器映像檔,或從機器映像檔建立 VM 執行個體時,系統不會保留或還原下列執行個體和磁碟屬性:
從 VM 建立機器映像檔
您可以使用Google Cloud 控制台、Google Cloud CLI 或 REST 建立機器映像檔。
如要建立機器映像檔,請備妥下列資訊:
主控台
gcloud
使用
gcloud compute machine-images create
指令從執行個體建立機器映像檔。gcloud compute machine-images create MACHINE_IMAGE_NAME \ --source-instance=SOURCE_VM_NAME
更改下列內容:
示例
舉例來說,您可以使用下列
gcloud
指令,從名為my-instance
的來源執行個體建立名為my-machine-image
的機器映像檔:gcloud compute machine-images create my-machine-image \ --source-instance=my-instance
這個程序需要幾分鐘。建立機器映像檔後,您會看到類似以下的輸出內容:
Created [https://www.googleapis.com/compute/v1/projects/project-12345/global/machineImages/my-machine-image]. NAME STATUS my-machine-image READY
REST
在 API 中,建構對
machineimages.insert
方法的POST
要求。在要求主體中,加入下列POST
要求:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/machineImages { "name": "MACHINE_IMAGE_NAME", "sourceInstance": "SOURCE_INSTANCE_URL" }
更改下列內容:
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-11 (世界標準時間)。
-