How to Create Swap Partition on Linux
Abstract: /dev/sdb1 swap swap defaults 0 0 How to Create swap partition for lvm You can have an LVM installation on your server and you need to create a swap pa
Swap memory is required when the system requires more memory than it is physically available. The kernel swaps out less used pages and gives memory to the current process that needs the memory immediately. So a page of memory is copied to the preconfigured space on the hard disk. Disk speed is much slower compared to memory speed. Swapping pages give more space for current applications in the memory (RAM) and make the application run faster.
Swap space is located on hard drives, which have a slower access time than physical memory. Swap space can be a dedicated swap partition or swap file, or a combination of both.
In this tutorial, we learn how to create a swap partition on a Linux system.
How to Create Swap File in Linux fo...To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video
How to Create Swap File in Linux for Increase Memory / RAM Size of VPSRelated Read: How to Create Linux Swap File
Create a swap partitionLets first check disk space, then create a partition and followed by enabling swap.
1) Check disk spaceCheck if you have enough space on disk to create a new partition for swap using fdisk or parted command.
# fdisk -l
Disk /dev/sda: 10.7 GB, 10720641024 bytes, 20938752 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
Disk /dev/sdb: 536 MB, 536870912 bytes, 1048576 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
Disk label type: dos
Disk identifier: 0x66fe37bd
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
2) Create a partition for swapAs we saw we have enough unallocated space on the disk, we can create a new partition using tools like parted or fdisk.
Here I am going to use fdisk command to create a partition:
# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the
Command (m for help):
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
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-1048575, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1048575, default 1048575):
Using default value 1048575
Partition 1 of type Linux and of size 511 MiB is set
To define now our partition as swap type, we will use t command
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
The Hex code for swap partition on Linux is 82
. Now we will save the changes with w
command
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Make the new partition as swap. Change toggle id to 82 (for swap). Let's check with fdisk -l
command:
# fdisk -l
Disk /dev/sda: 10.7 GB, 10720641024 bytes, 20938752 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
Disk /dev/sdb: 536 MB, 536870912 bytes, 1048576 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
Disk label type: dos
Disk identifier: 0x66fe37bd
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1048575 523264 82 Linux swap / Solaris
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.
Just use dd command or fallocate to create a file (say 1 GB or 2GB).
# dd if=/dev/zero of=/mnt/swapfile bs=1024 count=2097152
or# fallocate -l 2G /mnt/swapfile
# chmod 600 /mnt/swapfile
Then follow the below steps.
3) Format to swap modeAfter defining our partition, we need to format it for "swap mode" so run mkswap command on the newly created swap partition. Make sure to choose the correct partition number which you need to enable swap. You may use -L option to set LABEL on the swap partition.
Run the following command define /dev/sdb1 as swap partition:
# mkswap /dev/sdb1
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=c4696894-0b09-4fbe-87bb-a34d6d307a4e
or
# mkswap /mnt/swapfile
4) Enable Swap space
Now that our swap partition is formatted, we need to enable the swap space so run swapon command to enable it:
# swapon /dev/sdb1
5) Verify swap space
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
6) Add to fstab file
Then add newly created swap partition to /etc/fstab
file. It should look as below:
/dev/sdb1 swap swap defaults 0 0
How to Create swap partition for lvm
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"
We must first create the LVM2 logical volume of size 8 GB:
# lvcreate rootvg -n swapvol -L 8G
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:
/dev/rootvg/swapvol swap swap defaults 0 0
Now we need to enable the extended logical volume:
# swapon -v /dev/rootvg/swapvol
To test if the logical volume was successfully created, use swapon -s
or free -m
command to inspect the swap space.
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
# lvm lvresize /dev/rootvg/swapvol -L +8G
We want to increase from 8 GB to 16 GB
Now we need to format the space
# mkswap /dev/rootvg/swapvol
Now we need to activate the swap for devices marked as swap in /etc/fstab
# swapon -va
Remove swap partition
For some reason, you can need to remove your swap partition in lvm mode.
To remove a swap partition, you first need to disable the swapping for the associated logical volume whether it is lvm or something else there
# swapoff -v /dev/rootvg/swapvol
The second principle is to remove the volume so you need to delete the swap partition entirely.
# lvremove /dev/rootvg/swapvol
Now we need to remove the following entry from the /etc/fstab
file
/dev/rootvg/swapvol swap swap defaults 0 0
How to adjust swappiness property
Swappiness value defines how often system swaps data out of RAM to the swap space. The current swappiness value is stored in "/proc/sys/vm/swappiness' file. This is a value between 0 and 100. A low value say close to zero will make the kernel to try to avoid swapping. A server can have a value closer to 0 and Desktop 60 should be okay.
# cat /proc/sys/vm/swappiness
60
Conclusion
In this tutorial, we learned how to create a swap partition and enable swap on Linux. The modern computer comes with high memory and if you think your application will exhaust memory then it's advised to add a bit swap.
For old computers with small memory, it is always good to give twice the RAM size for your swap space.
I hope you enjoyed reading this and please provide your suggestions on the below comment section.