-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaintain_gentoo.sh
271 lines (196 loc) · 8.74 KB
/
maintain_gentoo.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
#!/bin/bash
[ "${UID}" -eq "0" ] || {
echo "This script must be run as root" >&2
exit "1"
}
set -Eeo pipefail
GREEN='\e[1;92m' RED='\e[1;91m' BLUE='\e[1;94m'
PURPLE='\e[1;95m' YELLOW='\e[1;93m' NC='\033[0m'
CYAN='\e[1;96m' WHITE='\e[1;97m'
handle_error() {
error_status="${?}"
command_line="${BASH_COMMAND}"
error_line="${BASH_LINENO[0]}"
log_info r "Error on line ${BLUE}${error_line}${RED}: command ${BLUE}'${command_line}'${RED} exited with status: ${BLUE}${error_status}"
}
trap 'handle_error' ERR RETURN INT
log_info() {
sleep "0.3"
case "${1}" in
g) COLOR="${GREEN}" MESSAGE="DONE!" ;;
r) COLOR="${RED}" MESSAGE="WARNING!" ;;
b) COLOR="${BLUE}" MESSAGE="STARTING." ;;
c) COLOR="${BLUE}" MESSAGE="RUNNING." ;;
esac
COLORED_TASK_INFO="${WHITE}(${CYAN}${TASK_NUMBER}${PURPLE}/${CYAN}${TOTAL_TASKS}${WHITE})"
MESSAGE_WITHOUT_TASK_NUMBER="${2}"
DATE="$(date "+%Y-%m-%d ${CYAN}/${PURPLE} %H:%M:%S")"
FULL_LOG="${CYAN}[${PURPLE}${DATE}${CYAN}] ${YELLOW}>>>${COLOR}${MESSAGE}${YELLOW}<<< ${COLORED_TASK_INFO} - ${COLOR}${MESSAGE_WITHOUT_TASK_NUMBER}${NC}"
{ [[ ${1} == "c" ]] && echo -e "\n\n${FULL_LOG}"; } || echo -e "${FULL_LOG}"
}
USER="$(getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' | head -n "1")"
KERNEL_PATH="/boot/EFI/BOOT/BOOTX64.EFI"
NEW_KERNEL="/usr/src/linux/arch/x86/boot/bzImage"
MODULES_DIR="/lib/modules"
CACHE_DIR="/var/cache"
USER_CACHE_DIR="/home/${USER}/.cache"
LOG_DIR="/var/log"
VAR_TMP_DIR="/var/tmp"
FAILED_LOG_DIR="/home/${USER}/failed_builds"
renew_env() {
env-update && source "/etc/profile"
}
sync_repos() {
emaint sync -a > "/dev/null" 2>&1
}
update_world() {
emerge --update "@world" 2>&1 || true
}
update_live() {
emerge "@live-rebuild" 2>&1 || true
emerge @preserved-rebuild || true
}
remove_unneeded() {
CLEAN_DELAY="0" emerge --depclean -q --verbose=n > "/dev/null" 2>&1 || true
}
create_logs() {
rm -rfv "${FAILED_LOG_DIR}"
mkdir -pv "${FAILED_LOG_DIR}"
}
update_kernel() {
DIR_COUNT="$(find /usr/src/ -maxdepth "1" -type d -name 'linux-*' | wc -l)"
[ "${DIR_COUNT}" -gt "1" ] || {
echo -e "${BLUE}There is only one linux directory. The function stops...${NC}"
KERNEL_IS_UPDATED="0"
return
}
KERNEL_DIR="$(find "/usr/src" -maxdepth "1" -type "d" -name 'linux-*' | sort -rV | head -n "1")"
echo -e "${GREEN}New kernel directory:${CYAN} ${KERNEL_DIR} ${NC}"
OLD_KERNEL_DIR="$(find "/usr/src" -maxdepth "1" -type "d" -name 'linux-*' | sort -rV | head -n "2" | tail -n "1")"
echo -e "${GREEN}Old kernel directory:${CYAN} ${OLD_KERNEL_DIR} ${NC}"
export LLVM="1" LLVM_IAS="1" CFLAGS="-O3 -march=native -pipe" KCFLAGS="-O3 -march=native -pipe"
cp -fv "${OLD_KERNEL_DIR}/.config" "${KERNEL_DIR}"
make -C "${KERNEL_DIR}" "olddefconfig" > "/dev/null" 2>&1
make -C "${KERNEL_DIR}" -j"$(nproc)" > "/dev/null" 2>&1
make -C "${KERNEL_DIR}" "modules_install" > "/dev/null" 2>&1
cp -fv "${NEW_KERNEL}" "${KERNEL_PATH}"
echo -e "${GREEN}New kernel${CYAN} ${NEW_KERNEL} ${GREEN}has been copied to${CYAN} ${KERNEL_PATH}.${NC}"
rm -rf "${OLD_KERNEL_DIR}"
echo -e "${GREEN}Old kernel directory${CYAN} ${OLD_KERNEL_DIR} ${GREEN}has been deleted.${NC}"
}
update_nvidia() {
grep -q 'VIDEO_CARDS="nvidia"' "/etc/portage/make.conf" || {
echo "Not using Nvidia GPU."
return
}
[[ "${KERNEL_IS_UPDATED}" == "0" ]] && {
echo -e "${BLUE}The kernel has not been updated. Skipping Nvidia drivers...${NC}"
return
}
emerge "x11-drivers/nvidia-drivers" > "/dev/null" 2>&1
}
copy_failed_logs() {
while IFS= read -r pkg; do
log_path="/var/tmp/portage/${pkg}/temp/build.log"
safe_pkg_name="$(echo "${pkg}" | tr '/' '_')"
[ -f "${log_path}" ] && cp "${log_path}" "${FAILED_LOG_DIR}/${safe_pkg_name}.log"
done < "${FAILED_PACKAGES}" || true
chown -R "${USER}":"${USER}" "/home/${USER}"
}
clean_old_modules() {
find "${MODULES_DIR}" -mindepth "1" -maxdepth "1" -type "d" | sort -V | head -n "-1" | xargs rm -rf
}
clean_temp_files() {
rm -rf "${CACHE_DIR:?}"/* "${LOG_DIR:?}"/* "${VAR_TMP_DIR:?}"/*
}
clean_user_cache() {
find "${USER_CACHE_DIR}" -mindepth "1" -maxdepth "1" \( -name 'wal' -o -name 'youtube_channels' \) -prune -o -print0 | xargs -0 rm -rf
}
run_fstrim() {
fstrim -Av
}
handle_shutdown() {
echo -e "${GREEN}The system will shutdown."
delay_shutdown="$(echo -e "No\nYes" | rofi -dmenu -l "2" -p "Want to delay shutdown?")"
[[ "${delay_shutdown}" == "Yes" ]] && {
while true; do
delay_amount="$(echo "" | rofi -dmenu -l "0" -p "Amount in minutes:")"
[[ "${delay_amount}" ]] || {
echo -e "Shutdown not delayed. Shutting down now."
openrc-shutdown -p "now"
break
}
[[ "${delay_amount}" =~ ^[0-9]+$ ]] || {
notify-send "Invalid input. Please enter a number."
continue
}
notify-send "Shutdown delayed by ${delay_amount} minutes."
sleep "$((delay_amount * 60))"
delay_shutdown="$(echo -e "No\nYes" | rofi -dmenu -l "2" -p "Want to delay shutdown?")"
[[ "${delay_shutdown}" == "No" ]] && {
openrc-shutdown -p "now"
break
}
done
} || openrc-shutdown -p "now"
}
main() {
declare -A "tasks"
tasks["renew_env"]="Renew the environment.
Environment renewed."
tasks["sync_repos"]="Sync the Gentoo repositories.
Gentoo repositories synced."
tasks["update_world"]="Update the world.
The world updated."
tasks["update_live"]="Update live packages.
Live packages updated."
tasks["remove_unneeded"]="Remove unneeded packages.
Unneeded packages removed."
tasks["create_logs"]="Create logs for failed packages.
Logs for failed packages created."
tasks["update_kernel"]="Update the kernel.
The kernel is ready."
tasks["update_nvidia"]="Recompile Nvidia drivers.
Nvidia drivers ready."
tasks["copy_failed_logs"]="Copy failed build logs.
Failed build logs copied."
tasks["clean_old_modules"]="Clean old kernel modules.
Old kernel modules cleaned."
tasks["clean_temp_files"]="Clean temporary files.
Temporary files cleaned."
tasks["clean_user_cache"]="Clean the user cache.
The user cache cleaned."
tasks["run_fstrim"]="Trim the filesystem.
Filesystem trimmed."
task_order=("renew_env" "sync_repos" "update_world" "update_live" "remove_unneeded"
"create_logs" "update_kernel" "update_nvidia" "copy_failed_logs"
"clean_old_modules" "clean_temp_files" "clean_user_cache" "run_fstrim")
TOTAL_TASKS="${#tasks[@]}"
TASK_NUMBER="1"
trap '[[ -n "${log_pid}" ]] && kill "${log_pid}" 2> "/dev/null"' EXIT INT QUIT TERM ERR RETURN
for function in "${task_order[@]}"; do
description="${tasks[${function}]}"
description="${description%%$'\n'*}"
done_message="$(echo "${tasks[${function}]}" | tail -n "1" | sed 's/^[[:space:]]*//g')"
log_info b "${description}"
(
sleep "120"
while true; do
log_info c "${description}"
sleep "120"
done || true
) &
log_pid="${!}"
"${function}"
log_info g "${done_message}"
[[ "${TASK_NUMBER}" == "${TOTAL_TASKS}" ]] && {
log_info g "All tasks completed."
kill "${log_pid}" 2> "/dev/null" || true
break
}
kill "${log_pid}" 2> "/dev/null" || true
((TASK_NUMBER++))
done
[[ "${1}" == "-f" ]] && openrc-shutdown -p "now" || "handle_shutdown"
}
main "${@}"