從一般化映像版本建立 VM

從儲存在 Azure Compute Gallery (先前稱為共用映像庫) 中的一般化映像版本建立 VM。 如果您想要使用特殊化映像建立 VM,請參閱從特殊化映像建立 VM

本文說明如何從一般化映像建立 VM:

使用 az sig image-definition list 來列出資源庫中的映像定義,以查看定義的名稱和識別碼。

resourceGroup=myGalleryRG
gallery=myGallery
az sig image-definition list --resource-group $resourceGroup --gallery-name $gallery --query "[].[name, id]" --output tsv

使用 az vm create 建立 VM。 若要使用最新版本的映像,請將 --image 設定為映像定義的識別碼。

此範例用於建立使用 SSH 保護的 Linux VM。 若要 Windows 或使用密碼保護 Linux VM,請移除 --generate-ssh-keys 以提示輸入密碼。 如果您想要直接提供密碼,請將 --generate-ssh-keys 取代為 --admin-password。 視需要取代此範例中的資源名稱。

imgDef="/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition"
vmResourceGroup=myResourceGroup
location=eastus
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

您也可以使用 --image 參數的映像版本識別碼來使用特定版本。 例如,若要使用映像 1.0.0 版類型:--image "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0"

RBAC – 在組織內共用

如果資源庫所在的訂用帳戶位於相同的租用戶,則可使用透過 RBAC 共用的映像,透過 CLI 和 PowerShell 建立 VM。

您需要將要使用之映像的 imageID,並且需要確保將其複寫到要建立 VM 的區域。

請確認映像的狀態為 Generalized。 如果您想要使用狀態為 Specialized 的映像,請參閱從特製化映像版本建立 VM

imgDef="/SharedGalleries/1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f-MYDIRECTSHARED/Images/myDirectDefinition/Versions/latest"
vmResourceGroup=myResourceGroup
location=westus
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

RBAC - 從另一個租用戶共用

如果所要使用的映像儲存在非相同租用戶 (目錄) 的資源庫,則您必須登入每個租用戶以驗證您具有存取權。

您也需要將要使用之映像的 imageID,並且需要確保將其複寫到要建立 VM 的區域。 您也需要來源資源庫的 tenantID 和所要建立 VM 的 tenantID

在此範例中,我們會示範如何從一般化映像建立 VM。 如果您想要使用特製化映像,請參閱使用特製化映像版本建立 VM

您必須登入儲存映像的租用戶,取得存取權丈,然後登入所要建立 VM 的租用戶。 這是 Azure 驗證您具有映像存取權的方式。


tenant1='<ID for tenant 1>'
tenant2='<ID for tenant 2>'

az account clear
az login --tenant $tenant1
az account get-access-token 
az login --tenant $tenant2
az account get-access-token
az login --tenant $tenant1
az account get-access-token

建立 VM。 以您自己的資訊取代範例中的資訊。 建立 VM 前,請確保映像複寫到所要建立 VM 的區域。

imageid="<ID of the image that you want to use>"
resourcegroup="<name for the resource group>"
location="<location where the image is replicated>"
user='<username for the VM>'
name='<name for the VM>'

az group create --location $location --resource-group $resourcegroup
az vm create \
  --resource-group $resourcegroup \
  --name $name \
  --image $imageid \
  --admin-username $user \
  --generate-ssh-keys

重要

Microsoft 不支援社群資源庫中的映像。

報告社群映像的問題

使用社群提交的虛擬機器映像有數個風險。 映像可能包含惡意程式碼、安全性弱點或違反某人智慧財產權。 為了協助建立安全可靠的社群體驗,您可在發現這些問題時報告映像。

報告社群資源庫問題的最簡單方式是使用入口網站,這會預設填入報告的諮詢:

  • 針對連結問題或映像定義中欄位的其他資訊,請選取 [報告社群映像]
  • 如果映像版本包含惡意程式碼或特定映像版本發生其他問題,請在映像版本表格的 [報告版本] 資料行下選取 [報告]

您也可使用下列連結以報告問題,但不會預先填入表單:

若要使用共用至社群資源庫的映像建立 VM,請使用映像 --image 的唯一識別碼,其格式如下:

/CommunityGalleries/<community gallery name, like: ContosoImages-1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f>/Images/<image name>/Versions/latest

請依照下列指示來取得使用 CLI 的社群映像清單:

Step 1:  Show all 'Community images' in a specific location
az sig list-community --location westus2

Step 2: Once you have the public gallery name from Step 1, Get the Image definition (Name) of the image by running the following command
az sig image-definition list-community --public-gallery-name <<public gallery name>> --location westus2

Step 3: Finally, run the following command to list different image versions available for the specific image
az sig image-version list-community --public-gallery-name <<galleryname>> --gallery-image-definition <<image name>> --location westus2

若要從入口網站取得社群資源庫的公用名稱。 依序移至 [虛擬機器]>[建立]>[Azure 虛擬機器]>[映像]>[查看所有映像]>[社群映像]>[公開資源庫名稱]

在此範例中,我們會從 Linux 映像建立 VM,並建立 SSH 金鑰以進行驗證。

imgDef="/CommunityGalleries/ContosoImages-1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f>/Images/myLinuxImage/Versions/latest"
vmResourceGroup=myResourceGroup
location=eastus
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

使用社群映像時,系統會提示您接受法律條款。 該訊息如下所示:

To create the VM from community gallery image, you must accept the license agreement and privacy statement: http://contoso.com. (If you want to accept the legal terms by default, please use the option '--accept-term' when creating VM/VMSS) (Y/n): 

重要

Azure Compute Gallery - 直接共用資源庫目前為「預覽」,並遵守 Azure Compute Gallery 的預覽條款

若要在預覽期間將映像發佈至直接共用資源庫,您必須在 https://aka.ms/directsharedgallery-preview 註冊。 所有 Azure 使用者都可以從直接共用資源庫建立 VM。

在預覽期間,您必須建立新的資源庫,並將屬性 sharingProfile.permissions 設定為 Groups。 使用 CLI 建立資源庫時,請使用 --permissions groups 參數。 您無法使用現有的資源庫,目前無法更新此屬性。

若要使用共用至訂用帳戶或租用戶的映像來建立 VM,您需要下列格式的映像唯一識別碼:

/SharedGalleries/<uniqueID>/Images/<image name>/Versions/latest

若要尋找與您共用的資源庫的 uniqueID,請使用 az sig list-shared。 在此範例中,我們在美國西部區域尋找資源庫。

region=westus
az sig list-shared --location $region --query "[].name" -o tsv

使用資源庫名稱來尋找可用的映像。 在此範例中,我們列出「美國西部」中的所有映像,並依名稱列出建立 VM、OS 和 OS 狀態所需的唯一識別碼。

galleryName="1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f-myDirectShared"
 az sig image-definition list-shared \
   --gallery-unique-name $galleryName \
   --location $region \
   --query [*]."{Name:name,ID:uniqueId,OS:osType,State:osState}" -o table

請確認映像的狀態為 Generalized。 如果您想要使用狀態為 Specialized 的映像,請參閱從特製化映像版本建立 VM

使用來自輸出的 Id 並附加 /Versions/latest,以使用最新版本作為 --image 的值,以利建立 VM。 在此範例中,我們會從直接和我們共用的 Linux 映像建立 VM,並建立 SSH 金鑰以進行驗證。

imgDef="/SharedGalleries/1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f-MYDIRECTSHARED/Images/myDirectDefinition/Versions/latest"
vmResourceGroup=myResourceGroup
location=westus
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

下一步