How to add swap space on CentOS 8

Pick a disk on which we will create the swap partition. In this case sdb is unused disk (extra empty volume that was mounted to the virtual machine via Virtualbox).

[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   64G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   63G  0 part 
  ├─cl-root 253:0    0   41G  0 lvm  /
  ├─cl-swap 253:1    0  2.1G  0 lvm  [SWAP]
  └─cl-home 253:2    0   20G  0 lvm  /home
sdb           8:16   0   16G  0 disk 
sr0          11:0    1 73.6M  0 rom  /run/media/coil/VBox_GAs_6.0.12
[root@localhost ~]#

Use fdisk command to create new swap partition. We will need to use 82 Linux swap option so the system will know that this partition is supposed to be used for swap.

[root@localhost ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
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 0x22289e9f.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-33554431, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-33554431, default 33554431): +2G

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# 

Run kpartx to be sure the system knows about newly created partition.

[root@localhost ~]# kpartx /dev/sdb
sdb1 : 0 4194304 /dev/sdb 2048

New partition called sdb1 with size 2G has been created under sdb.

[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   64G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   63G  0 part 
  ├─cl-root 253:0    0   41G  0 lvm  /
  ├─cl-swap 253:1    0  2.1G  0 lvm  [SWAP]
  └─cl-home 253:2    0   20G  0 lvm  /home
sdb           8:16   0   16G  0 disk 
└─sdb1        8:17   0    2G  0 part 
sr0          11:0    1 73.6M  0 rom  /run/media/coil/VBox_GAs_6.0.12
[root@localhost ~]# 

now actual swap creation:

Make swap with mkswap command and mount it permanently as swap via editing /etc/fstab file. This way swap remains mounted after reboot.

[root@localhost ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=f42c1354-5a08-4792-ab6d-e37ebf296d2d
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="f42c1354-5a08-4792-ab6d-e37ebf296d2d" TYPE="swap" PARTUUID="22289e9f-01"
[root@localhost ~]# vim /etc/fstab

Add this line to the /etc/fstab

UUID="f42c1354-5a08-4792-ab6d-e37ebf296d2d"     swap    swap defaults   0 0

Just use all avaible swap and show what is currently being used.

[root@localhost ~]# swapon -a
[root@localhost ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/dm-1                               partition   2170876 17240   -2
/dev/sdb1                               partition   2097148 0   -3
[root@localhost ~]#

If we want to remove our swap at runtime

[root@localhost ~]# swapoff /dev/sdb1
[root@localhost ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/dm-1                               partition   2170876 17284   -2
[root@localhost ~]# 

and remove it from /etc/fstab so it doesnt come back after restart.