在 Linux VM 上使用 cloud-init 配置交换分区Use cloud-init to configure a swap partition on a Linux VM

本文介绍如何使用 cloud-init 在各种 Linux 分发版中配置交换分区。This article shows you how to use cloud-init to configure the swap partition on various Linux distributions. 在传统上,交换分区由 Linux 代理 (WALA) 根据分发版的需要进行配置。The swap partition was traditionally configured by the Linux Agent (WALA) based on which distributions required one. 本文档概述在预配期间,使用 cloud-init 按需生成交换分区的过程。This document will outline the process for building the swap partition on demand during provisioning time using cloud-init. 有关 cloud-init 如何在 Azure 以及受支持的 Linux 发行版中本机工作的详细信息,请参阅 cloud-init 概述For more information about how cloud-init works natively in Azure and the supported Linux distros, see cloud-init overview

为基于 Ubuntu 的映像创建交换分区Create swap partition for Ubuntu based images

在 Azure 上,Ubuntu 库映像默认不会创建交换分区。By default on Azure, Ubuntu gallery images do not create swap partitions. 若要在预配 VM 期间使用 cloud-init 启用交换分区配置,请参阅 Ubuntu wiki 中的 AzureSwapPartitions 文档To enable swap partition configuration during VM provisioning time using cloud-init - please see the AzureSwapPartitions document on the Ubuntu wiki.

为基于 CentOS 的映像创建交换分区Create swap partition for CentOS based images

在当前 shell 中,创建名为 cloud_init_swappart.txt 的文件并粘贴以下配置。Create a file in your current shell named cloud_init_swappart.txt and paste the following configuration. 对于此示例,请在本地计算机中创建文件。For this example, create the file on your local machine. 可使用任何想要使用的编辑器。You can use any editor you wish. 输入 sensible-editor cloud_init_swappart.txt 以创建文件并查看可用编辑器的列表。Enter sensible-editor cloud_init_swappart.txt to create the file and see a list of available editors. 选择 #1 以使用 nano 编辑器 。Choose #1 to use the nano editor. 请确保已正确复制整个 cloud-init 文件,尤其是第一行。Make sure that the whole cloud-init file is copied correctly, especially the first line.

#cloud-config
disk_setup:
  ephemeral0:
    table_type: gpt
    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"]

在部署此映像之前,需要使用 az group create 命令创建资源组。Before deploying this image, you need to create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 以下示例在“chinaeast”位置创建名为“myResourceGroup”的资源组。The following example creates a resource group named myResourceGroup in the chinaeast location.

az group create --name myResourceGroup --location chinaeast

现在,使用 az vm create 创建 VM,并通过 --custom-data cloud_init_swappart.txt 指定 cloud-init 文件,如下所示:Now, create a VM with az vm create and specify the cloud-init file with --custom-data cloud_init_swappart.txt as follows:

az vm create \
  --resource-group myResourceGroup \
  --name centos74 \
  --image OpenLogic:CentOS:7-CI:latest \
  --custom-data cloud_init_swappart.txt \
  --generate-ssh-keys 

验证是否已创建交换分区Verify swap partition was created

通过 SSH 连接到 VM 的公共 IP 地址显示在先前命令的输出中。SSH to the public IP address of your VM shown in the output from the preceding command. 按如下所示输入自己的 publicIpAddressEnter your own publicIpAddress as follows:

ssh <publicIpAddress>

通过 SSH 连接到 VM 后,请验证是否已创建交换分区Once you have SSH'ed into the vm, check if the swap partition was created

swapon -s

此命令的输出应如下所示:The output from this command should look like this:

Filename                Type        Size    Used    Priority
/dev/sdb2  partition   2494440 0   -1

备注

如果在现有的 Azure 映像中配置了交换分区,而你想要更改新映像的交换分区配置,则应删除现有的交换分区。If you have an existing Azure image that has a swap partition configured and you want to change the swap partition configuration for new images, you should remove the existing swap partition. 有关更多详细信息,请参阅“自定义 cloud-init 预配的映像”文档。Please see 'Customize Images to provision by cloud-init' document for more details.

后续步骤Next steps

有关配置更改的其他 cloud-init 示例,请参阅以下文章:For additional cloud-init examples of configuration changes, see the following: