Difference between revisions of "GRUB (正體中文)"

From ArchWiki
Jump to: navigation, search
(Menu colors)
(BIOS 系統的 開機引導程式安裝方法)
Line 67: Line 67:
 
  # pacman -S grub2-bios
 
  # pacman -S grub2-bios
  
另外一提,GRUB2 必需安裝在磁碟機的開機扇區(boot sector)來進行開機引導程式的服務。內容請詳見 [[#開機引導程式安裝方法]]  
+
另外一提,GRUB2 必需安裝在磁碟機的開機磁區(boot sector)來進行開機引導程式的服務。內容請詳見 [[#開機引導程式安裝方法]]  
  
 
GRUB2 可以由 Live 環境中安裝,也能直接在可運作的 Arch 中安裝。
 
GRUB2 可以由 Live 環境中安裝,也能直接在可運作的 Arch 中安裝。

Revision as of 10:53, 2 August 2012

zh-CN:GRUB2 Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary text Template:Article summary heading Template:Article summary wiki - Burg 是一個由 GRUB2 修改而來全新的開機引導程式. 使用了新的物件格式能讓它在更多種的作業系統中,包括了Linux/Windows/OSX/Solaris/FreeBSD,等。它也擁有高自訂性的選單系統同時支援文字與圖形模式 。 Template:Article summary heading Template:Article summary link Template:Article summary link Template:Article summary end

GRUB2 是新一代的 GRand Unified Bootloader (GRUB/開機引導程式). GRUB2 來自於 PUPA 這個探討下一代 GRUB 的研究專案 。GRUB 2 清除了所有東西並被重新編寫,擁有模組化和可移植性。[1]

簡單地說,開機引導程式 是當電腦開機時第一個執行的程式(在此請自動乎略bios,謝謝)。它負責載入並移交控制權給 Linux 核心。而核心載入後,則反過來啟動作業系統內的其它部份。

現在來說, GRUB (i.e. version 0.9x) 實際上便是標準的 Linux 引導程式,預計將在不久的將來被 grub2 取代。在那之後,“GRUB“ 將變成 “傳統GRUB“。儘管從2005年2月以來 grub2 己經努力至今,而 GRUB(版本0.9x)也在網路上某些地方被稱為舊版,但實際上仍然 *不是* 。新的 grub2 仍然沒有完全落實為標準。

GRUB2 仍然處於開發階段,因此必須特別注意。在此警告你多作考慮! grub2 有可能不照你的預期作動,某些特性可能會取消,某些功能可能會改變。沒有任何具體的理由讓你必需使用 grub2,用戶應考慮安裝更穩定的 GRUB 來代替。

給目前 GRUB 使用者的注意事項

  • GRUB and GRUB2 的指令是有差別的. 請在進行下一步之前自己熟悉一下 GRUB2 commands (例如, "find" 就被更換為 "search").
  • GRUB2 現在己經 模組化 且不再需要 "stage 1.5"。因此,引導程式本身是有限的 - 當需要擴充功能時模組才從硬碟內被載入(例如,LVM 或 RAID支援)
  • GRUB2 設備命名也改得和 GRUB 不同。磁區 Partitions 由數字 1 起算,而硬碟仍然由 0 起算,舉例來說,/dev/sda1 被 GRUB2 指引為 (hd0,1)

安裝過程

在安裝 Arch Linux 過程中安裝 GRUB2

  • 跳過 Install Bootloader (安裝開機引導程序)步驟並離開安裝程序。
  • 載入模組 (若你要裝的 grub2 版本 >= 1.99~rc1)
# modprobe md-mod
  • 設定網路:
# aif -p partial-configure-network
  • 若你在安裝過程中並沒有設定欲安裝系統的 /etc/resolv.conf 檔案(例如,若你打算之後讓 DHCP 每次自動產生),你便需要複製一份 AIF 設定網路時所產生的檔案:
# cp /etc/resolv.conf /mnt/etc/resolv.conf
  • 從安裝用的 live shell, chroot (虛擬掛載)到被安裝的系統中去:
# mount -o bind /dev /mnt/dev
# chroot /mnt bash
  • 用下一步的方法安裝 GRUB2 程式套件包

BIOS 系統的 開機引導程式安裝方法

GRUB2 套件包可以用 pacman 進行安裝(安裝時會提示替換 grub):

For grub2 <= 1.98

# pacman -S grub2

For grub2 >=1.99

# pacman -S grub2-bios

另外一提,GRUB2 必需安裝在磁碟機的開機磁區(boot sector)來進行開機引導程式的服務。內容請詳見 #開機引導程式安裝方法

GRUB2 可以由 Live 環境中安裝,也能直接在可運作的 Arch 中安裝。

一般情況下,GRUB2 可以簡單地以系統管理者權限用 grub-install 指令來進行安裝:

For grub2 upto 1.98

# grub-install --no-floppy /dev/sda

For grub2 >= 1.99 (including bzr trunk)

# sudo modprobe dm-mod
Note: 先前提過必需先完成此步驟
# sudo grub_bios-install --boot-directory=/boot --no-floppy --recheck /dev/sda
# sudo cp /usr/share/grub/{unicode.pf2,ascii.pf2} /boot/grub/

要單獨產生一個 core.img 檔案而不安裝 grub2 到 MBR 內的話,加上 --grub-setup=/bin/true 到 grub2_bios-install 之後

# sudo grub_bios-install --grub-setup=/bin/true --boot-directory=/boot --no-floppy --recheck /dev/sda

然後你便可以從舊版 grub-legacy 鏈結(chainload)grub2 的 core.img 作為 linux kernel 或者多重啟動核心(kernel)。

The --no-floppy tells to not search for floppy devices which will vastly improve the command's overall execution time on many systems (it will also prevent the issue below from occuring) and where /dev/sda is the destination of the installation (in this case the MBR of the first SATA disk). If you use LVM for your /boot, you can install GRUB2 on multiple physical disks.

Executing grub_bios-install without the --no-floppy flag can also lead to this when there is a problem detecting the path of the floppy device:

grub-probe: error: Cannot get the real path of '/dev/fd0'
Auto-detection of a filesystem module failed.
Please specify the module with the option '--modules' explicitly.

Thus it is recommended to use the --no-floppy switch, if you don't want to have your MBR on a floppy. If you do, you also want to set floppy as the first boot device in BIOS.

GPT specific instructions

GRUB2 in BIOS-GPT configuration requires a BIOS Boot Partition to embed its core.img in the absence of 32 KN post MBR gap in GPT partitioned systems.

Create a 1 MB (approx) partition using GPT fdisk or GNU Parted with no filesystem. The location of the partition does not matter but it is advisable to put it somewhere in the beginning of the disk before the /boot partition. Set the partition type to "EF02" in GPT fdisk or set "bios_grub" flag on in GNU Parted or GParted.

This partition is used by GRUB2 only in BIOS-GPT setups. No such partition type exists in case of MBR partitioning (at least not for GRUB2). This partition is also not required if the system is UEFI based, as no embedding takes place in that case. Neither GRUB-legacy nor SYSLINUX require this partition.

Note: This partition should be created before grub2_bios-install or grub-setup is run or before the Install Bootloader step of the Archlinux installer (if GRUB2 is selected as bootloader).

UEFI 系統的 開機引導程式安裝方法

Note: Unless specified as EFI 1.x , EFI and UEFI terms are used interchangeably to denote UEFI 2.x firmware. Also unless stated explicitely, the instructions are general and not Mac specific. Some of them may not work or may be different in Macs. Apple's EFI implementation is neither a EFI 1.1 version nor UEFI 2.x version but includes features of both. This kind of firmware does not fall under any one UEFI version so it is not a standard EFI firmware.

GRUB2 UEFI bootloader is available in Arch Linux only from version 1.99~rc1 . To install, first Detect which UEFI firmware arch you have (i386 or x86_64).

Depending on that, install the appropriate package

For 64-bit aka x86_64 UEFI firmware

# pacman -S grub2-efi-x86_64

For 32-bit aka i386 UEFI firmware

# pacman -S grub2-efi-i386

Mount the EFI SYSTEM PARTITION in your system at /boot/efi . It should be FAT32 formatted and should be at least 200 MB in size. If you have mounted the EFISYS partition in some other mountpoint, replace /boot/efi in the below commands with that mountpoint.

# sudo mkdir -p /boot/efi/efi  # If the directory does not exist

# sudo modprobe dm-mod

The below commands assume you are using grub2-efi-x86_64 (for grub2-efi-i386, replace all x86_64 with i386 in the commands)

# sudo grub_efi_x86_64-install --boot-directory=/boot/efi/efi --bootloader-id=grub --no-floppy --recheck

If you notice carefully, there is no <device_path> option (Eg:- /dev/sda) at the end of the grub_efi_x86_64-install command unlike the case of setting up grub2 for BIOS systems. Any <device_path> provided will be ignored by the install script as UEFI bootloaders do not use MBR or Partition boot sectors at all.

Then copy the font files required to enable UEFI video mode (GOP mostly) in grub2 (otherwise no kernel boot messages will be seen in the console)

# sudo cp /usr/share/grub/{unicode.pf2,ascii.pf2} /boot/efi/efi/grub/

The grub_efi_x86_64-install automatically generates a "grub.efi" while setting up the /boot/efi/efi/grub/ directory.

If you want, you can create a custom grub2.efi app using :-

# sudo grub-mkimage -d /usr/lib/grub/x86_64-efi -O x86_64-efi -p "" -o /boot/efi/efi/grub/grub2.efi <Your_List_of_GRUB2_Modules>

Note : The -p "" option is important for creating a portable grub2.efi app.

There is no file-size restriction on grub2.efi either due to GRUB2 or due to the UEFI firmware. Therefore you can include any number of modules you want.

A "grub.cfg" created for BIOS based GRUB2 will be sufficient for the UEFI GRUB2 as long as all the paths in the config are absolute paths. The "grub.efi" uefi application can be launched using the firmware's "Boot Manager", "Boot from file" option or using the UEFI Shell.

Alternatively, you can also setup /boot/efi/efi/grub directory by copying all the files in /usr/lib/grub/x86_64-efi to /boot/efi/efi/grub and using the above grub-mkimage command to create a grub2.efi application. Just make sure you have the required modules embedded in grub2.efi to enable it to access the EFI SYSTEM PARTITION. A basic grub2.efi can be generated using the following command -

# sudo grub-mkimage -d /usr/lib/grub/x86_64-efi -O x86_64-efi -p "" -o /boot/efi/efi/grub/grub2.efi part_gpt fat iso9660 udf normal chain linux \
ls search search_fs_file search_fs_uuid search_label help boot configfile echo efi_gop

In the /boot/efi/efi/grub/grub.cfg file, add the following lines to enable grub2 to pass the video mode correctly to the kernel, without which you will only get a black screen (no output) but booting (actually) proceeds successfully without any system hang.

insmod efi_gop
 insmod font

 if loadfont ${prefix}/unicode.pf2
 then
     insmod gfxterm
     set gfxmode=auto
     set gfxpayload=keep
     terminal_output gfxterm
 fi

As you can see for gfxterm (graphical terminal) to function properly, unicode.pf2 font file should exists in /boot/efi/efi/grub/ dir.

手動編譯

For BIOS Systems

GRUB2 for BIOS systems should be compiled as follows

./autogen.sh

./configure --with-platform=pc --prefix=/usr

make

make install

The --with-platform=pc generates grub2 for bios alone irrespective of the firmware of the build system.

For UEFI Systems

First Detect which UEFI Firmware arch you have and then follow the compile instructions below -

./autogen.sh

./configure --with-platform=efi --target=TARGET_EFI_ARCH --prefix=/usr

make

make install

The "--target" option denotes the UEFI firmware arch. for which grub2 should be compiled, not the architecture of the linux kernel grub2 may boot.

It is possible to use UEFI 64-bit firmware + GRUB2 as x86_64-EFI app loading a i686 linux kernel, as long as the kernel does not try to access UEFI Runtime Services. Vice-versa situation is also possible. But a x86_64 UEFI firmware cannot launch GRUB2 i386-efi app (unlike x86_64 Operating Systems), and a i386 UEFI firmware will not launch GRUB2 x86_64-efi app. It is important to compile GRUB2 to match the architecture of the UEFI firmware.

Configuration

The configuration files are /etc/default/grub and /etc/grub.d/*. These files are used to generate the /boot/grub/grub.cfg file. You can also choose to manually edit grub.cfg.

grub-mkconfig

The grub-mkconfig script can be used to generate a grub.cfg file. By default the script outputs to stdout. Note that gettext ― an optional dependency of the GRUB2 package ― is required by the grub-mkconfig script. To generate a grub.cfg file run the command:

# grub-mkconfig -o /boot/grub/grub.cfg

grub.cfg

A basic grub file uses the following options

  • (hdX,Y) is the partition Y on disk X, partition numbers starting at 1, disk numbers starting at 0
  • set default=N is the default boot entry that is chosen after timeout for user action
  • set timeout=M is the time M to wait in seconds for a user selection before default is booted
  • menuentry "title" {entry options} is a boot entry titled title
  • set root=(hdX,Y) sets the boot partition, where the kernel and GRUB modules are stored (boot need not be a separate partition, and may simply be a directory under the "root" partition (/)

An example configuration:

/boot/grub/grub.cfg
# Config file for GRUB2 - The GNU GRand Unified Bootloader
# /boot/grub/grub.cfg

# DEVICE NAME CONVERSIONS
#
#  Linux           Grub
# -------------------------
#  /dev/fd0        (fd0)
#  /dev/sda        (hd0)
#  /dev/sdb2       (hd1,2)
#  /dev/sda3       (hd0,3)
#

# Timeout for menu
set timeout=5

# Set default boot entry as Entry 0
set default=0

# (0) Arch Linux
menuentry "Arch Linux" {
set root=(hd0,1)
linux /vmlinuz26 root=/dev/sda3 ro
initrd /kernel26.img
}

## (1) Windows
#menuentry "Windows" {
#set root=(hd0,3)
#chainloader +1
#}

If you choose to modify grub.cfg completely manually and won't even generate the first grub.cfg, you should know that if you do not have a separate boot partition, /boot must prefix entries in grub.cfg. Example:

# (0) Arch Linux
menuentry "Arch Linux" {
set root=(hd0,1)
linux /boot/vmlinuz26 root=/dev/sda1 ro
initrd /boot/kernel26.img
}

Dual-booting

NOTE: If you want GRUB2 to automatically search for other systems, for example as in Ubuntu. Then you may need to download os-proberAUR from the AUR.

Using grub-mkconfig

The best way to add other entries is editing the /etc/grub.d/40_custom. The entries in this file will be automatically added when running grub-mkconfig. After adding the new lines, run

# grub-mkconfig -o /boot/grub/grub.cfg 

to generate an updated grub.cfg.

With GNU/Linux

Assuming that the other distro is on partition sda2:

menuentry "Other Linux" {
set root=(hd0,2)
linux /boot/vmlinuz (add other options here as required)
initrd /boot/initrd.img (if the other kernel uses/needs one)
}
With Windows

This assumes that your Windows partition is sda3.

# (2) Windows XP
menuentry "Windows XP" {
    set root=(hd0,3)
    chainloader (hd0,3)+1
}

If the windows bootloader is on an entirely different harddrive than grub, it may be neccessary to trick windows into believing that it is in fact the first harddrive. This was possible in the old grub with map and is now done with drivemap. Assume grub is on hd0 and windows on hd2, you need to add the following after set root

drivemap -s hd0 hd2

With Windows via EasyBCD and NeoGRUB

Since EasyBCD's NeoGRUB currently does not understand the GRUB2 menu format, chainload to it by replacing the contents of your C:\NST\menu.lst file with lines similar to the following:

default 0
timeout 1
title       Chainload into GRUB v2
root        (hd0,7)
kernel      /boot/grub/core.img

Visual Configuration

In GRUB2 it is possible, by default, to change the look of the menu.

Background image and bitmap fonts

GRUB2 comes with support for background images and bitmap fonts in pf2 format. The unifont font is included in the grub2 package under the filename unicode.pf2, or, as only ascii characters under the name ascii.pf2. Image formats supported include tga, png and jpeg, providing the correct modules are loaded. The maximum supported resolution depends on your hardware. There are two ways of setting a tga file as background. Two sample configurations are shown below.

The new über method

Edit /etc/default/grub like this:

GRUB_GFXMODE=1024x768x32
GRUB_GFXPAYLOAD_LINUX=keep
GRUB_BACKGROUND="/boot/grub/archlinux.tga"
#GRUB_THEME="/path/to/gfxtheme"

To generate the changes, run:

grub-mkconfig -o /boot/grub/grub.cfg
The deprecated method

This is the piece of text that would be generated. You can choose to add it manually to grub.cfg:

if loadfont /usr/share/grub/unicode.pf2 ; then
  set gfxmode="1024x768x32"
  set gfxpayload=keep
  insmod gfxterm
  insmod vbe
  terminal_output gfxterm
  if terminal_output gfxterm; then true ; else
     terminal gfxterm
  fi
fi
insmod tga
background_image /boot/grub/archlinux.tga
Note: If this example doesn't work for you try to replace gfxmode="1024x768x32" by vbemode="0x105".
Note: To show all the modes you can use the vbeinfo command at grub2 prompt (you need to load the vbe module before).
Note: If you have installed Grub on a separate partition, /boot/grub/archlinux.tga becomes /grub/archlinux.tga.

選單色彩

As in Grub (0.9x), you can change the menu colors in Grub2. The available colors for GRUB2 are at http://www.gnu.org/software/grub/manual/html_node/Theme-file-format.html#Theme-file-format . Here's an example:

The new über method

Edit /etc/default/grub:

GRUB_COLOR_NORMAL="light-blue/black"
GRUB_COLOR_HIGHLIGHT="light-cyan/blue"

Execute:

grub-mkconfig -o /boot/grub/grub.cfg
The deprecated method
### BEGIN /etc/grub.d/00_header ###
....
set menu_color_normal=light-blue/black
set menu_color_highlight=light-cyan/blue
....
### END /etc/grub.d/00_header ###

Hidden menu

One of the unique features of Grub2 is hiding/skipping the menu and showing it by holding "Shift" when needed. You can also adjust whether you want to see the timeout counter.

The new über method

Edit /etc/default/grub as you wish. Here's an example where the comments from the beginning of the two lines have been removed to enable the feature, the timeout has been set to five seconds and to be shown to the user:

GRUB_HIDDEN_TIMEOUT=5
GRUB_HIDDEN_TIMEOUT_QUIET=false

And run:

grub-mkconfig -o /boot/grub/grub.cfg
The deprecated method

Once again, you can choose to be an exception and do this manually as shown here:

....
set locale_dir=($root)/boot/grub/locale
set lang=en
insmod gettext
'if sleep --interruptible 5 ; then
 set timeout=0
fi
### END /etc/grub.d/00_header ###
....

Setting the framebuffer resolution

The new über method

Grub2 can set the framebuffer for both grub2 itself and the kernel. The old vga= way is deprecated. The preferred method is editing /etc/default/grub as the following sample:

GRUB_GFXMODE=1024x768x32
GRUB_GFXPAYLOAD_LINUX=keep

To generate the changes, run:

grub-mkconfig -o /boot/grub/grub.cfg

The gfxpayload property will make sure the kernel keeps the resolution.

If this method does not work for you, the deprecated vga= method will still work. Just add the vga= setting next to the "GRUB_CMDLINE_LINUX_DEFAULT=" line in "/etc/default/grub" for eg: "GRUB_CMDLINE_LINUX_DEFAULT="quiet splash vga=792" will give you a 1024x768 resolution.

You can choose one of these resolutions: 640×480, 800×600, 1024×768, 1280×1024, 1600×1200

The deprecated method

Again, you can add the configuration manually to grub.cfg as shown below.

if loadfont /usr/share/grub/unicode.pf2 ; then
  set gfxmode="1024x768x32" 
  set gfxpayload=keep
  insmod gfxterm
  insmod vbe
  terminal_output gfxterm
  if terminal_output gfxterm; then true ; else
     terminal gfxterm
  fi
fi

Other Options

LVM

If you use LVM for your /boot, add the following before menuentry lines:

insmod lvm

and specify your root in the menuentry as:

set root=(lvm_group_name-lvm_logical_boot_partition_name)

Example:

# (0) Arch Linux
menuentry "Arch Linux" {
insmod lvm
set root=(VolumeGroup-lv_boot)
# you can only set following two lines
linux /vmlinuz26 root=/dev/mapper/VolumeGroup-root ro
initrd /kernel26.img
}

Raid

Grub2 provides convenient handling of raid-volumes. You need to add

insmod raid

which allows you to address the volume natively. E.g. /dev/md0 becomes

set root=(md0)

whereas a partitioned raid-volume (e.g. /dev/md0p1) becomes

set root=(md0,1)

Persistent block device naming

You can use UUIDs to detect partitions instead of the "old" /dev/sd* and /dev/hd* scheming. It has the advantage of detecting partitions by their unique UUIDs, which is needed by some people booting with complicated partition setups.

UUIDs are used by default in the recent versions of grub2 - there's no downside in it anyway except that you need to re-generate the grub.cfg file every time you resize or reformat your partitions. Remember this when modifying partitions with Live-CD.

The new über method

The recent versions of grub2 use UUIDs by default. You can re-enable the use of UUIDS by simply commenting the UUID line (this is also what it looks like by default):

#GRUB_DISABLE_LINUX_UUID=true

you can also just set the value as false as shown here:

GRUB_DISABLE_LINUX_UUID=false

Either way don't forget to generate the changes:

grub-mkconfig -o /boot/grub/grub.cfg
The deprecated method

To list UUIDs, from a running system:

# blkid

Replace the value of the root pointer on the linux line with the following:

linux /vmlinuz26 root=/dev/disk/by-uuid/<UUID> ro

However, you still have to set Grub2's notion of a root partition. In order to do that, use the search command:

search --fs-uuid --no-floppy --set=root <UUID>

What the --no-floppy flag does has already been explained. An example boot entry using Persistent block device naming would look like:

menuentry "Arch Linux" {
    search --fs-uuid --no-floppy --set=root 355ccb5c-99e1-400d-b612-451f9247e35e
    linux /boot/vmlinuz26 root=/dev/disk/by-uuid/355ccb5c-99e1-400d-b612-451f9247e35e ro
    initrd /boot/kernel26.img
}

Using Labels

It is possible to use labels, human-readable strings attached to filesystems, by using the --label option to search. First of all, label your existing partition:

# tune2fs -L a <LABEL> <PARTITION>

Then, add an entry using labels. An example of this:

menuentry "Arch Linux, session texte" {
    search --label --no-floppy --set=root archroot
    linux /boot/vmlinuz26 root=/dev/disk/by-label/archroot ro
    initrd /boot/kernel26.img
}

Recall previous entry

Grub2 can remember the last entry you booted from and use this as the default entry to boot from next time. This is useful if you have multiple kernels (i.e., the current Arch one and the LTS kernel as a fallback option) or operating systems. To do this, edit /etc/default/grub and change the setting of GRUB_DEFAULT:

GRUB_DEFAULT=saved

This ensures that grub will default to the saved entry. To enable saving the selected entry, add the following line to /etc/default/grub:

GRUB_SAVEDEFAULT=true

Remember to regenerate your configuration file.

Using the command shell

Since the MBR is too small to store all GRUB2 modules, only the menu and a few basic commands reside there. The majority of GRUB2 functionality remains in modules in /boot/grub, which are inserted as needed. In error conditions (e.g. if the partition layout changes) GRUB2 may fail to boot. When this happens, a command shell may appear.

GRUB2 offers multiple shells/prompts. If there is a problem reading the menu but the bootloader is able to find the disk, you will likely be dropped to the "normal" shell:

sh:grub>

If there is a more serious problem (e.g. GRUB cannot find required files), you may instead be dropped to the "rescue" shell:

grub rescue>

The rescue shell is a restricted subset of the normal shell, offering much less functionality. If dumped to the rescue shell, first try inserting the "normal" module, then starting the "normal" shell:

grub rescue> set prefix=(hdX,Y)/boot/grub
grub rescue> insmod (hdX,Y)/boot/grub/normal.mod
rescue:grub> normal

Pager support

GRUB2 supports pager for reading commands that provide long output (like the help command). This works only in normal shell mode and not in rescue mode. To enable pager, in GRUB2 command shell type

sh:grub> set pager=1

parttool or legacy hide/unhide =

If you have a win9x paradigm with hidden C disks GRUB legacy had the hide/unhide feature. In GRUB2 this has been replaced by parttool. For example, to boot the third C disk of three win9x installations on the CLI enter the CLI and:

parttool hd0,1 hidden+ boot-
parttool hd0,2 hidden+ boot-
parttool hd0,3 hidden- boot+
set root=hd0,3
chainloader +1
boot

Using the rescue console

See #Using the command shell first. If unable to activate the standard shell, one possible solution is to boot using a live CD or some other rescue disk to correct configuration errors and reinstall GRUB. However, such a boot disk is not always available (nor necessary); the rescue console is surprisingly robust.

The available commands in GRUB rescue include "insmod", "ls", "set", and "unset". This example uses "set" and "insmod". "set" modifies variables and "insmod" inserts new modules to add functionality.

Before starting, the user must know the location of their /boot partition (be it a separate partition, or a subdirectory under their root):

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

where X is the physical drive number and Y is the partition number.

To expand console capabilities, insert the "linux" module:

grub rescue> insmod (hdX,Y)/boot/grub/linux.mod
Note: With a separate boot partition, omit /boot from the path, (i.e. type set prefix=(hdX,Y)/grub and insmod (hdX,Y)/grub/linux.mod).

This introduces the "linux" and "initrd" commands, which should be familiar (see #Configuration).

An example, booting Arch Linux:

set root=(hd0,5)
linux /boot/vmlinuz26 root=/dev/sda5
initrd /boot/kernel26.img
boot

With a separate boot partition, again change the lines accordingly:

set root=(hd0,5)
linux /vmlinuz26 root=/dev/sda6
initrd /kernel26.img
boot

After successfully booting the Arch Linux installation, users can correct grub.cfg as needed and then run:

# grub-install /dev/sda --no-floppy

to reinstall GRUB2 and fix the problem completely, changing /dev/sda if needed. See #Bootloader installation for details.

Combining the use of UUID's and basic scripting

--Celilo 06:04, 19 March 2010 (EDT)

If you like the idea of using UUID's to avoid unreliable bios mappings or are struggling with grub syntax, here is an example boot menu item that uses UUID's and a small script to direct grub to the proper disk partitions for your system. All you need to do is replace the UUID's in the sample with the correct UUID's for your system. (The example applies to a system with a boot and root partition. You will obviously need to modify the grub configuration if you have additional partitions.)

 menuentry "Arch Linux 64" {
   #Enter the UUID of your boot partition (this is where grub and your kernel reside)
   set uuid_grub_boot=ece0448f-bb08-486d-9864-ac3271bd8d07
   
   #Enter the UUID of the partition containing the root partition of your Arch Linux installation.            
   set uuid_os_root=c55da16f-e2af-4603-9e0b-03f5f565ec4a
   
   #(Note: this may be the same as your boot partition)
   
   #Here we set the grub "root" variable by locating the uuid of the root partition identified above   
   search --fs-uuid --no-floppy --set=root $uuid_os_root
   
   #Here we set a custom variable grub_boot by locating the uuid of the boot partition identified above          
   search --fs-uuid --no-floppy --set=grub_boot $uuid_grub_boot
   
   #Here's the magic. We test to see if the boot and root partitions have the same UUID.
   #If they do we append /boot to the $grub_boot variable. For ex. (hd0,1) becomes (hd0,1)/boot.
   if [ $uuid_grub_boot == $uuid_os_root ] ; then
      set grub_boot=$grub_boot/boot
   fi
   
   # $grub_boot now points to the correct location, so the following will properly find the kernel and initrd
   linux ($grub_boot)/vmlinuz26 root=/dev/disk/by-uuid/$uuid_os_root ro
   initrd ($grub_boot)/kernel26.img
 }

疑難排解

Any troubleshooting should be added here.

msdos-style error message

grub-setup: warn: This msdos-style partition label has no post-MBR gap; embedding won't be possible!
grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.
            However, blocklists are UNRELIABLE and its use is discouraged.
grub-setup: error: If you really want blocklists, use --force.

This error may occur when you try installing GRUB2 in a VMware container. Read more about it here. Hopefully a fix will be provided soon.

It also happens when the first partition starts just after the MBR, without the usual space of 60-something block before the first partition.

其他

I couldn't figure out how to uninstall grub1, and install grub2 to the MBR, as it isn't being booted by default. It is still booting grub1. So, an easy work-around, is rename menu.lst.pacsave or whatever, to menu.lst (in /boot/grub/) and for each menu entry that you would like to use grub2, at the end type "chainloader +1". This will tell grub1 to forward control to grub2. This is an ugly hack though, so I advise setting the menu.lst's timout as 0, otherwise the total timeout would be grub1's time out + grub2's which, for me would equal more than 18 seconds, which is quite a bit.

P.S. hopefully someone figures out how to pry grub1's dead fingers off of my MBR, and place grub2 on it :) .

In my case it had to do with my boot partition. Say boot-partition is (hd0,1) and your root is (hd0,3) (grub2 naming). grub-setup searches for (hd0,3)/boot/grub/core.img. Just because it's on (hd0,1)/grub/core.img, it is unable to find it. So I copied the grub-folder to my root partition and everything worked fine:

E.g. (as root:)

# mount /boot
# cp -a /boot/grub /
# umount /boot
# mv /grub /boot/
# grub-install /dev/sda

參考資料

  1. Official GRUB2 Manual - http://www.gnu.org/software/grub/manual/grub.html
  2. GRUB2 wiki page describing steps to compile for BIOS - http://grub.enbug.org/TestingOnX86
  3. GRUB2 wiki page describing steps to compile for UEFI - http://grub.enbug.org/TestingOnMacbook
  4. Wikipedia's page on BIOS Boot Partition
  5. GRUB2 Wiki page describing why BIOS Boot Partition is required.

外部連結

  1. A Linux Bash Shell script to compile and install GRUB2 for BIOS from BZR Source
  2. A Linux Bash Shell script to compile and install GRUB2 for UEFI from BZR Source