Grub 2 Basics Grub 2 Basics 1. Introduction 2. Grub 2 Files & Options 3. Booting Grub 4. Adding Entries to Grub 2 5. Removing Entries from Grub 2 6. Grub 2 Splash Images 7. Selected Problems 8. Booting to Recovery Mode w/o Menu Option 9. Links 1. Introduction I've written this guide to present some basic information about Grub 2. It is meant for users who may be familiar with basic editing of the original Grub (Grub Legacy) menu.lst and wonder how it carries over to Grub 2 (ver 1.97). I will not cover how to install or uninstall Grub 2 or whether you should convert to Grub 2. Grub 2 will be the default in Ubuntu 9.10, Karmic Koala but the plan is not to convert over previous Grub legacy installations to Grub 2. Official Announcement / Story StartUp-Manager supports Grub 2, but currently only on a limited basis. Many of StartUp-Manager's options are not currently compatible with Grub 2. There are plans for a StartUp-Manager 2 that works only for Grub 2 but it is still under development according to its creator. Also note that Grub 2 is still in development. Official documentation at this point is limited and still being created. That is partly the reason for this post. More comprehensive and complete documentation will hopefully be forthcoming from the developers and volunteers. Of the existing documentation, I have have provided several links to the better sources at the end of this post. First, before proceeding, confirm the version of Grub you are using. Running this command should provide the version: Code: grub-install -v Which should produce something like this: Quote: drs305@mycomputer:~$ grub-install -v grub-install (GNU GRUB 1.97~beta2) 2. Booting Grub Grub 2 loads before the operating system. It's modular components are loaded on an as-needed basis. Menu display behavior is generally determined by settings in /etc/default/grub. Review the "Grub 2 Files & Options" section for specific entry and formatting guidance. The main options for displaying the menu are: * Initial Default o Grub 2 will boot straight into the default operating system if no other operating system is detected. No menu will be displayed. If another operating system is detected, the Grub 2 menu will display. * Timed display. o The default delay is 10 seconds. If no user input is made Grub 2 boots to the default entry. o The countdown can be stopped by pressing any key. The user must then make a selection manually. o The booted entry is determined by the DEFAULT= setting in /etc/default/grub, The first "menuentry" is 0. * Hidden o The user can interrupt the boot process and display the menu by holding down the SHIFT key until the menu displays. Grub 2 searches for a depressed SHIFT key signal during boot. If the key is pressed or Grub 2 cannot determine the status of the key, the menu is displayed. o The time the screen remains blank but available for display is determined by a setting in /etc/default/grub. o To provide visual feedback during while the countdown continues, a countdown display can be shown on the screen. o At the end of the timeout, the default entry determined in /etc/default/grub will be selected. * Saved o If the default option is set to "saved", the last kernel/system successfully booted will be selected and run if no input is made. 3. Grub 2 Files & Options Many of the files in /boot/grub will not be recognizable by users of Grub Legacy. Especially noticeable are the multitude of *.mod files. Grub 2 is modular and these files are loaded as necessary by the grub bootloader. The Grub 2 user-configurable settings are contained mainly in /etc/default/grub and the files in /etc/grub.d. When update-grub is executed the results are input into the /boot/grub/grub.cfg file. * /boot/grub/grub.cfg o This is the main Grub 2 file. It "replaces" Grub Legacy's /boot/grub/menu.lst This file contains the Grub menu information but unlike Grub Legacy's menu.lst file, grub.cfg is not meant to be edited. + grub.cfg is automatcially generated when "update-grub" is executed: + Each section (### BEGIN) is clearly delineated and references the file in the /etc/grub.d folder from which the information was generated. + grub.cfg is updated by running the "update-grub2" or "update-grub" command as root. + By default, and whenever the "update-grub2" command is executed, this file is made "read-only". This is in keeping with the intent that the file should not be edited manually. If you must edit this file, instructions are provided in Section 2. o Sample grub.cfg including Windows and one manual entry (41_srcd): Code: # # DO NOT EDIT THIS FILE # # It is automatically generated by /usr/sbin/grub-mkconfig using templates # from /etc/grub.d and settings from /etc/default/grub # ### BEGIN /etc/grub.d/00_header ### load_env set default="0" if [ ${prev_saved_entry} ]; then saved_entry=${prev_saved_entry} save_env saved_entry prev_saved_entry= save_env prev_saved_entry fi insmod ext2 set root=(hd0,1) search --no-floppy --fs-uuid --set 7ebcfe33-6914-42ec-9d2e-0859f7396925 if loadfont /usr/share/grub/unicode.pf2 ; then set gfxmode=640x480 insmod gfxterm insmod vbe if terminal_output gfxterm ; then true ; else # For backward compatibility with versions of terminal.mod that don't # understand terminal_output terminal gfxterm fi fi if [ ${recordfail} = 1 ]; then set timeout=-1 else set timeout=10 fi ### END /etc/grub.d/00_header ### ### BEGIN /etc/grub.d/10_linux ### menuentry "Ubuntu, Linux 2.6.31-12-generic" { recordfail=1 save_env recordfail set quiet=1 insmod ext2 set root=(hd0,1) search --no-floppy --fs-uuid --set 7ebcfe33-6914-42ec-9d2e-0859f7396933 linux /boot/vmlinuz-2.6.31-12-generic root=UUID=7ebcfe33-6914-42ec-9d2e-0859f7396933 ro quiet splash initrd /boot/initrd.img-2.6.31-12-generic } menuentry "Ubuntu, Linux 2.6.31-12-generic (recovery mode)" { recordfail=1 save_env recordfail insmod ext2 set root=(hd0,1) search --no-floppy --fs-uuid --set 7ebcfe33-6914-42ec-9d2e-0859f7396933 linux /boot/vmlinuz-2.6.31-12-generic root=UUID=7ebcfe33-6914-42ec-9d2e-0859f7396933 ro single initrd /boot/initrd.img-2.6.31-12-generic } menuentry "Ubuntu, Linux 2.6.31-11-generic" { recordfail=1 save_env recordfail set quiet=1 insmod ext2 set root=(hd0,1) search --no-floppy --fs-uuid --set 7ebcfe33-6914-42ec-9d2e-0859f7396933 linux nv saved_entry set root=(hd0,6) linux /sysrcd/rescuecd subdir=sysrcd setkmap=us initrd /sysrcd/initram.igz } ### END /etc/grub.d/40_custom ### * /etc/default/grub o This file contains information formerly contained in the upper section of Grub Legacy's menu.lst and items contained on the end of the kernel line. The items in this file can be edited by a user with administrator (root) privileges. Quote: # If you change this file, run 'update-grub' afterwards to update # /boot/grub/grub.cfg. GRUB_DEFAULT=0 GRUB_HIDDEN_TIMEOUT=0 GRUB_HIDDEN_TIMEOUT_QUIET=true GRUB_TIMEOUT="10" GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian` GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" GRUB_CMDLINE_LINUX="" # Uncomment to disable graphical terminal (grub-pc only) #GRUB_TERMINAL=console # The resolution used on graphical terminal # note that you can use only modes which your graphic card supports via VBE # you can see them in real GRUB with the command `vbeinfo' #GRUB_GFXMODE=640x480 # Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux #GRUB_DISABLE_LINUX_UUID=true # Uncomment to disable generation of recovery mode menu entrys #GRUB_DISABLE_LINUX_RECOVERY="true" o GRUB_DEFAULT - Sets the default menu entry. Entries may be numeric or "saved" + GRUB_DEFAULT=0 - Sets the default menu entry by menu position. As Grub Legacy, the first "menuentry" in grub.cfg is 0, the second is 1, etc. + GRUB_DEFAULT=saved - Sets the default menu entry with whatever was selected last. If the menu is displayed duru hidden by default unless another OS is detected by the system. If there is no other OS, this line might be commented out. To display the menu on each boot, uncomment the line and use a value of 1 or higher. + In addition to editing the file as root, you can also run the following commands the check and change the default timeout value. The first checks the existing timeout, the second replaces the value. Replace T with the new value. [code] cat /etc/default/grub | grep 'GRUB_TIMEOUT=' # Checks current TIMEOUT value. sudo sed 's/GRUB_TIMEOUT=5/GRUB_TIMEOUT=T/g' -i /etc/default/grub # Change TIMEOUT value. Replace T with new value. o GRUB_HIDDEN_TIMEOUT=0 + The menu will be hidden unless a # symbol is present at the beginning of this line. ( # GRUB_HIDDEN_TIMEOUT=0 ) + Bugs still exist in this feature. Hiding the menu may or may not work. + The default setting initially depends on the presence of other operating systems. # Another OS Detected: The menu will be displayed. ( The line will begin with a # symbol. ) # No other OS Detected: The menu will be hidden. + For integers greater than 0, the system will pause, but not display the menu, for the entered number of seconds. + 0 The menu will not be displayed. There will be no delay. # When this entry is set to 0: * The user may force displaying the menu as the computer boots by holding down the SHIFT key. * During boot, the system will check the SHIFT key status. If it cannot determine the key status, a short delay will enable the user to display the menu by pressing the ESC key. + If enabled, the splash screen designated in 05_debian_theme will be displayed even if the hidden menu feature is selected. o GRUB_HIDDEN_TIMEOUT_QUIET=true + true - No countdown is displayed. The screen will be blank. This line imports any entries to the end of the 'linux' line (Grub Legacy's "kernel" line). The entries are appended to the end of the normal mode only. This is similar to the "defoptions" line in menu.lst. For a black screen with boot processes displayed in text, remove "quiet splash". To see the grub splash image plus a condensed text output, use "splash". o #GRUB_TERMINAL=console Uncomment to disable graphical terminal (grub-pc only) o #GRUB_DISABLE_LINUX_UUID=true Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux o #GRUB_GFXMODE=640x480 You can add this line and remove the # symbol to make it active. This entry sets the resolution of the graphical menu (the menu text size). It provides resolutions supported by the user's graphics card (e.g. 640x480, 800x600, 1280x1024, etc). The setting applies only to the boot menu text. o GRUB_DISABLE_LINUX_RECOVERY=true Add or uncomment this line to prevent "Recovery" mode kernel options from appearing in the menu. If you want a "Recovery" option for only one kernel, make a special entry in /etc/grub/40_custom. o GRUB_DISABLE_OS_PROBER="true" - Enables/disables the os-prober check of other partitions for operating systems, including Windows, Linux, OSX and Hurd. * /etc/grub.d/ o The files in this folder are read during execution of "update-grub" or "update-grub2" commands. The contents are imported into /boot/grub/grub.cfg The order of the entries in the grub menu is based on the order of the file names. File named with a starting numeral are run before those beginning with a letter. The order the files are run determines the menu order in grub.cfg. Custom entries can be added to the "40_custom" file or in a newly created file. Any file created must be executable in order to be included in the grub.cfg file during the "update-grub2" command. + 00_header +ub2" is executed, Grub 2 will search for linux kernels and other Operating Systems. What and where is looks is based on the files contained in /etc/grub.d folder. + 10_linux searches for installed linux kernels. + 30_os-prober searches for other Operating Systems. * Custom User Entries (/etc/grub.d/40_custom). o Entries to grub.cfg can be manually inserted by creating a file in the /etc/grub.d folder. + The name of the file determines the order in the menu. 30_os-prober entries will be placed before 40_custom entries, which will be placed before 50_my-sample entries. + Any created file must be made executable. This can be done as root by running "sudo chmod +x /etc/grub.d/filename". + The files in the /etc/grub.d folder will be read and the contents included in grub.cfg when the "update-grub2" command is executed as root. o A sample entry. This file creates a menu item for running the SystemRescueCD (previously installed) from a partition created on sda10. Folders and files must have been copied to the correct location in accordance with the SystemRescueCD if you wish to actually use this entry. + Quote: #!/bin/sh echo "Adding SystemRescueCD" >&2 cat << EOF menuentry "System Rescue CD" { set root=(hd0,10) linux /sysrcd/rescuecd subdir=sysrcd setkmap=us initrd /sysrcd/initram.igz } EOF + Note the new partition naming convention. Devices start counting from "0" as done previously. sda is designated as "hd0", sdb is "hd1", etc. However the first partition is now designated as sda1. Counting partitions does not start with "0". sda5 is "5". + The line 'echo "Adding SystemRescueCD" >&2' is not required. Including it in the file allows this line to be seen in the terminal when "update-grub2" is executed. It provides visualfile and name it "07_xxxx". Since the files in /etc/grub.d/ are read sequentially, those in "07_custom" will be placed before those of "10_linux". I recommend not naming a custom menu file lower than 06 so that any theme run from 05_debian_theme is allowed to run before any custom menu is created. After creating the file, run sudo update-grub and then check the value of "DEFAULT" in /etc/default/grub. If it doesn't point to the correct menuentry, change the value of DEFAULT to the correct menuentry value. o Omitting memtest86+: To prevent "memtest86+" entries in your Grub 2 menu, remove the "executable" bit from /etc/grub.d/20_memtest86+. You can do thisMergeList via a file browser by selecting "Properties (right click), Permissions", or via the command line: Code: sudo chmod -x /etc/grub.d/20_memtest86+ o Omitting Recovery Mode entries: The file /etc/grub.d/10_linux was recently updated to include a check for recovery mode options. Edit /etc/default/grub and add or change this line: Quote: GRUB_DISABLE_LINUX_RECOVERY=true If you have an older version of /etc/grub.d/10_linux and the above does not work after updating grub, you can prevent "Recovery mode" entries in your Grub 2 menu, by editing /etc/grub.d/10_linux. If there are no conditional "if" statements concerning the recovery mode, place a comment symbol (#) in front of the following lines (at approximately line 146) of the old file: Quote: # linux_entry "${OS}, Linux ${version} (recovery mode)" \ # "single ${GRUB_CMDLINE_LINUX}" If you wish to retain one "Recovery mode" entry for insurance, you can add an entry to /etc/grub.d/40_custom which will appear at the bottom of your grub menu. o Building a Totally Customized Menu: Ok, admit you are a control freak and you want to see only what you build yourself - customized titles, no "memtest86+" and no extra kernels. Here is how you do it: + Run sudo update-grub to get the current available kernels. u. + Edit the titles of the "menuentry" line if desired (between the quotation symbols). Do not change the lines following the "menuentry" line. Each entry should start with a "menuentry" line and end with a "}" on the last line. + Remove the executable bit from /etc/grub.d/10_linux, /etc/grub.d/20_memtest86+ and /etc/grub.d/30_os-prober Removing the executable bit from any file in /etc/grub.d will exclude the file from being included in grub updates. Code: sudo chmod -x /etc/grub.d/10_linux /etc/grub.d/20_memtest86+ /etc/grub.d/30_os-prober + Run "sudo update-grub" + The updated /boot/grub/grub.cfg file should now contain only sections for "00_header", "05_debian_theme" and "40_custom". + The grub.cfg file will not be updated with the addition of a new kernel. To add a new kernel, make "10_linux" executable, run "sudo update-grub" to refresh the available kernels, and repeat these instructions. o Incorporating the DEFAULT=save Option: In order to enable the "saved default" option with which Grub 2 preselects the last successfully-used option at boot, the "DEFAULT=save" option must be entered in /etc/default/grub and the 40_custom file must be modified. An example of a 40_custom file follows: + Quote: #! /bin/sh -e prefix=/usr exec_prefix=${prefix} libdir=${exec_prefix}/lib . ${libdir}/grub/grub-mkconfig_lib echo "Adding SystemRescueCD & Custom Kernel" >&2 cat << EOF menuentry "Jaunty 2.6.28-15-custom" { EOF save_default_entry | sed -e "s/^/\t/" cat << EOF set root=(hd0,7) linux /boot/vmlinuz-2.6.28-15-custom root=UUID=12c55255-27b3-488b-hje7e-9dbe4e2esfg5 ro quiet splash initrd /boot/initrd.img<< EOF set root=(hd0,6) linux /sysrcd/rescuecd subdir=sysrcd setkmap=us initrd /sysrcd/initram.igz } EOF + Manual Windows Entry (with /etc/grub.d/30_os-prober made unexecutable) Quote: #! /bin/sh -e echo "Adding Windows 43_custom" >&2 cat << EOF menuentry "Windows Vista 43_custom" { insmod ntfs set root=(hd0,1) search --no-floppy --fs-uuid --set CFFCFF9EECFF7F49 chainloader +1 } EOF o Don't forget to run "sudo update-grub" after making any changes to your /etc/grub.d files. * Manual Editing of grub.cfg (Not encouraged) Manual editing of /boot/grub/grub.cfg is not encouraged. Think of grub.cfg as a result, not as an initiator. The files that should be edited are contained in the /etc/grub.d folders and the /etc/default/grub file. In order to discourage its editing, grub.cfg is read-only. Even attempting to open, edit and save this file using root privileges cannot be done until the 'read-only' status is changed. If you must edit this file: Code: sudo chmod +w /boot/grub/grub.cfg gksudo gedit /boot/grub/grub.cfg Note: This file is returned to 'read-only' status anytime the update-grub command is run. 5. Removing Entries from Grub 2 Entries should be removed by editing or removing files in the /etc/grub.d folder. The /boot/grub/grub.cfg file is read-only and should not normally require editing. * Automatically. o Too Many Kernels? Kernels removed via Synaptic or with "apt-get remove" will automatically update grub.cfg and no user action is required. + In Synaptic, type the kernel number in the search window at the upper right (for example - 2.6.28-11).moval" and then press the Apply main menu button. + The kernels will be removed from your system and from the Grub menu. + If you are not sure of the kernel you are currently using, in a terminal type "uname -r". + Many users keep one previous kernel on the machine which previously ran without problems. o Other Operating Systems which have been removed from the computer will also be removed from the menu once "update-grub2" is run as root. o To prevent one of the /etc/init.d files from running, remove the "executable" bit. + Example: If you don't want to see the "Memtest86+" entries, run this command: Code: sudo chmod -x /etc/grub.d/20_memtest86+ + Run the update-grub command to allow the changes to be incorporated in grub.cfg User-Created Entries. o To remove a user-created menu entry, remove the applicable file from the /etc/grub.d folder. o If a custom file contains multiple entries, individual items may be removed and others retained. o Once the file has been removed or edited, run "update-grub2" to update grub.cfg. 6. Grub 2 Splash Images Why reinvent the wheel? Visit this site for an excellent presentation on creating Grub 2 images: http://members.iinet.net/~herman546/p20/GRUB2%20Splashimages.html However, if encrypted disks are not an issue, here are the basics: * Manually copy grub splash images into the /usr/share/images/grub folder or install the default grub2 splash images via Synaptic or: Code: sudo apt-get install grub2-splashimages * The grub2's splash images are controlled by /etc/grub.d/05_debian_theme. Open this file for editing: Code: gksudo gedit /etc/grub.d/05_debian_theme Find the following line and edit the highlighted area, replacing it with the grub splash image you point Grub 2 splash images were downloaded and stored in /usr/share/images/desktop.base If this is where your grub images are stored, change the address in the previous command accordingly ( ... /usr/share/images/desktop-base} ... ). * Save the file, then update grub2: Code: sudo update-grub2 7. Selected Problems If you find you need to remove the following lines manually to get Grub to boot properly: Quote: recordfail=1 save_env recordfail Open /etc/grub.d/10_linux with admin rights: Code: gksu gedit /etc/grub.d/10_linux Look for this section and comment the lines in bold; Quote: linux_entry () { cat << EOF menuentry "$1" { recordfail=1 save_env recordfail Change it to: Quote: - linux_entry () { cat << EOF menuentry "$1" { # recordfail=1 # save_env recordfail Save the file, then run Code: sudo update-grub The two lines should no longer appear when you press E to view the selection in the Grub 2 menu. You will see a "save_env save_entry" line but this should not cause the failure if you successfully booted the previous time. 8. How to Boot to the Recovery Mode w/o a Menu Option 1. If you have Grub 2 set to boot without displaying the menu at all, hold the SHIFT key down until the menu displays. (In Grub it was the ESC key.) 2. Press any key once the menu is displayed to 'freeze' it. Then arrow to the kernel you want to boot. 3. Press E 4. Scroll to the end of the "linux /boot/vmlinuz...." line. If displayed, remove "quiet" and/or "splash". Add the word "single" to the end of the line. 5. Press CTRL-X to boot to the Recovery menu. 9. Links Grub 2 Introduction https://wiki.ubuntu.com/KernelTeam/Grub2Testing GNU Grub 2 Manual Links GNU Grub 2 Manual (in development) Grub 2 Wiki Herman's Grub 2 Site Comprehensive.