Monday, September 8, 2008

Creating and Using Swap Partitions

Swap partitions are needed in Linux systems to hold data that overflows from your sys-
tem’s RAM. If you didn’t create a swap partition when you installed Linux, you can cre-
ate it later using the mkswap command. You can create your swap partition either on a regular
disk partition or in a file formatted as a swap partition. Here are some examples:

$ sudo mkswap /dev/sda1 Format sda1 as a swap partition
Setting up swapspace version 1, size = 205594 kB

To check your swap area for bad blocks, use the -c option to mkswap:
$ sudo mkswap -c /dev/sda1

If you don’t have a spare partition, you can create a swap area within a file:
$ sudo dd if=/dev/zero of=/tmp/swapfile count=65536
65536+0 records in
65536+0 records out
33554432 bytes (34 MB) copied, 1.56578 s, 21.4 MB/s

$ sudo chmod 600 /tmp/swapfile
$ sudo mkswap /tmp/swapfile
Setting up swapspace version 1, size = 67104 kB

The dd command above creates a 32MB file named swapfile. The chmod command locks down the permissions on the file, to avoid getting a warning from the swapon command down the road. The mkswap command formats the /tmp/swapfile file to be a swap partition.


After you have created a swap partition or swap file, you need to tell the system to use
the swap area you made using the swapon command. This is similar to what happens at boot time. Here are examples:

$ sudo swapon /dev/sda1 Turn swap on for /dev/sda1 partition

$ sudo swapon -v /dev/sda1 Increase verbosity as swap is turned on

$ sudo swapon -v /tmp/swapfile Turn swap on for the /tmp/swapfile file


You can also use the swapon command to see a list of your swaps files and partitions:

$ swapon -s View all swap files and partitions that are on

Filename Type Size Used Priority
/dev/sda5 partition 1020088 142764 -1
/tmp/swapfile file 65528 0 -6


To turn off a swap area, you can use the swapoff command:
$ sudo swapoff -v /tmp/swapfile
swapoff on /tmp/swapfile

Swap areas are prioritized. The kernel will swap first to areas of high priorities, and
then go down the list. Areas of the same priority get striped between. You can specify
the priority of your swap area as you enable it using the -p option:

$ sudo swapon -v -p 1 /dev/sda1 Assign top swap priority to sda1


to get complete swap space details:- type cat /proc/swaps

No comments:

Post a Comment