3.6. Configuring swap space with cloud-init on Azure
To use swap space for a Red Hat Enterprise Linux (RHEL) virtual machine (VM) on Microsoft Azure, you need to create a swap partition on the ephemeral disk. Only use the ephemeral disk for creating a swap partition, not the operating system disk or data (storage) disk. Because the ephemeral disk is deleted when the virtual machine is deleted, the swap partition is also removed.
You can use the cloud-init utility to configure a swap partition on the ephemeral disk on-demand. Ephemeral disk is a local storage of the VM, while a resource disk is mounted storage on VM itself. Both storage types store data temporarily. Deleting, moving, stopping, or failure of the VM will result in the loss of the data stored on the ephemeral or resource disk.
Do not use the ephemeral disk for persistent data. All contents, including the swap partition, are deleted when the VM is stopped or moved.
Prerequisites
-
You have installed the
cloud-initutility on the VM. You have disabled the swap configuration in the Windows Azure Linux Agent (WALA) by setting the parameters in the
/etc/waagent.conffile:ResourceDisk.Format=n ResourceDisk.EnableSwap=n ResourceDisk.SwapSizeMB=0- You have an ephemeral disk available on the VM.
Procedure
- Log in to the VM.
Create and edit the
/etc/cloud/cloud.cfg.d/00-azure-swap.cfgconfiguration file and add the followingcloud-initconfiguration to the file:# vi /etc/cloud/cloud.cfg.d/00-azure-swap.cfg#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,nofail,x-systemd.requires=cloud-init.service", "0", "0"]This configuration:
-
Partitions the ephemeral disk (
ephemeral0) with a GPT partition table. -
Creates two partitions: 66% for a file system (mounted at
/mnt) and 33% forswapspace. -
Formats the first partition as
ext4and the second partition asswap. Configures automatic mounting of both partitions at boot time.
注意The partition layout
[66, [33,82]]allocates 66% of the disk to the first partition and 33% to the second partition. The82in the second partition specification indicates a Linux swap partition type. You can adjust these percentages based on your requirements.
-
Partitions the ephemeral disk (
Verify the configuration file for any errors:
# cloud-init devel schema --config-file /etc/cloud/cloud.cfg.d/00-azure-swap.cfgIf the configuration is valid, the command returns no errors.
Verification
After you reboot the VM, check that the swap partition is configured and active by verifying the active swap space, swap usage, and the swap partition entry in the
/etc/fstabfile.Check active swap space:
$ swapon -sThe output should show the swap partition from
ephemeral0.2:Filename Type Size Used Priority /dev/ephemeral0.2 partition 8388604 0 -2Check swap usage:
$ free -hThe output should show swap space in the
Swaprow:total used free shared buffered/cache available Mem: 7.8Gi 1.2Gi 5.8Gi 16MiB 800MiB 6.3Gi Swap: 8.0Gi 0B 8.0GiVerify the swap partition is present in the
/etc/fstabfile:$ grep swap /etc/fstabThe output should include an entry for the swap partition, for example:
/dev/ephemeral0.2 none swap sw,nofail,x-systemd.requires=cloud-init.service 0 0