您现在访问的是微软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 市场中提供了预配置的 Linux 映像。For these endorsed distributions, pre-configured Linux images are provided in the Azure Marketplace.

Azure 上运行的所有分发版都要满足一些先决条件。All distributions running on Azure have a number of prerequisites. 本文的内容并不全面,因为每个分发版不同。This article can't be comprehensive, as every distribution is different. 即使满足以下所有条件,但仍可能需要大幅调整 Linux 系统才能让其正常运行。Even if you meet all the criteria below, you may need to significantly tweak your Linux system for it to run properly.

我们建议从某个 Azure 上的 Linux 认可的分发版着手。We recommend that you start with one of the Linux on Azure Endorsed Distributions. 以下文章介绍了如何准备 Azure 上支持的各种认可的 Linux 分发版:The following articles show you how to prepare the various endorsed Linux distributions that are supported on Azure:

本文重点介绍有关在 Azure 上运行 Linux 分发版的一般准则。This article focuses on general guidance for running your Linux distribution on Azure.

常规 Linux 安装说明General Linux Installation Notes

  • Azure 不支持 Hyper-V 虚拟硬盘 (VHDX) 格式,仅支持固定大小的 VHD。The Hyper-V virtual hard disk (VHDX) format isn't 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're using VirtualBox, select Fixed size rather than 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. 但无法更改虚拟机的代次。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 we recommend that you use standard partitions, rather than Logical Volume Manager (LVM) which is the default for many installations. 使用标准分区可避免 LVM 名称与克隆的 VM 发生冲突,特别是在 OS 磁盘曾经连接到另一台相同的 VM 进行故障排除的情况下。Using standard partitions will avoid LVM name conflicts with cloned VMs, particularly if an OS disk is ever 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 necessary. 在 Azure 上首次启动时,预配配置将使用附加到来宾的 UDF 格式媒体传递到 Linux VM。At first boot on Azure the provisioning configuration is passed to the Linux VM by using UDF-formatted media that is attached to the guest. Azure Linux 代理必须装载 UDF 文件系统才能读取其配置和预配 VM。The Azure Linux agent must mount the UDF file system to read its configuration and provision the VM.
  • 低于 2.6.37 的 Linux 内核版本不支持具有更大 VM 大小的 Hyper-V 上的 NUMA。Linux kernel versions earlier than 2.6.37 don't support NUMA on Hyper-V with larger VM sizes. 此问题主要影响使用上游 Red Hat 2.6.32 内核的旧分发版,在 Red Hat Enterprise Linux (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 Red Hat Enterprise Linux (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.
  • 不要在 OS 磁盘上配置交换分区。Don't 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, as described in the following steps.
  • Azure 上的所有 VHD 必须已将虚拟大小调整为 1 MB。All VHDs on Azure must have a virtual size aligned to 1 MB. 从原始磁盘转换为 VHD 时,必须根据以下步骤中所述,确保在转换前原始磁盘大小是 1 MB 的倍数。When converting from a raw disk to VHD you must ensure that the raw disk size is a multiple of 1 MB before conversion, as described in the following steps.

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

Azure 在 Hyper-V 虚拟机监控程序上运行,因此 Linux 需要某些内核模块才能在 Azure 中运行。Azure runs on the Hyper-V hypervisor, so Linux requires certain kernel modules to run in Azure. 如果具有在 Hyper-V 外部创建的虚拟机,Linux 安装程序可能无法在初始 ramdisk(initrd 或 initramfs)中包含 Hyper-V 驱动程序,除非 VM 检测到它正在 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 the VM detects that it's running on a Hyper-V environment. 使用不同的虚拟化系统(例如 Virtualbox、KVM 等)来准备 Linux 映像时,可能需要重新生成 initrd,以便至少 hv_vmbus 和 hv_storvsc 内核模块可在初始 ramdisk 上使用。When using a different virtualization system (such as Virtualbox, KVM, and so on) to prepare your Linux image, you may need to rebuild the initrd so that at least the hv_vmbus and hv_storvsc kernel modules are available on the initial ramdisk. 在基于上游 Red Hat 分发版的系统上(可能还包括其他系统),这是一个已知问题。This known issue is for systems based on the upstream Red Hat distribution, and possibly others.

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

  1. 备份现有 initrd 映像:Back up the existing initrd image:

    cd /boot
    sudo cp initrd-`uname -r`.img  initrd-`uname -r`.img.bak
    
  2. 使用 hv_vmbus 和 hv_storvsc 内核模块重新生成 initrd: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 1 MB. 通常情况下,使用 Hyper-V 创建的 VHD 已正确调整。Typically, VHDs created using Hyper-V are aligned correctly. 如果未正确调整 VHD,在尝试基于 VHD 创建映像时,可能会收到如下错误消息:If the VHD isn't aligned correctly, you may receive an error message similar to the following when you try to create an image from your VHD.

  • VHD http://.blob.core.windows.net/vhds/MyLinuxVM.vhd 的虚拟大小为 21475270656 字节,这是不受支持的。The VHD http://.blob.core.windows.net/vhds/MyLinuxVM.vhd has an unsupported virtual size of 21475270656 bytes. 大小必须是整数(以 MB 为单位)。The size must be a whole number (in MBs).

在这种情况下,可使用 Hyper-V 管理器控制台或 Resize-VHD PowerShell cmdlet 调整 VM 大小。In this case, resize the VM using either the Hyper-V Manager console or the Resize-VHD PowerShell cmdlet. 如果不是在 Windows 环境中运行,我们建议使用 qemu-img 转换(如果需要)并调整 VHD 大小。If you aren't running in a Windows environment, we recommend using qemu-img to convert (if needed) and resize the VHD.

备注

2.2.1 或更高版本的 qemu-img 存在一个已知的 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 或更高版本。We recommend using either qemu-img 2.2.0 or lower, or 2.6 or higher.

  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 磁盘映像。We recommend first converting the VHD to a RAW disk image. 如果已将 VM 映像创建为 RAW 磁盘映像(对于 KVM 等某些虚拟机监控程序,这是默认设置),则可以跳过此步骤。If the VM image was created as a 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 so that the virtual size is aligned to 1 MB. 以下 bash shell 脚本使用 qemu-img info 来确定磁盘映像的虚拟大小,然后将大小计算到下个 1 MB。The following bash shell script uses qemu-img info to determine the virtual size of the disk image, and then calculates the size to the next 1 MB.

    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 above.

    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 (such as 3.x) 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 we recommend running an endorsed distribution that includes these fixes and updates.

如果你正在运行 Red Hat Enterprise Linux 版本 6.0 到 6.3 的一个变体,需要安装适用于 Hyper-V 的最新 LIS 驱动程序If you're running a variant of Red Hat Enterprise Linux versions 6.0 to 6.3, then you'll need to install the latest LIS drivers for Hyper-V. 从 RHEL 6.4+(和派生产品)开始,LIS 驱动程序已包含在内核中,因此,无需其他安装包。Beginning with RHEL 6.4+ (and derivatives) the LIS drivers are already included with the kernel and so no additional installation packages are needed.

如果需要自定义内核,我们建议使用最新的内核版本(例如 3.8+)。If a custom kernel is required, we recommend a recent kernel version (such as 3.8+). 对于维护自己内核的分发版或供应商,需要定期将 LIS 驱动程序从上游内核向后移植到自定义内核。For distributions or vendors who maintain their own kernel, you'll need to regularly backport the LIS drivers from the upstream kernel to your custom kernel. 即使已运行相对较新的内核版本,我们也强烈建议跟踪 LIS 驱动程序中的任何上游修复,并根据需要向后移植这些修复。Even if you're already running a relatively recent kernel version, we highly recommend keeping track of any upstream fixes in the LIS drivers and backport them as needed. LIS 驱动程序源文件的位置在 Linux 内核源树中的 MAINTAINERS 文件中指定:The locations of the LIS driver source files are specified 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/

必须在内核中包含以下修补程序。The following patches must be included in the kernel. 此列表并不完整,并未包括所有分发版。This list can't be complete for all distributions.

Azure Linux 代理The Azure Linux Agent

Azure Linux 代理 waagent 在 Azure 中预配 Linux 虚拟机。The Azure Linux Agent waagent provisions 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 these packages can easily be installed and updated.
  • 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 module as a separate package to be installed.
  • 在某些情况下,Azure Linux 代理可能与 NetworkManager 不兼容。In some cases, the Azure Linux Agent may not be compatible with NetworkManager. 分发版提供的许多 RPM/Deb 包所配置的 NetworkManager 与 waagent 包相冲突。Many of the RPM/Deb packages provided by distributions configure NetworkManager as a conflict to the waagent package. 在这种情况下,它会在你安装 Linux 代理包时卸载 NetworkManager。In these cases, it will uninstall NetworkManager when you install the Linux agent package.
  • Azure Linux 代理必须至少是支持的最低版本The Azure Linux Agent must be at or above the minimum supported version.

一般 Linux 系统要求General Linux System Requirements

  1. 修改 GRUB 或 GRUB2 中的内核引导行并包含以下参数,以便将所有控制台消息发送到第一个串行端口。Modify the kernel boot line in GRUB or GRUB2 to include the following parameters, so that all console messages are sent to the first serial port. 这些消息可以协助 Azure 支持人员调试任何问题。These messages can assist Azure support with debugging any issues.

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

    我们还建议删除以下参数(如果存在)。We also recommend removing the following parameters if they exist.

    rhgb quiet crashkernel=auto
    

    图形引导和静默引导不适用于云环境,在该环境中我们希望将所有日志发送到串行端口。Graphical and quiet boot isn't useful in a cloud environment, where we want all logs sent to the serial port. 可根据需要配置 crashkernel 选项,但请注意此参数会使 VM 中的可用内存量至少减少 128 MB,这对于较小的 VM 而言可能是个问题。The crashkernel option may be left configured if needed, but note that this parameter reduces the amount of available memory in the VM by at least 128 MB, which may be problematic for smaller VM sizes.

  2. 安装 Azure Linux 代理。Install 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.

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

  4. 不要在 OS 磁盘上创建交换空间。Don't 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 时被清空。The local resource disk is a temporary disk, and might be emptied when the VM is deprovisioned. 安装 Azure Linux 代理(上述步骤 2)后,根据需要在 /etc/waagent.conf 中修改以下参数。After installing the Azure Linux Agent (step 2 above), modify the following parameters in /etc/waagent.conf as needed.

        ResourceDisk.Format=y
        ResourceDisk.Filesystem=ext4
        ResourceDisk.MountPoint=/mnt/resource
        ResourceDisk.EnableSwap=y
        ResourceDisk.SwapSizeMB=2048    ## NOTE: Set this to your desired size.
    
  5. 运行以下命令以取消预配虚拟机。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 that says [Errno 5] Input/output error. 此错误消息并不关键,可以忽略。This error message is not critical and can be ignored.

  • 关闭虚拟机并将 VHD 上传到 Azure。Shut down the virtual machine and upload the VHD to Azure.