您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

为 Azure 准备 Ubuntu 虚拟机Prepare an Ubuntu virtual machine for Azure

备注

Azure 具有用于创建和处理资源的两个不同的部署模型:Resource Manager 和经典Azure has two different deployment models for creating and working with resources: Resource Manager and classic. 这篇文章介绍如何使用这两种模型,但 Microsoft 建议大多数最新部署使用 Resource Manager 模型。This article covers using both models, but Microsoft recommends that most new deployments use the Resource Manager model.

正式 Ubuntu 云映像Official Ubuntu cloud images

Ubuntu 现已发布正式 Azure VHD,可从 http://cloud-images.ubuntu.com/ 下载。Ubuntu now publishes official Azure VHDs for download at http://cloud-images.ubuntu.com/. 如果需要为 Azure 构建自己专用的 Ubuntu 映像,而不是使用以下手动过程,则我们建议先使用这些已知良好的 VHD,并根据需要进行自定义。If you need to build your own specialized Ubuntu image for Azure, rather than use the manual procedure below it is recommended to start with these known working VHDs and customize as needed. 始终可以在以下位置找到最新的映像版本:The latest image releases can always be found at the following locations:

先决条件Prerequisites

本文假定已在虚拟硬盘中安装了 Ubuntu Linux 操作系统。This article assumes that you have already installed an Ubuntu Linux operating system to a virtual hard disk. 存在多个用于创建 .vhd 文件的工具,例如 Hyper-V 等虚拟化解决方案。Multiple tools exist to create .vhd files, for example a virtualization solution such as Hyper-V. 有关说明,请参阅安装 Hyper-V 角色和配置虚拟机For instructions, see Install the Hyper-V Role and Configure a Virtual Machine.

Ubuntu 安装说明Ubuntu installation notes

  • 另请参阅常规 Linux 安装说明,获取更多有关如何为 Azure 准备 Linux 的提示。Please see also General Linux Installation Notes for more tips on preparing Linux for Azure.
  • Azure 不支持 VHDX 格式,仅支持固定大小的 VHDThe VHDX format is not supported in Azure, only fixed VHD. 可使用 Hyper-V 管理器或 convert-vhd cmdlet 将磁盘转换为 VHD 格式。You can convert the disk to VHD format using Hyper-V Manager or the convert-vhd cmdlet.
  • 在安装 Linux 系统时,建议使用标准分区而不是 LVM(通常是许多安装的默认值)。When installing the Linux system it is recommended that you use standard partitions rather than LVM (often the default for many installations). 这会避免 LVM 与克隆 VM 发生名称冲突,特别是在 OS 磁盘需要连接到另一台 VM 以进行故障排除的情况下。This will avoid LVM name conflicts with cloned VMs, particularly if an OS disk ever needs to be attached to another VM for troubleshooting. 如果需要,可以在数据磁盘上使用 LVMRAIDLVM or RAID may be used on data disks if preferred.
  • 不要在操作系统磁盘上配置交换分区。Do not configure a swap partition on the OS disk. 可以配置 Linux 代理,以在临时资源磁盘上创建交换文件。The Linux agent can be configured to create a swap file on the temporary resource disk. 可以在下面的步骤中找到有关此内容的详细信息。More information about this can be found in the steps below.
  • Azure 上的所有 VHD 必须已将虚拟大小调整为 1MB。All VHDs on Azure must have a virtual size aligned to 1MB. 从原始磁盘转换为 VHD 时,必须确保在转换前原始磁盘大小是 1MB 的倍数。When converting from a raw disk to VHD you must ensure that the raw disk size is a multiple of 1MB before conversion. 有关详细信息,请参阅 Linux 安装说明See Linux Installation Notes for more information.

手动步骤Manual steps

备注

尝试为 Azure 创建自己的自定义 Ubuntu 映像之前,请考虑改用 http://cloud-images.ubuntu.com/ 中的预建且经过测试的映像。Before attempting to create your own custom Ubuntu image for Azure, please consider using the pre-built and tested images from http://cloud-images.ubuntu.com/ instead.

  1. 在 Hyper-V 管理器的中间窗格中,选择虚拟机。In the center pane of Hyper-V Manager, select the virtual machine.

  2. 单击“连接”打开虚拟机窗口。Click Connect to open the window for the virtual machine.

  3. 替换映像中的当前存储库,以使用 Ubuntu 的 Azure 存储库。Replace the current repositories in the image to use Ubuntu's Azure repos. 这些步骤可能会由于 Ubuntu 版本的不同而稍有差异。The steps vary slightly depending on the Ubuntu version.

    编辑 /etc/apt/sources.list 之前,建议进行备份:Before editing /etc/apt/sources.list, it is recommended to make a backup:

     # sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    

    Ubuntu 12.04:Ubuntu 12.04:

     # sudo sed -i 's/[a-z][a-z].archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
     # sudo apt-get update
    

    Ubuntu 14.04:Ubuntu 14.04:

     # sudo sed -i 's/[a-z][a-z].archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
     # sudo apt-get update
    

    Ubuntu 16.04:Ubuntu 16.04:

     # sudo sed -i 's/[a-z][a-z].archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
     # sudo apt-get update
    
  4. Ubuntu Azure 映像现在遵循硬件支持 (HWE) 内核要求。The Ubuntu Azure images are now following the hardware enablement (HWE) kernel. 通过运行以下命令将操作系统更新为最新内核:Update the operating system to the latest kernel by running the following commands:

    Ubuntu 12.04:Ubuntu 12.04:

     # sudo apt-get update
     # sudo apt-get install linux-image-generic-lts-trusty linux-cloud-tools-generic-lts-trusty
     # sudo apt-get install hv-kvp-daemon-init
     (recommended) sudo apt-get dist-upgrade
    
     # sudo reboot
    

    Ubuntu 14.04:Ubuntu 14.04:

     # sudo apt-get update
     # sudo apt-get install linux-image-virtual-lts-vivid linux-lts-vivid-tools-common
     # sudo apt-get install hv-kvp-daemon-init
     (recommended) sudo apt-get dist-upgrade
    
     # sudo reboot
    

    Ubuntu 16.04:Ubuntu 16.04:

     # sudo apt-get update
     # sudo apt-get install linux-generic-hwe-16.04 linux-cloud-tools-generic-hwe-16.04
     (recommended) sudo apt-get dist-upgrade
    
     # sudo reboot
    

    另请参阅:See also:

  5. 修改 Grub 的内核引导行以使其包含 Azure 的其他内核参数。Modify the kernel boot line for Grub to include additional kernel parameters for Azure. 为此,请在文本编辑器中打开 /etc/default/grub,找到名为 GRUB_CMDLINE_LINUX_DEFAULT 的变量(或根据需要添加它)并对它进行编辑使其包含以下参数:To do this open /etc/default/grub in a text editor, find the variable called GRUB_CMDLINE_LINUX_DEFAULT (or add it if needed) and edit it to include the following parameters:

     GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0,115200n8 earlyprintk=ttyS0,115200 rootdelay=300"
    

    保存并关闭此文件,运行 sudo update-grubSave and close this file, and then run sudo update-grub. 这还将确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 技术支持人员调试问题。This will ensure all console messages are sent to the first serial port, which can assist Azure technical support with debugging issues.

  6. 请确保已安装 SSH 服务器且已将其配置为在引导时启动。Ensure that the SSH server is installed and configured to start at boot time. 这通常是默认设置。This is usually the default.

  7. 安装 Azure Linux 代理:Install the Azure Linux Agent:

     # sudo apt-get update
     # sudo apt-get install walinuxagent
    

    备注

    安装 walinuxagent 包时会删除 NetworkManagerNetworkManager-gnome 包(如果已安装它们)。The walinuxagent package may remove the NetworkManager and NetworkManager-gnome packages, if they are installed.

  8. 运行以下命令可取消对虚拟机的设置并且对其进行准备以便在 Azure 上进行设置:Run the following commands to deprovision the virtual machine and prepare it for provisioning on Azure:

     # sudo waagent -force -deprovision
     # export HISTSIZE=0
     # logout
    
  9. 在 Hyper-V 管理器中单击“操作”->“关闭”。Click Action -> Shut Down in Hyper-V Manager. Linux VHD 现已准备好上传到 Azure。Your Linux VHD is now ready to be uploaded to Azure.

后续步骤Next steps

现在,可以使用 Ubuntu Linux 虚拟硬盘在 Azure 中创建新的 Azure 虚拟机了。You're now ready to use your Ubuntu Linux virtual hard disk to create new virtual machines in Azure. 如果是首次将 .vhd 文件上传到 Azure,请参阅从自定义磁盘创建 Linux VMIf this is the first time that you're uploading the .vhd file to Azure, see Create a Linux VM from a custom disk.

参考References

Ubuntu 硬件支持 (HWE) 内核:Ubuntu hardware enablement (HWE) kernel: