-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.sh
executable file
·384 lines (355 loc) · 11.9 KB
/
script.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
GRY='\033[1;30m'
RED='\033[0;31m'
BLU='\033[0;34m'
GRN='\033[0;32m'
PUL='\033[0;35m'
RST='\033[0m'
# Current Linux Distribution
distro=$(grep ^ID= /etc/os-release | cut -d= -f2 | tr '[:upper:]' '[:lower:]' | sed 's/\"//g')
MEMORY_RAM=0
MEMORY_SWAP=0
loadMemory() {
MEMORY_RAM=$(cat /proc/meminfo | grep MemTotal: | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g')
# Convert to MB
MEMORY_RAM=$((${MEMORY_RAM} / 1024))
# Convert to GB
MEMORY_RAM=$((${MEMORY_RAM} / 1024))
# Get SWAP size.
MEMORY_SWAP=$(cat /proc/meminfo | grep SwapTotal: | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g')
# Convert to MB
MEMORY_SWAP=$((${MEMORY_SWAP} / 1024))
# Convert to GB
MEMORY_SWAP=$((${MEMORY_SWAP} / 1024))
}
SwapOnPartition() {
#
# print partition in use as root
# /boot (for efi usage) and print / (system)
#
DontUse=($(df -h | grep ^/dev | grep '/boot\|/$' | tr ' ' '\n' | grep /dev))
printf "${RED}Don't use this partition:\n"
echo "${DontUse[*]}"
printf "${RST}"
# print disk
DISK=$(sudo fdisk -l | grep Disk | tr ' ' '\n' | tr ':' ' ' | grep /dev)
printf "${GRY}Available disk:\n"
echo "$DISK"
printf "${RST}"
# save partition
PARTITION=$(sudo fdisk -l | awk '!/Disk/' | tr ' ' '\n' | tr ':' ' ' | grep /dev)
printf "${GRN}Available partition:\n"
COUNTER=0
for part in ${PARTITION[*]}; do
#
# Filter for not print DontUse disk.
#
if [[ ! "${DontUse[*]}" =~ "${part}" ]]; then
COUNTER=$(expr $COUNTER + 1)
echo " $COUNTER - $part"
fi
done
printf "${RST}"
read -p "Select your partition number: " part_num
COUNTER=0
for part in ${PARTITION[*]}; do
#
# Filter partition for check DontUse disk.
#
if [[ ! "${DontUse[*]}" =~ "${part}" ]]; then
COUNTER=$(expr $COUNTER + 1)
#
# Check chose number with number of partition
# N.B. : if the number not exist in list of partition nothing happen.
#
if [[ "$COUNTER" == "$part_num" ]]; then
echo "You select $part"
read -p "You are sure to continue and format this partition? [y/N]: " part_num
if [ "$part_num" == "y" ] || [ "$part_num" == "Y" ]; then
#
# I need to save UUID to put it into fstab
#
UUID=$(sudo mkswap $part | tr ' ' '\n' | grep 'UUID' | tr -d 'UUID=')
echo "Enable swap partition"
sudo swapon $part
read -p "You want put this in fstab? [y/N]: " write_fstab
if [ "$write_fstab" == "y" ] || [ "$write_fstab" == "Y" ]; then
printf "${RED}If you want remove or resize this partition\nyou need to remove this from fstab with manual way.\n${RST}"
sudo su -c "printf '\n# swap created with script' >> /etc/fstab"
sudo su -c "printf '\nUUID=$UUID\tnone\tswap\tdefaults\t0\t0\n' >> /etc/fstab"
fi
loadMemory
printf "${BLU}New swap size: $MEMORY_SWAP GB${RST}\n"
fi
break
fi
fi
done
}
#
# Read doc:
# https://wiki.archlinux.org/title/Swap#Swap_file
#
SwapOnFile() {
SWAP_FILE=/swapfile
read -p "Write size of swap (integer and in GB): " size_file
#
# if user write for example: 3 GB
# this keep only number
#
size_file=$(echo $size_file | tr -dc '0-9')
if [[ "$size_file" -lt "1" ]]; then
echo "Less than 1 GB. Abort."
exit 1
fi
#
# convert GB to MiB
# WARNING: the right size for convert is 953,674
# but bash can only multiply integers
#
size_file=$((size_file * 954))
#
# If this file already exit maybe this script
# isn't started for first time here
#
if [[ -f $SWAP_FILE ]]; then
echo "Resize..."
# If is enabled as swap, disable it
if [[ $(swapon -s | tr ' ' '\n' | grep $SWAP_FILE) ]]; then
echo "Disable swap for resize..."
sudo swapoff $SWAP_FILE
fi
# Remove for remake again
sudo rm -rf $SWAP_FILE
fi
# make swapfile
sudo dd if=/dev/zero of=$SWAP_FILE bs=1M count=$size_file status=progress
if [[ -f $SWAP_FILE ]]; then
sudo chmod 0600 $SWAP_FILE
sudo mkswap -U clear $SWAP_FILE
sudo swapon $SWAP_FILE
fi
if [[ $(cat /etc/fstab | tr '\t' '\n' | grep /swapfile | tr -d '/') ]]; then
echo "Already in fstab!"
else
read -p "You want put this in fstab? [y/N]: " write_fstab
if [ "$write_fstab" == "y" ] || [ "$write_fstab" == "Y" ]; then
sudo su -c "printf '\n# swap created with script' >> /etc/fstab"
sudo su -c "printf '\n$SWAP_FILE\tnone\tswap\tdefaults\t0\t0\n' >> /etc/fstab"
fi
fi
loadMemory
printf "${BLU}New swap size: $MEMORY_SWAP GB${RST}\n"
}
set_git_user() {
# Ask for user name and email for Github
if [ -z "$(git config --global user.name)" ] || [ -z "$(git config --global user.email)" ]; then
echo "Enter your Github name and email to configure Git:"
read -p "Name: " YOUR_NAME
read -p "Email: " YOUR_EMAIL
git config --global user.name "$YOUR_NAME"
git config --global user.email "$YOUR_EMAIL"
else
echo "Git is already configured with the following details:"
echo "Name: $(git config --global user.name)"
echo "Email: $(git config --global user.email)"
read -p "Do you want to change these details? [y/N]: " change_details
if [ "$change_details" == "y" ] || [ "$change_details" == "Y" ]; then
read -p "Enter new name: " YOUR_NAME
read -p "Enter new email: " YOUR_EMAIL
git config --global user.name "$YOUR_NAME"
git config --global user.email "$YOUR_EMAIL"
fi
fi
}
script_install() {
# Cloning of akhilnarang Scripts in '/home/$USER/scripts' and execution
SCRIPT_DIR=~/scripts
if [[ ! -f ~/scripts/setup/android_build_env.sh ]]; then
git clone https://github.com/akhilnarang/scripts --depth=1 $SCRIPT_DIR
#
# Abort the script if it failed to clone.
# Problems with github? Problems with the user's internet?
# What do I know?
#
if [ $? -ne 0 ]; then
echo "Failled to clone akhilnarang/scripts in $SCRIPT_DIR"
exit 1
fi
fi
cd $SCRIPT_DIR
# Installation of necessary packages
case "$distro" in
"ubuntu" | "debian" | "linuxmint" | "kali")
./setup/android_build_env.sh
;;
"arch" | "manjaro" | "arcolinux" | "garuda" | "artix" | "cachyos")
./setup/arch-manjaro.sh
;;
"solus")
./setup/solus.sh
;;
"fedora" | "centos")
./setup/fedora.sh
;;
*)
echo "Distribution Not Supported"
exit 1
;;
esac
}
ccache_settings() {
# Check if ccache config is already present in .bashrc
if ! grep -q 'Generated ccache config' "$HOME/.bashrc"; then
echo "Configuring ccache..."
# Set the ccache directory
export CCACHE_DIR=~/.ccache
# Set the path to ccache executable
export CCACHE_EXEC=$(which ccache)
# Enable ccache
export USE_CCACHE=1
# Set cache size to 50 GB
ccache -M 50G >/dev/null
# Enable compression
ccache -o compression=true >/dev/null
# Zero statistics
ccache -z >/dev/null
# Append ccache config to .bashrc
cat <<EOF >>"$HOME/.bashrc"
# Generated ccache config
export USE_CCACHE=1
export CCACHE_EXEC=$CCACHE_EXEC
export CCACHE_DIR=$CCACHE_DIR
EOF
fi
}
#
# True only if RAM is less than 16 GB and SWAP is less than 30 GB
#
AddSwap() {
if [ $MEMORY_RAM -lt 16 ] && [ $MEMORY_SWAP -lt 30 ]; then
echo "Your ram is less than 16 GB ($MEMORY_RAM GB) and your current swap is $MEMORY_SWAP GB"
read -p "Do you want to add a swap partition? [y/N]: " enable_swap
if [ "$enable_swap" == "y" ] || [ "$enable_swap" == "Y" ]; then
echo "1) Swap on disk partition"
echo "2) Swap on file"
echo "0 (or other) exit"
read -p "Chose [1-2]: " enable_swap
if [[ "$enable_swap" == "1" ]]; then
printf "${RED}WARNING: This script will FORMAT all the partition selected as SWAP."
printf "\nIf you are not sure what do selection default N${RST}\n"
read -p "Do you want continue? [y/N]" ImSure
if [ "$ImSure" == "y" ] || [ "$ImSure" == "Y" ]; then
SwapOnPartition
else
echo "Abort."
fi
elif [[ "$enable_swap" == "2" ]]; then
SwapOnFile
fi
fi
fi
}
#
# Increase swap ratio if your machine is less than 16 GB of RAM
#
SwapRatio() {
if [[ $MEMORY_RAM -lt 16 ]]; then
if free | awk '/^Swap:/ {exit !$2}'; then
SWAPPINESS="/proc/sys/vm/swappiness"
if [ -f $SWAPPINESS ]; then
#
# 200 is max for linux 5.10 or over.
# 100 is max for linux 5.4 or before.
#
VALUE="100" # 100 by default
#
# put VERSION - PATCHLEVEL - SUBLEVEL in array
# but we need to check only version and pathlevel (array index 0 - 1)
#
LINUX_VERSION="$(uname -r | tr '-' '\n' | head -n 1)"
LINUX_VERSION=($(echo $LINUX_VERSION | tr '.' '\n'))
# check if we can't increase max swap percent over 100
if [[ "5" -le "${LINUX_VERSION[0]}" ]]; then
if [[ "5" -ne ${LINUX_VERSION[0]} ]]; then
# here only if is 6.x.y kernel
echo "Your kernel is over 6.x.y"
VALUE="200"
else
if [[ "10" -le ${LINUX_VERSION[1]} ]]; then
# here if is over or equal 5.10.y kernel
echo "Your kernel is over 5.10.y"
VALUE="200"
fi
fi
else
echo "You have old kernel."
fi
if [[ $(cat $SWAPPINESS) -ne $VALUE ]]; then
echo "Increase the interaction with the swap from $(cat $SWAPPINESS) to $VALUE..."
sudo su -c "echo $VALUE > $SWAPPINESS"
echo " "
fi
fi
fi
fi
}
case $1 in
"--mem")
loadMemory
echo "RAM : $MEMORY_RAM GB"
echo "Swap : $MEMORY_SWAP GB"
;;
"--git")
set_git_user
;;
"--setup")
script_install
;;
"--ccache")
ccache_settings
;;
"--swap")
loadMemory
SwapRatio
AddSwap
;;
"-h" | "--help")
echo "Available option:"
printf "\t--mem\t\tTo show memory information\n"
printf "\t--git\t\tTo configure git account\n"
printf "\t--setup\t\tTo install akhilnarang Scripts\n"
printf "\t--ccache\tTo configure ccache\n"
printf "\t--swap\t\tTo add swap and increase swap ratio\n"
echo " "
echo "Don't use args for start full script"
;;
*)
#
# Get RAM size.
# If RAM < 16 GB optimize swap usage.
# Check also for already swap partition.
#
loadMemory
#
# git config
#
set_git_user
#
# Install akhilnarang Scripts
#
script_install
#
# Default configuration of ccache in bashrc
#
ccache_settings
#
# Chose if u want add swap with partition or with file.
#
AddSwap
#
# Increase swap usage from /proc/sys/vm/swappiness
#
SwapRatio
;;
esac