Search

How to create and manage Linux swap space, file based swap, partition based swap

Create an additional swap partition and enabling swaping

• Check the current swap usage by using the commands “swapon –s” or “free”

Example:

[root@RHEL2 ~]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 803240 0 -1

[root@RHEL2 ~]# free -m
total used free shared buffers cached
Mem: 503 492 10 0 37 346
-/+ buffers/cache: 108 394
Swap: 784 0 784

• Create a new partition using fdisk. Set the partition’s system id as “82” (Linux Swap / Solaris).

Note: You should be familiar with the Linux fdisk command to learn this lesson. Click the following link to learn how to use the Linux fdisk command.

[root@RHEL2 ~]# fdisk /dev/sdd

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-783, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-783, default 783): +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.

 

• Create the swap filesystem on the raw partition using “mkswap” command.

[root@RHEL2 ~]# mkswap /dev/sdd1
Setting up swapspace version 1, size = 518156 kB

• Enable swaping on the swap partition created now.

[root@RHEL2 ~]# swapon /dev/sdd1

• Update /etc/fstab to make use the new swap partition whenever the system is rebooted.

LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
# Beginning of the block added by the VMware software
.host:/ /mnt/hgfs vmhgfs defaults,ttl=5 0 0
# End of the block added by the VMware software
/dev/sdd1 swap swap defaults 0 0

Note: To disable swaping on a partition, use “swapoff” command.

Enable file based swaping

We can create file based swap space (similar to Windows pagefile), if there is not available partition, but space is available in already existing partitions.

• To enable file based swap space, you must create a file using “dd” command.

[root@RHEL2 ~]# dd if=/dev/zero of=/root/swap bs=1024 count=524288
524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 4.98139 seconds, 108 MB/s

• Overlay a swap file system on the binary file created using the above command.

[root@RHEL2 ~]# mkswap /root/swap
Setting up swapspace version 1, size = 536866 kB

• Enable swaping on the swap file created now by using “swapon” command.

[root@RHEL2 ~]# swapon /root/swap

• Update /etc/fstab to make sure that the swap file is available to the OS after a reboot.

LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
# Beginning of the block added by the VMware software
.host:/ /mnt/hgfs vmhgfs defaults,ttl=5 0 0
# End of the block added by the VMware software
/dev/sdd1 swap swap defaults 0 0
/root/swap swap swap defaults 0 0

Related Tutorials
• Introduction to Logical Volume Manager (LVM)
• How to create and manage Logical Volume Manager (LVM)
• Introduction to Redundant Array of inexpensive (or Independent) Disks (RAID)
• How to create and manage Redundant Array of Inexpensive Disks (RAID)
• Introduction to swap space