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
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
I gave this a shot and am getting the following error: /root/fakeswap.sh: 5: Bad substitution Any thoughts?
ReplyDeleteGot 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!
DeleteFoor noobs like me and others, type not sh fakeswap.sh, type ./fakeswap.sh
DeleteThanks Man
ReplyDeleteIt really solved the problem of creating swap files
Regards,
Ashok G
Just write fake memory, mem also came fake no more responding, are you joke ?
ReplyDeletemount --bind /etc/fake_meminfo /proc/meminfo lol ?
Easy copy+paste from http://pastebin.com/riGXh9nq
ReplyDeletewasn'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.
ReplyDeletetotal used free shared buffers cached
Mem: 256 225 30 103 0 61
-/+ buffers/cache: 164 91
Swap: 256 0 255
This just fakes your /proc/meminfo
ReplyDeleteThis just fakes your /proc/meminfo
ReplyDeleteDoes it work? I am using 4gb ram server, please advice me. My website: http://youtubetoany.com
ReplyDeletesh fakeswap.sh
ReplyDeletefakeswap.sh: 5: fakeswap.sh: Bad substitution
I see no change by using this shell script. no difference between before and after
ReplyDeleteThis does not generate a swap, it is even if it does not generate swap.
ReplyDeleteWhere is swap file stored in , BTW it's solves my yum install problem .
ReplyDelete