为 Azure Linux VM 创建 SWAP 分区

若要在 Azure Linux VM 上创建 SWAP 分区,需要设置 cloud-init 以在 VM 的临时 (资源) 磁盘上自动创建它。 默认情况下,资源磁盘装载到下 /mnt 。 它位于托管 Linux VM 的物理服务器上,延迟较低。 不建议在 OS 磁盘或数据磁盘上创建可能影响操作系统和应用性能的 SWAP 分区。 请务必记住,资源磁盘绝不应用于存储常规数据,因为它只是临时存储。 将 VM 移动到另一个主机或停止/解除分配时,将擦除写入此磁盘的任何数据。 建议仅对可删除的数据(例如 SWAP 和缓存文件)使用资源磁盘。 有关详细信息,请参阅 临时磁盘

在 waagent 配置中禁用 SWAP 创建

如果在 /etc/waagent.conf 中配置了 SWAP 创建,则必须禁用它。

  1. 在 /etc/waagent.conf 中禁用资源磁盘格式化和 SWAP 配置,因为此任务现在由 Cloud-Init 处理。 按如下所示设置参数:

    # Format if unformatted. If 'n', resource disk will not be mounted.
    ResourceDisk.Format=n
    
    # Create and use SWAPfile on resource disk.
    ResourceDisk.EnableSWAP=n
    
    #Mount point for the resource disk
    ResourceDisk.MountPoint=/mnt
    
    #Size of the SWAPfile.
    ResourceDisk.SWAPSizeMB=0
    
  2. 重启 Azure Linux 代理。 有关不同 Linux 分发版的重启命令的信息,请参阅 如何在 VM 上更新 Azure Linux 代理

创建 SWAP 分区

可以使用以下选项之一创建 SWAP 分区。

注意

创建 SWAP 分区时,还会在它上创建交换文件。

选项 1:使用脚本在资源或自定义磁盘路径下创建 SWAP 分区
  1. 使用以下脚本在 /var/lib/cloud/scripts/per-boot 下创建名为 swap.sh 的 SWAP 创建脚本:

    #!/bin/sh
    
    # Percent of space on the ephemeral disk to dedicate to swap. Here 30% is being used. Modify as appropriate.
    PCT=0.3
    
    # Location of the swap file. Modify as appropriate based on the location of the ephemeral disk.
    LOCATION=/mnt
    
    if [ ! -f ${LOCATION}/swapfile ]
    then
    
        # Get size of the ephemeral disk and multiply it by the percent of space to allocate
        size=$(/bin/df -m --output=target,avail | /usr/bin/awk -v percent="$PCT" -v pattern=${LOCATION} '$0 ~ pattern {SIZE=int($2*percent);print SIZE}')
        echo "$size MB of space allocated to swap file"
    
         # Create an empty file first and set correct permissions
        /bin/dd if=/dev/zero of=${LOCATION}/swapfile bs=1M count=$size
        /bin/chmod 0600 ${LOCATION}/swapfile
    
        # Make the file available to use as swap
        /sbin/mkswap ${LOCATION}/swapfile
    fi
    
    # Enable swap
    /sbin/swapon ${LOCATION}/swapfile
    /sbin/swapon -a
    
    # Display current swap status
    /sbin/swapon -s
    

    脚本将在每次启动时执行,并在资源磁盘中分配 30% 的可用空间。 可以根据自己的情况自定义值。

  2. 使脚本可执行:

    chmod +x /var/lib/cloud/scripts/per-boot/swap.sh
    
  3. 停止并启动 VM。 仅在第一次创建 SWAP 文件后,才需要停止和启动 VM。

选项 2:使用 cloud-init 在资源磁盘路径下创建 SWAP 分区
  1. CLOUD_CFG/systemd/system.conf 中创建变量,以设置 SWAP 和资源磁盘:

    sudo echo 'DefaultEnvironment="CLOUD_CFG=/etc/cloud/cloud.cfg.d/00-azure-swap.cfg"' >> /etc/systemd/system.conf
    
  2. 创建用于设置 SWAP、资源磁盘创建和装入点的 YAML 文件:

    sudo cat > /etc/cloud/cloud.cfg.d/00-azure-swap.cfg << EOF
    #cloud-config
    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
    
  3. 停止并启动 VM 或重新部署 VM,以在资源磁盘上创建 SWAP 分区。

选项 3:使用 cloud-init 在自定义资源磁盘路径下创建 SWAP 分区
  1. CLOUD_CFG/systemd/system.conf 中创建变量,以设置 SWAP 和资源磁盘:

    sudo echo 'DefaultEnvironment="CLOUD_CFG=/etc/cloud/cloud.cfg.d/00-azure-swap.cfg"' >> /etc/systemd/system.conf
    
  2. 创建一个 YAML 文件,该文件将 SWAP、资源磁盘创建和自定义装入点 (“azure”是一个示例) :

    sudo cat > /etc/cloud/cloud.cfg.d/00-azure-swap.cfg << EOF
    #cloud-config
    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", "/azure"]
      - ["ephemeral0.2", "none", "swap", "sw,nofail,x-systemd.requires=cloud-init.service,x-systemd.device-timeout=2", "0", "0"]
    EOF
    

    注意

    确保自定义装入点位于 YAML 文件中指定的位置。

  3. 停止并启动 VM 或重新部署 VM,以在资源磁盘上创建 SWAP 分区。

联系我们寻求帮助

如果你有任何疑问或需要帮助,请创建支持请求联系 Azure 社区支持。 还可以向 Azure 反馈社区提交产品反馈。