-
Notifications
You must be signed in to change notification settings - Fork 5
/
cleanup_gentoo.sh
executable file
·224 lines (205 loc) · 7.36 KB
/
cleanup_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
#!/usr/bin/env sh
# Written by: Daulton
# Website: https://daulton.ca
# Repository: https://github.com/jeekkd
#
# Purpose: To be a simple script to run after uninstalling packages to clean the dependencies or wanting to
# clean portages left over ebuilds from previous emerges. There is a couple extra handy things like emptying
# the trash, cleaning /var/tmp and /tmp, checks the system for compliance with Gentoo Linux Security
# Advisories and so forth.
user=$(who am i | awk '{print $1}')
echo "* Cleaning /var/tmp, /tmp, etc..."
rm -rf /var/tmp/portage/*
rm -rf /var/tmp/ccache/*
rm -rf /var/tmp/binpkgs/*
rm -rf /var/tmp/genkernel/*
rm -rf /tmp/*
rm -rf /var/cache/genkernel/*
echo
echo "* Clean contents of /usr/portage/distfiles? Y/N"
echo "Source code archives and distribution files for older versions of programs are not automatically removed when a new version is emerged. "
read -r cleanDistfiles
if [ "$cleanDistfiles" = "Y" ] || [ "$cleanDistfiles" = "y" ]; then
rm -rf /usr/portage/distfiles/*
fi
echo
echo "* Remove contents of /usr/portage/packages? Y/N"
echo "As with distribution files, binary packages are not automatically removed. "
read -r cleanPackages
if [ "$cleanPackages" = "Y" ] || [ "$cleanPackages" = "y" ]; then
rm -rf /usr/portage/packages/*
fi
echo
echo "Enter kernel module cleaning menu? Y/N"
echo "Module files installed after kernel compilation are not tracked by the package manager an thus are not deleted after being unmerged. "
read -r cleanModules
if [ "$cleanModules" = "Y" ] || [ "$cleanModules" = "y" ]; then
echo "Select each entry by the its number to delete it then when done type exit to exit"
echo
for (( ; ; )); do
ls -l /lib64/modules/ | sed 1d | cat -n
deleteConfirmation=
read -r moduleSelection
if [ "$moduleSelection" = "exit" ] || [ "$moduleSelection" = "Exit" ]; then
echo "Exiting now"
break
fi
moduleToDelete=$(ls /lib64/modules/ | sed -n $moduleSelection\p)
echo
echo "Are you sure you want to delete the modules for $moduleToDelete? Y/N or exit"
read -r deleteConfirmation
if [ "$deleteConfirmation" = "Y" ] || [ "$deleteConfirmation" = "y" ]; then
echo "* Deleting modules.. please wait"
rm -rf /lib64/modules/"$moduleToDelete"/
if [ $? -eq 0 ]; then
echo "Deletion complete.."
echo
echo "Select each entry to delete then when done type exit at any time to exit"
echo
fi
elif [ "$deleteConfirmation" = "N" ] || [ "$deleteConfirmation" = "n" ]; then
echo "Returning back.."
elif [ "$deleteConfirmation" = "exit" ] || [ "$deleteConfirmation" = "Exit" ]; then
echo "Exiting now"
break
else
echo "Exiting now"
break
fi
done
fi
echo
echo "Enter kernel sources cleaning menu? Y/N"
echo "As with module files, kernel object files are not removed by the package manager. "
read -r cleanKernels
if [ "$cleanKernels" = "Y" ] || [ "$cleanKernels" = "y" ]; then
echo "Select each entry to delete then when done type exit at any time to exit"
echo
for (( ; ; )); do
ls -l --hide=linux /usr/src/ | sed 1d | cat -n
deleteConfirmation=
read -r kernelSelection
if [ $kernelSelection = "exit" ] || [ $kernelSelection = "Exit" ]; then
echo "Exiting now"
break
fi
kernelToDelete=$(ls -l --hide=linux /usr/src/ | sed 1d | awk '/linux/{print $9}' | sed -n $kernelSelection\p)
echo
echo "Are you sure you want to delete the kernel $kernelToDelete? Y/N or exit"
read -r deleteConfirmation
if [ "$deleteConfirmation" = "Y" ] || [ "$deleteConfirmation" = "y" ]; then
rm -rf /usr/src/"$kernelToDelete"/
if [ $? -eq 0 ]; then
echo "Deletion complete.."
echo
echo "Select each entry to delete then when done type exit at any time to exit"
echo
fi
elif [ "$deleteConfirmation" = "N" ] || [ "$deleteConfirmation" = "n" ]; then
echo "Returning back.."
elif [ "$deleteConfirmation" = "exit" ] || [ "$deleteConfirmation" = "Exit" ]; then
echo "Exiting now"
break
else
echo "Exiting now"
break
fi
done
fi
echo
echo "Enter /boot kernel cleaning menu? Y/N"
echo "Removing old kernels that have accumulated can save space."
read -r cleanBootKernels
if [ "$cleanBootKernels" = "Y" ] || [ "$cleanBootKernels" = "y" ]; then
echo "Select each entry to delete then when done type exit at any time to exit"
echo
for (( ; ; )); do
ls -l --hide=config* --hide=initramfs* --hide=System.map* --hide=.keep --hide=grub* /boot | sed 1d | cat -n
deleteConfirmation=
read -r bootKernelSelection
if [ $bootKernelSelection = "exit" ] || [ $bootKernelSelection = "Exit" ]; then
echo "Exiting now"
break
fi
kernelToDelete=$(ls -l --hide=config* --hide=initramfs* --hide=System.map* --hide=.keep --hide=grub* /boot | awk '{print $9}' | sed 1d | sed -n $bootKernelSelection\p)
echo
echo "Are you sure you want to delete the kernel $kernelToDelete? Y/N or exit"
read -r deleteConfirmation
if [ "$deleteConfirmation" = "Y" ] || [ "$deleteConfirmation" = "y" ]; then
rm /boot/$kernelToDelete
if [ $? -eq 0 ]; then
echo "Deletion complete.."
echo
echo "Select each entry to delete then when done type exit at any time to exit"
echo
fi
elif [ "$deleteConfirmation" = "N" ] || [ "$deleteConfirmation" = "n" ]; then
echo "Returning back.."
elif [ "$deleteConfirmation" = "exit" ] || [ "$deleteConfirmation" = "Exit" ]; then
echo "Exiting now"
break
else
echo "Exiting now"
break
fi
done
echo
echo "Note that only kernels are deleted, associated initramfs, System.map, config will need to be manually deleted."
fi
echo
echo "Clean firefox and chromium browser caches? Y/N"
read -r browserClean
if [ "$browserClean" = "Y" ] || [ "$browserClean" = "y" ]; then
echo "* Cleaning browser and other caches..."
rm -rf /home/$user/.cache/chromium/Default/*
rm /home/$user/.mozilla/firefox/*.default/*.sqlite /home/$user/.mozilla/firefox/*default/sessionstore.js
rm -rf /home/$user/.cache/mozilla/firefox/*.default/*
echo
fi
echo "* Cleaning unused libraries and programs..."
emerge -av --depclean
emerge -cav
echo
echo "* Cleaning out the trash..."
rm -rf /home/*/.local/share/Trash/*/** &> /dev/null
rm -rf /root/.local/share/Trash/*/** &> /dev/null
echo
echo "* Checking system for compliance with Gentoo Linux Security Advisories"
good_result="This system is not affected by any of the listed GLSAs"
glsa_check_result=$(glsa-check -t all)
if [ "$(diff -q $glsa_check_result $good_result 2>&1)" = "" ]; then
glsa-check -f all
fi
echo
echo "* Removing locale files"
find /usr/share/locale/* -type d | xargs rm -rf
echo
echo "* Removing unused packages"
eclean -d packages
echo
echo "* Checking for packages with changed use flags"
emerge --ask --update --changed-use --deep @world
echo
echo "* Disabling python bytecode generation"
echo "PYTHONDONTWRITEBYTECODE=1" > /etc/env.d/99python
env-update && source /etc/profile
isInstalledRotate=$(equery list "*" | grep logrotate)
if [ "$isInstalledRotate" = *[!\ ]* ]; then
echo "* Running a forced logrotate and cleaning logs"
logrotate --force /etc/logrotate.conf
find /var/log/ -name '*[0-5]*' -exec rm {} \;
else
echo "* Would you like to install logrotate to keep your logs clean? YES/NO"
read -r logrotate_answer
if [ "$logrotate_answer" = "YES" ] || [ "$logrotate_answer" = "yes" ]; then
emerge app-admin/logrotate
fi
fi
echo
echo "* Checking for obselete packages..."
eix-test-obsolete
echo
echo "* Finishing up with checking for and rebuild missing libraries..."
revdep-rebuild -v
echo
echo "* Complete!"