Code: sudo cp /usr/sbin/grub-mkconfig /usr/sbin/grub-mkconfig_backup sudo cp /etc/grub.d/10_linux /etc/grub.d/10_linux_backup sudo chmod a-x /etc/grub.d/10_linux_backup sudo cp /etc/defaults/grub /etc/defaults/grub_backup

Code: # These are optional, user-defined variables. export GRUB_DEFAULT \ GRUB_HIDDEN_TIMEOUT \ GRUB_HIDDEN_TIMEOUT_QUIET \ GRUB_TIMEOUT \ GRUB_DISTRIBUTOR \ GRUB_CMDLINE_LINUX \ GRUB_CMDLINE_LINUX_DEFAULT \ GRUB_TERMINAL_INPUT \ GRUB_TERMINAL_OUTPUT \ GRUB_SERIAL_COMMAND \ GRUB_DISABLE_LINUX_UUID \ GRUB_DISABLE_LINUX_RECOVERY \ GRUB_GFXMODE \ GRUB_DISABLE_OS_PROBER \ GRUB_HOWMANY

Code: if [ "x${GRUB_HOWMANY}" = "x" ] || ! [ "x`echo ${GRUB_HOWMANY} | sed 's/^[0-9]\+\(\.[0-9]\+\)\?$//'`" = "x" ] ;then GRUB_HOWMANY=0 fi

Code: found=0

Code: while ( [ "x$list" != "x" ] && ( [ "${GRUB_HOWMANY}" -eq "0" ] || [ "$found" -lt "${GRUB_HOWMANY}" ] )); do found=$(( found + 1 ));

Code: GRUB_HOWMANY=N

Code: sudo update-grub

I don't really like the default behavior of Grub in Ubuntu to continue to add all the kernel updates to my grub boot menu. I never go back and use old kernels (except by mistake) and it just crowds up my boot menu.In the past, I have used the #howmany option in /boot/grub/menu.lst to limit this to only the most recent kernel. Now that I am using Karmic Koala though, this option no longer appears to be available due to the fact that the default bootloader is now Grub 2.The general recommendations I am seeing on the internet for users who would like the #howmany functionality are to either comment out the entries in /boot/grub/grub.cfg or manually remove old kernel packages using aptitude. This means that every time a kernel is updated, I either have to manually comment out more and more lines from the grub.cfg file despite many warnings not to edit that file directly, or go into aptitude and find and uninstall the old "linux-image" packages.I decided to try and add support back in for the #howmany option by adding a GRUB_HOWMANY variable to /etc/defaults/grub. This turned out to be fairly simple.Make backups of your original files before editing them. Issue the following commands to make the backups:(the chmod is there because any scripts in the grub.d directory will be executed during an "update-grub", so the 10_linux_backup file might be executed after the 10_linux script, meaning that the original file would be used instead of our modified script and no changes would be observed. Make it non-executable and it won't be seen as a script).First, I edited the /usr/sbin/grub-mkconfig file and added GRUB_HOWMANY to the list of optional, user-defined variables being exported as shown below:Then I added some logic to /etc/grub.d/10_linux to check and see if GRUB_HOWMANY is a number and if not, then convert it to zero. By the way, I had not noticed that the file was being run in the "dash" shell instead of the "bash" shell, so my first version of the following line using native regular expressions didn't work at all.then a "found" variable to keep track of how many kernels have been added to the menuLast, I add a check to the while loop looping through kernels to stop after finding GRUB_HOWMANY (unless GRUB_HOWMANY is 0) and a line to increment $found each timeTo set the number of kernels, add the following line to /etc/default/grubwhere N is the number of kernels to set. In other words, use GRUB_HOWMANY=1 to show only the most recent kernel in the grub2 boot menu and GRUB_HOWMANY=2 to show two most recent kernels. To show all kernels, use GRUB_HOWMANY=0 or any non-numeric value such as GRUB_HOWMANY="ALL".Finish by runningThat's it. Now I get only the number of kernels I want, and I don't have to do anything after updating to a new kernel.