Linux Swapfile Error
In this tutorial, you'll learn about Linux Swapfile Creation Error Fix. We cover key concepts, practical examples, and best practices.
Creating a swapfile on Linux fails with errors like "mkswap: error: swap area needs to be at least 40 KiB", "swapon: /swapfile: swapon failed: Invalid argument", or "swapon: /swapfile: skipping - it appears to have holes". These occur when the file has incorrect permissions, filesystem holes, or unsupported file attributes.
The Problem
sudo mkswap /swapfile
Error:
mkswap: error: /swapfile: insecure permissions 0644, 0600 suggested.
Or:
sudo swapon /swapfile
Error:
swapon: /swapfile: swapon failed: Invalid argument
Wrong Approach
# WRONG — creating a swapfile with cp or fallocate without precautions
sudo cp /dev/zero /swapfile # Wrong size
sudo fallocate -l 2G /swapfile # Holes not supported on all filesystems
Right Approach
# Create a swapfile with dd (works on all filesystems)
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
# Set correct permissions
sudo chmod 600 /swapfile
# Set up swap area
sudo mkswap /swapfile
# Enable swap
sudo swapon /swapfile
Expected output:
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied
Setting up swapspace version 1, size = 2 GiB
...
swapon /swapfile
Step-by-Step Fix
Step 1: Check filesystem support for swapfiles
# Btrfs and certain filesystems require specific settings
# For Btrfs, create with:
sudo btrfs filesystem mkswapfile --size 2G /swapfile
Step 2: Create the swapfile
# For ext4, XFS:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
# For Btrfs:
sudo btrfs filesystem mkswapfile --size 2G /swapfile
Step 3: Set correct permissions
sudo chmod 600 /swapfile
Step 4: Format as swap
sudo mkswap /swapfile
Step 5: Enable swap
sudo swapon /swapfile
Step 6: Verify swap is active
sudo swapon --show
free -h
Expected:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
Step 7: Make permanent in fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Prevention Tips
- Use
ddinstead offallocateon ext4/XFS to avoid filesystem holes - Use
btrfs filesystem mkswapfileon Btrfs instead of dd + mkswap - Always set
chmod 600on the swapfile - Verify swap is enabled after every reboot with
swapon --show - Set swap size to at least RAM size for hibernation support
Common Mistakes with swapfile error
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro