Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
RHCSA - Configure Local Storage: Add New Partitions and Logical Volumes, and Swap to a System Non-Destructively
Partitions and Logical volume Management were covered previously in the course, links below:
For the exam objective, this leaves swap management remaining which we will focus on in this guide.
Swap Overview
Swap is a designated area on a storage device (often a separate partition or a swap file) used as virtual memory when the physical RAM (Random Access Memory) is fully utilized. When the system's RAM is insufficient to accommodate all active processes and data, the Linux kernel transfers less frequently accessed or idle memory pages to the swap space. This frees up RAM for more critical and frequently used tasks. Swap allows the operating system to efficiently manage memory and prevents system crashes due to memory exhaustion, ensuring smooth performance even under heavy memory load or when running multiple resource-intensive applications. However, swap usage may lead to performance degradation if excessively relied upon, as accessing data from a storage device is slower than accessing data directly from RAM.
Lesson Setup
To actively participate in the exercises, make sure to attach an additional empty disk to your system. While the exercises assume that the additional disk is 1G in size, feel free to use whatever disk size is available to you.
Do not use a disk with data on as following the exercises will mean that data will be deleted.
Once the disk is attached, you should be able to see it by running the lsblk
command, which in this example shows as the sdb
device:
After rebooting, the disk may not retain the same device name, making it essential to always run lsblk
to verify that you are working with the correct disk.
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 600M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 17G 0 part
├─os_vg-root 253:0 0 15G 0 lvm /
└─os_vg-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
Create & Add a Swap Partition to System
To create a swap
partition you can use fdisk
or gdisk
. For the exercise below we will use fdisk
.
Create a partition on /dev/sdb
of size 500MB:
Run the fdisk
utility for your attached disk:
sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x5ce8f1fb.
Command (m for help):
Create a new (n
) partition:
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Press Enter to accept default partition type:
Select (default p):
Using default response p.
Partition number (1-4, default 1):
Press Enter to accept default partition number:
Partition number (1-4, default 1):
First sector (2048-2097151, default 2048):
Press Enter to accept default first sector:
First sector (2048-2097151, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097151, default 2097151):
Type +500MB
and press Enter to set partition to size 500MB.
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097151, default 2097151): +500MB
Created a new partition 1 of type 'Linux' and of size 477 MiB.
Command (m for help):
Type t
and press Enter to display the file system type selection:
Set the alias to swap
and press Enter:
Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): swap
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help):
To save and quit, type w
and press Enter:
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Now a partition is created, you need to format the partition as swap
, which is achieved using the mkswap
command. The -L
flag is useful for setting a label which can be used for mounting the partition in later exercises.
Format partition as swap
:
sudo mkswap -L swap_partition /dev/sdb1
Setting up swapspace version 1, size = 477 MiB (500166656 bytes)
LABEL=swap_partition, UUID=3b320e4a-ac86-4b34-a0fb-6580663efe02
Enabling swap is achieved using the swapon
command but this will not persist reboot. To persist a reboot, you will need to add an entry into the /etc/fstab
file.
Enable the new swap partition:
Display any active swap (if any):
swapon --show
On my system it shows a 2GB swap already present:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2
Enable new swap partition:
sudo swapon /dev/sdb1
Display all the active swap again:
swapon --show
Output should now display the /dev/sdb1
partition we formatted as swap:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2
/dev/sdb1 partition 477M 0B -3
To make this swap partition active on reboot, run the following to add an entry to the /etc/fstab
file:
echo "LABEL=swap_partition none swap defaults 0 0" | sudo tee -a /etc/fstab
To test if the entry is valid you will first deactivate the swap partition:
sudo swapoff -L swap_partition
Now you can run swapon -a
which will activate any devices in /etc/fstab
marked as swap:
sudo swapon -a
Display all the active swap again:
swapon --show
Output should display the /dev/sdb1
partition:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2
/dev/sdb1 partition 477M 0B -3
Create & Add a Swap File to System
You may have to create a swap file to use in a scenario where an increase in swap is needed urgently but there is no disk space available for partitioning. The steps are to create a file, set appropriate permissions on the file, format file as swap space
Create swap file:
The fallocate
command is used to create and preallocate space to a file:
sudo fallocate -l 100MB /swapfile
Change permissions on the file:
sudo chmod 600 /swapfile
Confirm permissions and files size are as expected:
ls -l /swapfile
-rw-------. 1 root root 100000000 Aug 3 08:57 /swapfile
Now a file is created, you need to format the file as swap
, which is achieved using the mkswap
command.
Format file as swap
:
sudo mkswap /swapfile
Setting up swapspace version 1, size = 95.4 MiB (99995648 bytes)
no label, UUID=2f237a81-1458-4789-9ee5-34fb9d921aed
Enable the new swap file:
Enable new swap file:
sudo swapon /swapfile
Display all the active swap again:
swapon --show
Output should now display the /swapfile
:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2
/dev/sdb1 partition 477M 0B -3
/swapfile file 95.4M 0B -4
To make this swap file active on reboot, run the following to add an entry to the /etc/fstab
file:
echo "/swapfile none swap defaults 0 0" | sudo tee -a /etc/fstab
To test if the entry is valid you will first deactivate the swap partition:
sudo swapoff /swapfile
Now you can run swapon -a
which will activate any devices in /etc/fstab
marked as swap:
sudo swapon -a
Display all the active swap again:
swapon --show
Output should display the /swapfile
file:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2
/dev/sdb1 partition 477M 0B -3
/swapfile file 95.4M 0B -4
Lesson Tidy Up
In the previous exercises you used swapoff
to deactivate a swap file and partition. You will run this again and we will also; remove the entries from /etc/fstab
, delete the swap file and delete the partition.
Deactivate and remove swap file /swapfile
and swap partition:
sudo swapoff /swapfile
sudo swapoff -L swap_partition
Confirm the /swapfile
is no longer active:
swapon --show
Output will show both our swap file and partition no longer present, leaving just your original swap space (if any did exist):
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2
Remove the swap entries from /etc/fstab
:
sudo sed -i.$(date +%Y%m%d%H%M) '/swap_partition/d;/swapfile/d' /etc/fstab
Delete the swap file:
sudo rm -v /swapfile
removed '/swapfile'
Wipe the /dev/sdb
device clean:
sudo wipefs --all /dev/sdb
/dev/sdb: 2 bytes were erased at offset 0x000001fe (dos): 55 aa
/dev/sdb: calling ioctl to re-read partition table: Success
Remove swap signatures from /dev/sdb
device:
sudo dd if=/dev/zero of=/dev/sdb bs=1M
dd: error writing '/dev/sdb': No space left on device
1025+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.502 s, 715 MB/s
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.