mkswap
Create Linux swap areas on partitions or files for virtual memory
Syntax
mkswap [options] device
Basic Usage
Create swap area on partition
mkswap /dev/sdb2
Creates a swap area on the specified partition.
Create swap area with label
mkswap -L "SWAP" /dev/sdb2
Create swap area with custom UUID
mkswap -U 12345678-1234-1234-1234-123456789abc /dev/sdb2
Common Options
-c, --check: Check for bad blocks before creating swap area-f, --force: Force creation even if the device is not a partition-L, --label label: Set swap area label-p, --pagesize size: Set page size in bytes-U, --uuid uuid: Set swap area UUID-v, --verbose: Enable verbose output-V, --version: Display version information
Practical Examples
Create basic swap partition
mkswap /dev/sdb2
Create swap with label and check for bad blocks
mkswap -L "SWAP" -c /dev/sdb2
Create swap with custom page size
mkswap -p 4096 /dev/sdb2
Create swap with specific UUID
mkswap -U 12345678-1234-1234-1234-123456789abc /dev/sdb2
Force creation on non-partition device
mkswap -f /dev/loop0
Create swap with verbose output
mkswap -v /dev/sdb2
Complete Swap Setup Process
1. Create swap area
mkswap /dev/sdb2
2. Activate swap area
swapon /dev/sdb2
3. Add to /etc/fstab for persistence
echo "/dev/sdb2 none swap sw 0 0" >> /etc/fstab
Best Practices
Swap Space Guidelines
- Systems with < 4GB RAM: 1.5-2x RAM
- Systems with 4-8GB RAM: 1x RAM
- Systems with > 8GB RAM: 0.5x RAM or less
- SSDs: Consider reducing swap usage
- Servers: Monitor actual swap usage patterns
Important Considerations
- Swap areas will DESTROY all data on the device
- Ensure the partition is not mounted before running mkswap
- Use appropriate partition type (82 for Linux swap)
- Consider using swap files instead of partitions for flexibility
- Monitor swap usage to optimize performance