将自定义 VM 映像添加到 Azure Stack HubAdd a custom VM image to Azure Stack Hub

在 Azure Stack Hub 中,可将虚拟机 (VM) 自定义映像添加到市场供用户使用。In Azure Stack Hub, you can add your custom virtual machine (VM) image to the marketplace and make it available to your users. 可以通过管理员门户或 Windows PowerShell 将 VM 映像添加到 Azure Stack Hub 市场。You can add VM images to the Azure Stack Hub Marketplace through the administrator portal or Windows PowerShell. 使用 Azure 市场中的映像作为自定义映像的基础,或使用 Hyper-V 创建自己的映像。Use either an image from the Azure Marketplace as a base for your custom image, or your create your own using Hyper-V.

步骤 1:创建自定义 VM 映像Step 1: Create the custom VM image

WindowsWindows

创建自定义的通用化 VHD。Create a custom generalized VHD.

如果 VHD 来自 Azure 外部,请遵循上传通用化 VHD 并使用它在 Azure 中创建新 VM 中的步骤,正确地对 VHD 执行 Sysprep 并使其通用化。If the VHD is from outside Azure, follow the steps in Upload a generalized VHD and use it to create new VMs in Azure to correctly Sysprep your VHD and make it generalized.

如果 VHD 来自 Azure,请在通用化 VM 之前确保:If the VHD is from Azure, prior to generalizing the VM, make sure of the following:

  • 在 Azure 上预配 VM 时,使用 PowerShell 并在不使用 -ProvisionVMAgent 标志的情况下对其进行预配。When you provision the VM on Azure, use PowerShell and provision it without the -ProvisionVMAgent flag.

  • 通用化 Azure 中的 VM 之前,先在该 VM 中使用 Remove-AzureRmVMExtension cmdlet 删除所有 VM 扩展。Remove all VM extensions using the Remove-AzureRmVMExtension cmdlet from the VM before generalizing the VM in Azure. 可以转到 Windows (C:) > WindowsAzure > Logs > Plugins 查找已安装的 VM 扩展。You can find which VM extensions are installed by going to Windows (C:) > WindowsAzure > Logs > Plugins.

    Remove-AzureRmVMExtension -ResourceGroupName winvmrg1 -VMName windowsvm -Name "CustomScriptExtension"
    

按照此文中的说明正确通用化并下载 VHD,然后将其移植到 Azure Stack Hub。Follow the instructions in this article to correctly generalize and download the VHD before porting it to Azure Stack Hub.

LinuxLinux

如果 VHD 来自 Azure 外部,请根据相应的说明将 VHD 通用化:If the VHD is from outside Azure, follow the appropriate instructions to generalize the VHD:

如果 VHD 来自 Azure,请按照以下说明通用化并下载 VHD:If the VHD is from Azure, follow these instructions to generalize and download the VHD:

  1. 停止 waagent 服务:Stop the waagent service:

    sudo waagent -force -deprovision
    export HISTSIZE=0
    logout
    

    适用于 Azure Stack Hub 的 Azure Linux 代理版本在此处记述The Azure Linux Agent versions that work with Azure Stack Hub are documented here. 确保已执行 Sysprep 的映像包含与 Azure Stack Hub 兼容的 Azure Linux 代理版本。Make sure that the sysprepped image has an Azure Linux agent version that is compatible with Azure Stack Hub.

  2. 停止/解除分配 VM。Stop deallocate the VM.

  3. 下载 VHD。Download the VHD.

    1. 若要下载 VHD 文件,请生成共享访问签名 (SAS) URL。To download the VHD file, generate a shared access signature (SAS) URL. 生成 URL 时,将为 URL 分配到期时间。When the URL is generated, an expiration time is assigned to the URL.

    2. 在 VM 的边栏选项卡菜单中选择“磁盘”。On the menu of the blade for the VM, select Disks.

    3. 为 VM 选择操作系统磁盘,然后选择“磁盘导出”。Select the operating system disk for the VM, and then select Disk Export.

    4. 将 URL 的过期时间设置为 36000。Set the expiration time of the URL to 36000.

    5. 选择“生成 URL”。Select Generate URL.

    6. 生成 URL。Generate the URL.

    7. 在生成的 URL 下,选择“下载 VHD 文件”。Under the URL that was generated, select Download the VHD file.

    8. 可能需要选择浏览器中的“保存”才能开始下载。You might need to select Save in the browser to start the download. VHD 文件的默认名称为 abcdThe default name for the VHD file is abcd.

注意事项Considerations

在上传映像之前,必须考虑以下因素:Before you upload the image, it's important to consider the following:

  • Azure Stack Hub 仅支持采用固定磁盘 VHD 格式的第 1 代 VM。Azure Stack Hub only supports generation 1 VMs in the fixed disk VHD format. 固定格式在文件中将逻辑磁盘以线性方式布局,使磁盘偏移量 X 存储在 Blob 偏移量 X 的位置。在 Blob 末尾有一小段脚注,描述了 VHD 的属性。The fixed-format structures the logical disk linearly within the file, so that disk offset X is stored at blob offset X. A small footer at the end of the blob describes the properties of the VHD. 若要确认磁盘是否为固定格式,请使用 Get-VHD PowerShell cmdlet。To confirm if your disk is fixed, use the Get-VHD PowerShell cmdlet.

  • Azure Stack Hub 不支持动态磁盘 VHD。Azure Stack Hub does not support dynamic disk VHDs.

步骤 2:将 VM 映像上传到存储帐户Step 2: Upload the VM image to a storage account

  1. 安装适用于 Azure Stack Hub 的 PowerShellInstall PowerShell for Azure Stack Hub.

  2. 以操作员身份登录到 Azure Stack Hub。Sign in to Azure Stack Hub as an operator. 有关说明,请参阅以操作员身份登录到 Azure Stack HubFor instructions, see Sign in to Azure Stack Hub as an operator.

  3. 映像必须通过 Blob 存储 URI 进行引用。Images must be referenced by a blob storage URI. 以 VHD(不是 VHDX)格式准备 Windows 或 Linux 操作系统映像,然后将映像上传到 Azure Stack Hub 中的存储帐户。Prepare a Windows or Linux operating system image in VHD format (not VHDX), and then upload the image to a storage account in Azure Stack Hub.

    • 如果 VHD 位于 Azure 中,且你在联网的 Azure Stack Hub 上运行,可以使用 Azcopy 等工具直接在 Azure 与 Azure Stack Hub 存储帐户之间传输 VHD。If the VHD is in Azure, you can use a tool such as Azcopy to directly transfer the VHD between an Azure and your Azure Stack Hub storage account if you are running on a connected Azure Stack Hub.

    • 在离线的 Azure Stack Hub 上,如果 VHD 位于 Azure 中,则必须将 VHD 下载到可以同时连接 Azure 和 Azure Stack Hub 的计算机上。On a disconnected Azure Stack Hub, if your VHD is in Azure, you must download the VHD to a machine that has connectivity to both Azure and Azure Stack Hub. 然后,先从 Azure 将 VHD 复制到此计算机,再使用任何可跨 Azure 与 Azure Stack Hub 使用的通用存储数据传输工具,将 VHD 传输到 Azure Stack Hub。Then, copy the VHD to this machine from Azure before you transfer the VHD to Azure Stack Hub using any of the common storage data transfer tools that can be used across Azure and Azure Stack Hub.

      本示例中使用的一个此类工具是 Add-AzureRmVHD cmdlet,可将 VHD 上传到 Azure Stack Hub 管理员门户中的存储帐户。One such tool used in this example is the Add-AzureRmVHD cmdlet that uploads a VHD to a storage account in the Azure Stack Hub administrator portal.

      Add-AzureRmVhd -Destination "https://bash.blob.redmond.azurestack.com/sample/vhdtestingmgd.vhd" -LocalFilePath "C:\vhd\vhdtestingmgd.vhd"
      
  4. 记下已在其中上传映像的 Blob 存储 URI。Make a note of the blob storage URI where you uploaded the image. Blob 存储 URI 的格式为 <storageAccount>/<blobContainer>/<targetVHDName>.vhdThe blob storage URI has the format <storageAccount>/<blobContainer>/<targetVHDName>.vhd.

  5. 若要使 Blob 可供匿名访问,请转到已在其中上传了 VM 映像 VHD 的存储帐户 Blob 容器。To make the blob anonymously accessible, go to the storage account blob container where the VM image VHD was uploaded. 选择“Blob”,然后选择“访问策略”。Select Blob, and then select Access policy. 也可生成容器的共享访问签名,然后将其作为 Blob URI 的一部分包括进去。Optionally, you can generate a shared access signature for the container, and include it as part of the blob URI. 此步骤使 blob 可供使用。This step makes the blob available to be used. 如果 blob 不可匿名访问,则 VM 映像创建将处于失败状态。If the blob isn't anonymously accessible, the VM image is created in a failed state.

    转到存储帐户 Blob

    将 Blob 访问权限设置为公共

    将 Blob 访问权限设置为公共

步骤 3,选项 1:以 Azure Stack Hub 操作员的身份使用门户添加 VM 映像Step 3, option 1: Add the VM image as an Azure Stack Hub operator using the portal

  1. 以操作员身份登录到 Azure Stack Hub 门户。Sign in to the Azure Stack Hub portal as operator. 选择“所有服务”,然后在“VM 映像” > “添加”下选择“计算”。Select All services, then Compute under VM Images > Add.

    自定义映像旁加载 UI

  2. 在“创建映像”下,输入“发布者”、“套餐”、“SKU”、“版本”和 OS 磁盘 blob URI。Under Create image, enter the Publisher, Offer, SKU, Version, and OS disk blob URI. 然后选择“创建”,开始创建 VM 映像。Then, select Create to begin creating the VM image.

    自定义映像旁加载 UI

    成功创建映像后,VM 映像状态会更改为“已成功”。When the image is successfully created, the VM image status changes to Succeeded.

  3. 添加映像时,它仅适用于基于 Azure 资源管理器的模板和 PowerShell 部署。When you add an image, it is only available for Azure Resource Manager-based templates and PowerShell deployments. 若要将映像作为市场项提供给用户,请使用创建和发布市场项一文中的步骤发布市场项To make an image available to your users as a marketplace item, publish the marketplace item using the steps in the article Create and publish a Marketplace item. 请务必记下“发布者”、“套餐”、“SKU”和“版本”的值。 Make sure to note the Publisher, Offer, SKU, and Version values. 在自定义 .azpkg 中编辑资源管理器模板和 Manifest.json 时,需要用到这些值。You will need them when you edit the Resource Manager template and Manifest.json in your custom .azpkg.

步骤 3,选项 2:以 Azure Stack Hub 操作员的身份使用 PowerShell 添加 VM 映像Step 3, option 2: Add a VM image as an Azure Stack Hub operator using PowerShell

  1. 安装适用于 Azure Stack Hub 的 PowerShellInstall PowerShell for Azure Stack Hub.

  2. 以操作员身份登录到 Azure Stack Hub。Sign in to Azure Stack Hub as an operator. 有关说明,请参阅以操作员身份登录到 Azure Stack HubFor instructions, see Sign in to Azure Stack Hub as an operator.

  3. 使用权限提升的提示符打开 PowerShell,并运行:Open PowerShell with an elevated prompt, and run:

     Add-AzsPlatformimage -publisher "<publisher>" `
       -offer "<Offer>" `
       -sku "<SKU>" `
       -version "<#.#.#>" `
       -OSType "<OS type>" `
       -OSUri "<OS URI>"
    

    Add-AzsPlatformimage cmdlet 指定 Azure 资源管理器模板用来引用 VM 映像的值。The Add-AzsPlatformimage cmdlet specifies values used by the Azure Resource Manager templates to reference the VM image. 这些值包括:The values include:

    • publisherpublisher
      例如: CanonicalFor example: Canonical
      VM 映像的发布者名称段,供用户在部署映像时使用。The publisher name segment of the VM image that users use when they deploy the image. 此字段不得包含空格或其他特殊字符。Do not include a space or other special characters in this field.

    • offeroffer
      例如: UbuntuServerFor example: UbuntuServer
      VM 映像的套餐名称段,供用户在部署 VM 映像时使用。The offer name segment of the VM image that users use when they deploy the VM image. 此字段不得包含空格或其他特殊字符。Do not include a space or other special characters in this field.

    • skusku
      例如: 14.04.3-LTSFor example: 14.04.3-LTS
      VM 映像的 SKU 名称段,供用户在部署 VM 映像时使用。The SKU name segment of the VM Image that users use when they deploy the VM image. 此字段不得包含空格或其他特殊字符。Do not include a space or other special characters in this field.

    • versionversion
      例如: 1.0.0For example: 1.0.0
      VM 映像的版本,供用户在部署 VM 映像时使用。The version of the VM Image that users use when they deploy the VM image. 此版本采用“#.#.#”格式。This version is in the format #.#.#. 此字段不得包含空格或其他特殊字符。Do not include a space or other special characters in this field.

    • osTypeosType
      例如: LinuxFor example: Linux
      映像的 osType 必须为 WindowsLinuxThe osType of the image must be either Windows or Linux.

    • OSUriOSUri
      例如: https://storageaccount.blob.core.chinacloudapi.cn/vhds/Ubuntu1404.vhdFor example: https://storageaccount.blob.core.chinacloudapi.cn/vhds/Ubuntu1404.vhd
      可以指定 osDisk 的 Blob 存储 URI。You can specify a blob storage URI for an osDisk.

      有关详细信息,请参阅 Add-AzsPlatformimage cmdlet 的 PowerShell 参考。For more information, see the PowerShell reference for the Add-AzsPlatformimage cmdlet.

  4. 添加映像时,它仅适用于基于 Azure 资源管理器的模板和 PowerShell 部署。When you add an image, it is only available for Azure Resource Manager-based templates and PowerShell deployments. 若要将映像作为市场项提供给用户,请使用创建和发布市场项一文中的步骤发布市场项To make an image available to your users as a marketplace item, publish the marketplace item using the steps in the article Create and publish a Marketplace item. 请务必记下“发布者”、“套餐”、“SKU”和“版本”的值。 Make sure you note the Publisher, Offer, SKU, and Version values. 在自定义 .azpkg 中编辑资源管理器模板和 Manifest.json 时,需要用到这些值。You will need them when you edit the resource manager template and Manifest.json in your custom .azpkg.

以 Azure Stack Hub 操作员的身份使用门户删除 VM 映像Remove the VM image as an Azure Stack Hub operator using the portal

  1. 打开 Azure Stack Hub [管理员门户] https://adminportal.local.azurestack.externalOpen the Azure Stack Hub [administrator portal] https://adminportal.local.azurestack.external.

  2. 如果 VM 映像有关联的市场项,请选择“市场管理”,然后选择要删除的 VM 市场项。If the VM image has an associated Marketplace item, select Marketplace management, and then select the VM marketplace item you want to delete.

  3. 如果 VM 映像没有关联的市场项,请导航到“所有服务”>“计算”>“VM 映像”,然后选择 VM 映像旁边的省略号 ( ... )。If the VM image does not have an associated Marketplace item, navigate to All services > Compute > VM Images, and then select the ellipsis (...) next to the VM image.

  4. 选择“删除” 。Select Delete.

以 Azure Stack Hub 操作员的身份使用 PowerShell 删除 VM 映像Remove a VM image as an Azure Stack Hub operator using PowerShell

不再需要上传的 VM 映像时,可使用 Remove-AzsPlatformImage cmdlet 从市场中删除它:When you no longer need the VM image that you uploaded, you can delete it from the Marketplace by using the Remove-AzsPlatformImage cmdlet:

  1. 安装适用于 Azure Stack Hub 的 PowerShellInstall PowerShell for Azure Stack Hub.

  2. 以操作员身份登录到 Azure Stack Hub。Sign in to Azure Stack Hub as an operator.

  3. 使用权限提升的提示符打开 PowerShell,并运行:Open PowerShell with an elevated prompt, and run:

    Remove-AzsPlatformImage `
     -publisher "<Publisher>" `
     -offer "<Offer>" `
     -sku "<SKU>" `
     -version "<Version>" `
    

    Remove-AzsPlatformImage cmdlet 指定 Azure 资源管理器模板用来引用 VM 映像的值。The Remove-AzsPlatformImage cmdlet specifies values used by the Azure Resource Manager templates to reference the VM image. 这些值包括:The values include:

    • publisherpublisher
      例如: CanonicalFor example: Canonical
      VM 映像的发布者名称段,供用户在部署映像时使用。The publisher name segment of the VM image that users use when they deploy the image. 此字段不得包含空格或其他特殊字符。Do not include a space or other special characters in this field.

    • offeroffer
      例如: UbuntuServerFor example: UbuntuServer
      VM 映像的套餐名称段,供用户在部署 VM 映像时使用。The offer name segment of the VM image that users use when they deploy the VM image. 此字段不得包含空格或其他特殊字符。Do not include a space or other special characters in this field.

    • skusku
      例如: 14.04.3-LTSFor example: 14.04.3-LTS
      VM 映像的 SKU 名称段,供用户在部署 VM 映像时使用。The SKU name segment of the VM Image that users use when they deploy the VM image. 此字段不得包含空格或其他特殊字符。Do not include a space or other special characters in this field.

    • versionversion
      例如: 1.0.0For example: 1.0.0
      VM 映像的版本,供用户在部署 VM 映像时使用。The version of the VM image that users use when they deploy the VM image. 此版本采用“#.#.#”格式。This version is in the format #.#.#. 此字段不得包含空格或其他特殊字符。Do not include a space or other special characters in this field.

      有关 Remove-AzsPlatformImage cmdlet 的详细信息,请参阅 Azure PowerShell Azure Stack Hub 操作员模块文档For more information about the Remove-AzsPlatformImage cmdlet, see the Azure PowerShell Azure Stack Hub Operator module documentation.

后续步骤Next steps