使用现有的由 Azure Arc 启用的 Azure 本地虚拟机创建 Azure 本地虚拟机映像

适用于:Azure Local 2311.2 及更高版本

本文介绍如何使用 Azure Command-Line 接口(CLI)通过现有的 Azure 本地 VM 为 Azure 本地创建虚拟机(VM)映像。 你将使用 Azure Local VM 的操作系统 (OS) 磁盘在 Azure Local 上创建库映像。

先决条件

在开始之前,请确保:

从现有的 Azure 本地 VM 创建 VM 映像

从 Azure 本地 VM 的 OS 磁盘开始创建 VM 映像,然后使用此映像在 Azure 本地部署 VM。

按照以下步骤使用 Azure CLI 创建 VM 映像。

登录并设置订阅

  1. 连接到 Azure Local 上的计算机

  2. 登录。 类型:

    az login --use-device-code
    
  3. 设置订阅。

    az account set --subscription <Subscription ID>
    

设置一些参数

设置订阅、资源组、位置、本地共享中映像的路径,以及映像的 OS 类型。 请将 < > 中的参数替换为适当的值。

$subscription = "<Subscription ID>"
$resource_group = "<Resource group>"
$___location = "<Location for your Azure Local>"
$custom_location = "<Custom ___location for your Azure Local>"
$osType = "<OS of source VM>"
$imageName = "<VM image name>"
$sourceVmName = "<Name of source VM  in the Storage account>"

下表介绍了这些参数:

参数 说明
subscription 与此映像关联的 Azure Local 实例的订阅。
resource_group 与此映像关联的 Azure Local 实例的资源组。
___location 你的 Azure Local 的位置。 例如,它可以是 eastus
custom-___location Azure Local 的自定义位置 ID。
name 从本地共享中的映像开始创建的 VM 映像的名称。
注意:Azure 将拒绝所有包含关键字“Windows”的名称。
source-vm 用于创建 VM 映像的现有 Azure 本地 VM 的名称。
os-type 与源映像关联的操作系统。 这可以是 Windows 或 Linux。

下面是示例输出:

PS C:\Users\azcli> $subscription = "mysub-id"
PS C:\Users\azcli> $resource_group = "mylocal-rg"
PS C:\Users\azcli> $___location = "eastus"
PS C:\Users\azcli> $custom_location = "mylocal-cl"
PS C:\Users\azcli> $osType = "Windows"
PS C:\Users\azcli> $imageName = "mylocal-image"
PS C:\Users\azcli> $sourceVmName = "mysourcevm"

从 Azure 本地 VM 创建 VM 映像

从现有 VM 创建 VM 映像。 运行下面的命令:

az stack-hci-vm image create -resource-group $resource_group --___location $___location --custom-___location $custom_location --os-type $osType --source-vm $sourceVmName --name $imageName

将针对 VM 映像启动一个部署作业。 映像部署需要几分钟才能完成。

下面是示例输出:

{
  "extendedLocation": {
    "name": "/subscriptions/mysub-id/resourceGroups/mylocal-rg/providers/Microsoft.ExtendedLocation/customLocations/mylocal-cl",
    "type": "CustomLocation"
  },
  "id": "/subscriptions/mysub-id/resourceGroups/mylocal-rg/providers/Microsoft.AzureStackHCI/galleryImages/mylocal-image",
  "___location": "eastus",
  "name": "mylocal-image",
  "properties": {
    "cloudInitDataSource": null,
    "containerId": null,
    "hyperVGeneration": null,
    "identifier": null,
    "imagePath": null,
    "osType": "Windows",
    "provisioningState": "Succeeded",
    "sourceVirtualMachineId": "/subscriptions/mysub-id/resourceGroups/mylocal-rg/providers/Microsoft.HybridCompute/machines/mysourcevm/providers/Microsoft.AzureStackHCI/virtualMachineInstances/default",
    "status": {
      "downloadStatus": {
        "downloadSizeInMb": null
      },
      "errorCode": "",
      "errorMessage": "",
      "progressPercentage": null,
      "provisioningStatus": {
        "operationId": null,
        "status": null
      }
    },
    "version": {
      "name": null,
      "properties": {
        "storageProfile": {
          "osDiskImage": {
            "sizeInMb": null
          }
        }
      }
    }
  },
  "resourceGroup": "mylocal-rg",
  "systemData": {
    "createdAt": "2024-09-26T20:16:17.625002+00:00",
    "createdBy": "a5e473cb-e3e7-4035-b4da-290a65350ae1",
    "createdByType": "Application",
    "lastModifiedAt": "2024-09-26T20:16:17.625002+00:00",
    "lastModifiedBy": "a5e473cb-e3e7-4035-b4da-290a65350ae1",
    "lastModifiedByType": "Application"
  },
  "tags": null,
  "type": "microsoft.azurestackhci/galleryimages"
}

列出虚拟机镜像

需要查看 VM 映像列表才能选择要管理的映像。

按照以下步骤使用 Azure CLI 列出 VM 映像。

  1. 以管理员身份运行 PowerShell。

  2. 设置一些参数。

    $subscription = "<Subscription ID associated with your Azure Local>"
    $resource_group = "<Resource group name for your Azure Local>"
    
  3. 列出与 Azure 本地关联的所有 VM 映像。 运行下面的命令:

    az stack-hci-vm image list --subscription $subscription --resource-group $resource_group
    

    根据所使用的命令,会列出与 Azure Local 关联的相应映像集。

    • 如果你仅指定订阅,则该命令会列出该订阅中的所有映像。
    • 如果你同时指定订阅和资源组,则该命令会列出该资源组中的所有映像。

    这些映像包括:

    • 市场映像中的 VM 映像。
    • 驻留在 Azure 存储帐户中的自定义映像,或驻留在系统或连接到系统的客户端的本地共享中的自定义映像。

下面是示例输出。

PS C:\Users\azcli> az stack-hci-vm image list --subscription "<Subscription ID>" --resource-group "myhci-rg"
Command group 'stack-hci-vm' is experimental and under development. Reference and support levels: https://aka.ms/CLI_refstatus
[
  {
    "extendedLocation": {
      "name": "/subscriptions/<Subscription ID>/resourcegroups/myhci-rg/providers/microsoft.extendedlocation/customlocations/myhci-cl",
      "type": "CustomLocation"
    },
    "id": "/subscriptions/<Subscription ID>/resourceGroups/myhci-rg/providers/microsoft.azurestackhci/marketplacegalleryimages/w
inServer2022Az-01",
    "___location": "eastus",
    "name": "winServer2022Az-01",
    "properties": {
      "hyperVGeneration": "V2",
      "identifier": {
        "offer": "windowsserver",
        "publisher": "microsoftwindowsserver",
        "sku": "2022-datacenter-azure-edition-core"
      },
      "imagePath": null,
      "osType": "Windows",
      "provisioningState": "Succeeded",
      "status": {
        "downloadStatus": {
          "downloadSizeInMB": 6710
        },
        "progressPercentage": 100,
        "provisioningStatus": {
          "operationId": "19742d69-4a00-4086-8f17-4dc1f7ee6681*E1E9889F0D1840B93150BD74D428EAE483CB67B0904F9A198C161AD471F670ED",
          "status": "Succeeded"
        }
      },
      "storagepathId": null,
      "version": {
        "name": "20348.2031.231006",
        "properties": {
          "storageProfile": {
            "osDiskImage": {
              "sizeInMB": 130050
            }
          }
        }
      }
    },
    "resourceGroup": "mylocal-rg",
    "systemData": {
      "createdAt": "2023-10-30T21:44:53.020512+00:00",
      "createdBy": "guspinto@contoso.com",
      "createdByType": "User",
      "lastModifiedAt": "2023-10-30T22:08:25.495995+00:00",
      "lastModifiedBy": "319f651f-7ddb-4fc6-9857-7aef9250bd05",
      "lastModifiedByType": "Application"
    },
    "tags": {},
    "type": "microsoft.azurestackhci/marketplacegalleryimages"
  }
]
PS C:\Users\azcli>

有关此 CLI 命令的详细信息,请参阅 az stack-hci-vm image list

查看 VM 映像属性

在使用该映像创建 VM 之前,可能需要先查看 VM 映像的属性。 按照以下步骤查看映像属性:

按照以下步骤使用 Azure CLI 查看映像的属性:

  1. 以管理员身份运行 PowerShell。

  2. 设置以下参数。

    $subscription = "<Subscription ID>"
    $resource_group = "<Azure Local resource group>"
    $mktplaceImage = "<Marketplace image name>"
    
  3. 可以通过两种不同的方式查看映像属性:指定 ID,或指定名称和资源组。 指定市场映像 ID 时,请执行以下步骤:

    1. 设置以下参数。

      $mktplaceImageID = "/subscriptions/<Subscription ID>/resourceGroups/myhci-rg/providers/Microsoft.AzureStackHCI/galleryimages/mylocal-marketplaceimage"
      
    2. 运行以下命令以查看属性。

      az stack-hci-vm image show --ids $mktplaceImageID

      下面是此命令的示例输出:

      PS C:\Users\azcli> az stack-hci-vm image show --ids $mktplaceImageID
      Command group 'stack-hci-vm' is experimental and under development. Reference and support levels: https://aka.ms/CLI_refstatus
      {
        "extendedLocation": {
          "name": "/subscriptions/<Subscription ID>/resourcegroups/myhci-rg/providers/microsoft.extendedlocation/customlocations/mylocal-cl",
          "type": "CustomLocation"
        },
        "id": "/subscriptions/<Subscription ID>/resourceGroups/myhci-rg/providers/Microsoft.AzureStackHCI/galleryimages/mylocal-marketplaceimage",
        "___location": "eastus",
        "name": "mylocal-marketplaceimage",
        "properties": {
          "containerName": null,
          "hyperVGeneration": null,
          "identifier": null,
          "imagePath": null,
          "osType": "Windows",
          "provisioningState": "Succeeded",
          "status": null,
          "version": null
        },
        "resourceGroup": "mylocal-rg",
        "systemData": {
          "createdAt": "2022-08-05T20:52:38.579764+00:00",
          "createdBy": "guspinto@microsoft.com",
          "createdByType": "User",
          "lastModifiedAt": "2022-08-05T20:52:38.579764+00:00",
          "lastModifiedBy": "guspinto@microsoft.com",
          "lastModifiedByType": "User"
        },
        "tags": null,
        "type": "microsoft.azurestackhci/galleryimages"
      }
      PS C:\Users\azcli> 
      

删除 VM 映像

如果下载失败或不再需要 VM 映像,您可能需要删除 VM 映像。 按照以下步骤删除 VM 映像。

  1. 以管理员身份运行 PowerShell。

  2. 设置以下参数:

    $subscription = "<Subscription ID>"
    $resource_group = "<Azure Local resource group>"
    $mktplaceImage = "<Markeplace image name>"    
    
  3. 删除现有的 VM 映像。 运行下面的命令:

    az stack-hci-vm image delete --subscription $subscription --resource-group $resource_group --name $mktplaceImage --yes
    

可以通过两种方式删除映像:

  • 指定名称和资源组。
  • 指定 ID。

删除某个映像后,可以检查该映像是否已删除。 下面是通过指定名称和资源组删除映像后的示例输出。

PS C:\Users\azcli> $subscription = "<Subscription ID>"
PS C:\Users\azcli> $resource_group = "mylocal-rg"
PS C:\Users\azcli> $mktplaceImage = "mymylocal-marketplaceimage"
PS C:\Users\azcli> az stack-hci-vm image delete --name $mktplaceImage --resource-group $resource_group
Command group 'stack-hci-vm' is experimental and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Are you sure you want to perform this operation? (y/n): y
PS C:\Users\azcli> az stack-hci-vm image show --name $mktplaceImage --resource-group $resource_group
Command group 'stack-hci-vm' is experimental and under development. Reference and support levels: https://aka.ms/CLI_refstatus
ResourceNotFound: The Resource 'Microsoft.AzureStackHCI/marketplacegalleryimages/myhci-marketplaceimage' under resource group 'mylocal-rg' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix
PS C:\Users\azcli>

后续步骤