Linux create swap partition

Table of Contents

1 Purpose of document
2 Understanding of SWAP partition
3 New disk assignment to system for swap partition
4 Partition new disk
4.1. Display partition status before any activity
4.2. Create primary partition for swap
4.3. Display partition status after swap partition create
5 Setup swap area on new partition
6 Entry of swap partition in /etc/fstab
7 Setup swap space on and check the swap space status
8 End of Document

Linux create swap partition

1 Purpose of document

Purpose of this document is to explain how to create swap partition on Linux systems.

2 Understanding of SWAP partition

  • Once physical RAM/ RAM allocated to VM get exhausted and system (OS/application) still require more RAM, swap space is used.
  • Inactive pages in memory are moved to the swap space.
  • Since swap space is allocation in hard disk which have a slower access time than physical memory. SWAP space cannot be a replacement of physical memory.
  • Swap space can be a created on dedicated swap partition, a swap file, or a combination of swap partitions and swap files (Recommended is to create separate swap partition).
  • Usually swap space is used while application installation/ high memory intensive work.
  • Usually swap space is provisioned double of RAM.
  • For SWAP partition file type system ID is 82.

3 New disk assignment to system for swap partition

In this document new disk assignment is shown for Azure VM.

Login to Azure portal https://portal.azure.com => Virtual Machine => Select the VM => Disk => Add data disk

Create disk:

Provide Disk Name, Resource Group, and select size of disk

Click on “Save”

Conclusion:

Disk provisioned to Azure VM and this new disk should visible in OS.

4 Partition new disk

4.1. Display partition status before any activity

#fdisk –l => Display all disk and all partitions

In OS disks are name as /dev/sda for first disk, /dev/sdb for second and so on. In this document new fourth disk is provisioned and in OS visible as “/dev/sdd”.

#fdisk –l | grep”/dev”

Status of new disk (Fourth disk) in OS before creating any partition

#fdisk –l /dev/sdd

4.2. Create primary partition for swap

Only 4 primary partitions can be created, for more than 4 partition need to create extended partition. Extended partition is a logical partition which needs to be further partitioned to use it. For extended/ logical partition creation, please refer https://nsitautomation.in/ubuntu-linux-how-to-create-partition/

Fdisk switches

  • “m” => For help
  • “p” => To display partition info
  • “n” => to create new partition
  • “n” => “p” => To create primary partition
  • “t” => To toggle File System Type
  • “w” => Write partition to disk

Create partition

#fdisk /dev/sdd

Press “p” to display partition table. => Press “n” to create new partition => Press “p” to create primary partition => Partition number (1-4, default1) : “Press enter” => First Sector “Press enter” => Last Sector “Press enter”

Assign file system ID (swap partition) to new partition (toggle partition ID)

For SWAP partition file type system ID is 82.

Press “p” to display partition table. => Press “t” to toggle file system ID => Select partition 1 “Press enter” => “L” to see list of list of filesystem ID available => Hex code : “82” for Linux swap partition and press enter”

Write partition table to disk

Press “p” to display partition table and verify system ID as “82” => Press “w” to write to disk.

4.3. Display partition status after swap partition create

#fdisk –l /dev/sdd

#fdisk –l /dev/sdd

5 Setup swap area on new partition

#mkswap /dev/sdd1

6 Entry of swap partition in /etc/fstab

  • The /etc/fstab file allows you to specify how the partitions to be mounted, in next reboot.

  • /etc/fstab entry for new swap partition.

/dev/sdd1       swap    swap    defaults        0       0

7 Setup swap space on and check the swap space status

#swapon –a

#swapon –show

#grep Swap /proc/meminfo                       or #cat /proc/meminfo | grep “Swap”

Conclusion:

  • Swap partition created.
  • Setup of swap partition in fstab file to auto mount swap partition in next reboot
  • Enable swap space.

8 End of Document

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *