Swap file

From Sysadmin

Jump to: navigation, search

Linux

When using the Linux 2.6 kernel a swap file performs just as well as a swap partition[1]. A swap file is also more flexible as it can be created or destroyed dynamically on a filesystem as required.

It is important to note that the system cannot suspend to a swap file. So a Linux laptop wanting to suspend will need a swap partition.

Creating A Swap File

Create the file of 2GB

dd if=/dev/zero of=/swapfile bs=1024 count=2048k

Prepare the file as a swap file

mkswap /swapfile

Enable the swap file

swapon /swapfile

Make it permanent

Add the following line to /etc/fstab in order for the new swap file to be a enabled on boot:

/swapfile swap swap defaults 0 0

Prevent the swapfile from being backed up (if possible)

Set the no dump attribute on the file:

chattr +d /swapfile

Note: Only some backup utilities (eg, xfsdump & dump) honour this attribute and even then a switch may have to passed to the utility.

Personal tools