Grub2

GRUB 2 is the default boot loader and manager for Ubuntu since version 9.10 (Karmic Koala). As the computer starts, GRUB 2 either presents a menu and awaits user input or automatically transfers control to an operating system kernel. GRUB 2 is a descendant of GRUB (GRand Unified Bootloader). It has been completely rewritten to provide the user significantly increased flexibility and performance. GRUB 2 is Free Software.

In this guide, GRUB 2 is version 1.98 or later. GRUB legacy (version 0.97) will be referred to as GRUB. To determine your version, use grub-install -v. Grub version 1.99 became the default on Ubuntu 11.04 (Natty Narwhal) and introduced some major changes in the Grub file contents. This guide covers the use of Grub 1.98, the Grub release found in Ubuntu 10.04 LTS (Lucid Lynx). and Grub 1.99, packaged with Ubuntu 12.04 LTS (Precise Pangolin). Content reflecting other versions of Grub 2 will be noted in the appropriate entry.

Improvements

GRUB 2's major improvements over the original GRUB include:

  • Scripting support including conditional statements and functions
  • Dynamic module loading
  • Rescue mode
  • Custom Menus
  • Themes
  • Graphical boot menu support and improved splash capability
  • Boot LiveCD ISO images directly from hard drive
  • New configuration file structure
  • Non-x86 platform support (such as PowerPC)
  • Universal support for UUIDs (not just Ubuntu)

GRUB vs GRUB 2

GRUB 2's default menu will look familiar to GRUB users but there are a great number of differences beneath the surface.

  • On a new installation of Ubuntu 9.10 or later with no other installed operating systems, GRUB 2 will boot directly to the login prompt or Desktop. No menu will be displayed.
  • Hold down (right) SHIFT to display the menu during boot. In certain cases, pressing the ESC key may also display the menu.
  • No /boot/grub/menu.lst. It has been replaced by /boot/grub/grub.cfg.

  • There is no "/find boot/grub/stage1" at the grub prompt. Stage 1.5 has been eliminated.
  • The main Grub 2 configuration file, normally located in the /boot/grub folder, is grub.cfg. It is the product of various scripts and should not normally be edited directly.

  • grub.cfg is overwritten by certain Grub 2 package updates, whenever a kernel is added or removed, or when the user runs update-grub.

  • The menu list of available Linux kernels is automatically generated by running update-grub.

  • The user can create a custom file in which the user can place his own menu entries. This file will not be overwritten. By default, a custom file named 40_custom is available for use in the /etc/grub.d folder.

  • The primary configuration file for changing menu display settings is called grub and by default is located in the /etc/default folder.

  • There are multiple files for configuring the menu - /etc/default/grub mentioned above, and all the files in the /etc/grub.d/ directory.

  • Partition numbering has changed. The first partition is now 1 rather than 0. The first device/drive is still hd0 by default (no change). These designations can be altered if necessary in the device.map file in the /boot/grub folder.

  • Automated searches for other operating systems, such as Windows, are accomplished whenever update-grub is executed. Operating systems found will be placed in the Grub 2 menu.

  • Changes made in the configuration files will not take effect until the update-grub command is executed.

Upgrading to GRUB 2 From GRUB

GRUB 2 is the default bootloader for Ubuntu. Users who still have Grub 0.97 installed on their Ubuntu systems can upgrade to GRUB 2 by installing the currently-supported releases of Ubuntu or by enabling repositories which contain the GRUB 2 package grub-pc.

Please visit the Grub2/Upgrading community documentation for more information and instructions.

File Structure

GRUB 2 incorporates a totally revised directory and file hierarchy. The major GRUB 2 folders include /etc/grub.d, which contains the main GRUB 2 scripts, and /boot/grub, which contains the GRUB 2 modules and menu file (grub.cfg). User customizations are normally made to the /etc/default/grub file.

The description of the major GRUB 2 folders and files is located on the Grub2/Setup page.

Configuring GRUB 2

Configuration changes are normally made to the /etc/default/grub file and to the custom scripts located in /etc/grub.d. No changes are made to the GRUB 2 menu until the update-grub command is run as root. This command runs the GRUB 2 configuration scripts and updates the /boot/grub/grub.cfg file.

Descriptions of the GRUB 2 options and how to change them are presented on the Grub2/Setup community documentation page.

Boot Display Behavior

GRUB 2 loads before any operating system. Its modular components are loaded on an as-needed basis. Menu display behavior is generally determined by settings in /etc/default/grub. Review the "Configuring GRUB 2" section for specific entry and formatting guidance.

Initial Default

  • 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

  • The menu is displayed for a predetermined number of seconds. The default entry is highlighted and other selections may be made by the user until the timeout expires. The default timeout is 2 seconds. The timer continues until any key is pressed or the highlighted entry is selected by pressing ENTER. If no user input is made GRUB 2 boots the default entry at the end of the timeout period.

  • Once a key has been pressed the timeout is deactivated and the user must make a selection manually.
  • The default entry is determined by the GRUB_DEFAULT= setting in /etc/default/grub; the first "menuentry" has a value of "0".

Hidden

  • Normal Hidden Operations Enabled:
    • No menu entries are displayed. The splash screen, if configured, will be displayed.
    • The time the screen remains blank but available for display is determined by a setting in /etc/default/grub (GRUB_HIDDEN_TIMEOUT)

    • GRUB 2 can display a countdown timer to provide visual feedback on the time remaining until the default selection is chosen. The timeout setting is enabled in /etc/default/grub (GRUB_HIDDEN_TIMEOUT_QUIET)

    • The user may display the menu by pressing any key.
      • Once the menu displays, the GRUB_TIMEOUT counter begins. Pressing any key stops the countdown.
        • If no key is pressed by the end of the timeout the default entry determined by settings in /etc/default/grub will be selected.

  • Hidden Menu Operations Not Expected (Abnormal):
    • The user may be able to display the menu in one or more of the following ways:
      • Holding down the SHIFT key early in the boot process 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.
      • Pressing the ESC key during a 3 second window as GRUB 2 runs.

Saved

  • Saving an OS can be achieved by running sudo grub-set-default if GRUB_DEFAULT=saved is set in /etc/default/grub. It may also be saved if GRUB_SAVEDEFAULT=true is also set in /etc/default/grub. In this case, the default OS remains until a new OS is manually selected from the GRUB 2 menu or the grub-set-default command is executed.

Last Boot Failed or Boot into Recovery Mode

If the last boot failed or after a boot into Recovery Mode the menu will be displayed until the user makes a selection. The timeout setting in /etc/default/grub do not apply in this case.

To change this behaviour, edit /etc/grub.d/00_header and change line 236 (this line is in the make_timeout() function) to

set timeout=0
  • For timeout=-1, there will be no countdown and thus the menu will display.

  • For timeout=0, menu will not display even for a failed startup.

  • For timeout>=1, menu will display for the specified number of seconds.

Run update-grub after the change have been made. Special thanks to McCunha on Ubuntu Forums for the above tip.

The above change, however, still causes grub to boot into text graphics mode. Thus, an additional change is required. Edit /etc/grub.d/10_linux and change line 188 to

set linux_gfx_mode=keep

Once again, run update-grub after the change has been made.

Password Protection

GRUB 2 is capable of password-protecting menu editing, access to the GRUB 2 terminal, and specific or all menuentries. It can also limit access to specific entries to specific users, and can encrypt plain-text passwords for increased security. Please refer to the Grub2/Passwords community documentation.

Custom Menu Entries

GRUB 2 allows users to create customized menu selections which will be automatically added to the main menu when sudo update-grub is executed. A 40_custom file is available in /etc/grub.d/ for use or to serve as an example to create other custom menus. Information on building and using custom menus in GRUB 2 is located in the Grub2/CustomMenus community document.

Troubleshooting

GRUB 2 provides excellent capabilities for troubleshooting and correcting boot problems from the boot menu or GRUB 2 terminal. Troubleshooting and recovery procedures detailed in the Grub2/Troubleshooting community document.

Included on the referenced page is a section on Selected_Problems_and_Bugs.

Fixing reboot/shutdown freezes

If your machine freezes upon attempts to shutdown or reboot, try modifying /etc/default/grub. Open the file with gksudo gedit /etc/default/grub (graphical interface) or sudo nano /etc/default/grub (command-line). Any other plaintext editor (Vim, Emacs, Kate, Leafpad) is fine too. Find the line that starts with GRUB_CMDLINE_LINUX_DEFAULT and add reboot=bios to the end. It should result in something like

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash reboot=bios"

if done properly. Then save the file, run sudo update-grub and reboot in any way that's possible for you. After that, reboots and shutdowns should start working properly.

With certain hardware (notably DELL) the kernel parameter that you add needs to be reboot=pci (instead of reboot=bios), the procedure remains the same.

Displays

GRUB 2 can do more than provide a simple black & white menu. Its menu can be customized by the user to present a more visually-appealing menu by changing font colors, backgrounds and resolutions.

For information on using splash images and changing font colors and menu resolutions, please refer to the Grub2/Displays community documentation.

Some people may have old bookmarks leading to this page in which case these links might be useful to them but it's better to link to the sub-pages directly as the sub-sections here are only here temporarily while this main page gets broken-up into digestible sub-pages.

GRUB 2 Splash Images

Grub2/Displays#Installing_Splash_Images

Set the splash image

Grub2/Displays#Installing_Splash_Images

Set menu font and highlight colors

Grub2/Displays#Setting_Menu_Font_Colors

Testing Fonts and Splash Images

Grub2/Displays#Testing_Fonts_and_Splash_Images

Changing Resolutions w/ Splash Images

Grub2/Displays#Changing_Menu_Resolutions

Creating User Splash Images

Grub2/Displays#Creating_User_Splash_Images

Resolution Settings

Grub2/Displays#Resolution_Settings

Image Creation and Setup

Grub2/Displays#Image_Creation_and_Setup

Themes

GRUB 2 Theming is still under development, as is integration with gfxmenu. Theme elements will include colors, fonts, progress indicators, menus, and labels. Both of these hold great promise but are not ready for release with Ubuntu 9.10. Check the Links section at the bottom of this page for references.

Installing/Reinstalling/Moving GRUB2

Please visit the Grub2/Installing community documentation. Information concerning upgrades from GRUB 0.97 (Legacy) to GRUB 2 is now located at Grub2/Upgrading.

Booting an ISO from a Menuentry

Ubuntu ISOs, as well as many utility ISOs and some other Linux operating systems, can be booted from a hard drive via a GRUB menuentry. This eliminates the need to create a CD/DVD and allows bootable image files to be stored only on the hard drive.

For information on this Grub 2 feature, please visit the Grub2/ISOBoot page.

Security

Preventing booting via Grub command-line

The ability to manually boot using the Grub command-line constitutes the biggest security risk in Linux. Any OS can be booted in this manner from any USB or CD/DVD drive, circumventing BIOS restrictions. Placing passwords or locking menu items (in the Grub configuration files) does not prevent a user from booting manually using commands entered at the grub command-line. (Once a foreign OS is booted, of course, it can be used to access any part of an unencrypted hard drive).

To prevent the command-line usage of Grub and require bootloading from menu options only, there are currently no options.

Miscellaneous

Booting from a serial console

If you want GRUB to operate over a serial line, you will need to uncomment GRUB_TERMINAL in /etc/default/grub and set it to serial (instead of the console default). The default serial console settings are to operate on the first serial port (ttyS0) at a 9600 bit/s transfer rate with 8 data bits, 1 stop bit and no parity.

If you want to use another serial port or if your console uses different settings, you must add a GRUB_SERIAL_COMMAND line to specify additional parameters to the serial command. The serial command in GRUB 2 uses the same syntax as its GRUB Legacy counterpart (documented here). For example, for a very common 9600 bit/s (baud) serial line with 8 data bits, 1 stop bit and no parity:

GRUB_SERIAL_COMMAND="serial --unit=0 --speed=9600 --word=8 --parity=no --stop=1"

An example is a 4800 bit/s (baud) serial line with 7 data bits, 1 stop bit and even parity:

GRUB_SERIAL_COMMAND="serial --unit=0 --speed=4800 --word=7 --parity=even --stop=1"

Booting FreeBSD, NetBSD, and multiboot OSes

Please refer to the article Grub2OtherOs

Links

  • GrubHowTo

  • Grub2/CustomMenus

  • Grub2/Displays

  • Grub2/Installing

  • Grub2/ISOBoot

  • Grub2/Setup

  • Grub2/Passwords

  • Grub2/Submenus

  • Grub2/Troubleshooting

  • Grub2/Upgrading

  • Grub 2 (Ubuntu Wiki)

External Links

  • GNU GRUB

  • GNU GRUB2 Manual

  • Illustrated Dual Boot HomePage

  • Mac OS X (XNU) Support

  • Mac OS X Boot Snow Leopard

  • GRUB Manual - Themes

  • The Definitive Guide to Theming GRUB2 by Towheed Mohammed

  • Analysis of problems with grub2 and RAID1 with workarounds

你可能感兴趣的:(Grub2)