Can Grub be configured to remember the last OS you booted into?

In the original Grub you'd do this by setting, in /boot/grub/menu.lst, the default keyword to "saved" and then using the savedefault keyword in each boot menu entry.


Updated:
Grub2 is a little more complicated. It's also still evolving, so even though Ubuntu 9.10 and 10.04 both use Grub2, there are enough differences in how things are packaged and configured to make things interesting.

Here's how to do the savedefault trick in both:

  • Ubuntu 10.04:
    The 9.10 directions are a trick; Lucid is much simpler. See Ubuntu's help for more gory details. You need to make two changes in /etc/default/grub:

    1. Change the line that reads GRUB_DEFAULT=0 to GRUB_DEFAULT=saved

    2. Add a line with GRUB_SAVEDEFAULT=true

    That's it. Actually isn't that bad, is it? Don't forget to run sudo update-grub.

  • Ubuntu 9.10:
    There are several hacks on the Ubuntu forums for getting this to work; apparently the savedefault keyword was buggy in Grub v1.97. This post on Ubuntu Forums provides one method. Another post suggests a slightly simpler method:

    1. Set GRUB_DEFAULT=saved in /etc/default/grub.

    2. Put these two lines into /etc/grub.d/40_custom (near the bottom, after the "exec tail" line):

      saved_entry=${chosen}
      save_env saved_entry
      

    I've tested this method in Ubuntu 9.10 and it works; the next boot will automatically highlight the last entry chosen. Combined with a timeout, this will automatically boot into the last OS you booted.

And don't forget to run sudo update-grub. After you're finished with your changes. It's important and embarrassingly easy to forget....


Grub has no way to differentiate a reboot from a cold boot, so Grub can't do this on reboot by itself. However, if you're booted into Linux, you can use sudo grub-set-default N (where N is the number of a boot menu entry, starting from 0) to set entry N to be chosen at next boot (instead of whatever entry was saved).

In theory, you could hack together something that hooked into the shutdown routines (an Upstart script, perhaps) that ran grub-set-default for you when you told Linux to shutdown (but not when you told it to reboot). You wouldn't get the same functionality when shutting down Windows, however.

I did hear about a Grub4DOS or Grub1 technique that involved installing Grub to a FAT32 or NTFS /boot partition -- that would allow you to also run grub-set-default from Windows, so you could call it from a Windows logoff script. Last time I saw anything about that, this was not possible on Grub2. That may have changed by now.


This post will be probably be of use to you, it is pretty self-explanatory. Note that 10.04 and 9.10 use the same GRUB version, so things should be exactly the same for you.