Monday, November 3, 2008

Adding a Linux swap partition

I'm putting this howto in my blog to make it easier to find later when I need it again. I typically use it to resize swap partitions. I tested these instructions with RHEL 4.

To add a swap partition (assuming /dev/hdb2 is the swap partition you want to add):

  1. Turn off all the swap space on the hard drive with the swapoff command.

  2. 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.

  3. 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:

    mkswap /dev/hdb2
  4. To enable the swap partition immediately, type the following command:

    swapon /dev/hdb2
  5. To enable it at boot time, edit /etc/fstab to include:

    /dev/hdb2               swap                    swap    defaults        0 0

    The next time the system boots, it will enable the new swap partition.

  6. After adding the new swap partition and enabling it, make sure it is enabled using the free command.