Unable to create swap file with error “swapon failed: Invalid argument”

Unable to create swap file with error “swapon failed: Invalid argument”

Environment

  • Red Hat Enterprise Linux 6
  • Red Hat Enterprise Linux 7
  • XFS

Issue

  • swapon fails with error “swapon failed: Invalid argument” if swap file created using fallocate

Resolution

There are 3 resolutions :
  • Create the swap file with dd instead of fallocate if using XFS on RHEL6 or RHEL7.
  • Create the swap file on ext filesystem if using RHEL6 or RHEL7.
  • Can upgrade to RHEL8 as issue is fixed for XFS.

Root Cause

The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the filesystem. This is a problem on files with holes. Commands like cp(1) or truncate(1) create files with holes. These files will be rejected by swapon.
Preallocated files created by fallocate(1) may be interpreted as files with holes too depending of the filesystem. Preallocated swap files are supported on XFS since Linux 4.18.

Diagnostic Steps

  • Try to create the file with fallocate.
[root@server ~]# fallocate -l 4294967296 /swap
  • Make the swap on above file.
[root@server ~]# mkswap /swap 
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=a830901f-613d-4ed9-bb89-f076eb6d94f7
  • Change permission to 600 for swap file.
[root@server ~]# chmod 600 /swap 
  • Enable the swap to see error.
[root@server ~]# swapon /swap
swapon: /swap: swapon failed: Invalid argument

Leave a Comment

Your email address will not be published. Required fields are marked *

To Resolve this issue at just 5$ from ServerExpert

https://serverexpert.io/

For Hosting please visit Hostingwebsite.io

You have Successfully Subscribed!

Open chat