为 Azure 准备基于 CentOS 的虚拟机Prepare a CentOS-based virtual machine for Azure

了解如何创建和上传包含基于 CentOS 的 Linux 操作系统的 Azure 虚拟硬盘 (VHD)。Learn to create and upload an Azure virtual hard disk (VHD) that contains a CentOS-based Linux operating system.

先决条件Prerequisites

本文假设已在虚拟硬盘中安装 CentOS(或类似的衍生产品)Linux 操作系统。This article assumes that you have already installed a CentOS (or similar derivative) 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.

CentOS 安装说明CentOS 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. 如果使用 VirtualBox,则意味着选择的是“固定大小”,而不是在创建磁盘时动态分配默认大小。 If you are using VirtualBox this means selecting Fixed size as opposed to the default dynamically allocated when creating the disk.

  • 在安装 Linux 系统时,建议使用标准分区而不是 LVM(通常是许多安装的默认值)。When installing the Linux system it is recommended that you use standard partitions rather than LVM (often the default for many installations). 这可以避免与克隆 VM 发生 LVM 名称冲突,尤其是在需要将 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.

  • 不要在操作系统磁盘上配置交换分区。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.

CentOS 6.xCentOS 6.x

  1. 在 Hyper-V 管理器中,选择虚拟机。In Hyper-V Manager, select the virtual machine.

  2. 单击“连接”打开该虚拟机的控制台窗口。 Click Connect to open a console window for the virtual machine.

  3. 在 CentOS 6 中,NetworkManager 可能会干扰 Azure Linux 代理。In CentOS 6, NetworkManager can interfere with the Azure Linux agent. 请运行以下命令来卸载该包:Uninstall this package by running the following command:

    sudo rpm -e --nodeps NetworkManager
    
  4. 创建或编辑 /etc/sysconfig/network 文件,添加以下文本:Create or edit the file /etc/sysconfig/network and add the following text:

    NETWORKING=yes
    HOSTNAME=localhost.localdomain
    
  5. 创建或编辑 /etc/sysconfig/network-scripts/ifcfg-eth0 文件,添加以下文本:Create or edit the file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the following text:

    DEVICE=eth0
    ONBOOT=yes
    BOOTPROTO=dhcp
    TYPE=Ethernet
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    
  6. 修改 udev 规则,以免为以太网接口生成静态规则。Modify udev rules to avoid generating static rules for the Ethernet interface(s). 在 Azure 或 Hyper-V 中克隆虚拟机时,这些规则可能会引发问题:These rules can cause problems when cloning a virtual machine in Azure or Hyper-V:

    sudo ln -s /dev/null /etc/udev/rules.d/75-persistent-net-generator.rules
    sudo rm -f /etc/udev/rules.d/70-persistent-net.rules
    
  7. 通过运行以下命令,确保网络服务在引导时启动:Ensure the network service will start at boot time by running the following command:

    sudo chkconfig network on
    
  8. 如果要使用 Azure 数据中心托管的 OpenLogic 镜像,请使用以下存储库替换 /etc/yum.repos.d/CentOS-Base.repo 文件。If you would like to use the OpenLogic mirrors that are hosted within the Azure datacenters, then replace the /etc/yum.repos.d/CentOS-Base.repo file with the following repositories. 这还会添加包含 Azure Linux 代理等其他包的 [openlogic] 存储库:This will also add the [openlogic] repository that includes additional packages such as the Azure Linux agent:

    [openlogic]
    name=CentOS-$releasever - openlogic packages for $basearch
    baseurl=http://olcentgbl.trafficmanager.net/openlogic/$releasever/openlogic/$basearch/
    enabled=1
    gpgcheck=0
    
    [base]
    name=CentOS-$releasever - Base
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/os/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
    #released updates
    [updates]
    name=CentOS-$releasever - Updates
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/updates/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/extras/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/centosplus/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    
    #contrib - packages by Centos Users
    [contrib]
    name=CentOS-$releasever - Contrib
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/contrib/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    

    Note

    本指南的余下部分假设用户至少会使用 [openlogic] 存储库,下面将使用该存储库安装 Azure Linux 代理。The rest of this guide will assume you are using at least the [openlogic] repo, which will be used to install the Azure Linux agent below.

  9. 将以下行添加到 /etc/yum.conf:Add the following line to /etc/yum.conf:

    http_caching=packages
    
  10. 运行以下命令,清除当前的 yum 元数据并使用最新的包更新系统:Run the following command to clear the current yum metadata and update the system with the latest packages:

    yum clean all
    

    建议将所有包都更新到最新版本,除非要为旧版 CentOS 创建映像:Unless you are creating an image for an older version of CentOS, it is recommended to update all the packages to the latest:

    sudo yum -y update
    

    运行此命令后,可能需要重新启动。A reboot may be required after running this command.

  11. (可选)安装适用于 Linux Integration Services (LIS) 的驱动程序。(Optional) Install the drivers for the Linux Integration Services (LIS).

    Important

    此步骤对于 CentOS 6.3 及更低版本来说是 必需 步骤,对于更高版本来说是可选步骤。The step is required for CentOS 6.3 and earlier, and optional for later releases.

    sudo rpm -e hypervkvpd  ## (may return error if not installed, that's OK)
    sudo yum install microsoft-hyper-v
    

    也可按照 LIS 下载页 上的手动安装说明进行操作,将 RPM 安装到 VM。Alternatively, you can follow the manual installation instructions on the LIS download page to install the RPM onto your VM.

  12. 安装 Azure Linux 代理和依赖项。Install the Azure Linux Agent and dependencies. 启动并启用 waagent 服务:Start and enable waagent service:

    sudo yum install python-pyasn1 WALinuxAgent
    sudo service waagent start
    sudo chkconfig waagent on
    

    如果没有如步骤 3 中所述删除 NetworkManager 包和 NetworkManager-gnome 包,则 WALinuxAgent 包会将其删除。The WALinuxAgent package will remove the NetworkManager and NetworkManager-gnome packages if they were not already removed as described in step 3.

  13. 在 grub 配置中修改内核引导行,使其包含 Azure 的其他内核参数。Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. 为此,请在文本编辑器中打开 /boot/grub/menu.lst ,并确保默认内核包含以下参数:To do this, open /boot/grub/menu.lst in a text editor and ensure that the default kernel includes the following parameters:

    console=ttyS0 earlyprintk=ttyS0 rootdelay=300
    

    这还可以确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 支持人员调试问题。This will also ensure all console messages are sent to the first serial port, which can assist Azure support with debugging issues.

    除此之外,建议 删除 以下参数:In addition to the above, it is recommended to remove the following parameters:

    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.

    Important

    CentOS 6.5 和更早版本还必须设置内核参数 numa=offCentOS 6.5 and earlier must also set the kernel parameter numa=off.

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

  15. 不要在 OS 磁盘上创建交换空间。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. 请注意,本地资源磁盘是 临时 磁盘,并可能在取消设置虚拟机时被清空。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.
    
  16. 运行以下命令可取消对虚拟机的预配并且对其进行准备以便在 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
    
  17. 在 Hyper-V 管理器中单击“操作”->“关闭” 。Click Action -> Shut Down in Hyper-V Manager. 现在,准备将 Linux VHD 上传到 Azure。Your Linux VHD is now ready to be uploaded to Azure.

CentOS 7.0+CentOS 7.0+

CentOS 7(和类似衍生产品)中的更改Changes in CentOS 7 (and similar derivatives)

为 Azure 准备 CentOS 7 虚拟机非常类似于 CentOS 6,但有几个值得注意的重要区别:Preparing a CentOS 7 virtual machine for Azure is very similar to CentOS 6, however there are several important differences worth noting:

  • NetworkManager 包不再与 Azure Linux 代理冲突。The NetworkManager package no longer conflicts with the Azure Linux agent. 默认会安装此包,建议不要删除。This package is installed by default and we recommend that it is not removed.
  • GRUB2 现在用作默认引导加载程序,因此编辑内核参数的过程已更改(见下文)。GRUB2 is now used as the default bootloader, so the procedure for editing kernel parameters has changed (see below).
  • XFS 现在是默认文件系统。XFS is now the default file system. 如果需要,仍可以使用 ext4 文件系统。The ext4 file system can still be used if desired.

配置步骤Configuration Steps

  1. 在 Hyper-V 管理器中,选择虚拟机。In Hyper-V Manager, select the virtual machine.

  2. 单击“连接” 以打开该虚拟机的控制台窗口。Click Connect to open a console window for the virtual machine.

  3. 创建或编辑 /etc/sysconfig/network 文件,添加以下文本:Create or edit the file /etc/sysconfig/network and add the following text:

    NETWORKING=yes
    HOSTNAME=localhost.localdomain
    
  4. 创建或编辑 /etc/sysconfig/network-scripts/ifcfg-eth0 文件,添加以下文本:Create or edit the file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the following text:

    DEVICE=eth0
    ONBOOT=yes
    BOOTPROTO=dhcp
    TYPE=Ethernet
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    NM_CONTROLLED=no
    
  5. 修改 udev 规则,以免为以太网接口生成静态规则。Modify udev rules to avoid generating static rules for the Ethernet interface(s). 在 Azure 或 Hyper-V 中克隆虚拟机时,这些规则可能会引发问题:These rules can cause problems when cloning a virtual machine in Azure or Hyper-V:

    sudo ln -s /dev/null /etc/udev/rules.d/75-persistent-net-generator.rules
    
  6. 如果要使用 Azure 数据中心托管的 OpenLogic 镜像,请使用以下存储库替换 /etc/yum.repos.d/CentOS-Base.repo 文件。If you would like to use the OpenLogic mirrors that are hosted within the Azure datacenters, then replace the /etc/yum.repos.d/CentOS-Base.repo file with the following repositories. 这还会添加包含 Azure Linux 代理包的 [openlogic] 存储库:This will also add the [openlogic] repository that includes packages for the Azure Linux agent:

    [openlogic]
    name=CentOS-$releasever - openlogic packages for $basearch
    baseurl=http://olcentgbl.trafficmanager.net/openlogic/$releasever/openlogic/$basearch/
    enabled=1
    gpgcheck=0
    
    [base]
    name=CentOS-$releasever - Base
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/os/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    #released updates
    [updates]
    name=CentOS-$releasever - Updates
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/updates/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/extras/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
    baseurl=http://olcentgbl.trafficmanager.net/centos/$releasever/centosplus/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    

    Note

    本指南的余下部分假设用户至少会使用 [openlogic] 存储库,下面将使用该存储库安装 Azure Linux 代理。The rest of this guide will assume you are using at least the [openlogic] repo, which will be used to install the Azure Linux agent below.

  7. 运行以下命令清除当前 yum 元数据并安装所有更新:Run the following command to clear the current yum metadata and install any updates:

    sudo yum clean all
    

    建议将所有包都更新到最新版本,除非要为旧版 CentOS 创建映像:Unless you are creating an image for an older version of CentOS, it is recommended to update all the packages to the latest:

    sudo yum -y update
    

    运行此命令后,可能需要重新启动。A reboot maybe required after running this command.

  8. 在 grub 配置中修改内核引导行,使其包含 Azure 的其他内核参数。Modify the kernel boot line in your grub configuration to include additional kernel parameters for Azure. 为此,请在文本编辑器中打开 /etc/default/grub 并编辑 GRUB_CMDLINE_LINUX 参数,例如:To do this, open /etc/default/grub in a text editor and edit the GRUB_CMDLINE_LINUX parameter, for example:

    GRUB_CMDLINE_LINUX="rootdelay=300 console=ttyS0 earlyprintk=ttyS0 net.ifnames=0"
    

    这还可以确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 支持人员调试问题。This will also ensure all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 此外还会关闭新的针对 NIC 的 CentOS 7 命名约定。It also turns off the new CentOS 7 naming conventions for NICs. 除此之外,建议 删除 以下参数:In addition to the above, it is recommended to remove the following parameters:

    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 选项,但请注意此参数会使 VM 中的可用内存量减少 128 MB 或更多,这在较小的 VM 上可能会出现问题。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.

  9. 按照上面所示完成编辑 /etc/default/grub 后,运行以下命令以重新生成 grub 配置:Once you are done editing /etc/default/grub per above, run the following command to rebuild the grub configuration:

    sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    
  10. 如果从 VMware、VirtualBox 或 KVM 生成映像:请确保 initramfs 中包含 HYPER-V 驱动程序:If building the image from VMware, VirtualBox or KVM: Ensure the Hyper-V drivers are included in the initramfs:

    编辑 /etc/dracut.conf,添加内容:Edit /etc/dracut.conf, add content:

    add_drivers+="hv_vmbus hv_netvsc hv_storvsc"
    

    重新生成 initramfs:Rebuild the initramfs:

    sudo dracut -f -v
    
  11. 安装 Azure Linux 代理和依赖项:Install the Azure Linux Agent and dependencies:

    sudo yum install python-pyasn1 WALinuxAgent
    sudo systemctl enable waagent
    
  12. 不要在 OS 磁盘上创建交换空间。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. 请注意,本地资源磁盘是 临时 磁盘,并可能在取消设置虚拟机时被清空。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.
    
  13. 运行以下命令可取消对虚拟机的预配并且对其进行准备以便在 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
    
  14. 在 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

现在,可以使用 CentOS Linux 虚拟硬盘在 Azure 中创建新的 Azure 虚拟机了。You're now ready to use your CentOS 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.