-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap
executable file
·135 lines (120 loc) · 2.8 KB
/
swap
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
function echo() {
command echo -e "${@}"
}
function help_menu() {
echo
echo "${BOLD}SWAP MANAGER${RST}"
echo
echo "${BOLD}USAGE:${RST} ${0} <options>"
echo
echo "${BOLD}EXAMPLE:${RST} ${0} -a"
echo
echo "${BOLD}EXAMPLE:${RST} ${0} --add"
echo
echo " -a | --add: Add Swap"
echo " -c | --check: Check swap"
echo " -d | --disable: Disable swap"
echo " -e | --enable: Enable swap"
echo " -h | --help: Help menu"
echo " -r | --replace: Replace current swap"
echo
}
function setup_variables() {
BOLD="\033[1m"
RST="\033[0m"
}
function enable() {
echo
sudo swapon /swapfile
}
function disable() {
echo
sudo swapoff -a
}
function diuripna() {
sudo chmod 600 /swapfile
sudo mkswap /swapfile
enable
}
function overwrite_cust_swap() {
if [[ -z $SIZE ]]; then
echo
echo "overwrite swap to 1G"
sudo fallocate -l 1G /swapfile
else
echo
echo "overwrite swap to ${SIZE}G"
sudo fallocate -l "${SIZE}"G /swapfile
fi
}
function custom_swap() {
read -r -p "Do you want change swap size? [y/N]" PROMPT
if [[ $PROMPT =~ [yY](es)* ]]; then
echo "Enter swap size : "
read -r SIZE
fi
}
function addswap() {
custom_swap
if ! grep -q "swapfile" /etc/fstab; then
echo "swapfile not found. Adding swapfile."
if [[ -z $SIZE ]]; then
echo
echo "Add 1G swap"
sudo fallocate -l 1G /swapfile
else
echo
echo "Add ${SIZE}G swap"
sudo fallocate -l "${SIZE}"G /swapfile
fi
diuripna
sudo cp /etc/fstab /etc/fstab.bak
echo "/swapfile none swap sw 0 0" >> /etc/fstab
tuning_swap
else
overwrite_swap
fi
}
function overwrite_swap() {
read -r -p "do you want replace current swap? [y/N]" PROMPT
if [[ $PROMPT =~ [yY](es)* ]]; then
overwrite_cust_swap
diuripna
fi
}
function replace_swap() {
read -r -p "do you want replace current swap? [y/N]" PROMPT
if [[ $PROMPT =~ [yY](es)* ]]; then
custom_swap
disable
overwrite_cust_swap
diuripna
fi
}
function result() {
echo
< /proc/meminfo grep Swap
echo
}
function tuning_swap() {
echo
echo "tuning swap"
sudo sysctl vm.swappiness=10
echo "vm.swappiness=10" >> /etc/sysctl.conf
sudo sysctl vm.overcommit_memory=1
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
echo
}
function parameters() {
case "${1}" in
"-a"|"--add") addswap ;;
"-c"|"--check") result ;;
"-d"|"--disable") disable ;;
"-e"|"--enable") enable ;;
"-r"|"--replace") replace_swap ;;
"-h"|"--help"|*) help_menu ;;
esac
}
setup_variables
parameters "${@}"