-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaintenance.py
executable file
·424 lines (373 loc) · 12.3 KB
/
maintenance.py
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python3
"""
Routine maintenance script for GNU/Linux-based systems
"""
import argparse
import glob
import logging
import os
import shutil
import subprocess as sp
import time
import typing as tp
import misc_utils as utils
from misc_utils import print_info, run
SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
LOG_PATH = os.path.join(SCRIPT_PATH, "logs")
os.makedirs(LOG_PATH, exist_ok=True)
logging.basicConfig(
handlers=[
logging.FileHandler(os.path.join(LOG_PATH, "maintenance_{}.txt".format(time.strftime("%Y-%m-%d_%H-%M-%S"))))
],
level=logging.DEBUG,
format="%(asctime)s %(levelname)-8s %(message)s"
)
logger = logging.getLogger(__name__)
APT_WAIT_TIME: int = 1
BLEACHBIT_FEATURES: tp.List[str] = [
"adobe_reader.*",
"amsn.*",
"amule.*",
"audacious.*",
"bash.*",
"beagle.*",
"chromium.*",
# "chromium.cache",
# "chromium.cookies",
# "chromium.current_session",
# "chromium.dom",
# "chromium.form_history",
# "chromium.history",
# "chromium.passwords",
# "chromium.search_engines",
# "chromium.vacuum"
"d4x.*",
# "deepscan.backup",
# "deepscan.ds_store",
# "deepscan.thumbs_db",
# "deepscan.tmp",
"easytag.*",
"elinks.*",
"emesene.*",
"epiphany.*",
"evolution.*",
"exaile.*",
"filezilla.*",
# "firefox.*",
# "firefox.backup",
"firefox.cache",
# "firefox.cookies",
"firefox.crash_reports",
"firefox.dom",
# "firefox.download_history",
"firefox.forms",
# "firefox.passwords",
"firefox.session_restore",
"firefox.site_preferences",
"firefox.url_history",
"firefox.vacuum",
"flash.*",
"gedit.*",
"gftp.*",
"gimp.*",
"gl-117.*",
"gnome.*",
"google_chrome.*",
"google_earth.*",
"google_toolbar.*",
"gpodder.*",
"gwenview.*",
"hexchat.*",
"hippo_opensim_viewer.*",
"java.*",
"kde.*",
"konqueror.*",
"libreoffice.*",
"liferea.*",
"links2.*",
"midnightcommander.*",
"miro.*",
"nautilus.*",
"nexuiz.*",
"octave.*",
"openofficeorg.*",
"opera.*",
"pidgin.*",
"realplayer.*",
"recoll.*",
"rhythmbox.*",
"screenlets.*",
"seamonkey.*",
"secondlife_viewer.*",
"skype.*",
"sqlite3.*",
"system.cache",
"system.clipboard",
# "system.custom",
"system.desktop_entry",
# "system.free_disk_space",
# "system.localizations",
# "system.memory",
"system.recent_documents",
"system.rotated_logs",
"system.tmp",
"system.trash",
"thumbnails.*",
# "thunderbird.cache",
"thunderbird.cookies",
# "thunderbird.index",
# "thunderbird.passwords",
"thunderbird.vacuum",
# "transmission.blocklists",
"transmission.history",
# "transmission.torrents",
"tremulous.*",
"vim.*",
"vlc.*",
"vuze.*",
"warzone2100.*",
"wine.*",
"winetricks.*",
"x11.*",
"xine.*",
"yum.*"
]
BLEACHBIT_ROOT: tp.List[str] = [
"apt.*",
]
BLEACHBIT_DEEP: tp.List[str] = [
"deepscan.backup",
"deepscan.ds_store",
"deepscan.thumbs_db",
"deepscan.tmp"
]
BLEACHBIT_FIREFOX: tp.List[str] = [
"firefox.*"
]
BLEACHBIT_THUNDERBIRD: tp.List[str] = [
"thunderbird.cache",
"thunderbird.index",
]
def apt() -> None:
"""This does not print output properly while the process is being run,
which makes it impossible to answer prompts.
"""
print_info("Running apt")
run(["apt-get", "update"], sudo=True)
# run(["apt-get", "autoremove", "-y"])
while True:
apt_ret = run(["apt-get", "dist-upgrade", "-y"], check=False, sudo=True)
if apt_ret == 0:
break
if apt_ret != 100:
error_text = f"Got unknown APT return code: {apt_ret}"
logger.error(error_text)
raise ValueError(error_text)
print_info("Waiting for APT lock to be freed")
time.sleep(APT_WAIT_TIME)
# run(["apt-get", "upgrade", "-y"])
run(["apt-get", "autoremove", "-y"], sudo=True)
run(["apt-get", "autoclean", "-y"], sudo=True)
def bleachbit(deep: bool = False, firefox: bool = False, thunderbird: bool = False) -> None:
if not os.path.exists("/usr/bin/bleachbit"):
print_info("Bleachbit not found")
return
print_info("Running Bleachbit")
args = ["bleachbit", "--clean"] + BLEACHBIT_FEATURES
if deep:
print_info("Using deep scan. This will take a while.")
args += BLEACHBIT_DEEP
if firefox:
args += BLEACHBIT_FIREFOX
if thunderbird:
args += BLEACHBIT_THUNDERBIRD
# Bleachbit does not run with the run() function for some reason.
# Run both as root and as the current user.
if os.geteuid() != 0:
args2 = ["sudo", *args, *BLEACHBIT_ROOT]
sp.run(args2, check=True)
else:
args += BLEACHBIT_ROOT
sp.run(args, check=True)
def docker(all_unused_images: bool = False) -> None:
if not os.path.exists("/usr/bin/docker"):
print_info("Docker not found")
return
print_info("Pruning Docker")
args = ["docker", "system", "prune", "-f"]
if all_unused_images:
args.append("-a")
run(args)
def flatpak() -> None:
if os.path.exists("/usr/bin/flatpak"):
print_info("Removing unused Flatpak apps (so that unnecessary apps are not updated)")
run(["flatpak", "remove", "--unused"])
print_info("Updating Flatpak apps")
# Flatpak may return a non-zero exit code even when it's capable of installing the updates.
run(["flatpak", "update"], check=False)
print_info("Removing unused Flatpak apps (if some apps have become unused due to the update)")
# Especially different Nvidia driver versions come as separate packages, and upgrades may leave old
# versions dangling on the system.
run(["flatpak", "remove", "--unused"])
else:
print_info("flatpak not found")
def fwupdmgr() -> None:
if not os.path.exists("/usr/bin/fwupdmgr"):
print_info("fwupdmgr is not installed")
return
print_info("Checking for firmware updates")
ret = run(["fwupdmgr", "refresh"], check=False)
# Return code 2 = no updates available
if ret > 0 and ret != 2:
print_info(f"Got return code {ret}. Is this an error?")
ret = run(["fwupdmgr", "get-updates"], check=False)
if ret > 0 and ret != 2:
print_info(f"Got return code {ret}. Is this an error?")
def get_zerofree_status(args: argparse.Namespace) -> bool:
if utils.is_virtual():
if args.zerofree:
print("Virtual machine detected. Enabling zeroing of free space as requested.")
return True
print("This seems to be a virtual machine. Do you want to zero free space?")
print("THIS IS FOR VIRTUALBOX GUESTS ONLY")
return utils.yes_or_no()
if args.zerofree:
print("This does not seem to be a virtual machine. Are you sure you want to zero free space regardless?")
print("THIS IS FOR VIRTUALBOX GUESTS ONLY")
return utils.yes_or_no()
return False
def mdadm() -> None:
"""mdadm scrubbing
https://wiki.archlinux.org/title/RAID#Scrubbing
"""
if os.path.exists("/sbin/mdadm"):
print_info(
"Starting mdadm scrubbing. "
"You can monitor the progress with \"cat /proc/mdstat\". "
"Rebooting will restart the scrubbing from the beginning.")
endpoints = glob.glob(f"/sys/block/md*/md/sync_action")
for path in endpoints:
with open(path, "w") as endpoint:
endpoint.write("check")
with open("/proc/mdstat") as file:
print(file.read())
def remove_custom_files() -> None:
print("Removing custom files")
home = os.path.join("/home", utils.get_user())
home_files = [
"client_state.xml",
"coproc_info.xml",
"lockfile",
"stderrgpudetect.txt",
"stdoutgpudetect.txt",
"time_stats_log"
]
for file in home_files:
full_path = os.path.join(home, file)
if os.path.exists(full_path):
print("Removing \"{full_path}\"")
os.remove(full_path)
def security() -> None:
if os.path.exists("/usr/bin/freshclam"):
print_info("Running freshclam")
freshclam_ret = run(["freshclam", "-d"], check=False, sudo=True)
if freshclam_ret in [2, 62]:
print_info("Freshclam is already running")
elif freshclam_ret != 0:
raise ValueError(f"Got unknown freshclam return code: {freshclam_ret}")
else:
print_info("freshclam not found")
if os.path.exists("/usr/sbin/chkrootkit"):
print_info("Running chkrootkit")
run(["chkrootkit", "-q"], sudo=True)
else:
print_info("chkrootkit not found")
if os.path.exists("/usr/bin/rkhunter"):
print_info("Running rkhunter")
run(["rkhunter", "--update", "-q"], check=False, sudo=True)
run(["rkhunter", "-c", "-q"], check=False, sudo=True)
def snap() -> None:
if os.path.exists("/usr/bin/snap"):
print_info("Updating snaps")
run(["snap", "refresh"], sudo=True)
else:
print_info("snap not found")
def trim() -> None:
if os.path.exists("/sbin/fstrim"):
print_info("Running fstrim")
run(["fstrim", "-a", "-v"], sudo=True)
# run(["fstrim", "/", "-v"])
else:
print_info("fstrim not found")
def virtualbox_host() -> None:
"""This does not work, as it does not capture output."""
if os.path.exists("/usr/bin/virtualbox"):
print_info("Running VirtualBox maintenance script.")
run([os.path.join(SCRIPT_PATH, "virtualbox", "vbox_host_maintenance.sh")])
else:
print_info("VirtualBox not found")
def zerofree() -> None:
print_info("Zeroing free disk space on /")
# The directory /var/tmp is used instead of /tmp, as the latter may be on a ramdisk or a separate partition.
run(["dd", "if=/dev/zero", "of=/var/tmp/bigemptyfile", "bs=4096k", "status=progress"], sudo=True)
print_info("Removing temporary file.")
run(["rm", "/var/tmp/bigemptyfile"], sudo=True)
print_info("Zeroing ready.")
def zgen() -> None:
if shutil.which("zgen") is not None:
print_info("Running zgen")
run(["zgen"])
else:
print_info("zgen not found.")
def main():
parser = argparse.ArgumentParser(description="Maintenance script")
parser.add_argument("--bleachbit-only", help="Run only Bleachbit", action="store_true")
parser.add_argument("--deep", help="Deep-clean all", action="store_true")
parser.add_argument("--docker", help="Deep-clean Docker", action="store_true")
parser.add_argument("--firefox", help="Deep-clean Firefox", action="store_true")
parser.add_argument("--reboot", help="Reboot once the script is ready", action="store_true")
parser.add_argument("--shutdown", help="Shutdown once the script is ready", action="store_true")
parser.add_argument("--thunderbird", help="Deep-clean Thunderbird", action="store_true")
parser.add_argument("--virtualbox", help="Optimize VirtualBox disk images on the host", action="store_true")
parser.add_argument("--zerofree", help="Zero free disk space", action="store_true")
args = parser.parse_args()
logger.info("Args: %s", args)
if args.reboot and args.shutdown:
raise ValueError("Both reboot and shutdown cannot be selected simultaneously.")
if args.deep:
print_info("Deep scan has been selected. Some processes may take a long time.")
if args.virtualbox:
raise NotImplementedError("VirtualBox support does not work yet.")
if args.bleachbit_only:
bleachbit(deep=args.deep, firefox=(args.deep or args.firefox), thunderbird=(args.deep or args.thunderbird))
return
zero = get_zerofree_status(args)
print_info("Running maintenance script")
# apt()
# print()
snap()
print()
flatpak()
print()
zgen()
print()
security()
print()
docker(args.deep or args.docker)
print()
bleachbit(deep=args.deep, firefox=(args.deep or args.firefox), thunderbird=(args.deep or args.thunderbird))
print()
remove_custom_files()
print()
trim()
print()
fwupdmgr()
if zero:
zerofree()
mdadm()
if args.reboot:
run(["reboot", "now"])
if args.shutdown:
run(["shutdown", "now"])
if __name__ == "__main__":
main()