Bookmark this page

Guided Exercise: Manage Swap Space

Create and format a partition as a swap space, and activate it persistently.

Outcomes

  • Create a partition and a swap space on a disk by using the GPT partitioning scheme.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

[student@workstation ~]$ lab start storage-swap

Instructions

  1. Log in to servera as the student user and switch to the root user.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$ sudo -i
    [sudo] password for student: student
    [root@servera ~]#
  2. Inspect the /dev/vdb disk. The disk already has a partition table and uses the GPT partitioning scheme. Also, it has an existing 1 GB partition.

    [root@servera ~]# parted /dev/vdb print
    Model: Virtio Block Device (virtblk)
    Disk /dev/vdb: 5369MB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:
    
    Number  Start   End     Size    File system  Name  Flags
     1      1049kB  1001MB  1000MB               data
  3. Add a new partition of 500 MB for use as a swap space. Set the partition type to linux-swap.

    1. Create the myswap partition. Because the disk uses the GPT partitioning scheme, you must give a name to the partition. Notice that the start position, 1001 MB, is the end of the existing first partition. The parted command ensures that the new partition immediately follows the previous one, without any gap. Because the partition starts at the 1001 MB position, the command sets the end position to 1501 MB to get a partition size of 500 MB.

      [root@servera ~]# parted /dev/vdb mkpart myswap linux-swap \
      1001MB 1501MB
      Information: You may need to update /etc/fstab.
    2. Verify your work by listing the partitions on the /dev/vdb disk. The size of the new partition is not exactly 500 MB. The difference in size is because the parted command must align the partition with the disk layout.

      [root@servera ~]# parted /dev/vdb print
      Model: Virtio Block Device (virtblk)
      Disk /dev/vdb: 5369MB
      Sector size (logical/physical): 512B/512B
      Partition Table: gpt
      Disk Flags:
      
      Number  Start   End     Size    File system  Name   Flags
       1      1049kB  1001MB  1000MB               data
       2      1001MB  1501MB  499MB                myswap swap
    3. Run the udevadm settle command. This command waits for the system to register the new partition, and returns when it is done.

      [root@servera ~]# udevadm settle
  4. Initialize the new partition as a swap space.

    [root@servera ~]# mkswap /dev/vdb2
    Setting up swapspace version 1, size = 476 MiB (499118080 bytes)
    no label, UUID=cb7f71ca-ee82-430e-ad4b-7dda12632328
  5. Enable the new swap space.

    1. Verify that creating and initializing the swap space does not yet enable it for use.

      [root@servera ~]# swapon --show
    2. Enable the new swap space.

      [root@servera ~]# swapon /dev/vdb2
    3. Verify that the new swap space is now available.

      [root@servera ~]# swapon --show
      NAME      TYPE      SIZE USED PRIO
      /dev/vdb2 partition 476M   0B   -2
    4. Disable the swap space.

      [root@servera ~]# swapoff /dev/vdb2
    5. Confirm that the swap space is disabled.

      [root@servera ~]# swapon --show
  6. Enable the new swap space at system boot.

    1. Use the lsblk command with the --fs option to discover the UUID of the /dev/vdb2 device. The UUID in the output is different on your system.

      [root@servera ~]# lsblk --fs /dev/vdb2
      NAME FSTYPE FSVER LABEL UUID                                   FSAVAIL FSUSE% MOUNTPOINTS
      vdb2 swap   1           762735cb-a52a-4345-9ed0-e3a68aa8bb97
    2. Add an entry to the /etc/fstab file. In the following command, replace the UUID with the one that you discovered from the previous step.

      ...output omitted...
      UUID=762735cb-a52a-4345-9ed0-e3a68aa8bb97  swap  swap  defaults  0 0
    3. Update the systemd daemon for the system to register the new /etc/fstab file configuration.

      [root@servera ~]# systemctl daemon-reload
    4. Enable the swap space by using the entry in the /etc/fstab file.

      [root@servera ~]# swapon -a
    5. Verify that the new swap space is enabled.

      [root@servera ~]# swapon --show
      NAME      TYPE      SIZE USED PRIO
      /dev/vdb2 partition 476M   0B   -2
  7. Reboot the servera machine. After the server reboots, log in and verify that the swap space is enabled. When done, log out from servera.

    1. Reboot the servera machine.

      [root@servera ~]# systemctl reboot
      Connection to servera closed by remote host.
      Connection to servera closed.
      [student@workstation ~]$
    2. Wait for servera to reboot and log in as the student user.

      [student@workstation ~]$ ssh student@servera
      ...output omitted...
      [student@servera ~]$
    3. Verify that the swap space is enabled.

      [student@servera ~]# swapon --show
      NAME      TYPE      SIZE USED PRIO
      /dev/vdb2 partition 476M   0B   -2
    4. Return to the workstation machine as the student user.

      [student@servera ~]$ exit
      logout
      Connection to servera closed.
      [student@workstation ~]$

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish storage-swap

Revision: rh199-9.3-8dd73db