LinuxCommandLibrary

mkswap

Mark a partition or file as swap

TLDR

Set up a given swap area

$ sudo mkswap [path/to/file]
copy

Check a partition for bad blocks before creating the swap area
$ sudo mkswap [[-c|--check]] [path/to/file]
copy

Specify a label for the partition (to allow swapon to use the label)
$ sudo mkswap [[-L|--label]] [label] [/dev/sdXY]
copy

Use the specified UUID
$ sudo mkswap [[-U|--uuid]] [clear|random|time|UUID_value]
copy

Set up a swap file (for btrfs, see tldr btrfs filesystem instead)
$ sudo mkswap [[-s|--size]] [file_size] [[-F|--file]] [path/to/swapfile]
copy

SYNOPSIS

mkswap [options]

PARAMETERS

--check, -c
    Checks the device for bad blocks before creating the swap space. This can increase the time required for the operation.

--force, -f
    Forces mkswap to proceed even if the specified device is busy, mounted, or appears to contain an existing file system or swap signature. Use with extreme caution as this can lead to irrevocable data loss.

--label
    Specifies a label for the swap area. Labels can be useful for identifying swap partitions, especially in /etc/fstab entries, providing a more human-readable identifier than a UUID.

--uuid , -U
    Specifies the UUID (Universally Unique Identifier) to be used for the swap area. If not provided, mkswap generates a new, random UUID. Using UUIDs in /etc/fstab is generally recommended for stable system configurations.

--pagesize , -p
    Specifies the page size in bytes. This option is usually not needed as mkswap automatically determines the appropriate page size based on the system's architecture.

--size , -s
    Specifies the desired size of the swap area in 1KB blocks. This option is mostly deprecated as mkswap can infer the size from the device or file's total capacity. It's primarily useful for creating swap files with specific sizes.

--discard[=once|pages], -d[=once|pages]
    Enables discard/TRIM support for SSDs or thin-provisioned LVM volumes. once discards all blocks once, pages discards pages when they are freed.

--version, -v
    Displays version information and exits.

--help, -h
    Displays a help message and exits.

DESCRIPTION

mkswap is a Linux command used to set up a swap area on a device or file. A swap area is a portion of a hard drive, SSD, or even a regular file, that is used as virtual memory. When the system runs out of physical RAM, inactive pages of memory are moved to swap space, freeing up RAM for active processes. This process is known as "swapping" or "paging out".

The mkswap command writes a swap signature to the specified device or file, marking it as a valid swap space. This signature includes crucial information like the UUID, label, and page size. It is crucial to understand that mkswap only prepares the swap space; it does not activate it. After running mkswap, the swap area must be enabled using the swapon command. To make the swap space persistent across reboots, an entry must be added to the /etc/fstab file. Using mkswap on a device will overwrite any existing data on it, so extreme caution is advised to avoid data loss.

CAVEATS

  • Data Loss Risk:
    mkswap overwrites all data on the specified device or file. Ensure you are targeting the correct volume to prevent accidental data loss.
  • Activation Required:
    Creating a swap area with mkswap does not automatically activate it. You must use the swapon command (e.g., swapon /dev/sdXn or swapon /path/to/swapfile) to enable it.
  • Persistence:
    For swap to be active after reboot, an entry must be added to /etc/fstab (e.g., UUID=<uuid> none swap sw 0 0 or /path/to/swapfile none swap sw 0 0).
  • Performance:
    While convenient, using a swap file on a general-purpose filesystem can be slower than using a dedicated swap partition due to filesystem overhead and fragmentation.

<I>CREATING A SWAP FILE</I>

Instead of a dedicated partition, mkswap can create a swap area within a regular file. First, create a file of the desired size using fallocate (or dd for older systems), for example: fallocate -l 2G /swapfile. Then, secure its permissions: chmod 600 /swapfile. Finally, format it with mkswap: mkswap /swapfile. This method offers flexibility but might be slower than a dedicated partition.

<I>MAKING SWAP PERMANENT</I>

To ensure your swap space is activated automatically on every system boot, you need to add an entry to the /etc/fstab file. It's highly recommended to use the UUID of the swap partition/file for reliable identification. You can find the UUID using blkid or lsblk -f. An entry would typically look like: UUID=YOUR_SWAP_UUID none swap sw 0 0 for a partition, or /path/to/swapfile none swap sw 0 0 for a swap file. After editing fstab, run swapon -a to activate all swap entries specified in the file.

HISTORY

The mkswap command is a fundamental utility within the util-linux project, which provides a wide range of essential system utilities for Linux. It has been a core component of Linux distributions since the early days of the operating system, evolving alongside kernel developments to support various swap formats and features like UUIDs and labels, improving system administration and reliability.

SEE ALSO

swapon(8), swapoff(8), free(1), fstab(5), lsblk(8)

Copied to clipboard