Wednesday, 28 August 2013

Create Fake Swap in OpenVZ VPS if you get swapon failed: Operation not permitted Error

if you get swapon failed: Operation not permitted Error even if you run as root it is because in your VPS swap creation is not allowed
follow below steps to Create Fake Swap in OpenVZ VPS

[root@server] free -m
              total       used       free
Mem:          4096        104       3991
-/+ buffers/cache:        104       3991
Swap:            0          0          0

create shell script file add lines like below

[root@server] vi fakeswap.sh
#!/bin/bash

SWAP="${1:-512}"


NEW="$[SWAP*1024]"; TEMP="${NEW//?/ }"; OLD="${TEMP:1}0"


umount /proc/meminfo 2> /dev/null

sed "/^Swap\(Total\|Free\):/s,$OLD,$NEW," /proc/meminfo > /etc/fake_meminfo
mount --bind /etc/fake_meminfo /proc/meminfo

free -m


[root@server] chmod +x fakeswap.sh
[root@server] sh fakeswap.sh
[root@server] free -m
 total       used       free
Mem:          4096        104       3991
-/+ buffers/cache:        104       3991
Swap:          512          0        512

to create 1024MB Swap run like below
[root@server] sh fakeswap.sh 1024
[root@server] free -m
 total       used       free
Mem:          4096        104       3991
-/+ buffers/cache:        104       3991
Swap:          1024          0        1024


Share This!



13 comments:

  1. I gave this a shot and am getting the following error: /root/fakeswap.sh: 5: Bad substitution Any thoughts?

    ReplyDelete
    Replies
    1. Got an answer to my own question via stack overflow: http://stackoverflow.com/a/19922556/2540204 In summary, the issue was the attempt to use bourne shell to execute a bash script. Other than that, the hack works really well!

      Delete
  2. Thanks Man

    It really solved the problem of creating swap files


    Regards,
    Ashok G

    ReplyDelete
  3. Just write fake memory, mem also came fake no more responding, are you joke ?

    mount --bind /etc/fake_meminfo /proc/meminfo lol ?

    ReplyDelete
  4. Easy copy+paste from http://pastebin.com/riGXh9nq

    ReplyDelete
  5. wasn't able to do this. is there a way to backtrack. my swap isn't working now, and how can i free up the space used? thx.

    total used free shared buffers cached
    Mem: 256 225 30 103 0 61
    -/+ buffers/cache: 164 91
    Swap: 256 0 255

    ReplyDelete
  6. This just fakes your /proc/meminfo

    ReplyDelete
  7. This just fakes your /proc/meminfo

    ReplyDelete
  8. Does it work? I am using 4gb ram server, please advice me. My website: http://youtubetoany.com

    ReplyDelete
  9. sh fakeswap.sh
    fakeswap.sh: 5: fakeswap.sh: Bad substitution

    ReplyDelete
  10. I see no change by using this shell script. no difference between before and after

    ReplyDelete
  11. This does not generate a swap, it is even if it does not generate swap.

    ReplyDelete

Here We Write The Problems we face at different situations and Solutions to those problems.