openvz的廉价小鸡使用常规的增加swap方式会报错:
failed: Operation not permitted
基于此,我们使用如下方式,进行swap的添加。
Ubuntu 16.04 LTS (GNU/Linux 2.6.32-042stab116.2 x86_64)
Ubuntu 20.04.1 LTS x86_64
openvz 120M内存/256内存
root@webhk:~# free -m
total used free shared buff/cache available
Mem: 256 138 4 1 113 115
vim mkswap.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
chmod +x mkswap.sh
sudo ./mkswap.sh 768
root@webhk:~# free -m
total used free shared buff/cache available
Mem: 256 138 4 1 113 115
Swap: 768 0 768
sudo ./mkswap.sh 0