Manipulation of Linux Swap Partitions

It's easy to create a swap partition when you install a GNU/Linux distribution. Many installation programs provide user-friendly interface to help you to do it easily, or optionally create it for you automatically. However, how do you manipulate it after the installation? How do you do it for a remote server that doesn't run X server?

You quickly become an expert if you know how to perform the following operations for swap partitions. Let's see.

  • Create a Swap Partition. You can create one with many partition manipulators. In GNU/Linux, you can use fdisk or cfdisk, which is included in many distros. You need to know that the system ID for a Linux swap partition is 82 (hexadecimal).
  • Delete a Swap Partition. As above, use a partition manipulator that supports Linux swap partitions. Beware that a Solaris partition also uses the same ID (0x82) as a Linux swap partition. So be careful not to delete your Solaris if you have one!
  • Format a Swap Partition. A swap partition needs to be formatted before it can be used. Use mkswap [-L label] device to format a partition device and label it with label. E.g., mkswap -L swap /dev/hda3.
  • Enable (Mount) a Swap Partition. Use swapon device to enable the swap partition device. You can also use swapon -a to enable all partitions marked as swap in /etc/fstab.
  • Disable (Unmount) a Swap Partition. Use swapoff device to disable the swap partition device. You can also use swapoff -a to disable all known swap devices.
  • Find out Swap Partition(s) in Use. Use swapon -s to get a list of all swap devices in use. The command is equivalent to cat /proc/swaps if the /proc file system is supported by the kernel.

Besides, you need to know that the data in the swap partition is volatile. You DON'T need to make any backup for the partition if you want to delete or relocate it!

Let's consider a practical example. We want to relocate a swap partition from /dev/hdb2 to /dev/hdb6. Here are the steps involved:

  1. Disable /dev/hdb2 swap partition with swapoff /dev/hdb2. If your computer RAM is small, you may consider rebooting the machine into runlevel 3 first before doing so. Without the X server, you generally won't use the swap partition and thus swapoff should not fail.
  2. Use fdisk, cfdisk, or any partition manipulator to delete /dev/hdb2 and create /dev/hdb6 swap partition. Note that it's nonsense to backup any data from /dev/hdb2!
  3. Format the swap partition with mkswap -L swap /dev/hdb6. It's always a good idea to label the swap partition. A label allows better reference to the swap partition from /etc/fstab.
  4. Edit /etc/fstab to reflect the relocation:
    ...
    LABEL=swap  none  swap  sw  0 0
    ...
    As you can see, if you have always labeled your (only) swap partition with a unique label, you can omit this step.
  5. Enable the swap partition with swapon -a, or simply reboot the machine.

Hope that you know how to play with swap partitions now without sweat. For more information, do man mkswap, man swapon, etc.