1 minute read

Note: This is an RHCSA 7 exam objective.

Presentation

You’ve got three options when adding some swap space to a system:

  • use a logical volume inside a volume group,
  • use a new partition,
  • use a file system (this is not a good solution performance-wise).

Logical Volume Configuration

If you decide to create a logical volume, follow these steps: Create a logical volume (here called lv_swap with a size of 1G in the vg volume group):

# lvcreate --size 1G --name lv_swap vg

Prepare the swap logical volume:

# mkswap /dev/vg/lv_swap

Add the swap logical volume to the system:

# swapon /dev/vg/lv_swap

Choose one of these commands to check the result:

# swapon -s
# cat /proc/swaps

Edit the /etc/fstab file and add the following line (you can replace the beginning of the line with the UUID of the swap logical volume):

/dev/mapper/vg-lv_swap swap swap defaults 0 0

Note: to remove the swap logical volume, remove the line previously created in the /etc/fstab file and type:

# swapoff /dev/vg/lv_swap
# lvremove /dev/vg/lv_swap

Partition Configuration

If you decide to create a new partition, follow these steps: Create a new partition with fdisk (here on the /dev/vda disk):

# fdisk /dev/vda

At the fdisk prompt, type ‘c‘, then ‘u‘ and finally ‘p‘ to print the partition table. Still at the fdisk prompt, type ‘n‘ to create a new partition, type the partition number, the first sector and the partition size. Don’t forget to give the swap type to this partition: press ‘t‘, then the partition number, then ‘82‘. Exit the fdisk prompt with the ‘w‘ to write the partition table on disk.

Ask the kernel to read again the partition table (where X is the number of the swap partition):

# partprobe /dev/vdaX

Prepare the swap partition:

# mkswap /dev/vdaX

Add the swap partition to the system:

# swapon /dev/vdaX

Choose one of these commands to check the result:

# swapon -s
# cat /proc/swaps

Edit the /etc/fstab file and add the following line (you can replace the beginning of the line with the UUID of the swap partition):

/dev/vdaX swap swap defaults 0 0

Note: to remove the swap partition, remove the line previously created in the /etc/fstab file and type:

# swapoff /dev/vdaX

Leave a comment