本文件說明如何在虛擬機器 (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.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the 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.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
直接在新 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"
更改下列內容:
VM_NAME
:已啟用巢狀虛擬化的新 L1 VM 名稱ZONE
:啟用巢狀虛擬化的新 L1 VM 適用的可用區
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 }, ... }
更改下列內容:
PROJECT_ID
:專案 IDZONE
:啟用巢狀虛擬化的新 L1 VM 適用的可用區VM_NAME
:已啟用巢狀虛擬化的新 L1 VM 名稱
直接在現有 VM 上啟用巢狀虛擬化
請按照下列程序,在現有 VM 上啟用巢狀虛擬化功能。
gcloud
使用下列
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 的可用區
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" }, ⋮ }
更改下列內容:
PROJECT_ID
:專案 IDZONE
:包含 VM 的可用區VM_NAME
:要匯出屬性的 VM 名稱
使用特殊授權金鑰啟用巢狀虛擬化功能
您可以使用特殊授權金鑰在 L1 VM 上啟用 VMX,藉此在 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 已啟用巢狀虛擬化
連結至 VM 執行個體。
gcloud compute ssh VM_NAME
將
VM_NAME
替換為要連線的 VM 名稱。確認已啟用巢狀虛擬化。除了
0
以外的任何回應,都表示已啟用巢狀虛擬化。grep -cw vmx /proc/cpuinfo