Swap File Creation

This page explains how to create and use a file-based swap in Linux.  This will generally be used in VMware and Xen images to simplify the images.

  1. Type the following command to create a 512MB swap file (1024*512 = 524288 block size – multiply to generate a bigger swap file)
    $ sudo dd if=/dev/zero of=/swap.file bs=1024 count=524288
  2. Set up a linux swap area
    $ sudo mkswap /swap.file
  3. Activate the swap space immediately
    $ sudo swapon /swap.file
  4. To active the swap space after a system reboot, and an entry to /etc/fstab.  Open this file using a text editor such as vim.
    $ sudo vim /etc/fstab
  5. Append the following line
    /swap.file              swap                    swap    defaults        0 0
  6. Verify swap is activated
    $ free -m