你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

为 Azure 准备 SLES 或 openSUSE Leap 虚拟机

适用于: ✔️ Linux VM ✔️ 灵活规模集 适用于: ✔️ 统一规模集

在某些情况下,你可能希望在 Azure 环境中使用自定义的 SUSE Linux Enterprise Server (SLES) 或 openSUSE Leap Linux 虚拟机 (VM),并能够通过自动化来生成这些类型的 VM。 本文演示如何创建和上传包含 SUSE Linux 操作系统的自定义 Azure 虚拟硬盘 (VHD)。

先决条件

本文假定你已在虚拟硬盘中安装了 SLES 或 openSUSE Leap Linux 操作系统。 可使用多种工具创建 .vhd 文件。 例如,可使用 Hyper-V 等虚拟化解决方案。 有关说明,请参阅安装 Hyper-V 并创建虚拟机

SLES/openSUSE Leap 安装说明

  • 有关为 Azure 准备 Linux 映像的更多提示,请参阅常规 Linux 安装说明
  • Azure 不支持 Windows 硬盘映像 (.vhdx) 文件。 仅虚拟机外部支持 VHD (.vhd) 文件。 可使用 Hyper-V 管理器或 Convert-VHD cmdlet 将磁盘转换为 VHD 格式。
  • Azure 支持 Gen1(BIOS 引导)和 Gen2(UEFI 引导)虚拟机。
  • 必须在内核中启用虚拟文件分配表 (VFAT) 内核模块。
  • 不要在 OS 磁盘上配置交换分区。 可将 Linux 代理配置为在临时资源磁盘上创建交换文件。 有关配置交换空间的详细信息,请参阅本文后面的步骤。
  • Azure 上所有 VHD 的虚拟大小必须已按 1 MB 对齐。 从原始磁盘转换为 VHD 时,请确保在转换前的原始磁盘大小是 1 MB 的倍数。 有关详细信息,请参阅常规 Linux 安装说明

注意

Cloud-init 版本 21.2 或更高版本会删除用户定义的函数 (UDF) 要求。 但如果未启用 udf 模块,CD-ROM 将不会在预配期间装载,这会阻止应用自定义数据。 解决方法是应用用户数据。 但是,与自定义数据不同,用户数据不会加密。 有关详细信息,请参阅 cloud-init 文档中的用户数据格式

使用 SUSE Studio

SUSE Studio 可以轻松创建和管理 Azure 和 Hyper-V 的 SLES 和 openSUSE Leap 映像。 要自定义用户自己的 SUSE 和 openSUSE Leap 映像,推荐的方法是 SUSE Studio。

作为一种构建你自己的 VHD 的替代方法,SUSE 也会在 VM Depot 上发布 SLES 的 BYOS(自带订阅)映像。

为 Azure 准备 SLES

  1. 根据需要配置 Azure 和 Hyper-V 模块。

    如果软件虚拟机监控程序不是 Hyper-V,则需要将其他模块添加到初始 RAM 磁盘 (initramfs) 中,才能在 Azure 中成功启动。

    编辑 /etc/dracut.conf 文件并在其中添加以下行

    add_drivers+=" hv_vmbus hv_netvsc hv_storvsc "
    

    运行 dracut 命令以重新生成 initramfs 文件:

    sudo dracut --verbose --force
    
  2. 设置串行控制台。

    要成功使用串行控制台,必须在 /etc/defaults/grub 文件中设置多个变量,并在服务器上重新创建 GRUB

    # Add console=ttyS0 and earlyprintk=ttS0 to the variable.
    # Remove "splash=silent" and "quiet" options.
    GRUB_CMDLINE_LINUX_DEFAULT="audit=1 no-scroll fbcon=scrollback:0 mitigations=auto security=apparmor crashkernel=228M,high crashkernel=72M,low console=ttyS0 earlyprintk=ttyS0"
    
    # Add "console serial" to GRUB_TERMINAL.
    GRUB_TERMINAL="console serial"
    
    # Set the GRUB_SERIAL_COMMAND variable.
    
    GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
    
    /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg
    
  3. 注册 SUSE Linux Enterprise 系统以允许其下载更新并安装程序包。

  4. 使用最新修补程序更新系统:

    sudo zypper update
    
  5. 安装 Azure VM 代理 (waagent) 和 cloud-init:

    sudo SUSEConnect -p sle-module-public-cloud/15.2/x86_64  (SLES 15 SP2)
    sudo zypper refresh
    sudo zypper install python-azure-agent
    sudo zypper install cloud-init
    
  6. 启用 waagent 和 cloud-init 以便在引导时启动:

    sudo systemctl enable  waagent
    sudo systemctl enable cloud-init-local.service
    sudo systemctl enable cloud-init.service
    sudo systemctl enable cloud-config.service
    sudo systemctl enable cloud-final.service
    sudo systemctl daemon-reload
    sudo cloud-init clean
    
  7. 更新 cloud-init 配置:

    cat <<EOF | sudo tee /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg
    datasource_list: [ Azure ]
    datasource:
        Azure:
            apply_network_config: False
    
    EOF
    
    sudo cat <<EOF | sudo tee  /etc/cloud/cloud.cfg.d/05_logging.cfg
    # 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
    
    # Make sure mounts and disk_setup are in the init stage:
    echo "Adding mounts and disk_setup to init stage"
    sudo sed -i '/ - mounts/d' /etc/cloud/cloud.cfg
    sudo sed -i '/ - disk_setup/d' /etc/cloud/cloud.cfg
    sudo sed -i '/cloud_init_modules/a\\ - mounts' /etc/cloud/cloud.cfg
    sudo sed -i '/cloud_init_modules/a\\ - disk_setup' /etc/cloud/cloud.cfg
    
  8. 如果要装载、格式化和创建交换分区,一个选项是在每次创建 VM 时传入 cloud-init 配置。

    另一个选项是使用映像中的 cloud-init 指令,在每次创建 VM 时配置交换空间:

    cat  <<EOF | sudo tee -a /etc/systemd/system.conf
    'DefaultEnvironment="CLOUD_CFG=/etc/cloud/cloud.cfg.d/00-azure-swap.cfg"'
    EOF
    
    cat <<EOF | sudo tee /etc/cloud/cloud.cfg.d/00-azure-swap.cfg
    #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,nofail,x-systemd.requires=cloud-init.service,x-systemd.device-timeout=2", "0", "0"]
    EOF
    
  9. 以前,在 Azure 上预配虚拟机后,Azure Linux 代理会使用附加到虚拟机的本地资源磁盘来自动配置交换空间。 现在,由于 cloud-init 会处理此步骤,因此你不得使用 Azure Linux 代理格式化资源磁盘或创建交换文件。 可使用以下命令对 /etc/waagent.conf 进行适当的修改

    sudo sed -i 's/Provisioning.UseCloudInit=n/Provisioning.UseCloudInit=auto/g' /etc/waagent.conf
    sudo sed -i 's/Provisioning.Enabled=y/Provisioning.Enabled=n/g' /etc/waagent.conf
    sudo sed -i 's/ResourceDisk.Format=y/ResourceDisk.Format=n/g' /etc/waagent.conf
    sudo sed -i 's/ResourceDisk.EnableSwap=y/ResourceDisk.EnableSwap=n/g' /etc/waagent.conf
    

    注意

    如果使用的 cloud-init 版本低于 21.2,请确保启用 udf 模块。 删除或禁用它将导致预配/启动失败。 Cloud-init 版本 21.2 或更高版本会删除 UDF 要求。

  10. 确保 /etc/fstab 文件使用磁盘其 UUID (by-uuid) 引用磁盘

  11. 删除 udev 规则和网络适配器配置文件,以避免为以太网接口生成静态规则。 在 Microsoft Azure 或 Hyper-V 中克隆虚拟机时,这些规则会引发问题。

    sudo rm -f /etc/udev/rules.d/70-persistent-net.rules
    sudo rm -f /etc/udev/rules.d/85-persistent-net-cloud-init.rules
    sudo rm -f /etc/sysconfig/network/ifcfg-eth*
    
  12. 建议编辑 /etc/sysconfig/network/dhcp 文件,并将 参数更改为以下值DHCLIENT_SET_HOSTNAME

    DHCLIENT_SET_HOSTNAME="no"
    
  13. 在 /etc/sudoers 文件中,注释掉或删除以下行(如果存在)

    Defaults targetpw   # Ask for the password of the target user i.e. root
    ALL    ALL=(ALL) ALL   # WARNING! Only use this setting together with 'Defaults targetpw'!
    
  14. 确保已安装安全外壳 (SSH) 服务器且已将其配置为在引导时启动:

    sudo systemctl enable sshd
    
  15. 清理 cloud-init 阶段:

    sudo cloud-init clean --seed --logs
    
  16. 运行以下命令以取消对虚拟机的预配,并准备虚拟机以便在 Azure 上进行预配。

    如果要迁移特定的虚拟机,但不希望创建通用化映像,请跳过取消预配步骤。

    sudo rm -f /var/log/waagent.log
    sudo waagent -force -deprovision+user
    sudo export HISTSIZE=0
    sudo rm -f ~/.bash_history
    

准备 openSUSE 15.2+

  1. 在 Hyper-V 管理器的中间窗格中,选择虚拟机。

  2. 选择“连接”,打开虚拟机窗口。

  3. 在终端中运行命令“zypper lr”。 如果此命令返回的输出类似于下面的示例,则表示已按预期配置了存储库,不需要进行任何调整。 (版本号可能有所不同。)

    # Alias “属性” 已启用 刷新
    1 Cloud:Tools_15.2 Cloud:Tools_15.2
    2 openSUSE_15.2_OSS openSUSE_15.2_OSS
    3 openSUSE_15.2_Updates openSUSE_15.2_Updates

    如果该命令返回“未定义存储库,”,则使用以下命令来添加这些存储库:

    sudo zypper ar -f http://download.opensuse.org/repositories/Cloud:Tools/openSUSE_15.2 Cloud:Tools_15.2
    sudo zypper ar -f https://download.opensuse.org/distribution/15.2/repo/oss openSUSE_15.2_OSS
    sudo zypper ar -f http://download.opensuse.org/update/15.2 openSUSE_15.2_Updates
    

    然后,可通过再次运行命令“zypper lr”验证已添加存储库。 如果未启用某个相关的更新存储库,请使用以下命令启用该存储库:

    sudo zypper mr -e [NUMBER OF REPOSITORY]
    
  4. 将内核更新为可用的最新版本:

    sudo zypper up kernel-default
    

    或者使用所有最新补丁更新操作系统:

    sudo zypper update
    
  5. 安装 Azure Linux 代理:

    sudo zypper install WALinuxAgent
    
  6. 在 GRUB 配置中修改内核引导行,以包含 Azure 的其他内核参数。 为此,请在文本编辑器中打开“/boot/grub/menu.lst”,并确保默认内核包含以下参数

     console=ttyS0 earlyprintk=ttyS0
    

    此选项可确保所有控制台消息都发送到第一个串行端口,从而可以协助 Azure 支持人员调试问题。 此外,从内核引导行删除以下参数(如果它们存在):

     libata.atapi_enabled=0 reserve=0x1f0,0x8
    
  7. 建议编辑 /etc/sysconfig/network/dhcp 文件,并将 参数更改为以下设置DHCLIENT_SET_HOSTNAME

     DHCLIENT_SET_HOSTNAME="no"
    
  8. 在“/etc/sudoers”文件中,注释掉或删除以下行(如果存在)。 这是重要步骤。

    Defaults targetpw   # ask for the password of the target user i.e. root
    ALL    ALL=(ALL) ALL   # WARNING! Only use this together with 'Defaults targetpw'!
    
  9. 请确保已安装 SSH 服务器且将其配置为在引导时启动。

  10. 不要在 OS 磁盘上创建交换空间。

    Azure Linux 代理可使用在 Azure 上预配后附加到 VM 的本地资源磁盘自动配置交换空间。 本地资源磁盘是临时磁盘,并将在取消预配 VM 时被清空。

    安装 Azure Linux 代理后,按如下所示在 /etc/waagent.conf 中修改参数

    ResourceDisk.Format=n
    ResourceDisk.Filesystem=ext4
    ResourceDisk.MountPoint=/mnt/resource
    ResourceDisk.EnableSwap=n
    ResourceDisk.SwapSizeMB=2048    ## NOTE: set the size to whatever you need it to be.
    
  11. 确保在启动时运行 Azure Linux 代理:

    sudo systemctl enable waagent.service
    
  12. 运行以下命令以取消对虚拟机的预配,并准备虚拟机以便在 Azure 上进行预配。

    如果要迁移特定的虚拟机,但不希望创建通用化映像,请跳过取消预配步骤。

        sudo rm -f ~/.bash_history # Remove current user history
        sudo rm -rf /var/lib/waagent/
        sudo rm -f /var/log/waagent.log
        sudo waagent -force -deprovision+user
        sudo rm -f ~/.bash_history # Remove root user history
        sudo export HISTSIZE=0
    
  13. 在 Hyper-V 管理器中选择“操作”>“关闭”

后续步骤

准备就绪后,现在即可使用 SUSE Linux VHD 在 Azure 中创建新的虚拟机。 如果是首次将 .vhd 文件上传到 Azure,请参阅从自定义磁盘创建 Linux VM