Skip to content

Instantly share code, notes, and snippets.

@changhoon-sung
Created November 3, 2024 05:29
Show Gist options
  • Save changhoon-sung/478afad462711f04f7dfc3b674e86aff to your computer and use it in GitHub Desktop.
Save changhoon-sung/478afad462711f04f7dfc3b674e86aff to your computer and use it in GitHub Desktop.
Script to easily and safely remove a specific version of Linux kernel files in /boot and its modules in /lib/modules
#!/bin/bash
usage() {
echo "Usage: $0 [--apply]"
echo "Default is dry-run mode. Use --apply to actually delete files and directories."
exit 1
}
if [ "$EUID" -ne 0 ]; then
echo "You must run this script as root."
exit 1
fi
# Dry-run as default
apply_changes=0
if [ "$1" == "--apply" ]; then
apply_changes=1
elif [ "$1" != "" ]; then
usage
fi
# Fetch current symbolic links
current_vmlinuz=$(readlink -f /boot/vmlinuz)
current_vmlinuz_old=$(readlink -f /boot/vmlinuz.old)
current_config=$(readlink -f /boot/config)
current_config_old=$(readlink -f /boot/config.old)
current_initrd=$(readlink -f /boot/initrd.img)
current_initrd_old=$(readlink -f /boot/initrd.img.old)
current_system_map=$(readlink -f /boot/System.map)
current_system_map_old=$(readlink -f /boot/System.map.old)
# Fetch kernel lists
kernels=($(ls /boot/vmlinuz-* | xargs -n 1 basename))
echo "Available kernels:"
for i in "${!kernels[@]}"; do
echo "$i) ${kernels[$i]}"
done
# Input
echo "Enter the kernel numbers to delete (space-separated):"
read -a delete_indices
delete_files=()
delete_dirs=()
for index in "${delete_indices[@]}"; do
kernel="${kernels[$index]}"
# Set related files
vmlinuz_file="/boot/$kernel"
config_file="/boot/config-${kernel#vmlinuz-}"
initrd_file="/boot/initrd.img-${kernel#vmlinuz-}"
system_map_file="/boot/System.map-${kernel#vmlinuz-}"
modules_dir="/lib/modules/${kernel#vmlinuz-}"
# For safety, do not delete if the selected kernel is symlinked to vmlinuz and initrd
if [[ "$vmlinuz_file" == "$current_vmlinuz" || "$vmlinuz_file" == "$current_vmlinuz_old" ]]; then
echo "Skipping $vmlinuz_file (linked to vmlinuz or vmlinuz.old)"
else
delete_files+=("$vmlinuz_file")
fi
if [[ "$config_file" == "$current_config" || "$config_file" == "$current_config_old" ]]; then
echo "Skipping $config_file (linked to config or config.old)"
else
delete_files+=("$config_file")
fi
if [[ "$initrd_file" == "$current_initrd" || "$initrd_file" == "$current_initrd_old" ]]; then
echo "Skipping $initrd_file (linked to initrd.img or initrd.img.old)"
else
delete_files+=("$initrd_file")
fi
if [[ "$system_map_file" == "$current_system_map" || "$system_map_file" == "$current_system_map_old" ]]; then
echo "Skipping $system_map_file (linked to System.map or System.map.old)"
else
delete_files+=("$system_map_file")
fi
if [ -d "$modules_dir" ]; then
if [[ "$vmlinuz_file" == "$current_vmlinuz" || "$vmlinuz_file" == "$current_vmlinuz_old" ]]; then
echo "Skipping $modules_dir (linked kernel in use)"
else
delete_dirs+=("$modules_dir")
fi
else
echo "Skipping $modules_dir (directory not found)"
fi
done
# Print targets
echo ""
echo "Files to delete:"
for file in "${delete_files[@]}"; do
echo "$file"
done
echo ""
echo "Directories to delete:"
for dir in "${delete_dirs[@]}"; do
echo "$dir"
done
# Apply
if [ $apply_changes -eq 1 ]; then
echo "Deleting files and directories..."
for file in "${delete_files[@]}"; do
if [ -f "$file" ]; then
sudo rm -f "$file"
echo "Deleted $file"
else
echo "File not found: $file"
fi
done
for dir in "${delete_dirs[@]}"; do
if [ -d "$dir" ]; then
sudo rm -rf "$dir"
echo "Deleted directory $dir"
else
echo "Directory not found: $dir"
fi
done
echo "Deletion completed."
sudo update-grub
else
echo ""
echo "Dry-run mode: No files or directories were deleted. Use --apply to delete them."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment