swapon failed: Invalid argument

i’m having a hard time debugging this one. i have a node script to setup a swapfile after build before running:

#!/usr/bin/env node

import { writeFile } from 'node:fs/promises'
import { $ } from 'execa'

console.log('setting up swapfile...')
await $`fallocate -l 512M /swapfile`
await $`chmod 0600 /swapfile`
await $`mkswap /swapfile`
await writeFile('/proc/sys/vm/swappiness', '10')
await $`swapon /swapfile`
await writeFile('/proc/sys/vm/overcommit_memory', '1')
console.log('swapfile setup complete')

and i get an error: swapon failed: Invalid argument.

i tried to ssh into the machine and run the steps manually as well and get stuck with the same error (see repro below):

How can i enable swap on a fly machine?

1 Like

The easiest way is to set it in the swap_size_mb option in fly.toml.


works well, thanks for making me aware of this option jp.

That didn’t work :frowning:

Added swap_size_mb = 512 to my fly.toml file and the error continues:

level=INFO msg="executing command: node [./other/setup-swap.js]"
  setting up swapfile...
  		error = new Error(message);
  Error: Command failed with exit code 255: swapon /swapfile
  swapon: /swapfile: swapon failed: Invalid argument
      at makeError (file:///myapp/node_modules/execa/lib/error.js:60:11)
      at handlePromise (file:///myapp/node_modules/execa/index.js:124:26)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async file:///myapp/other/setup-swap.js:11:1 {
    shortMessage: 'Command failed with exit code 255: swapon /swapfile',
    command: 'swapon /swapfile',
    escapedCommand: 'swapon "/swapfile"',
    exitCode: 255,
    signal: undefined,
    signalDescription: undefined,
    stdout: '',
    stderr: 'swapon: /swapfile: swapon failed: Invalid argument',
    cwd: '/myapp',
    failed: true,
    timedOut: false,
    isCanceled: false,
    killed: false
  Node.js v20.12.2
  ERROR: cannot exec: sync cmd: cannot run command: exit status 1
  level=INFO msg="270A70BEF3E0B35F: exiting primary, destroying lease"
   INFO Main child exited normally with code: 1
   INFO Starting clean up.
   INFO Umounting /dev/vdd from /data
   WARN could not unmount /rootfs: EINVAL: Invalid argument

My setup swap file is exactly the same as @justinthareja

since you’re using the swap_size_mb option provided by fly, you no longer need to run the ./other/setup-swap.js node script. you can remove that step during your deployment and the error will go away

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.