How to use the command 'mkswap' (with examples)
- Linux
- December 25, 2023
The ‘mkswap’ command is used to set up a Linux swap area on a device or in a file. Swap space is a portion of a hard disk that is used as virtual memory when the system’s physical memory (RAM) is full. This command allows users to create and configure a swap area, which can improve system performance by providing additional memory when needed.
Use case 1: Set up a given swap area
Code:
sudo mkswap path/to/file
Motivation:
The motivation for using this example is to create a swap area on a specified file. This file can be a regular file or a swap partition. Setting up a swap area is essential for systems with limited physical memory, as it allows the operating system to allocate virtual memory when the RAM is full.
Explanation:
sudo
: This command is used to execute the ‘mkswap’ command with administrative privileges.mkswap
: This is the main command that sets up the swap area.path/to/file
: This argument specifies the file or partition on which the swap area will be created.
Example output:
Setting up swapspace version 1, size = 2 GB (2147479552 bytes)
no label, UUID=e9a41f5f-2074-4e3a-90d2-69bad7c2d7e3
Use case 2: Check a partition for bad blocks before creating the swap area
Code:
sudo mkswap -c path/to/file
Motivation:
The motivation for using this example is to check a partition for bad blocks before creating the swap area. This can be helpful in identifying and avoiding any potential issues with the partition that may affect the functionality of the swap area.
Explanation:
sudo
: This command is used to execute the ‘mkswap’ command with administrative privileges.-c
: This option instructs ‘mkswap’ to check for bad blocks on the specified partition before creating the swap area.path/to/file
: This argument specifies the file or partition that needs to be checked for bad blocks.
Example output:
Setting up swapspace version 1, size = 2 GB (2147479552 bytes)
no label, UUID=e9a41f5f-2074-4e3a-90d2-69bad7c2d7e3
Checking for bad blocks in /dev/sda1
Use case 3: Specify a label for the partition (to allow ‘swapon’ to use the label)
Code:
sudo mkswap -L label /dev/sda1
Motivation:
The motivation for using this example is to specify a label for the partition, which allows the ‘swapon’ command to use the label instead of specifying the entire device path.
Explanation:
sudo
: This command is used to execute the ‘mkswap’ command with administrative privileges.-L label
: This option sets a label for the partition to be used by the ‘swapon’ command./dev/sda1
: This argument specifies the device path of the partition for which the label needs to be set.
Example output:
Setting up swapspace version 1, size = 2 GB (2147479552 bytes)
label = label, UUID=e9a41f5f-2074-4e3a-90d2-69bad7c2d7e3