-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_swap.sh
78 lines (66 loc) · 1.81 KB
/
build_swap.sh
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
# by https://github.com/oneclickvirt/convoypanel-scripts
#./build_swap.sh Memory size (in MB)
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8")
if [[ -z "$utf8_locale" ]]; then
echo "No UTF-8 locale found"
else
export LC_ALL="$utf8_locale"
export LANG="$utf8_locale"
export LANGUAGE="$utf8_locale"
echo "Locale set to $utf8_locale"
fi
swapsize="$1"
Green="\033[32m"
Font="\033[0m"
Red="\033[31m"
#root权限
root_need(){
if [[ $EUID -ne 0 ]]; then
echo -e "${Red}Error:This script must be run as root!${Font}"
exit 1
fi
}
ovz_no(){
if [[ -d "/proc/vz" ]]; then
echo -e "${Red}Your VPS is based on OpenVZ,not supported!${Font}"
exit 1
fi
}
add_swap(){
grep -q "swapfile" /etc/fstab
if [ $? -ne 0 ]; then
echo -e "${Green}swapfile not found, swapfile being created for it${Font}"
fallocate -l ${swapsize}M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap defaults 0 0' >> /etc/fstab
echo -e "${Green}The swap was created successfully and the following information was viewed:${Font}"
cat /proc/swaps
cat /proc/meminfo | grep Swap
else
echo -e "${Red}swapfile already exists, swap setting failed, please run the script to delete swap first and then set it again!${Font}"
fi
}
del_swap(){
grep -q "swapfile" /etc/fstab
if [ $? -eq 0 ]; then
echo -e "${Green}swapfile has been found and is in the process of removing it...${Font}"
sed -i '/swapfile/d' /etc/fstab
echo "3" > /proc/sys/vm/drop_caches
swapoff -a
rm -f /swapfile
echo -e "${Green}swap is deleted!${Font}"
else
echo -e "${Red}swapfile not found,swap delete failed!${Font}"
fi
}
main(){
root_need
ovz_no
del_swap
sleep 1
add_swap
}
main