Interview Prep/Linux/Disk space, inodes and filesystems

A swap file won't activate: `swapon: /swapfile: insecure permissions 0644, 0600 suggested` followed by `swapon failed: Invalid argument`.

IntermediateTroubleshootingDisk space, inodes and filesystems
Answer

Likely causes → The permissions warning is cosmetic; Invalid argument usually means the file has holes (created with fallocate on a filesystem that doesn't support it for swap, like older XFS or btrfs) or wasn't mkswaped.

Diagnose →

ls -l /swapfile; file /swapfile
findmnt -no FSTYPE /

Fix →

sudo swapoff /swapfile 2>/dev/null; sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile

On btrfs use btrfs filesystem mkswapfile.

Prevent → Use dd for swap files in scripts that must work on any filesystem.

All Linux questions Roadmap: Linux

More on Disk space, inodes and filesystems