Thursday, August 29, 2013
2:49 AM

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


0 comments:

Post a Comment