Disk and space management is an essential knowledge of a sysadmin. It is his or her everyday job to handle disk issues. As part of the RHCSA exam preparation, we will learn how to add new space of various types to the system, using the tools provided by RHEL8. We already covered many of these tasks, and in this tutorial we will focus on adding new space without harming the data contained in the system.
In this tutorial you will learn:
- How to add new partitions to RHEL8
- How to add new logical volumes to RHEL8
- How to add swap to RHEL8
Software Requirements and Conventions Used
|Category||Requirements, Conventions or Software Version Used|
|System||Red Hat Enterprise Linux 8.1|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to add new partitions to RHEL8
We already covered the technical steps to create a partition in the partitioning tutorial for RHCSA, so here all that needs to be made clear is to think twice before partitioning.
fdisk may ask if you are sure about a modification, but after that it will rewrite the partition table, which may result the data already on disk becomes inaccessible. Always create a backup if the data is needed.
How to add new logical volumes to RHEL8
Creating a new logical volume is covered in the logical volume tutorial for RHCSA. Logical volumes give system storage the flexibility that single disks alone can’t provide. What need to be outlined here is that we need to build our LVM setup from the bottom, and if we need to extend it, we always need to check if we have enough space in the underlying physical volumes.
How to add swap to RHEL8
Swap is a special disk space which is used by the operating system to write memory pages to. Memory contents that are not used for some time can be written to this swap partition, and thus the memory can be used by other programs. If the system is running low on free memory, swap will help solve the problem. But if the system need to use more memory it actually has, intense read/write occurs on the swap partition (which is called “swapping”), which will slow down the overall system, to the point where it becomes less and less responsive.
This is something that should be avoided, even a desktop is hard to use while swapping, and imagine a production server that runs an OLTP database, which suddenly starts swapping. While this intense swapping is a dreaded situation, using the swap space is a good thing in general – it helps run operations faster. If we are about to add more swap space to the system, we have two options: we can create a new swap partition, or we can create a file in the filesystem that will serve as swap space. We will cover both cases.
Creating a new swap partition
To create a swap partition, we need an empty partition on one of our disks, without a filesystem. In our test setup we have a 2 GB empty disk seen by the system as
# fdisk -l /dev/sdb Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
Creating a swap partition is only one command away, called
# mkswap /dev/sdb Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=34aa2332-0514-42ab-9635-1fd6b922d213
As with all disk operations, check twice before proceeding. Reformatting a partition to swap destroys any content it held before. The
mkswap utility is providing us the new partition’s UUID, which will be needed for mounting.
Next we’ll create a swapfile in the filesystem, which we’ll also use as swap. Because our test environment uses
xfs filesystem, we’ll need to prepare the file using
# dd if=/dev/zero of=swapfile count=2048000 2048000+0 records in 2048000+0 records out 1048576000 bytes (1.0 GB, 1000 MiB) copied, 7.91227 s, 133 MB/s
We copied the default 512 bytes from
/dev/zero two million times to the file called “swapfile”, effectively filling the file with zeros to the size of around 1 GB. This file in turn can be used to create swap:
# mkswap swapfile mkswap: swapfile: insecure permissions 0644, 0600 suggested. Setting up swapspace version 1, size = 1000 MiB (1048571904 bytes) no label, UUID=8dc7aa71-524c-4d2b-bbb3-5b9fbbfb3327
Our initial swap space on the test environment is 1.5 GB:
# free -m | grep Swap Swap: 1535 482 1053
Which is provided by the device
# cat /proc/swaps Filename Type Size Used Priority /dev/dm-1 partition 1572860 492984 -2
To make our new swap spaces usable to the system with the
swapon command. Running it without arguments provide data of the swap already available:
# swapon NAME TYPE SIZE USED PRIO /dev/dm-1 partition 1.5G 478M -2
Adding our swapfile as argument enables the file as swap. In this example the file is in the
/root directory, where the command is issued from, so no path need to be specified.
# swapon swapfile
free command shows the increased swap space:
# free -m total used free shared buff/cache available Mem: 981 615 121 7 243 216 Swap: 2535 477 2058
We can turn on the
/dev/sdb swap partition by UUID:
# swapon UUID=34aa2332-0514-42ab-9635-1fd6b922d213
And again our swap space is increased:
# free -m total used free shared buff/cache available Mem: 981 617 119 7 243 215 Swap: 4583 477 4106
swapon command also shows our new devices:
# swapon NAME TYPE SIZE USED PRIO /dev/dm-1 partition 1.5G 474M -2 /root/swapfile file 1000M 0B -3 /dev/sdb partition 2G 0B -4
And with this we have successfully added new swap space to our system. These changes aren’t permanent at this stage, after reboot they will not be recognized as swap. To make use of these swap spaces after reboot, we’ll need to add two entries into the
/etc/fstab file that points to these devices, so the system can recognize and mount them at startup.
# tail -n 2 /etc/fstab UUID=34aa2332-0514-42ab-9635-1fd6b922d213 swap swap defaults 0 0 /root/swapfile swap swap defaults 0 0
- After creating a swapfile and adding it to the
/etc/fstab, delete it, and reboot your system. Will there be errors in the logfiles and the swapon output?
- After adding swap with either a file or partition, try turning the old partition off with
swapoff. Don’t do this on production systems!
mkswapon a partition with a filesystem.