Linux虚拟内存创建

写了一个脚本方便一键扩容虚拟分区
简单版本

1
2
3
4
5
6
swapoff -a
dd if=/dev/zero of=/swapfile bs=1M count=2000
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
grep SwapTotal /proc/meminfo

复杂版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
#

if test $(whoami) != root
then
echo "require root privilege"
exit 1
fi

SWAPFILE_SIZE_GB=8 # 8GB

SWAPFILE_SIZE=$(stat --format="%s" /swapfile)
EXPECT_SIZE=$(($SWAPFILE_SIZE_GB << 30))
if [[ $SWAPFILE_SIZE == $EXPECT_SIZE ]]
then
echo "Swapfile is no need to expand."
exit 0
fi

swapoff -a
dd if=/dev/zero of=/swapfile bs=1G count=${SWAPFILE_SIZE_GB:-8}
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
grep SwapTotal /proc/meminfo

作者

codeskyblue

发布于

2023-06-05

更新于

2024-05-13

许可协议