HOW-TO: Add SWAP space from partition (Linux)

From the previous tip, the addition of the swap file was discussed. It is one of the ways to add swap space to an existing host. Now we will discuss another way to add swap space -- using a free partition.

More often than not, when a host needs extra swap space the disk is already utilized so addition of another disk is required. Assuming this scenario let us go through the steps in creating the partition and the addition as swap space.

Follow the procedure below:

1. Create the swap partition using parted or fdisk. Using parted is easier than fdisk; thus, only parted will be explained. To create a swap partition with parted:

* At a shell prompt as root, type the command parted /dev/hdb, where /dev/hdb is the device name for the hard drive with free space.
* At the (parted) prompt, type print to view the existing partitions and the amount of free space. The start and end values are in megabytes. Determine how much free space is on the hard drive and how much you want to allocate for a new swap partition.
* At the (parted) prompt, type mkpartfs part-type linux-swap start end, where part-type is one of primary, extended, or logical, start is the starting point of the partition, and end is the end point of the partition.
(Warning) Changes take place immediately; be careful when you type.
* Exit parted by typing quit.

2. Now that you have the swap partition, use the command mkswap to setup the swap partition. At a shell prompt as root, type the following:
root@host # mkswap /dev/hdb1

3. To enable the swap partition immediately, type the following command:
root@host # swapon /dev/hdb1

To make changes permanent (and persistent at boot time), edit /etc/fstab to include:
/dev/hdb1 swap swap defaults  0 0

You might also be interested in:


We at pimp-my-rig strive to keep on improving, help us reach that goal by leaving comments or constructive criticisms. Don't miss out on our next feature -- subscribe via RSS (What is RSS?).

Share This