将 VM 从 Azure 移动到 Azure Stack HubMove a VM from Azure to Azure Stack Hub

可以从 Azure 中创建的虚拟机 (VM) 将虚拟硬盘 (VHD) 上传到 Azure Stack Hub 实例。You can upload a virtual hard drive (VHD) from a virtual machine (VM) created in Azure to your Azure Stack Hub instance.

准备 VHD 并从 Azure 下载Prepare and download your VHD from Azure

在准备 VHD 时查找特定于你需求的部分。Find the section that that is specific to your needs when preparing your VHD.

  • 按照使用 PowerShell 从专用磁盘创建 Windows VM 一文中的步骤来准备 VHD。Follow the steps in the article Create a Windows VM from a specialized disk by using PowerShell to prepare the VHD.
  • 若要部署 VM 扩展,请确保 VM 代理 .msi 可用。To deploy VM extensions, make sure that the VM agent .msi available.
    有关信息和步骤,请参阅 Azure 虚拟机代理概述For information and steps, see Azure Virtual Machine Agent overview. 移动 VM 之前,请确保已在 VM 上安装了扩展。Make sure the extension is installed on the VM before your move VM. 如果 VHD 中不存在 VM 代理,扩展部署将失败。If the VM agent is not present in the VHD, extension deployment will fail. 预配时无需设置 OS 配置文件或设置 $vm.OSProfile.AllowExtensionOperations = $trueYou do not need to set the OS profile while provisioning, or set $vm.OSProfile.AllowExtensionOperations = $true.

验证 VHDVerify your VHD

上传 VHD 之前,必须验证 VHD 是否满足要求。Before uploading your VHD, you must validate that the VHD meets the requirements. 不符合要求的 VHD 将无法在 Azure Stack Hub 中加载。VHDs that don't meet the requirements will fail to load in Azure Stack Hub.

  1. 你将使用 Hyper-V 提供的 PowerShell 模块。You will use the PowerShell modules found with Hyper-V. 激活 Hyper-V 会安装支持 PowerShell 模块。Activating Hyper-V installs supporting PowerShell modules. 你可以通过使用提升的提示符打开 PowerShell 并运行以下 cmdlet 来检查是否具有该模块:You can check that you have the module by opening PowerShell with an elevated prompt and running the following cmdlet:

    Get-Command -Module hyper-v
    

    如果你没有 Hyper-V 命令,请参阅使用 Hyper-V 和 Windows PowerShellIf you do not have the Hyper-V commands, see, see Working with Hyper-V and Windows PowerShell.

  2. 获取计算机上的 VHD 路径。Get the path to your VHD on your machine. 运行以下 cmdlet:Run the following cmdlet:

    get-vhd <path-to-your-VHD>
    

    该 cmdlet 会返回 VHD 对象并显示属性,例如:The cmdlet will return the VHD object and display the attributes, such as:

    ComputerName            : YOURMACHINENAME
    Path                    : <path-to-your-VHD>
    VhdFormat               : VHD
    VhdType                 : Fixed
    FileSize                : 68719477248
    Size                    : 68719476736
    MinimumSize             : 32212254720
    LogicalSectorSize       : 512
    PhysicalSectorSize      : 512
    BlockSize               : 0
    ParentPath              :
    DiskIdentifier          : 3C084D21-652A-4C0E-B2D1-63A8E8E64C0C
    FragmentationPercentage : 0
    Alignment               : 1
    Attached                : False
    DiskNumber              :
    IsPMEMCompatible        : False
    AddressAbstractionType  : None
    Number                  :
    
  3. 对于 VHD 对象,检查是否满足 Azure Stack Hub 的要求。With the VHD object, check that meets the requirements for Azure Stack Hub.

    此外,Azure Stack Hub 仅支持来自第一 (1) 代 VM 的映像。In addition, Azure Stack Hub only supports images from generation one (1) VMs.

  4. 如果 VHD 与 Azure Stack Hub 不兼容,则需要返回到源映像和 Hyper-V,创建满足要求的 VHD 并上传。If your VHD is not compatible with Azure Stack Hub, you will need to return to the source image and Hyper-V, create a VHD that meets the requirements, and upload. 若要在上传过程中最大程度减少可能发生的损坏,请使用 AzCopy。To minimize possible corruption in the upload process, use AzCopy.

如何修复 VHDHow to fix your VHD

若要使 VHD 与 Azure Stack Hub 兼容,必须满足以下要求。The following requirements must be met for compatibility of your VHD with Azure Stack Hub.

VHD 属于固定类型VHD is of fixed type

识别:使用 get-vhd cmdlet 获取 VHD 对象。Identify: Use get-vhd cmdlet to get the VHD object.
修复:可以将 VHDX 文件转换为 VHD,将动态扩展磁盘转换为固定大小的磁盘,但无法更改 VM 的代次。Fix: You can convert a VHDX file to VHD, convert a dynamically expanding disk to a fixed-size disk, but you can't change a VM's generation. 使用 Hyper-V 管理器或 PowerShell 转换磁盘。Use Hyper-V Manager or PowerShell to convert the disk.

VHD 的最小虚拟大小至少为 20 MBVHD has minimum virtual size of at least 20 MB

识别:使用 get-vhd cmdlet 获取 VHD 对象。Identify: Use get-vhd cmdlet to get the VHD object.
修复:使用 Hyper-V 管理器或 PowerShell 调整磁盘大小。Fix: Use Hyper-V Manager or PowerShell to resize the disk.

VHD 已调整VHD is aligned

识别:使用 get-vhd cmdlet 获取 VHD 对象。Identify: Use get-vhd cmdlet to get the VHD object.
修复:虚拟大小必须是 1 MB 的倍数。Fix: The virtual size must be a multiple of one (1) MB.

磁盘必须已将虚拟大小调整为 1 MiB。Disks must have a virtual size aligned to 1 MiB. 如果 VHD 的大小不是 1 MiB 的整数倍,需要将磁盘大小调整为 1 MiB 的倍数。If your VHD is a fraction of 1 MiB, you'll need to resize the disk to a multiple of 1 MiB. 基于上传的 VHD 创建映像时,不到 1 MiB 的磁盘将导致错误。Disks that are fractions of a MiB cause errors when creating images from the uploaded VHD. 若要验证该大小,可以使用 PowerShell Get-VHD comdlet 来显示“大小”(在 Azure 中必须是 1 MiB 的倍数),以及“文件大小”(等于“大小”加上 VHD 页脚的 512 字节)。To verify the size you can use the PowerShell Get-VHD cmdlet to show "Size", which must be a multiple of 1 MiB in Azure, and "FileSize", which will be equal to "Size" plus 512 bytes for the VHD footer.

使用 Hyper-V 管理器或 PowerShell 调整磁盘大小。Use Hyper-V Manager or PowerShell to resize the disk.

VHD blob 长度VHD blob length

识别:使用 get-vhd cmdlet 显示 Size Identify: Use the get-vhd cmdlet to show Size
修复:VHD blob 长度 = 虚拟大小 + vhd 页脚长度 (512)。Fix: The VHD blob length = virtual size + vhd footer length (512). 在 Blob 末尾有一小段脚注,描述了 VHD 的属性。A small footer at the end of the blob describes the properties of the VHD. Size 在 Azure 中必须是 1 MiB 的倍数,FileSize 等于 Size + VHD 页脚的 512 字节。Size must be a multiple of 1 MiB in Azure, and FileSize, which will be equal to Size + 512 bytes for the VHD footer.

使用 Hyper-V 管理器或 PowerShell 调整磁盘大小。Use Hyper-V Manager or PowerShell to resize the disk.

第一代 VMGeneration one VMs

识别:若要确认虚拟机是否为第 1 代虚拟机,请使用 cmdlet Get-VM | Format-Table Name, GenerationIdentify: To confirm if your virtual machine is Generation 1, use the cmdlet Get-VM | Format-Table Name, Generation.
修复:需要在虚拟机监控程序 (Hyper-V) 中重新创建 VM。Fix: You will need to recreate your VM in your hypervisor (Hyper-V).

上传到存储帐户Upload to a storage account

可以使用门户上传 VHD,或是对于在门户中创建的容器使用 AzCopy。You can upload your VHD with the portal, or with the container you created in the portal, use AzCopy.

用于生成 SAS URL 并上传 VHD 的门户Portal to generate SAS URL and upload VHD

  1. 登录到 Azure Stack Hub 用户门户。Sign in to the Azure Stack Hub user portal.

  2. 选择“存储帐户”,并选择现有存储帐户,或创建新存储帐户。Select Storage Accounts and select an existing storage account or create a new storage account.

  3. 在存储帐户的存储帐户边栏选项卡中选择“Blob”。Select Blobs in the storage account blade for your storage account. 选择“容器”来新建容器。Select Container to create a new container.

  4. 键入容器的名称,然后选择“Blob (仅匿名读取访问 blob)”。Type the name of your container, and then select Blob (anonymous read access for blobs only).

  5. 如果要使用 AzCopy 上传映像而不是门户,请创建 SAS 令牌。If you are going to use AzCopy to upload your image rather than the portal, create a SAS token. 在存储帐户中选择“共享访问签名”,然后选择“生成 SAS 和连接字符串” 。Select Shared access signature in the storage account, and then select Generate SAS and connection string. 复制并记下“Blob 服务 SAS URL”。Copy and make a note of the Blob service SAS URL. 使用 AzCopy 上传 VHD 时,将使用此 URL。You will use this URL when using AzCopy to upload your VHD.

  6. 选择容器,然后选择“上传”。Select your container and then select Upload. 上传 VHD。Upload your VHD.

AzCopy VHDAzCopy VHD

使用 Azure 存储资源管理器或 AzCopy 可减少 VHD 在上传过程中损坏的可能性,并且上传速度会更快。Use Azure Storage Explorer or AzCopy to reduce that chance that your VHD will be corrupted in the upload process, and your upload will be faster. 以下步骤在 Windows 10 计算机上使用 AzCopy。The following steps use AzCopy on a Windows 10 machine. AzCopy 是一个命令行实用工具,可用于向/从存储帐户复制 Blob 或文件。AzCopy is a command-line utility that you can use to copy blobs or files to or from a storage account.

  1. 如果尚未安装 AzCopy,请安装 AzCopy。If you don't have AzCopy installed, install AzCopy. AzCopy 入门一文中提供了用于下载和开始使用 AzCopy 的说明。You find find instruction to download and get started with AzCopy in the article Get started with AzCopy. 记下二进制文件的存储位置。Make a note of where you store the binary. 可以将 AzCopy 添加到路径,以便从 PowerShell 命令行使用它。You can add AzCopy to your path to use it from the PowerShell command line.

  2. 打开 PowerShell,以便从 shell 使用 AzCopy。Open PowerShell to use AzCopy from the shell.

  3. 使用 AzCopy 将 VHD 上传到存储帐户中的容器中。Use AzCopy to upload your VHD into the your container in the storage account.

    set AZCOPY_DEFAULT_SERVICE_API_VERSION=2017-11-09
    azcopy cp "/path/to/file.vhd" "https://[account].blob.core.chinacloudapi.cn/[container]/[path/to/blob]?[SAS] --blob-type=PageBlob
    

备注

使用类似于将单个文件上传到虚拟目录的语法上传 VHD。Upload your VHD using syntax similar to uploading a single file to virtual directory. 添加 --blob-type=PageBlob 以确保 VHD 作为“页 Blob”上传,而不是默认情况下的“块” 。Add --blob-type=PageBlob to make sure that the VHD is uploaded as a Page Blob, instead of Block by default.

有关使用 AzCopy 和其他存储工具的详细信息,请参阅在 Azure Stack Hub 存储中使用数据传输工具For more information about using AzCopy and other storage tools, see Use data transfer tools in Azure Stack Hub Storage.

创建 VMCreate the VM

自定义映像有两种形式:专用和通用 。Custom images come in two forms: specialized and generalized.

  1. 登录到 Azure Stack Hub 用户门户。Sign in to the Azure Stack Hub user portal.

    如果你是创建平台磁盘的云操作员,请按照添加平台映像中的说明操作,通过管理员门户或管理员终结点添加 VHD。If you are a cloud operator creating a platform disk, follow the instructions in Add a platform image to add the VHD through the administrator portal or with the administrator endpoints.

  2. 在用户门户中,选择“所有服务” > “磁盘” > “添加”。In the user portal, select All Services > Disks > Add.

  3. 在“创建托管磁盘”中:In Create managed disk:

    1. 键入映像的“名称”。Type the Name of your image.

    2. 选择订阅Select your Subscription.

    3. 创建映像,或将映像添加到资源组。Create or add the image to a Resource group.

    4. 选择 ASDK 的“位置”(也称为区域)。Select the Location, also referred to as the region, of your ASDK.

    5. 选择“帐户类型”。Select the Account type.

      • 高级磁盘 (SSD) 基于固态硬盘,提供一致的低延迟性能。Premium disks (SSD) are backed by solid-state drives and offer consistent, low-latency performance. 高级磁盘可在价格与性能之间实现最佳平衡,非常适合用于 I/O 密集型应用程序和生产工作负荷。They provide the best balance between price and performance, and are ideal for I/O-intensive applications and production workloads.
      • 标准磁盘 (HDD) 基于磁驱动器,适用于不经常访问数据的应用程序。Standard disks (HDD) are backed by magnetic drives and are preferable for applications where data is accessed infrequently.
    6. 对“源类型”选择“存储 blob” 。Select Storage blob your Source type. 会通过存储帐户中的 blob 创建磁盘。You are created a disk from a blob in a storage account.

    7. 对于 VHD 源,请选择:For the VHD source select:

      1. 存储帐户所在的源订阅。The Source subscription where the storage account is located.
      2. 选择“浏览”,然后导航到存储帐户、容器和 VHD。Select Browse and then navigate to your Storage account, container, and VHD. 选择“选择” 。Choose Select.
      3. 选择与 VHD 匹配的“OS 类型”。Select the OS Type that matches the VHD.
    8. 选择等于或大于 VHD 的磁盘“大小(GiB)”。Select a disk Size (GiB) that the size of or larger than your VHD.

    9. 选择“创建” 。Select Create.

  4. 创建磁盘后,便可以使用磁盘创建新 VM。Once the disk is created, you can use the disk to create a new VM.

后续步骤Next steps

将 VM 移动到 Azure Stack Hub 概述Move a VM to Azure Stack Hub Overview