-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-linux-kernels.sh
More file actions
executable file
·87 lines (72 loc) · 2.14 KB
/
cleanup-linux-kernels.sh
File metadata and controls
executable file
·87 lines (72 loc) · 2.14 KB
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
#!/bin/bash
DRY_RUN=false
if [ "$1" = "--dry-run" ]; then
DRY_RUN=true
fi
if [ "$DRY_RUN" = false ]; then
if [ "$(id -u)" -ne 0 ]; then
echo "This script requires sudo privileges."
echo "Please run the script as root or use sudo."
exit 1
fi
fi
echo "## Delete Linux Kernels ##"
echo ""
CURRENT_KERNEL=$(uname -r)
KERNELS=($(dpkg --list | grep linux-image | awk '{print $2}' | grep -v "$CURRENT_KERNEL" | sort -Vr))
if [ ${#KERNELS[@]} -eq 0 ]; then
echo "No additional kernels found to remove."
exit 0
fi
echo "The following kernels were found:"
for kernel in "${KERNELS[@]}"; do
echo "$kernel"
done
echo "---"
if [ ${#KERNELS[@]} -le 3 ]; then
echo "Only ${#KERNELS[@]} kernels are installed. Nothing will be deleted."
exit 0
fi
for (( i=3; i<${#KERNELS[@]}; i++ )); do
KERNEL_TO_REMOVE="${KERNELS[$i]}"
if [ "$DRY_RUN" = true ]; then
echo "[DRY RUN] Would delete kernel: $KERNEL_TO_REMOVE"
else
echo "Deleting kernel: $KERNEL_TO_REMOVE"
if ! apt purge -y "$KERNEL_TO_REMOVE"; then
echo "Failed to remove kernel: $KERNEL_TO_REMOVE"
exit 1
fi
fi
HEADERS_TO_REMOVE=$(dpkg --list | grep linux-headers | awk '{print $2}' | grep "${KERNEL_TO_REMOVE#linux-image-}")
if [ -n "$HEADERS_TO_REMOVE" ]; then
if [ "$DRY_RUN" = true ]; then
echo "[DRY RUN] Would delete headers: $HEADERS_TO_REMOVE"
else
echo "Deleting headers: $HEADERS_TO_REMOVE"
if ! apt purge -y "$HEADERS_TO_REMOVE"; then
echo "Failed to remove headers: $HEADERS_TO_REMOVE"
exit 1
fi
fi
fi
done
echo "---"
if [ "$DRY_RUN" = false ]; then
echo "Updating GRUB..."
if ! update-grub; then
echo "Failed to update GRUB"
exit 1
fi
echo "Cleaning up unnecessary packages..."
if ! apt autoremove -y; then
echo "Failed to clean up unnecessary packages"
exit 1
fi
fi
echo ""
if [ "$DRY_RUN" = true ]; then
echo "Dry run complete. No changes were made."
else
echo "Done. Please reboot the system to apply the changes."
fi