Introduction

Creating a swap partition is quite straightforward but extremely useful when you’re running out of RAM.

Creating Swap

On a Partition

Use fdisk on the target partition:

  fdisk /dev/hdc1
  

Then create a primary partition with the key combination “n p 1”. Next, change the ID to indicate it’s a swap partition with “t 82”.

Save everything with the “w” key. To finish setting up our partition, we need to initialize it:

  mkswap /dev/hdc1
  

Now let’s use it:

  swapon /dev/hdc1
  

I recommend modifying your /etc/fstab file so that the swap is used on the next boot:

  ...
/dev/hdc1               swap                    swap    defaults        0 0
...
  

On a Disk Image

If you can’t create a swap partition, you have the option to create a disk image and assign it as additional swap:

  dd if=/dev/zero of=/swapfile bs=1M count=64
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
  

Obviously, this size is small and serves as an example. You should adjust the dd command according to the available space on one of your partitions and adapt it to your swap needs.

Resources

Last updated 19 Sep 2009, 21:47 CEST. history