本文件說明如何在虛擬機器 (VM) 執行個體上啟用巢狀虛擬化功能,以及如何確認您可以建立巢狀虛擬機器。使用下列任一方法,在 VM 上啟用巢狀虛擬化:
建議做法:如要在新或現有 VM 上直接啟用巢狀虛擬化,請在建立 VM 時將
enableNestedVirtualization
欄位設為true
,或更新 VM。這是建議做法,因為您不需要建立自訂映像檔或使用特殊授權金鑰。使用特殊授權金鑰啟用巢狀虛擬化,方法是建立開機磁碟、使用特殊巢狀虛擬化授權金鑰建立自訂映像檔,然後建立使用自訂映像檔的 VM。
事前準備
-
如果尚未設定,請先設定驗證機制。「驗證」是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,以便對 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
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.
VM_NAME
:已啟用巢狀虛擬化的新 L1 VM 名稱ZONE
:新 L1 VM 的可用區,已啟用巢狀虛擬化PROJECT_ID
:專案 IDZONE
:新 L1 VM 的可用區,已啟用巢狀虛擬化VM_NAME
:已啟用巢狀虛擬化的新 L1 VM 名稱使用下列
gcloud compute instances export
指令匯出 VM 的屬性:gcloud compute instances export VM_NAME \ --destination=YAML_FILE_PATH \ --zone=ZONE
更改下列內容:
VM_NAME
:要匯出屬性的 VM 名稱YAML_FILE_PATH
:匯出設定資料時,用於儲存資料的 .yaml 檔案路徑和檔案名稱ZONE
:包含 VM 的可用區
在 FILE_PATH 中儲存的 VM 設定檔中,更新
enableNestedVirtualization
的值。如果檔案中沒有該值,請新增以下內容:advancedMachineFeatures: enableNestedVirtualization: true
使用下列
gcloud compute instances update-from-file
指令,更新 VM 的enableNestedVirtualization
值:gcloud compute instances update-from-file VM_NAME \ --source=FILE_PATH \ --most-disruptive-allowed-action=RESTART \ --zone=ZONE
更改下列內容:
VM_NAME
:要更新的 VM 名稱FILE_PATH
:更新的 VM 設定檔路徑ZONE
:包含要更新的 VM 的可用區
PROJECT_ID
:專案 IDZONE
:包含 VM 的可用區VM_NAME
:要匯出屬性的 VM 名稱從公用映像檔或自訂映像檔建立開機磁碟。以下範例將
debian-cloud
用於映像檔專案,將debian-10
用於映像檔系列。如果您已擁有含有現有磁碟的 VM 執行個體,可以略過這個步驟。gcloud
gcloud compute disks create DISK_NAME \ --zone=ZONE \ --image-project=debian-cloud \ --image-family=debian-10
更改下列內容:
DISK_NAME
:新磁碟的名稱ZONE
:建立磁碟的可用區
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks { ... "name": "DISK_NAME", "sourceImage": "projects/debian-cloud/global/images/family/debian-10", ... }
更改下列內容:
PROJECT_ID
:專案 IDZONE
:建立磁碟的可用區DISK_NAME
:新磁碟的名稱
使用巢狀虛擬化所需的特殊授權金鑰,建立自訂映像檔。
gcloud
gcloud compute images create IMAGE_NAME \ --source-disk DISK_NAME \ --source-disk-zone ZONE \ --licenses "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"
更改下列內容:
IMAGE_NAME
:新映像檔的名稱DISK_NAME
:先前建立的磁碟名稱ZONE
:要建立圖片的可用區
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/images { ... "licenses": ["projects/vm-options/global/licenses/enable-vmx"], "name": "IMAGE_NAME", "sourceDisk": "zones/ZONE/disks/DISK_NAME", ... }
更改下列內容:
PROJECT_ID
:專案 IDIMAGE_NAME
:新映像檔的名稱ZONE
:要建立圖片的可用區DISK_NAME
:先前建立的磁碟名稱
使用特殊授權建立映像檔後,可視需要刪除來源磁碟。
gcloud
gcloud compute disks delete DISK_NAME --zone=ZONE
更改下列內容:
DISK_NAME
:要刪除的磁碟名稱ZONE
:包含要刪除的磁碟的可用區
REST
DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME
更改下列內容:
PROJECT_ID
:專案 IDZONE
:包含要刪除的磁碟的可用區DISK_NAME
:要刪除的磁碟名稱
建立使用特殊授權的新映像檔的 VM。最低 CPU 平台必須為
"Intel Haswell"
。gcloud
gcloud compute instances create VM_NAME \ --zone=ZONE \ --min-cpu-platform "Intel Haswell" \ --image IMAGE_NAME
更改下列內容:
VM_NAME
:VM 名稱ZONE
:建立 VM 的可用區IMAGE_NAME
:先前建立的圖片名稱
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { ... "name": "VM_NAME", "minCpuPlatform": "Intel Haswell", "disks": [ { "initializeParams": { "sourceImage": "IMAGE_NAME" } } ] ... }
更改下列內容:
PROJECT_ID
:專案 IDVM_NAME
:VM 名稱ZONE
:建立 VM 的可用區IMAGE_NAME
:先前建立的圖片名稱
連結至 VM 執行個體。
gcloud compute ssh VM_NAME
將
VM_NAME
替換為要連線的 VM 名稱。確認已啟用巢狀虛擬化。除了
0
以外的任何回應,都表示已啟用巢狀虛擬化。grep -cw vmx /proc/cpuinfo
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 驗證說明文件中的「驗證以使用 REST」。
直接在新 VM 上啟用巢狀虛擬化功能
請按照下列程序,直接在 VM 上啟用巢狀虛擬化功能。
gcloud
使用下列
gcloud compute instances create
指令建立啟用巢狀虛擬化的 L1 VM:gcloud compute instances create VM_NAME \ --enable-nested-virtualization \ --zone=ZONE \ --min-cpu-platform="Intel Haswell"
更改下列內容:
REST
使用下列
instances.insert
方法建立啟用巢狀虛擬化的 L1 VM:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { ... "name": "VM_NAME", ... "minCpuPlatform": "Intel Haswell", "advancedMachineFeatures": { "enableNestedVirtualization": true }, ... }
更改下列內容:
直接在現有 VM 上啟用巢狀虛擬化
請按照下列程序,在現有 VM 上啟用巢狀虛擬化功能。
gcloud
REST
使用下列
instances.update
方法更新enableNestedVirtualization
的值:PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?most_disruptive_allowed_action=RESTART { ⋮ "advanced_machine_features": { ⋮ "enableNestedVirtualization": "true" }, ⋮ }
更改下列內容:
使用特殊授權金鑰啟用巢狀虛擬化功能
您可以使用特殊授權金鑰在 L1 VM 上啟用 VMX,藉此在 VM 上啟用巢狀虛擬化,授權金鑰不收取額外費用。
確認 VM 已啟用巢狀虛擬化
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-06-16 (世界標準時間)。
-