mkswap
Mark a partition or file as swap
TLDR
Set up a given swap area
Check a partition for bad blocks before creating the swap area
Specify a label for the partition (to allow swapon to use the label)
SYNOPSIS
mkswap [options] <device>
PARAMETERS
-vN
Specify the swap space version number. N can be 0 or 1. The default is to autodetect; use version 1 if available, otherwise version 0.
-L <label>
Specifies a label for the swap space.
This label can be used by 'swapon' to activate swap by label instead of by device name.
-U <UUID>
Specifies a UUID for the swap space.
This UUID can be used by 'swapon' to activate swap by UUID instead of by device name.
-f
Forces mkswap to proceed, even if it suspects the device is already in use or is not a valid swap device.
-p <page-size>
Specifies the page size to use for the swap space. This option is rarely needed, as the default page size is usually appropriate.
-h
Displays help text and exits.
-V
Displays version information and exits.
<device>
The device or file to be initialized as swap space. This is a mandatory argument.
DESCRIPTION
The mkswap command is used to create a swap area on a device or in a file.
Swap space is used by the Linux kernel to provide virtual memory, allowing the system to run more programs than would fit in physical RAM.
mkswap initializes the specified device or file so that it can be used as swap space.
It does not enable the swap space; that is done with the 'swapon' command.
Using a file for swap space is generally slower than using a dedicated partition.
The command writes a swap signature to the beginning of the specified device/file and sets up necessary data structures for the kernel to use it as swap.
CAVEATS
Using mkswap on a partition containing data will destroy that data. Ensure you specify the correct device or file.
Swap files can sometimes be slower than dedicated swap partitions.
SWAP SPACE AND PERFORMANCE
While swap space can help a system avoid out-of-memory errors, excessive swapping can significantly degrade performance.
It's generally best to have enough RAM to minimize the need for swapping.
Consider using a swap partition for better performance than a swap file.
SWAP SPACE CONFIGURATION IN /ETC/FSTAB
To enable swap space automatically at boot time, you need to add an entry to the /etc/fstab file. This entry specifies the device or file to use as swap, along with mount options (typically 'swap' and 'sw').
HISTORY
The mkswap command has been a standard part of Unix-like operating systems, including Linux, for a very long time.
Its primary function has remained the same: to prepare a device or file for use as swap space.
Over time, features like labels and UUIDs have been added to improve swap management and flexibility.