Working with SWAP in Linux
by Rajesh Sivaraman • February 14, 2012 • Linux • 0 Comments
From Wikipedia : “From a software point of view with the 2.6 Linux kernel, swap files are just as fast as swap partitions. The kernel keeps a map of where the swap file exists, and accesses the disk directly, bypassing caching and filesystem overhead. Red Hat recommends using a swap partition. With a swap partition one can choose where on the disk it resides and place it where the disk throughput is highest. The administrative flexibility of swap files can outweigh the other advantages[specify] of swap partitions.
Linux supports using a virtually unlimited number of swapping devices, each of which can be assigned a priority. When the operating system needs to swap pages out of physical memory, it uses the highest-priority device with free space. If multiple devices are assigned the same priority, they are used in a fashion similar to level 0 RAID arrangements. This provides improved performance as long as the devices can be accessed efficiently in parallel. Therefore, care should be taken assigning the priorities. For example, swaps located on the same physical disk should not be used in parallel, but in order ranging from the fastest to the slowest (i.e.: the fastest having the highest priority).”
Today we are going to look at both ways of enabling swap space on your linux box. i.e. Swap partition and Swap file. Get ready and load up our ultimate weapon, the terminal.
1. Using a Partition as SWAP
First, we will create a partition. In this case I am creating a 512 M partition in the second disk ( /dev/sdb ) of my computer. What we need to make sure is that the partition type is set to 82 instead of the default 83. After the partition is created, we will make it ready as swap space and then enable swap on it. Here’s what my terminal output looked like.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
[root@server ~]# fdisk -cu /dev/sdb
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +512M
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=93c1f282-a65d-4b4b-8ea5-9f9df5320309
[root@server ~]# free -m
total used free shared buffers cached
Mem: 499 118 380 0 6 55
-/+ buffers/cache: 56 443
Swap: 0 0 0
[root@server ~]# swapon -v /dev/sdb1
swapon on /dev/sdb1
swapon: /dev/sdb1: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/sdb1: pagesize=4096, swapsize=536870912, devsize=536870912
[root@server ~]#
[root@server ~]# free -m
total used free shared buffers cached
Mem: 499 118 380 0 6 55
-/+ buffers/cache: 56 443
Swap: 511 0 511
[root@server ~]#
[root@server ~]# |
As you can see in the free -m result, we have successfully enabled 512 M swap space in the system. If you want to make this swap available permanently in the system, add it to the /etc/fstab file as below.
/dev/sdb1 swap swap defaults 0 0
That will make it available even if you reboot the system. Now let’s take a look at how to do the same thing without making a partition, in the following section.
2. Using a file as SWAP
First, we create a file using the dd command. Then we make the swap available from the file. This method is particularly useful when you don’t have enough free space available on disk to create a new partition.
[root@server ~]# dd if=/dev/zero of=/opt/swap.file bs=512 count=1000000
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 18.8782 s, 27.1 MB/s
[root@server ~]#
[root@server ~]# mkswap /opt/swap.file
mkswap: /opt/swap.file: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 499996 KiB
no label, UUID=880adc48-0735-41a1-a3c1-63a59d852513
[root@server ~]#
[root@server ~]# free -m
total used free shared buffers cached
Mem: 499 476 22 0 3 410
-/+ buffers/cache: 62 436
Swap: 0 0 0
[root@server ~]# chmod 0600 /opt/swap.file
[root@server ~]# swapon -v /opt/swap.file
swapon on /opt/swap.file
swapon: /opt/swap.file: found swap signature: version 1, page-size 4, same byte order
swapon: /opt/swap.file: pagesize=4096, swapsize=512000000, devsize=512000000
[root@server ~]# free -m
total used free shared buffers cached
Mem: 499 476 22 0 3 410
-/+ buffers/cache: 63 436
Swap: 488 0 488
[root@server ~]# |
For /etc/fstab , please use the below line :
/opt/swap.file swap swap defaults 0 0
Hope you find this article useful. Feel free to try, share and discuss.
Related posts:
