Last active
January 25, 2025 05:26
-
-
Save changhoon-sung/8d0faa3b8b23cc41a835ec12d55d97e4 to your computer and use it in GitHub Desktop.
Script to easily reset symbolic links for vmlinuz and initrd in the /boot directory on Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage() { | |
echo "Usage: $0 [--apply]" | |
echo "Default is dry-run mode. Use --apply to actually update symlinks." | |
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 | |
echo "Current symlink values:" | |
echo " vmlinuz -> $(readlink -f /boot/vmlinuz)" | |
echo " vmlinuz.old -> $(readlink -f /boot/vmlinuz.old)" | |
echo " initrd.img -> $(readlink -f /boot/initrd.img)" | |
echo " initrd.img.old -> $(readlink -f /boot/initrd.img.old)" | |
echo "" | |
# Fetch kernel lists | |
kernels=($(ls /boot/vmlinuz-* | xargs -n 1 basename)) | |
echo "Available kernels:" | |
for i in "${!kernels[@]}"; do | |
echo "$i) ${kernels[$i]}" | |
done | |
# Choose a default kernel | |
echo "Select the default kernel (current 'vmlinuz' symlink):" | |
read default_kernel_index | |
# Choose an old kernel | |
echo "Select the old kernel (current 'vmlinuz.old' symlink):" | |
read old_kernel_index | |
default_kernel="${kernels[$default_kernel_index]}" | |
default_initrd="initrd.img-${default_kernel#vmlinuz-}" | |
old_kernel="${kernels[$old_kernel_index]}" | |
old_initrd="initrd.img-${old_kernel#vmlinuz-}" | |
# Results | |
echo "" | |
echo "Selected default kernel: $default_kernel" | |
echo "Selected old kernel: $old_kernel" | |
echo "" | |
echo "Default kernel related files:" | |
echo " vmlinuz -> /boot/$default_kernel" | |
echo " initrd.img -> /boot/$default_initrd" | |
echo "" | |
echo "Old kernel related files:" | |
echo " vmlinuz.old -> /boot/$old_kernel" | |
echo " initrd.img.old -> /boot/$old_initrd" | |
# Apply | |
if [ $apply_changes -eq 1 ]; then | |
echo "Applying changes..." | |
sudo ln -sf "/boot/$default_kernel" /boot/vmlinuz | |
sudo ln -sf "/boot/$default_initrd" /boot/initrd.img | |
sudo ln -sf "/boot/$old_kernel" /boot/vmlinuz.old | |
sudo ln -sf "/boot/$old_initrd" /boot/initrd.img.old | |
echo "Symlinks updated successfully." | |
else | |
echo "" | |
echo "Dry-run mode: No changes made. Use --apply to update symlinks." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment