grub2 Essentials - Amendment (change)

grub2 Basics Tutorial - revised

Transfer from ( )

grub2 Basics Tutorial - revised



1, grub2 new features

Second, grub2 installation and startup

3, grub2 profile Xiangjie

Fourth, the script changes

5, using the common errors and repair methods grub2

1, grub2 new features

1, grub2 and grub a lot of different places:
a) graphical interface
b) use the module mechanism, through the dynamic loading of modules to extend the functionality needed
c) support for scripting languages, such as conditional, loop, variables and functions
d) support the rescue mode, can be used in case the system does not boot
e) international language. Including support for non-ASCII character sets and similar news categories gettext, fonts, graphics console, etc.
f) has a flexible command line interface. If no configuration file exists, GRUB will automatically enter the command mode
g) for the file system, files, equipment, drives, terminals, commands, partition table, os loder modular, hierarchical, object-based framework
h) support for multiple file system formats
i) the equipment has been installed to access the data
j) Support automatic decompression


Equipment naming
grub2 use the same fd that floppy, hd hard drive (with IDE and SCSI hard drives). Equipment is the beginning number from 0, partitions start at 1 is the main area from 1-4, logical partitions start from 5, while the grub partition numbers start from 0. Here are a few examples:
(Fd0): that the floppy disk
(Hd0, 1): that the first BIOS hard disk partitions 1
(Hd0, 5) / boot / vmlinuz : that the first hard drive in the BIOS the first logical partition boot directory under the vmlinuz file

Second, grub2 installation and startup

1, the installation ubuntu9.10 time, grub is installed by default in the first hard disk (hd0) in the mbr, is actually written to the hard disk boot file boot.img mbr, of course, users can choose not to write but to write to the hard disk mbr linux partition boot sector. Start to find the information provided under the mbr boot partition, grub loads the core files sub-region core.img and configuration files grub.cfg, enter the selection menu screen, the menu screen, press the down arrow to select the system menu item, press Enter into the option.

grub2 Essentials - Amendment (change)

Press e to enter has been selected menu edit mode, the edit mode, the upper and lower left and right arrows to move the cursor, enter key for line, ctrl + x to edit the contents of the boot, ctrl + c into the command line, press Esc exit, return to the menu item.

In need of additional kernel boot parameters, such as when to add acpi = off, move the cursor to linux this line Finally, add a space after the splash and then enter the acpi = off, then press ctrl + x start.

grub2 Essentials - Amendment (change)

By c into the command line, press Tab key to check all available commands.

grub2 Essentials - Amendment (change)

State of the command line, you can add or remove as necessary the appropriate module, can also be used to start the menu does not appear in the system.

For example, in the first partition on first hard drive with windows xp system, but the menu did not show up, we can state the command line, enter the command to start:

grub> set root = (hd0, 1)

GRUB> chainloader +1

grub> boot

Another example is the first start the second logical partition on the hard disk ubuntu system:

grub> set root = (hd1, 5)

grub> linux / boot / vmlinuz-xxx-xxx root = / dev/sdb5

grub> initrd / boot / initrd.img-xxx-xxx

grub> boot

Kernel vmlinuz and initrd.img which the version number of press Tab key to automatically check available.

2, Here are some commonly used under the command grub2

1) help
See the command usage, such as
Help search : View search command usage details
2) set

Set variable value, such as

set default = 0
set timeout = 5

set root = (hd0, 3), etc.

Need to call the variable value of AA, use $ (AA), as set root = (hd1, 1), then $ (root) = (hd1, 1)

3) default

Defines the default boot operating system. 0 indicates that the first operating system, one that the first two, and so on

4) timeout

Defined in time, the user does not press a key on the keyboard to automatically guide the default specified in the operating system.

5) root

Specify the partition used to start the system.

6) insmod and rmmod

Add or remove a module, such as

insmod JPEG

insmod PNG

insmod NTFS

rmmod PNG


7) drivemap
drivemap compatible with grub the map, used only from the first hard disk (hd0) boot the system, such as win2000 xp 2003, for example to add a second hard disk first partition on the xp system:
menuentry "Windows XP" (
set root = (hd1, 1)
drivemap-s (hd0) $ (root)
chainloader +1

8) ls
The current list of all equipment. Such as (hd0), (hd0, 1), (hd0, 5), (hd1), (hd1, 1), (hd1, 2), etc.

Details of all current equipment. For the district, will show its label and uuid.
ls /
Lists the current set of files under root partition
ls (hd1, 1) /
List (hd1, 1) partition root directory file
9) search
search-f / ntldr
Listed in the root directory partition contains ntldr file, return to the partition number
search-l LINUX
Search label is a LINUX partition.
search-set-f / ntldr
Search the root directory partition contains ntldr file and set to root, note that if the ntldr file that contains multiple partitions,

set useless.

10) loopback
loopback command can be used to build playback devices, such as
loopback lo0 (hd1, 1) / abc.iso
Lo0 device can be used to access abc.iso in the content, for example, can abc.iso start in the floppy disk image
loopback lo0 (hd1, 1) / aa.iso
linux (lo0) / memdisk
initrd (lo0) / abc.img
To remove a playback device, you can use the-d parameter:
loopback-d lo0
11) pager
Page Display.
set pager = 1
Full page display when the pause, press space to continue
set pager = 0
Pagination Cancel
12) linux
Linux command to replace grub with the kernel command

13) chainloader

Call another starter, such as

chainloader (hd0, 1) +1

Call the first hard disk partition boot sector within the first starter, can be windows or linux boot device

3, grub2 profile Xiangjie

grub2 use grub.cfg for the configuration file, configuration file contains the following basic elements: (red is explained in part)

# # # BEGIN / etc/grub.d/00_header # # #

# Load variable, if grubenv saved variables, then start loading
set default = "0"

# Set the default boot entry, the default value of 0
insmod ext2

# Insert the file system support module, in addition to the partition for boot, the other partition format can menuentry add below
set root = (hd0, 8)

# Specifies the system root partition, that is / partition
search-no-floppy-fs-uuid-set 2d61e5f9-1d2a-4167-a6f1-b991ba00878b

# Specify the uuid = 2d61e5f9-1d2a-4167-a6f1-b991ba00878b partition as root partition, if the partition number in front of (hd0, 8) of the # uuid uuid consistent with here, as these two roles, and if not, then the specified uuid of work.

if loadfont / usr/share/grub/unicode.pf2; then

# Set terminal font, unicode.pf2 support Chinese characters display
set gfxmode = 640 × 480

# Set the display resolution, default is 640 × 480, can be 800 × 600,1024 × 768, with the proposed size of the image you want to set the same
insmod gfxterm

# Insert terminal module gfxterm, support Chinese character display and support for 24-bit image
insmod vbe

# Insert the vbe module, GRUB2 introduce modular system, to use it, need to join here
if terminal_output gfxterm; then true; else
# For backward compatibility with versions of terminal.mod that don't
# Understand terminal_output
terminal gfxterm

# Set terminal gfxterm grub2
set timeout = 10

# Set the default wait time before the start, the default is 10 seconds
# # # END / etc/grub.d/00_header # # #

# # # BEGIN / etc/grub.d/05_debian_theme # # #

set menu_color_normal = white / black

# Set the menu font and background color
set menu_color_highlight = black / blue

# Set the selected item font and background color

# If you use the default, the blue background will be completely blocked, need to modify the blue to black, the background image to display
# # # END / etc/grub.d/05_debian_theme # # #

# # # BEGIN / etc/grub.d/10_linux # # #

# 10_linux for the system to automatically add the current root partition linux boot entry

# For each menu item to include menuentry double quotes "" and the only complete braces (), otherwise the menu does not appear
menuentry "Ubuntu, Linux 2.6.31-10-generic" (
set quiet = 1
insmod ext2
set root = (hd0, 8)
search-no-floppy-fs-uuid-set 2d61e5f9-1d2a-4167-a6f1-b991ba00878b

# Phrase and set root = (hd0, 8) the same effect can be removed first, the two inconsistent sentence subject to
linux / boot/vmlinuz-2.6.31-10-generic root = UUID = 2d61e5f9-1d2a-4167-a6f1-b991ba00878b ro quiet splash

# Do not like to see a long list of, roo = UUID =*** available root = / dev/sda8 (/ partition partition number) instead of
initrd / boot/initrd.img-2.6.31-10-generic

# # # END / etc/grub.d/10_linux # # #

# 20_memtest86 + for the system to automatically add a menu item memory test
# # # BEGIN / etc/grub.d/20_memtest86 + # # #

menuentry "Memory test (memtest86 +)" (

linux16 / boot/memtest86 +. bin


menuentry "Memory test (memtest86 +, serial console 115200)" (

linux16 / boot/memtest86 +. bin console = ttyS0, 115200n8


# # # END / etc/grub.d/20_memtest86 + # # #
# 30_os-prober or 30_others for the system to automatically find and add other system menu items, according to windows, linux, macos order

# Find and add support the identification of windows 7
# # # BEGIN / etc/grub.d/30_os-prober # # #
# # # END / etc/grub.d/30_os-prober # # #
# # # BEGIN / etc/grub.d/30_otheros # # #
# This entry automatically added by the Debian installer for a non-linux OS
# On / dev/sda1
menuentry "Microsoft Windows XP Professional" (
set root = (hd0, 1)
search-no-floppy-fs-uuid-set e852-230b
drivemap-s (hd0) $ root

# On to ntldr guidance systems such as win2000, xp, win2003, its guidance mechanism can only start from the first hard drive, the system will automatically add the # map command, on the vista and there is no phrase command win7
chainloader +1


# # # END / etc/grub.d/30_otheros # # #
# 40_custom custom startup items, such as the start cdlinux
# # # BEGIN / etc/grub.d/40_custom # # #

menuentry "CDLinux" (

set root = (hd0, 8)

linux / CDlinux / bzImage root = / dev/ram0 vga = 791 CDL_LANG = zh_CN.UTF-8

initrd / CDlinux / initrd

# # # END / etc/grub.d/40_custom # # #

Personalized custom configuration file, you can join the background image, the use of Chinese characters, unique to the splash screen, instead of monotonous black, white and blue. The following is a splash screen by landscaping:

grub2 Essentials - Amendment (change)

Fourth, the script changes

How to customize the configuration of personal documents?

System installation is complete, users will find / boot / grub / grub.cfg file only root permissions to read, if you want to directly modify the grub.cfg file, first change its permissions. Well, finally the grub.cfg modify the system to upgrade the kernel or grub, it will automatically update-grub, grub.cfg file will be equally sharp, how to ensure that the modified configuration file can be kept down?

In fact, do not modify grub.cfg, as long as the individual configuration write / etc / default / directory grub and / etc / gurb.d directory script file, after upgrading the kernel, or whether to upgrade grub update-grub performed , will be required to create personalized grub.cfg.

1, first of all see next / etc / default / grub, start the application - open the Terminal Annex in, enter

sudo gedit / etc / default / grub


grub2 Essentials - Amendment (change)

Open the file to see what changes can be made:

# If you change this file, run 'update-grub' afterwards to update
# / Boot / grub / grub.cfg.

GRUB_DEFAULT = 0 -> set the default boot entry, according to menuentry order. For example to the default menu item from the fourth start, the number to 3
GRUB_HIDDEN_TIMEOUT_QUIET = true -> Hide menu, grub2 no longer in use, regardless of
GRUB_TIMEOUT = "3" -> set the default boot entry into the waiting time, the default value of 10 seconds, according to their need to modify the
GRUB_DISTRIBUTOR = `lsb_release-i-s 2> / dev / null | | echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT = "quiet splash" -> add the kernel boot parameters, this is the default
GRUB_CMDLINE_LINUX = "noresume" -> manually add the kernel boot parameters, such as acpi = off noapic, etc. can be added here

# Uncomment to disable graphical terminal (grub-pc only)
# GRUB_TERMINAL = console -> settings whether to use graphical interface. Remove # in front, only using the console terminal, do not use graphical interface

# 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 = 640 × 480 resolution setting graphical interface, such as not using the default, remove the # in front of, the resolution to 800 × 600 or a 1024 × 768

# Uncomment if you don't want GRUB to pass "root = UUID = xxx" parameter to Linux
# GRUB_DISABLE_LINUX_UUID = true -> grub command to set whether to use UUID, remove the #, using the root = / dev / sdax instead of root = UUDI = xxx

# Uncomment to disable generation of recovery mode menu entrys
# GRUB_DISABLE_LINUX_RECOVERY = "true" -> Settings menu item to create the repair mode

2, look under / etc / grub.d directory script file:

grub2 Essentials - Amendment (change)

00_header, 05_debian_theme, 10_linux, 20_memtest86 +, 30_os-prober, 40_custom five scripts in various parts of the corresponding grub.cfg on some versions of grub possible to separate the other part 30_os-prober as 30_os-others, this does not affect. How to modify the script to ensure the destruction of update-grub does not run, while still allowing the resulting grub.cfg out of wishes? Is very simple, as long as the script file to find
cat <<EOF
Such statements, EOF the middle of the text will be written directly to the appropriate location grub.cfg, so personalized statement added in this area can be had.

Do not use the statements of other variables make changes with the change, unless you know what the result will be modified.

1) look 00_header, open the terminal as sudo gedit / etc/grub.d/00_header, find this part of the

cat <<EOF
load_env -> loaded by the save_env variables stored in grubenv
set default = "$ (GRUB_DEFAULT)" -> use / etc / default / grub settings in
if [\ $ (prev_saved_entry)]; then
saved_entry = \ $ (prev_saved_entry)
save_env saved_entry
prev_saved_entry = -> change in the variable settings saved by the next sentence grubenv
save_env prev_saved_entry
fi -> Enter here to add a new line to add the module can be inserted
insmod jpeg -> Add background image format support, such as jpeg, png, tga and so on.
insmod fat -> add file system format support, such as fat, ntfs, can also be added under the menuentry

Look in the last stretch

cat <<EOF
if [\ $ (recordfail) = 1]; then
set timeout =- 1 -> set if it failed to start recording, the display menu options need to manually select the item to start
set timeout = $ (GRUB_TIMEOUT) -> use / etc / default / grub settings in
fi -> after this line add the line
background_image (hd0, 7) / boot/images/002.jpg -> add a background image of the absolute path

2) 05_debian_theme modify the font and background color

First open the file sudo gedit / etc/grub.d/05_debian_theme

See here

cat <<EOF
set menu_color_normal = white / black -> Settings menu item in the font and background color
set menu_color_highlight = black / white -> select the item to set the font and background color.

3) 10_linux this part is automatic search for the current system, the establishment of the current system startup menu, including the system header, the kernel and other information, do not tinker, to use the personalized menu names, such as Chinese menu, can be added:

First open the file sudo gedit / etc/grub.d/10_linux, to find

cat <<EOF
menuentry "$ 1" (-> double quotes can add personalized characters, $ 1 variable value can be changed, such as to menuentry "Start Ubuntu Karmic" (
recordfail = 1
save_env recordfail -> if not to save the record failed to start, these two could be deleted

if ["x $ 3" = "xquiet"]; then
cat <<EOF
set quiet = 1
EOF -> splash screen without splash, beat characters to use, this could be deleted

Personalized menu item can also be modified here, that the above menuenrty "$ 1" change, modify the following values:
linux_entry "$ (OS), Linux $ (version)" \ -> can be changed linux_entry "start Ubuntu, Linux $ (version)" \
if ["x $ (GRUB_DISABLE_LINUX_RECOVERY)"! = "xtrue"]; then
linux_entry "$ (OS), Linux $ (version) (recovery mode)" \ -> can be changed linux_entry "start Ubuntu, Linux $ (version) (repair mode)" \

Note that only changes the characters inside the quotation marks in front of menuentry, linux_entry and back \ not make changes.

4) 20_memtest86 + automatically add the item memory test start

Open the file sudo gedit / etc/grub.d/20_memtest86 +, found

cat <<EOF
menuentry "Memory test (memtest86 +)" (-> double quotation marks can be added to individual characters, such as menuentry "start memory test"
menuentry "Memory test (memtest86 +, serial console 115200)" (
linux16 $ MEMTESTPATH console = ttyS0, 115200n8
Memory test above two items can be removed one of them.

5) 30_os-prober to find a system exists in other partitions and create a menu item, followed by windows, linux, macos.

For a single system, the default will hide the menu, you can remove hidden here

First open the file sudo gedit / etc/grub.d/30_os-prober

Single ubuntu will automatically hide the menu to unhide menu, can this part of the three timeout = 0 to, and / etc / default / grub of the timeout line, such as timeout = 10.
cat <<EOF
if [\ $ (timeout)! = -1]; then
if keystatus; then
if keystatus-shift; then
set timeout =- 1
set timeout = 0
if sleep $ verbose-interruptible 3; then
set timeout = 0
cat <<EOF
if [\ $ (timeout)! = -1]; then
if sleep $ verbose-interruptible $ (GRUB_HIDDEN_TIMEOUT); then
set timeout = 0

Following this automatically find and add windows, linux and macos Startup Items can modify the individual characters within the menuentry
windows startup items
for OS in $ (OSPROBED); do
... ...
if [-z "$ (LONGNAME)"]; then
... ...
cat <<EOF
menuentry "$ (LONGNAME) (on $ (DEVICE))" (-> such as to menuentry "start Windows XP" (

linux startup items
LINUXPROBED = "` linux-boot-prober $ (DEVICE) 2> / dev / null | tr '' '^' | paste-s-d '' `"
... ...
if [-z "$ (LLABEL)"]; then
cat <<EOF
menuentry "$ (LLABEL) (on $ (DEVICE))" (-> such as to menuentry "start the Mandriva" (

macos Startup Items
OSXUUID = "` grub-probe-target = fs_uuid-device $ (DEVICE) 2> / dev / null `"
cat <<EOF
menuentry "$ (LONGNAME) (on $ (DEVICE))" (-> such as to menuentry "start the MacOS" (

6) 40_custom custom startup items, according to the menu can be added later in a standard format, such as

#! / Bin / sh
exec tail-n +3 $ 0
# This file provides an easy way to add custom menu entries. Simply type the
# Menu entries you want to add after this comment. Be careful not to change
# The 'exec tail' line above.

menuentry "Start Veket" (
set root = (hd0, 8)
linux / veket / vmlinuz root = / dev/ram0 PMEDIA = hd
initrd / veket / initrd.gz
menuentry "Start CDLinux" (
set root = (hd0, 8)
linux / CDlinux / bzImage root = / dev/ram0 vga = 791 CDL_LANG = zh_CN.UTF-8
initrd / CDlinux / initrd

3, to save the script changes after the implementation of the terminal
sudo update-grub

sudo gedit / boot / grub / grub.cfg
See configuration files is not, and you want the same.
If you have not satisfied with the re-modify the script file under, then the implementation of sudo update-grub

4, ubuntu and windows dual system, and how the system defaults to start from windows

In / etc / grub.d directory script file name is start with a digit, which identifies when the update-grub in the implementation of the contents of each file to be executed in the order, we just put the file name 30_os-prober figure 30 to number between 05 to 10 can (not many optional 06,07,08,09), such as to 08_os-prober, it created out of the menu items grub.cfg, windows will automatically sort Prior to the ubuntu.

5, using the common errors and repair methods grub2

1, two systems, re-install windows startup items cause no ubuntu

Installed version of windows to use when reloading windows will overwrite mbr, grub lost cause, you can use grub4dos to repair the grub to boot into ubuntu or livecd startup repair grub.

1) Use grub4dos repair ubuntu

First download the latest version of the grub4dos, Download, if xp system to grub4dos grldr within compressed under the root directory C copy, modify boot.ini, add a line in the final c: \ grldr = "grub4dos",

grub2 Essentials - Amendment (change)

For vista/win7 systems, the compressed package of grldr.mbr and grldr copied to C under the root directory, the C disk itself to create a boot.ini file, if hidden boot partition, you'd boot partition drive letter, then grldr, grldr.mbr and under boot.ini on the boot partition, boot.ini reads as follows:

grub2 Essentials - Amendment (change)

Then in the root directory create a new menu.lst, content

timeout 0
default 0

title grub2
find-set-root / boot / grub / core.img

kernel / boot / grub / core.img

(A / boot partition to

find-set-root / grub / core.img

kernel / grub / core.img)

After rebooting, choose Grub4Dos automatically transferred grub2, select the menu item from the grub2 ubuntu starts, the terminal implementation

sudo grub-install / dev / sda

The grub into the first hard drive mbr, such as to mount the second hard disk mbr to sda to sdb, the third hard drive as sdc, analogy.

As for the grub should be installed to sda or sdb or sdc in the mbr, the need to determine according to their own circumstances and see which one you want to set the hard disk as the startup disk, put grub installed to the mbr on that disk.

2) The repair grub livecd

Starts with ubuntu9.10 the livecd, open a terminal

If your ubuntu's / partition is sda7, and if the / boot partition is sda6, with the livecd boot, in the terminal, enter
mount / dev/sda7 / mnt
mount / dev/sda6 / mnt / boot (if no / boot partition alone skip this step)
grub-install-root-directory = / mnt / / dev / sda

As before, to mount the second hard disk to sda to sdb.

If grub.cfg have lost, but also requires the implementation of the reconstruction

mount-bind / proc / mnt / proc
mount-bind / dev / mnt / dev
mount-bind / sys / mnt / sys
chroot / mnt update-grub
umount / mnt / sys
umount / mnt / dev
umount / mnt / proc


3) The start of the disc with a grub or U disk repair

If you have got the tools grub boot disk, boot disk with the tool, the grub menu press c to enter command-line state,

In the grub> prompt, enter

grub> find / boot / grub / core.img (a / boot partition with the find / grub / core.img)

(Hdx, y) (show find the partition number)

grub> root (hdx, y)

grub> kernel / boot / grub / core.img (/ boot partition with kernel / grub / core.img)

grub> boot

After the implementation can boot

分类:OS 时间:2010-05-04 人气:1449
blog comments powered by Disqus


iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 0.045 (s). 11 q(s)