Adding more swap space



 

 


Due to changing requirements for swap space on already installed Linux machines, it may become necessary to modify the amount of swap space defined for the system.

This document can be used for any general case where the swap partition needs to be changed. It assumes sufficient available disk space. This procedure also assumes that the disks are partitioned in “raw” ext3 and swap partitions and do not use LVM.

Overview

The basic steps to take are simple.

  1. Turn off the existing swap space.
  2. Delete the existing swap partition.
  3. Create a new swap partition of the desired size.
  4. Reread the partition table.
  5. Change /etc/fstab if necessary.
  6. Turn on the new swap partition.

A reboot should not be necessary.

Details

For safety sake, before turning off swap, at the very least you should ensure that no applications are running and that no swap space is in use. The free or top commands can tell you whether swap space is in use. To be even more safe, you could revert to runlevel 1 or single user mode.

Turn off the swap partition with the command:

swapoff -a

This command turns off all swap space. Delete the existing swap partition with fdisk. Start fdisk with the command:

fdisk -l

This displays the current partition tables on each drive. Identify the current swap partition by number.

Start fdisk in interactive mode with the command:

fdisk /dev/<device name>

For example:

fdisk /dev/sda

At this point fdisk is now interactive and will only operate on the specified disk drive. Then use the d sub-command to delete the partition using the partition number you previously determined. The fdisk command will ask you which partition number you want to delete.

Use the fdisk p sub-command to verify that there is enough free space on the disk to create the new swap partition. The space on the hard drive is shown in terms of 512-Byte blocks, and starting and ending cyliner numbers, so you may have to do some math in order to determine the available space between and at the end of allocated partitions.

Use the n sub-command to create a new swap partition. Fdisk will ask you the starting cylinder. By default it chooses the lowest numbered available cylinder. If you wish to change that, type in the mumber of the starting cylinder.

The fdisk command now allows you to enter the size of the partitions in a number of formats, including the last cylinder number or the size in bykes, KB or MB. Type in 6000M which will giv about 6GB and press enter.

Use the p sub-command to verify that the partition was created as you specified it. You should note that the partition will probably not be exactly what you specified unless you used the ending cylinder number. The fdisk command can only allocate disk space in increments on whole cylinders so your partition may be a little smaller or larger than you specified.

If the partition is not what you want, you can delete it and create it again. When you are satisfied with the partition you have created, use the w sub-command to write the new partirion table to the disk. The fdisk program will exit and return you to the command prompt after it completes writing the new partition table.

Now it is necessary to specify that the new partition is to be a swap partition. The sub-command t allows you to specify the type of partition. So enter t, specify the partition number, and then when it asks for the hex code partition type, type in 82, which is the Linux swap partition type and press enter.

You will probably receive the following message as fdisk completes writing the new partition table:

The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

At this point you use the partprobe command to force the kernel to re-read the partition table so that it is not really necessary to perform a reboot to do so.

partprobe

At this point you should be able to use the command fdisk -l to list the partitions and the new swap partition should be among those listed. Be sure that the partition type is “Linux swap”.

It will be necessary to modify the /etc/fstab file to point to the new swap partition. The existing line will probably look like this:

LABEL=SWAP-sdaX         swap        swap    defaults        0 0

Where X is the partition number. Change it to look like this:

/dev/sdaX               swap        swap    defaults        0 0

Be sure to use the current partition number if it is different from the previous one. Now you can perform the final step in creating the swap partition. Use the mkswap command to define the partition as a swap partition.

mkswap /dev/[device name]

The final step is to turn swap on using the command:

swapon -a

Your new swap partition is now on-line. You can use the free or top commands to verify this.

If your disk setup uses LVM, changing swap space will be fairly easy, but that is covered in another document.