Skip to content

Linux Swapfile Error

DodaTech 3 min read

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 dd instead of fallocate on ext4/XFS to avoid filesystem holes
  • Use btrfs filesystem mkswapfile on Btrfs instead of dd + mkswap
  • Always set chmod 600 on 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

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [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

### Why does swapon fail with "Invalid argument"?

The swapfile has filesystem holes (created by fallocate) or is on a filesystem that requires special handling (Btrfs with Copy-on-Write enabled). Use dd instead of fallocate and disable COW: chattr +C /swapfile before creating.

Should I use a swap partition or a swapfile?

Swapfiles are easier to resize and manage without repartitioning. Swap partitions have slightly better performance on spinning disks. For SSDs, the difference is negligible. Swapfiles are the default on modern Ubuntu installations.

How big should my swapfile be?

For systems with 1-4GB RAM, use 2x RAM. For 4-8GB, use 1x RAM. For 8-32GB, use 0.5x RAM. For 32GB+, use 4-8GB unless you need hibernation (which requires swap >= RAM size).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro