4

This is a theoretical question.

Why can I only have 29 Swap files/partitions?

If I wanted to generate 10k 1MB swap file and swap them on, it fails at the 30 swap file with swapon: /tmp/swap29: swapon failed: Operation not permitted

the script to achieve this

#!/bin/bash
i=0
SWAP=/tmp/swap

while [ "$i" -lt 10000 ];do
    dd if=/dev/urandom of=$SWAP$i bs=1M count=1
    mkswap "${SWAP}${i}"
    swapon "${SWAP}${i}"
    i=$((i+1))
done

can someone explain this?

  • 1
    I added the "linux" tag here assuming that's the context (it is an OS specific issue). If not, you'll want to change that. – goldilocks Oct 29 '15 at 15:37
  • another theoretical question: how 10k 1MB swap files can be useful instead of just one 10GB swap file? or two of 5GB? – Scantlight Oct 29 '15 at 17:35
  • @Scantlight you are right of course. This script is just only a fast way to see what the problem is I am asking. It has no practical use. I was just wondering and I was not able to find any information on this. – syss Oct 29 '15 at 19:32
3

In plain English:

The kernel has to keep track of swap files/partitions, and it does that in a table. Each entry takes a small amount of memory, which is pre-allocated. So a more than reasonable default limit (max swap table size) was chosen.

Anyone who really needs more can modify and re-compile the kernel.

  • max swap table size is this a kernel config option or what? ... I'm not able to find such config option or a partial match. I just want to confirm what you said. – Scantlight Oct 29 '15 at 21:39
  • it was written in very simple english just to explain the concept, no technical details at all (because i'm too lazy to look it up :). No, it's not a kernel config option. There'll certainly be somewhere in the kernel swapping code where it is defined (and thus can be modified). It won't be called "max swap table size". – cas Oct 29 '15 at 21:42
  • so you can't prove this :) ... ok. – Scantlight Oct 29 '15 at 22:01
  • nope. a question that was stated to be merely idle curiousity got as much research effort as it warranted. the OP wanted an explanation. if the OP had said 'my server is dying because i can't add another swap file', i'd have put more effort in to find the technical details and how to change them and recompile. or said 'but more RAM, it's dirt cheap' – cas Oct 29 '15 at 22:05
  • you are right :) ... this seems to be a good theoretical explanation. although no one can say that this is true or not. – Scantlight Oct 29 '15 at 22:10
1

The answer is found here: https://www.kernel.org/doc/gorman/html/understand/understand014.html

It says "... declared array called swap_info which holds MAX_SWAPFILES, which is statically defined as 32, entries. This means that at most 32 swap areas can exist on a running system."

There is even more information in Chapter 11.2 on this link which is far more than I want to know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.