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.
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).mkswap [-L label] device to format a partition device and label it with label. E.g., mkswap -L swap /dev/hda3.swapon device to enable the swap partition device. You can also use swapon -a to enable all partitions marked as swap in /etc/fstab.swapoff device to disable the swap partition device. You can also use swapoff -a to disable all known swap devices.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:
/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.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!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./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.
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.