Disk
mkswap
Set up a Linux swap area on a device or file.
swapmemorydiskpartitionvirtual-memory
Additional Notes
mkswap prepares a block device or regular file to be used as Linux swap space. It writes a swap signature to the device, which the kernel recognizes when the swap area is activated with swapon.
Swap space is used by the kernel as virtual memory extension: when physical RAM is full, less frequently accessed pages are moved to swap. Swap can be on a dedicated partition, a file on an existing filesystem, or an LVM logical volume.
Syntax
mkswap [options] device [size-in-blocks]
Parameters
device: The block device, file, or partition to set up as swap (e.g.,/dev/sda2,/swapfile).size-in-blocks: Optional size in 1024-byte blocks. If omitted, the entire device is used.
Common Options
-f,--force: Force creation even if the device appears to contain a filesystem.-c,--check: Check the device for bad blocks before creating the swap area.-L label,--label label: Set a label for the swap area.-U UUID,--uuid UUID: Set a specific UUID.-v,--verbose: Show detailed output.-p priority,--priority priority: Set swap priority (higher numbers are used first).
Examples
mkswap /dev/sda2
Set up /dev/sda2 as a swap partition.
mkswap -L "swap1" /dev/sda2
Create swap with a label.
mkswap -f /dev/sdb1
Force creation on a device that may already have data or a filesystem.
dd if=/dev/zero of=/swapfile bs=1M count=2048
mkswap /swapfile
chmod 600 /swapfile
swapon /swapfile
Create a 2 GB swap file and activate it.
mkswap -U "random" /dev/sda2
Assign a random UUID to the swap area.
Practical Notes
- Activate the swap area with
swapon /dev/sda2and deactivate withswapoff /dev/sda2. - To make swap permanent, add an entry to
/etc/fstab: - Partition:
/dev/sda2 none swap defaults 0 0 - File:
/swapfile none swap defaults 0 0 - Use
swapon --showto list active swap areas. - Swap files must not have holes. Always use
dd(notcpor truncate) to create them. - Set permissions on swap files to
600for security, since they may contain sensitive data.