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

有关未认可分发版的信息Information for Non-Endorsed Distributions

备注

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.

仅当使用某个认可的分发版时,Azure 平台 SLA 才适用于运行 Linux OS 的虚拟机。The Azure platform SLA applies to virtual machines running the Linux OS only when one of the endorsed distributions is used. 对于这些认可的分发版,将在 Azure Marketplace 中提供具有所需配置的 Linux 映像。For these endorsed distributions, Linux images are provided in the Azure Marketplace with the required configuration.

所有正在 Azure 上运行的分发都需要满足多个先决条件才能在平台上正常运行。All distributions running on Azure will need to meet a number of prerequisites to have a chance to properly run on the platform. 本文并未涵盖所有信息,因为每个分发都是不同的;即使你满足以下所有条件,也可能仍需显著调整 Linux 系统以确保其在平台上正常运行。This article is by no means comprehensive as every distribution is different; and it is quite possible that even if you meet all the criteria below you will still need to significantly tweak your Linux system to ensure that it properly runs on the platform.

正是出于这个原因,建议尽可能地从某个 Azure 上的 Linux - 认可的分发版开始操作。It is for this reason that we recommend that you start with one of our Linux on Azure Endorsed Distributions when possible. 以下文章指导完成如何准备 Azure 上支持的各种认可的 Linux 分发:The following articles will guide you through how to prepare the various endorsed Linux distributions that are supported on Azure:

本文的其余部分将重点介绍有关在 Azure 上运行 Linux 分发的一般准则。The rest of this article will focus on general guidance for running your Linux distribution on Azure.

常规 Linux 安装说明General Linux Installation Notes

  • 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. 如果使用 VirtualBox,则意味着选择的是“固定大小”,而不是在创建磁盘时动态分配默认大小。If you are using VirtualBox this means selecting Fixed size as opposed to the default dynamically allocated when creating the disk.
  • Azure 仅支持第 1 代虚拟机。Azure only supports generation 1 virtual machines. 可以将第 1 代虚拟机从 VHDX 转换为 VHD 文件格式,从动态扩展磁盘转换为固定大小磁盘。You can convert a generation 1 virtual machine from VHDX to the VHD file format and from dynamically expanding to a fixed sized disk. 但无法更改虚拟机的代次。But you can't change a virtual machine's generation. 有关详细信息,请参阅是否应在 Hyper-V 中创建第 1 代或第 2 代虚拟机?For more information, see Should I create a generation 1 or 2 virtual machine in Hyper-V?
  • VHD 允许的最大大小为 1,023 GB。The maximum size allowed for the VHD is 1,023 GB.
  • 在安装 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 identical VM for troubleshooting. LVMRAID 可以在数据磁盘上使用。LVM or RAID may be used on data disks.
  • 需要装载 UDF 文件系统的内核支持。Kernel support for mounting UDF file systems is required. 在 Azure 上首次启动时,预配配置将通过附加到来宾的 UDF 格式媒体传递到 Linux VM。At first boot on Azure the provisioning configuration is passed to the Linux VM via UDF-formatted media that is attached to the guest. Azure Linux 代理必须能够装载 UDF 文件系统才能读取其配置和预配 VM。The Azure Linux agent must be able to mount the UDF file system to read its configuration and provision the VM.
  • 低于 2.6.37 的 Linux 内核版本不支持具有更大 VM 大小的 Hyper-V 上的 NUMA。Linux kernel versions below 2.6.37 do not support NUMA on Hyper-V with larger VM sizes. 此问题主要影响使用上游 Red Hat 2.6.32 内核的旧分发版,在 RHEL 6.6 (kernel-2.6.32-504) 中已解决。This issue primarily impacts older distributions using the upstream Red Hat 2.6.32 kernel, and was fixed in RHEL 6.6 (kernel-2.6.32-504). 运行版本低于 2.6.37 的自定义内核的系统,或者版本低于 2.6.32-504 的基于 RHEL 的内核必须在 grub.conf 中的内核命令行上设置启动参数 numa=offSystems running custom kernels older than 2.6.37, or RHEL-based kernels older than 2.6.32-504 must set the boot parameter numa=off on the kernel command-line in grub.conf. 有关详细信息,请参阅 Red Hat KB 436883For more information see Red Hat KB 436883.
  • 不要在操作系统磁盘上配置交换分区。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. 可以在下面的步骤中找到更多信息。More information can be found in the steps below.

安装无 Hyper-V 的内核模块Installing kernel modules without Hyper-V

Azure 在 Hyper-V 虚拟机监控程序上运行,因此 Linux 需要安装某些内核模块才能在 Azure 中运行。Azure runs on the Hyper-V hypervisor, so Linux requires that certain kernel modules are installed in order to run in Azure. 如果具有在 Hyper-V 外部创建的虚拟机,Linux 安装程序可能无法在初始 ramdisk(initrd 或 initramfs)中包含 Hyper-V 驱动程序,除非它检测到它正在运行 Hyper-V 环境。If you have a VM that was created outside of Hyper-V, the Linux installers may not include the drivers for Hyper-V in the initial ramdisk (initrd or initramfs) unless it detects that it is running an a Hyper-V environment. 使用不同虚拟化系统(即 Virtualbox、KVM 等)来准备 Linux 映像时,可能需要重新生成 initrd 以确保至少 hv_vmbushv_storvsc 内核模块可在初始 ramdisk 上使用。When using a different virtualization system (i.e. Virtualbox, KVM, etc.) to prepare your Linux image, you may need to rebuild the initrd to ensure that at least the hv_vmbus and hv_storvsc kernel modules are available on the initial ramdisk. 至少在基于上游 Red Hat 分发的系统上这是一个已知问题。This is a known issue at least on systems based on the upstream Red Hat distribution.

重新生成 initrd 或 initramfs 映像的机制可能会因分发而有所不同。The mechanism for rebuilding the initrd or initramfs image may vary depending on the distribution. 请查阅分发的文档或相应过程的支持。Please consult your distribution's documentation or support for the proper procedure. 下面是有关如何使用 mkinitrd 实用工具重新生成 initrd 的示例:Here is one example for how to rebuild the initrd using the mkinitrd utility:

首先,备份现有 initrd 映像:First, back up the existing initrd image:

# cd /boot
# sudo cp initrd-`uname -r`.img  initrd-`uname -r`.img.bak

接下来,使用 hv_vmbushv_storvsc 内核模块重新生成 initrd:Next, rebuild the initrd with the hv_vmbus and hv_storvsc kernel modules:

# sudo mkinitrd --preload=hv_storvsc --preload=hv_vmbus -v -f initrd-`uname -r`.img `uname -r`

调整 VHD 大小Resizing VHDs

Azure 上的 VHD 映像必须已将虚拟大小调整为 1MB。VHD images on Azure must have a virtual size aligned to 1MB. 通常情况下,使用 Hyper-V 创建的 VHD 应已正确调整。Typically, VHDs created using Hyper-V should already be aligned correctly. 如果未正确调整 VHD,则尝试基于 VHD 创建映像时,可能会收到如下错误消息:If the VHD is not aligned correctly then you may receive an error message similar to the following when you attempt to create an image from your VHD:

"The VHD http://<mystorageaccount>.blob.core.windows.net/vhds/MyLinuxVM.vhd has an unsupported virtual size of 21475270656 bytes. The size must be a whole number (in MBs).”

若要修正此问题,可使用 Hyper-V 管理器控制台或 Resize-VHD Powershell cmdlet 调整 VM 大小。To remedy this you can resize the VM using either the Hyper-V Manager console or the Resize-VHD Powershell cmdlet. 如果未在 Windows 环境中运行,则建议使用 qemu-img 转换(如果需要)并调整 VHD 大小。If you are not running in a Windows environment then it is recommended to use qemu-img to convert (if needed) and resize the VHD.

备注

qemu-img 版本(>=2.2.1)中有一个已知 bug,会导致 VHD 格式不正确。There is a known bug in qemu-img versions >=2.2.1 that results in an improperly formatted VHD. QEMU 2.6 中已修复此问题。The issue has been fixed in QEMU 2.6. 建议使用 qemu-img 2.2.0 或更低版本,或者更新到 2.6 或更高版本。It is recommended to use either qemu-img 2.2.0 or lower, or update to 2.6 or higher. 参考:https://bugs.launchpad.net/qemu/+bug/1490611。Reference: https://bugs.launchpad.net/qemu/+bug/1490611.

  1. 直接使用工具(如 qemu-imgvbox-manage)调整 VHD 大小可能会生成无法启动的 VHD。Resizing the VHD directly using tools such as qemu-img or vbox-manage may result in an unbootable VHD. 因此,建议先将 VHD 转换为 RAW 磁盘映像。So it is recommended to first convert the VHD to a RAW disk image. 如果已将 VM 映像创建为 RAW 磁盘映像(对于 KVM 等某些虚拟机监控程序,这是默认设置),则可以跳过此步骤:If the VM image was already created as RAW disk image (the default for some Hypervisors such as KVM) then you may skip this step:

    # qemu-img convert -f vpc -O raw MyLinuxVM.vhd MyLinuxVM.raw
    
  2. 计算磁盘映像所需的大小,以确保虚拟大小已调整为 1MB。Calculate the required size of the disk image to ensure that the virtual size is aligned to 1MB. 以下 bash shell 脚本可以对此有帮助。The following bash shell script can assist with this. 此脚本使用“qemu-img info”来确定磁盘映像的虚拟大小,并将大小计算到下个 1MB:The script uses "qemu-img info" to determine the virtual size of the disk image and then calculates the size to the next 1MB:

    rawdisk="MyLinuxVM.raw"
    vhddisk="MyLinuxVM.vhd"
    
    MB=$((1024*1024))
    size=$(qemu-img info -f raw --output json "$rawdisk" | \
           gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')
    
    rounded_size=$((($size/$MB + 1)*$MB))
    echo "Rounded Size = $rounded_size"
    
  3. 使用 $rounded_size 调整原始磁盘大小,如上述脚本所设置:Resize the raw disk using $rounded_size as set in the above script:

    # qemu-img resize MyLinuxVM.raw $rounded_size
    
  4. 现在,将 RAW 磁盘重新转换为固定大小 VHD:Now, convert the RAW disk back to a fixed-size VHD:

    # qemu-img convert -f raw -o subformat=fixed -O vpc MyLinuxVM.raw MyLinuxVM.vhd
    

    或者,对于 qemu 版本 2.6+,包括 force_size 选项:Or, with qemu version 2.6+ include the force_size option:

    # qemu-img convert -f raw -o subformat=fixed,force_size -O vpc MyLinuxVM.raw MyLinuxVM.vhd
    

Linux 内核要求Linux Kernel Requirements

Hyper-V 和 Azure 的 Linux 集成服务 (LIS) 驱动程序会直接影响上游 Linux 内核。The Linux Integration Services (LIS) drivers for Hyper-V and Azure are contributed directly to the upstream Linux kernel. 包括最新 Linux 内核版本(即 3.x)在内的许多分发已提供这些驱动程序,或以其他方式为其内核提供了这些驱动程序的向后移植版本。Many distributions that include a recent Linux kernel version (i.e. 3.x) will have these drivers available already, or otherwise provide backported versions of these drivers with their kernels. 这些驱动程序会不断地在上游内核中使用新的修补程序和功能进行更新,因此,如果可能,请运行认可的分发版以包含这些修补程序和更新。These drivers are constantly being updated in the upstream kernel with new fixes and features, so when possible it is recommended to run an endorsed distribution that will include these fixes and updates.

如果正在运行 Red Hat Enterprise Linux 版本 6.0-6.3 的一个变体,需要为 Hyper-V 安装最新的 LIS 驱动程序。If you are running a variant of Red Hat Enterprise Linux versions 6.0-6.3, then you will need to install the latest LIS drivers for Hyper-V. 在此处找到这些驱动程序。The drivers can be found at this location. 从 RHEL 6.4+(和派生产品)开始,LIS 驱动程序已包含在内核中,因此,无需其他安装包即在 Azure 上运行这些系统。As of RHEL 6.4+ (and derivatives) the LIS drivers are already included with the kernel and so no additional installation packages are needed to run those systems on Azure.

如果需要自定义内核,建议使用较新的内核版本(即 3.8+)。If a custom kernel is required, it is recommended to use a more recent kernel version (i.e. 3.8+). 对于这些分发或维护自己的内核的供应商,将需要执行一些操作,以便定期将 LIS 驱动程序从上游内核向后移植到自定义内核。For those distributions or vendors who maintain their own kernel, some effort will be required to regularly backport the LIS drivers from the upstream kernel to your custom kernel. 即使你已运行相对较新的内核版本,也强烈建议跟踪 LIS 驱动程序中的任何上游修复,并根据需要向后移植这些修复。Even if you are already running a relatively recent kernel version, it is highly recommended to keep track of any upstream fixes in the LIS drivers and backport those as needed. LIS 驱动程序源文件的位置可在 Linux 内核源树中的 MAINTAINERS 文件中找到:The location of the LIS driver source files is available in the MAINTAINERS file in the Linux kernel source tree:

F:    arch/x86/include/asm/mshyperv.h
F:    arch/x86/include/uapi/asm/hyperv.h
F:    arch/x86/kernel/cpu/mshyperv.c
F:    drivers/hid/hid-hyperv.c
F:    drivers/hv/
F:    drivers/input/serio/hyperv-keyboard.c
F:    drivers/net/hyperv/
F:    drivers/scsi/storvsc_drv.c
F:    drivers/video/fbdev/hyperv_fb.c
F:    include/linux/hyperv.h
F:    tools/hv/

至少,缺少以下修补程序已知会在 Azure 上导致问题,因此必须在内核中包含这些修补程序。At a very minimum, the absence of the following patches have been known to cause problems on Azure and so these must be included in the kernel. 对于所有分发而言,此列表绝并非详尽或完整:This list is by no means exhaustive or complete for all distributions:

Azure Linux 代理The Azure Linux Agent

Azure Linux 代理 (waagent) 是在 Azure 中正确设置 Linux 虚拟机所必需的。The Azure Linux Agent (waagent) is required to properly provision a Linux virtual machine in Azure. 可以在 Linux 代理 GitHub 存储库中获取最新版本、文件问题或提交拉取请求。You can get the latest version, file issues or submit pull requests at the Linux Agent GitHub repo.

  • 根据 Apache 2.0 许可证发布 Linux 代理。The Linux agent is released under the Apache 2.0 license. 许多分发已为该代理提供 RPM 或 deb 包,因此,在某些情况下不费吹灰之力即可安装和更新该代理。Many distributions already provide RPM or deb packages for the agent, and so in some cases this can be installed and updated with little effort.
  • Azure Linux 代理需要 Python v2.6 以上版本。The Azure Linux Agent requires Python v2.6+.
  • 该代理还需要 python-pyasn1 模块。The agent also requires the python-pyasn1 module. 大多数分发提供此模块作为可以安装的单独包。Most distributions provide this as a separate package that can be installed.
  • 在某些情况下,Azure Linux 代理可能与 NetworkManager 不兼容。In some cases the Azure Linux Agent may not be compatible with NetworkManager. 分发提供的许多 RPM/Deb 包会将 NetworkManager 配置为与 waagent 包冲突,因此安装 Linux 代理包时会卸载 NetworkManager。Many of the RPM/Deb packages provided by distributions configure NetworkManager as a conflict to the waagent package, and thus will uninstall NetworkManager when you install the Linux agent package.
  • Azure Linux 代理必须是上面的最低支持版本,请参阅此文章中的详细信息The Azure Linux Agent must be above the minimum supported version, see this article for details.

一般 Linux 系统要求General Linux System Requirements

  • 修改 GRUB 或 GRUB2 中的内核引导行,以便包含以下参数。Modify the kernel boot line in GRUB or GRUB2 to include the following parameters. 这还将确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 支持人员调试问题:This will also ensure all console messages are sent to the first serial port, which can assist Azure support with debugging issues:

      console=ttyS0,115200n8 earlyprintk=ttyS0,115200 rootdelay=300
    

    除此之外,建议删除以下参数(如果存在):In addition to the above, it is recommended to remove the following parameters if they exist:

      rhgb quiet crashkernel=auto
    

    图形引导和无人参与引导不适用于云环境,在该环境中我们想要将所有日志都发送到串行端口。Graphical and quiet boot are not useful in a cloud environment where we want all the logs to be sent to the serial port. 根据需要可以配置 crashkernel 选项,但请注意此参数会使虚拟机中的可用内存量减少 128MB 或更多,这在较小的虚拟机上可能会出现问题。The crashkernel option may be left configured if desired, but note that this parameter will reduce the amount of available memory in the VM by 128MB or more, which may be problematic on the smaller VM sizes.

  • 安装 Azure Linux 代理Installing the Azure Linux Agent

    Azure Linux 代理是在 Azure 上设置 Linux 映像所必需的。The Azure Linux Agent is required for provisioning a Linux image on Azure. 许多分发将该代理提供为 RPM 或 Deb 包(该包通常称为“WALinuxAgent”或“walinuxagent”)。Many distributions provide the agent as an RPM or Deb package (the package is typically called 'WALinuxAgent' or 'walinuxagent'). 还可以按照 Linux 代理指南中的步骤手动安装该代理。The agent can also be installed manually by following the steps in the Linux Agent Guide.

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

  • 不要在操作系统磁盘上创建交换空间Do not create swap space on the OS disk

    Azure Linux 代理可使用在 Azure 上设置后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space using the local resource disk that is attached to the VM after provisioning on Azure. 请注意,本地资源磁盘是临时磁盘,并可能在取消预配 VM 时被清空。Note that the local resource disk is a temporary disk, and might be emptied when the VM is deprovisioned. 在安装 Azure Linux 代理(请参见前一步骤)后,相应地在 /etc/waagent.conf 中修改以下参数:After installing the Azure Linux Agent (see previous step), modify the following parameters in /etc/waagent.conf appropriately:

      ResourceDisk.Format=y
      ResourceDisk.Filesystem=ext4
      ResourceDisk.MountPoint=/mnt/resource
      ResourceDisk.EnableSwap=y
      ResourceDisk.SwapSizeMB=2048    ## NOTE: set this to whatever you need it to be.
    
  • 最后一步,请运行以下命令以取消设置虚拟机:As a final step, run the following commands to deprovision the virtual machine:

      # sudo waagent -force -deprovision
      # export HISTSIZE=0
      # logout
    

    备注

    运行“waagent -force -deprovision”之后,在 Virtualbox 上可能看到以下错误:[Errno 5] Input/output errorOn Virtualbox you may see the following error after running 'waagent -force -deprovision': [Errno 5] Input/output error. 此错误消息并不关键,可以忽略。This error message is not critical and can be ignored.

  • 然后,需要关闭虚拟机并将 VHD 上传到 Azure。You will then need to shut down the virtual machine and upload the VHD to Azure.