mkswap

Create and set up SWAP swap partition

Supplementary instructions

mkswap command is used to create a swap partition on a file or device. After creation, use the sawpon command to start using this swap area. The last optional parameter specifies the size of the swap area, but this parameter is set for backward compatibility and is not necessary. Generally, the entire file or device is used as the swap area.

grammar

mkswap(options)(parameters)

Options

-c: Before creating the swap area, check whether there are damaged blocks;
-f: When creating a swap area on a SPARC computer, this parameter must be added;
-v0: Create an old swap area, this is the default value;
-v1: Create a new swap area.

Parameters

Device: Specify the device file or swap file corresponding to the swap space.

Example

Check the system swap space size:

free -m
total used free shared buffers cached
Mem: 377 180 197 0 19 110
-/+ buffers/cache: 50 327
Swap: 572 0 572

View the current swap space (file(s)/partition(s)):

swapon -s

Equivalent to

cat /proc/swaps

Add swap space

Add a swap partition or add a swap file. It is recommended that you add a swap partition; however, if you do not have much free space available, add a swap file.

To add a swap partition, follow these steps:

Use fdisk to create a swap partition (assuming /dev/sdb2 is the created swap partition), and use the mkswap command to set up the swap partition:

mkswap /dev/sdb2

Enable swap partition:

swapon /dev/sdb2

Write to /etc/fstab to enable on boot:

/dev/sdb2 swap swap defaults 0 0

To add a swap file, follow these steps:

Create a swap file of size 512M:

dd if=/dev/zero of=/swapfile1 bs=1024 count=524288

Use the mkswap command to set up the swap file:

mkswap /swapfile1

Enable swap partition:

swapon/swapfile1

Write to /etc/fstab to enable on boot:

/swapfile1 swap swap defaults 0 0

After adding a new swap partition and enabling it, check the output of the cat /proc/swaps or free command to make sure the swap partition is enabled.

Remove swap space:

Disable swap partition:

swapoff /dev/sdb2

Remove items from /etc/fstab and use fdisk or yast tools to delete partitions.