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

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

在本文中,将了解如何准备 Red Hat Enterprise Linux (RHEL) 虚拟机,以供在 Azure 中使用。In this article, you will learn how to prepare a Red Hat Enterprise Linux (RHEL) virtual machine for use in Azure. 本文介绍的 RHEL 版本为 6.7+ 和 7.1+。The versions of RHEL that are covered in this article are 6.7+ and 7.1+. 本文所述的用于准备工作的虚拟机监控程序为 Hyper-V、基于内核的虚拟机 (KVM) 和 VMware。The hypervisors for preparation that are covered in this article are Hyper-V, kernel-based virtual machine (KVM), and VMware. 有关参与 Red Hat 云访问计划的资格要求的详细信息,请参阅 Red Hat 的云访问网站在 Azure 上运行 RHELFor more information about eligibility requirements for participating in Red Hat's Cloud Access program, see Red Hat's Cloud Access website and Running RHEL on Azure.

从 Hyper-V 管理器准备基于 Red Hat 的虚拟机Prepare a Red Hat-based virtual machine from Hyper-V Manager

先决条件Prerequisites

本部分假定已经从 Red Hat 网站获取 ISO 文件并将 RHEL 映像安装到虚拟硬盘 (VHD)。This section assumes that you have already obtained an ISO file from the Red Hat website and installed the RHEL image to a virtual hard disk (VHD). 有关如何使用 Hyper-V 管理器来安装操作系统映像的更多详细信息,请参阅安装 Hyper-V 角色和配置虚拟机For more details about how to use Hyper-V Manager to install an operating system image, see Install the Hyper-V Role and Configure a Virtual Machine.

RHEL 安装说明RHEL installation notes

  • Azure 不支持 VHDX 格式。Azure does not support the VHDX format. Azure 仅支持固定 VHD。Azure supports only fixed VHD. 可使用 Hyper-V 管理器将磁盘转换为 VHD 格式,也可以使用 convert-vhd cmdlet。You can use Hyper-V Manager to convert the disk to VHD format, or you can use the convert-vhd cmdlet. 如果使用 VirtualBox,则选择“固定大小”,而不是在创建磁盘时默认动态分配选项。If you use VirtualBox, select Fixed size as opposed to the default dynamically allocated option when you create the disk.
  • Azure 仅支持第 1 代虚拟机。Azure supports only 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-size 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 that's allowed for the VHD is 1,023 GB.
  • 在安装 Linux 操作系统时,建议使用标准分区而不是逻辑卷管理器 (LVM)(通常是许多安装的默认设置)。When you install the Linux operating system, we recommend that you use standard partitions rather than Logical Volume Manager (LVM), which is often the default for many installations. 这种做法可以避免 LVM 名称与克隆的虚拟机冲突,尤其是当需要将操作系统磁盘附加到另一台相同的虚拟机进行故障排除时。This practice will avoid LVM name conflicts with cloned virtual machines, particularly if you ever need to attach an operating system disk to another identical virtual machine for troubleshooting. LVMRAID 可以在数据磁盘上使用。LVM or RAID may be used on data disks.
  • 需要装载通用磁盘格式 (UDF) 文件系统的内核支持。Kernel support for mounting Universal Disk Format (UDF) file systems is required. 在 Azure 上首次启动时,附加到来宾的 UDF 格式媒体会将预配配置传递到 Linux 虚拟机。At first boot on Azure, the UDF-formatted media that is attached to the guest passes the provisioning configuration to the Linux virtual machine. Azure Linux 代理必须能够装载 UDF 文件系统才能读取其配置和预配虚拟机。The Azure Linux Agent must be able to mount the UDF file system to read its configuration and provision the virtual machine.
  • 早于 2.6.37 的 Linux 内核版本不支持虚拟机比较大的 Hyper-V 上的非一致性内存访问 (NUMA)。Versions of the Linux kernel that are earlier than 2.6.37 do not support non-uniform memory access (NUMA) on Hyper-V with larger virtual machine sizes. 此问题主要影响使用上游 Red Hat 2.6.32 内核的旧分发版,在 RHEL 6.6 (kernel-2.6.32-504) 中已得到解决。This issue primarily impacts older distributions that use 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 that run custom kernels that are older than 2.6.37 or RHEL-based kernels that are older than 2.6.32-504 must set the numa=off boot parameter 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 operating system 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 following steps.
  • 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 details can be found in the steps below. 有关详细信息,另请参阅 Linux 安装说明See also Linux Installation Notes for more information.

从 Hyper-V 管理器准备 RHEL 6 虚拟机Prepare a RHEL 6 virtual machine from Hyper-V Manager

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

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

  3. 在 RHEL 6 中,NetworkManager 可能会干扰 Azure Linux 代理。In RHEL 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 /etc/sysconfig/network file, and add the following text:

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

     DEVICE=eth0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
    
  6. 移动(或删除)udev 规则,以避免产生以太网接口的静态规则。Move (or remove) the udev rules to avoid generating static rules for the Ethernet interface. 在 Microsoft Azure 或 Hyper-V 中克隆虚拟机时,这些规则会引发问题:These rules cause problems when you clone a virtual machine in Microsoft 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 that the network service will start at boot time by running the following command:

     # sudo chkconfig network on
    
  8. 注册 Red Hat 订阅,以通过运行以下命令来启用来自 RHEL 存储库中的包的安装:Register your Red Hat subscription to enable the installation of packages from the RHEL repository by running the following command:

     # sudo subscription-manager register --auto-attach --username=XXX --password=XXX
    
  9. WALinuxAgent 包 WALinuxAgent-<version> 已推送到 Red Hat extras 存储库。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 通过运行以下命令启用 extras 存储库:Enable the extras repository by running the following command:

     # subscription-manager repos --enable=rhel-6-server-extras-rpms
    
  10. 在 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 modification, 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 that all console messages are sent to the first serial port, which can assist Azure support with debugging issues.

    除此之外,建议删除以下参数:In addition, we recommended that you 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 选项。You can leave the crashkernel option configured if desired. 请注意,此参数可以将虚拟机中的可用内存量减少 128 MB 或更多。Note that this parameter reduces the amount of available memory in the virtual machine by 128 MB or more. 在遇到较小的虚拟机大小时,此配置可能会有问题。This configuration might be problematic on smaller virtual machine sizes.

    重要

    RHEL 6.5 和更早版本还必须设置 numa=off 内核参数。RHEL 6.5 and earlier must also set the numa=off kernel parameter. 请参阅 Red Hat KB 436883See Red Hat KB 436883.

  11. 请确保安全外壳 (SSH) 服务器已安装且已配置为在引导时启动(默认采用此配置)。Ensure that the secure shell (SSH) server is installed and configured to start at boot time, which is usually the default. 修改 /etc/ssh/sshd_config 以包含以下行:Modify /etc/ssh/sshd_config to include the following line:

    ClientAliveInterval 180
    
  12. 通过运行以下命令来安装 Azure Linux 代理:Install the Azure Linux Agent by running the following command:

    # sudo yum install WALinuxAgent
    
    # sudo chkconfig waagent on
    

    如果步骤 3 中没有删除 NetworkManager 包和 NetworkManager-gnome 包,则安装 WALinuxAgent 包将删除它们。Installing the WALinuxAgent package removes the NetworkManager and NetworkManager-gnome packages if they were not already removed in step 3.

  13. 不要在操作系统磁盘上创建交换空间。Do not create swap space on the operating system disk.

    Azure Linux 代理可使用在 Azure 上预配虚拟机后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space by using the local resource disk that is attached to the virtual machine after the virtual machine is provisioned on Azure. 请注意,本地资源磁盘是临时磁盘,可能在取消预配虚拟机后被清空。Note that the local resource disk is a temporary disk and that it might be emptied when the virtual machine is deprovisioned. 在上一步中安装 Azure Linux 代理后,相应地在 /etc/waagent.conf 中修改以下参数:After you install the Azure Linux Agent in the 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.
    
  14. 通过运行以下命令取消注册订阅(如有必要):Unregister the subscription (if necessary) by running the following command:

    # sudo subscription-manager unregister
    
  15. 运行以下命令可取消对虚拟机的设置并且对其进行准备以便在 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
    
  16. 在 Hyper-V 管理器中单击“操作” > “关闭”。Click Action > Shut Down in Hyper-V Manager. Linux VHD 现已准备好上传到 Azure。Your Linux VHD is now ready to be uploaded to Azure.

从 Hyper-V 管理器准备 RHEL 7 虚拟机Prepare a RHEL 7 virtual machine from Hyper-V Manager

  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 /etc/sysconfig/network file, and add the following text:

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

     DEVICE=eth0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
     NM_CONTROLLED=no
    
  5. 通过运行以下命令,确保网络服务会在引导时启动:Ensure that the network service will start at boot time by running the following command:

     # sudo systemctl enable network
    
  6. 注册 Red Hat 订阅,以通过运行以下命令来启用来自 RHEL 存储库中的包的安装:Register your Red Hat subscription to enable the installation of packages from the RHEL repository by running the following command:

     # sudo subscription-manager register --auto-attach --username=XXX --password=XXX
    
  7. 在 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 modification, 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 that all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 此配置还会关闭 NIC 的新 RHEL 7 命名约定。This configuration also turns off the new RHEL 7 naming conventions for NICs. 除此之外,建议删除以下参数:In addition, we recommend that you 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 选项。You can leave the crashkernel option configured if desired. 请注意,此参数可以将虚拟机中的可用内存量减少 128 MB 或更多,遇到较小的虚拟机大小时,此配置可能会有问题。Note that this parameter reduces the amount of available memory in the virtual machine by 128 MB or more, which might be problematic on smaller virtual machine sizes.

  8. 完成 /etc/default/grub 编辑后,运行以下命令以重新生成 grub 配置:After you are done editing /etc/default/grub, run the following command to rebuild the grub configuration:

     # sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    
  9. 请确保 SSH 服务器已安装且已配置为在引导时启动(默认采用此配置)。Ensure that the SSH server is installed and configured to start at boot time, which is usually the default. 修改 /etc/ssh/sshd_config 以包含以下行:Modify /etc/ssh/sshd_config to include the following line:

     ClientAliveInterval 180
    
  10. WALinuxAgent 包 WALinuxAgent-<version> 已推送到 Red Hat extras 存储库。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 通过运行以下命令启用 extras 存储库:Enable the extras repository by running the following command:

    # subscription-manager repos --enable=rhel-7-server-extras-rpms
    
  11. 通过运行以下命令来安装 Azure Linux 代理:Install the Azure Linux Agent by running the following command:

    # sudo yum install WALinuxAgent
    
    # sudo systemctl enable waagent.service
    
  12. 不要在操作系统磁盘上创建交换空间。Do not create swap space on the operating system disk.

    Azure Linux 代理可使用在 Azure 上预配虚拟机后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space by using the local resource disk that is attached to the virtual machine after the virtual machine is provisioned on Azure. 请注意,本地资源磁盘是临时磁盘,并可能在取消预配虚拟机时被清空。Note that the local resource disk is a temporary disk, and it might be emptied when the virtual machine is deprovisioned. 在上一步中安装 Azure Linux 代理后,相应地在 /etc/waagent.conf 中修改以下参数:After you install the Azure Linux Agent in the 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. 如果想要取消注册订阅,运行以下命令:If you want to unregister the subscription, run the following command:

    # sudo subscription-manager unregister
    
  14. 运行以下命令可取消对虚拟机的设置并且对其进行准备以便在 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
    
  15. 在 Hyper-V 管理器中单击“操作” > “关闭”。Click Action > Shut Down in Hyper-V Manager. Linux VHD 现已准备好上传到 Azure。Your Linux VHD is now ready to be uploaded to Azure.

从 KVM 准备基于 Red Hat 的虚拟机Prepare a Red Hat-based virtual machine from KVM

从 KVM 准备 RHEL 6 虚拟机Prepare a RHEL 6 virtual machine from KVM

  1. 从 Red Hat 网站上下载 RHEL 6 的 KVM 映像。Download the KVM image of RHEL 6 from the Red Hat website.

  2. 设置 root 密码。Set a root password.

    生成加密密码,并复制命令的输出:Generate an encrypted password, and copy the output of the command:

     # openssl passwd -1 changeme
    

    使用 guestfish 设置 root 密码:Set a root password with guestfish:

     # guestfish --rw -a <image-name>
     > <fs> run
     > <fs> list-filesystems
     > <fs> mount /dev/sda1 /
     > <fs> vi /etc/shadow
     > <fs> exit
    

    将 root 用户的第二个字段从“!!”更改Change the second field of the root user from "!!" 为加密密码。to the encrypted password.

  3. 在 KVM 中通过 qcow2 映像创建虚拟机。Create a virtual machine in KVM from the qcow2 image. 将磁盘类型设置为 qcow2,将虚拟网络接口设备型号设置为 virtioSet the disk type to qcow2, and set the virtual network interface device model to virtio. 然后,启动该虚拟机,并以 root 身份登录。Then, start the virtual machine, and sign in as root.

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

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

     DEVICE=eth0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
    
  6. 移动(或删除)udev 规则,以避免产生以太网接口的静态规则。Move (or remove) the udev rules to avoid generating static rules for the Ethernet interface. 在 Azure 或 Hyper-V 中克隆虚拟机时,这些规则会引发问题:These rules cause problems when you clone 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 that the network service will start at boot time by running the following command:

     # chkconfig network on
    
  8. 注册 Red Hat 订阅,以通过运行以下命令来启用来自 RHEL 存储库中的包的安装:Register your Red Hat subscription to enable the installation of packages from the RHEL repository by running the following command:

     # subscription-manager register --auto-attach --username=XXX --password=XXX
    
  9. 在 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 configuration, 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 that all console messages are sent to the first serial port, which can assist Azure support with debugging issues.

    除此之外,建议删除以下参数:In addition, we recommend that you 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 选项。You can leave the crashkernel option configured if desired. 请注意,此参数可以将虚拟机中的可用内存量减少 128 MB 或更多,遇到较小的虚拟机大小时,此配置可能会有问题。Note that this parameter reduces the amount of available memory in the virtual machine by 128 MB or more, which might be problematic on smaller virtual machine sizes.

    重要

    RHEL 6.5 和更早版本还必须设置 numa=off 内核参数。RHEL 6.5 and earlier must also set the numa=off kernel parameter. 请参阅 Red Hat KB 436883See Red Hat KB 436883.

  10. 将 Hyper-V 模块添加到 initramfs 中:Add Hyper-V modules to initramfs:

    编辑 /etc/dracut.conf 并添加以下内容:Edit /etc/dracut.conf, and add the following content:

    add_drivers+="hv_vmbus hv_netvsc hv_storvsc"
    

    重新生成 initramfs:Rebuild initramfs:

    # dracut -f -v
    
  11. 卸载 cloud-init:Uninstall cloud-init:

    # yum remove cloud-init
    
  12. 确保已安装 SSH 服务器且已将其配置为在引导时启动。Ensure that the SSH server is installed and configured to start at boot time:

    # chkconfig sshd on
    

    修改 /etc/ssh/sshd_config 以包含以下行:Modify /etc/ssh/sshd_config to include the following lines:

    PasswordAuthentication yes
    ClientAliveInterval 180
    
  13. WALinuxAgent 包 WALinuxAgent-<version> 已推送到 Red Hat extras 存储库。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 通过运行以下命令启用 extras 存储库:Enable the extras repository by running the following command:

    # subscription-manager repos --enable=rhel-6-server-extras-rpms
    
  14. 通过运行以下命令来安装 Azure Linux 代理:Install the Azure Linux Agent by running the following command:

    # yum install WALinuxAgent
    
    # chkconfig waagent on
    
  15. Azure Linux 代理可使用在 Azure 上预配虚拟机后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space by using the local resource disk that is attached to the virtual machine after the virtual machine is provisioned on Azure. 请注意,本地资源磁盘是临时磁盘,并可能在取消预配虚拟机时被清空。Note that the local resource disk is a temporary disk, and it might be emptied when the virtual machine is deprovisioned. 在上一步中安装 Azure Linux 代理后,相应地在 /etc/waagent.conf 中修改以下参数:After you install the Azure Linux Agent in the 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. 通过运行以下命令取消注册订阅(如有必要):Unregister the subscription (if necessary) by running the following command:

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

    # waagent -force -deprovision
    
    # export HISTSIZE=0
    
    # logout
    
  18. 关闭 KVM 中的虚拟机。Shut down the virtual machine in KVM.

  19. 将 qcow2 映像转换为 VHD 格式。Convert the qcow2 image to the VHD format.

备注

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.

First convert the image to raw format:

    # qemu-img convert -f qcow2 -O raw rhel-6.9.qcow2 rhel-6.9.raw

Make sure that the size of the raw image is aligned with 1 MB. Otherwise, round up the size to align with 1 MB:

    # MB=$((1024*1024))
    # size=$(qemu-img info -f raw --output json "rhel-6.9.raw" | \
      gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')

    # rounded_size=$((($size/$MB + 1)*$MB))
    # qemu-img resize rhel-6.9.raw $rounded_size

Convert the raw disk to a fixed-sized VHD:

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

Or, with qemu version **2.6+** include the `force_size` option:

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

从 KVM 准备 RHEL 7 虚拟机Prepare a RHEL 7 virtual machine from KVM

  1. 从 Red Hat 网站上下载 RHEL 7 的 KVM 映像。Download the KVM image of RHEL 7 from the Red Hat website. 此过程以 RHEL 7 为例。This procedure uses RHEL 7 as the example.

  2. 设置 root 密码。Set a root password.

    生成加密密码,并复制命令的输出:Generate an encrypted password, and copy the output of the command:

     # openssl passwd -1 changeme
    

    使用 guestfish 设置 root 密码:Set a root password with guestfish:

     # guestfish --rw -a <image-name>
     > <fs> run
     > <fs> list-filesystems
     > <fs> mount /dev/sda1 /
     > <fs> vi /etc/shadow
     > <fs> exit
    

    将 root 用户的第二个字段从“!!”更改Change the second field of root user from "!!" 为加密密码。to the encrypted password.

  3. 在 KVM 中通过 qcow2 映像创建虚拟机。Create a virtual machine in KVM from the qcow2 image. 将磁盘类型设置为 qcow2,将虚拟网络接口设备型号设置为 virtioSet the disk type to qcow2, and set the virtual network interface device model to virtio. 然后,启动该虚拟机,并以 root 身份登录。Then, start the virtual machine, and sign in as root.

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

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

     DEVICE=eth0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
     NM_CONTROLLED=no
    
  6. 通过运行以下命令,确保网络服务会在引导时启动:Ensure that the network service will start at boot time by running the following command:

     # sudo systemctl enable network
    
  7. 注册 Red Hat 订阅,以通过运行以下命令来启用来自 RHEL 存储库中的包的安装:Register your Red Hat subscription to enable installation of packages from the RHEL repository by running the following command:

     # subscription-manager register --auto-attach --username=XXX --password=XXX
    
  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 configuration, 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 command also ensures that all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 此命令还会关闭 NIC 的新 RHEL 7 命名约定。The command also turns off the new RHEL 7 naming conventions for NICs. 除此之外,建议删除以下参数:In addition, we recommend that you 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 选项。You can leave the crashkernel option configured if desired. 请注意,此参数可以将虚拟机中的可用内存量减少 128 MB 或更多,遇到较小的虚拟机大小时,此配置可能会有问题。Note that this parameter reduces the amount of available memory in the virtual machine by 128 MB or more, which might be problematic on smaller virtual machine sizes.

  9. 完成 /etc/default/grub 编辑后,运行以下命令以重新生成 grub 配置:After you are done editing /etc/default/grub, run the following command to rebuild the grub configuration:

     # grub2-mkconfig -o /boot/grub2/grub.cfg
    
  10. 将 Hyper-V 模块添加到 initramfs 中。Add Hyper-V modules into initramfs.

    编辑 /etc/dracut.conf 并添加以下内容:Edit /etc/dracut.conf and add content:

    add_drivers+="hv_vmbus hv_netvsc hv_storvsc"
    

    重新生成 initramfs:Rebuild initramfs:

    # dracut -f -v
    
  11. 卸载 cloud-init:Uninstall cloud-init:

    # yum remove cloud-init
    
  12. 确保已安装 SSH 服务器且已将其配置为在引导时启动。Ensure that the SSH server is installed and configured to start at boot time:

    # systemctl enable sshd
    

    修改 /etc/ssh/sshd_config 以包含以下行:Modify /etc/ssh/sshd_config to include the following lines:

    PasswordAuthentication yes
    ClientAliveInterval 180
    
  13. WALinuxAgent 包 WALinuxAgent-<version> 已推送到 Red Hat extras 存储库。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 通过运行以下命令启用 extras 存储库:Enable the extras repository by running the following command:

    # subscription-manager repos --enable=rhel-7-server-extras-rpms
    
  14. 通过运行以下命令来安装 Azure Linux 代理:Install the Azure Linux Agent by running the following command:

    # yum install WALinuxAgent
    

    启用 waagent 服务:Enable the waagent service:

    # systemctl enable waagent.service
    
  15. 不要在操作系统磁盘上创建交换空间。Do not create swap space on the operating system disk.

    Azure Linux 代理可使用在 Azure 上预配虚拟机后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space by using the local resource disk that is attached to the virtual machine after the virtual machine is provisioned on Azure. 请注意,本地资源磁盘是临时磁盘,并可能在取消预配虚拟机时被清空。Note that the local resource disk is a temporary disk, and it might be emptied when the virtual machine is deprovisioned. 在上一步中安装 Azure Linux 代理后,相应地在 /etc/waagent.conf 中修改以下参数:After you install the Azure Linux Agent in the 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. 通过运行以下命令取消注册订阅(如有必要):Unregister the subscription (if necessary) by running the following command:

    # subscription-manager unregister
    
  17. 运行以下命令可取消对虚拟机的设置并且对其进行准备以便在 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
    
  18. 关闭 KVM 中的虚拟机。Shut down the virtual machine in KVM.

  19. 将 qcow2 映像转换为 VHD 格式。Convert the qcow2 image to the VHD format.

备注

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.

First convert the image to raw format:

    # qemu-img convert -f qcow2 -O raw rhel-7.4.qcow2 rhel-7.4.raw

Make sure that the size of the raw image is aligned with 1 MB. Otherwise, round up the size to align with 1 MB:

    # MB=$((1024*1024))
    # size=$(qemu-img info -f raw --output json "rhel-7.4.raw" | \
      gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')

    # rounded_size=$((($size/$MB + 1)*$MB))
    # qemu-img resize rhel-7.4.raw $rounded_size

Convert the raw disk to a fixed-sized VHD:

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

Or, with qemu version **2.6+** include the `force_size` option:

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

从 VMware 准备基于 Red Hat 的虚拟机Prepare a Red Hat-based virtual machine from VMware

先决条件Prerequisites

本部分假设已在 VMware 中安装了 RHEL 虚拟机。This section assumes that you have already installed a RHEL virtual machine in VMware. 有关如何在 VMware 中安装操作系统的详细信息,请参阅 VMware 来宾操作系统安装指南For details about how to install an operating system in VMware, see VMware Guest Operating System Installation Guide.

  • 在安装 Linux 操作系统时,建议使用标准分区而不是 LVM,这通常是许多安装的默认设置。When you install the Linux operating system, we recommend that you use standard partitions rather than LVM, which is often the default for many installations. 这种做法可以避免 LVM 名称与克隆的虚拟机名称冲突,尤其是在需要将操作系统磁盘附加到另一台虚拟机进行故障排除时。This will avoid LVM name conflicts with cloned virtual machine, particularly if an operating system disk ever needs to be attached to another virtual machine for troubleshooting. 如果需要,可以在数据磁盘上使用 LVM 或 RAID。LVM or RAID can be used on data disks if preferred.
  • 不要在操作系统磁盘上配置交换分区。Do not configure a swap partition on the operating system disk. 可将 Linux 代理配置为在临时资源磁盘上创建交换文件。You can configure the Linux agent to create a swap file on the temporary resource disk. 可以在下面的步骤中找到有关此操作的详细信息。You can find more information about this in the steps that follow.
  • 创建虚拟硬盘时,选择“将虚拟磁盘存储为单个文件”。When you create the virtual hard disk, select Store virtual disk as a single file.

从 VMware 准备 RHEL 6 虚拟机Prepare a RHEL 6 virtual machine from VMware

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

     # sudo rpm -e --nodeps NetworkManager
    
  2. 在包含以下文本的 /etc/sysconfig/ 目录中创建一个名为 network 的文件:Create a file named network in the /etc/sysconfig/ directory that contains the following text:

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

     DEVICE=eth0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
    
  4. 移动(或删除)udev 规则,以避免产生以太网接口的静态规则。Move (or remove) the udev rules to avoid generating static rules for the Ethernet interface. 在 Azure 或 Hyper-V 中克隆虚拟机时,这些规则会引发问题:These rules cause problems when you clone 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
    
  5. 通过运行以下命令,确保网络服务会在引导时启动:Ensure that the network service will start at boot time by running the following command:

     # sudo chkconfig network on
    
  6. 注册 Red Hat 订阅,以通过运行以下命令来启用来自 RHEL 存储库中的包的安装:Register your Red Hat subscription to enable the installation of packages from the RHEL repository by running the following command:

     # sudo subscription-manager register --auto-attach --username=XXX --password=XXX
    
  7. WALinuxAgent 包 WALinuxAgent-<version> 已推送到 Red Hat extras 存储库。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 通过运行以下命令启用 extras 存储库:Enable the extras repository by running the following command:

     # subscription-manager repos --enable=rhel-6-server-extras-rpms
    
  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"
    

    这还将确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 支持人员调试问题。This will also ensure that all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 除此之外,建议删除以下参数:In addition, we recommend that you 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 选项。You can leave the crashkernel option configured if desired. 请注意,此参数可以将虚拟机中的可用内存量减少 128 MB 或更多,遇到较小的虚拟机大小时,此配置可能会有问题。Note that this parameter reduces the amount of available memory in the virtual machine by 128 MB or more, which might be problematic on smaller virtual machine sizes.

  9. 将 Hyper-V 模块添加到 initramfs 中:Add Hyper-V modules to initramfs:

    编辑 /etc/dracut.conf 并添加以下内容:Edit /etc/dracut.conf, and add the following content:

     add_drivers+="hv_vmbus hv_netvsc hv_storvsc"
    

    重新生成 initramfs:Rebuild initramfs:

     # dracut -f -v
    
  10. 请确保 SSH 服务器已安装且已配置为在引导时启动(默认采用此配置)。Ensure that the SSH server is installed and configured to start at boot time, which is usually the default. 修改 /etc/ssh/sshd_config 以包含以下行:Modify /etc/ssh/sshd_config to include the following line:

    ClientAliveInterval 180ClientAliveInterval 180

  11. 通过运行以下命令来安装 Azure Linux 代理:Install the Azure Linux Agent by running the following command:

    # sudo yum install WALinuxAgent
    
    # sudo chkconfig waagent on
    
  12. 不要在操作系统磁盘上创建交换空间。Do not create swap space on the operating system disk.

    Azure Linux 代理可使用在 Azure 上预配虚拟机后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space by using the local resource disk that is attached to the virtual machine after the virtual machine is provisioned on Azure. 请注意,本地资源磁盘是临时磁盘,并可能在取消预配虚拟机时被清空。Note that the local resource disk is a temporary disk, and it might be emptied when the virtual machine is deprovisioned. 在上一步中安装 Azure Linux 代理后,相应地在 /etc/waagent.conf 中修改以下参数:After you install the Azure Linux Agent in the 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. 通过运行以下命令取消注册订阅(如有必要):Unregister the subscription (if necessary) by running the following command:

    # sudo subscription-manager unregister
    
  14. 运行以下命令可取消对虚拟机的设置并且对其进行准备以便在 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
    
  15. 关闭虚拟机,并将 VMDK 文件转换为 .vhd 文件。Shut down the virtual machine, and convert the VMDK file to a .vhd file.

备注

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.

First convert the image to raw format:

    # qemu-img convert -f vmdk -O raw rhel-6.9.vmdk rhel-6.9.raw

Make sure that the size of the raw image is aligned with 1 MB. Otherwise, round up the size to align with 1 MB:

    # MB=$((1024*1024))
    # size=$(qemu-img info -f raw --output json "rhel-6.9.raw" | \
      gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')

    # rounded_size=$((($size/$MB + 1)*$MB))
    # qemu-img resize rhel-6.9.raw $rounded_size

Convert the raw disk to a fixed-sized VHD:

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

Or, with qemu version **2.6+** include the `force_size` option:

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

从 VMware 准备 RHEL 7 虚拟机Prepare a RHEL 7 virtual machine from VMware

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

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

     DEVICE=eth0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
     NM_CONTROLLED=no
    
  3. 通过运行以下命令,确保网络服务会在引导时启动:Ensure that the network service will start at boot time by running the following command:

     # sudo systemctl enable network
    
  4. 注册 Red Hat 订阅,以通过运行以下命令来启用来自 RHEL 存储库中的包的安装:Register your Red Hat subscription to enable the installation of packages from the RHEL repository by running the following command:

     # sudo subscription-manager register --auto-attach --username=XXX --password=XXX
    
  5. 在 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 modification, 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 configuration also ensures that all console messages are sent to the first serial port, which can assist Azure support with debugging issues. 此外,还会关闭 NIC 的新 RHEL 7 命名约定。It also turns off the new RHEL 7 naming conventions for NICs. 除此之外,建议删除以下参数:In addition, we recommend that you 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 选项。You can leave the crashkernel option configured if desired. 请注意,此参数可以将虚拟机中的可用内存量减少 128 MB 或更多,遇到较小的虚拟机大小时,此配置可能会有问题。Note that this parameter reduces the amount of available memory in the virtual machine by 128 MB or more, which might be problematic on smaller virtual machine sizes.

  6. 完成 /etc/default/grub 编辑后,运行以下命令以重新生成 grub 配置:After you are done editing /etc/default/grub, run the following command to rebuild the grub configuration:

     # sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    
  7. 将 Hyper-V 模块添加到 initramfs 中。Add Hyper-V modules to initramfs.

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

     add_drivers+="hv_vmbus hv_netvsc hv_storvsc"
    

    重新生成 initramfs:Rebuild initramfs:

     # dracut -f -v
    
  8. 请确保已安装 SSH 服务器且已将其配置为在引导时启动。Ensure that the SSH server is installed and configured to start at boot time. 此设置通常是默认设置。This setting is usually the default. 修改 /etc/ssh/sshd_config 以包含以下行:Modify /etc/ssh/sshd_config to include the following line:

     ClientAliveInterval 180
    
  9. WALinuxAgent 包 WALinuxAgent-<version> 已推送到 Red Hat extras 存储库。The WALinuxAgent package, WALinuxAgent-<version>, has been pushed to the Red Hat extras repository. 通过运行以下命令启用 extras 存储库:Enable the extras repository by running the following command:

     # subscription-manager repos --enable=rhel-7-server-extras-rpms
    
  10. 通过运行以下命令来安装 Azure Linux 代理:Install the Azure Linux Agent by running the following command:

    # sudo yum install WALinuxAgent
    
    # sudo systemctl enable waagent.service
    
  11. 不要在操作系统磁盘上创建交换空间。Do not create swap space on the operating system disk.

    Azure Linux 代理可使用在 Azure 上预配虚拟机后附加到虚拟机的本地资源磁盘自动配置交换空间。The Azure Linux Agent can automatically configure swap space by using the local resource disk that is attached to the virtual machine after the virtual machine is provisioned on Azure. 请注意,本地资源磁盘是临时磁盘,并可能在取消预配虚拟机时被清空。Note that the local resource disk is a temporary disk, and it might be emptied when the virtual machine is deprovisioned. 在上一步中安装 Azure Linux 代理后,相应地在 /etc/waagent.conf 中修改以下参数:After you install the Azure Linux Agent in the 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.
    
  12. 如果想要取消注册订阅,运行以下命令:If you want to unregister the subscription, run the following command:

    # sudo subscription-manager unregister
    
  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. 关闭虚拟机,将 VMDK 文件转换为 VHD 格式。Shut down the virtual machine, and convert the VMDK file to the VHD format.

备注

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.

First convert the image to raw format:

    # qemu-img convert -f vmdk -O raw rhel-7.4.vmdk rhel-7.4.raw

Make sure that the size of the raw image is aligned with 1 MB. Otherwise, round up the size to align with 1 MB:

    # MB=$((1024*1024))
    # size=$(qemu-img info -f raw --output json "rhel-7.4.raw" | \
      gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')

    # rounded_size=$((($size/$MB + 1)*$MB))
    # qemu-img resize rhel-7.4.raw $rounded_size

Convert the raw disk to a fixed-sized VHD:

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

Or, with qemu version **2.6+** include the `force_size` option:

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

使用 kickstart 文件自动从 ISO 准备基于 Red Hat 的虚拟机Prepare a Red Hat-based virtual machine from an ISO by using a kickstart file automatically

从 kickstart 文件准备 RHEL 7 虚拟机Prepare a RHEL 7 virtual machine from a kickstart file

  1. 创建包括以下内容的 kickstart 文件,并保存该文件。Create a kickstart file that includes the following content, and save the file. 有关 kickstart 安装的详细信息,请参阅 Kickstart 安装指南For details about kickstart installation, see the Kickstart Installation Guide.

    # Kickstart for provisioning a RHEL 7 Azure VM
    
    # System authorization information
      auth --enableshadow --passalgo=sha512
    
    # Use graphical install
    text
    
    # Do not run the Setup Agent on first boot
    firstboot --disable
    
    # Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    
    # System language
    lang en_US.UTF-8
    
    # Network information
    network  --bootproto=dhcp
    
    # Root password
    rootpw --plaintext "to_be_disabled"
    
    # System services
    services --enabled="sshd,waagent,NetworkManager"
    
    # System timezone
    timezone Etc/UTC --isUtc --ntpservers 0.rhel.pool.ntp.org,1.rhel.pool.ntp.org,2.rhel.pool.ntp.org,3.rhel.pool.ntp.org
    
    # Partition clearing information
    clearpart --all --initlabel
    
    # Clear the MBR
    zerombr
    
    # Disk partitioning information
    part /boot --fstype="xfs" --size=500
    part / --fstyp="xfs" --size=1 --grow --asprimary
    
    # System bootloader configuration
    bootloader --location=mbr
    
    # Firewall configuration
    firewall --disabled
    
    # Enable SELinux
    selinux --enforcing
    
    # Don't configure X
    skipx
    
    # Power down the machine after install
    poweroff
    
    %packages
    @base
    @console-internet
    chrony
    sudo
    parted
    -dracut-config-rescue
    
    %end
    
    %post --log=/var/log/anaconda/post-install.log
    
    #!/bin/bash
    
    # Register Red Hat Subscription
    subscription-manager register --username=XXX --password=XXX --auto-attach --force
    
    # Install latest repo update
    yum update -y
    
    # Enable extras repo
    subscription-manager repos --enable=rhel-7-server-extras-rpms
    
    # Install WALinuxAgent
    yum install -y WALinuxAgent
    
    # Unregister Red Hat subscription
    subscription-manager unregister
    
    # Enable waaagent at boot-up
    systemctl enable waagent
    
    # Disable the root account
    usermod root -p '!!'
    
    # Configure swap in WALinuxAgent
    sed -i 's/^\(ResourceDisk\.EnableSwap\)=[Nn]$/\1=y/g' /etc/waagent.conf
    sed -i 's/^\(ResourceDisk\.SwapSizeMB\)=[0-9]*$/\1=2048/g' /etc/waagent.conf
    
    # Set the cmdline
    sed -i 's/^\(GRUB_CMDLINE_LINUX\)=".*"$/\1="console=tty1 console=ttyS0 earlyprintk=ttyS0 rootdelay=300"/g' /etc/default/grub
    
    # Enable SSH keepalive
    sed -i 's/^#\(ClientAliveInterval\).*$/\1 180/g' /etc/ssh/sshd_config
    
    # Build the grub cfg
    grub2-mkconfig -o /boot/grub2/grub.cfg
    
    # Configure network
    cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    ONBOOT=yes
    BOOTPROTO=dhcp
    TYPE=Ethernet
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    NM_CONTROLLED=no
    EOF
    
    # Deprovision and prepare for Azure
    waagent -force -deprovision
    
    %end
    
  2. 将 kickstart 文件放在安装系统可以访问的位置。Place the kickstart file where the installation system can access it.

  3. 在 Hyper-V 管理器中,创建新的虚拟机。In Hyper-V Manager, create a new virtual machine. 在“连接虚拟硬盘”页上,选择“稍后附加虚拟硬盘”,并完成新建虚拟机向导。On the Connect Virtual Hard Disk page, select Attach a virtual hard disk later, and complete the New Virtual Machine Wizard.

  4. 打开虚拟机设置:Open the virtual machine settings:

    a.a. 将新的虚拟硬盘附加到虚拟机。Attach a new virtual hard disk to the virtual machine. 请务必选择“VHD 格式”和“固定大小”。Make sure to select VHD Format and Fixed Size.

    b.b. 将安装 ISO 附加到 DVD 光驱。Attach the installation ISO to the DVD drive.

    c.c. 将 BIOS 设置为从 CD 启动。Set the BIOS to boot from CD.

  5. 启动虚拟机。Start the virtual machine. 当安装指南出现时,请按 Tab 键来配置启动选项。When the installation guide appears, press Tab to configure the boot options.

  6. 在启动选项的末尾输入 inst.ks=<the location of the kickstart file>,并按 EnterEnter inst.ks=<the location of the kickstart file> at the end of the boot options, and press Enter.

  7. 等待安装完成。Wait for the installation to finish. 完成后,虚拟机会自动关闭。When it's finished, the virtual machine will be shut down automatically. Linux VHD 现已准备好上传到 Azure。Your Linux VHD is now ready to be uploaded to Azure.

已知问题Known issues

使用非 Hyper-V 虚拟机监控程序时,初始 RAM 磁盘未包含 Hyper-V 驱动程序The Hyper-V driver could not be included in the initial RAM disk when using a non-Hyper-V hypervisor

在某些情况下,Linux 安装程序可能无法在初始 RAM 磁盘(initrd 或 initramfs)中包含 Hyper-V 驱动程序,除非 Linux 检测到它正在 Hyper-V 环境中运行。In some cases, Linux installers might not include the drivers for Hyper-V in the initial RAM disk (initrd or initramfs) unless Linux detects that it is running in a Hyper-V environment.

使用不同虚拟化系统(即 Virtualbox、Xen 等)来准备 Linux 映像时,可能需要重新生成 initrd 以确保至少 hv_vmbus 和 hv_storvsc 内核模块可在初始 RAM 磁盘上使用。When you're using a different virtualization system (that is, Virtualbox, Xen, etc.) to prepare your Linux image, you might need to rebuild initrd to ensure that at least the hv_vmbus and hv_storvsc kernel modules are available on the initial RAM disk. 至少在基于上游 Red Hat 分发的系统上这是一个已知问题。This is a known issue at least on systems that are based on the upstream Red Hat distribution.

要解决此问题,请将 Hyper-V 模块添加到 initramfs 并进行重新生成:To resolve this issue, add Hyper-V modules to initramfs and rebuild it:

编辑 /etc/dracut.conf 并添加以下内容:Edit /etc/dracut.conf, and add the following content:

    add_drivers+="hv_vmbus hv_netvsc hv_storvsc"

重新生成 initramfs:Rebuild initramfs:

    # dracut -f -v

有关详细信息,请参阅有关重新生成 initramfs 的信息。For more details, see the information about rebuilding initramfs.

后续步骤Next steps

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

有关已通过认证可运行 Red Hat Enterprise Linux 的虚拟机监控程序的更多详细信息,请参阅 Red Hat 网站For more details about the hypervisors that are certified to run Red Hat Enterprise Linux, see the Red Hat website.