Hack 69. Create a swap file system.

by Ramesh

Create a file for swap usage as shown below.

  1. # dd if=/dev/zero of=/home/swap-fs bs=1M count=512
  2. 512+0 records in
  3. 512+0 records out
  4.  
  5. # ls -l /home/swap-fs
  6. -rw-r--r-- 1 root root 536870912 Jan 2 23:13 /home/swap-fs

Use mkswap to setup a Linux swap area in the /home/swap-fs file that was created above.

  1. # mkswap /home/swap-fs
  2.  
  3. Setting up swapspace version 1, size = 536866 kB

Once the file is created and has been setup for Linux swap area, it is time to enable the swap using swapon as shown below.

  1. # swapon /home/swap-fs

Add the following line to /etc/fstab and reboot the system for the swap to take into effect.

  1. /home/swap-fs swap swap defaults 0 0