ubuntu12.04 grub乱码解决方案及相关grub知识了解

ubuntu12.04升级之后移植乱码,原先还装了13.04,所以用13.04的grub不会乱码,但是删了13.04之后,12.04的grub乱码确实很难忍受,查了下官方文档,顺便摘抄一下相关知识。

原因可能是grub2被屏蔽掉了,我发现进入grub显示的是grub0.98,所以有了头绪,执行如下命令即可

sudo apt-get install grub-pc

sudo upgrade-from-grub-legacy
当然除了使用命令行外,你也可以使用图形工具Boot-repair

Getting Boot-Repair

1st option : get a CD including Boot-Repair

The easiest way to use Boot-Repair is to burn one of the following disks and boot on it.

  • Boot-Repair-Disk is a CD starting Boot-Repair automatically.

  • Boot-Repair is also included in Linux-Secure-Remix.

Remark : you can also install the ISO on a live-USB (eg via UnetBootin or LiliUSB or Universal USB Installer).

2nd option : install Boot-Repair in Ubuntu

- boot your computer on a Ubuntu live-CD or live-USB.

- choose "Try Ubuntu"

- connect internet

- open a new Terminal, then type:

sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update

- Press Enter.

- Then type:

sudo apt-get install -y boot-repair && (boot-repair &)

- Press Enter

Using Boot-Repair

  • launch Boot-Repair from either :
    • the Dash (the Ubuntu logo at the top-left of the screen)
    • or System->Administration->Boot-Repair menu (Ubuntu 10.04 only)

    • or by typing 'boot-repair' in a terminal
  • Then click the "Recommended repair" button. When repair is finished, note the URL (paste.ubuntu.com/XXXXX) that appeared on a paper, then reboot and check if you recovered access to your OSs. 

  • If the repair did not succeed, indicate the URL to people who help you by email or forum.

Advanced options

Warning: the default settings are the ones used by the "Recommended Repair". Changing them may worsen your problem. Don't modify them before creating a BootInfo URL, and asking advice on this thread.

http://pix.toile-libre.org/upload/img/1335263156.png http://pix.toile-libre.org/upload/img/1335263804.png http://pix.toile-libre.org/upload/img/1357337899.png http://pix.toile-libre.org/upload/img/1335263417.png http://pix.toile-libre.org/upload/img/1357348203.png

除此之外,官方文档还介绍另外一种修复方法:

  1. Update the repositories to get a list of the most recent packages available. Ensure you have a working Internet connection and access to the repositories. If you do not, STOP! 
    apt-get update
  2. The following command will completely remove grub-pc and grub-common. In GRUB 1.99 and later, grub-gfxpayload-lists will also be removed. 

    • You will be warned you are removing the bootloader. TAB to OK and press ENTER.

    apt-get purge grub-common
  3. Reinstall GRUB 2. When you run the following command:
    • You will be given the opportunity to add kernel options. TAB to OK. You will be given the opportunity to select the devices/drives on which to install GRUB 2. Select the appropriate drive(s) with the SPACEBAR. Normally you do NOT want to select any partitions. TAB to OK after selecting the drive(s), then press ENTER.
    apt-get install grub-pc
  4. If using the ChRoot procedure, Exit chroot then continue the ChRoot procedure at Step 13:

rrr

一下摘自ubuntu官方文档:


  • To upgrade to GRUB 2 install the grub-pc package. This package can be installed with Synaptic, if installed, or by running the command below. Once the installation begins, a simple script will guide the user through the rest of the installation:

    sudo apt-get install grub-pc
    • The package manager may complain about version conflicts and offer a solution to un-install GRUB. This may cause you to worry that it might become impossible for you to use the "Chain load to GRUB2 from legacy GRUB" option that is mentioned below. Selecting the un-install GRUB option here will not prevent you booting with the legacy menu described in step 4 below.

    1. At the first prompt, read the message, tab to select "OK", then press ENTER.

    ubuntu12.04 grub乱码解决方案及相关grub知识了解_第1张图片

    2. After selecting "OK" the next option allows the user to test GRUB 2 by adding an entry to their normal GRUB menu. Select "Yes" to place a Chainload option on the GRUB menu. When GRUB boots the next time, the user can select a normal GRUB entry or transfer control to GRUB 2 via the Chainload entry.

    3. The next step(s) will import sections from GRUB's menu.lst to the "Linux command line" (shown) and possibly the "Linux default command line". These entries allow the user to enter any special commands such as quiet, splash, noapic and others of that nature. GRUB 2 should automatically import these settings. Normally the user should accept any existing entries and TAB to "OK". If necessary, entries can be added later. Refer to the Configuring GRUB 2 section for more detail.

    ubuntu12.04 grub乱码解决方案及相关grub知识了解_第2张图片

info.pngWhen installing on a fresh drive, you may need to mark the selected partition as bootable. Although Linux does not use the boot flag, other OS's may require its presence.

  • 4. During installation the user will be prompted to provide the device(s) on which to install GRUB 2. Use the UP/DOWN keys to highlight a device (sda, sdb, etc), then the SPACE key to select the device. Once at least one device is selected, use the TAB key to highlight OK and press ENTERFailure to select a drive will result in a GRUB Error 15 and failure of the installation.

    ubuntu12.04 grub乱码解决方案及相关grub知识了解_第3张图片

    Read the information on the screen carefully. Selecting an option which includes a partition number (such as /dev/sda9 in the example) is not recommended. It will install GRUB 2 boot information directly on the Ubuntu partition. The location is specified by blocklists and GRUB 2 is more likely to break if boot files are moved for any reason.


    6. If the GRUB 2 menus function properly the user can upgrade to GRUB 2 at any time by running:

    sudo upgrade-from-grub-legacy

    Upon successful installation of GRUB 2, a message will inform the user that some GRUB Legacy configuration files are still present and can be removed. The instructions provide the following command, which must be run as 'root': rm -f /boot/grub/menu.lst* Use caution when running the rm command as "root". Incorrectly entering this command could result in permanently deleting necessary system files.

    Reverting to GRUB Legacy

    If a user chooses to return to GRUB legacy (0.97), these steps will remove GRUB 2 and install GRUB.

    via Boot-Repair graphical tool

    1. Run Boot-Repair from a liveCD or your installed session

    2. Click "Advanced options" --> GRUB options --> tick the "GRUB Legacy" option

    3. Click the "Apply" button



    grub2主要介绍:


    GRUB 2 Initial Installation

    GRUB 2 installation is automatically incorporated into a normal Ubuntu installation. Input required by the user for the initial setup is very simple. If the installation will use the entire disk no specific GRUB selections are necessary. If the user wants to use a specific partition for Ubuntu the user must select the Ubuntu partition and the drive on which to embed the bootloader information.

    The main GRUB 2 folder locations include /boot/grub/ and /etc/grub.d/. User preferences are stored in the /etc/default/grub file. The GRUB 2 menu is built by information contained in/boot/grub/grub.cfg.

    Reinstalling GRUB 2 from a Working System

    If Ubuntu is operating normally, boot into the working installation and run the following command from a terminal. 

    • X is the drive (letter) on which you want GRUB to write the boot information. Normally users should not include a partition number, which would produce an error message as the command would attempt to write the information to a partition. 

      sudo grub-install /dev/sdX  # Example: sudo grub-install /dev/sda

    This will rewrite the MBR information to point to the current installation and rewrite some GRUB 2 files (which are already working). Since it isn't done during execution of the previous command, running sudo update-grub after the install will ensure GRUB 2's menu is up-to-date.

    Fixing a Broken System

    If a system has become unbootable due to a GRUB 2 failure or corrupted MBR/boot sector, there are various methods for reinstalling GRUB 2. The simplest is listed first, followed by more complicated methods.

    via Boot-Repair Graphical Tool

    Boot-Repair is a GUI application which can fix a variety of GRUB 2 problems. It can be used either from a LiveCD, its own CD, or during a normal Ubuntu session. In addition to providing a simple user interface, Boot-Repair also includes the ability to run a script to gather details of the system which can be used for troubleshooting help on various forums and IRC channels.

    Please refer to the Boot-Repair community documentation for instructions on how to install and run this application.

    via GRUB2 Rescue mode

    GRUB 2 provides a rescue mode which allows the user to attempt to repair GRUB 2 from a special GRUB terminal during boot. Refer to the Grub2 Command_Line_and_Rescue_Mode section for information on GRUB terminal operations. 

    via the LiveCD terminal

    This method uses the terminal from a LiveCD. The user must know the device name/partition of the installed system (sda1, sdb5, etc), which is then mounted from the LiveCD. GRUB 2 files are copied from the LiveCD libraries to the proper locations and MBR. 

    This operation will write to the MBR and restore the modules and core.img to /boot/grub. It will not replace or restore grub.cfg or fix corrupted files. Boot the LiveCD, select "Try It", and open a terminal.

    • On systems with a separate /boot partition, that partition should be mounted to /mnt/boot. For instance:sudo mount /dev/sda6 /mnt/boot

    • grub-install will restore missing files in the grub folder but will not restore intentionally deleted or corrupted files. To accomplish these tasks GRUB 2 must be completely removed and reinstalled.

      sudo mount /dev/sdXY /mnt # Example: sudo mount /dev/sda5 /mnt
      sudo grub-install --boot-directory=/mnt/boot /dev/sdX # Example: sudo grub-install --boot-directory=/mnt/boot /dev/sda

      If Ubuntu is installed on B-tree file system i.e. btrfs then /boot changes to /@/boot in above commands such that :

      sudo grub-install --boot-directory=/mnt/@/boot /dev/sdX # Example: sudo grub-install --boot-directory=/mnt/@/boot /dev/sda

      For GRUB 2 version 1.98 and earlier (Ubuntu 10.04), the installation command is slightly different. Rather than designating the --boot-directory, the switch is --root-directory. In this case, the location changes to /mnt

      sudo grub-install --root-directory=/mnt /dev/sdX

    Purging & Reinstalling GRUB 2

    The difference between using the grub-install command and purging & reinstalling GRUB 2 is that the latter completely removes all GRUB 2 files and system settings. Purging GRUB 2 is also a method to reset all configuration settings to the default values on a working system. This may correct GRUB 2 failures due to corrupted files and improper configurations as well as for missing folders/files deleted by the user. The GRUB 2 packages removed and reinstalled via this procedure include grub-pcgrub-common and, in Grub 1.99 and later, grub-gfxpayload-lists.

    important.png During this operation the user will be temporarily left with no bootloader. Ensure you have a working Internet connection and access to the repositories before purging the GRUB 2 packages.

    This procedure can be accomplished very simply via Boot-Repair, or via the terminal procedure below.

    Remark: the Boot-Repair procedure will also remove custom files that may block the boot. It is also able to purge and reinstall GRUB2 for UEFI / LVM / RAID systems.

    via Boot-Repair Graphical Tool

    * Run Boot-Repair either from liveCD or from your installed session * Click "Advanced options" --> "GRUB options" tab * Tick the "Purge GRUB and reinstall it" option --> Apply

    via Terminal Commands

    The commands from a working Ubuntu installation or once within the ChRoot of a LiveCD are the same. 

    1. If using the LiveCD, complete the steps up to and including the sudo chroot command (Step 9) in the previous section.

    Run the following commands as root. If not in a ChRoot environment, preface each command with sudo.

    1. Update the repositories to get a list of the most recent packages available. Ensure you have a working Internet connection and access to the repositories. If you do not, STOP! 
      apt-get update
    2. The following command will completely remove grub-pc and grub-common. In GRUB 1.99 and later, grub-gfxpayload-lists will also be removed. 

      • You will be warned you are removing the bootloader. TAB to OK and press ENTER.

      apt-get purge grub-common
    3. Reinstall GRUB 2. When you run the following command:
      • You will be given the opportunity to add kernel options. TAB to OK. You will be given the opportunity to select the devices/drives on which to install GRUB 2. Select the appropriate drive(s) with the SPACEBAR. Normally you do NOT want to select any partitions. TAB to OK after selecting the drive(s), then press ENTER.
      apt-get install grub-pc
    4. If using the ChRoot procedure, Exit chroot then continue the ChRoot procedure at Step 13:


Main Directories & Files

The Menu: /boot/grub/grub.cfg

grub.cfg is the file which replaced GRUB 0.97's menu.lst. This file contains the GRUB 2 menu information but unlike GRUB's menu.lst the grub.cfgfile is not normally edited directly. The file is the result of various scripts which build the menu in sections, with each script responsible for a specific part of the menu. 

  • grub.cfg is updated by running the update-grub command as root. 

  • The grub.cfg file is not created or updated when the grub-install command is run.

    File Layout

    • Each section is clearly delineated with "(### BEGIN)" and references the script in the /etc/grub.d directory from which the information was generated. These sections are more fully explained in the Scripts section.

    User Settings: /etc/default/grub

    Scripts: /etc/grub.d/

    The scripts in this directory are read during execution of the  update-grub  command and their instructions are incorporated into /boot/grub/grub.cfg .

Script Descriptions

  • 00_header Sets environmental variables such system file locations, video settings, and previously saved entries. It also imports preferences stored in /etc/default/grub. Users normally do not need to make changes to this file. 

  • 05_debian_theme The settings in this file set the GRUB 2 background image, text colors, selection highlighting and themes. In the absence of a splash image, this file sets a monochromatic theme for the initial menu display. Information on how to set font renderings and splash images are discussed in the Grub2/Displays community documentation page. 

  • 10_linux Identifies kernels on the root device for the operating system in use and creates menu entries for these items. This includes the associated recovery mode option if enabled. In GRUB 1.99 and later, only the latest kernel is displayed on the main menu page, with additional kernels included in a submenu. See the Grub2/Submenus page for information on this feature.

    Note: For earlier versions of GRUB 2, all kernels residing in the boot folder are included on the main menu. To reduce the number of displayed kernels, remove the older kernels from the /boot folder or use one of the available GRUB 2 customization applications.

  • 20_memtest86+ Searches for /boot/memtest86+.bin and includes it as an option on the GRUB 2 boot menu. There is currently no line option to remove this entry from the menu. The display of memtest86+ can be inhibited by removing the executable bit from this file and runningupdate-grub.

    • sudo chmod -x /etc/grub.d/20_memtest86+
      sudo update-grub
  • 30_os-prober This script uses os-prober to search for Linux and other operating systems and places the results in the GRUB 2 menu. 

    1. The file's sections include options for Windows, Linux, OSX, and Hurd.
    2. Variables in this file determine the format of the displayed names in /boot/grub/grub.cfg and on the GRUB 2 menu. Users familiar with basic scripting can alter these variables to change the format of the displayed menu entries.

    3. The user can insert an entry into /etc/default/grub which disables this script (see Configuring GRUB 2). Removing the executable bit from the file will also prevent it from searching for other operating systems.

    4. By default os-prober ignores any disks that possess "dmraid" signatures, which can be listed via sudo dmraid -r -c (if installed).

    5. If the script finds another Ubuntu/Linux operating system, it will attempt to use the titles contained in the 10_linux section of that installation's grub.cfg file. If not found, the script will construct a menuentry from the actual boot information it locates.

  • 40_custom A template for adding custom menu entries which will be inserted into grub.cfg upon execution of the update-grubcommand. 

    1. The contents of this file, below the "exec tail -n +3 $0" line and the default comments, are imported directly into /boot/grub/grub.cfgwithout any changes. 

      1. As the comments at the top of the file state, add custom lines below those already contained in the 40_custom file. 

    2. The file can be renamed or copied. The file must be executable to be incorporated into the GRUB 2 menu.
      1. As mentioned in the introduction of this section, the name of the file determines the location of its contents in the GRUB 2 menu. As a general rule, if using numerals at the start it is advisable to allow the 00_header and 05_debian_theme scripts to be run before the first custom menu. These files contain no menuentries and thus will not interfere with a menuentry from a custom script with a lower priority.

Configuring GRUB 2

Configuration changes are normally made to /etc/default/grub and to the custom files located in /etc/grub.d. Any changes made directly to the/boot/grub/grub.cfg are overwritten whenever update-grub is executed either by the user or when called automatically by various system functions.

After editing /etc/default/grub or the scripts in the /etc/grub.d folder the user should run sudo update-grub to incorporate the changes into the GRUB 2 menu.

/etc/default/grub

This file contains basic settings which would be considered normal for the user to configure. Options include the time the menu is displayed, the default OS to boot, etc. The default options included in file upon installation are only a small number of the variables which GRUB 2 can recognize. The GRUB 2 defined variables can be identified using the command previously documented in the GRUB 2 Settings section or at the end of this section.

In addition to the defined variables, the user may introduce self-created variable(s) and use them in the scripts contained in the /etc/grub.dfolder. 

Editing the File

The grub file is a system file, therefore any editing must be done by a user with 'Administrator/root' privileges. The file is a simple text file and can be edited by any text editor. The default text editor in Ubuntu is Gedit, and the file can be edited with the following command. "gksu" is the graphical equivalent of "sudo" and the "&" allows the terminal to be used to update GRUB 2 once the user saves the file.

  • gksu gedit /etc/default/grub &

After making changes and saving the file, the GRUB 2 menu must be updated to include the changes by running:

  • sudo update-grub
  • GRUB_DEFAULT=

    • Sets the default menu entry. Entries may be numeric, a complete menuentry quotation, or "saved"
      1. GRUB_DEFAULT=0 Sets the default menu entry by menu position. The first "menuentry" in grub.cfg is 0, the second is 1, etc.

        • Note: Grub 1.99 introduces a submenu menu structure. For a menu item in a submenu, the entry becomes a two-digit entry. The first entry is the position of the submenu title in the main menu. The second entry is the position within the submenu. If the submenu is the 3rd entry in the main entry, and the user wishes to boot the first entry in the submenu, it would be designated as "2>0". See the community documentation for a fuller explanation: Grub2/Submenus.

      2. GRUB_DEFAULT="xxxx" An exact menu entry, including the quotation symbols, may also be used. In this case, location in the menu will not matter. 

        • Example: GRUB_DEFAULT="Ubuntu, Linux 2.6.31-9-generic"

        • Example (Submenu Entry): GRUB_DEFAULT="2>Ubuntu, Linux 2.6.38-8-generic"

      3. GRUB_DEFAULT=saved The information in this section applies to GRUB 1.98 and later. The "saved" entry enables the "grub-reboot" and "grub-set-default" commands to set the default OS for future boots.

        • The default OS for future boots will not be set merely by selecting an OS when booting.

        1. grub-set-default Sets the default boot entry until changed.

          • The format is sudo grub-set-default X, with X being the menu entry position (starting with 0 as the first entry) or the exact menu string.

            • Example: sudo grub-set-default 3

            • Example: sudo grub-set-default "Ubuntu, Linux 2.6.32-15-generic"

          • To obtain the existing menu entry choice number (starting from 0) or the menu entry "string", run:
            • grep menuentry /boot/grub/grub.cfg

        2. grub-reboot This command sets the default boot entry for the next boot only. The format of the command is the same as for grub-set-default (see above).

  • GRUB_HIDDEN_TIMEOUT=0

    • Wait this many seconds for the user to press a key. During this period no menu is shown unless the user presses a key. If no key is pressed, control is passed to GRUB _TIMEOUT when the GRUB_HIDDEN_TIMEOUT expires. See the note regarding bugs to this feature at the end of this section.
      • The developers envisioned using this setting with a GRUB_TIMEOUT value of 0. This would give users a period of time (GRUB_HIDDEN_TIMEOUT) to display the menu by pressing a key, after which the system would boot without the menu being displayed (GRUB_TIMEOUT=0). 
    • GRUB_HIDDEN_TIMEOUT=0

      • No menu is displayed. The system is immediately booted to the default OS.
      • This is the default setting with only one identified operating system.
        • To display the menu under this condition, place a # symbol at the start of the line and ensure the GRUB_TIMEOUT setting is a positive integer.

      • If the value is set to 0, a keystatus check is performed to determine if the SHIFT key is depressed. If GRUB 2 determines the SHIFTkey is depressed during the boot process, the menu will be displayed. This gives the user a method of interrupting an automatic boot which would normally not display the menu.

    • GRUB_HIDDEN_TIMEOUT=X

      • X is a positive integer (e.g. 1, 5, 10, etc)

      • The boot process will pause and display a blank screen or the designated splash image for X seconds. At the end of the time period, the system will boot. No menu will be displayed.

      • While GRUB_HIDDEN_TIMEOUT is active, the menu can be displayed by pressing any key.
    • GRUB_HIDDEN_TIMEOUT= 

      • No value entered after the = sign

      • The menu will be displayed for the number of seconds designated by GRUB_TIMEOUT.
  • GRUB_HIDDEN_TIMEOUT_QUIET=true

    • Determines whether a countdown timer is displayed on a blank screen when using the GRUB_HIDDEN_TIMEOUT feature.

      • true No countdown is displayed. The screen will be blank.

      • false A counter will display on a blank screen for the duration of the GRUB_HIDDEN_TIMEOUT value.

      Note: There is a longstanding confirmed bug on the hidden menu feature in GRUB 1.97 to GRUB 1.99. The menu may not hide as specified in the description on this page. While editing the 30_os-prober script can fix this issue, it is beyond the scope of this page.

  • GRUB_TIMEOUT=10

    • Sets the time period in seconds for the menu to be displayed before automatically booting unless the user intervenes.
      • This instruction begins at the expiration of GRUB_HIDDEN_TIMEOUT. 
      • Setting this value to -1 will cause the menu to display until the user makes a selection.
      • On a single OS system, by default this setting is not used and the menu will not display. 
        • To display the menu on each boot:
          • Place a comment symbol (#) in front of the GRUB_HIDDEN_TIMEOUT entry

          • Make the GRUB_TIMEOUT value 1 or higher.
  • GRUB_DISTRIBUTOR=lsb_release -i -s 2> /dev/null || echo Debian

    • Retrieves the descriptive name in the menu entry. (Ubuntu, Xubuntu, Debian, etc.)
  • GRUB_CMDLINE_LINUX

    • Entries on this line are added to the end of the 'linux' command line (GRUB legacy's "kernel" line) for both normal and recovery modes. It is used to pass options to the kernel.
  • GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

    • 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.
      • To view 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".
  • #GRUB_TERMINAL=console

    • Uncomment to disable graphical terminal. This may provide help if the GRUB 2 menu is too large or unreadable. It also may help when using the GRUB_HIDDEN_TIMEOUT feature. 

grub常见问题及命令行的使用:
  • grub> prompt: GRUB 2 loaded modules but was unable to find the grub.cfg file.

  • grub rescue> prompt: GRUB 2 failed to find its grub folder, or failed to load thenormal module.

  • grub>: - The grub prompt on a blank screen.

    • GRUB 2 has found the boot information but has been either unable to locate or unable to use an existing GRUB 2 configuration file (usuallygrub.cfg).

  • grub rescue>: - The rescue mode.

    • GRUB 2 is unable to find the grub folder or its contents are missing/corrupted. The grub folder contains the GRUB 2 menu, modules and stored environmental data.

  • GRUB - a single word at the top left of the screen, with no prompt and no cursor.

GRUB 2 Terminal Commands

info.png Here are some useful tips and features for use with the GRUB 2 terminal:

  • Turning off the splash image. This may make viewing the terminal easier. Press c at the GRUB 2 menu to get to the command line and then type: set color_normal=white/blue or the color combination you wish to use. "black" as the second entry retains the menu's transparency and should be avoided as a selection if the user wants to work with a solid background color.

  • TAB completion. This feature is very handy. At any point, pressing the TAB key may complete an entry, if the element is unique, or display available options. Typing a few additional characters and pressing TAB again may allow tab completion to finish the entry.

  • set pager=1 To prevent text from scrolling off the screen, type set pager=1

  • Help. Type help to view a list of all the commands. Type help x to view help commands beginning with the letter "x". Tab completion and using the up arrow to repeat commands work the same as in a normal terminal.

Normal

When GRUB 2 is fully functional, the GRUB 2 terminal is accessed by pressing c. If the menu is not displayed during boot, hold down the SHIFT key until it appears. If it still does not appear, try pressing the ESC key repeatedly. 

From a GRUB 2 terminal with the grub> prompt, a wide variety of commands are available. 

  • A few of the more important commands:

    Command

    Result / Example

    boot

    Initiate the boot sequence, also F10 or CTRL-x

    cat

    Display the contents of readable files; cat (hd0,1)/boot/grub/grub.cfg

    configfile

    Load a GRUB 2 configuration file such as grub.cfg; configfile (hd0,5)/boot/grub/grub.cfg

    initrd

    Loads the initrd.img, necessary for booting; initrd (hd0,5)/initrd.img

    insmod

    Loads a module; insmod (hd0,5)/boot/grub/normal.mod, or insmod normal

    linux

    Loads the kernel; insmod /vmlinuz root=(hd0,5) ro

    loop

    Mount a file as a device; loopback loop (hd0,2)/iso/my.iso

    ls

    Lists the contents of a partition/folder; lsls /boot/grubls (hd0,5)/ls (hd0,5)/boot

    lsmod

    List loaded modules

    normal

    Activate the normal module, if loaded

    search

    Search for a device. Type help search for the available options.

    set

    Review current settings, or set XXX to set a variable such as colors, prefix, root

    vbeinfo

    Display GRUB 2 available resolutions

    To view the complete command list, type help. For details on a particular command, type help [command] 

grub>

When GRUB 2 is unable to boot or display the GRUB 2 menu the system will be left at a GRUB 2 terminal if possible. If the system boots to thegrub> prompt, the usual modules and commands are normally available. The user may need to manually load modules using the insmod [module] command before some commands will work.

grub rescue>

In the grub rescue mode, only a limited set of commands are available. These commands are sufficient to investigate the contents of the drives, set prefix (path to the grub folder) and root (partition), load modules and boot.

The rescue mode provides fewer commands than the normal GRUB prompt line, but also provides these additional commands:

  • Command

    Result / Example

    dump

    Clears memory

    exit

    Exit GRUB 2

    normal

    Return to the standard "grub>" mode if possible.

Among the commands which can be used in the grub rescue mode:

  • boot

    cat

    chain

    help

    insmod

    linux

    ls

    multiboot

    normal

    search

    set

    unset

General Troubleshooting Preparation

Boot-Repair

The majority of this guide is devoted to working with the GRUB 2 terminal, which is used to enter commands to attempt to repair a broken bootloader. info.png Boot-Repair is a GUI application which can automatically analyze boot problems and select the proper course of action to repair them. Its simple interface provides beginners as well as experienced Linux users an easy method of repairing the majority of GRUB 2 problems.

Additionally, Boot-Repair contains an automated method to run a boot info script which performs a series of tests and provides a file containing much valuable information regarding the status of the computer's operating and boot systems. This file can be inspected by community helpers on forums or IRC channels to help diagnose boot problems.

Boot-Repair can be run from a LiveCD or an operating Linux system. To download the package run the following commands:

  • sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update
    sudo apt-get install -y boot-repair && boot-repair

Visit the Boot-Repair community document for more information.

Boot-Repair is available from the Launchpad repositories and can be run from a CD or a working Linux OS.

Search & Set

A great many boot problems are due to incorrect paths to required files. The GRUB 2 terminal, in either 'failure' mode, provides a robust ability to search hard drive(s) and partitions and to inspect their contents. 

In order to boot successfully, the root, prefix, linux and initrd variables must be correct. The user must verify the paths and names of these items. If they are incorrect, use the commands below to find and fix them. GRUB 2 variable settings can be viewed with the set command.

In the following examples, X is a hard drive number; Y is a partition number. If a command example includes either of these replace them with the appropriate value.

In the graphic below, the text in red are commands to be entered by the user, and text in green is the output of the command on an operating system (i.e. what you would like to see if your Ubuntu installation is on sda1).

ubuntu12.04 grub乱码解决方案及相关grub知识了解_第4张图片

  • The first hard drive is 0. The first partition is 1. Thus sda1 becomes (hd0,1), sdb5 is (hd1,5).

  • Use the ls command in the follow manner

  • "Tab completion" may work - enter part of the filename and press the TAB key. 

Command

Purpose

ls

Search the entire computer for devices and partitions: (hd0) (hd1) (hd0,1) (hd0,5) (hd1,1)

ls /

Search the root directory of the device designated as root (use the set command to check root).

ls (hdX,Y)

View information about a partition - format, size, UUID, etc.

ls (hdX,Y)/

View the root contents of a partition. Use this command to look for the presence of vmlinuz and initrd.imgsymlinks

ls (hdX,Y)/boot/

View the contents of a folder

ls (hdX,Y)/boot/

Inspect the /boot folder. It should contain the actual kernel (linux-3.2...) and initrd image (initrd.img-3.2....)

ls (hdX,Y)/boot/grub/

Inspect the /boot/grub folder. It should contain grub.cfg and many *.mod files. If looking for a specific file, include the name in the search to limit the number of returns. If available, the command set pager=1 will also limit returns to a single screen.

What to Look For

Where It Should Be (Default Installation)

Specific / General Search Example

grub.cfg

(hdX,Y)/boot/grub/ or /boot/grub/

ls (hdX,Y)/boot/grub/grub.cfg or ls /boot/grub/

vmlinuz

(hdX,Y)/ or /

ls (hdX,Y)/vmlinuz or ls /vmlinuz or ls /

linux-3.2.0-14*

(hdX,Y)/boot/ or /boot/

ls (hdX,Y)/boot/vmlinuz-3.2.0-14

initrd

(hdX,Y)/ or /

ls (hdX,Y)/ or ls /initrd

initrd.img-3.20-14

(hdX,Y)/ or /boot/

ls (hdX,Y)/boot/initrd.img-3.20-14 or ls (hdX,Y)/boot/

* Note: Use the full kernel name, including -generic, when searching or setting a kernel variable. Using the "TAB completion" technique may eliminate some typing and be more accurate if available.

Use the following commands to set these parameters (if incorrect). Substitute the correct value for X and Y. (Example: set root=(hdX,Y) becomesset root=(hd0,5) )

Task

Command

Notes

Set the prefix

set prefix=(hdX,Y)/boot/grub

Use the actual location of the grub folder

Set root

set root=(hdX,Y)

 

Set the kernel

linux /vmlinuz root=/dev/sda1 ro

Set the kernel if the symlink vmlinuz exists in /

Set the kernel

linux (hdX,Y)/boot/vmlinuz-3.0.2-14 root=/dev/sda1 ro

Set the kernel itself

Set the initrd image

initrd /initrd.img

Set the initrd image if the symlink exists in /

Set the initrd image

initrd (hdX,Y)/boot/initrd.img-3.0.2-14

Set the initrd image itself

Specific Troubleshooting

Use the section below based on the type of GRUB 2 terminal prompt displayed on the monitor. 

important.png The commands which follow assume you have determined the proper parameters for prefixrootlinux, and initrd. Review the Search & Set section of this page for guidance .

grub>

Terminal Display: The GRUB 2 header/version information and a grub> prompt.

  • ubuntu12.04 grub乱码解决方案及相关grub知识了解_第5张图片

If GRUB 2 leaves you at the grub> prompt, it has normally found the grub folder and loaded at least some basic modules. The configuration file (grub.cfg) may be missing, misnamed, or corrupted.

Quick Fix:

  • Use the Search & Set section to confirm the correct paths are set. Inspect the contents of the /boot/grub folder. Look for the grub.cfg file. It could be misnamed or missing. If not located in /boot/grub, use the ls command look for another .cfg file or look in /boot or other locations. If you locate the correct .cfg file:

  1. configfile /boot/grub/grub.cfg or configfile (hdX,Y)/boot/grub/grub.cfg

    If a .cfg with another name is located, substitute its path/filename in the command. 

    If the configuration file is loaded and not corrupt, when the above command is executed the GRUB 2 menu should appear and the user can make a selection to boot. Once booted into the system, correct the filename or move the configuration file to its proper location. Runupdate-grubIf the configuration file is not found, a message will be generated and the user must enter the boot commands manually.

Extended Fix:

The following commands should set the root and prefix paths and load the kernel and initrd image. With this information, GRUB 2 does not need a configuration file and should be able to boot the system if the only problem was a corrupted or missing menu. 

  • Press ENTER after completing each line. Some entries will not provide feedback. This is normal.

  • If a "file not found" or similar error message is displayed while running these commands, ensure you are using the correct X,Y values and the correct paths are set.

1. set root=(hdX,Y)

Confirm the correct X,Y values and press ENTER.

Example: If the Ubuntu system is on sda5, enter: set root=(hd0,5)

2. linux /vmlinuz root=/dev/sdXY ro

Example: linux /vmlinuz root=/dev/sda5 ro

If the vmlinuz symlink does not exist, use the full path to the kernel in /boot

Example: linux /boot/vmlinuz-3.2.0-14-generic root=/dev/sda1 ro

If successful, after ENTER there will be a slight delay and no messages.

  • Wubi users only - substitute these commands in Steps 1 and 2: 

  • 1.set root=(loop0)

    2. linux /vmlinuz root=/dev/sdXY loop=/ubuntu/disks/root.disk ro

3. initrd /initrd.img

Selects the latest initrd image.

If the vmlinuz symlink does not exist, use the full path to the initrd image in /boot

If successful, after ENTER there will be a slight delay and no messages.

4. boot

Boot to the latest kernel on the selected partition.

If the system fails to boot:

  1. Attempt to find the GRUB 2 configuration file. The normal name is grub.cfg file. If not found, look for a misnamed .cfg file or in alternate locations. The menu configuration file may contain settings required for a successful boot (such as non-standard kernel options) even if the paths/filenames are correct. 

    1. Use the set command to confirm the correct root and prefix settngs. Also inspect the folders' actual contents with the ls command. The grub prompt normally means the GRUB 2 folder is intact but doesn't guarantee the integrity of other system files.

    2. root= should point to the drive and partition on which Ubuntu is installed. If you use the ls / command you should see the main Ubuntu system folders. 

If the system boots, please refer to the [[#Post-Boot Follow Up|Post Boot Follow Up] section

If the system fails to boot, proceed to the [#grub rescue|grub rescue]] section for more detailed troubleshooting options.

grub rescue>

The GRUB 2 rescue mode is a major enhancement to the GRUB bootloader. The presence of the grub rescue> prompt signifies that GRUB 2 has failed to find the grub folder, the grub.cfg file, and/or the associated modules. The rescue prompt is presented so the user can provide the path to the grub folder, load the necessary modules, and provide the proper boot commands.

  • g2_grub_rescue.png

A common reason for the grub rescue> prompt is an incorrect path to the grub folder. Reasons for the prompt also include a failure to update GRUB 2 after certain system or partition operations, improper designation of the grub folder location, missing linux or initrd.img symlinks in /, or a failed installation.

To successfully boot from the grub rescue> prompt:

  • The grub folder must exist and contain the necessary GRUB 2 files and modules.

  • The proper paths must be set via the set prefix command.

    • Many GRUB 2 commands will not work until the correct path is set.

      If the path to the grub folder (normally /boot/grub) is not correct, an unknown command or file not found message is likely.

  • The necessary modules must be loaded.
    • The kernel cannot be loaded until the 'linux' module is loaded.
  • A Linux kernel and initrd.img must be located and loaded.

Use the General Troubleshooting Preparation section to locate the correct partitions and file locations. Once the user has confirmed the paths and existence of the proper folders using the Search & Set section, run the following commands:

1. set prefix=(hdX,Y)/boot/grub

Use the values determined earlier.

Example: If the Ubuntu system is on sda5, enter: set prefix=(hd0,5)/boot/grub

2.* set root=(hdX,Y)

Confirm the correct X,Y values and press ENTER.

Example: If the Ubuntu system is on sda5, enter: set root=(hd0,5)

3. insmod normal

Load the normal module.

If the module loads there will be no message.

If the module fails to load, try the full path: insmod (hdX,Y)/boot/grub/normal.mod

4. normal

Transition to the normal GRUB 2 mode with increased functionality.

If the module loads there will be no message.

If the module loads, HELP, TAB completion and command recall using the UP/DN keys should be available.

5. set

(Optional) Review the current settings.

6. insmod linux

Load the linux module. An error message usually means the path is incorrect.

7.* linux /vmlinuz root=/dev/sdXY ro

Selects the latest kernel.

Example: linux /vmlinuz root=/dev/sda5 ro

If the vmlinuz symlink does not exist in /, use the full path to the kernel in /boot

Example: linux /boot/vmlinuz-3.2.0-14-generic root=/dev/sda1 ro

8. initrd /initrd.img

Selects the latest initrd image.

If the initrd symlink does not exist in /, use the full path to the initrd image in /boot

If successful, after ENTER there will be a slight delay and no messages.

9. boot

Boot to the latest kernel on the selected partition.

  • * Wubi users only - substitute these commands in Steps 2 and 7: 

  • 1.set root=(loop0)

    2. linux /vmlinuz root=/dev/sdXY loop=/ubuntu/disks/root.disk ro

Some additional considerations:

  • The current prefix and root settings may be checked at any time with the set command. To remove a setting, use the unset command.

    • Example: unset prefix

  • Modules must be loaded before they can be used. If a module has not been loaded a unknown command error is displayed. If an incorrect path is specified, a file not found error message may be displayed.

  • The linux module must be loaded to be able to load both the kernel and the initrd image unless the normal module is loaded first.

  • If the modules cannot be found in the /boot/grub folder, the user may be able to load them from the /usr/lib/grub/i386-pc folder. The address if Ubuntu was installed on sda1 would be (hd0,1)/usr/lib/grub/i386-pc and the command would be:

    • insmod (hd0,1)/usr/lib/grub/i386-pc/normal.mod

Refer to the Post Boot Follow Up section if the system successfully boots.

GRUB

Presence of the word GRUB at the top left of the monitor with no blinking cursor indicates that GRUB 2 can not even find the Master Boot Record (or equivalent) information. Thus, the core.img file, the /boot and /grub folder locations and contents are completely unknown to GRUB 2.

  • g2_grub_fail.png

The seriousness of the problem cannot be assessed without the use of another operating system or an Ubuntu LiveCD or equivalent. The Ubuntu partition should be mounted for inspection and the user can then check to see if the system files are intact. If so, the user can use the ''ChRoot''procedure from the LiveCD to reinstall GRUB 2 and rewrite the information to the MBR.

Details on this procedure are contained in the Grub2/Installing community documentation.

Editing the GRUB 2 Menu During Boot

Following a failed boot, GRUB 2 is designed to display the GRUB 2 menu on the next boot and await user input. This will happen even if the user has set the system to boot without displaying the GRUB 2 menu. This provides the user the opportunity to select a different menu option or edit a menuentry to change boot parameters. While this can cause problems on a server, for most users it is a benefit which will prevent constant rebooting during unmonitored operations.

  • ubuntu12.04 grub乱码解决方案及相关grub知识了解_第6张图片

In addition to its troubleshooting benefits, pre-boot editing of the GRUB 2 menu also allows users to add or remove kernel options, change operating modes, and accomplish other tasks such as testing fonts and splash images. For users who like to experiment, the settings altered via the GRUB 2 terminal are non-persistent and won't affect future boots.



相关网址:
https://help.ubuntu.com/community/Grub2/Setup#File_Structure(grub文件结构)
https://help.ubuntu.com/community/Grub2(grub全程指导)
https://help.ubuntu.com/community/Grub2/Passwords(设置grub密码)
https://help.ubuntu.com/community/Grub2/CustomMenus(菜单项设置)
https://help.ubuntu.com/community/Grub2/Troubleshooting(相关问题解决思路)
https://help.ubuntu.com/community/Grub2/Upgrading(更新指导)
https://help.ubuntu.com/community/Grub2/Installing(安装指导)


你可能感兴趣的:(linux)