Mastering Grub 2 The Easy Way

If you’re running Linux, there’s a good chance your distro of choice uses Grub as the default bootloader. Grub has served well for many years, but it’s beginning to show its age. As with all software, it doesn’t take long before the latest-and-greatest becomes old-and-haggard. Features have been piling up in Grub without much thought going into revamping the core program. Eventually, this lead to a messy patchwork that no one really wanted to maintain. At this point, Grub2 was born. It’s a complete rewrite from the ground up using a completely redesigned structure. This new Grub gives us powerful features like conditional statements (if/then, etc), intelligent upgrades, and some greatly improved graphics.

Getting Grub2

Currently, if you want to use Grub2, your best bet is to run Ubuntu 9.10 (Karmic Koala). If you just want to toy around and see what it can do without risking your system, I’d suggest running it inside a virtual machine as described here, but of course with Ubuntu 9.10.

menu.lst/grub.cfg

If you’ve messed with the original Grub (now called Grub legacy), you doubtless know all about menu.lst. That was the file where Grub legacy stored pretty much everything. In Grub2, the closest equivalent is grub.cfg, also located in /boot/grub. One of the most important differences, however, is that you should not edit the grub.cfg file directly. Some of you may be thinking “Ha! Warnings like that are for n00bs!”. Well the reason for this rule is not because the developers don’t think you’re l33t enough, it’s because that file is auto-generated by Grub2 and will be overwritten without warning as a result of things like kernel upgrades.

grub2-grubcfg

Any time the update-grub command is run, Grub2 will read its various config files and use their contents to generate grub.cfg. These files are dealt with individually below.

/etc/default/grub

Many of Grub’s options that used to be in menu.lst are now here. This includes things like whether or not to display the Grub menu, and how long to wait before timeout. This screenshot is the default config in Ubuntu 9.10, and is set to hide the Grub menu, disable “recovery mode” options, and instruct the kernel to include a splash screen.

grub2-etcgrub

By the way, to force Grub 2 to show the menu on boot just hold down the Shift key while Grub is starting.

/etc/grub.d/

This is where things really begin to differ from Grub legacy. Where you’d normally enter all your boot options in the menu.lst file, Grub 2 takes a very different approach. The /etc/grub.d/ directory contains individual files, each representing a option for the boot menu. The following shows the files contained in the default install.

grub2-etcgrubd

While it’s certainly a lot different than Grub legacy, you may notice something familiar about that folder. It’s set up in a way very similar to the standard Linux startup system, SysV Init. Each of the files shown is a shell script that executes in order. Order is determined by the numbers at the start of each filename, so the script in 00_header will always run before, say, 05_debian_theme.

/etc/grub.d/40_custom

Chances are, if you need to edit your boot menu, this is the file you’re looking for. This is where you add your custom entries to the menu. Since this is the last numbered file in the grub.d directory (as explained in the section above), any entries you place in this file will show up at the end of the boot menu.

By default, the 40_custom file is mostly empty. In this example, I’ve added a new menu entry.

grub2-40custom

Splash Images

You can easily download the Grub 2 splash pack in Ubuntu with:

When it’s done, all the images will be stores as TGA files in /usr/share/images/grub/.

grub2-splashpreview

The file that specifies what splash image to use on boot is the 05_debian_theme file we saw earlier. So to set our new custom splash, we want to open /etc/grub.d/05_debian_theme as root in whatever text editor you prefer. Look for a line similar to the highlighted one in the example below,

grub2-changesplash

And change the filename to whichever splash image you prefer. Make certain the path points to the /usr/share/images/grub/ directory. Some older versions default to /usr/share/images/desktop-base/ and that’s not what we want.

Applying changes

As you may remember from the beginning of this guide, the grub.cfg file is auto-generated from the other config files we’ve covered. When you’ve made changes to those files, you’ve got to tell Grub to read them and pull together a new config file. To do that, simply run the following command as root:

Once it’s complete, reboot your computer (or virtual machine) to see the results. Don’t forget to hold Shift while booting, if your menu is still set to Hidden.

grub2-large

26 comments

  1. Nice explanation! I was recently frustrated with the new Grub2 package. Maybe now I’ll go tinkering to see if I can bend it to my will.

  2. Suppose on your dual boot you foolishly set timeout to zero, thinking that is an infinite setting, not zero. In the days of legacy Grub, you just booted up your live install disk, edited menu.1st, changing timeout=0 to timeout=10 or whatever and rebooted. Problem solved!

    Now, plastered all over the grub.cfg file are dire warnings not to edit that file. You cannot run your installed copy of startupmanager from the live CD, nor can you install it. What to do?

    I just boldly and foolishly edited grub.cfg anyway, changing timeout=0 to timeout=10. It worked fine. Then after Ubuntu returned, I ran startupmanager to set to 10 and get all the auxiliary files in agreement. Without tools to fix bonehead moves like mine, Grub2 IS NOT an improvement in this regard.

    • Your method was fine, except you wanted to edit /etc/default/grub, not grub.cfg. Everything in grub.cfg is pulled from other files.

      In your case, the only difference needed was that you needed to edit /etc/default/grub, not menu.lst.

      • … except that Steve had to edit the actual grub configuration from another OS (to recover from a previous grub configuration failure). Editing /etc/default/grub wouldn’t help, since he was not able to run the update script in the target OS. So, he had to directly edit the grub startup files (then he could reboot, re-apply his changes to /etc/default/grub, run the update script, then be back to normal).

        Right?

        Having to edit configuration then run some other script to “build” the “real” configuration is annoying.

        • from livecd, it’d be something like:

          chroot /dev/hda1
          nano /etc/default/grub
          update-grub

        • @Joshua Price:

          You can only hope your “chroot/nano/update-grub” sequence will work. Even with identical architecture distros, I’ve encountered chroot’d environments where libraries won’t link, device files are missing (or remapped), or filesystem features available on updated kernels conflict with older kernel versions.

          Please, actually try something on a test system before dispensing this kind of advice.

  3. Will this grub auto-boot a degraded RAID array? Or will it still ask me if I want to do so? Well, yea, I suppose if I went to the trouble of mirroring two disks in case one should fail out there in that distant data center, I *do* want it to boot. That’s why I’m mirroring, isn’t it?

    Oh, but wait, I can put a pretty picture on the boot screen! Sheesh, this is a non-starter. I’m sticking with Lilo.

  4. Thanks for your article which is most useful

    I have some suggestions/criticism in relation to Grub2 development. This is
    based on my experience using Grub 2 with Ubuntu/Kubuntu – I think it’s similar
    with outer distros.

    1. On a Dual boot system which I share with a non Linux user (windows), I
    discovered that Grub automatically added a Windows restore partition as a first
    of two Windows entries – leading to potential wiping of system by non
    technical user

    2. Every time Grub gets updated, all older installed kernels get added as boot
    options. I think the older kernels and the memory test options should be
    relegated to a ‘More options’ menu. Normally they are not used.

    3. In light of the above, I think that the user should be given some graphical
    UI or popup when Grub is being installed or updated – so that the user can
    (de)select appropriate boot options (e.g. check-boxes). Hints could be
    provided (e.g. “Note: This partition is probably a Windows restore partition).

    Thanks again for the article – I previously just edited
    ‘grub.cfg’ – without any problems ;) – I will take another look – and I hope
    there is some options so that I don’t have to remove Restore partitions and
    unused kernels every update)

    23.

  5. For me, grub2 didn’t work. Appearantly “grub-probe” can’t deal with LUKS encrypted partitions or /etc/crypttab, or maybe it’s the btrfs partition that disturbs it that badly.
    However, the automatic boot file configuration doesn’t seem all too well thought out. The additional programming language features in the boot loader would better be spent elsewhere.

  6. Can Grub2 boot from CD/DVD drive? System Commander was able to do this task 10 years ago. Grub-legacy cannot do that. What about Grub2? I prefer to disable DVD boot in BIOS and have an option in my GRUB menu to boot from DVD drive when needed.

  7. Grub2 showed lots of promise to boot live CD iso images directly from Hard disk, the same way as it would boot from CD/DVD drive. But for me it was not possible even after doing lots of experiments with grub2. I never edited grub.cfg instead I always edited the 40_custom file and updated grub. Has anybody tried doing it and was successful in it??? Please help me out… Thanx..

  8. Grub 2 is far from ready for general distribution, and certainly is unsuitable being put in the latest Ubuntu release as the default. I found this out the hard way because I went ahead and installed karmic on my multi-boot pc. Luckily, because it got the configuration spectacularly wrong, including writing itself to the mbr of the wrong hdd, the only thing it messed up was the new karmic installation — it was very difficult to chainload from my existing grub legacy mbr bootloader (sometimes it would boot, often it wouldn’t).

    I finally got karmic to boot reliably by booting it directly from grub legacy and bypassing grub 2 (which is still installed, but harmlessly doing nothing).

    The advice on how to “restore” grub 2 is a horrible kludge, for power users only. Everything about grub 2, from functions to appearance shows it to be in a very, very unfinished state.

    GNU/Linux has come far enough along that users need not be subjected to this sort of thing anymore.

  9. Thanks for your post!

    I have a question. There was a bug on Ubuntu 9.04 where we needed to insert some configs on the menu.lst file. The bug is described here (https://bugs.launchpad.net/ubuntu/+bug/334249) and we needed to use something like this:

    kernel /boot/vmlinuz-2.6.28-13-generic
    root=UUID=aa6577a8-fec3-4628-b0db-90e400355d15 ro vga=791 i8042.reset
    i8042.nomux i8042.nopnp i8042.noloop

    where i8042.reset i8042.nomux i8042.nopnp i8042.noloop are the options who must be inserted on the end of the line to make the book works fine again. With Grub2, where we can append/insert/edit those options?

    Best regards,

  10. Put additional boot options in /etc/default/grub. The first is default extra options for normal kernels, and the second is anything added for (recovery) entries:

    GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash”
    GRUB_CMDLINE_LINUX=””

    I had to remove the “splash” option from 64-bit Ubuntu 9.10 on my desktop, because splash crashes grub2 with my DVI connected older 1280×720 HDTV Ready LCD even if I specifically set GRUB_GFXMODE=640×480 (1 second flashing cursor, then endless black screen with no disk access or anything). Although, the HDTV handles PC video modes up to 1280×1024 and regular defaults worked fine when VGA connected.

  11. Fully agree with David Smith. I currently have one Windows 7 entry at the bottom (I want it at the top), two entries for memtest, recovery and normal kernels, and only want one for each (these came after I ran Synaptic Update Manager and it updated the Linux kernels).

    I have no idea how to move Windows to the top, or how to remove the duplicate entries.

    Hate to say this, but sometimes, Windows does it better. Their startup menu is easy to edit (as was Grub the first) using 3rd party apps. Maybe someone will come up with something for grub2.

    • If you want to change the order of boot entry, just change the number in front of the files in grub.d/
      I wanted win7 first so I change 30_os-prober to 07_win7 for example

  12. I got grub2 installed (upgraded on my karmic). Though I see TIMEOUT=10 in one of the files, the grub does not automatically boot with the DEFAULT=0 OS. I will have to press “Enter” to start the booting.

    Any idea what I may have to change in order to get this working?

  13. i did this how to but i am haveing problems hiding the othere intres in my menu i need help thanx .

  14. In grub legacy I could easily edit /boot/grub/menu.lst.

    I have two PC’s with XP and several Linux OS’s including Puppy where I must manually enter the entry.

    On Grub 2 ( installed from Ubuntu 9.10 or Mint 8 [Ubuntu-based] ) I need to edit, delete and add certain OS entries to what was easily placed or removed from ~/menu.lst.

    What should I do?


    Bob

  15. Ubuntu is starting to remind me of windows where it “improves” things just to make it more difficult for the user. Grub 2 is a case in point.

  16. I wish it were so easy.
    I've wasted 4 weeks of productive 'tinkering' whilst going round in circles trying to get grub2 to boot Mint8 AND Fedora FC12 (still grub1) off the same menu. Too scared to setup anything permanently incase I trash everything with a badly formed grub install.
    I can F8 select the boot drive and individually get either to boot, but.. (and chainload should be the answer) anything I try gets me nowhere but Error 15 , 13, 8, 'unrecognised format' etc. The disk numbers from the chroot are different from the debug console (that's when I get that far). Part of my problem is that I'm finding dozens of posts not quite dealing with my problem (SATA + PATA boot drives too) and NO clear information about fixing 'my' problem.
    Some things just seem broken from the outset.
    os-prober does not report any OS even though I have 2, (so what's it doing ?)
    It seems impossible to test out grub2 from the command line like I could with grub.
    Confusing terminology muddles the correct place to use '(hdx)' or '/dev/sdx' to a layman.

    I just want to boot my computer.
    Enjoying debugging Grub2 was not the purpose I bought the computer for.

  17. I have downloaded the splash images and made the changes in /etc/grub.d/*02_debian_theme file as
    for i in {/boot/grub,/usr/share/images/grub/Windbuchencom.tga}/moreblue-orbit-grub.{png,tga} ; do

    while i am running command for updating the grub getting error as which is highlighted below. why is it so??????

    vivek@vivek-friend:~$ sudo update-grub
    Generating grub.cfg …
    Found linux image: /boot/vmlinuz-2.6.31-14-generic
    Found initrd image: /boot/initrd.img-2.6.31-14-generic
    Found memtest86+ image: /boot/memtest86+.bin
    /sbin/lvs: 1: Syntax error: newline unexpected
    Found Windows 7 (loader) on /dev/sda1

    thanks

  18. I don't worry about Grub2.

    I have a small 100 Mb /boot partition on the hard drive in which I have Grub Legacy installed. The Master Boot Record (MBR) points to this and this never changes. I use this version of Grub Legacy to chainload whichever bootloader exists in the partition of the OS being loaded.

    So, on my hard drive I have Windows 7 on one partition, with the Windows bootloader installed within that partition. I have Kubuntu in another partition, with the Grub2 bootloader installed in that partition (but not to the MBR). I also have ubuntu in another partition, and it also has Grub2 installed only within that partition (not in the MBR). Finally, I have Mac OSX, with its own bootloader.

    When the system boots, the MBR is set to point to Grub Legacy in the small boot partition. This gives me the menu with the list of Operating Systems on my hard drive.

    When I select one of the choices on the menu, Grub Legacy then merely chainloads whichever bootloader is associated with the OS I have chosen, each stored in the same partition as the OS.

    With this setup, I never have to change the MBR, don't have to fiddle much with the boot partition version of Grub Legacy (except to change the menu), and don't have to worry about each individual bootloader (since they are free to update themselves within their own partition without affecting any other partition).

    The chainloading process takes only 1 extra second, so doesn't slow down things at all.

    There is no better way to go, if you are using multiple OSs.

    My method is at http://ubuntuguide.org/wiki/Multiple_OS_Installation or
    http://kubuntuguide.org/Multiple_OS_Installation

Comments are closed.

Sponsored Stories