Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # install_kernels is an array
- declare -a install_kernels
- boot=/boot
- initrd=$boot/initrd.img
- vmlinuz=$boot/vmlinuz
- # Our current kernel
- current_kernel=$(uname -r)
- # command line options for the kernel
- cmd_line=$(cat /proc/cmdline)
- # Get the installed kernels and put into an array
- #install_kernels=( $(dpkg -l | grep "^ii" | grep linux-image | awk '{print $2}' | sed -e 's/linux-image-//g') )
- install_kernels=( $(dpkg -l | grep "^ii" | grep linux-image | grep -v meta |
- awk '{print $2}' | sed -e 's/linux-image-//g' | grep "\." ))
- is_numeric() {
- echo "$@" | egrep -q -v "[^0-9]"
- }
- #is_numeric() { (exit 1) }
- root_do() {
- if [ "$UID" -eq 0 ] ; then
- eval $@
- RC=$?
- [ $RC -eq 0 ] && return
- echo "Failed!" #&& cd $cur ; exit $RC
- fi
- echo $@
- }
- MAX=${#install_kernels[@]}
- display_choice() {
- # Display the choices
- echo "Installed kernels:"
- for ((i = 0; i < MAX; i++)) ; do
- msg="[$i]"
- if [ "${install_kernels[$i]}" == "$current_kernel" ] ; then
- msg="$msg *** "
- else
- msg="$msg "
- fi
- echo "$msg ${install_kernels[$i]}"
- done
- echo ""
- # Select a kernel to boot:
- echo -n "Select a kernel to boot from: "
- read choice
- if [ -n "$choice" ] ; then
- is_numeric $choice
- if [ $? -eq 0 ] && [ $choice -ge 0 ] && [ $choice -lt $MAX ] ; then
- return
- fi
- fi
- display_choice
- }
- do_kernel() {
- if [ -n "${install_kernels[$choice]}" ] ; then
- if [ "${install_kernels[$choice]}" == "$current_kernel" ] ; then
- echo "You want to reboot the same kernel, sorry!"
- exit 1
- fi
- # cmd_line starts with the following line:
- # BOOT_IMAGE=/boot/vmlinuz-2.6.32-trunk-686
- # Let's not use it, since we are going to use a completely different kernel
- cmd_line=$(echo $cmd_line | sed -e 's#BOOT_IMAGE=[^[:space:]]\+ ##')
- echo -e "\n\n"
- echo "Will boot kernel: [$choice] ${install_kernels[$choice]}"
- _kkexec="kexec -l '${vmlinuz}-${install_kernels[$choice]}' --append '$cmd_line' --initrd '$initrd-${install_kernels[$choice]}'"
- _kkexec_exec="kexec -e"
- root_do $_kkexec
- root_do sync
- root_do /etc/init.d/sendsigs stop
- root_do /etc/init.d/umountfs stop
- root_do /etc/init.d/umountroot stop
- root_do umount -alf
- root_do $_kkexec_exec
- fi
- }
- display_choice
- do_kernel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement