本文說明如何查看與虛擬機器 (VM) 執行個體相關聯的授權,將授權附加至 VM,以及查看授權更新記錄。如要進一步瞭解 Compute Engine 的授權,請參閱「關於授權」一文。
事前準備
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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.
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 相關聯的授權,請使用 Google Cloud 控制台、gcloud CLI 或 Compute Engine API。
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下 VM 名稱,查看相關授權。「Instance details」頁面隨即開啟。
在「Instance details」(執行個體詳細資料) 頁面的底部,按一下「Equivalent Code」(對等程式碼)。
查看開機磁碟的
licenses
欄位。
gcloud
執行下列
gcloud compute instances describe
指令:gcloud compute instances describe VM_NAME
將
VM_NAME
替換為您的 VM 名稱。查看輸出內容的
disks
區段。licenses
欄位會顯示與啟動磁碟相關聯的授權。
REST
呼叫下列
instances.get
v1 方法:GET https://compute.googleapis.com/compute/v1/projects/PROJECT/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT
:專案名稱ZONE
:包含 VM 的可用區VM_NAME
:VM 名稱
Compute Engine API 會在
licenses
欄位的disks
部分傳回與啟動磁碟相關聯的授權。
查看與 VM 相關聯的使用者授權
如要查看與 VM 相關聯的使用者授權,請使用 Google Cloud 控制台、gcloud CLI 或 Compute Engine API。
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下 VM 名稱,查看使用者授權。「Instance details」頁面隨即開啟。
在「Instance details」(執行個體詳細資料) 頁面的底部,按一下「Equivalent Code」(對等程式碼)。
查看開機磁碟的
userLicenses
欄位。
gcloud
執行下列
gcloud beta compute instances describe
指令:gcloud beta compute instances describe VM_NAME
將
VM_NAME
替換為您的 VM 名稱。查看輸出內容的
disks
區段。licenses
欄位會顯示與啟動磁碟相關聯的授權。
REST
呼叫以下
instances.get
beta 版方法:GET https://compute.googleapis.com/compute/beta/projects/PROJECT/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT
:專案名稱ZONE
:包含 VM 的可用區VM_NAME
:VM 名稱
Compute Engine API 會在
licenses
欄位的disks
部分傳回與啟動磁碟相關聯的授權。
匯入圖片時附加授權
如要在匯入圖片時附加授權,請使用下列 gcloud compute
images import
指令。
gcloud compute images import IMAGE_NAME \ --source-file=SOURCE_FILE --os=OS
更改下列內容:
IMAGE_NAME
:要建立的圖片名稱。SOURCE_FILE
:要匯入的虛擬磁碟的本機檔案或 Cloud Storage URI。OS
:要匯入的磁碟映像檔作業系統。系統會根據這個標記的值推斷作業系統的授權,因為匯入時沒有明確的標記可提供授權資訊。如需支援的值清單,請參閱--os
旗標。
如要進一步瞭解如何匯入映像檔,請參閱「匯入虛擬磁碟」。
在建立圖片時附加授權
如要在建立映像檔時附加授權,請使用下列 gcloud compute
images create
指令。
gcloud compute images create IMAGE_NAME \ --source-image=SOURCE_IMAGE \ --licenses=LICENSES
更改下列內容:
IMAGE_NAME
:要建立的圖片名稱。SOURCE_IMAGE
:要用來建立新圖片的圖片名稱。LICENSES
:以逗號分隔的授權字串清單。例如:"license1"
、"license2"
。
如要進一步瞭解如何建立映像檔,請參閱「建立、刪除及淘汰自訂映像檔」和「建立 Windows 映像檔」。
建立圖片時附加授權和使用者授權
如要在建立映像檔時附加授權和使用者授權,請使用下列 gcloud beta compute images create
指令。
gcloud beta compute images create IMAGE_NAME \ --source-image=SOURCE_IMAGE \ --licenses=LICENSES \ --user-licenses=USER_LICENSES
更改下列內容:
IMAGE_NAME
:要建立的圖片名稱。SOURCE_IMAGE
:要用來建立新圖片的圖片名稱。LICENSES
:以逗號分隔的授權字串清單。例如:"license1"
、"license2"
。USER_LICENSES
:以逗號分隔的使用者授權字串清單。例如:"userlicense1"
、"userlicense2"
。
如要進一步瞭解如何建立映像檔,請參閱「建立、刪除及淘汰自訂映像檔」和「建立 Windows 映像檔」。
附加及驗證使用者授權
如要附加及驗證使用者授權,請按照下列步驟操作:
使用
gcloud compute instances describe
指令列出與 VM 相關聯的磁碟:gcloud compute instances describe VM_NAME --format="yaml(disks)"
確認輸出結果是否類似下列內容:
disks: - autoDelete: true boot: true deviceName: BOOT_DISK_NAME diskSizeGb: '20' guestOsFeatures: - type: UEFI_COMPATIBLE index: 0 interface: SCSI kind: compute#attachedDisk licenses: - BOOT_DISK_LICENSE_STRING mode: READ_WRITE source: https://www.googleapis.com/compute/v1/projects/PROJECT_NAME/zones/ZONE/disks/BOOT_DISK_NAME type: PERSISTENT
在先前步驟的輸出內容中,找出開機磁碟的名稱。對於開機磁碟,
boot
的值為true
,開機磁碟的名稱則為deviceName
的值。使用
gcloud beta compute disks update
指令,將使用者授權附加至開機磁碟:gcloud beta compute disks update BOOT_DISK_NAME \ --update-user-licenses=LICENSE_URIS
更改下列內容:
BOOT_DISK_NAME
:要附加授權的開機磁碟名稱LICENSE_URIS
:以半形逗號分隔的授權字串清單,可取代所有現有的使用者授權
使用
gcloud beta compute disks describe
指令取得開機磁碟相關資訊:gcloud beta compute disks describe BOOT_DISK_NAME
將
BOOT_DISK_NAME
替換為啟動磁碟的名稱,以便檢查附加的授權。確認輸出結果是否類似下列內容:
creationTimestamp: '2021-05-05T15:26:27.835-07:00' guestOsFeatures: - type: UEFI_COMPATIBLE id: '727854272460268924' interface: SCSI kind: compute#disk labelFingerprint: 42WmSpB8rSM= lastAttachTimestamp: '2021-05-05T15:26:27.836-07:00' licenseCodes: - '1000006' - '6213885950785916969' licenses: - BOOT_DISK_LICENSE_STRING multiWriter: false name: VM_NAME physicalBlockSizeBytes: '4096' selfLink: https://www.googleapis.com/compute/beta/projects/PROJECT_NAME/zones/ZONE/disks/DISK_NAME selfLinkWithId: https://www.googleapis.com/compute/beta/projects/PROJECT_NAME/zones/ZONE/disks/727854272460268924 sizeGb: '20' sourceImage: https://www.googleapis.com/compute/beta/projects/IMAGE_PROJECT/global/images/IMAGE sourceImageId: '7397991294075835597' status: READY type: https://www.googleapis.com/compute/beta/projects/PROJECT_NAME/zones/ZONE/diskTypes/pd-standard userLicenses: - LICENSE_URI users: - https://www.googleapis.com/compute/beta/projects/PROJECT_NAME/zones/ZONE/instances/VM_NAME zone: https://www.googleapis.com/compute/beta/projects/PROJECT_NAME/zones/ZONE
檢查上一個步驟的輸出內容,確認
userLicenses
欄位是否有適當的授權字串,藉此驗證使用者授權是否已附加至啟動磁碟。如果
userLicenses
欄位未包含所需的授權字串,您可能需要清除授權字串清單,然後再更新。您可以使用gcloud beta compute disks update
指令執行這項操作。
查看授權更新記錄
如要查看專案中 VM 授權更新記錄,請按照下列程序操作:
前往 Google Cloud 控制台中的「活動」頁面。
在「類別」的「篩選器」窗格中,針對「活動類型」選取「設定」。
在「Categories」的「Filters」窗格中,針對「Resource type」選取「Disk」。