在 VM 上啟用虛擬顯示器


本文說明如何在虛擬機器 (VM) 執行個體上啟用或停用虛擬顯示器。

如果在 VM 上執行的應用程式需要顯示裝置,但您不需要 GPU 的效能,請將 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

    1. 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.

    2. Set a default region and zone.

    Terraform

    To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.

    1. Install the Google Cloud CLI.
    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    For more information, see Set up authentication for a local development environment.

    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 上虛擬螢幕所需的權限,請要求管理員為您授予專案的 Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這個預先定義的角色包含在 VM 上啟用或停用虛擬顯示器所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

如要啟用或停用 VM 上的虛擬顯示裝置,您必須具備下列權限:

  • 如要在現有 VM 上啟用或停用虛擬顯示器,請執行下列操作: compute.instances.updateDisplayDevice 在 VM 上
  • 如要建立已啟用虛擬顯示器的 VM,請按照下列步驟操作:
    • 專案的 compute.instances.create 權限
    • 如何使用自訂映像檔在映像檔上建立 VM: compute.images.useReadOnly
    • 如何使用快照建立 VM: 在快照上執行 compute.snapshots.useReadOnly
    • 如何使用執行個體範本在執行個體範本上建立 VM: compute.instanceTemplates.useReadOnly
    • 如要將舊版網路指派給專案中的 VM: compute.networks.use
    • 如要為專案中的 VM 指定靜態 IP 位址:compute.addresses.use
    • 如要在使用舊版網路時將外部 IP 位址指派給 VM,請在專案中使用 compute.networks.useExternalIp
    • 如要為 VM 指定子網路,請在專案或所選子網路中使用 compute.subnetworks.use
    • 如要在使用虛擬私有雲網路時將外部 IP 位址指派給 VM,請在專案或所選子網路上設定 compute.subnetworks.useExternalIp
    • 如何為專案中的 VM: compute.instances.setMetadata 設定 VM 執行個體中繼資料
    • 如何為 VM 設定標記:compute.instances.setTags
    • 如何為 VM 設定標籤:compute.instances.setLabels
    • 如何設定 VM 要使用的服務帳戶: 在 VM 上使用 compute.instances.setServiceAccount
    • 如要為專案中的 VM compute.disks.create 建立新磁碟
    • 如要以唯讀或讀寫模式連接現有磁碟:磁碟的 compute.disks.use
    • 如要以唯讀模式連接現有磁碟:磁碟上的 compute.disks.useReadOnly

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

定價

在 VM 上啟用或停用虛擬顯示器不會產生任何費用。

限制

針對已啟用虛擬螢幕的 VM,適用下列限制:

  • 如果您的 VM 執行的是比 v20190312 版本舊的 x64 架構 Windows 作業系統映像檔,則在 VM 上啟用虛擬顯示器後,您必須按照本文件所述安裝虛擬顯示驅動程式。如果 VM 執行的是較新的 OS 映像檔版本,則驅動程式已安裝在 OS 映像檔中。

  • 只有在客體作業系統啟動並初始化虛擬顯示器驅動程式後,您才能在 VM 上使用虛擬顯示裝置。

  • 您無法在執行 Sandy Bridge CPU 平台的 VM 上使用虛擬顯示裝置。

  • 您無法在 T2A Arm 虛擬機器上使用虛擬顯示裝置。

在 VM 上啟用虛擬顯示器

如要在 VM 上啟用虛擬顯示器,請選取本文所述的下列任一方法:

如果您的 VM 執行的是比 v20190312 版本舊的 x64 架構 Windows 作業系統映像檔,請在 VM 上啟用虛擬顯示器後,按照本文件所述安裝虛擬顯示驅動程式

建立啟用虛擬顯示器的 VM

如要建立啟用虛擬螢幕的 VM,請選取下列任一選項:

主控台

  1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

    前往「Create an instance」(建立執行個體)

  2. 指定 VM 的屬性,包括名稱、區域和機器類型。

  3. 在「顯示裝置」部分,勾選「啟用顯示裝置」核取方塊。

  4. 如要建立 VM,請按一下「建立」

gcloud

如要建立啟用虛擬顯示器的 VM,請使用 gcloud compute instances create 指令搭配 --enable-display-device 旗標。

gcloud compute instances create VM_NAME \
    --enable-display-device \
    --machine-type=MACHINE_TYPE \
    --zone=ZONE

更改下列內容:

  • VM_NAME:VM 名稱。

  • MACHINE_TYPE:VM 使用的機器類型。

  • ZONE:建立 VM 的區域。

Terraform

如要建立已啟用虛擬螢幕的 VM,請使用 Terraform 資源,並將 enable_display 引數設為 true

舉例來說,如要在可用區 us-central1-c 中建立已啟用虛擬螢幕的 VM,並指定 f1-micro 做為機器類型,請使用下列資源:


resource "google_compute_instance" "instance_virtual_display" {
  name         = "instance-virtual-display"
  machine_type = "f1-micro"
  zone         = "us-central1-c"

  # Set the below to true to enable virtual display
  enable_display = true

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
  network_interface {
    # A default network is created for all GCP projects
    network = "default"
    access_config {
    }
  }
}

REST

如要建立啟用虛擬顯示裝置的 VM,請對 instances.insert 方法發出 POST 要求。在要求主體中,請將 enableDisplay 欄位設為 true

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances

{
  "name": "VM_NAME",
  "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
  "disks": [
    {
      "boot": true,
      "initializeParams": {
        "sourceImage": "project/IMAGE_PROJECT/global/images/IMAGE"
      }
    }
  ],
  "displayDevice": {
    "enableDisplay": true
  },
  "networkInterfaces": [
    {
      "network": "global/networks/default"
    }
  ]
}

更改下列內容:

  • PROJECT_ID:要建立 VM 的專案 ID。

  • ZONE:建立 VM 的區域。

  • VM_NAME:VM 名稱。

  • MACHINE_TYPE:VM 使用的機器類型。

  • IMAGE_PROJECT:包含 OS 映像檔的映像檔專案,例如 debian-cloud。如要進一步瞭解支援的圖像專案,請參閱「公開圖像」。

  • IMAGE:指定下列其中一個值:

    • 特定 OS 映像檔版本,例如 debian-12-bookworm-v20240617

    • 映像檔系列,其格式必須為 family/IMAGE_FAMILY。這會指定最新的非淘汰作業系統映像檔。舉例來說,如果您指定 family/debian-12,系統會使用 Debian 12 映像檔系列中的最新版本。如要進一步瞭解如何使用映像檔系列,請參閱「映像檔系列最佳做法」。

如要進一步瞭解如何建立 VM,請參閱「建立及啟動 Compute Engine 執行個體」一文。

在現有 VM 上啟用虛擬顯示器

在 VM 上啟用虛擬顯示器前,請務必停止 VM

如要在現有 VM 上啟用虛擬顯示器,請選取下列任一選項:

主控台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體頁面

  2. 在「Name」欄中,按一下 VM 名稱。

    VM 的詳細資料頁面隨即開啟。

  3. 按一下「Edit」(編輯)

    系統會開啟可編輯 VM 屬性的頁面。

  4. 在「顯示裝置」部分,勾選「啟用顯示裝置」核取方塊。

  5. 按一下 [儲存]

gcloud

如要在現有 VM 上啟用虛擬顯示裝置,請使用 gcloud compute instances update 指令搭配 --enable-display-device 旗標。

gcloud compute instances update VM_NAME \
    --enable-display-device \
    --zone=ZONE

更改下列內容:

  • VM_NAME:VM 名稱。

  • ZONE:VM 所在區域。

REST

如要在現有 VM 上啟用虛擬顯示器,請對 instances.updateDisplayDevice 方法發出 POST 要求。在要求主體中加入 enableDisplay 欄位,並將該欄位設為 true

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/updateDisplayDevice

{
  "enableDisplay": true
}

更改下列內容:

  • PROJECT_ID:VM 所在專案的 ID。

  • ZONE:VM 所在區域。

  • VM_NAME:VM 名稱。

在現有 VM 上停用虛擬顯示器

在 VM 上停用虛擬顯示器前,請務必停止 VM

如要在現有 VM 上停用虛擬顯示器,請選取下列任一選項:

主控台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體頁面

  2. 在「Name」欄中,按一下 VM 名稱。

    VM 的詳細資料頁面隨即開啟。

  3. 按一下「Edit」(編輯)

    系統會開啟可編輯 VM 屬性的頁面。

  4. 在「顯示裝置」部分,清除「啟用顯示裝置」核取方塊。

  5. 按一下 [儲存]

gcloud

如要在現有 VM 上停用虛擬顯示裝置,請使用 gcloud compute instances update 指令搭配 --no-enable-display-device 旗標。

gcloud compute instances update VM_NAME \
    --no-enable-display-device \
    --zone=ZONE

更改下列內容:

  • VM_NAME:VM 名稱。

  • ZONE:VM 所在區域。

REST

如要停用現有 VM 上的虛擬顯示器,請對 instances.updateDisplayDevice 方法發出 POST 要求。在要求主體中加入 enableDisplay 欄位,並將該欄位設為 false

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/updateDisplayDevice

{
  "enableDisplay": false
}

更改下列內容:

  • PROJECT_ID:VM 所在專案的 ID。

  • ZONE:VM 所在區域。

  • VM_NAME:VM 名稱。

安裝虛擬顯示驅動程式

如果您在執行比 v20190312 舊的 Windows 作業系統映像檔的 Windows VM 上啟用虛擬顯示裝置,則如要在 VM 上使用虛擬顯示裝置,您必須安裝 Google Cloud提供的虛擬顯示驅動程式。如果 VM 執行的是較新的 OS 映像檔版本,則表示驅動程式已安裝,您可以略過本節。

如要在已啟用虛擬顯示器的 VM 上安裝虛擬顯示器驅動程式,請執行下列操作:

  1. 連線至 Windows VM

  2. 以系統管理員身分開啟 PowerShell 終端機。

  3. 安裝 google-compute-engine-driver-gga 元件:

    googet install google-compute-engine-driver-gga
    
  4. 重新啟動 VM

重新啟動 VM 後,您可以確認驅動程式是否已正確安裝,如下一節所述。

確認虛擬顯示驅動程式安裝作業

如果您需要在 Windows VM 上手動安裝虛擬顯示驅動程式,請按照上一節所述操作,驗證安裝是否成功:

  1. 如果尚未連線,請連線至 Windows VM

  2. 開啟裝置管理工具。

  3. 在「裝置管理員」清單的「顯示卡」清單中,確認 Google Graphics Array (GGA) 驅動程式是否列在清單中。

    如果沒有列出驅動程式,請按照本文件說明重新安裝虛擬顯示驅動程式

後續步驟