-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable_overcommit.sh
executable file
·150 lines (135 loc) · 3.56 KB
/
disable_overcommit.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/sh
#####################################################################################
## The script sets up LMA OS-wide performance prerequisites.
##
## Version 1.2
## Written by Y.Voinov (C) 2022-2023
#####################################################################################
# Sysctl config path
CONFIG_BASE="/etc"
SYSCTL_PATH="$CONFIG_BASE/sysctl.d"
SYSCTL_FILE="$CONFIG_BASE/sysctl.conf"
CONFIG_NAME="10-lma.conf"
# Write file content
SYSCTL_FILE_STR1="# System tweaks by TCW"
# Note: Don't set swappiness too low on Proxmox/KVM or create big enough swap.
SYSCTL_FILE_STR2="vm.swappiness = 30"
SYSCTL_FILE_STR3="vm.vfs_cache_pressure = 50"
SYSCTL_FILE_STR4="vm.overcommit_ratio = 99"
# Note: When run aerospike or KVM with relatively small memory footprint, set vm.overcommit_memory=1. Otherwise asd will fail to start.
SYSCTL_FILE_STR5="vm.overcommit_memory = 2"
# Existence flags
SYSCTL_STR1_EXIST=""
SYSCTL_STR2_EXIST=""
SYSCTL_STR3_EXIST=""
SYSCTL_STR4_EXIST=""
SYSCTL_STR5_EXIST=""
# Subroutines
usage_note()
{
echo "The script sets up LMA OS-wide prerequisites."
echo "Reboot is recommended, but non-required. Must be run as root."
echo "Example: `basename $0`"
exit 0
}
check_os()
{
if [ "`uname`" != "Linux" ]; then
echo "ERROR: This script is for Linux only."
exit 2
fi
}
check_root()
{
if [ -z "`id | grep 'uid=0(root)'`" ]; then
echo "ERROR: Must be run as root."
exit 3
fi
}
check_config()
{
if [ ! -z "`sysctl --system | grep "\b$SYSCTL_FILE_STR1\b"`" ]; then
echo "Value $SYSCTL_FILE_STR1 exists."
SYSCTL_STR1_EXIST="1"
fi
if [ ! -z "`sysctl --system | grep "\b$SYSCTL_FILE_STR2\b"`" ]; then
echo "Value $SYSCTL_FILE_STR2 exists."
SYSCTL_STR2_EXIST="1"
fi
if [ ! -z "`sysctl --system | grep "\b$SYSCTL_FILE_STR3\b"`" ]; then
echo "Value $SYSCTL_FILE_STR3 exists."
SYSCTL_STR3_EXIST="1"
fi
if [ ! -z "`sysctl --system | grep "\b$SYSCTL_FILE_STR4\b"`" ]; then
echo "Value $SYSCTL_FILE_STR4 exists."
SYSCTL_STR4_EXIST="1"
fi
if [ ! -z "`sysctl --system | grep "\b$SYSCTL_FILE_STR5\b"`" ]; then
echo "Value $SYSCTL_FILE_STR5 exists."
SYSCTL_STR5_EXIST="1"
fi
}
write_file()
{
file=$1
if [ -z "$SYSCTL_STR1_EXIST" ]; then
if [ -z "`grep "\b$SYSCTL_FILE_STR1\b" $file`" ]; then
echo $SYSCTL_FILE_STR1
fi
fi
if [ -z "$SYSCTL_STR2_EXIST" ]; then
if [ -z "`grep "\b$SYSCTL_FILE_STR2\b" $file`" ]; then
echo $SYSCTL_FILE_STR2
fi
fi
if [ -z "$SYSCTL_STR3_EXIST" ]; then
if [ -z "`grep "\b$SYSCTL_FILE_STR3\b" $file`" ]; then
echo $SYSCTL_FILE_STR3
fi
fi
if [ -z "$SYSCTL_STR4_EXIST" ]; then
if [ -z "`grep "\b$SYSCTL_FILE_STR4\b" $file`" ]; then
echo $SYSCTL_FILE_STR4
fi
fi
if [ -z "$SYSCTL_STR5_EXIST" ]; then
if [ -z "`grep "\b$SYSCTL_FILE_STR5\b" $file`" ]; then
echo $SYSCTL_FILE_STR5
fi
fi
}
set_config()
{
# Modern OS
if [ -d "$SYSCTL_PATH" ]; then
if [ ! -f "$SYSCTL_PATH/$CONFIG_NAME" ]; then
echo "Writing $SYSCTL_PATH/$CONFIG_NAME..."
write_file $SYSCTL_PATH/$CONFIG_NAME > $SYSCTL_PATH/$CONFIG_NAME
fi
# Old-fashiones OS
elif [ -f "$SYSCTL_FILE" ]; then
echo "Writing $SYSCTL_FILE..."
write_file $SYSCTL_PATH/$CONFIG_NAME >> $SYSCTL_FILE
fi
}
# Main
# Parse command line
if [ "x$*" != "x" ]; then
arg_list=$*
# Read arguments
for i in $arg_list
do
case $i in
-h|-H|\?) usage_note;;
*) shift
;;
esac
done
fi
check_os
check_root
check_config
set_config
sysctl --system >/dev/null 2>&1
echo "Done. Reboot recommended but non-required."
exit 0