How to Create Swap Partition on Linux
How to Create Swap Partition on Linux
com
Lets first check disk space, then create a partition and followed by
enabling swap.
# fdisk -l
We will use /dev/sdb disk for our swap. Check the swap with free
-m command; we have:
# free -m
total used free shared buff/cache available
Mem: 988 88 645 50 254 813
Swap: 0 0 0
You can see that we don't have a swap partition. We can also use
the command below for verification
# swapon -s
You see that we don't have a return. It means that there is no swap
# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
You can type m command for the help which will list you different
possibilities. We will create a new partition for our swap with n
command
The Hex code for swap partition on Linux is 82. Now we will save
the changes with w command
# fdisk -l
You can see the mention 'Linux swap' on the last line.
Note: On the latest version Ubuntu and Centos it uses create swap
file instead of a swap partition. Let's see how to create a swap file.
or
# fallocate -l 2G /mnt/swapfile
# mkswap /dev/sdb1
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=c4696894-0b09-4fbe-87bb-
a34d6d307a4e
or
# mkswap /mnt/swapfile
# swapon /dev/sdb1
Verify the newly added swap space using the command below:
# free -m
total used free shared buff/cache available
Mem: 988 88 646 50 254 814
Swap: 511 0 511
You can have an LVM installation on your server and you need to
create a swap partition. The procedure is not exactly the same
because of "lvm mode"
After creating the logical volume, we need to format the new swap
space:
# mkswap /dev/rootvg/swapvol
To be sure that our swap partition will be mounted automatically
even if we restart the server, we need to add the following entry to
the /etc/fstab file:
# swapon -v /dev/rootvg/swapvol
You can need to extend your swap partition because the actual
swap size doesn't satisfy your job. With lvm, it is possible to directly
increase the size of an existing partition as below.
You must first identify the swap volume group which is '/dev/rootvg
/swapvol' in our case. You need first to disable the current swapping
# swapoff -v /dev/rootvg/swapvol
Now you must resize the volume group to indicate the space to
increase
# mkswap /dev/rootvg/swapvol
# swapon -va
For some reason, you can need to remove your swap partition in
lvm mode.
# swapoff -v /dev/rootvg/swapvol
# lvremove /dev/rootvg/swapvol
# cat /proc/sys/vm/swappiness
60
Conclusion