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

本文假定已在虚拟硬盘中安装了 Oracle Linux 操作系统。This article assumes that you have already installed an Oracle 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.

Oracle Linux 安装说明Oracle Linux installation notes

  • 另请参阅常规 Linux 安装说明,获取更多有关如何为 Azure 准备 Linux 的提示。Please see also General Linux Installation Notes for more tips on preparing Linux for Azure.
  • Hyper-V 和 Azure 支持具有 Unbreakable Enterprise Kernel (UEK) 或 Red Hat 兼容内核的 Oracle Linux。Hyper-V and Azure support Oracle Linux with either the Unbreakable Enterprise Kernel (UEK) or the Red Hat Compatible Kernel.
  • Hyper-V 和 Azure 不支持 Oracle 的 UEK2,因为它不包括所需的驱动程序。Oracle's UEK2 is not supported on Hyper-V and Azure as it does not include the required drivers.
  • 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.
  • 需要装载 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.
  • 在安装 Linux 系统时,建议使用标准分区而不是 LVM(通常是许多安装的默认值)。When installing the Linux system it is recommended that you use standard partitions rather than LVM (often the default for many installations). 这会避免 LVM 与克隆 VM 发生名称冲突,特别是在 OS 磁盘需要连接到另一台 VM 以进行故障排除的情况下。This will avoid LVM name conflicts with cloned VMs, particularly if an OS disk ever needs to be attached to another VM for troubleshooting. 如果需要,可以在数据磁盘上使用 LVMRAIDLVM or RAID may be used on data disks if preferred.
  • 低于 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 内核的旧分发版,并已在 Oracle Linux 6.6 及更高版本中修复This issue primarily impacts older distributions using the upstream Red Hat 2.6.32 kernel, and was fixed in Oracle Linux 6.6 and later
  • 不要在操作系统磁盘上配置交换分区。Do not configure a swap partition on the OS 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.
  • 请确保已启用 Addons 存储库。Make sure that the Addons repository is enabled. 编辑文件 /etc/yum.repos.d/public-yum-ol6.repo(Oracle Linux 6) 或 /etc/yum.repos.d/public-yum-ol7.repo(Oracle Linux 7),并在此文件中的 [ol6_addons] 或 [ol7_addons] 下将行 enabled=0 更改为 enabled=1Edit the file /etc/yum.repos.d/public-yum-ol6.repo(Oracle Linux 6) or /etc/yum.repos.d/public-yum-ol7.repo(Oracle Linux 7), and change the line enabled=0 to enabled=1 under [ol6_addons] or [ol7_addons] in this file.

Oracle Linux 6.4 及更高版本Oracle Linux 6.4 and later

必须在操作系统中完成特定的配置步骤才能使虚拟机在 Azure 中运行。You must complete specific configuration steps in the operating system for the virtual machine to run in Azure.

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

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

  3. 通过运行以下命令卸载 NetworkManager:Uninstall NetworkManager by running the following command:

    # sudo rpm -e --nodeps NetworkManager
    

    注意: 如果未安装此包,则该命令会失败,并显示一条错误消息。Note: If the package is not already installed, this command will fail with an error message. 这是正常情况。This is expected.

  4. 在包含以下文本的 /etc/sysconfig/ 目录中创建一个名为 network 的文件:Create a file named network in the /etc/sysconfig/ directory that contains the following text:

    NETWORKING=yes
    HOSTNAME=localhost.localdomain
    
  5. 在包含以下文本的 /etc/sysconfig/network-scripts/ 目录中创建一个名为 ifcfg-eth0 的文件:Create a file named ifcfg-eth0 in the /etc/sysconfig/network-scripts/ directory that contains 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:

    # chkconfig network on
    
  8. 通过运行以下命令安装 python-pyasn1:Install python-pyasn1 by running the following command:

    # sudo yum install python-pyasn1
    
  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 open "/boot/grub/menu.lst" in a text editor and ensure that the kernel includes the following parameters:

    console=ttyS0 earlyprintk=ttyS0 rootdelay=300
    

    这会确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 支持人员调试问题。This will 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.

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

  11. 通过运行以下命令来安装 Azure Linux 代理。Install the Azure Linux Agent by running the following command. 最新版本为 2.0.15。The latest version is 2.0.15.

    # sudo yum install WALinuxAgent
    

    请注意,如果没有如步骤 2 中所述删除 NetworkManager 包和 NetworkManager-gnome 包,则安装 WALinuxAgent 包将删除它们。Note that installing the WALinuxAgent package will remove the NetworkManager and NetworkManager-gnome packages if they were not already removed as described in step 2.

  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. 请注意,本地资源磁盘是 临时 磁盘,并可能在取消预配 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.
    
  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.


Oracle Linux 7.0 及更高版本Oracle Linux 7.0 and later

Oracle Linux 7 中的更改Changes in Oracle Linux 7

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

  • Azure 支持具有 Unbreakable Enterprise Kernel (UEK) 或 Red Hat 兼容内核的 Oracle Linux。Azure supports Oracle Linux with either the Unbreakable Enterprise Kernel (UEK) or the Red Hat Compatible Kernel. 建议使用具有 UEK 的 Oracle Linux。Oracle Linux with UEK is recommended.
  • 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 a file named network in the /etc/sysconfig/ directory that contains the following text:

    NETWORKING=yes
    HOSTNAME=localhost.localdomain
    
  4. 在包含以下文本的 /etc/sysconfig/network-scripts/ 目录中创建一个名为 ifcfg-eth0 的文件:Create a file named ifcfg-eth0 in the /etc/sysconfig/network-scripts/ directory that contains the following text:

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

    # sudo chkconfig network on
    
  7. 通过运行以下命令安装 python-pyasn1 包:Install the python-pyasn1 package by running the following command:

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

    # sudo yum clean all
    # sudo yum -y update
    
  9. 在 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. 它还会关闭具有 Unbreakable Enterprise Kernel 的 Oracle Linux 7 中 NIC 的命名约定。It also turns off the naming conventions for NICs in Oracle Linux 7 with the Unbreakable Enterprise Kernel. 除此之外,建议 删除 以下参数: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.

  10. 完成后,请按照上面所示编辑“/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
    
  11. 请确保已安装 SSH 服务器且已将其配置为在引导时启动。Ensure that the SSH server is installed and configured to start at boot time. 这通常是默认设置。This is usually the default.

  12. 安装 Azure Linux 代理和依赖项:Install the Azure Linux Agent and dependencies:

    sudo yum install WALinuxAgent
    sudo systemctl enable waagent
    
  13. 安装 cloud-init 来处理预配Install cloud-init to handle the provisioning

    yum install -y cloud-init cloud-utils-growpart gdisk hyperv-daemons
    
    # Configure waagent for cloud-init
    sed -i 's/Provisioning.UseCloudInit=n/Provisioning.UseCloudInit=y/g' /etc/waagent.conf
    sed -i 's/Provisioning.Enabled=y/Provisioning.Enabled=n/g' /etc/waagent.conf
    
    
    echo "Adding mounts and disk_setup to init stage"
    sed -i '/ - mounts/d' /etc/cloud/cloud.cfg
    sed -i '/ - disk_setup/d' /etc/cloud/cloud.cfg
    sed -i '/cloud_init_modules/a\\ - mounts' /etc/cloud/cloud.cfg
    sed -i '/cloud_init_modules/a\\ - disk_setup' /etc/cloud/cloud.cfg
    
    echo "Allow only Azure datasource, disable fetching network setting via IMDS"
    cat > /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg <<EOF
    datasource_list: [ Azure ]
    datasource:
    Azure:
        apply_network_config: False
    EOF
    
    if [[ -f /mnt/resource/swapfile ]]; then
    echo Removing swapfile - RHEL uses a swapfile by default
    swapoff /mnt/resource/swapfile
    rm /mnt/resource/swapfile -f
    fi
    
    echo "Add console log file"
    cat >> /etc/cloud/cloud.cfg.d/05_logging.cfg <<EOF
    
    # This tells cloud-init to redirect its stdout and stderr to
    # 'tee -a /var/log/cloud-init-output.log' so the user can see output
    # there without needing to look on the console.
    output: {all: '| tee -a /var/log/cloud-init-output.log'}
    EOF
    
    
  14. 交换配置:不要在操作系统磁盘上创建交换空间。Swap configuration Do not create swap space on the operating system disk.

    以前,Azure Linux 代理用于使用在 Azure 上预配虚拟机后附加到虚拟机的本地资源磁盘自动配置交换空间。Previously, the Azure Linux Agent was used 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. 但是,现在这由 cloud-init 处理,你不能使用 Linux 代理来格式化资源磁盘以创建交换文件,请在 /etc/waagent.conf 中相应地修改以下参数:However this is now handled by cloud-init, you must not use the Linux Agent to format the resource disk create the swap file, modify the following parameters in /etc/waagent.conf appropriately:

    sed -i 's/ResourceDisk.Format=y/ResourceDisk.Format=n/g' /etc/waagent.conf
    sed -i 's/ResourceDisk.EnableSwap=y/ResourceDisk.EnableSwap=n/g' /etc/waagent.conf
    

    如果需要装载、格式化和创建交换文件,可以使用以下任一方法:If you want mount, format and create swap you can either:

    • 每次创建 VM 时,将此代码作为 cloud-init 配置传入Pass this in as a cloud-init config every time you create a VM

    • 使用融入到映像的 cloud-init 指令,从而在每次创建 VM 时执行此操作:Use a cloud-init directive baked into the image that will do this every time the VM is created:

      cat > /etc/cloud/cloud.cfg.d/00-azure-swap.cfg << EOF
      #cloud-config
      # Generated by Azure cloud image build
      disk_setup:
        ephemeral0:
          table_type: mbr
          layout: [66, [33, 82]]
          overwrite: True
      fs_setup:
        - device: ephemeral0.1
          filesystem: ext4
        - device: ephemeral0.2
          filesystem: swap
      mounts:
        - ["ephemeral0.1", "/mnt"]
        - ["ephemeral0.2", "none", "swap", "sw", "0", "0"]
      EOF
      
  15. 运行以下命令可取消对虚拟机的预配并且对其进行准备以便在 Azure 上进行预配:Run the following commands to deprovision the virtual machine and prepare it for provisioning on Azure:

    # Note: if you are migrating a specific virtual machine and do not wish to create a generalized image,
    # skip the deprovision step
    # sudo rm -rf /var/lib/waagent/
    # sudo rm -f /var/log/waagent.log
    
    # waagent -force -deprovision+user
    # rm -f ~/.bash_history
    
    
    # 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.

后续步骤Next steps

现在,已准备就绪,可以使用 Oracle Linux .vhd 在 Azure 中创建新的虚拟机了。You're now ready to use your Oracle Linux .vhd 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.