https://wiki.archlinux.org/api.php?action=feedcontributions&user=%E6%AE%8B%E6%9C%88%E8%88%9E%E6%B5%81%E6%AE%87&feedformat=atom
ArchWiki - User contributions [en]
2024-03-28T12:06:57Z
User contributions
MediaWiki 1.41.0
https://wiki.archlinux.org/index.php?title=GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=393233
GRUB (简体中文)
2015-08-23T04:35:36Z
<p>残月舞流殇: /* 多系统启动 */</p>
<hr />
<div>[[Category:Boot loaders (简体中文)]]<br />
[[ar:GRUB]]<br />
[[cs:GRUB]]<br />
[[de:GRUB]]<br />
[[el:GRUB]]<br />
[[en:GRUB]]<br />
[[es:GRUB]]<br />
[[fr:GRUB]]<br />
[[he:GRUB]]<br />
[[id:GRUB2]]<br />
[[it:GRUB2]]<br />
[[ja:GRUB]]<br />
[[nl:GRUB]]<br />
[[ru:GRUB]]<br />
[[tr:GRUB2]]<br />
[[uk:GRUB]]<br />
[[zh-TW:GRUB2]]<br />
{{TranslationStatus (简体中文)|GRUB|2013-11-15|282734}}<br />
{{Related articles start (简体中文)}}<br />
{{Related|Burg}}<br />
{{Related|GRUB Legacy}}<br />
{{Related|GRUB EFI Examples}}<br />
{{Related articles end}}<br />
[http://www.gnu.org/software/grub/ GRUB2] 是下一代 GRand Unified Bootloader (GRUB,请不要和Grub Legacy混淆了)。 它来自下一代 GRUB 研究项目 [http://www.nongnu.org/pupa/ PUPA],代码全部重写,实现了模块化和增强了移植性。[http://www.gnu.org/software/grub/grub-faq.en.html#q1].<br />
<br />
简单的说,'''Boot Loader'''是电脑启动时运行的第一个程序,它负责装载内核并将控制权转交。内核再初始化操作系统的其它部分。<br />
<br />
==前言==<br />
* 引导程序是计算机启动时第一个运行的程序。它负责加载并将控制权转移到Linux内核。内核作为回报,将初始化操作系统剩余部分<br />
* 官方所称的GRUB代表的是本软件的第二版,即GRUB2,请参考[https://www.gnu.org/software/grub/].如果你是在找有关Grub Legacy的文章,请参考[[GRUB Legacy]].<br />
* GRUB2支持由zlib或者LZO压缩过的[[Btrfs]]格式的根目录,如果使用Btrfs,不需要单独的{{ic|/boot}}分区.<br />
* GRUB2 不支持[[F2fs]]格式的根目录,所以你需要为{{ic|/boot}}分区单独设置一个支持的文件系统.<br />
<br />
== BIOS 系统==<br />
=== GUID分区表(GPT)具体说明 ===<br />
BIOS/[[GPT]]配置中,一个 [http://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html BIOS启动分区]是必需的。GRUB将{{ic|core.img}}嵌入到这个分区。<br />
{{Note|<br />
* 在尝试分区之前请记住不是所有的系统都能够支持这种分区方案, 关于更多请阅读[[GUID Partition Table#BIOS systems|GUID分区表]].<br />
* This additional partition is only needed on a GRUB, BIOS/GPT partitioning scheme. Previously, for a GRUB, BIOS/MBR partitioning scheme, GRUB used the Post-MBR gap for the embedding the {{ic|core.img}}). GRUB for GPT, however, does not use the Post-GPT gap to conform to GPT specifications that require 1_megabyte/2048_sector disk boundaries.<br />
* For [[UEFI]] systems this extra partition is not required as no embedding of boot sectors takes place in that case.<br />
}}<br />
Create a mebibyte partition ({{ic|1=+1M}} with {{ic|fdisk}} or {{ic|gdisk}}) on the disk with no file system and type BIOS boot (''BIOS boot'' in fdisk, {{ic|ef02}} in gdisk, {{ic|bios_grub}} in {{ic|parted}}). This partition can be in any position order but has to be on the first 2 TiB of the disk. This partition needs to be created before GRUB installation. When the partition is ready, install the bootloader as per the instructions below.<br />
The post-GPT gap can also be used as the BIOS boot partition though it will be out of GPT alignment specification. Since the partition will not be regularly accessed performance issues can be disregarded (though some disk utilities will display a warning about it). In {{ic|fdisk}} or {{ic|gdisk}} create a new partition starting at sector 34 and spanning to 2047 and set the type. To have the viewable partitions begin at the base consider adding this partition last.<br />
<br />
=== 主引导记录(MBR)具体说明 ===<br />
<br />
一般来说,如果使用兼容DOS的分区对齐模式,MBR后面空间(post-MBR gap)的大小都是31KiB(MBR和第一个分区之间的空间).不过,为了提供足够的空间嵌入GRUB的{{ic|core.img}}文件({{bug|24103}}),建议将这个空间设置到1到2Mib.最好使用支持1MiB分区对齐的分区软件来分区,因为这样也能满足非512B扇区磁盘分区的需求.<br />
<br />
=== 安装 ===<br />
<br />
可以使用[[pacman|pacman]]从[[official repositories|官方仓库]]安装{{Pkg|grub}}包.安装完成后它会代替{{AUR|grub-legacy}}.<br />
<br />
{{Note|简单的安装Grub包并不会更新{{ic|/boot/grub/i386-pc/core.img}}和{{ic|/boot/grub/i386-pc}}里的GRUB模组.需要使用下面介绍的{{ic|grub-install}}来手动更新它们.}}<br />
<br />
==== 安装Boot文件 ====<br />
<br />
有三种方式安装GRUB Boot文件:<br />
<br />
* [[#安装到磁盘上|安装到磁盘上]] (推荐方式)<br />
* [[#安装到U盘|安装到U盘]] (用于恢复)<br />
* [[#安装到分区上或者无分区磁盘上|安装到分区或者无分区磁盘上]] (不推荐)<br />
* [[#只生成core.img|只生成core.img文件]] (最安全的方法, 但是需要另外的bootloader,比如[[Syslinux]]来进行链式加载{{ic|/boot/grub/i386-pc/core.img}})<br />
<br />
{{Note|请参考 http://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html 获取更详尽的资料}}<br />
<br />
===== 安装到磁盘上 =====<br />
<br />
{{Note|这种方法只限于将GRUB安装到可分区磁盘(MBR或GPT),GRUB 文件会被安装到{{ic|/boot/grub}},1st stage code会被安装到MBR的启动代码区域(以便和MBR分区表分开).对于无分区磁盘,请参考[[#安装到分区上或者无分区磁盘上]]}}<br />
<br />
以下命令会将{{ic|grub}}安装到MBR的启动代码区域,填充{{ic|/boot/grub}}文件夹,生成{{ic|/boot/grub/i386-pc/core.img}},将其嵌入post-MBR gap或者放入BIOS boot partition中(分别对应MBR分区表和GPT分区表):<br />
# grub-install --target=i386-pc --recheck --debug /dev/sda<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
如果你使用[[LVM]]来进行启动,你可以将GRUB安装在多个物理磁盘上.<br />
执行{{ic|grub-install}}并不会生成GRUB配置文件,请移至[[#生成GRUB配置文件]]一节<br />
<br />
===== 安装到U盘 =====<br />
<br />
假设你的U盘第一个分区是FAT32,其分区是/dev/sdy1<br />
<br />
# mkdir -p /mnt/usb ; mount /dev/sdy1 /mnt/usb<br />
# grub-install --target=i386-pc --recheck --debug --boot-directory=/mnt/usb/boot /dev/sdy<br />
# grub-mkconfig -o /mnt/usb/boot/grub/grub.cfg<br />
<br />
# optional, backup config files of grub.cfg<br />
# mkdir -p /mnt/usb/etc/default<br />
# cp /etc/default/grub /mnt/usb/etc/default<br />
# cp -a /etc/grub.d /mnt/usb/etc<br />
<br />
# sync ; umount /mnt/usb<br />
<br />
===== 安装到分区上或者无分区磁盘上 =====<br />
<br />
{{Note|GRUB并不推荐将其安装到分区启动扇区或者无分区磁盘上(Grub Legacy和syslinux相反).这种安装方式不安全,当升级时可能会损坏.Arch开发人员也不支持这种方式}}<br />
<br />
下面的命令将会将GRUB2安装到分区扇区或者无分区磁盘上(比如闪存上):<br />
<br />
# chattr -i /boot/grub/i386-pc/core.img<br />
# grub-install --target=i386-pc --recheck --debug --force /dev/sdaX<br />
# chattr +i /boot/grub/i386-pc/core.img<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
<br />
<br />
必须使用{{ic|--force}}选项来启用对blocklists(块列表)的支持,不应使用{{ic|1=--grub-setup=/bin/true}}(这个选项的效果类似于只生成{{ic|core.img}}).<br />
{{ic|grub-install}}会生成以下警告,你可以通过这些警告判断发生了什么问题:<br />
/sbin/grub-setup: warn: Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea.<br />
/sbin/grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists. <br />
However, blocklists are UNRELIABLE and their use is discouraged.<br />
<br />
如果不指定{{ic|--force}}选项,会出现以下错误,并且不会将启动代码安装到启动扇区上: <br />
/sbin/grub-setup: error: will not proceed with blocklists<br />
<br />
而指定了{{ic|--force}},会出现:<br />
<br />
Installation finished. No error reported.<br />
<br />
{{ic|grub-setup}}不默认允许这种情况的原因是,在分区或者无分区磁盘上,{{ic|grub}}依赖于嵌入分区引导扇区的块列表(blocklists)来定位{{ic|/boot/grub/i386-pc/core.img}}和{{ic|/boot/grub}}.而{{ic|core.img}}在分区上的扇区位置很有可能随着分区文件系统的更改而变化(复制文件,删除文件等).详情请参考https://bugzilla.redhat.com/show_bug.cgi?id=728742 和 https://bugzilla.redhat.com/show_bug.cgi?id=730915.<br />
<br />
临时解决方案是给{{ic|/boot/grub/i386-pc/core.img}}文件加"不可变"(immutable)标志.只有当将{{ic|grub}}安装到分区启动扇区或者无分区磁盘上时才需要给core.img加"不可变"标志.<br />
执行{{ic|grub-install}}并不会生成GRUB配置文件,请移至[[#生成GRUB配置文件]]一节<br />
<br />
===== 只生成core.img =====<br />
<br />
通过添加{{ic|1=--grub-setup=/bin/true}}选项,{{ic|grub-install}}命令会填充{{ic|/boot/grub}}文件夹并生成{{ic|/boot/grub/i386-pc/core.img}},但是不会将grub启动引导代码嵌入到MBR,post-MBR gap和分区引导扇区中:<br />
<br />
# grub-install --target=i386-pc --grub-setup=/bin/true --recheck --debug /dev/sda<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
生成后,Grub Legacy或者syslinux就可以通过链式加载GRUB2的{{ic|core.img}}来间接加载Linux内核或者多启动内核了.<br />
<br />
==== 生成GRUB配置文件 ====<br />
<br />
最后,生成GRUB2所需的配置文件(在配置章节会详述):<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{Note|文件路径是{{ic|/boot/grub/grub.cfg}}, 而不是{{ic|/boot/grub/i386-pc/grub.cfg}}.}}<br />
<br />
如果GRUB在启动时报错"no suitable mode found",请参考[[#"No suitable mode found" error]].<br />
<br />
如果{{ic|grub-mkconfig}}执行失败,可以通过如下方式将{{ic|/boot/grub/menu.lst}}文件转化为{{ic|/boot/grub/grub.cfg}}<br />
{{Note|这种方法只适合BIOS,不适合UEFI}}<br />
# grub-menulst2cfg /boot/grub/menu.lst /boot/grub/grub.cfg<br />
<br />
示例如下:<br />
{{hc|/boot/grub/menu.lst|<nowiki><br />
default=0<br />
timeout=5<br />
<br />
title Arch Linux Stock Kernel<br />
root (hd0,0)<br />
kernel /vmlinuz-linux root=/dev/sda2 ro<br />
initrd /initramfs-linux.img<br />
<br />
title Arch Linux Stock Kernel Fallback<br />
root (hd0,0)<br />
kernel /vmlinuz-linux root=/dev/sda2 ro<br />
initrd /initramfs-linux-fallback.img<br />
</nowiki>}}<br />
<br />
{{hc|/boot/grub/grub.cfg|<nowiki><br />
set default='0'; if [ x"$default" = xsaved ]; then load_env; set default="$saved_entry"; fi<br />
set timeout=5<br />
<br />
menuentry 'Arch Linux Stock Kernel' {<br />
set root='(hd0,1)'; set legacy_hdbias='0'<br />
legacy_kernel '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'<br />
legacy_initrd '/initramfs-linux.img' '/initramfs-linux.img'<br />
<br />
}<br />
<br />
menuentry 'Arch Linux Stock Kernel Fallback' {<br />
set root='(hd0,1)'; set legacy_hdbias='0'<br />
legacy_kernel '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'<br />
legacy_initrd '/initramfs-linux-fallback.img' '/initramfs-linux-fallback.img'<br />
}<br />
</nowiki>}}<br />
<br />
如果你忘记创建GRUB配置文件{{ic|/boot/grub/grub.cfg}},然后直接重启到了GRUB命令行界面,输入以下命令:<br />
sh:grub> insmod legacycfg<br />
sh:grub> legacy_configfile ${prefix}/menu.lst<br />
选择启动到Arch下然后再重新创建合适的GRUB配置文件{{ic|/boot/grub/grub.cfg}}<br />
<br />
==== 多系统启动 ====<br />
<br />
为了实现多系统启动,需要安装{{Pkg|os-prober}}.安装后,再执行{{ic|grub-mkconfig -o /boot/grub/grub.cfg}}.如果执行失败,可能就需要手动添加启动条目了.(后面有指导)<br />
<br />
{{Note|如果找不到Windows,可以将其boot partition挂载后再试}}<br />
<br />
===== 在BIOS-MBR模式下安装的Microsoft Windows =====<br />
<br />
{{Note|GRUB支持直接从{{ic|bootmgr}}启动,现在不再需要链式加载分区启动扇区了}}<br />
<br />
{{Warning|{{ic|bootmgr}}位于'''系统分区'''('''system partition'''),而不是Windows系统所在的分区(比如C盘).在uuid-卷名列表中,系统分区是那个名为{{ic|LABEL&#61;"SYSTEM RESERVED"}} 或 {{ic|LABEL&#61;"SYSTEM"}}的项,而且这个分区一般只有100到200MB大(和Arch的启动分区差不多).请参考[[Wikipedia:System partition and boot partition]]}}<br />
<br />
本节假设你的Windows分区是{{ic|/dev/sda1}}.首先,找到Windows系统分区的UUID(Windows's SYSTEM PARTITION,{{ic|bootmgr存放其上}}).如果Windows {{ic|bootmgr}}的位置是{{ic|/media/SYSTEM_RESERVED/bootmgr}},对于 Windows Vista/7/8,执行:<br />
<br />
# grub-probe --target=fs_uuid /media/SYSTEM_RESERVED/bootmgr<br />
69B235F6749E84CE<br />
<br />
# grub-probe --target=hints_string /media/SYSTEM_RESERVED/bootmgr<br />
--hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1<br />
<br />
{{Note|对于Windows XP, 请在上面的命令中用{{ic|NTLDR}}替代{{ic|bootmgr}}. 注意,可能{{ic|NTLDR}}不是在名为"SYSTEM_RESERVED"的分区上,请在XP系统所在的分区上寻找 {{ic|NTLDR}}}}<br />
<br />
接着,将下面的代码添加到{{ic|/etc/grub.d/40_custom}} 或者{{ic|/boot/grub/custom.cfg}}中.然后,使用{{ic|grub-mkconfig}}重新生成{{ic|grub.cfg}},这样就能在BIOS-MBR配置下使用GRUB2引导启动Windows系统了:<br />
<br />
对于 Windows Vista/7/8<br />
<br />
if [ "${grub_platform}" == "pc" ]; then<br />
menuentry "Microsoft Windows Vista/7/8 BIOS-MBR" {<br />
insmod part_msdos<br />
insmod ntfs<br />
insmod search_fs_uuid<br />
insmod ntldr <br />
search --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 69B235F6749E84CE<br />
ntldr /bootmgr<br />
}<br />
fi<br />
<br />
对于Windows XP<br />
<br />
if [ "${grub_platform}" == "pc" ]; then<br />
menuentry "Microsoft Windows XP" {<br />
insmod part_msdos<br />
insmod ntfs<br />
insmod search_fs_uuid<br />
insmod ntldr <br />
search --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 69B235F6749E84CE<br />
ntldr /bootmgr<br />
}<br />
fi<br />
<br />
<br />
{{Note|在某些情况下,可能在安装Windows 8之前就已经安装了GRUB.启动Windows时可能会{{ic|\boot\bcd}}报错(错误代码为{{ic|0xc000000f}}).可以通过Wondows Recovery Console来修复这个错误:<br />
x:\> "bootrec.exe /fixboot" <br />
x:\> "bootrec.exe /RebuildBcd".<br />
'''不要'''使用{{ic|bootrec.exe /Fixmbr}},因为那会将GRUB清除掉<br />
}}<br />
<br />
{{ic|/etc/grub.d/40_custom}}可以做为创建{{ic|/etc/grub.d/nn_custom}}的模板.{{ic|nn}}定义了脚本执行的优先级.脚本执行的顺序又决定了grub引导启动目录的内容.<br />
{{Note|{{ic|nn}} 应该比06大,这样才能确保必要的脚本被优先执行}}<br />
<br />
== UEFI 系统==<br />
{{Note|建议阅读并理解[[UEFI]], [[GPT]] and [[UEFI Bootloaders]]}}<br />
===== Check if you have GPT and an ESP检查你是否使用GPT和ESP =====<br />
如果想要使用EFI在某个磁盘上进行启动,那么就需要对这个磁盘进行EFI系统分区(EFI System Partition,即ESP),GPT倒不是必须的,不过我们还是高度建议使用GTP,并且这也是本篇文章当前支持的方法.如果你在一个支持EFI,并且已经有操作系统的电脑上安装Arch(比如Windows 8),那么你已经有了ESP了.可以通过{{ic|parted}}来列出启动磁盘上的分区表以检查其是否支持GPT和ESP(假设这个启动磁盘是/dev/sda)<br />
<br />
# parted /dev/sda print<br />
<br />
如果使用GPT,那么会出现"分区表:GPT".如果使用EFI,那么会有一个文件系统为vfat的小分区(一般小于512MiB)并且被标志为启动分区.在这个小分区上,应该有一个名为EFI的文件夹.如果这些条件都满足,那么这就是ESP了.注意分区序号,因为之后安装GRUB2时你需要mount它.<br />
<br />
===== 建立ESP =====<br />
<br />
如果你没有ESP,请参考[[UEFI#EFI System Partition]]的引导来创建它<br />
<br />
=== 安装 ===<br />
<br />
{{Note|众所周知,不同的主板厂商的UEFI实现方式也不一样.我们鼓励用户将其配置过程中遇到的问题及其细节分享出来.为了保证[[GRUB]]页面的简洁,请参考[[GRUB/EFI examples (简体中文)]]<br />
}}<br />
<br />
首先,安装{{Pkg|grub}}, {{Pkg|dosfstools}}, {{Pkg|efibootmgr}}(后面两个包是为了让GRUB支持EFI).然后按下面的指导进行配置.<br />
{{Note|简单的安装这些包并不会更新{{ic|core.efi}}和ESP中的GRUB模组.请按下面的指导手动进行配置}}<br />
====安装boot文件====<br />
<br />
=====推荐方法=====<br />
<br />
{{Note|<br />
* 下面的命令是假设你正在为{{ic|x86_64-efi}}安装GRUB.(如果是为{{ic|IA32-efi}}安装GRUB,请用{{ic|i386-efi}}代替{{ic|x86_64-efi}})<br />
* 如果要成功执行下面的命令,你需要使用UEFI启动而不是BIOS.如果你从USB驱动器中的ISO文件启动系统,那么可以判断,你已经是从BIOS启动了.你需要[[Unified Extensible Firmware Interface#Create UEFI bootable USB from ISO|建立一个UEFI可启动USB磁盘]],然后重启,否则grub-install会报错.<br />
}}<br />
<br />
首先,将ESP挂载上(一般挂在到{{ic|/boot/efi}}上,然后将其路径赋值到$esp).如果是第一次安装GRUB,你可能需要建立{{ic|/boot/efi}}文件夹<br />
然后,将GRUB UEFI应用安装到{{ic|$esp/EFI/grub}},将其模组安装到{{ic|/boot/grub/x86_64-efi}}:<br />
<br />
# grub-install --target=x86_64-efi --efi-directory=$esp --bootloader-id=grub --recheck --debug<br />
<br />
{{Note|<br />
* 如果grub-install在sysfs和procfs上运行的时候报错"modprobe efivars",尝试[[Unified_Extensible_Firmware_Interface#Switch_to_efivarfs]].<br />
* 如果没有{{ic|--target}} 或{{ic|--directory}} 选项,grub-install就不能决定安装哪种固件.在这个情况下,grub-install会报错{{ic|source_dir does not exist. Please specify --target or --directory}}.<br />
* {{ic|--efi-directory}} and {{ic|--bootloader-id}}是只适用于GRUB UEFI的选项.{{ic|--efi-directory}}替代{{ic|--root-directory}}指定ESP的挂载点.{{ic|--bootloader-id}}指定存放{{ic|grubx64.efi}}文件的目录名<br />
* 可能你会注意到,不像安装GRUB到BIOS那样,在{{ic|grub-install}}命令后没有<device_path>选项(例如{{ic|/dev/sda}}).哪怕提供了<device_path>,也会被安装脚本忽略.因为UEFI bootloader不使用MBR或者分区启动扇区.<br />
}}<br />
<br />
现在GRUB已经安装好了.请移至[[#配置|配置]]一节继续.<br />
<br />
=====其他方法=====<br />
<br />
如果你想将所有的GRUB boot 文件放在ESP中,请给grub-install命令添加{{ic|--boot-directory&#61;$esp/EFI}}选项:<br />
<br />
# grub-install --target=x86_64-efi --efi-directory=$esp --bootloader-id=grub --boot-directory=$esp/EFI --recheck --debug<br />
<br />
下面的命令会将GRUB模组文件放在{{ic|$esp/EFI/grub}}中.使用这个方法,grub.cfg也会被放在ESP中,所以请确保grub-mkconfig指向了正确的地方:<br />
<br />
# grub-mkconfig -o $esp/EFI/grub/grub.cfg<br />
<br />
配置档和BIOS-GRUB是一样的.<br />
<br />
====在固件启动管理器中创建GRUB条目====<br />
<br />
{{ic|grub-install}}会自动尝试在启动管理器中创建GRUB条目.如果没有成功,请参考[[Beginners' guide#GRUB]],里面有关于使用{{ic|efibootmgr}}创建启动目录条目的介绍.一般来说,这个问题都是因为你没有以UEFI模式启动CD/USB造成的.请参考[[UEFI#Create UEFI bootable USB from ISO]].<br />
<br />
====创建GRUB Standalone模式的UEFI应用程序====<br />
<br />
可以建立一个{{ic|grubx64_standalone.efi}},这个应用将所有的模组嵌入自身的memdisk中,所以就不需要使用单独的目录来存放GRUB UEFI模组和其他相关文件了,使用{{Pkg|grub}}包里的{{ic|grub-mkstandalone}}可以实现这个功能.<br />
<br />
最简单的的方法就是使用{{ic|grub-mkstandalone}},不过,我们还可以指定嵌入哪些模组:<br />
<br />
# grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi/" --format="x86_64-efi" --compression="xz" \<br />
--output="$esp/EFI/grub/grubx64_standalone.efi" <你想嵌入的模组><br />
<br />
{{ic|grubx64_standalone.efi}}文件要求将{{ic|grub.cfg}}放置到{{ic|(memdisk)/boot/grub}}中,而这个memdisk是嵌入到EFI应用当中的.{{ic|grub-mkstandlone}}脚本允许传递将要嵌入memdisk的文件列表.(上面命令里的"<你想嵌入的模组>")<br />
<br />
如果你的{{ic|grub.cfg}}的路径是{{ic|/home/user/Desktop/grub.cfg}},那么需要创建一个临时的{{ic|/home/user/Desktop/boot/grub/}}目录,然后将grub.cfg复制到其中并进入这个目录并运行:<br />
<br />
# grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi/" --format="x86_64-efi" --compression="xz" \<br />
--output="$esp/EFI/grub/grubx64_standalone.efi" "boot/grub/grub.cfg"<br />
<br />
进入{{ic|/home/user/Desktop/boot/grub/}}但是传递{{ic|boot/grub/grub.cfg}}参数(请注意是{{ic|boot/}}而不是{{ic|/boot/}})的原因是{{ic|grub-mkstandalone}}会自动将boot/grub/grub.cfg处理为{{ic|/(memdisk)/boot/grub/grub.cfg}}<br />
<br />
如果你传递{{ic|/home/user/Desktop/grub.cfg}},那么处理后的结果会是{{ic|(memdisk)/home/user/Desktop/grub.cfg}}.如果传递{{ic|/home/user/Desktop/boot/grub/grub.cfg}},那么结果就是{{ic|(memdisk)/home/user/Desktop/boot/grub/grub.cfg}}.所以需要进入{{ic|/home/user/Desktop/boot/grub/}}并传递{{ic|boot/grub/grub.cfg}}参数,因为这样才能生成{{ic|grub.efi}}需要的{{ic|(memdisk)/boot/grub/grub.cfg}}.<br />
<br />
如果需要为{{ic|$esp/EFI/arch_grub/grubx64_standalone.efi}}创建一个UEFI启动器条目,使用{{ic|efibootmgr}}.[[#Create GRUB entry in the Firmware Boot Manager]]里有介绍<br />
<br />
==配置==<br />
<br />
可以自动生成配置档,也可以手动编辑{{ic|grub.cfg}}.<br />
{{Note|<br />
* 对于EFI系统,如果在安装GRUB时使用 {{ic|1=--boot-directory=$esp/EFI}}了选项,{{ic|grub.cfg}}文件必须和{{ic|grubx64.efi}}放在同一文件夹.如果没有使用这个选项,那么{{ic|grub.cfg}}应当在{{ic|/boot/grub/}}下,和GRUB BIOS系统一样.<br />
*在[http://members.iinet.net/~herman546/p20/GRUB2%20Configuration%20File%20Commands.html 这里]可以找到关于如何配置GRUB的完整描述.<br />
}}<br />
<br />
===使用grub-mkconfig自动生成配置档===<br />
<br />
和GRUB Legacy配置文件{{ic|menu.lst}}对应的GRUB配置文件是{{ic|/etc/default/grub}} 和{{ic|/etc/grub.d/*}}.{{ic|grub-mkconfig}}使用这些文件来生成{{ic|grub.cfg}}.grub-mkconfig默认输出至stdout.以下命令可以生成一个{{ic|grub.cfg}}文件:<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|/etc/grub.d/10_linux}}会自动为Arch Linux设置启动项.其他操作系统的话,可能需要手动在{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}中设置.<br />
<br />
{{Note|如果你在一个chroot环境中或者systemd-nspawn容器里执行这些操作,可能完全不会成功,因为grub-probe不能获取'/dev/sdaX'这样的典型路径.这种情况下,可以尝试使用[https://bbs.archlinux.org/viewtopic.php?pid&#61;1225067#p1225067 arch-chroot].}}<br />
<br />
====额外的参数====<br />
<br />
在{{ic|/etc/default/grub}}中设置{{ic|GRUB_CMDLINE_LINUX}}和{{ic|GRUB_CMDLINE_LINUX_DEFAULT}}变量可以实现将向Linux镜像传递额外的参数.生成{{ic|grub.cfg}}时,如果遇到普通启动项,这两个参数会一起使用,遇到''recovery''启动项,就只使用{{ic|GRUB_CMDLINE_LINUX}}参数.<br />
<br />
没有必要两者一起使用.这两个参数很有用.比如,如果要系统支持休眠后恢复,需要使用{{ic|<nowiki>GRUB_CMDLINE_LINUX_DEFAULT="resume=/dev/sdaX</nowiki> quiet"}} ({{ic|sda'''X'''}}是交换分区).这个选项会生成一个recovery启动项,这个启动项没有resume和quiet参数.其他的普通启动项也可能使用它们.(GRUB会为每个内核生成两个启动项,一个默认的一个recovery的,GRUB_CMDLINE_LINUX指定的参数会传递给这两个启动项.GRUB_CMDLINE_LINUX_DEFAULT指定的参数只会传递给默认启动项)<br />
<br />
当生成GRUB recovery启动项时,你必须在{{ic|/etc/default/grub}}中将{{ic|<nowiki>#GRUB_DISABLE_RECOVERY=true</nowiki>}} 注释掉.<br />
<br />
你也可以使用{{ic|<nowiki>GRUB_CMDLINE_LINUX="resume=/dev/disk/by-uuid/${swap_uuid}"</nowiki>}}, {{ic|${swap_uuid} }}是交换分区的[[Persistent_block_device_naming|UUID]] <br />
<br />
不同的启动项使用双引号引起来并用空格隔开.所以,对于即想使用resume又想使用systemd的用户来说,这个参数的设置可能是:<br />
{{ic|<nowiki>GRUB_CMDLINE_LINUX="resume=/dev/sdaX init=/usr/lib/systemd/systemd"</nowiki>}}<br />
更多信息请参考[[Kernel parameters]].<br />
<br />
=== 手动创建 grub.cfg ===<br />
<br />
{{Warning|''不推荐''编辑这个文件.这个文件由{{ic|grub-mkconfig}}生成,最好编辑{{ic|/etc/default/grub}}和{{ic|/etc/grub.d}}文件夹下的脚本以实现修改.}}<br />
<br />
基本的GRUB配置文件使用如下选项:<br />
* {{ic|(hd''X'',''Y'')}} 是''X''磁盘的''Y''分区,分区从1开始计数,磁盘从0开始计数.<br />
* {{ic|1=set default=''N''}}设定用户选择超时时间过后的默认启动项<br />
* {{ic|1=set timeout=''M''}}设定用户选择超时时间(秒).<br />
* {{ic|<nowiki>menuentry "title" {entry options}</nowiki>}}设置一个名为{{ic|title}}的启动项<br />
* {{ic|1=set root=(hd''X'',''Y'')}}设定启动分区(kernel和GRUB模组所在磁盘),/boot没被要求独占一个分区,有可能就是root分区下的一个文件夹<br />
示例配置如下:<br />
<br />
{{hc|/boot/grub/grub.cfg|<nowiki><br />
# Config file for GRUB - The GNU GRand Unified Bootloader<br />
# /boot/grub/grub.cfg<br />
<br />
# DEVICE NAME CONVERSIONS<br />
#<br />
# Linux Grub<br />
# -------------------------<br />
# /dev/fd0 (fd0)<br />
# /dev/sda (hd0)<br />
# /dev/sdb2 (hd1,2)<br />
# /dev/sda3 (hd0,3)<br />
#<br />
<br />
# Timeout for menu<br />
set timeout=5<br />
<br />
# Set default boot entry as Entry 0<br />
set default=0<br />
<br />
# (0) Arch Linux<br />
menuentry "Arch Linux" {<br />
set root=(hd0,1)<br />
linux /vmlinuz-linux root=/dev/sda3 ro<br />
initrd /initramfs-linux.img<br />
}<br />
<br />
## (1) Windows<br />
#menuentry "Windows" {<br />
# set root=(hd0,3)<br />
# chainloader +1<br />
#}<br />
</nowiki>}}<br />
<br />
=== 双启动 ===<br />
<br />
{{Note|如果你希望GRUB自动搜寻其他系统, 可以安装{{Pkg|os-prober}}.}}<br />
<br />
==== 使用/etc/grub.d/40_custom 和 grub-mkconfig自动生成 ====<br />
<br />
添加其他启动项的最好方法是编辑{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}.运行{{ic|grub-mkconfig}}后,这些文件中的启动项会被自动添加到grub.cfg中.<br />
更新了这些文件后,运行:<br />
{{bc|<nowiki># grub-mkconfig -o /boot/grub/grub.cfg</nowiki>}}<br />
在UEFI-GPT模式下,运行:<br />
{{bc|<nowiki># grub-mkconfig -o /boot/efi/EFI/GRUB/grub.cfg</nowiki>}}<br />
这样会更新{{ic|grub.cfg}}<br />
<br />
<br />
一个典型{{ic|/etc/grub.d/40_custom}}文件类似于下面这个为[http://h10025.www1.hp.com/ewfrf/wc/product?cc=us&destPage=product&lc=en&product=5402703&tmp_docname= HP Pavilion 15-e056sl Notebook PC|预装WIN8的HP Pavilion 15-e056sl Notebook PC]而作的配置.每个启动项的结构都应该和下面的类似.请注意UEFI分区{{ic|/dev/sda2}} 被命名为{{ic|hd0,gpt2}} 和{{ic|ahci0,gpt2}}(请参考[[GRUB#Windows_Installed_in_UEFI-GPT_Mode_menu_entry|here]]获取更多信息)<br />
<br />
'''/etc/grub.d/40_custom''':<br />
<br />
{{bc|<nowiki>#!/bin/sh<br />
exec tail -n +3 $0<br />
# This file provides an easy way to add custom menu entries.&nbsp; Simply type the<br />
# menu entries you want to add after this comment.&nbsp; Be careful not to change<br />
# the 'exec tail' line above.<br />
<br />
menuentry "HP / Microsoft Windows 8.1" {<br />
echo "Loading HP / Microsoft Windows 8.1..."<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 763A-9CB6<br />
chainloader (${root})/EFI/Microsoft/Boot/bootmgfw.efi<br />
}<br />
<br />
menuentry "HP / Microsoft Control Center" {<br />
echo "Loading HP / Microsoft Control Center..."<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 763A-9CB6<br />
chainloader (${root})/EFI/HP/boot/bootmgfw.efi<br />
}<br />
<br />
menuentry "System shutdown" {<br />
echo "System shutting down..."<br />
halt<br />
}<br />
<br />
menuentry "System restart" {<br />
echo "System rebooting..."<br />
reboot<br />
}</nowiki>}}<br />
<br />
===== GNU/Linux 启动项 =====<br />
<br />
假设另一个发行版位于{{ic|sda2}}:<br />
<br />
{{bc|<nowiki>menuentry "Other Linux" {<br />
set root=(hd0,2)<br />
linux /boot/vmlinuz (add other options here as required)<br />
initrd /boot/initrd.img (if the other kernel uses/needs one)<br />
}</nowiki>}}<br />
<br />
===== FreeBSD 启动项 =====<br />
<br />
要求FreeBSD以UFS格式被安装在单独的分区上,假设安装在{{ic|sda4}}上:<br />
<br />
{{bc|<nowiki>menuentry "FreeBSD" {<br />
set root=(hd0,4)<br />
chainloader +1<br />
}</nowiki>}}<br />
<br />
===== Windows XP 启动项=====<br />
<br />
假设Windows分区是{{ic|sda3}}.请记住需要将root设置到Windows安装时建立的启动分区上,然后链式加载它,而不是Windows系统所在分区.下面的例子就是假设启动分区就是{{ic|sda3}}.<br />
{{bc|<nowiki># (2) Windows XP<br />
menuentry "Windows XP" {<br />
set root="(hd0,3)"<br />
chainloader +1<br />
}</nowiki>}}<br />
<br />
如果Windows的bootloader和GRUB不在一个硬盘上,那么需要让Windows认为它是在第一硬盘上.使用{{ic|drivemap}}可以实现这点.假设GRUB在{{ic|hd0}}而windows在{{ic|hd2}}上,需要在设定root后执行:<br />
<br />
{{bc|<nowiki>drivemap -s hd0 hd2</nowiki>}}<br />
<br />
===== UEFI-GPT 模式下安装的Windows的启动项 =====<br />
<br />
{{bc|<nowiki>if [ "${grub_platform}" == "efi" ]; then<br />
menuentry "Microsoft Windows Vista/7/8 x86_64 UEFI-GPT" {<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root $hints_string $uuid<br />
chainloader /EFI/Microsoft/Boot/bootmgfw.efi<br />
}<br />
fi</nowiki>}}<br />
<br />
{{ic|$hints_string}} 和 {{ic|$uuid}}可以通过以下命令获取:<br />
<br />
{{ic|$uuid}}:<br />
<br />
{{bc|<nowiki># grub-probe --target=fs_uuid $esp/EFI/Microsoft/Boot/bootmgfw.efi<br />
1ce5-7f28</nowiki>}}<br />
<br />
{{ic|$hints_string}}:<br />
<br />
{{bc|<nowiki># grub-probe --target=hints_string $esp/EFI/Microsoft/Boot/bootmgfw.efi<br />
--hint-bios=hd0,gpt1 --hint-efi=hd0,gpt1 --hint-baremetal=ahci0,gpt1</nowiki>}}<br />
<br />
这两个命令都是假设ESP挂载在{{ic|$esp}}上.当然,Windows的EFI文件路径可能有变,因为这就是Windows....<br />
<br />
===== "Shutdown" 启动项 =====<br />
<br />
{{bc|<nowiki>menuentry "System shutdown" {<br />
echo "System shutting down..."<br />
halt<br />
}</nowiki>}}<br />
<br />
===== "Restart" 启动项 =====<br />
<br />
{{bc|<nowiki>menuentry "System restart" {<br />
echo "System rebooting..."<br />
reboot<br />
}</nowiki>}}<br />
<br />
==== 通过EasyBCD NeoGRUB 和Windows共存 ====<br />
<br />
现在EasyBCD的NeoGRUB还不能识别GRUB的目录格式,在 {{ic|C:\NST\menu.lst}} 中添加如下行以链式加载到GRUB:<br />
<br />
default 0<br />
timeout 1<br />
<br />
title Chainload into GRUB v2<br />
root (hd0,7)<br />
kernel /boot/grub/i386-pc/core.img<br />
<br />
最后,使用{{ic|grub-mkconfig}}重建{{ic|grub.cfg}}<br />
<br />
===可视化配置===<br />
<br />
GRUB默认就支持定制启动目录的外观.不过要确保使用合适的视频模式初始化GRUB的图形化终端gfxterm.在[[#"No suitable mode found" error]]一节中有介绍.GRUB通过'gfxpayload'来将视频模式传递给linux内核,所以任何可视化配置都需要这个模式的信息以正确工作.<br />
<br />
==== 设定帧缓冲的分辨率 ====<br />
<br />
<br />
GRUB既可以为自己,也可以为内核设定帧缓冲.现在已经不使用老的{{ic|1=vga=}}配置了.推荐方法是在{{ic|/etc/default/grub}}进行如下编辑:<br />
<br />
GRUB_GFXMODE=1024x768x32<br />
GRUB_GFXPAYLOAD_LINUX=keep<br />
<br />
运行以下命令使配置生效: <br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|gfxpayload}}属性会确保内核也保持这个分辨率<br />
<br />
<br />
{{Note|<br />
* 如果示例不起作用,请尝试用{{ic|1=vbemode="0x105"}}代替{{ic|1=gfxmode="1024x768x32"}}.请使用适合你屏幕的分辨率.<br />
* 可以通过{{ic|1=# hwinfo --framebuffer}}命令来显示所有可以使用的分辨率模式(hwinfo在[[community]]里),在GRUB命令行下,可以使用{{ic|1=vbeinfo}} 命令<br />
}}<br />
<br />
这种方法不管用的话,可以试试老的{{ic|1=vga=}}方法.将它添加到{{ic|1="GRUB_CMDLINE_LINUX_DEFAULT="}}里面就行了,比如<br />
{{ic|1="GRUB_CMDLINE_LINUX_DEFAULT="quiet splash vga=792"}} <br />
这会将系统的分辨率设定为1024*768<br />
<br />
可以选择以下分辨率中的一种:{{ic|640×480}}, {{ic|800×600}}, {{ic|1024×768}}, {{ic|1280×1024}}, {{ic|1600×1200}}, {{ic|1920×1200}}<br />
<br />
==== 915resolution hack ====<br />
<br />
有些时候,Intel显卡无法通过{{ic|1=# hwinfo --framebuffer}} 或{{ic|1=vbeinfo}}显示你需要的分辨率.这种情况下,你可以使用{{ic|915resolution hack}}.915resolution hack会临时性的修改显卡BIOS来添加所需的分辨率.详情请参考[http://915resolution.mango-lang.org/ 915resolution's home page] <br />
<br />
首先,找一个你想要替代的视频模式.例如在GRUB命令行模式下运行:<br />
{{hc|sh:grub> 915resolution -l|<br />
Intel 800/900 Series VBIOS Hack : version 0.5.3<br />
[...]<br />
'''Mode 30''' : 640x480, 8 bits/pixel<br />
[...]<br />
}}<br />
然后,使用{{ic|1440x900}} 分辨率覆盖{{ic|Mode 30}}<br />
{{hc|/etc/grub.d/00_header|<br />
[...]<br />
'''915resolution 30 1440 900 # Inserted line'''<br />
set gfxmode&#61;${GRUB_GFXMODE}<br />
[...]<br />
}}<br />
最后,设置{{ic|GRUB_GFXMODE}},重新生成GRUB配置文件,重启并测试是否生效:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
# reboot<br />
<br />
==== 背景图像和点阵字体 ====<br />
<br />
GRUB原生支持设置背景图像和点阵字体(以pf2格式).{{Pkg|grub}}包含unifont字体,名为{{ic|unicode.pf2}}.(也有可能只包含名为{{ic|ascii.pf2}}的ASCII字符字体)<br />
<br />
GRUB支持的图像格式有tga,png,jpeg.所支持的最大图像分辨率跟硬件有关.<br />
<br />
Make sure you have set up the proper [[#Setting the framebuffer resolution|framebuffer resolution]].<br />
请确保你已经设定了合适的[[#设定帧缓冲的分辨率|帧缓冲分辨率]]<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
GRUB_BACKGROUND="/boot/grub/myimage"<br />
#GRUB_THEME="/path/to/gfxtheme"<br />
GRUB_FONT="/path/to/font.pf2"<br />
<br />
{{Note|如果你将GRUB安装在单独的分区上, {{ic|/boot/grub/myimage}} 应该改为 {{ic|/grub/myimage}}.}}<br />
<br />
重新生成配置文件:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
如果成功的添加了背景图片,那么用户会在命令行中看到{{ic|"Found background image..."}}.<br />
如果没有看到{{ic|"Found background image..."}},图像信息就可能没有嵌入{{ic|grub.cfg}}中了.<br />
<br />
如果图像没有正确显示,执行如下检查:<br />
* 在{{ic|/etc/default/grub}}里,图像的路径和名字是否正确<br />
* 图像的大小和格式是否合适(tga,png,8-bit jpg)<br />
* 图像是不是以RGB模式存储,是不是没有索引<br />
* {{ic|/etc/default/grub}}里面是不是没有开启console模式<br />
* 是否执行{{ic|grub-mkconfig}}以重新生成配置文件<br />
<br />
==== 主题====<br />
<br />
下面的例子将展示如何使用GRUB包重的starfield主题:<br />
<br />
编辑 {{ic|/etc/default/grub}}<br />
GRUB_THEME="/usr/share/grub/themes/starfield/theme.txt"<br />
<br />
重生成配置:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
配置成功的话,在重生成配置过程中,会出现{{ic|Found theme: /usr/share/grub/themes/starfield/theme.txt}}.使用主题就不会使用之前的背景图像<br />
<br />
====目录颜色====<br />
<br />
GRUB支持设置目录颜色.可使用的颜色能从[https://www.gnu.org/software/grub/manual/html_node/Theme-file-format.html the GRUB Manual]里面找到.示例如下:<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
GRUB_COLOR_NORMAL="light-blue/black"<br />
GRUB_COLOR_HIGHLIGHT="light-cyan/blue"<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
====隐藏目录====<br />
<br />
GRUB特性之一就是支持隐藏/跳过目录,同时支持按{{ic|Esc}}来打断隐藏/跳过.同时还支持设置是否显示timeout计时器<br />
下面的例子设置5s钟内没有按Esc键就启动默认的启动项,并且在屏幕上显示倒计时:<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
<br />
GRUB_HIDDEN_TIMEOUT=5<br />
GRUB_HIDDEN_TIMEOUT_QUIET=false<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
==== 禁用framebuffer ====<br />
<br />
Users who use NVIDIA proprietary driver might wish to disable GRUB's framebuffer as it can cause problems with the binary driver.<br />
使用NVIDIA私有驱动的用户可能希望禁用GRUB的framebuffer,因为会导致驱动错误.<br />
<br />
在{{ic|/etc/default/grub}}中添加(如果已经有背注释掉的这行,就取消注释):<br />
GRUB_TERMINAL_OUTPUT=console<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
如果你想保留GRUB的framebuffer,解决方法是在GRUB载入内核前进入文字模式.可以通过在{{ic|/etc/default/grub}}设置如下:<br />
GRUB_GFXPAYLOAD_LINUX=text<br />
然后重建配置档.<br />
<br />
=== 其他选项 ===<br />
<br />
==== LVM ====<br />
<br />
如果使用[[LVM]]做为启动设备,那么在启动项里添加:<br />
<br />
insmod lvm<br />
<br />
然后在启动项里指定root:<br />
<br />
set root=lvm/''lvm_group_name''-''lvm_logical_boot_partition_name''<br />
<br />
示例如下:<br />
<br />
# (0) Arch Linux<br />
menuentry "Arch Linux" {<br />
insmod lvm<br />
set root=lvm/VolumeGroup-lv_boot<br />
# you can only set following two lines<br />
linux /vmlinuz-linux root=/dev/mapper/VolumeGroup-root ro<br />
initrd /initramfs-linux.img<br />
}<br />
<br />
==== 阵列====<br />
<br />
通过添加{{ic|insmod mdraid}},GRUB能够很方便的处理磁盘阵列.比如{{ic|/dev/md0}}:<br />
<br />
set root=(md0)<br />
<br />
阵列上的分区(比如{{ic|/dev/md0p1}})则为:<br />
<br />
set root=(md0,1)<br />
<br />
如果启动分区在raid1上,想要安装grub,只需要在两个磁盘上分别运行grub-install即可:<br />
<br />
grub-install --target=i386-pc --recheck --debug /dev/sda && grub-install --target=i386-pc --recheck --debug /dev/sdb<br />
<br />
这时/dev/sda和/dev/sdb就都有了raid1的启动文件夹/boot了<br />
<br />
==== 持久块设备命名法 ====<br />
<br />
[[Persistent block device naming|持久块设备命名法]](Persistent block device naming)的一个目的是使用全局的UUID来区分分区,而不是用老的{{ic|/dev/sd*}}表示法.好处显而易见.<br />
<br />
GRUB默认使用持久块设备命名法<br />
<br />
{{Note|每次文件系统调整过后,就需要用新的UUID来更新{{ic|/boot/grub.cfg}}.通过Live-CD调整分区和文件系统后要特别注意这点}}<br />
<br />
是否使用UUID由{{ic|/etc/default/grub}}里的这个选项控制:<br />
<br />
# GRUB_DISABLE_LINUX_UUID=true<br />
<br />
无论如何,变更配置后请重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
==== 使用卷标 ====<br />
<br />
GRUB支持以卷标识别文件系统(通过{{ic|search}}命令的{{ic|--label参数}}).<br />
<br />
首先,给文件系统设置一个卷标:<br />
<br />
# tune2fs -L ''LABEL'' ''PARTITION''<br />
<br />
然后在启动项中使用这个卷标:<br />
<br />
menuentry "Arch Linux, session texte" {<br />
search --label --set=root archroot<br />
linux /boot/vmlinuz-linux root=/dev/disk/by-label/archroot ro<br />
initrd /boot/initramfs-linux.img<br />
}<br />
<br />
====调用之前的启动项====<br />
<br />
GRUB能够记住你当前使用的启动项并且在下次启动时将其作为默认项.当你使用多个内核或操作系统时,这个特性很有用.<br />
编辑{{ic|/etc/default/grub}}中的{{ic|GRUB_DEFAULT}}选项:<br />
<br />
GRUB_DEFAULT=saved<br />
<br />
上面的命令会告诉GRUB使用记住的启动项为默认启动项.<br />
将下面的行添加到{{ic|/etc/default/grub}}会让GRUB记住当前的启动项:<br />
<br />
GRUB_SAVEDEFAULT=true<br />
<br />
{{Note|手动添加启动项到{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}中,比如Windows启动项,需要添加{{ic|savedefault}}}}<br />
请记住重建配置档.<br />
<br />
==== 改变默认启动项 ====<br />
<br />
可以通过修改{{ic|/etc/default/grub}}中的{{ic|GRUB_DEFAULT}}值来改变默认启动项<br />
<br />
GRUB_DEFAULT=0<br />
<br />
GRUB启动项序号从0开始计数.0代表第一个启动项.<br />
<br />
除了使用启动项序号,也可以使用启动项名:<br />
<br />
GRUB_DEFAULT='Arch Linux, with Linux core repo kernel'<br />
<br />
{{Note|请记住重建配置档}}<br />
<br />
==== 安全====<br />
<br />
如果你想禁止其他人改变启动参数或者使用GRUB命令行,可以给GRUB配置添加用户名/密码.<br />
运行 {{ic|grub-mkpasswd-pbkdf2}},输入密码:<br />
{{hc|grub-mkpasswd-pbkdf2|<br />
[...]<br />
Your PBKDF2 is grub.pbkdf2.sha512.10000.C8ABD3E93C4DFC83138B0C7A3D719BC650E6234310DA069E6FDB0DD4156313DA3D0D9BFFC2846C21D5A2DDA515114CF6378F8A064C94198D0618E70D23717E82.509BFA8A4217EAD0B33C87432524C0B6B64B34FBAD22D3E6E6874D9B101996C5F98AB1746FE7C7199147ECF4ABD8661C222EEEDB7D14A843261FFF2C07B1269A<br />
}}<br />
然后将下面的内容添加到{{ic|/etc/grub.d/00_header}}:<br />
{{hc|/etc/grub.d/00_header|<br />
cat << EOF<br />
<br />
set superusers<nowiki>=</nowiki>"'''username'''"<br />
password_pbkdf2 '''username''' '''<password>'''<br />
<br />
EOF<br />
}}<br />
{{Note|不能直接将<br />
set superusers<nowiki>=</nowiki>"'''username'''"<br />
password_pbkdf2 '''username''' '''<password>'''<br />
添加到/etc/grub.d/00_header中去,而必须使用上述方法.否则会报错<br />
password_pbkdf2: not found<br />
}}<br />
{{ic|<password>}}是{{ic|grub-mkpasswd_pbkdf2}}生成的那个加密过后密码.<br />
<br />
然后重建配置档.其他用户没有密码就不能变更GRUB配置或者使用GRUB命令行了.<br />
<br />
可以参考[https://www.gnu.org/software/grub/manual/grub.html#Security the GRUB manual]中的"Security"部分来进行更多的客制化安全设定<br />
<br />
==== root加密 ====<br />
<br />
将{{ic|1=cryptdevice=/dev/yourdevice:label}}添加到{{ic|/etc/default/grub}}中的{{ic|GRUB_CMDLINE_LINUX}}配置项,可以让GRUB传递参数给内核,让内核对root加密:<br />
<br />
{{Tip|如果你使用从GRUB Legacy升级而来,检查{{ic|/boot/grub/menu.lst.pacsave}}以获取正确的device/label. 在{{ic|kernel /vmlinuz-linux}}后去找label.}}<br />
<br />
将root映射到{{ic|/dev/mapper/root}}:<br />
<br />
GRUB_CMDLINE_LINUX="cryptdevice=/dev/sda2:root"<br />
<br />
当然,需要在rootfs上禁用UUID:<br />
<br />
GRUB_DISABLE_LINUX_UUID=true<br />
<br />
Regenerate the configuration.<br />
<br />
==== 设定下次启动的启动项(一次性,非持久) ====<br />
<br />
命令{{ic|grub-reboot}}可以设置下次启动时启动哪个启动项而不必修改配置文件或者在启动时手动选择.这个设置是一次性的,即不会改变GRUB的默认启动项.<br />
<br />
{{Note|需要在{{ic|/etc/default/grub}}中设定 {{ic|1=GRUB_DEFAULT=saved}},然后重建配置档.在手动生成的 {{ic|grub.cfg}}中, 使用 {{ic|1=set default="${saved_entry}"}}.}}<br />
<br />
==== 启动时隐藏GRUB界面,除非按着SHIFT键 ====<br />
<br />
为了获取更快的启动速度,而不用等GRUB倒计时,可以命令GRUB在启动时隐藏目录,除非SHIFT被按着.<br />
将如下行添加到{{ic|/etc/default/grub}}:<br />
<br />
GRUB_FORCE_HIDDEN_MENU="true"<br />
<br />
然后创建如下文件:<br />
<br />
{{hc|/etc/grub.d/31_hold_shift|<nowiki><br />
#! /bin/sh<br />
set -e<br />
<br />
# grub-mkconfig helper script.<br />
# Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.<br />
#<br />
# GRUB is free software: you can redistribute it and/or modify<br />
# it under the terms of the GNU General Public License as published by<br />
# the Free Software Foundation, either version 3 of the License, or<br />
# (at your option) any later version.<br />
#<br />
# GRUB is distributed in the hope that it will be useful,<br />
# but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />
# GNU General Public License for more details.<br />
#<br />
# You should have received a copy of the GNU General Public License<br />
# along with GRUB. If not, see <http://www.gnu.org/licenses/>.<br />
<br />
prefix="/usr"<br />
exec_prefix="${prefix}"<br />
datarootdir="${prefix}/share"<br />
<br />
export TEXTDOMAIN=grub<br />
export TEXTDOMAINDIR="${datarootdir}/locale"<br />
source "${datarootdir}/grub/grub-mkconfig_lib"<br />
<br />
found_other_os=<br />
<br />
make_timeout () {<br />
<br />
if [ "x${GRUB_FORCE_HIDDEN_MENU}" = "xtrue" ] ; then <br />
if [ "x${1}" != "x" ] ; then<br />
if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then<br />
verbose=<br />
else<br />
verbose=" --verbose"<br />
fi<br />
<br />
if [ "x${1}" = "x0" ] ; then<br />
cat <<EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if keystatus; then<br />
if keystatus --shift; then<br />
set timeout=-1<br />
else<br />
set timeout=0<br />
fi<br />
else<br />
if sleep$verbose --interruptible 3 ; then<br />
set timeout=0<br />
fi<br />
fi<br />
fi<br />
EOF<br />
else<br />
cat << EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if sleep$verbose --interruptible ${GRUB_HIDDEN_TIMEOUT} ; then<br />
set timeout=0<br />
fi<br />
fi<br />
EOF<br />
fi<br />
fi<br />
fi<br />
}<br />
<br />
adjust_timeout () {<br />
if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then<br />
cat <<EOF<br />
if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then<br />
EOF<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_BUTTON}"<br />
echo else<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}"<br />
echo fi<br />
else<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}"<br />
fi<br />
}<br />
<br />
adjust_timeout<br />
<br />
cat <<EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if keystatus; then<br />
if keystatus --shift; then<br />
set timeout=-1<br />
else<br />
set timeout=0<br />
fi<br />
else<br />
if sleep$verbose --interruptible 3 ; then<br />
set timeout=0<br />
fi<br />
fi<br />
fi<br />
EOF<br />
</nowiki>}}<br />
<br />
===在GRUB中直接从ISO启动===<br />
<br />
编辑{{ic|/etc/grub.d/40_custom}} 或 {{ic|/boot/grub/custom.cfg}},给目标ISO添加一个启动项.然后使用{{ic|grub-mkconfig -o /boot/grub/grub.cfg}}更新配置档<br />
<br />
==== Arch ISO ====<br />
<br />
{{Note|下面的例子都是假设ISO文件位于{{ic|hd0,6}}分区上的{{ic|/archives}}文件夹里}}<br />
{{Tip|For thumbdrives, use something like {{ic|(hd1,$partition)}} and either {{ic|/dev/sdb'''Y'''}} for the {{ic|img_dev}} parameter or [[Persistent block device naming|a persistent name]], e.g. {{ic|img_dev&#61;/dev/disk/by-label/CORSAIR}}.}}<br />
{{Tip|对于闪存,使用{{ic|(hd1,$partition)}}或{{ic|/dev/sdb'''Y'''}}作为{{ic|img_dev}}参数的值,或者使用持久块设备命名法命名的设备,比如{{ic|img_dev&#61;/dev/disk/by-label/CORSAIR}}.}}<br />
===== x86_64 =====<br />
<br />
menuentry "Archlinux-2013.05.01-dual.iso" --class iso {<br />
set isofile="/archives/archlinux-2013.05.01-dual.iso"<br />
set partition="6"<br />
loopback loop (hd0,$partition)/$isofile<br />
linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop<br />
initrd (loop)/arch/boot/x86_64/archiso.img<br />
}<br />
<br />
===== i686 =====<br />
<br />
menuentry "Archlinux-2013.05.01-dual.iso" --class iso {<br />
set isofile="/archives/archlinux-2013.05.01-dual.iso"<br />
set partition="6"<br />
loopback loop (hd0,$partition)/$isofile<br />
linux (loop)/arch/boot/i686/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop<br />
initrd (loop)/arch/boot/i686/archiso.img<br />
}<br />
<br />
==== Ubuntu ISO ====<br />
<br />
{{Note|下面的例子都是假设ISO文件位于{{ic|hd0,6}}分区上的{{ic|/archives}}文件夹里. 用户需要根据自己系统的实际情况调整.}}<br />
<br />
menuentry "ubuntu-13.04-desktop-amd64.iso" {<br />
set isofile="/archives/ubuntu-13.04-desktop-amd64.iso"<br />
loopback loop (hd0,6)/$isofile<br />
linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash --<br />
initrd (loop)/casper/initrd.lz<br />
}<br />
<br />
menuentry "ubuntu-12.04-desktop-amd64.iso" {<br />
set isofile="/archives/ubuntu-12.04-desktop-amd64.iso"<br />
loopback loop (hd0,6)/$isofile<br />
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash --<br />
initrd (loop)/casper/initrd.lz<br />
}<br />
<br />
==== Other ISOs ====<br />
<br />
可从[http://askubuntu.com/questions/141940/how-to-boot-live-iso-images 这里]获取其他ISO的配置.<br />
<br />
== 使用GRUB命令行 ==<br />
<br />
MBR太小,所以不足以存储所有的GRUB模组.MBR里面只有启动目录配置和一些很基本的命令.GRUB的主要功能通过{{ic|/boot/grub}}里的模组实现,而且可以按需加载.出现错误时,GRUB可能不能引导启动(比如,磁盘分区发生了变化).这时候,一般会出现命令行界面.<br />
<br />
GRUB不止提供一个shell.如果GRUB不能读取到启动目录配置,但是能找到磁盘,你很可能需要进入"normal" shell:<br />
sh:grub><br />
如果有更严重的问题(比如,GRUB找不到必须的文件了),你就可能需要进入"rescue" shell:<br />
grub rescue><br />
<br />
"rescue" shell是"normal" shell的一个子集,其支持的功能更少.如果不幸进入了"rescue" shell里,首先记载"normal"模组,然后启动"normal" shell:<br />
grub rescue> set prefix=(hdX,Y)/boot/grub<br />
grub rescue> insmod (hdX,Y)/boot/grub/i386-pc/normal.mod<br />
rescue:grub> normal<br />
<br />
=== 分页支持 ===<br />
<br />
GRUB支持对长输出进行分页(比如运行{{ic|help}}的输出).不过只有normal shell支持分页,而rescue shell不支持.开启分页支持的方法如下:<br />
<br />
sh:grub> set pager=1<br />
<br />
===使用命令行引导操作系统 ===<br />
<br />
grub> <br />
<br />
可以使用GRUB命令行引导操作系统,一个典型的应用场景是通过'''chainloading'''来引导另一个Windows或Linux<br />
<br />
''ChainLoading''的意思是用当前的bootloader去载入另一个bootloader,所以叫做'''链式'''加载.这个bootloader可能位于MBR,也可能在另一个分区的引导扇区上.<br />
<br />
==== 链式加载一个分区 ====<br />
<br />
set root=(hdX,Y)<br />
chainloader +1<br />
boot<br />
<br />
X=0,1,2...<br />
Y=1,2,3...<br />
<br />
比如链式加载一个位于首磁盘,首分区上的Windows:<br />
<br />
set root=(hd0,1)<br />
chainloader +1<br />
boot<br />
<br />
也可以使用GRUB链式加载另一个分区引导扇区上的GRUB.<br />
<br />
==== 链式加载磁盘====<br />
<br />
set root=hdX<br />
chainloader +1<br />
boot<br />
<br />
====正常载入 ====<br />
<br />
请参考[[#使用应急命令行]]<br />
<br />
==图形化配置工具==<br />
<br />
* {{App|grub-customizer|定制bootloader(GRUB or BURG)|https://launchpad.net/grub-customizer|{{AUR|grub-customizer}}}}<br />
* {{App|grub2-editor|KDE4上配置GRUB的控制模组|http://kde-apps.org/content/show.php?content&#61;139643|{{AUR|grub2-editor}}{{Broken package link|{{aur-mirror|grub2-editor}}}}}}<br />
* {{App|kcm-grub2|可以管理大部分常用GRUB配置的kcm模组|http://kde-apps.org/content/show.php?content&#61;137886|{{AUR|kcm-grub2}}{{Broken package link|{{aur-mirror|kcm-grub2}}}}}}<br />
* {{App|startupmanager|GRUB Legacy, GRUB, Usplash and Splashy的图形化配置工具([https://launchpad.net/startup-manager/+announcement/8300 abandonned])|http://sourceforge.net/projects/startup-manager/|{{AUR|startupmanager}}}}<br />
<br />
== parttool ==<br />
<br />
如果你安装了Windows 9x系列操作系统,而它们隐藏了磁盘(比如C盘),GRUB能够使用parttool来设置是否隐藏.比如,想从三个Windows 9x系统的第三个启动,可以这样:<br />
parttool hd0,1 hidden+ boot-<br />
parttool hd0,2 hidden+ boot-<br />
parttool hd0,3 hidden- boot+<br />
set root=hd0,3<br />
chainloader +1<br />
boot<br />
<br />
==使用应急命令行==<br />
<br />
请先阅读[[#使用GRUB命令行]].如果无法进入命令行,请尝试使用Live CD或者其他rescue磁盘引导,然后修正错误.不过有些时候我们手上没有此类rescue磁盘,这时,应急命令行(rescue console)就可以派上用场了.<br />
<br />
GRUB应急命令行里可用的命令有{{ic|insmod}}, {{ic|ls}}, {{ic|set}}, {{ic|unset}}.可以使用set/unset修改变量,使用insmod来载入模组.<br />
<br />
首先,用户必须知道启动分区({{ic|/boot}}所在位置然后设置:<br />
<br />
grub rescue> set prefix=(hdX,Y)/boot/grub<br />
<br />
通过加载{{ic|linux}}模组来扩展命令行的功能:<br />
<br />
grub rescue> insmod (hdX,Y)/boot/grub/linux.mod<br />
<br />
{{Note|如果/boot是在单独的分区上,请从路径中移除/boot.(即. 输入 {{ic|1=set prefix=(hdX,Y)/grub}} 然后{{ic|insmod (hdX,Y)/grub/linux.mod}})}}<br />
<br />
这个模组会启动对我们熟悉的{{ic|linux}} 和 {{ic|initrd}} 命令的支持 (请参考[[#配置]]).<br />
<br />
比如:<br />
set root=(hd0,5)<br />
linux /boot/vmlinuz-linux root=/dev/sda5<br />
initrd /boot/initramfs-linux.img<br />
boot<br />
<br />
如果/boot在单独分区上:<br />
set root=(hd0,5)<br />
linux /vmlinuz-linux root=/dev/sda6<br />
initrd /initramfs-linux.img<br />
boot<br />
<br />
成功启动Arch后,用户可以修正配置的错误或者重新安装GRUB.<br />
<br />
关于修正配置和重新安装GRUB,请参考[[#配置]]和[[#安装]]章节.<br />
==使用UUID的基础脚本 ==<br />
<br />
如果你想要使用UUID来避免不可靠的BIOS设备命名或者正在研究GRUB语法,这里有个使用UUID的示例性的启动项配置脚本.如果你想要将其移植到自己的系统上,只需要修改UUID就行了.这个例子假设系统的boot和root文件系统是在不同的分区上.如果你还有其他分区,请做相应修改.<br />
<br />
menuentry "Arch Linux 64" {<br />
# Set the UUIDs for your boot and root partition respectively<br />
set the_boot_uuid=ece0448f-bb08-486d-9864-ac3271bd8d07<br />
set the_root_uuid=c55da16f-e2af-4603-9e0b-03f5f565ec4a<br />
<br />
# (Note: This may be the same as your boot partition)<br />
<br />
# Get the boot/root devices and set them in the root and grub_boot variables<br />
search --fs-uuid $the_root_uuid --set=root<br />
search --fs-uuid $the_boot_uuid --set=grub_boot<br />
<br />
# Check to see if boot and root are equal.<br />
# If they are, then append /boot to $grub_boot (Since $grub_boot is actually the root partition)<br />
if [ $the_boot_uuid == $the_root_uuid ] ; then<br />
set grub_boot=($grub_boot)/boot<br />
else<br />
set grub_boot=($grub_boot)<br />
fi<br />
<br />
# $grub_boot now points to the correct location, so the following will properly find the kernel and initrd<br />
linux $grub_boot/vmlinuz-linux root=/dev/disk/by-uuid/$the_root_uuid ro<br />
initrd $grub_boot/initramfs-linux.img<br />
}<br />
== 异常处理==<br />
<br />
=== Intel BIOS不能引导GPT ===<br />
<br />
一些Intel的BIOS要求至少要一个可启动的分区(MBR方案下的),导致GTP方案下的分区GRUB无法启动.<br />
<br />
可以通过fdisk来将一个GPT分区标志为'boot'((最好就设在你为GRUB创建的那个1007KiB分区上)),这样就可以绕过这个问题了:<br />
<br />
1.对目标磁盘(比如/dev/sda)运行fdisk<br />
2.按{{ic|a}}键,然后选择想要设置'boot'标志的分区<br />
3.最后按{{ic|w}}键,将变更写入磁盘<br />
<br />
<br />
{{Note|必须使用fdisk或者类似于它的工具来设置'boot flag',不能用Gparted等,因为它们不会在MBR里面设置'boot flag'.}}<br />
<br />
请参考 [http://www.rodsbooks.com/gdisk/bios.html| http://www.rodsbooks.com/gdisk/bios.html]<br />
<br />
=== 启用调试信息 ===<br />
<br />
在{{ic|grub.cfg}}里添加:<br />
<br />
set pager=1<br />
set debug=all<br />
<br />
=== "No suitable mode found" error ===<br />
<br />
启动时可能出现如下提示信息:<br />
<br />
error: no suitable mode found<br />
Booting however<br />
<br />
然后你需要以合适的视频模式({{ic|gfxmode}})启动 GRUB 图形化终端({{ic|gfxterm}}).视频模式由GRUB通过'gfxpayload'变量传递给linux内核.在UEFI系统下,如果没有初始化视频模式的值,终端上就不会显示内核启动消息(至少直到KMS开始运行)<br />
<br />
将{{ic|/usr/share/grub/unicode.pf2}}复制到 ${GRUB_PREFIX_DIR}({{ic|/boot/grub/}} .如果GRUB UEFI安装时设定了{{ic|1=--boot-directory=$esp/EFI}},那么复制的目的文件夹是{{ic|$esp/EFI/grub/}}:<br />
<br />
# cp /usr/share/grub/unicode.pf2 ${GRUB_PREFIX_DIR}<br />
<br />
如果{{ic|/usr/share/grub/unicode.pf2}} 不存在, 安装{{Pkg|bdf-unifont}}, 创建 {{ic|unifont.pf2}} 文件然后将其复制到 {{ic|${GRUB_PREFIX_DIR<nowiki>}</nowiki>}}:<br />
<br />
# grub-mkfont -o unicode.pf2 /usr/share/fonts/misc/unifont.bdf<br />
<br />
然后,在{{ic|grub.cfg}}文件中添加如下行:<br />
(会给linux内核传递合适的视频模式,否则你只会得到一个黑屏,虽然系统还是会启动成功)<br />
BIOS 系统:<br />
<br />
insmod vbe<br />
<br />
UEFI 系统:<br />
<br />
insmod efi_gop<br />
insmod efi_uga<br />
<br />
在这些行后添加(BIOS&&UEFI):<br />
<br />
insmod font<br />
<br />
if loadfont ${prefix}/fonts/unicode.pf2<br />
then<br />
insmod gfxterm<br />
set gfxmode=auto<br />
set gfxpayload=keep<br />
terminal_output gfxterm<br />
fi<br />
<br />
如果要gfxterm(图形化终端)工作正常,{{ic|${GRUB_PREFIX_DIR<nowiki>}</nowiki>}}文件夹里面应该要有{{ic|unicode.pf2}} .<br />
<br />
=== 出现"msdos-style"错误消息 ===<br />
<br />
以下错误可能出现在你将GRUB安装到VMware上时.请阅读[https://bbs.archlinux.org/viewtopic.php?pid=581760#p581760 相关链接].这种情况是因为首分区直接从MBR后开始(即第64个扇区),而不是和正常的那样有1到2M post-MBR gap.请参阅[[#MBR专用指令]]<br />
<br />
grub-setup: warn: This msdos-style partition label has no post-MBR gap; embedding will not be possible!<br />
grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.<br />
However, blocklists are UNRELIABLE and its use is discouraged.<br />
grub-setup: error: If you really want blocklists, use --force.<br />
<br />
=== GRUB UEFI 启动到了rescue shell下 ===<br />
<br />
如果GRUB直接就启动到了rescue shell下,而且没报错,这可能是因为{{ic|grub.cfg}}丢失或者位置不对.如果GRUB UEFI 安装时设定了{{ic|--boot-directory}}参数,而{{ic|grub.cfg}}文件丢失,会出现这个问题.如果启动分区的分区号发生了变化(这个分区号会被直接编码到{{ic|grubx64.efi}}文件中),也会出现这个问题.<br />
<br />
=== GRUB UEFI 无法被载入 ===<br />
<br />
下面是一个EFI启动项信息示例:<br />
{{hc|# efibootmgr -v|<br />
BootCurrent: 0000<br />
Timeout: 3 seconds<br />
BootOrder: 0000,0001,0002<br />
Boot0000* Grub HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\efi\grub\grub.efi)<br />
Boot0001* Shell HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\EfiShell.efi)<br />
Boot0002* Festplatte BIOS(2,0,00)P0: SAMSUNG HD204UI<br />
}}<br />
<br />
如果启动后,屏幕直接变黑,几秒后就跳到了下一个启动项,根据[https://bbs.archlinux.org/viewtopic.php?pid=981560#p981560 相关链接]的说法是,将GRUB移动到root分区上可能会解决这个问题.必须先删除启动项,然后重建,变化如下:<br />
Boot0000* Grub HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\grub.efi)<br />
<br />
=== "Invalid signature"错误 ===<br />
<br />
如果在启动Windows时出现了"invalid signature"错误(比如在重新分区或者添加了新硬盘后),删除GRUB的磁盘mapping,然后重建:<br />
<br />
# mv /boot/grub/device.map /boot/grub/device.map-old<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|grub-mkconfig}}此时就应该生成了新的启动项了,包括Windows.确认能启动成功后,再将备份文件{{ic|/boot/grub/device.map-old}}删除.<br />
<br />
=== 引导过程卡死 ===<br />
<br />
如果在GRUB载入内核并初始化ramdisk后引导过程卡死了,请尝试移除{{ic|add_efi_memmap}}这个内核参数<br />
<br />
=== 回滚到 GRUB Legacy ===<br />
<br />
* 将GRUB2相关文件改名以为备份:<br />
<br />
# mv /boot/grub /boot/grub.nonfunctional<br />
<br />
* 将GRUB Legacy 移回{{ic|/boot}}:<br />
<br />
# cp -af /boot/grub-legacy /boot/grub<br />
<br />
* 恢复备份的MBR<br />
<br />
{{Warning|这个命令同时会恢复分区表,所以请要非常注意.}}<br />
<br />
# dd if=/path/to/backup/first-sectors of=/dev/sdX bs=512 count=1<br />
<br />
安全的方法是只恢复MBR中的启动代码:<br />
<br />
# dd if=/path/to/backup/mbr-boot-code of=/dev/sdX bs=446 count=1<br />
<br />
=== 其他系统不能自动发现Arch Linux===<br />
<br />
有人发现有些发行版不能使用{{ic|os-prober}}自动发现Arch Linux.据称先使用{{ic|/etc/lsb-release}}可能会解决这个问题.相关文件和工具可以在 [[official repositories|官方仓库]]的{{Pkg|lsb-release}}包中找到<br />
<br />
== 参阅 ==<br />
<br />
* 官方GRUB说明 - https://www.gnu.org/software/grub/manual/grub.html<br />
* Ubuntu GRUB Wiki - https://help.ubuntu.com/community/Grub2<br />
* 在UEFI系统上编译GRUB步骤描述- https://help.ubuntu.com/community/UEFIBooting<br />
* 维基百科[[Wikipedia:BIOS Boot partition|BIOS启动分区]]<br />
* 配置GRUB的完整说明 - http://members.iinet.net/~herman546/p20/GRUB2%20Configuration%20File%20Commands.html</div>
残月舞流殇
https://wiki.archlinux.org/index.php?title=GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=393223
GRUB (简体中文)
2015-08-23T02:48:30Z
<p>残月舞流殇: /* 安装到分区上或者无分区磁盘上 */</p>
<hr />
<div>[[Category:Boot loaders (简体中文)]]<br />
[[ar:GRUB]]<br />
[[cs:GRUB]]<br />
[[de:GRUB]]<br />
[[el:GRUB]]<br />
[[en:GRUB]]<br />
[[es:GRUB]]<br />
[[fr:GRUB]]<br />
[[he:GRUB]]<br />
[[id:GRUB2]]<br />
[[it:GRUB2]]<br />
[[ja:GRUB]]<br />
[[nl:GRUB]]<br />
[[ru:GRUB]]<br />
[[tr:GRUB2]]<br />
[[uk:GRUB]]<br />
[[zh-TW:GRUB2]]<br />
{{TranslationStatus (简体中文)|GRUB|2013-11-15|282734}}<br />
{{Related articles start (简体中文)}}<br />
{{Related|Burg}}<br />
{{Related|GRUB Legacy}}<br />
{{Related|GRUB EFI Examples}}<br />
{{Related articles end}}<br />
[http://www.gnu.org/software/grub/ GRUB2] 是下一代 GRand Unified Bootloader (GRUB,请不要和Grub Legacy混淆了)。 它来自下一代 GRUB 研究项目 [http://www.nongnu.org/pupa/ PUPA],代码全部重写,实现了模块化和增强了移植性。[http://www.gnu.org/software/grub/grub-faq.en.html#q1].<br />
<br />
简单的说,'''Boot Loader'''是电脑启动时运行的第一个程序,它负责装载内核并将控制权转交。内核再初始化操作系统的其它部分。<br />
<br />
==前言==<br />
* 引导程序是计算机启动时第一个运行的程序。它负责加载并将控制权转移到Linux内核。内核作为回报,将初始化操作系统剩余部分<br />
* 官方所称的GRUB代表的是本软件的第二版,即GRUB2,请参考[https://www.gnu.org/software/grub/].如果你是在找有关Grub Legacy的文章,请参考[[GRUB Legacy]].<br />
* GRUB2支持由zlib或者LZO压缩过的[[Btrfs]]格式的根目录,如果使用Btrfs,不需要单独的{{ic|/boot}}分区.<br />
* GRUB2 不支持[[F2fs]]格式的根目录,所以你需要为{{ic|/boot}}分区单独设置一个支持的文件系统.<br />
<br />
== BIOS 系统==<br />
=== GUID分区表(GPT)具体说明 ===<br />
BIOS/[[GPT]]配置中,一个 [http://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html BIOS启动分区]是必需的。GRUB将{{ic|core.img}}嵌入到这个分区。<br />
{{Note|<br />
* 在尝试分区之前请记住不是所有的系统都能够支持这种分区方案, 关于更多请阅读[[GUID Partition Table#BIOS systems|GUID分区表]].<br />
* This additional partition is only needed on a GRUB, BIOS/GPT partitioning scheme. Previously, for a GRUB, BIOS/MBR partitioning scheme, GRUB used the Post-MBR gap for the embedding the {{ic|core.img}}). GRUB for GPT, however, does not use the Post-GPT gap to conform to GPT specifications that require 1_megabyte/2048_sector disk boundaries.<br />
* For [[UEFI]] systems this extra partition is not required as no embedding of boot sectors takes place in that case.<br />
}}<br />
Create a mebibyte partition ({{ic|1=+1M}} with {{ic|fdisk}} or {{ic|gdisk}}) on the disk with no file system and type BIOS boot (''BIOS boot'' in fdisk, {{ic|ef02}} in gdisk, {{ic|bios_grub}} in {{ic|parted}}). This partition can be in any position order but has to be on the first 2 TiB of the disk. This partition needs to be created before GRUB installation. When the partition is ready, install the bootloader as per the instructions below.<br />
The post-GPT gap can also be used as the BIOS boot partition though it will be out of GPT alignment specification. Since the partition will not be regularly accessed performance issues can be disregarded (though some disk utilities will display a warning about it). In {{ic|fdisk}} or {{ic|gdisk}} create a new partition starting at sector 34 and spanning to 2047 and set the type. To have the viewable partitions begin at the base consider adding this partition last.<br />
<br />
=== 主引导记录(MBR)具体说明 ===<br />
<br />
一般来说,如果使用兼容DOS的分区对齐模式,MBR后面空间(post-MBR gap)的大小都是31KiB(MBR和第一个分区之间的空间).不过,为了提供足够的空间嵌入GRUB的{{ic|core.img}}文件({{bug|24103}}),建议将这个空间设置到1到2Mib.最好使用支持1MiB分区对齐的分区软件来分区,因为这样也能满足非512B扇区磁盘分区的需求.<br />
<br />
=== 安装 ===<br />
<br />
可以使用[[pacman|pacman]]从[[official repositories|官方仓库]]安装{{Pkg|grub}}包.安装完成后它会代替{{AUR|grub-legacy}}.<br />
<br />
{{Note|简单的安装Grub包并不会更新{{ic|/boot/grub/i386-pc/core.img}}和{{ic|/boot/grub/i386-pc}}里的GRUB模组.需要使用下面介绍的{{ic|grub-install}}来手动更新它们.}}<br />
<br />
==== 安装Boot文件 ====<br />
<br />
有三种方式安装GRUB Boot文件:<br />
<br />
* [[#安装到磁盘上|安装到磁盘上]] (推荐方式)<br />
* [[#安装到U盘|安装到U盘]] (用于恢复)<br />
* [[#安装到分区上或者无分区磁盘上|安装到分区或者无分区磁盘上]] (不推荐)<br />
* [[#只生成core.img|只生成core.img文件]] (最安全的方法, 但是需要另外的bootloader,比如[[Syslinux]]来进行链式加载{{ic|/boot/grub/i386-pc/core.img}})<br />
<br />
{{Note|请参考 http://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html 获取更详尽的资料}}<br />
<br />
===== 安装到磁盘上 =====<br />
<br />
{{Note|这种方法只限于将GRUB安装到可分区磁盘(MBR或GPT),GRUB 文件会被安装到{{ic|/boot/grub}},1st stage code会被安装到MBR的启动代码区域(以便和MBR分区表分开).对于无分区磁盘,请参考[[#安装到分区上或者无分区磁盘上]]}}<br />
<br />
以下命令会将{{ic|grub}}安装到MBR的启动代码区域,填充{{ic|/boot/grub}}文件夹,生成{{ic|/boot/grub/i386-pc/core.img}},将其嵌入post-MBR gap或者放入BIOS boot partition中(分别对应MBR分区表和GPT分区表):<br />
# grub-install --target=i386-pc --recheck --debug /dev/sda<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
如果你使用[[LVM]]来进行启动,你可以将GRUB安装在多个物理磁盘上.<br />
执行{{ic|grub-install}}并不会生成GRUB配置文件,请移至[[#生成GRUB配置文件]]一节<br />
<br />
===== 安装到U盘 =====<br />
<br />
假设你的U盘第一个分区是FAT32,其分区是/dev/sdy1<br />
<br />
# mkdir -p /mnt/usb ; mount /dev/sdy1 /mnt/usb<br />
# grub-install --target=i386-pc --recheck --debug --boot-directory=/mnt/usb/boot /dev/sdy<br />
# grub-mkconfig -o /mnt/usb/boot/grub/grub.cfg<br />
<br />
# optional, backup config files of grub.cfg<br />
# mkdir -p /mnt/usb/etc/default<br />
# cp /etc/default/grub /mnt/usb/etc/default<br />
# cp -a /etc/grub.d /mnt/usb/etc<br />
<br />
# sync ; umount /mnt/usb<br />
<br />
===== 安装到分区上或者无分区磁盘上 =====<br />
<br />
{{Note|GRUB并不推荐将其安装到分区启动扇区或者无分区磁盘上(Grub Legacy和syslinux相反).这种安装方式不安全,当升级时可能会损坏.Arch开发人员也不支持这种方式}}<br />
<br />
下面的命令将会将GRUB2安装到分区扇区或者无分区磁盘上(比如闪存上):<br />
<br />
# chattr -i /boot/grub/i386-pc/core.img<br />
# grub-install --target=i386-pc --recheck --debug --force /dev/sdaX<br />
# chattr +i /boot/grub/i386-pc/core.img<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
<br />
<br />
必须使用{{ic|--force}}选项来启用对blocklists(块列表)的支持,不应使用{{ic|1=--grub-setup=/bin/true}}(这个选项的效果类似于只生成{{ic|core.img}}).<br />
{{ic|grub-install}}会生成以下警告,你可以通过这些警告判断发生了什么问题:<br />
/sbin/grub-setup: warn: Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea.<br />
/sbin/grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists. <br />
However, blocklists are UNRELIABLE and their use is discouraged.<br />
<br />
如果不指定{{ic|--force}}选项,会出现以下错误,并且不会将启动代码安装到启动扇区上: <br />
/sbin/grub-setup: error: will not proceed with blocklists<br />
<br />
而指定了{{ic|--force}},会出现:<br />
<br />
Installation finished. No error reported.<br />
<br />
{{ic|grub-setup}}不默认允许这种情况的原因是,在分区或者无分区磁盘上,{{ic|grub}}依赖于嵌入分区引导扇区的块列表(blocklists)来定位{{ic|/boot/grub/i386-pc/core.img}}和{{ic|/boot/grub}}.而{{ic|core.img}}在分区上的扇区位置很有可能随着分区文件系统的更改而变化(复制文件,删除文件等).详情请参考https://bugzilla.redhat.com/show_bug.cgi?id=728742 和 https://bugzilla.redhat.com/show_bug.cgi?id=730915.<br />
<br />
临时解决方案是给{{ic|/boot/grub/i386-pc/core.img}}文件加"不可变"(immutable)标志.只有当将{{ic|grub}}安装到分区启动扇区或者无分区磁盘上时才需要给core.img加"不可变"标志.<br />
执行{{ic|grub-install}}并不会生成GRUB配置文件,请移至[[#生成GRUB配置文件]]一节<br />
<br />
===== 只生成core.img =====<br />
<br />
通过添加{{ic|1=--grub-setup=/bin/true}}选项,{{ic|grub-install}}命令会填充{{ic|/boot/grub}}文件夹并生成{{ic|/boot/grub/i386-pc/core.img}},但是不会将grub启动引导代码嵌入到MBR,post-MBR gap和分区引导扇区中:<br />
<br />
# grub-install --target=i386-pc --grub-setup=/bin/true --recheck --debug /dev/sda<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
生成后,Grub Legacy或者syslinux就可以通过链式加载GRUB2的{{ic|core.img}}来间接加载Linux内核或者多启动内核了.<br />
<br />
==== 生成GRUB配置文件 ====<br />
<br />
最后,生成GRUB2所需的配置文件(在配置章节会详述):<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{Note|文件路径是{{ic|/boot/grub/grub.cfg}}, 而不是{{ic|/boot/grub/i386-pc/grub.cfg}}.}}<br />
<br />
如果GRUB在启动时报错"no suitable mode found",请参考[[#"No suitable mode found" error]].<br />
<br />
如果{{ic|grub-mkconfig}}执行失败,可以通过如下方式将{{ic|/boot/grub/menu.lst}}文件转化为{{ic|/boot/grub/grub.cfg}}<br />
{{Note|这种方法只适合BIOS,不适合UEFI}}<br />
# grub-menulst2cfg /boot/grub/menu.lst /boot/grub/grub.cfg<br />
<br />
示例如下:<br />
{{hc|/boot/grub/menu.lst|<nowiki><br />
default=0<br />
timeout=5<br />
<br />
title Arch Linux Stock Kernel<br />
root (hd0,0)<br />
kernel /vmlinuz-linux root=/dev/sda2 ro<br />
initrd /initramfs-linux.img<br />
<br />
title Arch Linux Stock Kernel Fallback<br />
root (hd0,0)<br />
kernel /vmlinuz-linux root=/dev/sda2 ro<br />
initrd /initramfs-linux-fallback.img<br />
</nowiki>}}<br />
<br />
{{hc|/boot/grub/grub.cfg|<nowiki><br />
set default='0'; if [ x"$default" = xsaved ]; then load_env; set default="$saved_entry"; fi<br />
set timeout=5<br />
<br />
menuentry 'Arch Linux Stock Kernel' {<br />
set root='(hd0,1)'; set legacy_hdbias='0'<br />
legacy_kernel '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'<br />
legacy_initrd '/initramfs-linux.img' '/initramfs-linux.img'<br />
<br />
}<br />
<br />
menuentry 'Arch Linux Stock Kernel Fallback' {<br />
set root='(hd0,1)'; set legacy_hdbias='0'<br />
legacy_kernel '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'<br />
legacy_initrd '/initramfs-linux-fallback.img' '/initramfs-linux-fallback.img'<br />
}<br />
</nowiki>}}<br />
<br />
如果你忘记创建GRUB配置文件{{ic|/boot/grub/grub.cfg}},然后直接重启到了GRUB命令行界面,输入以下命令:<br />
sh:grub> insmod legacycfg<br />
sh:grub> legacy_configfile ${prefix}/menu.lst<br />
选择启动到Arch下然后再重新创建合适的GRUB配置文件{{ic|/boot/grub/grub.cfg}}<br />
<br />
==== 多系统启动 ====<br />
<br />
为了实现多系统启动,需要安装{{Pkg|os-prober}}.安装后,再执行{{ic|grub-mkconfig -o /boot/grub/grub.cfg}}.如果执行失败,可能就需要手动添加启动条目了.(后面有指导)<br />
<br />
{{Note|如果找不到Windows,可以将其boot partition挂载后再试}}<br />
<br />
===== 在BIOS-MBR模式下安装的Microsoft Windows =====<br />
<br />
{{Note|GRUB支持直接从{{ic|bootmgr}}启动,现在不再需要链式加载分区启动扇区了}}<br />
<br />
{{Warning|{{ic|bootmgr}}位于'''系统分区'''('''system partition'''),而不是Windows系统所在的分区(比如C盘).在uuid-卷名列表中,系统分区是那个名为{{ic|LABEL&#61;"SYSTEM RESERVED"}} o或 {{ic|LABEL&#61;"SYSTEM"}}的项,而且这个分区一般只有100到200MB大(和Arch的启动分区差不多).请参考[[Wikipedia:System partition and boot partition]]}}<br />
<br />
本节假设你的Windows分区是{{ic|/dev/sda1}}.首先,找到Windows系统分区的UUID(Windows's SYSTEM PARTITION,{{ic|bootmgr存放其上}}).如果Windows {{ic|bootmgr}}的位置是{{ic|/media/SYSTEM_RESERVED/bootmgr}},对于 Windows Vista/7/8,执行:<br />
<br />
# grub-probe --target=fs_uuid /media/SYSTEM_RESERVED/bootmgr<br />
69B235F6749E84CE<br />
<br />
# grub-probe --target=hints_string /media/SYSTEM_RESERVED/bootmgr<br />
--hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1<br />
<br />
{{Note|对于Windows XP, 请在上面的命令中用{{ic|NTLDR}}替代{{ic|bootmgr}}. 注意,可能{{ic|NTLDR}}不是在名为"SYSTEM_RESERVED"的分区上,请在XP系统所在的分区上寻找 {{ic|NTLDR}}}}<br />
<br />
接着,将下面的代码添加到{{ic|/etc/grub.d/40_custom}} 或者{{ic|/boot/grub/custom.cfg}}中.然后,使用{{ic|grub-mkconfig}}重新生成{{ic|grub.cfg}},这样就能在BIOS-MBR配置下使用GRUB2引导启动Windows系统了:<br />
<br />
对于 Windows Vista/7/8<br />
<br />
if [ "${grub_platform}" == "pc" ]; then<br />
menuentry "Microsoft Windows Vista/7/8 BIOS-MBR" {<br />
insmod part_msdos<br />
insmod ntfs<br />
insmod search_fs_uuid<br />
insmod ntldr <br />
search --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 69B235F6749E84CE<br />
ntldr /bootmgr<br />
}<br />
fi<br />
<br />
对于Windows XP<br />
<br />
if [ "${grub_platform}" == "pc" ]; then<br />
menuentry "Microsoft Windows XP" {<br />
insmod part_msdos<br />
insmod ntfs<br />
insmod search_fs_uuid<br />
insmod ntldr <br />
search --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 69B235F6749E84CE<br />
ntldr /bootmgr<br />
}<br />
fi<br />
<br />
<br />
{{Note|在某些情况下,可能在安装Windows 8之前就已经安装了GRUB.启动Windows时可能会{{ic|\boot\bcd}}报错(错误代码为{{ic|0xc000000f}}).可以通过Wondows Recovery Console来修复这个错误:<br />
x:\> "bootrec.exe /fixboot" <br />
x:\> "bootrec.exe /RebuildBcd".<br />
'''不要'''使用{{ic|bootrec.exe /Fixmbr}},因为那会将GRUB清除掉<br />
}}<br />
<br />
{{ic|/etc/grub.d/40_custom}}可以做为创建{{ic|/etc/grub.d/nn_custom}}的模板.{{ic|nn}}定义了脚本执行的优先级.脚本执行的顺序又决定了grub引导启动目录的内容.<br />
{{Note|{{ic|nn}} 应该比06大,这样才能确保必要的脚本被优先执行}}<br />
<br />
== UEFI 系统==<br />
{{Note|建议阅读并理解[[UEFI]], [[GPT]] and [[UEFI Bootloaders]]}}<br />
===== Check if you have GPT and an ESP检查你是否使用GPT和ESP =====<br />
如果想要使用EFI在某个磁盘上进行启动,那么就需要对这个磁盘进行EFI系统分区(EFI System Partition,即ESP),GPT倒不是必须的,不过我们还是高度建议使用GTP,并且这也是本篇文章当前支持的方法.如果你在一个支持EFI,并且已经有操作系统的电脑上安装Arch(比如Windows 8),那么你已经有了ESP了.可以通过{{ic|parted}}来列出启动磁盘上的分区表以检查其是否支持GPT和ESP(假设这个启动磁盘是/dev/sda)<br />
<br />
# parted /dev/sda print<br />
<br />
如果使用GPT,那么会出现"分区表:GPT".如果使用EFI,那么会有一个文件系统为vfat的小分区(一般小于512MiB)并且被标志为启动分区.在这个小分区上,应该有一个名为EFI的文件夹.如果这些条件都满足,那么这就是ESP了.注意分区序号,因为之后安装GRUB2时你需要mount它.<br />
<br />
===== 建立ESP =====<br />
<br />
如果你没有ESP,请参考[[UEFI#EFI System Partition]]的引导来创建它<br />
<br />
=== 安装 ===<br />
<br />
{{Note|众所周知,不同的主板厂商的UEFI实现方式也不一样.我们鼓励用户将其配置过程中遇到的问题及其细节分享出来.为了保证[[GRUB]]页面的简洁,请参考[[GRUB/EFI examples (简体中文)]]<br />
}}<br />
<br />
首先,安装{{Pkg|grub}}, {{Pkg|dosfstools}}, {{Pkg|efibootmgr}}(后面两个包是为了让GRUB支持EFI).然后按下面的指导进行配置.<br />
{{Note|简单的安装这些包并不会更新{{ic|core.efi}}和ESP中的GRUB模组.请按下面的指导手动进行配置}}<br />
====安装boot文件====<br />
<br />
=====推荐方法=====<br />
<br />
{{Note|<br />
* 下面的命令是假设你正在为{{ic|x86_64-efi}}安装GRUB.(如果是为{{ic|IA32-efi}}安装GRUB,请用{{ic|i386-efi}}代替{{ic|x86_64-efi}})<br />
* 如果要成功执行下面的命令,你需要使用UEFI启动而不是BIOS.如果你从USB驱动器中的ISO文件启动系统,那么可以判断,你已经是从BIOS启动了.你需要[[Unified Extensible Firmware Interface#Create UEFI bootable USB from ISO|建立一个UEFI可启动USB磁盘]],然后重启,否则grub-install会报错.<br />
}}<br />
<br />
首先,将ESP挂载上(一般挂在到{{ic|/boot/efi}}上,然后将其路径赋值到$esp).如果是第一次安装GRUB,你可能需要建立{{ic|/boot/efi}}文件夹<br />
然后,将GRUB UEFI应用安装到{{ic|$esp/EFI/grub}},将其模组安装到{{ic|/boot/grub/x86_64-efi}}:<br />
<br />
# grub-install --target=x86_64-efi --efi-directory=$esp --bootloader-id=grub --recheck --debug<br />
<br />
{{Note|<br />
* 如果grub-install在sysfs和procfs上运行的时候报错"modprobe efivars",尝试[[Unified_Extensible_Firmware_Interface#Switch_to_efivarfs]].<br />
* 如果没有{{ic|--target}} 或{{ic|--directory}} 选项,grub-install就不能决定安装哪种固件.在这个情况下,grub-install会报错{{ic|source_dir does not exist. Please specify --target or --directory}}.<br />
* {{ic|--efi-directory}} and {{ic|--bootloader-id}}是只适用于GRUB UEFI的选项.{{ic|--efi-directory}}替代{{ic|--root-directory}}指定ESP的挂载点.{{ic|--bootloader-id}}指定存放{{ic|grubx64.efi}}文件的目录名<br />
* 可能你会注意到,不像安装GRUB到BIOS那样,在{{ic|grub-install}}命令后没有<device_path>选项(例如{{ic|/dev/sda}}).哪怕提供了<device_path>,也会被安装脚本忽略.因为UEFI bootloader不使用MBR或者分区启动扇区.<br />
}}<br />
<br />
现在GRUB已经安装好了.请移至[[#配置|配置]]一节继续.<br />
<br />
=====其他方法=====<br />
<br />
如果你想将所有的GRUB boot 文件放在ESP中,请给grub-install命令添加{{ic|--boot-directory&#61;$esp/EFI}}选项:<br />
<br />
# grub-install --target=x86_64-efi --efi-directory=$esp --bootloader-id=grub --boot-directory=$esp/EFI --recheck --debug<br />
<br />
下面的命令会将GRUB模组文件放在{{ic|$esp/EFI/grub}}中.使用这个方法,grub.cfg也会被放在ESP中,所以请确保grub-mkconfig指向了正确的地方:<br />
<br />
# grub-mkconfig -o $esp/EFI/grub/grub.cfg<br />
<br />
配置档和BIOS-GRUB是一样的.<br />
<br />
====在固件启动管理器中创建GRUB条目====<br />
<br />
{{ic|grub-install}}会自动尝试在启动管理器中创建GRUB条目.如果没有成功,请参考[[Beginners' guide#GRUB]],里面有关于使用{{ic|efibootmgr}}创建启动目录条目的介绍.一般来说,这个问题都是因为你没有以UEFI模式启动CD/USB造成的.请参考[[UEFI#Create UEFI bootable USB from ISO]].<br />
<br />
====创建GRUB Standalone模式的UEFI应用程序====<br />
<br />
可以建立一个{{ic|grubx64_standalone.efi}},这个应用将所有的模组嵌入自身的memdisk中,所以就不需要使用单独的目录来存放GRUB UEFI模组和其他相关文件了,使用{{Pkg|grub}}包里的{{ic|grub-mkstandalone}}可以实现这个功能.<br />
<br />
最简单的的方法就是使用{{ic|grub-mkstandalone}},不过,我们还可以指定嵌入哪些模组:<br />
<br />
# grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi/" --format="x86_64-efi" --compression="xz" \<br />
--output="$esp/EFI/grub/grubx64_standalone.efi" <你想嵌入的模组><br />
<br />
{{ic|grubx64_standalone.efi}}文件要求将{{ic|grub.cfg}}放置到{{ic|(memdisk)/boot/grub}}中,而这个memdisk是嵌入到EFI应用当中的.{{ic|grub-mkstandlone}}脚本允许传递将要嵌入memdisk的文件列表.(上面命令里的"<你想嵌入的模组>")<br />
<br />
如果你的{{ic|grub.cfg}}的路径是{{ic|/home/user/Desktop/grub.cfg}},那么需要创建一个临时的{{ic|/home/user/Desktop/boot/grub/}}目录,然后将grub.cfg复制到其中并进入这个目录并运行:<br />
<br />
# grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi/" --format="x86_64-efi" --compression="xz" \<br />
--output="$esp/EFI/grub/grubx64_standalone.efi" "boot/grub/grub.cfg"<br />
<br />
进入{{ic|/home/user/Desktop/boot/grub/}}但是传递{{ic|boot/grub/grub.cfg}}参数(请注意是{{ic|boot/}}而不是{{ic|/boot/}})的原因是{{ic|grub-mkstandalone}}会自动将boot/grub/grub.cfg处理为{{ic|/(memdisk)/boot/grub/grub.cfg}}<br />
<br />
如果你传递{{ic|/home/user/Desktop/grub.cfg}},那么处理后的结果会是{{ic|(memdisk)/home/user/Desktop/grub.cfg}}.如果传递{{ic|/home/user/Desktop/boot/grub/grub.cfg}},那么结果就是{{ic|(memdisk)/home/user/Desktop/boot/grub/grub.cfg}}.所以需要进入{{ic|/home/user/Desktop/boot/grub/}}并传递{{ic|boot/grub/grub.cfg}}参数,因为这样才能生成{{ic|grub.efi}}需要的{{ic|(memdisk)/boot/grub/grub.cfg}}.<br />
<br />
如果需要为{{ic|$esp/EFI/arch_grub/grubx64_standalone.efi}}创建一个UEFI启动器条目,使用{{ic|efibootmgr}}.[[#Create GRUB entry in the Firmware Boot Manager]]里有介绍<br />
<br />
==配置==<br />
<br />
可以自动生成配置档,也可以手动编辑{{ic|grub.cfg}}.<br />
{{Note|<br />
* 对于EFI系统,如果在安装GRUB时使用 {{ic|1=--boot-directory=$esp/EFI}}了选项,{{ic|grub.cfg}}文件必须和{{ic|grubx64.efi}}放在同一文件夹.如果没有使用这个选项,那么{{ic|grub.cfg}}应当在{{ic|/boot/grub/}}下,和GRUB BIOS系统一样.<br />
*在[http://members.iinet.net/~herman546/p20/GRUB2%20Configuration%20File%20Commands.html 这里]可以找到关于如何配置GRUB的完整描述.<br />
}}<br />
<br />
===使用grub-mkconfig自动生成配置档===<br />
<br />
和GRUB Legacy配置文件{{ic|menu.lst}}对应的GRUB配置文件是{{ic|/etc/default/grub}} 和{{ic|/etc/grub.d/*}}.{{ic|grub-mkconfig}}使用这些文件来生成{{ic|grub.cfg}}.grub-mkconfig默认输出至stdout.以下命令可以生成一个{{ic|grub.cfg}}文件:<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|/etc/grub.d/10_linux}}会自动为Arch Linux设置启动项.其他操作系统的话,可能需要手动在{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}中设置.<br />
<br />
{{Note|如果你在一个chroot环境中或者systemd-nspawn容器里执行这些操作,可能完全不会成功,因为grub-probe不能获取'/dev/sdaX'这样的典型路径.这种情况下,可以尝试使用[https://bbs.archlinux.org/viewtopic.php?pid&#61;1225067#p1225067 arch-chroot].}}<br />
<br />
====额外的参数====<br />
<br />
在{{ic|/etc/default/grub}}中设置{{ic|GRUB_CMDLINE_LINUX}}和{{ic|GRUB_CMDLINE_LINUX_DEFAULT}}变量可以实现将向Linux镜像传递额外的参数.生成{{ic|grub.cfg}}时,如果遇到普通启动项,这两个参数会一起使用,遇到''recovery''启动项,就只使用{{ic|GRUB_CMDLINE_LINUX}}参数.<br />
<br />
没有必要两者一起使用.这两个参数很有用.比如,如果要系统支持休眠后恢复,需要使用{{ic|<nowiki>GRUB_CMDLINE_LINUX_DEFAULT="resume=/dev/sdaX</nowiki> quiet"}} ({{ic|sda'''X'''}}是交换分区).这个选项会生成一个recovery启动项,这个启动项没有resume和quiet参数.其他的普通启动项也可能使用它们.(GRUB会为每个内核生成两个启动项,一个默认的一个recovery的,GRUB_CMDLINE_LINUX指定的参数会传递给这两个启动项.GRUB_CMDLINE_LINUX_DEFAULT指定的参数只会传递给默认启动项)<br />
<br />
当生成GRUB recovery启动项时,你必须在{{ic|/etc/default/grub}}中将{{ic|<nowiki>#GRUB_DISABLE_RECOVERY=true</nowiki>}} 注释掉.<br />
<br />
你也可以使用{{ic|<nowiki>GRUB_CMDLINE_LINUX="resume=/dev/disk/by-uuid/${swap_uuid}"</nowiki>}}, {{ic|${swap_uuid} }}是交换分区的[[Persistent_block_device_naming|UUID]] <br />
<br />
不同的启动项使用双引号引起来并用空格隔开.所以,对于即想使用resume又想使用systemd的用户来说,这个参数的设置可能是:<br />
{{ic|<nowiki>GRUB_CMDLINE_LINUX="resume=/dev/sdaX init=/usr/lib/systemd/systemd"</nowiki>}}<br />
更多信息请参考[[Kernel parameters]].<br />
<br />
=== 手动创建 grub.cfg ===<br />
<br />
{{Warning|''不推荐''编辑这个文件.这个文件由{{ic|grub-mkconfig}}生成,最好编辑{{ic|/etc/default/grub}}和{{ic|/etc/grub.d}}文件夹下的脚本以实现修改.}}<br />
<br />
基本的GRUB配置文件使用如下选项:<br />
* {{ic|(hd''X'',''Y'')}} 是''X''磁盘的''Y''分区,分区从1开始计数,磁盘从0开始计数.<br />
* {{ic|1=set default=''N''}}设定用户选择超时时间过后的默认启动项<br />
* {{ic|1=set timeout=''M''}}设定用户选择超时时间(秒).<br />
* {{ic|<nowiki>menuentry "title" {entry options}</nowiki>}}设置一个名为{{ic|title}}的启动项<br />
* {{ic|1=set root=(hd''X'',''Y'')}}设定启动分区(kernel和GRUB模组所在磁盘),/boot没被要求独占一个分区,有可能就是root分区下的一个文件夹<br />
示例配置如下:<br />
<br />
{{hc|/boot/grub/grub.cfg|<nowiki><br />
# Config file for GRUB - The GNU GRand Unified Bootloader<br />
# /boot/grub/grub.cfg<br />
<br />
# DEVICE NAME CONVERSIONS<br />
#<br />
# Linux Grub<br />
# -------------------------<br />
# /dev/fd0 (fd0)<br />
# /dev/sda (hd0)<br />
# /dev/sdb2 (hd1,2)<br />
# /dev/sda3 (hd0,3)<br />
#<br />
<br />
# Timeout for menu<br />
set timeout=5<br />
<br />
# Set default boot entry as Entry 0<br />
set default=0<br />
<br />
# (0) Arch Linux<br />
menuentry "Arch Linux" {<br />
set root=(hd0,1)<br />
linux /vmlinuz-linux root=/dev/sda3 ro<br />
initrd /initramfs-linux.img<br />
}<br />
<br />
## (1) Windows<br />
#menuentry "Windows" {<br />
# set root=(hd0,3)<br />
# chainloader +1<br />
#}<br />
</nowiki>}}<br />
<br />
=== 双启动 ===<br />
<br />
{{Note|如果你希望GRUB自动搜寻其他系统, 可以安装{{Pkg|os-prober}}.}}<br />
<br />
==== 使用/etc/grub.d/40_custom 和 grub-mkconfig自动生成 ====<br />
<br />
添加其他启动项的最好方法是编辑{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}.运行{{ic|grub-mkconfig}}后,这些文件中的启动项会被自动添加到grub.cfg中.<br />
更新了这些文件后,运行:<br />
{{bc|<nowiki># grub-mkconfig -o /boot/grub/grub.cfg</nowiki>}}<br />
在UEFI-GPT模式下,运行:<br />
{{bc|<nowiki># grub-mkconfig -o /boot/efi/EFI/GRUB/grub.cfg</nowiki>}}<br />
这样会更新{{ic|grub.cfg}}<br />
<br />
<br />
一个典型{{ic|/etc/grub.d/40_custom}}文件类似于下面这个为[http://h10025.www1.hp.com/ewfrf/wc/product?cc=us&destPage=product&lc=en&product=5402703&tmp_docname= HP Pavilion 15-e056sl Notebook PC|预装WIN8的HP Pavilion 15-e056sl Notebook PC]而作的配置.每个启动项的结构都应该和下面的类似.请注意UEFI分区{{ic|/dev/sda2}} 被命名为{{ic|hd0,gpt2}} 和{{ic|ahci0,gpt2}}(请参考[[GRUB#Windows_Installed_in_UEFI-GPT_Mode_menu_entry|here]]获取更多信息)<br />
<br />
'''/etc/grub.d/40_custom''':<br />
<br />
{{bc|<nowiki>#!/bin/sh<br />
exec tail -n +3 $0<br />
# This file provides an easy way to add custom menu entries.&nbsp; Simply type the<br />
# menu entries you want to add after this comment.&nbsp; Be careful not to change<br />
# the 'exec tail' line above.<br />
<br />
menuentry "HP / Microsoft Windows 8.1" {<br />
echo "Loading HP / Microsoft Windows 8.1..."<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 763A-9CB6<br />
chainloader (${root})/EFI/Microsoft/Boot/bootmgfw.efi<br />
}<br />
<br />
menuentry "HP / Microsoft Control Center" {<br />
echo "Loading HP / Microsoft Control Center..."<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 763A-9CB6<br />
chainloader (${root})/EFI/HP/boot/bootmgfw.efi<br />
}<br />
<br />
menuentry "System shutdown" {<br />
echo "System shutting down..."<br />
halt<br />
}<br />
<br />
menuentry "System restart" {<br />
echo "System rebooting..."<br />
reboot<br />
}</nowiki>}}<br />
<br />
===== GNU/Linux 启动项 =====<br />
<br />
假设另一个发行版位于{{ic|sda2}}:<br />
<br />
{{bc|<nowiki>menuentry "Other Linux" {<br />
set root=(hd0,2)<br />
linux /boot/vmlinuz (add other options here as required)<br />
initrd /boot/initrd.img (if the other kernel uses/needs one)<br />
}</nowiki>}}<br />
<br />
===== FreeBSD 启动项 =====<br />
<br />
要求FreeBSD以UFS格式被安装在单独的分区上,假设安装在{{ic|sda4}}上:<br />
<br />
{{bc|<nowiki>menuentry "FreeBSD" {<br />
set root=(hd0,4)<br />
chainloader +1<br />
}</nowiki>}}<br />
<br />
===== Windows XP 启动项=====<br />
<br />
假设Windows分区是{{ic|sda3}}.请记住需要将root设置到Windows安装时建立的启动分区上,然后链式加载它,而不是Windows系统所在分区.下面的例子就是假设启动分区就是{{ic|sda3}}.<br />
{{bc|<nowiki># (2) Windows XP<br />
menuentry "Windows XP" {<br />
set root="(hd0,3)"<br />
chainloader +1<br />
}</nowiki>}}<br />
<br />
如果Windows的bootloader和GRUB不在一个硬盘上,那么需要让Windows认为它是在第一硬盘上.使用{{ic|drivemap}}可以实现这点.假设GRUB在{{ic|hd0}}而windows在{{ic|hd2}}上,需要在设定root后执行:<br />
<br />
{{bc|<nowiki>drivemap -s hd0 hd2</nowiki>}}<br />
<br />
===== UEFI-GPT 模式下安装的Windows的启动项 =====<br />
<br />
{{bc|<nowiki>if [ "${grub_platform}" == "efi" ]; then<br />
menuentry "Microsoft Windows Vista/7/8 x86_64 UEFI-GPT" {<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root $hints_string $uuid<br />
chainloader /EFI/Microsoft/Boot/bootmgfw.efi<br />
}<br />
fi</nowiki>}}<br />
<br />
{{ic|$hints_string}} 和 {{ic|$uuid}}可以通过以下命令获取:<br />
<br />
{{ic|$uuid}}:<br />
<br />
{{bc|<nowiki># grub-probe --target=fs_uuid $esp/EFI/Microsoft/Boot/bootmgfw.efi<br />
1ce5-7f28</nowiki>}}<br />
<br />
{{ic|$hints_string}}:<br />
<br />
{{bc|<nowiki># grub-probe --target=hints_string $esp/EFI/Microsoft/Boot/bootmgfw.efi<br />
--hint-bios=hd0,gpt1 --hint-efi=hd0,gpt1 --hint-baremetal=ahci0,gpt1</nowiki>}}<br />
<br />
这两个命令都是假设ESP挂载在{{ic|$esp}}上.当然,Windows的EFI文件路径可能有变,因为这就是Windows....<br />
<br />
===== "Shutdown" 启动项 =====<br />
<br />
{{bc|<nowiki>menuentry "System shutdown" {<br />
echo "System shutting down..."<br />
halt<br />
}</nowiki>}}<br />
<br />
===== "Restart" 启动项 =====<br />
<br />
{{bc|<nowiki>menuentry "System restart" {<br />
echo "System rebooting..."<br />
reboot<br />
}</nowiki>}}<br />
<br />
==== 通过EasyBCD NeoGRUB 和Windows共存 ====<br />
<br />
现在EasyBCD的NeoGRUB还不能识别GRUB的目录格式,在 {{ic|C:\NST\menu.lst}} 中添加如下行以链式加载到GRUB:<br />
<br />
default 0<br />
timeout 1<br />
<br />
title Chainload into GRUB v2<br />
root (hd0,7)<br />
kernel /boot/grub/i386-pc/core.img<br />
<br />
最后,使用{{ic|grub-mkconfig}}重建{{ic|grub.cfg}}<br />
<br />
===可视化配置===<br />
<br />
GRUB默认就支持定制启动目录的外观.不过要确保使用合适的视频模式初始化GRUB的图形化终端gfxterm.在[[#"No suitable mode found" error]]一节中有介绍.GRUB通过'gfxpayload'来将视频模式传递给linux内核,所以任何可视化配置都需要这个模式的信息以正确工作.<br />
<br />
==== 设定帧缓冲的分辨率 ====<br />
<br />
<br />
GRUB既可以为自己,也可以为内核设定帧缓冲.现在已经不使用老的{{ic|1=vga=}}配置了.推荐方法是在{{ic|/etc/default/grub}}进行如下编辑:<br />
<br />
GRUB_GFXMODE=1024x768x32<br />
GRUB_GFXPAYLOAD_LINUX=keep<br />
<br />
运行以下命令使配置生效: <br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|gfxpayload}}属性会确保内核也保持这个分辨率<br />
<br />
<br />
{{Note|<br />
* 如果示例不起作用,请尝试用{{ic|1=vbemode="0x105"}}代替{{ic|1=gfxmode="1024x768x32"}}.请使用适合你屏幕的分辨率.<br />
* 可以通过{{ic|1=# hwinfo --framebuffer}}命令来显示所有可以使用的分辨率模式(hwinfo在[[community]]里),在GRUB命令行下,可以使用{{ic|1=vbeinfo}} 命令<br />
}}<br />
<br />
这种方法不管用的话,可以试试老的{{ic|1=vga=}}方法.将它添加到{{ic|1="GRUB_CMDLINE_LINUX_DEFAULT="}}里面就行了,比如<br />
{{ic|1="GRUB_CMDLINE_LINUX_DEFAULT="quiet splash vga=792"}} <br />
这会将系统的分辨率设定为1024*768<br />
<br />
可以选择以下分辨率中的一种:{{ic|640×480}}, {{ic|800×600}}, {{ic|1024×768}}, {{ic|1280×1024}}, {{ic|1600×1200}}, {{ic|1920×1200}}<br />
<br />
==== 915resolution hack ====<br />
<br />
有些时候,Intel显卡无法通过{{ic|1=# hwinfo --framebuffer}} 或{{ic|1=vbeinfo}}显示你需要的分辨率.这种情况下,你可以使用{{ic|915resolution hack}}.915resolution hack会临时性的修改显卡BIOS来添加所需的分辨率.详情请参考[http://915resolution.mango-lang.org/ 915resolution's home page] <br />
<br />
首先,找一个你想要替代的视频模式.例如在GRUB命令行模式下运行:<br />
{{hc|sh:grub> 915resolution -l|<br />
Intel 800/900 Series VBIOS Hack : version 0.5.3<br />
[...]<br />
'''Mode 30''' : 640x480, 8 bits/pixel<br />
[...]<br />
}}<br />
然后,使用{{ic|1440x900}} 分辨率覆盖{{ic|Mode 30}}<br />
{{hc|/etc/grub.d/00_header|<br />
[...]<br />
'''915resolution 30 1440 900 # Inserted line'''<br />
set gfxmode&#61;${GRUB_GFXMODE}<br />
[...]<br />
}}<br />
最后,设置{{ic|GRUB_GFXMODE}},重新生成GRUB配置文件,重启并测试是否生效:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
# reboot<br />
<br />
==== 背景图像和点阵字体 ====<br />
<br />
GRUB原生支持设置背景图像和点阵字体(以pf2格式).{{Pkg|grub}}包含unifont字体,名为{{ic|unicode.pf2}}.(也有可能只包含名为{{ic|ascii.pf2}}的ASCII字符字体)<br />
<br />
GRUB支持的图像格式有tga,png,jpeg.所支持的最大图像分辨率跟硬件有关.<br />
<br />
Make sure you have set up the proper [[#Setting the framebuffer resolution|framebuffer resolution]].<br />
请确保你已经设定了合适的[[#设定帧缓冲的分辨率|帧缓冲分辨率]]<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
GRUB_BACKGROUND="/boot/grub/myimage"<br />
#GRUB_THEME="/path/to/gfxtheme"<br />
GRUB_FONT="/path/to/font.pf2"<br />
<br />
{{Note|如果你将GRUB安装在单独的分区上, {{ic|/boot/grub/myimage}} 应该改为 {{ic|/grub/myimage}}.}}<br />
<br />
重新生成配置文件:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
如果成功的添加了背景图片,那么用户会在命令行中看到{{ic|"Found background image..."}}.<br />
如果没有看到{{ic|"Found background image..."}},图像信息就可能没有嵌入{{ic|grub.cfg}}中了.<br />
<br />
如果图像没有正确显示,执行如下检查:<br />
* 在{{ic|/etc/default/grub}}里,图像的路径和名字是否正确<br />
* 图像的大小和格式是否合适(tga,png,8-bit jpg)<br />
* 图像是不是以RGB模式存储,是不是没有索引<br />
* {{ic|/etc/default/grub}}里面是不是没有开启console模式<br />
* 是否执行{{ic|grub-mkconfig}}以重新生成配置文件<br />
<br />
==== 主题====<br />
<br />
下面的例子将展示如何使用GRUB包重的starfield主题:<br />
<br />
编辑 {{ic|/etc/default/grub}}<br />
GRUB_THEME="/usr/share/grub/themes/starfield/theme.txt"<br />
<br />
重生成配置:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
配置成功的话,在重生成配置过程中,会出现{{ic|Found theme: /usr/share/grub/themes/starfield/theme.txt}}.使用主题就不会使用之前的背景图像<br />
<br />
====目录颜色====<br />
<br />
GRUB支持设置目录颜色.可使用的颜色能从[https://www.gnu.org/software/grub/manual/html_node/Theme-file-format.html the GRUB Manual]里面找到.示例如下:<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
GRUB_COLOR_NORMAL="light-blue/black"<br />
GRUB_COLOR_HIGHLIGHT="light-cyan/blue"<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
====隐藏目录====<br />
<br />
GRUB特性之一就是支持隐藏/跳过目录,同时支持按{{ic|Esc}}来打断隐藏/跳过.同时还支持设置是否显示timeout计时器<br />
下面的例子设置5s钟内没有按Esc键就启动默认的启动项,并且在屏幕上显示倒计时:<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
<br />
GRUB_HIDDEN_TIMEOUT=5<br />
GRUB_HIDDEN_TIMEOUT_QUIET=false<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
==== 禁用framebuffer ====<br />
<br />
Users who use NVIDIA proprietary driver might wish to disable GRUB's framebuffer as it can cause problems with the binary driver.<br />
使用NVIDIA私有驱动的用户可能希望禁用GRUB的framebuffer,因为会导致驱动错误.<br />
<br />
在{{ic|/etc/default/grub}}中添加(如果已经有背注释掉的这行,就取消注释):<br />
GRUB_TERMINAL_OUTPUT=console<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
如果你想保留GRUB的framebuffer,解决方法是在GRUB载入内核前进入文字模式.可以通过在{{ic|/etc/default/grub}}设置如下:<br />
GRUB_GFXPAYLOAD_LINUX=text<br />
然后重建配置档.<br />
<br />
=== 其他选项 ===<br />
<br />
==== LVM ====<br />
<br />
如果使用[[LVM]]做为启动设备,那么在启动项里添加:<br />
<br />
insmod lvm<br />
<br />
然后在启动项里指定root:<br />
<br />
set root=lvm/''lvm_group_name''-''lvm_logical_boot_partition_name''<br />
<br />
示例如下:<br />
<br />
# (0) Arch Linux<br />
menuentry "Arch Linux" {<br />
insmod lvm<br />
set root=lvm/VolumeGroup-lv_boot<br />
# you can only set following two lines<br />
linux /vmlinuz-linux root=/dev/mapper/VolumeGroup-root ro<br />
initrd /initramfs-linux.img<br />
}<br />
<br />
==== 阵列====<br />
<br />
通过添加{{ic|insmod mdraid}},GRUB能够很方便的处理磁盘阵列.比如{{ic|/dev/md0}}:<br />
<br />
set root=(md0)<br />
<br />
阵列上的分区(比如{{ic|/dev/md0p1}})则为:<br />
<br />
set root=(md0,1)<br />
<br />
如果启动分区在raid1上,想要安装grub,只需要在两个磁盘上分别运行grub-install即可:<br />
<br />
grub-install --target=i386-pc --recheck --debug /dev/sda && grub-install --target=i386-pc --recheck --debug /dev/sdb<br />
<br />
这时/dev/sda和/dev/sdb就都有了raid1的启动文件夹/boot了<br />
<br />
==== 持久块设备命名法 ====<br />
<br />
[[Persistent block device naming|持久块设备命名法]](Persistent block device naming)的一个目的是使用全局的UUID来区分分区,而不是用老的{{ic|/dev/sd*}}表示法.好处显而易见.<br />
<br />
GRUB默认使用持久块设备命名法<br />
<br />
{{Note|每次文件系统调整过后,就需要用新的UUID来更新{{ic|/boot/grub.cfg}}.通过Live-CD调整分区和文件系统后要特别注意这点}}<br />
<br />
是否使用UUID由{{ic|/etc/default/grub}}里的这个选项控制:<br />
<br />
# GRUB_DISABLE_LINUX_UUID=true<br />
<br />
无论如何,变更配置后请重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
==== 使用卷标 ====<br />
<br />
GRUB支持以卷标识别文件系统(通过{{ic|search}}命令的{{ic|--label参数}}).<br />
<br />
首先,给文件系统设置一个卷标:<br />
<br />
# tune2fs -L ''LABEL'' ''PARTITION''<br />
<br />
然后在启动项中使用这个卷标:<br />
<br />
menuentry "Arch Linux, session texte" {<br />
search --label --set=root archroot<br />
linux /boot/vmlinuz-linux root=/dev/disk/by-label/archroot ro<br />
initrd /boot/initramfs-linux.img<br />
}<br />
<br />
====调用之前的启动项====<br />
<br />
GRUB能够记住你当前使用的启动项并且在下次启动时将其作为默认项.当你使用多个内核或操作系统时,这个特性很有用.<br />
编辑{{ic|/etc/default/grub}}中的{{ic|GRUB_DEFAULT}}选项:<br />
<br />
GRUB_DEFAULT=saved<br />
<br />
上面的命令会告诉GRUB使用记住的启动项为默认启动项.<br />
将下面的行添加到{{ic|/etc/default/grub}}会让GRUB记住当前的启动项:<br />
<br />
GRUB_SAVEDEFAULT=true<br />
<br />
{{Note|手动添加启动项到{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}中,比如Windows启动项,需要添加{{ic|savedefault}}}}<br />
请记住重建配置档.<br />
<br />
==== 改变默认启动项 ====<br />
<br />
可以通过修改{{ic|/etc/default/grub}}中的{{ic|GRUB_DEFAULT}}值来改变默认启动项<br />
<br />
GRUB_DEFAULT=0<br />
<br />
GRUB启动项序号从0开始计数.0代表第一个启动项.<br />
<br />
除了使用启动项序号,也可以使用启动项名:<br />
<br />
GRUB_DEFAULT='Arch Linux, with Linux core repo kernel'<br />
<br />
{{Note|请记住重建配置档}}<br />
<br />
==== 安全====<br />
<br />
如果你想禁止其他人改变启动参数或者使用GRUB命令行,可以给GRUB配置添加用户名/密码.<br />
运行 {{ic|grub-mkpasswd-pbkdf2}},输入密码:<br />
{{hc|grub-mkpasswd-pbkdf2|<br />
[...]<br />
Your PBKDF2 is grub.pbkdf2.sha512.10000.C8ABD3E93C4DFC83138B0C7A3D719BC650E6234310DA069E6FDB0DD4156313DA3D0D9BFFC2846C21D5A2DDA515114CF6378F8A064C94198D0618E70D23717E82.509BFA8A4217EAD0B33C87432524C0B6B64B34FBAD22D3E6E6874D9B101996C5F98AB1746FE7C7199147ECF4ABD8661C222EEEDB7D14A843261FFF2C07B1269A<br />
}}<br />
然后将下面的内容添加到{{ic|/etc/grub.d/00_header}}:<br />
{{hc|/etc/grub.d/00_header|<br />
cat << EOF<br />
<br />
set superusers<nowiki>=</nowiki>"'''username'''"<br />
password_pbkdf2 '''username''' '''<password>'''<br />
<br />
EOF<br />
}}<br />
{{Note|不能直接将<br />
set superusers<nowiki>=</nowiki>"'''username'''"<br />
password_pbkdf2 '''username''' '''<password>'''<br />
添加到/etc/grub.d/00_header中去,而必须使用上述方法.否则会报错<br />
password_pbkdf2: not found<br />
}}<br />
{{ic|<password>}}是{{ic|grub-mkpasswd_pbkdf2}}生成的那个加密过后密码.<br />
<br />
然后重建配置档.其他用户没有密码就不能变更GRUB配置或者使用GRUB命令行了.<br />
<br />
可以参考[https://www.gnu.org/software/grub/manual/grub.html#Security the GRUB manual]中的"Security"部分来进行更多的客制化安全设定<br />
<br />
==== root加密 ====<br />
<br />
将{{ic|1=cryptdevice=/dev/yourdevice:label}}添加到{{ic|/etc/default/grub}}中的{{ic|GRUB_CMDLINE_LINUX}}配置项,可以让GRUB传递参数给内核,让内核对root加密:<br />
<br />
{{Tip|如果你使用从GRUB Legacy升级而来,检查{{ic|/boot/grub/menu.lst.pacsave}}以获取正确的device/label. 在{{ic|kernel /vmlinuz-linux}}后去找label.}}<br />
<br />
将root映射到{{ic|/dev/mapper/root}}:<br />
<br />
GRUB_CMDLINE_LINUX="cryptdevice=/dev/sda2:root"<br />
<br />
当然,需要在rootfs上禁用UUID:<br />
<br />
GRUB_DISABLE_LINUX_UUID=true<br />
<br />
Regenerate the configuration.<br />
<br />
==== 设定下次启动的启动项(一次性,非持久) ====<br />
<br />
命令{{ic|grub-reboot}}可以设置下次启动时启动哪个启动项而不必修改配置文件或者在启动时手动选择.这个设置是一次性的,即不会改变GRUB的默认启动项.<br />
<br />
{{Note|需要在{{ic|/etc/default/grub}}中设定 {{ic|1=GRUB_DEFAULT=saved}},然后重建配置档.在手动生成的 {{ic|grub.cfg}}中, 使用 {{ic|1=set default="${saved_entry}"}}.}}<br />
<br />
==== 启动时隐藏GRUB界面,除非按着SHIFT键 ====<br />
<br />
为了获取更快的启动速度,而不用等GRUB倒计时,可以命令GRUB在启动时隐藏目录,除非SHIFT被按着.<br />
将如下行添加到{{ic|/etc/default/grub}}:<br />
<br />
GRUB_FORCE_HIDDEN_MENU="true"<br />
<br />
然后创建如下文件:<br />
<br />
{{hc|/etc/grub.d/31_hold_shift|<nowiki><br />
#! /bin/sh<br />
set -e<br />
<br />
# grub-mkconfig helper script.<br />
# Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.<br />
#<br />
# GRUB is free software: you can redistribute it and/or modify<br />
# it under the terms of the GNU General Public License as published by<br />
# the Free Software Foundation, either version 3 of the License, or<br />
# (at your option) any later version.<br />
#<br />
# GRUB is distributed in the hope that it will be useful,<br />
# but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />
# GNU General Public License for more details.<br />
#<br />
# You should have received a copy of the GNU General Public License<br />
# along with GRUB. If not, see <http://www.gnu.org/licenses/>.<br />
<br />
prefix="/usr"<br />
exec_prefix="${prefix}"<br />
datarootdir="${prefix}/share"<br />
<br />
export TEXTDOMAIN=grub<br />
export TEXTDOMAINDIR="${datarootdir}/locale"<br />
source "${datarootdir}/grub/grub-mkconfig_lib"<br />
<br />
found_other_os=<br />
<br />
make_timeout () {<br />
<br />
if [ "x${GRUB_FORCE_HIDDEN_MENU}" = "xtrue" ] ; then <br />
if [ "x${1}" != "x" ] ; then<br />
if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then<br />
verbose=<br />
else<br />
verbose=" --verbose"<br />
fi<br />
<br />
if [ "x${1}" = "x0" ] ; then<br />
cat <<EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if keystatus; then<br />
if keystatus --shift; then<br />
set timeout=-1<br />
else<br />
set timeout=0<br />
fi<br />
else<br />
if sleep$verbose --interruptible 3 ; then<br />
set timeout=0<br />
fi<br />
fi<br />
fi<br />
EOF<br />
else<br />
cat << EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if sleep$verbose --interruptible ${GRUB_HIDDEN_TIMEOUT} ; then<br />
set timeout=0<br />
fi<br />
fi<br />
EOF<br />
fi<br />
fi<br />
fi<br />
}<br />
<br />
adjust_timeout () {<br />
if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then<br />
cat <<EOF<br />
if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then<br />
EOF<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_BUTTON}"<br />
echo else<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}"<br />
echo fi<br />
else<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}"<br />
fi<br />
}<br />
<br />
adjust_timeout<br />
<br />
cat <<EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if keystatus; then<br />
if keystatus --shift; then<br />
set timeout=-1<br />
else<br />
set timeout=0<br />
fi<br />
else<br />
if sleep$verbose --interruptible 3 ; then<br />
set timeout=0<br />
fi<br />
fi<br />
fi<br />
EOF<br />
</nowiki>}}<br />
<br />
===在GRUB中直接从ISO启动===<br />
<br />
编辑{{ic|/etc/grub.d/40_custom}} 或 {{ic|/boot/grub/custom.cfg}},给目标ISO添加一个启动项.然后使用{{ic|grub-mkconfig -o /boot/grub/grub.cfg}}更新配置档<br />
<br />
==== Arch ISO ====<br />
<br />
{{Note|下面的例子都是假设ISO文件位于{{ic|hd0,6}}分区上的{{ic|/archives}}文件夹里}}<br />
{{Tip|For thumbdrives, use something like {{ic|(hd1,$partition)}} and either {{ic|/dev/sdb'''Y'''}} for the {{ic|img_dev}} parameter or [[Persistent block device naming|a persistent name]], e.g. {{ic|img_dev&#61;/dev/disk/by-label/CORSAIR}}.}}<br />
{{Tip|对于闪存,使用{{ic|(hd1,$partition)}}或{{ic|/dev/sdb'''Y'''}}作为{{ic|img_dev}}参数的值,或者使用持久块设备命名法命名的设备,比如{{ic|img_dev&#61;/dev/disk/by-label/CORSAIR}}.}}<br />
===== x86_64 =====<br />
<br />
menuentry "Archlinux-2013.05.01-dual.iso" --class iso {<br />
set isofile="/archives/archlinux-2013.05.01-dual.iso"<br />
set partition="6"<br />
loopback loop (hd0,$partition)/$isofile<br />
linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop<br />
initrd (loop)/arch/boot/x86_64/archiso.img<br />
}<br />
<br />
===== i686 =====<br />
<br />
menuentry "Archlinux-2013.05.01-dual.iso" --class iso {<br />
set isofile="/archives/archlinux-2013.05.01-dual.iso"<br />
set partition="6"<br />
loopback loop (hd0,$partition)/$isofile<br />
linux (loop)/arch/boot/i686/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop<br />
initrd (loop)/arch/boot/i686/archiso.img<br />
}<br />
<br />
==== Ubuntu ISO ====<br />
<br />
{{Note|下面的例子都是假设ISO文件位于{{ic|hd0,6}}分区上的{{ic|/archives}}文件夹里. 用户需要根据自己系统的实际情况调整.}}<br />
<br />
menuentry "ubuntu-13.04-desktop-amd64.iso" {<br />
set isofile="/archives/ubuntu-13.04-desktop-amd64.iso"<br />
loopback loop (hd0,6)/$isofile<br />
linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash --<br />
initrd (loop)/casper/initrd.lz<br />
}<br />
<br />
menuentry "ubuntu-12.04-desktop-amd64.iso" {<br />
set isofile="/archives/ubuntu-12.04-desktop-amd64.iso"<br />
loopback loop (hd0,6)/$isofile<br />
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash --<br />
initrd (loop)/casper/initrd.lz<br />
}<br />
<br />
==== Other ISOs ====<br />
<br />
可从[http://askubuntu.com/questions/141940/how-to-boot-live-iso-images 这里]获取其他ISO的配置.<br />
<br />
== 使用GRUB命令行 ==<br />
<br />
MBR太小,所以不足以存储所有的GRUB模组.MBR里面只有启动目录配置和一些很基本的命令.GRUB的主要功能通过{{ic|/boot/grub}}里的模组实现,而且可以按需加载.出现错误时,GRUB可能不能引导启动(比如,磁盘分区发生了变化).这时候,一般会出现命令行界面.<br />
<br />
GRUB不止提供一个shell.如果GRUB不能读取到启动目录配置,但是能找到磁盘,你很可能需要进入"normal" shell:<br />
sh:grub><br />
如果有更严重的问题(比如,GRUB找不到必须的文件了),你就可能需要进入"rescue" shell:<br />
grub rescue><br />
<br />
"rescue" shell是"normal" shell的一个子集,其支持的功能更少.如果不幸进入了"rescue" shell里,首先记载"normal"模组,然后启动"normal" shell:<br />
grub rescue> set prefix=(hdX,Y)/boot/grub<br />
grub rescue> insmod (hdX,Y)/boot/grub/i386-pc/normal.mod<br />
rescue:grub> normal<br />
<br />
=== 分页支持 ===<br />
<br />
GRUB支持对长输出进行分页(比如运行{{ic|help}}的输出).不过只有normal shell支持分页,而rescue shell不支持.开启分页支持的方法如下:<br />
<br />
sh:grub> set pager=1<br />
<br />
===使用命令行引导操作系统 ===<br />
<br />
grub> <br />
<br />
可以使用GRUB命令行引导操作系统,一个典型的应用场景是通过'''chainloading'''来引导另一个Windows或Linux<br />
<br />
''ChainLoading''的意思是用当前的bootloader去载入另一个bootloader,所以叫做'''链式'''加载.这个bootloader可能位于MBR,也可能在另一个分区的引导扇区上.<br />
<br />
==== 链式加载一个分区 ====<br />
<br />
set root=(hdX,Y)<br />
chainloader +1<br />
boot<br />
<br />
X=0,1,2...<br />
Y=1,2,3...<br />
<br />
比如链式加载一个位于首磁盘,首分区上的Windows:<br />
<br />
set root=(hd0,1)<br />
chainloader +1<br />
boot<br />
<br />
也可以使用GRUB链式加载另一个分区引导扇区上的GRUB.<br />
<br />
==== 链式加载磁盘====<br />
<br />
set root=hdX<br />
chainloader +1<br />
boot<br />
<br />
====正常载入 ====<br />
<br />
请参考[[#使用应急命令行]]<br />
<br />
==图形化配置工具==<br />
<br />
* {{App|grub-customizer|定制bootloader(GRUB or BURG)|https://launchpad.net/grub-customizer|{{AUR|grub-customizer}}}}<br />
* {{App|grub2-editor|KDE4上配置GRUB的控制模组|http://kde-apps.org/content/show.php?content&#61;139643|{{AUR|grub2-editor}}{{Broken package link|{{aur-mirror|grub2-editor}}}}}}<br />
* {{App|kcm-grub2|可以管理大部分常用GRUB配置的kcm模组|http://kde-apps.org/content/show.php?content&#61;137886|{{AUR|kcm-grub2}}{{Broken package link|{{aur-mirror|kcm-grub2}}}}}}<br />
* {{App|startupmanager|GRUB Legacy, GRUB, Usplash and Splashy的图形化配置工具([https://launchpad.net/startup-manager/+announcement/8300 abandonned])|http://sourceforge.net/projects/startup-manager/|{{AUR|startupmanager}}}}<br />
<br />
== parttool ==<br />
<br />
如果你安装了Windows 9x系列操作系统,而它们隐藏了磁盘(比如C盘),GRUB能够使用parttool来设置是否隐藏.比如,想从三个Windows 9x系统的第三个启动,可以这样:<br />
parttool hd0,1 hidden+ boot-<br />
parttool hd0,2 hidden+ boot-<br />
parttool hd0,3 hidden- boot+<br />
set root=hd0,3<br />
chainloader +1<br />
boot<br />
<br />
==使用应急命令行==<br />
<br />
请先阅读[[#使用GRUB命令行]].如果无法进入命令行,请尝试使用Live CD或者其他rescue磁盘引导,然后修正错误.不过有些时候我们手上没有此类rescue磁盘,这时,应急命令行(rescue console)就可以派上用场了.<br />
<br />
GRUB应急命令行里可用的命令有{{ic|insmod}}, {{ic|ls}}, {{ic|set}}, {{ic|unset}}.可以使用set/unset修改变量,使用insmod来载入模组.<br />
<br />
首先,用户必须知道启动分区({{ic|/boot}}所在位置然后设置:<br />
<br />
grub rescue> set prefix=(hdX,Y)/boot/grub<br />
<br />
通过加载{{ic|linux}}模组来扩展命令行的功能:<br />
<br />
grub rescue> insmod (hdX,Y)/boot/grub/linux.mod<br />
<br />
{{Note|如果/boot是在单独的分区上,请从路径中移除/boot.(即. 输入 {{ic|1=set prefix=(hdX,Y)/grub}} 然后{{ic|insmod (hdX,Y)/grub/linux.mod}})}}<br />
<br />
这个模组会启动对我们熟悉的{{ic|linux}} 和 {{ic|initrd}} 命令的支持 (请参考[[#配置]]).<br />
<br />
比如:<br />
set root=(hd0,5)<br />
linux /boot/vmlinuz-linux root=/dev/sda5<br />
initrd /boot/initramfs-linux.img<br />
boot<br />
<br />
如果/boot在单独分区上:<br />
set root=(hd0,5)<br />
linux /vmlinuz-linux root=/dev/sda6<br />
initrd /initramfs-linux.img<br />
boot<br />
<br />
成功启动Arch后,用户可以修正配置的错误或者重新安装GRUB.<br />
<br />
关于修正配置和重新安装GRUB,请参考[[#配置]]和[[#安装]]章节.<br />
==使用UUID的基础脚本 ==<br />
<br />
如果你想要使用UUID来避免不可靠的BIOS设备命名或者正在研究GRUB语法,这里有个使用UUID的示例性的启动项配置脚本.如果你想要将其移植到自己的系统上,只需要修改UUID就行了.这个例子假设系统的boot和root文件系统是在不同的分区上.如果你还有其他分区,请做相应修改.<br />
<br />
menuentry "Arch Linux 64" {<br />
# Set the UUIDs for your boot and root partition respectively<br />
set the_boot_uuid=ece0448f-bb08-486d-9864-ac3271bd8d07<br />
set the_root_uuid=c55da16f-e2af-4603-9e0b-03f5f565ec4a<br />
<br />
# (Note: This may be the same as your boot partition)<br />
<br />
# Get the boot/root devices and set them in the root and grub_boot variables<br />
search --fs-uuid $the_root_uuid --set=root<br />
search --fs-uuid $the_boot_uuid --set=grub_boot<br />
<br />
# Check to see if boot and root are equal.<br />
# If they are, then append /boot to $grub_boot (Since $grub_boot is actually the root partition)<br />
if [ $the_boot_uuid == $the_root_uuid ] ; then<br />
set grub_boot=($grub_boot)/boot<br />
else<br />
set grub_boot=($grub_boot)<br />
fi<br />
<br />
# $grub_boot now points to the correct location, so the following will properly find the kernel and initrd<br />
linux $grub_boot/vmlinuz-linux root=/dev/disk/by-uuid/$the_root_uuid ro<br />
initrd $grub_boot/initramfs-linux.img<br />
}<br />
== 异常处理==<br />
<br />
=== Intel BIOS不能引导GPT ===<br />
<br />
一些Intel的BIOS要求至少要一个可启动的分区(MBR方案下的),导致GTP方案下的分区GRUB无法启动.<br />
<br />
可以通过fdisk来将一个GPT分区标志为'boot'((最好就设在你为GRUB创建的那个1007KiB分区上)),这样就可以绕过这个问题了:<br />
<br />
1.对目标磁盘(比如/dev/sda)运行fdisk<br />
2.按{{ic|a}}键,然后选择想要设置'boot'标志的分区<br />
3.最后按{{ic|w}}键,将变更写入磁盘<br />
<br />
<br />
{{Note|必须使用fdisk或者类似于它的工具来设置'boot flag',不能用Gparted等,因为它们不会在MBR里面设置'boot flag'.}}<br />
<br />
请参考 [http://www.rodsbooks.com/gdisk/bios.html| http://www.rodsbooks.com/gdisk/bios.html]<br />
<br />
=== 启用调试信息 ===<br />
<br />
在{{ic|grub.cfg}}里添加:<br />
<br />
set pager=1<br />
set debug=all<br />
<br />
=== "No suitable mode found" error ===<br />
<br />
启动时可能出现如下提示信息:<br />
<br />
error: no suitable mode found<br />
Booting however<br />
<br />
然后你需要以合适的视频模式({{ic|gfxmode}})启动 GRUB 图形化终端({{ic|gfxterm}}).视频模式由GRUB通过'gfxpayload'变量传递给linux内核.在UEFI系统下,如果没有初始化视频模式的值,终端上就不会显示内核启动消息(至少直到KMS开始运行)<br />
<br />
将{{ic|/usr/share/grub/unicode.pf2}}复制到 ${GRUB_PREFIX_DIR}({{ic|/boot/grub/}} .如果GRUB UEFI安装时设定了{{ic|1=--boot-directory=$esp/EFI}},那么复制的目的文件夹是{{ic|$esp/EFI/grub/}}:<br />
<br />
# cp /usr/share/grub/unicode.pf2 ${GRUB_PREFIX_DIR}<br />
<br />
如果{{ic|/usr/share/grub/unicode.pf2}} 不存在, 安装{{Pkg|bdf-unifont}}, 创建 {{ic|unifont.pf2}} 文件然后将其复制到 {{ic|${GRUB_PREFIX_DIR<nowiki>}</nowiki>}}:<br />
<br />
# grub-mkfont -o unicode.pf2 /usr/share/fonts/misc/unifont.bdf<br />
<br />
然后,在{{ic|grub.cfg}}文件中添加如下行:<br />
(会给linux内核传递合适的视频模式,否则你只会得到一个黑屏,虽然系统还是会启动成功)<br />
BIOS 系统:<br />
<br />
insmod vbe<br />
<br />
UEFI 系统:<br />
<br />
insmod efi_gop<br />
insmod efi_uga<br />
<br />
在这些行后添加(BIOS&&UEFI):<br />
<br />
insmod font<br />
<br />
if loadfont ${prefix}/fonts/unicode.pf2<br />
then<br />
insmod gfxterm<br />
set gfxmode=auto<br />
set gfxpayload=keep<br />
terminal_output gfxterm<br />
fi<br />
<br />
如果要gfxterm(图形化终端)工作正常,{{ic|${GRUB_PREFIX_DIR<nowiki>}</nowiki>}}文件夹里面应该要有{{ic|unicode.pf2}} .<br />
<br />
=== 出现"msdos-style"错误消息 ===<br />
<br />
以下错误可能出现在你将GRUB安装到VMware上时.请阅读[https://bbs.archlinux.org/viewtopic.php?pid=581760#p581760 相关链接].这种情况是因为首分区直接从MBR后开始(即第64个扇区),而不是和正常的那样有1到2M post-MBR gap.请参阅[[#MBR专用指令]]<br />
<br />
grub-setup: warn: This msdos-style partition label has no post-MBR gap; embedding will not be possible!<br />
grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.<br />
However, blocklists are UNRELIABLE and its use is discouraged.<br />
grub-setup: error: If you really want blocklists, use --force.<br />
<br />
=== GRUB UEFI 启动到了rescue shell下 ===<br />
<br />
如果GRUB直接就启动到了rescue shell下,而且没报错,这可能是因为{{ic|grub.cfg}}丢失或者位置不对.如果GRUB UEFI 安装时设定了{{ic|--boot-directory}}参数,而{{ic|grub.cfg}}文件丢失,会出现这个问题.如果启动分区的分区号发生了变化(这个分区号会被直接编码到{{ic|grubx64.efi}}文件中),也会出现这个问题.<br />
<br />
=== GRUB UEFI 无法被载入 ===<br />
<br />
下面是一个EFI启动项信息示例:<br />
{{hc|# efibootmgr -v|<br />
BootCurrent: 0000<br />
Timeout: 3 seconds<br />
BootOrder: 0000,0001,0002<br />
Boot0000* Grub HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\efi\grub\grub.efi)<br />
Boot0001* Shell HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\EfiShell.efi)<br />
Boot0002* Festplatte BIOS(2,0,00)P0: SAMSUNG HD204UI<br />
}}<br />
<br />
如果启动后,屏幕直接变黑,几秒后就跳到了下一个启动项,根据[https://bbs.archlinux.org/viewtopic.php?pid=981560#p981560 相关链接]的说法是,将GRUB移动到root分区上可能会解决这个问题.必须先删除启动项,然后重建,变化如下:<br />
Boot0000* Grub HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\grub.efi)<br />
<br />
=== "Invalid signature"错误 ===<br />
<br />
如果在启动Windows时出现了"invalid signature"错误(比如在重新分区或者添加了新硬盘后),删除GRUB的磁盘mapping,然后重建:<br />
<br />
# mv /boot/grub/device.map /boot/grub/device.map-old<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|grub-mkconfig}}此时就应该生成了新的启动项了,包括Windows.确认能启动成功后,再将备份文件{{ic|/boot/grub/device.map-old}}删除.<br />
<br />
=== 引导过程卡死 ===<br />
<br />
如果在GRUB载入内核并初始化ramdisk后引导过程卡死了,请尝试移除{{ic|add_efi_memmap}}这个内核参数<br />
<br />
=== 回滚到 GRUB Legacy ===<br />
<br />
* 将GRUB2相关文件改名以为备份:<br />
<br />
# mv /boot/grub /boot/grub.nonfunctional<br />
<br />
* 将GRUB Legacy 移回{{ic|/boot}}:<br />
<br />
# cp -af /boot/grub-legacy /boot/grub<br />
<br />
* 恢复备份的MBR<br />
<br />
{{Warning|这个命令同时会恢复分区表,所以请要非常注意.}}<br />
<br />
# dd if=/path/to/backup/first-sectors of=/dev/sdX bs=512 count=1<br />
<br />
安全的方法是只恢复MBR中的启动代码:<br />
<br />
# dd if=/path/to/backup/mbr-boot-code of=/dev/sdX bs=446 count=1<br />
<br />
=== 其他系统不能自动发现Arch Linux===<br />
<br />
有人发现有些发行版不能使用{{ic|os-prober}}自动发现Arch Linux.据称先使用{{ic|/etc/lsb-release}}可能会解决这个问题.相关文件和工具可以在 [[official repositories|官方仓库]]的{{Pkg|lsb-release}}包中找到<br />
<br />
== 参阅 ==<br />
<br />
* 官方GRUB说明 - https://www.gnu.org/software/grub/manual/grub.html<br />
* Ubuntu GRUB Wiki - https://help.ubuntu.com/community/Grub2<br />
* 在UEFI系统上编译GRUB步骤描述- https://help.ubuntu.com/community/UEFIBooting<br />
* 维基百科[[Wikipedia:BIOS Boot partition|BIOS启动分区]]<br />
* 配置GRUB的完整说明 - http://members.iinet.net/~herman546/p20/GRUB2%20Configuration%20File%20Commands.html</div>
残月舞流殇
https://wiki.archlinux.org/index.php?title=GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=393220
GRUB (简体中文)
2015-08-23T02:47:16Z
<p>残月舞流殇: /* 安装到分区上或者无分区磁盘上 */</p>
<hr />
<div>[[Category:Boot loaders (简体中文)]]<br />
[[ar:GRUB]]<br />
[[cs:GRUB]]<br />
[[de:GRUB]]<br />
[[el:GRUB]]<br />
[[en:GRUB]]<br />
[[es:GRUB]]<br />
[[fr:GRUB]]<br />
[[he:GRUB]]<br />
[[id:GRUB2]]<br />
[[it:GRUB2]]<br />
[[ja:GRUB]]<br />
[[nl:GRUB]]<br />
[[ru:GRUB]]<br />
[[tr:GRUB2]]<br />
[[uk:GRUB]]<br />
[[zh-TW:GRUB2]]<br />
{{TranslationStatus (简体中文)|GRUB|2013-11-15|282734}}<br />
{{Related articles start (简体中文)}}<br />
{{Related|Burg}}<br />
{{Related|GRUB Legacy}}<br />
{{Related|GRUB EFI Examples}}<br />
{{Related articles end}}<br />
[http://www.gnu.org/software/grub/ GRUB2] 是下一代 GRand Unified Bootloader (GRUB,请不要和Grub Legacy混淆了)。 它来自下一代 GRUB 研究项目 [http://www.nongnu.org/pupa/ PUPA],代码全部重写,实现了模块化和增强了移植性。[http://www.gnu.org/software/grub/grub-faq.en.html#q1].<br />
<br />
简单的说,'''Boot Loader'''是电脑启动时运行的第一个程序,它负责装载内核并将控制权转交。内核再初始化操作系统的其它部分。<br />
<br />
==前言==<br />
* 引导程序是计算机启动时第一个运行的程序。它负责加载并将控制权转移到Linux内核。内核作为回报,将初始化操作系统剩余部分<br />
* 官方所称的GRUB代表的是本软件的第二版,即GRUB2,请参考[https://www.gnu.org/software/grub/].如果你是在找有关Grub Legacy的文章,请参考[[GRUB Legacy]].<br />
* GRUB2支持由zlib或者LZO压缩过的[[Btrfs]]格式的根目录,如果使用Btrfs,不需要单独的{{ic|/boot}}分区.<br />
* GRUB2 不支持[[F2fs]]格式的根目录,所以你需要为{{ic|/boot}}分区单独设置一个支持的文件系统.<br />
<br />
== BIOS 系统==<br />
=== GUID分区表(GPT)具体说明 ===<br />
BIOS/[[GPT]]配置中,一个 [http://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html BIOS启动分区]是必需的。GRUB将{{ic|core.img}}嵌入到这个分区。<br />
{{Note|<br />
* 在尝试分区之前请记住不是所有的系统都能够支持这种分区方案, 关于更多请阅读[[GUID Partition Table#BIOS systems|GUID分区表]].<br />
* This additional partition is only needed on a GRUB, BIOS/GPT partitioning scheme. Previously, for a GRUB, BIOS/MBR partitioning scheme, GRUB used the Post-MBR gap for the embedding the {{ic|core.img}}). GRUB for GPT, however, does not use the Post-GPT gap to conform to GPT specifications that require 1_megabyte/2048_sector disk boundaries.<br />
* For [[UEFI]] systems this extra partition is not required as no embedding of boot sectors takes place in that case.<br />
}}<br />
Create a mebibyte partition ({{ic|1=+1M}} with {{ic|fdisk}} or {{ic|gdisk}}) on the disk with no file system and type BIOS boot (''BIOS boot'' in fdisk, {{ic|ef02}} in gdisk, {{ic|bios_grub}} in {{ic|parted}}). This partition can be in any position order but has to be on the first 2 TiB of the disk. This partition needs to be created before GRUB installation. When the partition is ready, install the bootloader as per the instructions below.<br />
The post-GPT gap can also be used as the BIOS boot partition though it will be out of GPT alignment specification. Since the partition will not be regularly accessed performance issues can be disregarded (though some disk utilities will display a warning about it). In {{ic|fdisk}} or {{ic|gdisk}} create a new partition starting at sector 34 and spanning to 2047 and set the type. To have the viewable partitions begin at the base consider adding this partition last.<br />
<br />
=== 主引导记录(MBR)具体说明 ===<br />
<br />
一般来说,如果使用兼容DOS的分区对齐模式,MBR后面空间(post-MBR gap)的大小都是31KiB(MBR和第一个分区之间的空间).不过,为了提供足够的空间嵌入GRUB的{{ic|core.img}}文件({{bug|24103}}),建议将这个空间设置到1到2Mib.最好使用支持1MiB分区对齐的分区软件来分区,因为这样也能满足非512B扇区磁盘分区的需求.<br />
<br />
=== 安装 ===<br />
<br />
可以使用[[pacman|pacman]]从[[official repositories|官方仓库]]安装{{Pkg|grub}}包.安装完成后它会代替{{AUR|grub-legacy}}.<br />
<br />
{{Note|简单的安装Grub包并不会更新{{ic|/boot/grub/i386-pc/core.img}}和{{ic|/boot/grub/i386-pc}}里的GRUB模组.需要使用下面介绍的{{ic|grub-install}}来手动更新它们.}}<br />
<br />
==== 安装Boot文件 ====<br />
<br />
有三种方式安装GRUB Boot文件:<br />
<br />
* [[#安装到磁盘上|安装到磁盘上]] (推荐方式)<br />
* [[#安装到U盘|安装到U盘]] (用于恢复)<br />
* [[#安装到分区上或者无分区磁盘上|安装到分区或者无分区磁盘上]] (不推荐)<br />
* [[#只生成core.img|只生成core.img文件]] (最安全的方法, 但是需要另外的bootloader,比如[[Syslinux]]来进行链式加载{{ic|/boot/grub/i386-pc/core.img}})<br />
<br />
{{Note|请参考 http://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html 获取更详尽的资料}}<br />
<br />
===== 安装到磁盘上 =====<br />
<br />
{{Note|这种方法只限于将GRUB安装到可分区磁盘(MBR或GPT),GRUB 文件会被安装到{{ic|/boot/grub}},1st stage code会被安装到MBR的启动代码区域(以便和MBR分区表分开).对于无分区磁盘,请参考[[#安装到分区上或者无分区磁盘上]]}}<br />
<br />
以下命令会将{{ic|grub}}安装到MBR的启动代码区域,填充{{ic|/boot/grub}}文件夹,生成{{ic|/boot/grub/i386-pc/core.img}},将其嵌入post-MBR gap或者放入BIOS boot partition中(分别对应MBR分区表和GPT分区表):<br />
# grub-install --target=i386-pc --recheck --debug /dev/sda<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
如果你使用[[LVM]]来进行启动,你可以将GRUB安装在多个物理磁盘上.<br />
执行{{ic|grub-install}}并不会生成GRUB配置文件,请移至[[#生成GRUB配置文件]]一节<br />
<br />
===== 安装到U盘 =====<br />
<br />
假设你的U盘第一个分区是FAT32,其分区是/dev/sdy1<br />
<br />
# mkdir -p /mnt/usb ; mount /dev/sdy1 /mnt/usb<br />
# grub-install --target=i386-pc --recheck --debug --boot-directory=/mnt/usb/boot /dev/sdy<br />
# grub-mkconfig -o /mnt/usb/boot/grub/grub.cfg<br />
<br />
# optional, backup config files of grub.cfg<br />
# mkdir -p /mnt/usb/etc/default<br />
# cp /etc/default/grub /mnt/usb/etc/default<br />
# cp -a /etc/grub.d /mnt/usb/etc<br />
<br />
# sync ; umount /mnt/usb<br />
<br />
===== 安装到分区上或者无分区磁盘上 =====<br />
<br />
{{Note|GRUB并不推荐将其安装到分区启动扇区或者无分区磁盘上(Grub Legacy和syslinux相反).这种安装方式不安全,当升级时可能会损坏.Arch开发人员也不支持这种方式}}<br />
<br />
下面的命令将会将GRUB2安装到分区扇区或者无分区磁盘上(比如闪存上):<br />
<br />
# chattr -i /boot/grub/i386-pc/core.img<br />
# grub-install --target=i386-pc --recheck --debug --force /dev/sdaX<br />
# chattr +i /boot/grub/i386-pc/core.img<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
<br />
<br />
必须使用{{ic|--force}}选项来启用对blocklists(块列表)的支持,不应使用{{ic|1=--grub-setup=/bin/true}}(这个选项的效果类似于只生成{{ic|core.img}})<br />
{{ic|grub-install}}会生成以下警告,你可以通过这些警告判断发生了什么问题:<br />
/sbin/grub-setup: warn: Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea.<br />
/sbin/grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists. <br />
However, blocklists are UNRELIABLE and their use is discouraged.<br />
<br />
如果不指定{{ic|--force}}选项,会出现以下错误,并且不会将启动代码安装到启动扇区上: <br />
/sbin/grub-setup: error: will not proceed with blocklists<br />
<br />
而指定了{{ic|--force}},会出现:<br />
<br />
Installation finished. No error reported.<br />
<br />
{{ic|grub-setup}}不默认允许这种情况的原因是,在分区或者无分区磁盘上,{{ic|grub}}依赖于嵌入分区引导扇区的块列表(blocklists)来定位{{ic|/boot/grub/i386-pc/core.img}}和{{ic|/boot/grub}}.而{{ic|core.img}}在分区上的扇区位置很有可能随着分区文件系统的更改而变化(复制文件,删除文件等).详情请参考https://bugzilla.redhat.com/show_bug.cgi?id=728742 和 https://bugzilla.redhat.com/show_bug.cgi?id=730915.<br />
<br />
临时解决方案是给{{ic|/boot/grub/i386-pc/core.img}}文件加"不可变"(immutable)标志.只有当将{{ic|grub}}安装到分区启动扇区或者无分区磁盘上时才需要给core.img加"不可变"标志.<br />
执行{{ic|grub-install}}并不会生成GRUB配置文件,请移至[[#生成GRUB配置文件]]一节<br />
<br />
===== 只生成core.img =====<br />
<br />
通过添加{{ic|1=--grub-setup=/bin/true}}选项,{{ic|grub-install}}命令会填充{{ic|/boot/grub}}文件夹并生成{{ic|/boot/grub/i386-pc/core.img}},但是不会将grub启动引导代码嵌入到MBR,post-MBR gap和分区引导扇区中:<br />
<br />
# grub-install --target=i386-pc --grub-setup=/bin/true --recheck --debug /dev/sda<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
生成后,Grub Legacy或者syslinux就可以通过链式加载GRUB2的{{ic|core.img}}来间接加载Linux内核或者多启动内核了.<br />
<br />
==== 生成GRUB配置文件 ====<br />
<br />
最后,生成GRUB2所需的配置文件(在配置章节会详述):<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{Note|文件路径是{{ic|/boot/grub/grub.cfg}}, 而不是{{ic|/boot/grub/i386-pc/grub.cfg}}.}}<br />
<br />
如果GRUB在启动时报错"no suitable mode found",请参考[[#"No suitable mode found" error]].<br />
<br />
如果{{ic|grub-mkconfig}}执行失败,可以通过如下方式将{{ic|/boot/grub/menu.lst}}文件转化为{{ic|/boot/grub/grub.cfg}}<br />
{{Note|这种方法只适合BIOS,不适合UEFI}}<br />
# grub-menulst2cfg /boot/grub/menu.lst /boot/grub/grub.cfg<br />
<br />
示例如下:<br />
{{hc|/boot/grub/menu.lst|<nowiki><br />
default=0<br />
timeout=5<br />
<br />
title Arch Linux Stock Kernel<br />
root (hd0,0)<br />
kernel /vmlinuz-linux root=/dev/sda2 ro<br />
initrd /initramfs-linux.img<br />
<br />
title Arch Linux Stock Kernel Fallback<br />
root (hd0,0)<br />
kernel /vmlinuz-linux root=/dev/sda2 ro<br />
initrd /initramfs-linux-fallback.img<br />
</nowiki>}}<br />
<br />
{{hc|/boot/grub/grub.cfg|<nowiki><br />
set default='0'; if [ x"$default" = xsaved ]; then load_env; set default="$saved_entry"; fi<br />
set timeout=5<br />
<br />
menuentry 'Arch Linux Stock Kernel' {<br />
set root='(hd0,1)'; set legacy_hdbias='0'<br />
legacy_kernel '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'<br />
legacy_initrd '/initramfs-linux.img' '/initramfs-linux.img'<br />
<br />
}<br />
<br />
menuentry 'Arch Linux Stock Kernel Fallback' {<br />
set root='(hd0,1)'; set legacy_hdbias='0'<br />
legacy_kernel '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'<br />
legacy_initrd '/initramfs-linux-fallback.img' '/initramfs-linux-fallback.img'<br />
}<br />
</nowiki>}}<br />
<br />
如果你忘记创建GRUB配置文件{{ic|/boot/grub/grub.cfg}},然后直接重启到了GRUB命令行界面,输入以下命令:<br />
sh:grub> insmod legacycfg<br />
sh:grub> legacy_configfile ${prefix}/menu.lst<br />
选择启动到Arch下然后再重新创建合适的GRUB配置文件{{ic|/boot/grub/grub.cfg}}<br />
<br />
==== 多系统启动 ====<br />
<br />
为了实现多系统启动,需要安装{{Pkg|os-prober}}.安装后,再执行{{ic|grub-mkconfig -o /boot/grub/grub.cfg}}.如果执行失败,可能就需要手动添加启动条目了.(后面有指导)<br />
<br />
{{Note|如果找不到Windows,可以将其boot partition挂载后再试}}<br />
<br />
===== 在BIOS-MBR模式下安装的Microsoft Windows =====<br />
<br />
{{Note|GRUB支持直接从{{ic|bootmgr}}启动,现在不再需要链式加载分区启动扇区了}}<br />
<br />
{{Warning|{{ic|bootmgr}}位于'''系统分区'''('''system partition'''),而不是Windows系统所在的分区(比如C盘).在uuid-卷名列表中,系统分区是那个名为{{ic|LABEL&#61;"SYSTEM RESERVED"}} o或 {{ic|LABEL&#61;"SYSTEM"}}的项,而且这个分区一般只有100到200MB大(和Arch的启动分区差不多).请参考[[Wikipedia:System partition and boot partition]]}}<br />
<br />
本节假设你的Windows分区是{{ic|/dev/sda1}}.首先,找到Windows系统分区的UUID(Windows's SYSTEM PARTITION,{{ic|bootmgr存放其上}}).如果Windows {{ic|bootmgr}}的位置是{{ic|/media/SYSTEM_RESERVED/bootmgr}},对于 Windows Vista/7/8,执行:<br />
<br />
# grub-probe --target=fs_uuid /media/SYSTEM_RESERVED/bootmgr<br />
69B235F6749E84CE<br />
<br />
# grub-probe --target=hints_string /media/SYSTEM_RESERVED/bootmgr<br />
--hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1<br />
<br />
{{Note|对于Windows XP, 请在上面的命令中用{{ic|NTLDR}}替代{{ic|bootmgr}}. 注意,可能{{ic|NTLDR}}不是在名为"SYSTEM_RESERVED"的分区上,请在XP系统所在的分区上寻找 {{ic|NTLDR}}}}<br />
<br />
接着,将下面的代码添加到{{ic|/etc/grub.d/40_custom}} 或者{{ic|/boot/grub/custom.cfg}}中.然后,使用{{ic|grub-mkconfig}}重新生成{{ic|grub.cfg}},这样就能在BIOS-MBR配置下使用GRUB2引导启动Windows系统了:<br />
<br />
对于 Windows Vista/7/8<br />
<br />
if [ "${grub_platform}" == "pc" ]; then<br />
menuentry "Microsoft Windows Vista/7/8 BIOS-MBR" {<br />
insmod part_msdos<br />
insmod ntfs<br />
insmod search_fs_uuid<br />
insmod ntldr <br />
search --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 69B235F6749E84CE<br />
ntldr /bootmgr<br />
}<br />
fi<br />
<br />
对于Windows XP<br />
<br />
if [ "${grub_platform}" == "pc" ]; then<br />
menuentry "Microsoft Windows XP" {<br />
insmod part_msdos<br />
insmod ntfs<br />
insmod search_fs_uuid<br />
insmod ntldr <br />
search --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 69B235F6749E84CE<br />
ntldr /bootmgr<br />
}<br />
fi<br />
<br />
<br />
{{Note|在某些情况下,可能在安装Windows 8之前就已经安装了GRUB.启动Windows时可能会{{ic|\boot\bcd}}报错(错误代码为{{ic|0xc000000f}}).可以通过Wondows Recovery Console来修复这个错误:<br />
x:\> "bootrec.exe /fixboot" <br />
x:\> "bootrec.exe /RebuildBcd".<br />
'''不要'''使用{{ic|bootrec.exe /Fixmbr}},因为那会将GRUB清除掉<br />
}}<br />
<br />
{{ic|/etc/grub.d/40_custom}}可以做为创建{{ic|/etc/grub.d/nn_custom}}的模板.{{ic|nn}}定义了脚本执行的优先级.脚本执行的顺序又决定了grub引导启动目录的内容.<br />
{{Note|{{ic|nn}} 应该比06大,这样才能确保必要的脚本被优先执行}}<br />
<br />
== UEFI 系统==<br />
{{Note|建议阅读并理解[[UEFI]], [[GPT]] and [[UEFI Bootloaders]]}}<br />
===== Check if you have GPT and an ESP检查你是否使用GPT和ESP =====<br />
如果想要使用EFI在某个磁盘上进行启动,那么就需要对这个磁盘进行EFI系统分区(EFI System Partition,即ESP),GPT倒不是必须的,不过我们还是高度建议使用GTP,并且这也是本篇文章当前支持的方法.如果你在一个支持EFI,并且已经有操作系统的电脑上安装Arch(比如Windows 8),那么你已经有了ESP了.可以通过{{ic|parted}}来列出启动磁盘上的分区表以检查其是否支持GPT和ESP(假设这个启动磁盘是/dev/sda)<br />
<br />
# parted /dev/sda print<br />
<br />
如果使用GPT,那么会出现"分区表:GPT".如果使用EFI,那么会有一个文件系统为vfat的小分区(一般小于512MiB)并且被标志为启动分区.在这个小分区上,应该有一个名为EFI的文件夹.如果这些条件都满足,那么这就是ESP了.注意分区序号,因为之后安装GRUB2时你需要mount它.<br />
<br />
===== 建立ESP =====<br />
<br />
如果你没有ESP,请参考[[UEFI#EFI System Partition]]的引导来创建它<br />
<br />
=== 安装 ===<br />
<br />
{{Note|众所周知,不同的主板厂商的UEFI实现方式也不一样.我们鼓励用户将其配置过程中遇到的问题及其细节分享出来.为了保证[[GRUB]]页面的简洁,请参考[[GRUB/EFI examples (简体中文)]]<br />
}}<br />
<br />
首先,安装{{Pkg|grub}}, {{Pkg|dosfstools}}, {{Pkg|efibootmgr}}(后面两个包是为了让GRUB支持EFI).然后按下面的指导进行配置.<br />
{{Note|简单的安装这些包并不会更新{{ic|core.efi}}和ESP中的GRUB模组.请按下面的指导手动进行配置}}<br />
====安装boot文件====<br />
<br />
=====推荐方法=====<br />
<br />
{{Note|<br />
* 下面的命令是假设你正在为{{ic|x86_64-efi}}安装GRUB.(如果是为{{ic|IA32-efi}}安装GRUB,请用{{ic|i386-efi}}代替{{ic|x86_64-efi}})<br />
* 如果要成功执行下面的命令,你需要使用UEFI启动而不是BIOS.如果你从USB驱动器中的ISO文件启动系统,那么可以判断,你已经是从BIOS启动了.你需要[[Unified Extensible Firmware Interface#Create UEFI bootable USB from ISO|建立一个UEFI可启动USB磁盘]],然后重启,否则grub-install会报错.<br />
}}<br />
<br />
首先,将ESP挂载上(一般挂在到{{ic|/boot/efi}}上,然后将其路径赋值到$esp).如果是第一次安装GRUB,你可能需要建立{{ic|/boot/efi}}文件夹<br />
然后,将GRUB UEFI应用安装到{{ic|$esp/EFI/grub}},将其模组安装到{{ic|/boot/grub/x86_64-efi}}:<br />
<br />
# grub-install --target=x86_64-efi --efi-directory=$esp --bootloader-id=grub --recheck --debug<br />
<br />
{{Note|<br />
* 如果grub-install在sysfs和procfs上运行的时候报错"modprobe efivars",尝试[[Unified_Extensible_Firmware_Interface#Switch_to_efivarfs]].<br />
* 如果没有{{ic|--target}} 或{{ic|--directory}} 选项,grub-install就不能决定安装哪种固件.在这个情况下,grub-install会报错{{ic|source_dir does not exist. Please specify --target or --directory}}.<br />
* {{ic|--efi-directory}} and {{ic|--bootloader-id}}是只适用于GRUB UEFI的选项.{{ic|--efi-directory}}替代{{ic|--root-directory}}指定ESP的挂载点.{{ic|--bootloader-id}}指定存放{{ic|grubx64.efi}}文件的目录名<br />
* 可能你会注意到,不像安装GRUB到BIOS那样,在{{ic|grub-install}}命令后没有<device_path>选项(例如{{ic|/dev/sda}}).哪怕提供了<device_path>,也会被安装脚本忽略.因为UEFI bootloader不使用MBR或者分区启动扇区.<br />
}}<br />
<br />
现在GRUB已经安装好了.请移至[[#配置|配置]]一节继续.<br />
<br />
=====其他方法=====<br />
<br />
如果你想将所有的GRUB boot 文件放在ESP中,请给grub-install命令添加{{ic|--boot-directory&#61;$esp/EFI}}选项:<br />
<br />
# grub-install --target=x86_64-efi --efi-directory=$esp --bootloader-id=grub --boot-directory=$esp/EFI --recheck --debug<br />
<br />
下面的命令会将GRUB模组文件放在{{ic|$esp/EFI/grub}}中.使用这个方法,grub.cfg也会被放在ESP中,所以请确保grub-mkconfig指向了正确的地方:<br />
<br />
# grub-mkconfig -o $esp/EFI/grub/grub.cfg<br />
<br />
配置档和BIOS-GRUB是一样的.<br />
<br />
====在固件启动管理器中创建GRUB条目====<br />
<br />
{{ic|grub-install}}会自动尝试在启动管理器中创建GRUB条目.如果没有成功,请参考[[Beginners' guide#GRUB]],里面有关于使用{{ic|efibootmgr}}创建启动目录条目的介绍.一般来说,这个问题都是因为你没有以UEFI模式启动CD/USB造成的.请参考[[UEFI#Create UEFI bootable USB from ISO]].<br />
<br />
====创建GRUB Standalone模式的UEFI应用程序====<br />
<br />
可以建立一个{{ic|grubx64_standalone.efi}},这个应用将所有的模组嵌入自身的memdisk中,所以就不需要使用单独的目录来存放GRUB UEFI模组和其他相关文件了,使用{{Pkg|grub}}包里的{{ic|grub-mkstandalone}}可以实现这个功能.<br />
<br />
最简单的的方法就是使用{{ic|grub-mkstandalone}},不过,我们还可以指定嵌入哪些模组:<br />
<br />
# grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi/" --format="x86_64-efi" --compression="xz" \<br />
--output="$esp/EFI/grub/grubx64_standalone.efi" <你想嵌入的模组><br />
<br />
{{ic|grubx64_standalone.efi}}文件要求将{{ic|grub.cfg}}放置到{{ic|(memdisk)/boot/grub}}中,而这个memdisk是嵌入到EFI应用当中的.{{ic|grub-mkstandlone}}脚本允许传递将要嵌入memdisk的文件列表.(上面命令里的"<你想嵌入的模组>")<br />
<br />
如果你的{{ic|grub.cfg}}的路径是{{ic|/home/user/Desktop/grub.cfg}},那么需要创建一个临时的{{ic|/home/user/Desktop/boot/grub/}}目录,然后将grub.cfg复制到其中并进入这个目录并运行:<br />
<br />
# grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi/" --format="x86_64-efi" --compression="xz" \<br />
--output="$esp/EFI/grub/grubx64_standalone.efi" "boot/grub/grub.cfg"<br />
<br />
进入{{ic|/home/user/Desktop/boot/grub/}}但是传递{{ic|boot/grub/grub.cfg}}参数(请注意是{{ic|boot/}}而不是{{ic|/boot/}})的原因是{{ic|grub-mkstandalone}}会自动将boot/grub/grub.cfg处理为{{ic|/(memdisk)/boot/grub/grub.cfg}}<br />
<br />
如果你传递{{ic|/home/user/Desktop/grub.cfg}},那么处理后的结果会是{{ic|(memdisk)/home/user/Desktop/grub.cfg}}.如果传递{{ic|/home/user/Desktop/boot/grub/grub.cfg}},那么结果就是{{ic|(memdisk)/home/user/Desktop/boot/grub/grub.cfg}}.所以需要进入{{ic|/home/user/Desktop/boot/grub/}}并传递{{ic|boot/grub/grub.cfg}}参数,因为这样才能生成{{ic|grub.efi}}需要的{{ic|(memdisk)/boot/grub/grub.cfg}}.<br />
<br />
如果需要为{{ic|$esp/EFI/arch_grub/grubx64_standalone.efi}}创建一个UEFI启动器条目,使用{{ic|efibootmgr}}.[[#Create GRUB entry in the Firmware Boot Manager]]里有介绍<br />
<br />
==配置==<br />
<br />
可以自动生成配置档,也可以手动编辑{{ic|grub.cfg}}.<br />
{{Note|<br />
* 对于EFI系统,如果在安装GRUB时使用 {{ic|1=--boot-directory=$esp/EFI}}了选项,{{ic|grub.cfg}}文件必须和{{ic|grubx64.efi}}放在同一文件夹.如果没有使用这个选项,那么{{ic|grub.cfg}}应当在{{ic|/boot/grub/}}下,和GRUB BIOS系统一样.<br />
*在[http://members.iinet.net/~herman546/p20/GRUB2%20Configuration%20File%20Commands.html 这里]可以找到关于如何配置GRUB的完整描述.<br />
}}<br />
<br />
===使用grub-mkconfig自动生成配置档===<br />
<br />
和GRUB Legacy配置文件{{ic|menu.lst}}对应的GRUB配置文件是{{ic|/etc/default/grub}} 和{{ic|/etc/grub.d/*}}.{{ic|grub-mkconfig}}使用这些文件来生成{{ic|grub.cfg}}.grub-mkconfig默认输出至stdout.以下命令可以生成一个{{ic|grub.cfg}}文件:<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|/etc/grub.d/10_linux}}会自动为Arch Linux设置启动项.其他操作系统的话,可能需要手动在{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}中设置.<br />
<br />
{{Note|如果你在一个chroot环境中或者systemd-nspawn容器里执行这些操作,可能完全不会成功,因为grub-probe不能获取'/dev/sdaX'这样的典型路径.这种情况下,可以尝试使用[https://bbs.archlinux.org/viewtopic.php?pid&#61;1225067#p1225067 arch-chroot].}}<br />
<br />
====额外的参数====<br />
<br />
在{{ic|/etc/default/grub}}中设置{{ic|GRUB_CMDLINE_LINUX}}和{{ic|GRUB_CMDLINE_LINUX_DEFAULT}}变量可以实现将向Linux镜像传递额外的参数.生成{{ic|grub.cfg}}时,如果遇到普通启动项,这两个参数会一起使用,遇到''recovery''启动项,就只使用{{ic|GRUB_CMDLINE_LINUX}}参数.<br />
<br />
没有必要两者一起使用.这两个参数很有用.比如,如果要系统支持休眠后恢复,需要使用{{ic|<nowiki>GRUB_CMDLINE_LINUX_DEFAULT="resume=/dev/sdaX</nowiki> quiet"}} ({{ic|sda'''X'''}}是交换分区).这个选项会生成一个recovery启动项,这个启动项没有resume和quiet参数.其他的普通启动项也可能使用它们.(GRUB会为每个内核生成两个启动项,一个默认的一个recovery的,GRUB_CMDLINE_LINUX指定的参数会传递给这两个启动项.GRUB_CMDLINE_LINUX_DEFAULT指定的参数只会传递给默认启动项)<br />
<br />
当生成GRUB recovery启动项时,你必须在{{ic|/etc/default/grub}}中将{{ic|<nowiki>#GRUB_DISABLE_RECOVERY=true</nowiki>}} 注释掉.<br />
<br />
你也可以使用{{ic|<nowiki>GRUB_CMDLINE_LINUX="resume=/dev/disk/by-uuid/${swap_uuid}"</nowiki>}}, {{ic|${swap_uuid} }}是交换分区的[[Persistent_block_device_naming|UUID]] <br />
<br />
不同的启动项使用双引号引起来并用空格隔开.所以,对于即想使用resume又想使用systemd的用户来说,这个参数的设置可能是:<br />
{{ic|<nowiki>GRUB_CMDLINE_LINUX="resume=/dev/sdaX init=/usr/lib/systemd/systemd"</nowiki>}}<br />
更多信息请参考[[Kernel parameters]].<br />
<br />
=== 手动创建 grub.cfg ===<br />
<br />
{{Warning|''不推荐''编辑这个文件.这个文件由{{ic|grub-mkconfig}}生成,最好编辑{{ic|/etc/default/grub}}和{{ic|/etc/grub.d}}文件夹下的脚本以实现修改.}}<br />
<br />
基本的GRUB配置文件使用如下选项:<br />
* {{ic|(hd''X'',''Y'')}} 是''X''磁盘的''Y''分区,分区从1开始计数,磁盘从0开始计数.<br />
* {{ic|1=set default=''N''}}设定用户选择超时时间过后的默认启动项<br />
* {{ic|1=set timeout=''M''}}设定用户选择超时时间(秒).<br />
* {{ic|<nowiki>menuentry "title" {entry options}</nowiki>}}设置一个名为{{ic|title}}的启动项<br />
* {{ic|1=set root=(hd''X'',''Y'')}}设定启动分区(kernel和GRUB模组所在磁盘),/boot没被要求独占一个分区,有可能就是root分区下的一个文件夹<br />
示例配置如下:<br />
<br />
{{hc|/boot/grub/grub.cfg|<nowiki><br />
# Config file for GRUB - The GNU GRand Unified Bootloader<br />
# /boot/grub/grub.cfg<br />
<br />
# DEVICE NAME CONVERSIONS<br />
#<br />
# Linux Grub<br />
# -------------------------<br />
# /dev/fd0 (fd0)<br />
# /dev/sda (hd0)<br />
# /dev/sdb2 (hd1,2)<br />
# /dev/sda3 (hd0,3)<br />
#<br />
<br />
# Timeout for menu<br />
set timeout=5<br />
<br />
# Set default boot entry as Entry 0<br />
set default=0<br />
<br />
# (0) Arch Linux<br />
menuentry "Arch Linux" {<br />
set root=(hd0,1)<br />
linux /vmlinuz-linux root=/dev/sda3 ro<br />
initrd /initramfs-linux.img<br />
}<br />
<br />
## (1) Windows<br />
#menuentry "Windows" {<br />
# set root=(hd0,3)<br />
# chainloader +1<br />
#}<br />
</nowiki>}}<br />
<br />
=== 双启动 ===<br />
<br />
{{Note|如果你希望GRUB自动搜寻其他系统, 可以安装{{Pkg|os-prober}}.}}<br />
<br />
==== 使用/etc/grub.d/40_custom 和 grub-mkconfig自动生成 ====<br />
<br />
添加其他启动项的最好方法是编辑{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}.运行{{ic|grub-mkconfig}}后,这些文件中的启动项会被自动添加到grub.cfg中.<br />
更新了这些文件后,运行:<br />
{{bc|<nowiki># grub-mkconfig -o /boot/grub/grub.cfg</nowiki>}}<br />
在UEFI-GPT模式下,运行:<br />
{{bc|<nowiki># grub-mkconfig -o /boot/efi/EFI/GRUB/grub.cfg</nowiki>}}<br />
这样会更新{{ic|grub.cfg}}<br />
<br />
<br />
一个典型{{ic|/etc/grub.d/40_custom}}文件类似于下面这个为[http://h10025.www1.hp.com/ewfrf/wc/product?cc=us&destPage=product&lc=en&product=5402703&tmp_docname= HP Pavilion 15-e056sl Notebook PC|预装WIN8的HP Pavilion 15-e056sl Notebook PC]而作的配置.每个启动项的结构都应该和下面的类似.请注意UEFI分区{{ic|/dev/sda2}} 被命名为{{ic|hd0,gpt2}} 和{{ic|ahci0,gpt2}}(请参考[[GRUB#Windows_Installed_in_UEFI-GPT_Mode_menu_entry|here]]获取更多信息)<br />
<br />
'''/etc/grub.d/40_custom''':<br />
<br />
{{bc|<nowiki>#!/bin/sh<br />
exec tail -n +3 $0<br />
# This file provides an easy way to add custom menu entries.&nbsp; Simply type the<br />
# menu entries you want to add after this comment.&nbsp; Be careful not to change<br />
# the 'exec tail' line above.<br />
<br />
menuentry "HP / Microsoft Windows 8.1" {<br />
echo "Loading HP / Microsoft Windows 8.1..."<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 763A-9CB6<br />
chainloader (${root})/EFI/Microsoft/Boot/bootmgfw.efi<br />
}<br />
<br />
menuentry "HP / Microsoft Control Center" {<br />
echo "Loading HP / Microsoft Control Center..."<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 763A-9CB6<br />
chainloader (${root})/EFI/HP/boot/bootmgfw.efi<br />
}<br />
<br />
menuentry "System shutdown" {<br />
echo "System shutting down..."<br />
halt<br />
}<br />
<br />
menuentry "System restart" {<br />
echo "System rebooting..."<br />
reboot<br />
}</nowiki>}}<br />
<br />
===== GNU/Linux 启动项 =====<br />
<br />
假设另一个发行版位于{{ic|sda2}}:<br />
<br />
{{bc|<nowiki>menuentry "Other Linux" {<br />
set root=(hd0,2)<br />
linux /boot/vmlinuz (add other options here as required)<br />
initrd /boot/initrd.img (if the other kernel uses/needs one)<br />
}</nowiki>}}<br />
<br />
===== FreeBSD 启动项 =====<br />
<br />
要求FreeBSD以UFS格式被安装在单独的分区上,假设安装在{{ic|sda4}}上:<br />
<br />
{{bc|<nowiki>menuentry "FreeBSD" {<br />
set root=(hd0,4)<br />
chainloader +1<br />
}</nowiki>}}<br />
<br />
===== Windows XP 启动项=====<br />
<br />
假设Windows分区是{{ic|sda3}}.请记住需要将root设置到Windows安装时建立的启动分区上,然后链式加载它,而不是Windows系统所在分区.下面的例子就是假设启动分区就是{{ic|sda3}}.<br />
{{bc|<nowiki># (2) Windows XP<br />
menuentry "Windows XP" {<br />
set root="(hd0,3)"<br />
chainloader +1<br />
}</nowiki>}}<br />
<br />
如果Windows的bootloader和GRUB不在一个硬盘上,那么需要让Windows认为它是在第一硬盘上.使用{{ic|drivemap}}可以实现这点.假设GRUB在{{ic|hd0}}而windows在{{ic|hd2}}上,需要在设定root后执行:<br />
<br />
{{bc|<nowiki>drivemap -s hd0 hd2</nowiki>}}<br />
<br />
===== UEFI-GPT 模式下安装的Windows的启动项 =====<br />
<br />
{{bc|<nowiki>if [ "${grub_platform}" == "efi" ]; then<br />
menuentry "Microsoft Windows Vista/7/8 x86_64 UEFI-GPT" {<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root $hints_string $uuid<br />
chainloader /EFI/Microsoft/Boot/bootmgfw.efi<br />
}<br />
fi</nowiki>}}<br />
<br />
{{ic|$hints_string}} 和 {{ic|$uuid}}可以通过以下命令获取:<br />
<br />
{{ic|$uuid}}:<br />
<br />
{{bc|<nowiki># grub-probe --target=fs_uuid $esp/EFI/Microsoft/Boot/bootmgfw.efi<br />
1ce5-7f28</nowiki>}}<br />
<br />
{{ic|$hints_string}}:<br />
<br />
{{bc|<nowiki># grub-probe --target=hints_string $esp/EFI/Microsoft/Boot/bootmgfw.efi<br />
--hint-bios=hd0,gpt1 --hint-efi=hd0,gpt1 --hint-baremetal=ahci0,gpt1</nowiki>}}<br />
<br />
这两个命令都是假设ESP挂载在{{ic|$esp}}上.当然,Windows的EFI文件路径可能有变,因为这就是Windows....<br />
<br />
===== "Shutdown" 启动项 =====<br />
<br />
{{bc|<nowiki>menuentry "System shutdown" {<br />
echo "System shutting down..."<br />
halt<br />
}</nowiki>}}<br />
<br />
===== "Restart" 启动项 =====<br />
<br />
{{bc|<nowiki>menuentry "System restart" {<br />
echo "System rebooting..."<br />
reboot<br />
}</nowiki>}}<br />
<br />
==== 通过EasyBCD NeoGRUB 和Windows共存 ====<br />
<br />
现在EasyBCD的NeoGRUB还不能识别GRUB的目录格式,在 {{ic|C:\NST\menu.lst}} 中添加如下行以链式加载到GRUB:<br />
<br />
default 0<br />
timeout 1<br />
<br />
title Chainload into GRUB v2<br />
root (hd0,7)<br />
kernel /boot/grub/i386-pc/core.img<br />
<br />
最后,使用{{ic|grub-mkconfig}}重建{{ic|grub.cfg}}<br />
<br />
===可视化配置===<br />
<br />
GRUB默认就支持定制启动目录的外观.不过要确保使用合适的视频模式初始化GRUB的图形化终端gfxterm.在[[#"No suitable mode found" error]]一节中有介绍.GRUB通过'gfxpayload'来将视频模式传递给linux内核,所以任何可视化配置都需要这个模式的信息以正确工作.<br />
<br />
==== 设定帧缓冲的分辨率 ====<br />
<br />
<br />
GRUB既可以为自己,也可以为内核设定帧缓冲.现在已经不使用老的{{ic|1=vga=}}配置了.推荐方法是在{{ic|/etc/default/grub}}进行如下编辑:<br />
<br />
GRUB_GFXMODE=1024x768x32<br />
GRUB_GFXPAYLOAD_LINUX=keep<br />
<br />
运行以下命令使配置生效: <br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|gfxpayload}}属性会确保内核也保持这个分辨率<br />
<br />
<br />
{{Note|<br />
* 如果示例不起作用,请尝试用{{ic|1=vbemode="0x105"}}代替{{ic|1=gfxmode="1024x768x32"}}.请使用适合你屏幕的分辨率.<br />
* 可以通过{{ic|1=# hwinfo --framebuffer}}命令来显示所有可以使用的分辨率模式(hwinfo在[[community]]里),在GRUB命令行下,可以使用{{ic|1=vbeinfo}} 命令<br />
}}<br />
<br />
这种方法不管用的话,可以试试老的{{ic|1=vga=}}方法.将它添加到{{ic|1="GRUB_CMDLINE_LINUX_DEFAULT="}}里面就行了,比如<br />
{{ic|1="GRUB_CMDLINE_LINUX_DEFAULT="quiet splash vga=792"}} <br />
这会将系统的分辨率设定为1024*768<br />
<br />
可以选择以下分辨率中的一种:{{ic|640×480}}, {{ic|800×600}}, {{ic|1024×768}}, {{ic|1280×1024}}, {{ic|1600×1200}}, {{ic|1920×1200}}<br />
<br />
==== 915resolution hack ====<br />
<br />
有些时候,Intel显卡无法通过{{ic|1=# hwinfo --framebuffer}} 或{{ic|1=vbeinfo}}显示你需要的分辨率.这种情况下,你可以使用{{ic|915resolution hack}}.915resolution hack会临时性的修改显卡BIOS来添加所需的分辨率.详情请参考[http://915resolution.mango-lang.org/ 915resolution's home page] <br />
<br />
首先,找一个你想要替代的视频模式.例如在GRUB命令行模式下运行:<br />
{{hc|sh:grub> 915resolution -l|<br />
Intel 800/900 Series VBIOS Hack : version 0.5.3<br />
[...]<br />
'''Mode 30''' : 640x480, 8 bits/pixel<br />
[...]<br />
}}<br />
然后,使用{{ic|1440x900}} 分辨率覆盖{{ic|Mode 30}}<br />
{{hc|/etc/grub.d/00_header|<br />
[...]<br />
'''915resolution 30 1440 900 # Inserted line'''<br />
set gfxmode&#61;${GRUB_GFXMODE}<br />
[...]<br />
}}<br />
最后,设置{{ic|GRUB_GFXMODE}},重新生成GRUB配置文件,重启并测试是否生效:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
# reboot<br />
<br />
==== 背景图像和点阵字体 ====<br />
<br />
GRUB原生支持设置背景图像和点阵字体(以pf2格式).{{Pkg|grub}}包含unifont字体,名为{{ic|unicode.pf2}}.(也有可能只包含名为{{ic|ascii.pf2}}的ASCII字符字体)<br />
<br />
GRUB支持的图像格式有tga,png,jpeg.所支持的最大图像分辨率跟硬件有关.<br />
<br />
Make sure you have set up the proper [[#Setting the framebuffer resolution|framebuffer resolution]].<br />
请确保你已经设定了合适的[[#设定帧缓冲的分辨率|帧缓冲分辨率]]<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
GRUB_BACKGROUND="/boot/grub/myimage"<br />
#GRUB_THEME="/path/to/gfxtheme"<br />
GRUB_FONT="/path/to/font.pf2"<br />
<br />
{{Note|如果你将GRUB安装在单独的分区上, {{ic|/boot/grub/myimage}} 应该改为 {{ic|/grub/myimage}}.}}<br />
<br />
重新生成配置文件:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
如果成功的添加了背景图片,那么用户会在命令行中看到{{ic|"Found background image..."}}.<br />
如果没有看到{{ic|"Found background image..."}},图像信息就可能没有嵌入{{ic|grub.cfg}}中了.<br />
<br />
如果图像没有正确显示,执行如下检查:<br />
* 在{{ic|/etc/default/grub}}里,图像的路径和名字是否正确<br />
* 图像的大小和格式是否合适(tga,png,8-bit jpg)<br />
* 图像是不是以RGB模式存储,是不是没有索引<br />
* {{ic|/etc/default/grub}}里面是不是没有开启console模式<br />
* 是否执行{{ic|grub-mkconfig}}以重新生成配置文件<br />
<br />
==== 主题====<br />
<br />
下面的例子将展示如何使用GRUB包重的starfield主题:<br />
<br />
编辑 {{ic|/etc/default/grub}}<br />
GRUB_THEME="/usr/share/grub/themes/starfield/theme.txt"<br />
<br />
重生成配置:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
配置成功的话,在重生成配置过程中,会出现{{ic|Found theme: /usr/share/grub/themes/starfield/theme.txt}}.使用主题就不会使用之前的背景图像<br />
<br />
====目录颜色====<br />
<br />
GRUB支持设置目录颜色.可使用的颜色能从[https://www.gnu.org/software/grub/manual/html_node/Theme-file-format.html the GRUB Manual]里面找到.示例如下:<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
GRUB_COLOR_NORMAL="light-blue/black"<br />
GRUB_COLOR_HIGHLIGHT="light-cyan/blue"<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
====隐藏目录====<br />
<br />
GRUB特性之一就是支持隐藏/跳过目录,同时支持按{{ic|Esc}}来打断隐藏/跳过.同时还支持设置是否显示timeout计时器<br />
下面的例子设置5s钟内没有按Esc键就启动默认的启动项,并且在屏幕上显示倒计时:<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
<br />
GRUB_HIDDEN_TIMEOUT=5<br />
GRUB_HIDDEN_TIMEOUT_QUIET=false<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
==== 禁用framebuffer ====<br />
<br />
Users who use NVIDIA proprietary driver might wish to disable GRUB's framebuffer as it can cause problems with the binary driver.<br />
使用NVIDIA私有驱动的用户可能希望禁用GRUB的framebuffer,因为会导致驱动错误.<br />
<br />
在{{ic|/etc/default/grub}}中添加(如果已经有背注释掉的这行,就取消注释):<br />
GRUB_TERMINAL_OUTPUT=console<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
如果你想保留GRUB的framebuffer,解决方法是在GRUB载入内核前进入文字模式.可以通过在{{ic|/etc/default/grub}}设置如下:<br />
GRUB_GFXPAYLOAD_LINUX=text<br />
然后重建配置档.<br />
<br />
=== 其他选项 ===<br />
<br />
==== LVM ====<br />
<br />
如果使用[[LVM]]做为启动设备,那么在启动项里添加:<br />
<br />
insmod lvm<br />
<br />
然后在启动项里指定root:<br />
<br />
set root=lvm/''lvm_group_name''-''lvm_logical_boot_partition_name''<br />
<br />
示例如下:<br />
<br />
# (0) Arch Linux<br />
menuentry "Arch Linux" {<br />
insmod lvm<br />
set root=lvm/VolumeGroup-lv_boot<br />
# you can only set following two lines<br />
linux /vmlinuz-linux root=/dev/mapper/VolumeGroup-root ro<br />
initrd /initramfs-linux.img<br />
}<br />
<br />
==== 阵列====<br />
<br />
通过添加{{ic|insmod mdraid}},GRUB能够很方便的处理磁盘阵列.比如{{ic|/dev/md0}}:<br />
<br />
set root=(md0)<br />
<br />
阵列上的分区(比如{{ic|/dev/md0p1}})则为:<br />
<br />
set root=(md0,1)<br />
<br />
如果启动分区在raid1上,想要安装grub,只需要在两个磁盘上分别运行grub-install即可:<br />
<br />
grub-install --target=i386-pc --recheck --debug /dev/sda && grub-install --target=i386-pc --recheck --debug /dev/sdb<br />
<br />
这时/dev/sda和/dev/sdb就都有了raid1的启动文件夹/boot了<br />
<br />
==== 持久块设备命名法 ====<br />
<br />
[[Persistent block device naming|持久块设备命名法]](Persistent block device naming)的一个目的是使用全局的UUID来区分分区,而不是用老的{{ic|/dev/sd*}}表示法.好处显而易见.<br />
<br />
GRUB默认使用持久块设备命名法<br />
<br />
{{Note|每次文件系统调整过后,就需要用新的UUID来更新{{ic|/boot/grub.cfg}}.通过Live-CD调整分区和文件系统后要特别注意这点}}<br />
<br />
是否使用UUID由{{ic|/etc/default/grub}}里的这个选项控制:<br />
<br />
# GRUB_DISABLE_LINUX_UUID=true<br />
<br />
无论如何,变更配置后请重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
==== 使用卷标 ====<br />
<br />
GRUB支持以卷标识别文件系统(通过{{ic|search}}命令的{{ic|--label参数}}).<br />
<br />
首先,给文件系统设置一个卷标:<br />
<br />
# tune2fs -L ''LABEL'' ''PARTITION''<br />
<br />
然后在启动项中使用这个卷标:<br />
<br />
menuentry "Arch Linux, session texte" {<br />
search --label --set=root archroot<br />
linux /boot/vmlinuz-linux root=/dev/disk/by-label/archroot ro<br />
initrd /boot/initramfs-linux.img<br />
}<br />
<br />
====调用之前的启动项====<br />
<br />
GRUB能够记住你当前使用的启动项并且在下次启动时将其作为默认项.当你使用多个内核或操作系统时,这个特性很有用.<br />
编辑{{ic|/etc/default/grub}}中的{{ic|GRUB_DEFAULT}}选项:<br />
<br />
GRUB_DEFAULT=saved<br />
<br />
上面的命令会告诉GRUB使用记住的启动项为默认启动项.<br />
将下面的行添加到{{ic|/etc/default/grub}}会让GRUB记住当前的启动项:<br />
<br />
GRUB_SAVEDEFAULT=true<br />
<br />
{{Note|手动添加启动项到{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}中,比如Windows启动项,需要添加{{ic|savedefault}}}}<br />
请记住重建配置档.<br />
<br />
==== 改变默认启动项 ====<br />
<br />
可以通过修改{{ic|/etc/default/grub}}中的{{ic|GRUB_DEFAULT}}值来改变默认启动项<br />
<br />
GRUB_DEFAULT=0<br />
<br />
GRUB启动项序号从0开始计数.0代表第一个启动项.<br />
<br />
除了使用启动项序号,也可以使用启动项名:<br />
<br />
GRUB_DEFAULT='Arch Linux, with Linux core repo kernel'<br />
<br />
{{Note|请记住重建配置档}}<br />
<br />
==== 安全====<br />
<br />
如果你想禁止其他人改变启动参数或者使用GRUB命令行,可以给GRUB配置添加用户名/密码.<br />
运行 {{ic|grub-mkpasswd-pbkdf2}},输入密码:<br />
{{hc|grub-mkpasswd-pbkdf2|<br />
[...]<br />
Your PBKDF2 is grub.pbkdf2.sha512.10000.C8ABD3E93C4DFC83138B0C7A3D719BC650E6234310DA069E6FDB0DD4156313DA3D0D9BFFC2846C21D5A2DDA515114CF6378F8A064C94198D0618E70D23717E82.509BFA8A4217EAD0B33C87432524C0B6B64B34FBAD22D3E6E6874D9B101996C5F98AB1746FE7C7199147ECF4ABD8661C222EEEDB7D14A843261FFF2C07B1269A<br />
}}<br />
然后将下面的内容添加到{{ic|/etc/grub.d/00_header}}:<br />
{{hc|/etc/grub.d/00_header|<br />
cat << EOF<br />
<br />
set superusers<nowiki>=</nowiki>"'''username'''"<br />
password_pbkdf2 '''username''' '''<password>'''<br />
<br />
EOF<br />
}}<br />
{{Note|不能直接将<br />
set superusers<nowiki>=</nowiki>"'''username'''"<br />
password_pbkdf2 '''username''' '''<password>'''<br />
添加到/etc/grub.d/00_header中去,而必须使用上述方法.否则会报错<br />
password_pbkdf2: not found<br />
}}<br />
{{ic|<password>}}是{{ic|grub-mkpasswd_pbkdf2}}生成的那个加密过后密码.<br />
<br />
然后重建配置档.其他用户没有密码就不能变更GRUB配置或者使用GRUB命令行了.<br />
<br />
可以参考[https://www.gnu.org/software/grub/manual/grub.html#Security the GRUB manual]中的"Security"部分来进行更多的客制化安全设定<br />
<br />
==== root加密 ====<br />
<br />
将{{ic|1=cryptdevice=/dev/yourdevice:label}}添加到{{ic|/etc/default/grub}}中的{{ic|GRUB_CMDLINE_LINUX}}配置项,可以让GRUB传递参数给内核,让内核对root加密:<br />
<br />
{{Tip|如果你使用从GRUB Legacy升级而来,检查{{ic|/boot/grub/menu.lst.pacsave}}以获取正确的device/label. 在{{ic|kernel /vmlinuz-linux}}后去找label.}}<br />
<br />
将root映射到{{ic|/dev/mapper/root}}:<br />
<br />
GRUB_CMDLINE_LINUX="cryptdevice=/dev/sda2:root"<br />
<br />
当然,需要在rootfs上禁用UUID:<br />
<br />
GRUB_DISABLE_LINUX_UUID=true<br />
<br />
Regenerate the configuration.<br />
<br />
==== 设定下次启动的启动项(一次性,非持久) ====<br />
<br />
命令{{ic|grub-reboot}}可以设置下次启动时启动哪个启动项而不必修改配置文件或者在启动时手动选择.这个设置是一次性的,即不会改变GRUB的默认启动项.<br />
<br />
{{Note|需要在{{ic|/etc/default/grub}}中设定 {{ic|1=GRUB_DEFAULT=saved}},然后重建配置档.在手动生成的 {{ic|grub.cfg}}中, 使用 {{ic|1=set default="${saved_entry}"}}.}}<br />
<br />
==== 启动时隐藏GRUB界面,除非按着SHIFT键 ====<br />
<br />
为了获取更快的启动速度,而不用等GRUB倒计时,可以命令GRUB在启动时隐藏目录,除非SHIFT被按着.<br />
将如下行添加到{{ic|/etc/default/grub}}:<br />
<br />
GRUB_FORCE_HIDDEN_MENU="true"<br />
<br />
然后创建如下文件:<br />
<br />
{{hc|/etc/grub.d/31_hold_shift|<nowiki><br />
#! /bin/sh<br />
set -e<br />
<br />
# grub-mkconfig helper script.<br />
# Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.<br />
#<br />
# GRUB is free software: you can redistribute it and/or modify<br />
# it under the terms of the GNU General Public License as published by<br />
# the Free Software Foundation, either version 3 of the License, or<br />
# (at your option) any later version.<br />
#<br />
# GRUB is distributed in the hope that it will be useful,<br />
# but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />
# GNU General Public License for more details.<br />
#<br />
# You should have received a copy of the GNU General Public License<br />
# along with GRUB. If not, see <http://www.gnu.org/licenses/>.<br />
<br />
prefix="/usr"<br />
exec_prefix="${prefix}"<br />
datarootdir="${prefix}/share"<br />
<br />
export TEXTDOMAIN=grub<br />
export TEXTDOMAINDIR="${datarootdir}/locale"<br />
source "${datarootdir}/grub/grub-mkconfig_lib"<br />
<br />
found_other_os=<br />
<br />
make_timeout () {<br />
<br />
if [ "x${GRUB_FORCE_HIDDEN_MENU}" = "xtrue" ] ; then <br />
if [ "x${1}" != "x" ] ; then<br />
if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then<br />
verbose=<br />
else<br />
verbose=" --verbose"<br />
fi<br />
<br />
if [ "x${1}" = "x0" ] ; then<br />
cat <<EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if keystatus; then<br />
if keystatus --shift; then<br />
set timeout=-1<br />
else<br />
set timeout=0<br />
fi<br />
else<br />
if sleep$verbose --interruptible 3 ; then<br />
set timeout=0<br />
fi<br />
fi<br />
fi<br />
EOF<br />
else<br />
cat << EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if sleep$verbose --interruptible ${GRUB_HIDDEN_TIMEOUT} ; then<br />
set timeout=0<br />
fi<br />
fi<br />
EOF<br />
fi<br />
fi<br />
fi<br />
}<br />
<br />
adjust_timeout () {<br />
if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then<br />
cat <<EOF<br />
if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then<br />
EOF<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_BUTTON}"<br />
echo else<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}"<br />
echo fi<br />
else<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}"<br />
fi<br />
}<br />
<br />
adjust_timeout<br />
<br />
cat <<EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if keystatus; then<br />
if keystatus --shift; then<br />
set timeout=-1<br />
else<br />
set timeout=0<br />
fi<br />
else<br />
if sleep$verbose --interruptible 3 ; then<br />
set timeout=0<br />
fi<br />
fi<br />
fi<br />
EOF<br />
</nowiki>}}<br />
<br />
===在GRUB中直接从ISO启动===<br />
<br />
编辑{{ic|/etc/grub.d/40_custom}} 或 {{ic|/boot/grub/custom.cfg}},给目标ISO添加一个启动项.然后使用{{ic|grub-mkconfig -o /boot/grub/grub.cfg}}更新配置档<br />
<br />
==== Arch ISO ====<br />
<br />
{{Note|下面的例子都是假设ISO文件位于{{ic|hd0,6}}分区上的{{ic|/archives}}文件夹里}}<br />
{{Tip|For thumbdrives, use something like {{ic|(hd1,$partition)}} and either {{ic|/dev/sdb'''Y'''}} for the {{ic|img_dev}} parameter or [[Persistent block device naming|a persistent name]], e.g. {{ic|img_dev&#61;/dev/disk/by-label/CORSAIR}}.}}<br />
{{Tip|对于闪存,使用{{ic|(hd1,$partition)}}或{{ic|/dev/sdb'''Y'''}}作为{{ic|img_dev}}参数的值,或者使用持久块设备命名法命名的设备,比如{{ic|img_dev&#61;/dev/disk/by-label/CORSAIR}}.}}<br />
===== x86_64 =====<br />
<br />
menuentry "Archlinux-2013.05.01-dual.iso" --class iso {<br />
set isofile="/archives/archlinux-2013.05.01-dual.iso"<br />
set partition="6"<br />
loopback loop (hd0,$partition)/$isofile<br />
linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop<br />
initrd (loop)/arch/boot/x86_64/archiso.img<br />
}<br />
<br />
===== i686 =====<br />
<br />
menuentry "Archlinux-2013.05.01-dual.iso" --class iso {<br />
set isofile="/archives/archlinux-2013.05.01-dual.iso"<br />
set partition="6"<br />
loopback loop (hd0,$partition)/$isofile<br />
linux (loop)/arch/boot/i686/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop<br />
initrd (loop)/arch/boot/i686/archiso.img<br />
}<br />
<br />
==== Ubuntu ISO ====<br />
<br />
{{Note|下面的例子都是假设ISO文件位于{{ic|hd0,6}}分区上的{{ic|/archives}}文件夹里. 用户需要根据自己系统的实际情况调整.}}<br />
<br />
menuentry "ubuntu-13.04-desktop-amd64.iso" {<br />
set isofile="/archives/ubuntu-13.04-desktop-amd64.iso"<br />
loopback loop (hd0,6)/$isofile<br />
linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash --<br />
initrd (loop)/casper/initrd.lz<br />
}<br />
<br />
menuentry "ubuntu-12.04-desktop-amd64.iso" {<br />
set isofile="/archives/ubuntu-12.04-desktop-amd64.iso"<br />
loopback loop (hd0,6)/$isofile<br />
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash --<br />
initrd (loop)/casper/initrd.lz<br />
}<br />
<br />
==== Other ISOs ====<br />
<br />
可从[http://askubuntu.com/questions/141940/how-to-boot-live-iso-images 这里]获取其他ISO的配置.<br />
<br />
== 使用GRUB命令行 ==<br />
<br />
MBR太小,所以不足以存储所有的GRUB模组.MBR里面只有启动目录配置和一些很基本的命令.GRUB的主要功能通过{{ic|/boot/grub}}里的模组实现,而且可以按需加载.出现错误时,GRUB可能不能引导启动(比如,磁盘分区发生了变化).这时候,一般会出现命令行界面.<br />
<br />
GRUB不止提供一个shell.如果GRUB不能读取到启动目录配置,但是能找到磁盘,你很可能需要进入"normal" shell:<br />
sh:grub><br />
如果有更严重的问题(比如,GRUB找不到必须的文件了),你就可能需要进入"rescue" shell:<br />
grub rescue><br />
<br />
"rescue" shell是"normal" shell的一个子集,其支持的功能更少.如果不幸进入了"rescue" shell里,首先记载"normal"模组,然后启动"normal" shell:<br />
grub rescue> set prefix=(hdX,Y)/boot/grub<br />
grub rescue> insmod (hdX,Y)/boot/grub/i386-pc/normal.mod<br />
rescue:grub> normal<br />
<br />
=== 分页支持 ===<br />
<br />
GRUB支持对长输出进行分页(比如运行{{ic|help}}的输出).不过只有normal shell支持分页,而rescue shell不支持.开启分页支持的方法如下:<br />
<br />
sh:grub> set pager=1<br />
<br />
===使用命令行引导操作系统 ===<br />
<br />
grub> <br />
<br />
可以使用GRUB命令行引导操作系统,一个典型的应用场景是通过'''chainloading'''来引导另一个Windows或Linux<br />
<br />
''ChainLoading''的意思是用当前的bootloader去载入另一个bootloader,所以叫做'''链式'''加载.这个bootloader可能位于MBR,也可能在另一个分区的引导扇区上.<br />
<br />
==== 链式加载一个分区 ====<br />
<br />
set root=(hdX,Y)<br />
chainloader +1<br />
boot<br />
<br />
X=0,1,2...<br />
Y=1,2,3...<br />
<br />
比如链式加载一个位于首磁盘,首分区上的Windows:<br />
<br />
set root=(hd0,1)<br />
chainloader +1<br />
boot<br />
<br />
也可以使用GRUB链式加载另一个分区引导扇区上的GRUB.<br />
<br />
==== 链式加载磁盘====<br />
<br />
set root=hdX<br />
chainloader +1<br />
boot<br />
<br />
====正常载入 ====<br />
<br />
请参考[[#使用应急命令行]]<br />
<br />
==图形化配置工具==<br />
<br />
* {{App|grub-customizer|定制bootloader(GRUB or BURG)|https://launchpad.net/grub-customizer|{{AUR|grub-customizer}}}}<br />
* {{App|grub2-editor|KDE4上配置GRUB的控制模组|http://kde-apps.org/content/show.php?content&#61;139643|{{AUR|grub2-editor}}{{Broken package link|{{aur-mirror|grub2-editor}}}}}}<br />
* {{App|kcm-grub2|可以管理大部分常用GRUB配置的kcm模组|http://kde-apps.org/content/show.php?content&#61;137886|{{AUR|kcm-grub2}}{{Broken package link|{{aur-mirror|kcm-grub2}}}}}}<br />
* {{App|startupmanager|GRUB Legacy, GRUB, Usplash and Splashy的图形化配置工具([https://launchpad.net/startup-manager/+announcement/8300 abandonned])|http://sourceforge.net/projects/startup-manager/|{{AUR|startupmanager}}}}<br />
<br />
== parttool ==<br />
<br />
如果你安装了Windows 9x系列操作系统,而它们隐藏了磁盘(比如C盘),GRUB能够使用parttool来设置是否隐藏.比如,想从三个Windows 9x系统的第三个启动,可以这样:<br />
parttool hd0,1 hidden+ boot-<br />
parttool hd0,2 hidden+ boot-<br />
parttool hd0,3 hidden- boot+<br />
set root=hd0,3<br />
chainloader +1<br />
boot<br />
<br />
==使用应急命令行==<br />
<br />
请先阅读[[#使用GRUB命令行]].如果无法进入命令行,请尝试使用Live CD或者其他rescue磁盘引导,然后修正错误.不过有些时候我们手上没有此类rescue磁盘,这时,应急命令行(rescue console)就可以派上用场了.<br />
<br />
GRUB应急命令行里可用的命令有{{ic|insmod}}, {{ic|ls}}, {{ic|set}}, {{ic|unset}}.可以使用set/unset修改变量,使用insmod来载入模组.<br />
<br />
首先,用户必须知道启动分区({{ic|/boot}}所在位置然后设置:<br />
<br />
grub rescue> set prefix=(hdX,Y)/boot/grub<br />
<br />
通过加载{{ic|linux}}模组来扩展命令行的功能:<br />
<br />
grub rescue> insmod (hdX,Y)/boot/grub/linux.mod<br />
<br />
{{Note|如果/boot是在单独的分区上,请从路径中移除/boot.(即. 输入 {{ic|1=set prefix=(hdX,Y)/grub}} 然后{{ic|insmod (hdX,Y)/grub/linux.mod}})}}<br />
<br />
这个模组会启动对我们熟悉的{{ic|linux}} 和 {{ic|initrd}} 命令的支持 (请参考[[#配置]]).<br />
<br />
比如:<br />
set root=(hd0,5)<br />
linux /boot/vmlinuz-linux root=/dev/sda5<br />
initrd /boot/initramfs-linux.img<br />
boot<br />
<br />
如果/boot在单独分区上:<br />
set root=(hd0,5)<br />
linux /vmlinuz-linux root=/dev/sda6<br />
initrd /initramfs-linux.img<br />
boot<br />
<br />
成功启动Arch后,用户可以修正配置的错误或者重新安装GRUB.<br />
<br />
关于修正配置和重新安装GRUB,请参考[[#配置]]和[[#安装]]章节.<br />
==使用UUID的基础脚本 ==<br />
<br />
如果你想要使用UUID来避免不可靠的BIOS设备命名或者正在研究GRUB语法,这里有个使用UUID的示例性的启动项配置脚本.如果你想要将其移植到自己的系统上,只需要修改UUID就行了.这个例子假设系统的boot和root文件系统是在不同的分区上.如果你还有其他分区,请做相应修改.<br />
<br />
menuentry "Arch Linux 64" {<br />
# Set the UUIDs for your boot and root partition respectively<br />
set the_boot_uuid=ece0448f-bb08-486d-9864-ac3271bd8d07<br />
set the_root_uuid=c55da16f-e2af-4603-9e0b-03f5f565ec4a<br />
<br />
# (Note: This may be the same as your boot partition)<br />
<br />
# Get the boot/root devices and set them in the root and grub_boot variables<br />
search --fs-uuid $the_root_uuid --set=root<br />
search --fs-uuid $the_boot_uuid --set=grub_boot<br />
<br />
# Check to see if boot and root are equal.<br />
# If they are, then append /boot to $grub_boot (Since $grub_boot is actually the root partition)<br />
if [ $the_boot_uuid == $the_root_uuid ] ; then<br />
set grub_boot=($grub_boot)/boot<br />
else<br />
set grub_boot=($grub_boot)<br />
fi<br />
<br />
# $grub_boot now points to the correct location, so the following will properly find the kernel and initrd<br />
linux $grub_boot/vmlinuz-linux root=/dev/disk/by-uuid/$the_root_uuid ro<br />
initrd $grub_boot/initramfs-linux.img<br />
}<br />
== 异常处理==<br />
<br />
=== Intel BIOS不能引导GPT ===<br />
<br />
一些Intel的BIOS要求至少要一个可启动的分区(MBR方案下的),导致GTP方案下的分区GRUB无法启动.<br />
<br />
可以通过fdisk来将一个GPT分区标志为'boot'((最好就设在你为GRUB创建的那个1007KiB分区上)),这样就可以绕过这个问题了:<br />
<br />
1.对目标磁盘(比如/dev/sda)运行fdisk<br />
2.按{{ic|a}}键,然后选择想要设置'boot'标志的分区<br />
3.最后按{{ic|w}}键,将变更写入磁盘<br />
<br />
<br />
{{Note|必须使用fdisk或者类似于它的工具来设置'boot flag',不能用Gparted等,因为它们不会在MBR里面设置'boot flag'.}}<br />
<br />
请参考 [http://www.rodsbooks.com/gdisk/bios.html| http://www.rodsbooks.com/gdisk/bios.html]<br />
<br />
=== 启用调试信息 ===<br />
<br />
在{{ic|grub.cfg}}里添加:<br />
<br />
set pager=1<br />
set debug=all<br />
<br />
=== "No suitable mode found" error ===<br />
<br />
启动时可能出现如下提示信息:<br />
<br />
error: no suitable mode found<br />
Booting however<br />
<br />
然后你需要以合适的视频模式({{ic|gfxmode}})启动 GRUB 图形化终端({{ic|gfxterm}}).视频模式由GRUB通过'gfxpayload'变量传递给linux内核.在UEFI系统下,如果没有初始化视频模式的值,终端上就不会显示内核启动消息(至少直到KMS开始运行)<br />
<br />
将{{ic|/usr/share/grub/unicode.pf2}}复制到 ${GRUB_PREFIX_DIR}({{ic|/boot/grub/}} .如果GRUB UEFI安装时设定了{{ic|1=--boot-directory=$esp/EFI}},那么复制的目的文件夹是{{ic|$esp/EFI/grub/}}:<br />
<br />
# cp /usr/share/grub/unicode.pf2 ${GRUB_PREFIX_DIR}<br />
<br />
如果{{ic|/usr/share/grub/unicode.pf2}} 不存在, 安装{{Pkg|bdf-unifont}}, 创建 {{ic|unifont.pf2}} 文件然后将其复制到 {{ic|${GRUB_PREFIX_DIR<nowiki>}</nowiki>}}:<br />
<br />
# grub-mkfont -o unicode.pf2 /usr/share/fonts/misc/unifont.bdf<br />
<br />
然后,在{{ic|grub.cfg}}文件中添加如下行:<br />
(会给linux内核传递合适的视频模式,否则你只会得到一个黑屏,虽然系统还是会启动成功)<br />
BIOS 系统:<br />
<br />
insmod vbe<br />
<br />
UEFI 系统:<br />
<br />
insmod efi_gop<br />
insmod efi_uga<br />
<br />
在这些行后添加(BIOS&&UEFI):<br />
<br />
insmod font<br />
<br />
if loadfont ${prefix}/fonts/unicode.pf2<br />
then<br />
insmod gfxterm<br />
set gfxmode=auto<br />
set gfxpayload=keep<br />
terminal_output gfxterm<br />
fi<br />
<br />
如果要gfxterm(图形化终端)工作正常,{{ic|${GRUB_PREFIX_DIR<nowiki>}</nowiki>}}文件夹里面应该要有{{ic|unicode.pf2}} .<br />
<br />
=== 出现"msdos-style"错误消息 ===<br />
<br />
以下错误可能出现在你将GRUB安装到VMware上时.请阅读[https://bbs.archlinux.org/viewtopic.php?pid=581760#p581760 相关链接].这种情况是因为首分区直接从MBR后开始(即第64个扇区),而不是和正常的那样有1到2M post-MBR gap.请参阅[[#MBR专用指令]]<br />
<br />
grub-setup: warn: This msdos-style partition label has no post-MBR gap; embedding will not be possible!<br />
grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.<br />
However, blocklists are UNRELIABLE and its use is discouraged.<br />
grub-setup: error: If you really want blocklists, use --force.<br />
<br />
=== GRUB UEFI 启动到了rescue shell下 ===<br />
<br />
如果GRUB直接就启动到了rescue shell下,而且没报错,这可能是因为{{ic|grub.cfg}}丢失或者位置不对.如果GRUB UEFI 安装时设定了{{ic|--boot-directory}}参数,而{{ic|grub.cfg}}文件丢失,会出现这个问题.如果启动分区的分区号发生了变化(这个分区号会被直接编码到{{ic|grubx64.efi}}文件中),也会出现这个问题.<br />
<br />
=== GRUB UEFI 无法被载入 ===<br />
<br />
下面是一个EFI启动项信息示例:<br />
{{hc|# efibootmgr -v|<br />
BootCurrent: 0000<br />
Timeout: 3 seconds<br />
BootOrder: 0000,0001,0002<br />
Boot0000* Grub HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\efi\grub\grub.efi)<br />
Boot0001* Shell HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\EfiShell.efi)<br />
Boot0002* Festplatte BIOS(2,0,00)P0: SAMSUNG HD204UI<br />
}}<br />
<br />
如果启动后,屏幕直接变黑,几秒后就跳到了下一个启动项,根据[https://bbs.archlinux.org/viewtopic.php?pid=981560#p981560 相关链接]的说法是,将GRUB移动到root分区上可能会解决这个问题.必须先删除启动项,然后重建,变化如下:<br />
Boot0000* Grub HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\grub.efi)<br />
<br />
=== "Invalid signature"错误 ===<br />
<br />
如果在启动Windows时出现了"invalid signature"错误(比如在重新分区或者添加了新硬盘后),删除GRUB的磁盘mapping,然后重建:<br />
<br />
# mv /boot/grub/device.map /boot/grub/device.map-old<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|grub-mkconfig}}此时就应该生成了新的启动项了,包括Windows.确认能启动成功后,再将备份文件{{ic|/boot/grub/device.map-old}}删除.<br />
<br />
=== 引导过程卡死 ===<br />
<br />
如果在GRUB载入内核并初始化ramdisk后引导过程卡死了,请尝试移除{{ic|add_efi_memmap}}这个内核参数<br />
<br />
=== 回滚到 GRUB Legacy ===<br />
<br />
* 将GRUB2相关文件改名以为备份:<br />
<br />
# mv /boot/grub /boot/grub.nonfunctional<br />
<br />
* 将GRUB Legacy 移回{{ic|/boot}}:<br />
<br />
# cp -af /boot/grub-legacy /boot/grub<br />
<br />
* 恢复备份的MBR<br />
<br />
{{Warning|这个命令同时会恢复分区表,所以请要非常注意.}}<br />
<br />
# dd if=/path/to/backup/first-sectors of=/dev/sdX bs=512 count=1<br />
<br />
安全的方法是只恢复MBR中的启动代码:<br />
<br />
# dd if=/path/to/backup/mbr-boot-code of=/dev/sdX bs=446 count=1<br />
<br />
=== 其他系统不能自动发现Arch Linux===<br />
<br />
有人发现有些发行版不能使用{{ic|os-prober}}自动发现Arch Linux.据称先使用{{ic|/etc/lsb-release}}可能会解决这个问题.相关文件和工具可以在 [[official repositories|官方仓库]]的{{Pkg|lsb-release}}包中找到<br />
<br />
== 参阅 ==<br />
<br />
* 官方GRUB说明 - https://www.gnu.org/software/grub/manual/grub.html<br />
* Ubuntu GRUB Wiki - https://help.ubuntu.com/community/Grub2<br />
* 在UEFI系统上编译GRUB步骤描述- https://help.ubuntu.com/community/UEFIBooting<br />
* 维基百科[[Wikipedia:BIOS Boot partition|BIOS启动分区]]<br />
* 配置GRUB的完整说明 - http://members.iinet.net/~herman546/p20/GRUB2%20Configuration%20File%20Commands.html</div>
残月舞流殇
https://wiki.archlinux.org/index.php?title=GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=393150
GRUB (简体中文)
2015-08-22T11:29:28Z
<p>残月舞流殇: just a little mistakes</p>
<hr />
<div>[[Category:Boot loaders (简体中文)]]<br />
[[ar:GRUB]]<br />
[[cs:GRUB]]<br />
[[de:GRUB]]<br />
[[el:GRUB]]<br />
[[en:GRUB]]<br />
[[es:GRUB]]<br />
[[fr:GRUB]]<br />
[[he:GRUB]]<br />
[[id:GRUB2]]<br />
[[it:GRUB2]]<br />
[[ja:GRUB]]<br />
[[nl:GRUB]]<br />
[[ru:GRUB]]<br />
[[tr:GRUB2]]<br />
[[uk:GRUB]]<br />
[[zh-TW:GRUB2]]<br />
{{TranslationStatus (简体中文)|GRUB|2013-11-15|282734}}<br />
{{Related articles start (简体中文)}}<br />
{{Related|Burg}}<br />
{{Related|GRUB Legacy}}<br />
{{Related|GRUB EFI Examples}}<br />
{{Related articles end}}<br />
[http://www.gnu.org/software/grub/ GRUB2] 是下一代 GRand Unified Bootloader (GRUB,请不要和Grub Legacy混淆了)。 它来自下一代 GRUB 研究项目 [http://www.nongnu.org/pupa/ PUPA],代码全部重写,实现了模块化和增强了移植性。[http://www.gnu.org/software/grub/grub-faq.en.html#q1].<br />
<br />
简单的说,'''Boot Loader'''是电脑启动时运行的第一个程序,它负责装载内核并将控制权转交。内核再初始化操作系统的其它部分。<br />
<br />
==前言==<br />
* 引导程序是计算机启动时第一个运行的程序。它负责加载并将控制权转移到Linux内核。内核作为回报,将初始化操作系统剩余部分<br />
* 官方所称的GRUB代表的是本软件的第二版,即GRUB2,请参考[https://www.gnu.org/software/grub/].如果你是在找有关Grub Legacy的文章,请参考[[GRUB Legacy]].<br />
* GRUB2支持由zlib或者LZO压缩过的[[Btrfs]]格式的根目录,如果使用Btrfs,不需要单独的{{ic|/boot}}分区.<br />
* GRUB2 不支持[[F2fs]]格式的根目录,所以你需要为{{ic|/boot}}分区单独设置一个支持的文件系统.<br />
<br />
== BIOS 系统==<br />
=== GUID分区表(GPT)具体说明 ===<br />
BIOS/[[GPT]]配置中,一个 [http://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html BIOS启动分区]是必需的。GRUB将{{ic|core.img}}嵌入到这个分区。<br />
{{Note|<br />
* 在尝试分区之前请记住不是所有的系统都能够支持这种分区方案, 关于更多请阅读[[GUID Partition Table#BIOS systems|GUID分区表]].<br />
* This additional partition is only needed on a GRUB, BIOS/GPT partitioning scheme. Previously, for a GRUB, BIOS/MBR partitioning scheme, GRUB used the Post-MBR gap for the embedding the {{ic|core.img}}). GRUB for GPT, however, does not use the Post-GPT gap to conform to GPT specifications that require 1_megabyte/2048_sector disk boundaries.<br />
* For [[UEFI]] systems this extra partition is not required as no embedding of boot sectors takes place in that case.<br />
}}<br />
Create a mebibyte partition ({{ic|1=+1M}} with {{ic|fdisk}} or {{ic|gdisk}}) on the disk with no file system and type BIOS boot (''BIOS boot'' in fdisk, {{ic|ef02}} in gdisk, {{ic|bios_grub}} in {{ic|parted}}). This partition can be in any position order but has to be on the first 2 TiB of the disk. This partition needs to be created before GRUB installation. When the partition is ready, install the bootloader as per the instructions below.<br />
The post-GPT gap can also be used as the BIOS boot partition though it will be out of GPT alignment specification. Since the partition will not be regularly accessed performance issues can be disregarded (though some disk utilities will display a warning about it). In {{ic|fdisk}} or {{ic|gdisk}} create a new partition starting at sector 34 and spanning to 2047 and set the type. To have the viewable partitions begin at the base consider adding this partition last.<br />
<br />
=== 主引导记录(MBR)具体说明 ===<br />
<br />
一般来说,如果使用兼容DOS的分区对齐模式,MBR后面空间(post-MBR gap)的大小都是31KiB(MBR和第一个分区之间的空间).不过,为了提供足够的空间嵌入GRUB的{{ic|core.img}}文件({{bug|24103}}),建议将这个空间设置到1到2Mib.最好使用支持1MiB分区对齐的分区软件来分区,因为这样也能满足非512B扇区磁盘分区的需求.<br />
<br />
=== 安装 ===<br />
<br />
可以使用[[pacman|pacman]]从[[official repositories|官方仓库]]安装{{Pkg|grub}}包.安装完成后它会代替{{AUR|grub-legacy}}.<br />
<br />
{{Note|简单的安装Grub包并不会更新{{ic|/boot/grub/i386-pc/core.img}}和{{ic|/boot/grub/i386-pc}}里的GRUB模组.需要使用下面介绍的{{ic|grub-install}}来手动更新它们.}}<br />
<br />
==== 安装Boot文件 ====<br />
<br />
有三种方式安装GRUB Boot文件:<br />
<br />
* [[#安装到磁盘上|安装到磁盘上]] (推荐方式)<br />
* [[#安装到U盘|安装到U盘]] (用于恢复)<br />
* [[#安装到分区上或者无分区磁盘上|安装到分区或者无分区磁盘上]] (不推荐)<br />
* [[#只生成core.img|只生成core.img文件]] (最安全的方法, 但是需要另外的bootloader,比如[[Syslinux]]来进行链式加载{{ic|/boot/grub/i386-pc/core.img}})<br />
<br />
{{Note|请参考 http://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html 获取更详尽的资料}}<br />
<br />
===== 安装到磁盘上 =====<br />
<br />
{{Note|这种方法只限于将GRUB安装到可分区磁盘(MBR或GPT),GRUB 文件会被安装到{{ic|/boot/grub}},1st stage code会被安装到MBR的启动代码区域(以便和MBR分区表分开).对于无分区磁盘,请参考[[#安装到分区上或者无分区磁盘上]]}}<br />
<br />
以下命令会将{{ic|grub}}安装到MBR的启动代码区域,填充{{ic|/boot/grub}}文件夹,生成{{ic|/boot/grub/i386-pc/core.img}},将其嵌入post-MBR gap或者放入BIOS boot partition中(分别对应MBR分区表和GPT分区表):<br />
# grub-install --target=i386-pc --recheck --debug /dev/sda<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
如果你使用[[LVM]]来进行启动,你可以将GRUB安装在多个物理磁盘上.<br />
执行{{ic|grub-install}}并不会生成GRUB配置文件,请移至[[#生成GRUB配置文件]]一节<br />
<br />
===== 安装到U盘 =====<br />
<br />
假设你的U盘第一个分区是FAT32,其分区是/dev/sdy1<br />
<br />
# mkdir -p /mnt/usb ; mount /dev/sdy1 /mnt/usb<br />
# grub-install --target=i386-pc --recheck --debug --boot-directory=/mnt/usb/boot /dev/sdy<br />
# grub-mkconfig -o /mnt/usb/boot/grub/grub.cfg<br />
<br />
# optional, backup config files of grub.cfg<br />
# mkdir -p /mnt/usb/etc/default<br />
# cp /etc/default/grub /mnt/usb/etc/default<br />
# cp -a /etc/grub.d /mnt/usb/etc<br />
<br />
# sync ; umount /mnt/usb<br />
<br />
===== 安装到分区上或者无分区磁盘上 =====<br />
<br />
{{Note|GRUB并不推荐将其安装到分区启动扇区或者无分区磁盘上(Grub Legacy和syslinux相反).这种安装方式不安全,当升级时可能会损坏.Arch开发人员也不支持这种方式}}<br />
<br />
下面的命令将会将GRUB2安装到分区扇区或者无分区磁盘上(比如闪存上):<br />
<br />
# chattr -i /boot/grub/i386-pc/core.img<br />
# grub-install --target=i386-pc --recheck --debug --force /dev/sdaX<br />
# chattr +i /boot/grub/i386-pc/core.img<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
<br />
<br />
必须使用{{ic|--force}}选项来启用对blocklists(块列表)的支持,不应使用{{ic|1=--grub-setup=/bin/true}}(这个选项的效果类似于只生成{{ic|core.img}})<br />
{{ic|grub-install}} will give out warnings like which should give you the idea of what might go wrong with this approach:<br />
{{ic|grub-install}}会生成以下警告,你可以通过这些警告判断发生了什么问题:<br />
/sbin/grub-setup: warn: Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea.<br />
/sbin/grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists. <br />
However, blocklists are UNRELIABLE and their use is discouraged.<br />
<br />
如果不指定{{ic|--force}}选项,会出现以下错误,并且不会将启动代码安装到启动扇区上: <br />
/sbin/grub-setup: error: will not proceed with blocklists<br />
<br />
而指定了{{ic|--force}},会出现:<br />
<br />
Installation finished. No error reported.<br />
<br />
{{ic|grub-setup}}不默认允许这种情况的原因是,在分区或者无分区磁盘上,{{ic|grub}}依赖于嵌入分区引导扇区的块列表(blocklists)来定位{{ic|/boot/grub/i386-pc/core.img}}和{{ic|/boot/grub}}.而{{ic|core.img}}在分区上的扇区位置很有可能随着分区文件系统的更改而变化(复制文件,删除文件等).详情请参考https://bugzilla.redhat.com/show_bug.cgi?id=728742 和 https://bugzilla.redhat.com/show_bug.cgi?id=730915.<br />
<br />
临时解决方案是给{{ic|/boot/grub/i386-pc/core.img}}文件加"不可变"(immutable)标志.只有当将{{ic|grub}}安装到分区启动扇区或者无分区磁盘上时才需要给core.img加"不可变"标志.<br />
执行{{ic|grub-install}}并不会生成GRUB配置文件,请移至[[#生成GRUB配置文件]]一节<br />
<br />
===== 只生成core.img =====<br />
<br />
通过添加{{ic|1=--grub-setup=/bin/true}}选项,{{ic|grub-install}}命令会填充{{ic|/boot/grub}}文件夹并生成{{ic|/boot/grub/i386-pc/core.img}},但是不会将grub启动引导代码嵌入到MBR,post-MBR gap和分区引导扇区中:<br />
<br />
# grub-install --target=i386-pc --grub-setup=/bin/true --recheck --debug /dev/sda<br />
<br />
{{Note|<br />
* {{ic|/dev/sda}} 只是示例.<br />
* {{ic|1=--target=i386-pc}}指示{{ic|grub-install}}是为使用BIOS的系统安装. 推荐一直标明这点以防混淆.<br />
}}<br />
生成后,Grub Legacy或者syslinux就可以通过链式加载GRUB2的{{ic|core.img}}来间接加载Linux内核或者多启动内核了.<br />
<br />
==== 生成GRUB配置文件 ====<br />
<br />
最后,生成GRUB2所需的配置文件(在配置章节会详述):<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{Note|文件路径是{{ic|/boot/grub/grub.cfg}}, 而不是{{ic|/boot/grub/i386-pc/grub.cfg}}.}}<br />
<br />
如果GRUB在启动时报错"no suitable mode found",请参考[[#"No suitable mode found" error]].<br />
<br />
如果{{ic|grub-mkconfig}}执行失败,可以通过如下方式将{{ic|/boot/grub/menu.lst}}文件转化为{{ic|/boot/grub/grub.cfg}}<br />
{{Note|这种方法只适合BIOS,不适合UEFI}}<br />
# grub-menulst2cfg /boot/grub/menu.lst /boot/grub/grub.cfg<br />
<br />
示例如下:<br />
{{hc|/boot/grub/menu.lst|<nowiki><br />
default=0<br />
timeout=5<br />
<br />
title Arch Linux Stock Kernel<br />
root (hd0,0)<br />
kernel /vmlinuz-linux root=/dev/sda2 ro<br />
initrd /initramfs-linux.img<br />
<br />
title Arch Linux Stock Kernel Fallback<br />
root (hd0,0)<br />
kernel /vmlinuz-linux root=/dev/sda2 ro<br />
initrd /initramfs-linux-fallback.img<br />
</nowiki>}}<br />
<br />
{{hc|/boot/grub/grub.cfg|<nowiki><br />
set default='0'; if [ x"$default" = xsaved ]; then load_env; set default="$saved_entry"; fi<br />
set timeout=5<br />
<br />
menuentry 'Arch Linux Stock Kernel' {<br />
set root='(hd0,1)'; set legacy_hdbias='0'<br />
legacy_kernel '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'<br />
legacy_initrd '/initramfs-linux.img' '/initramfs-linux.img'<br />
<br />
}<br />
<br />
menuentry 'Arch Linux Stock Kernel Fallback' {<br />
set root='(hd0,1)'; set legacy_hdbias='0'<br />
legacy_kernel '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'<br />
legacy_initrd '/initramfs-linux-fallback.img' '/initramfs-linux-fallback.img'<br />
}<br />
</nowiki>}}<br />
<br />
如果你忘记创建GRUB配置文件{{ic|/boot/grub/grub.cfg}},然后直接重启到了GRUB命令行界面,输入以下命令:<br />
sh:grub> insmod legacycfg<br />
sh:grub> legacy_configfile ${prefix}/menu.lst<br />
选择启动到Arch下然后再重新创建合适的GRUB配置文件{{ic|/boot/grub/grub.cfg}}<br />
<br />
==== 多系统启动 ====<br />
<br />
为了实现多系统启动,需要安装{{Pkg|os-prober}}.安装后,再执行{{ic|grub-mkconfig -o /boot/grub/grub.cfg}}.如果执行失败,可能就需要手动添加启动条目了.(后面有指导)<br />
<br />
{{Note|如果找不到Windows,可以将其boot partition挂载后再试}}<br />
<br />
===== 在BIOS-MBR模式下安装的Microsoft Windows =====<br />
<br />
{{Note|GRUB支持直接从{{ic|bootmgr}}启动,现在不再需要链式加载分区启动扇区了}}<br />
<br />
{{Warning|{{ic|bootmgr}}位于'''系统分区'''('''system partition'''),而不是Windows系统所在的分区(比如C盘).在uuid-卷名列表中,系统分区是那个名为{{ic|LABEL&#61;"SYSTEM RESERVED"}} o或 {{ic|LABEL&#61;"SYSTEM"}}的项,而且这个分区一般只有100到200MB大(和Arch的启动分区差不多).请参考[[Wikipedia:System partition and boot partition]]}}<br />
<br />
本节假设你的Windows分区是{{ic|/dev/sda1}}.首先,找到Windows系统分区的UUID(Windows's SYSTEM PARTITION,{{ic|bootmgr存放其上}}).如果Windows {{ic|bootmgr}}的位置是{{ic|/media/SYSTEM_RESERVED/bootmgr}},对于 Windows Vista/7/8,执行:<br />
<br />
# grub-probe --target=fs_uuid /media/SYSTEM_RESERVED/bootmgr<br />
69B235F6749E84CE<br />
<br />
# grub-probe --target=hints_string /media/SYSTEM_RESERVED/bootmgr<br />
--hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1<br />
<br />
{{Note|对于Windows XP, 请在上面的命令中用{{ic|NTLDR}}替代{{ic|bootmgr}}. 注意,可能{{ic|NTLDR}}不是在名为"SYSTEM_RESERVED"的分区上,请在XP系统所在的分区上寻找 {{ic|NTLDR}}}}<br />
<br />
接着,将下面的代码添加到{{ic|/etc/grub.d/40_custom}} 或者{{ic|/boot/grub/custom.cfg}}中.然后,使用{{ic|grub-mkconfig}}重新生成{{ic|grub.cfg}},这样就能在BIOS-MBR配置下使用GRUB2引导启动Windows系统了:<br />
<br />
对于 Windows Vista/7/8<br />
<br />
if [ "${grub_platform}" == "pc" ]; then<br />
menuentry "Microsoft Windows Vista/7/8 BIOS-MBR" {<br />
insmod part_msdos<br />
insmod ntfs<br />
insmod search_fs_uuid<br />
insmod ntldr <br />
search --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 69B235F6749E84CE<br />
ntldr /bootmgr<br />
}<br />
fi<br />
<br />
对于Windows XP<br />
<br />
if [ "${grub_platform}" == "pc" ]; then<br />
menuentry "Microsoft Windows XP" {<br />
insmod part_msdos<br />
insmod ntfs<br />
insmod search_fs_uuid<br />
insmod ntldr <br />
search --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 69B235F6749E84CE<br />
ntldr /bootmgr<br />
}<br />
fi<br />
<br />
<br />
{{Note|在某些情况下,可能在安装Windows 8之前就已经安装了GRUB.启动Windows时可能会{{ic|\boot\bcd}}报错(错误代码为{{ic|0xc000000f}}).可以通过Wondows Recovery Console来修复这个错误:<br />
x:\> "bootrec.exe /fixboot" <br />
x:\> "bootrec.exe /RebuildBcd".<br />
'''不要'''使用{{ic|bootrec.exe /Fixmbr}},因为那会将GRUB清除掉<br />
}}<br />
<br />
{{ic|/etc/grub.d/40_custom}}可以做为创建{{ic|/etc/grub.d/nn_custom}}的模板.{{ic|nn}}定义了脚本执行的优先级.脚本执行的顺序又决定了grub引导启动目录的内容.<br />
{{Note|{{ic|nn}} 应该比06大,这样才能确保必要的脚本被优先执行}}<br />
<br />
== UEFI 系统==<br />
{{Note|建议阅读并理解[[UEFI]], [[GPT]] and [[UEFI Bootloaders]]}}<br />
===== Check if you have GPT and an ESP检查你是否使用GPT和ESP =====<br />
如果想要使用EFI在某个磁盘上进行启动,那么就需要对这个磁盘进行EFI系统分区(EFI System Partition,即ESP),GPT倒不是必须的,不过我们还是高度建议使用GTP,并且这也是本篇文章当前支持的方法.如果你在一个支持EFI,并且已经有操作系统的电脑上安装Arch(比如Windows 8),那么你已经有了ESP了.可以通过{{ic|parted}}来列出启动磁盘上的分区表以检查其是否支持GPT和ESP(假设这个启动磁盘是/dev/sda)<br />
<br />
# parted /dev/sda print<br />
<br />
如果使用GPT,那么会出现"分区表:GPT".如果使用EFI,那么会有一个文件系统为vfat的小分区(一般小于512MiB)并且被标志为启动分区.在这个小分区上,应该有一个名为EFI的文件夹.如果这些条件都满足,那么这就是ESP了.注意分区序号,因为之后安装GRUB2时你需要mount它.<br />
<br />
===== 建立ESP =====<br />
<br />
如果你没有ESP,请参考[[UEFI#EFI System Partition]]的引导来创建它<br />
<br />
=== 安装 ===<br />
<br />
{{Note|众所周知,不同的主板厂商的UEFI实现方式也不一样.我们鼓励用户将其配置过程中遇到的问题及其细节分享出来.为了保证[[GRUB]]页面的简洁,请参考[[GRUB/EFI examples (简体中文)]]<br />
}}<br />
<br />
首先,安装{{Pkg|grub}}, {{Pkg|dosfstools}}, {{Pkg|efibootmgr}}(后面两个包是为了让GRUB支持EFI).然后按下面的指导进行配置.<br />
{{Note|简单的安装这些包并不会更新{{ic|core.efi}}和ESP中的GRUB模组.请按下面的指导手动进行配置}}<br />
====安装boot文件====<br />
<br />
=====推荐方法=====<br />
<br />
{{Note|<br />
* 下面的命令是假设你正在为{{ic|x86_64-efi}}安装GRUB.(如果是为{{ic|IA32-efi}}安装GRUB,请用{{ic|i386-efi}}代替{{ic|x86_64-efi}})<br />
* 如果要成功执行下面的命令,你需要使用UEFI启动而不是BIOS.如果你从USB驱动器中的ISO文件启动系统,那么可以判断,你已经是从BIOS启动了.你需要[[Unified Extensible Firmware Interface#Create UEFI bootable USB from ISO|建立一个UEFI可启动USB磁盘]],然后重启,否则grub-install会报错.<br />
}}<br />
<br />
首先,将ESP挂载上(一般挂在到{{ic|/boot/efi}}上,然后将其路径赋值到$esp).如果是第一次安装GRUB,你可能需要建立{{ic|/boot/efi}}文件夹<br />
然后,将GRUB UEFI应用安装到{{ic|$esp/EFI/grub}},将其模组安装到{{ic|/boot/grub/x86_64-efi}}:<br />
<br />
# grub-install --target=x86_64-efi --efi-directory=$esp --bootloader-id=grub --recheck --debug<br />
<br />
{{Note|<br />
* 如果grub-install在sysfs和procfs上运行的时候报错"modprobe efivars",尝试[[Unified_Extensible_Firmware_Interface#Switch_to_efivarfs]].<br />
* 如果没有{{ic|--target}} 或{{ic|--directory}} 选项,grub-install就不能决定安装哪种固件.在这个情况下,grub-install会报错{{ic|source_dir does not exist. Please specify --target or --directory}}.<br />
* {{ic|--efi-directory}} and {{ic|--bootloader-id}}是只适用于GRUB UEFI的选项.{{ic|--efi-directory}}替代{{ic|--root-directory}}指定ESP的挂载点.{{ic|--bootloader-id}}指定存放{{ic|grubx64.efi}}文件的目录名<br />
* 可能你会注意到,不像安装GRUB到BIOS那样,在{{ic|grub-install}}命令后没有<device_path>选项(例如{{ic|/dev/sda}}).哪怕提供了<device_path>,也会被安装脚本忽略.因为UEFI bootloader不使用MBR或者分区启动扇区.<br />
}}<br />
<br />
现在GRUB已经安装好了.请移至[[#配置|配置]]一节继续.<br />
<br />
=====其他方法=====<br />
<br />
如果你想将所有的GRUB boot 文件放在ESP中,请给grub-install命令添加{{ic|--boot-directory&#61;$esp/EFI}}选项:<br />
<br />
# grub-install --target=x86_64-efi --efi-directory=$esp --bootloader-id=grub --boot-directory=$esp/EFI --recheck --debug<br />
<br />
下面的命令会将GRUB模组文件放在{{ic|$esp/EFI/grub}}中.使用这个方法,grub.cfg也会被放在ESP中,所以请确保grub-mkconfig指向了正确的地方:<br />
<br />
# grub-mkconfig -o $esp/EFI/grub/grub.cfg<br />
<br />
配置档和BIOS-GRUB是一样的.<br />
<br />
====在固件启动管理器中创建GRUB条目====<br />
<br />
{{ic|grub-install}}会自动尝试在启动管理器中创建GRUB条目.如果没有成功,请参考[[Beginners' guide#GRUB]],里面有关于使用{{ic|efibootmgr}}创建启动目录条目的介绍.一般来说,这个问题都是因为你没有以UEFI模式启动CD/USB造成的.请参考[[UEFI#Create UEFI bootable USB from ISO]].<br />
<br />
====创建GRUB Standalone模式的UEFI应用程序====<br />
<br />
可以建立一个{{ic|grubx64_standalone.efi}},这个应用将所有的模组嵌入自身的memdisk中,所以就不需要使用单独的目录来存放GRUB UEFI模组和其他相关文件了,使用{{Pkg|grub}}包里的{{ic|grub-mkstandalone}}可以实现这个功能.<br />
<br />
最简单的的方法就是使用{{ic|grub-mkstandalone}},不过,我们还可以指定嵌入哪些模组:<br />
<br />
# grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi/" --format="x86_64-efi" --compression="xz" \<br />
--output="$esp/EFI/grub/grubx64_standalone.efi" <你想嵌入的模组><br />
<br />
{{ic|grubx64_standalone.efi}}文件要求将{{ic|grub.cfg}}放置到{{ic|(memdisk)/boot/grub}}中,而这个memdisk是嵌入到EFI应用当中的.{{ic|grub-mkstandlone}}脚本允许传递将要嵌入memdisk的文件列表.(上面命令里的"<你想嵌入的模组>")<br />
<br />
如果你的{{ic|grub.cfg}}的路径是{{ic|/home/user/Desktop/grub.cfg}},那么需要创建一个临时的{{ic|/home/user/Desktop/boot/grub/}}目录,然后将grub.cfg复制到其中并进入这个目录并运行:<br />
<br />
# grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi/" --format="x86_64-efi" --compression="xz" \<br />
--output="$esp/EFI/grub/grubx64_standalone.efi" "boot/grub/grub.cfg"<br />
<br />
进入{{ic|/home/user/Desktop/boot/grub/}}但是传递{{ic|boot/grub/grub.cfg}}参数(请注意是{{ic|boot/}}而不是{{ic|/boot/}})的原因是{{ic|grub-mkstandalone}}会自动将boot/grub/grub.cfg处理为{{ic|/(memdisk)/boot/grub/grub.cfg}}<br />
<br />
如果你传递{{ic|/home/user/Desktop/grub.cfg}},那么处理后的结果会是{{ic|(memdisk)/home/user/Desktop/grub.cfg}}.如果传递{{ic|/home/user/Desktop/boot/grub/grub.cfg}},那么结果就是{{ic|(memdisk)/home/user/Desktop/boot/grub/grub.cfg}}.所以需要进入{{ic|/home/user/Desktop/boot/grub/}}并传递{{ic|boot/grub/grub.cfg}}参数,因为这样才能生成{{ic|grub.efi}}需要的{{ic|(memdisk)/boot/grub/grub.cfg}}.<br />
<br />
如果需要为{{ic|$esp/EFI/arch_grub/grubx64_standalone.efi}}创建一个UEFI启动器条目,使用{{ic|efibootmgr}}.[[#Create GRUB entry in the Firmware Boot Manager]]里有介绍<br />
<br />
==配置==<br />
<br />
可以自动生成配置档,也可以手动编辑{{ic|grub.cfg}}.<br />
{{Note|<br />
* 对于EFI系统,如果在安装GRUB时使用 {{ic|1=--boot-directory=$esp/EFI}}了选项,{{ic|grub.cfg}}文件必须和{{ic|grubx64.efi}}放在同一文件夹.如果没有使用这个选项,那么{{ic|grub.cfg}}应当在{{ic|/boot/grub/}}下,和GRUB BIOS系统一样.<br />
*在[http://members.iinet.net/~herman546/p20/GRUB2%20Configuration%20File%20Commands.html 这里]可以找到关于如何配置GRUB的完整描述.<br />
}}<br />
<br />
===使用grub-mkconfig自动生成配置档===<br />
<br />
和GRUB Legacy配置文件{{ic|menu.lst}}对应的GRUB配置文件是{{ic|/etc/default/grub}} 和{{ic|/etc/grub.d/*}}.{{ic|grub-mkconfig}}使用这些文件来生成{{ic|grub.cfg}}.grub-mkconfig默认输出至stdout.以下命令可以生成一个{{ic|grub.cfg}}文件:<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|/etc/grub.d/10_linux}}会自动为Arch Linux设置启动项.其他操作系统的话,可能需要手动在{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}中设置.<br />
<br />
{{Note|如果你在一个chroot环境中或者systemd-nspawn容器里执行这些操作,可能完全不会成功,因为grub-probe不能获取'/dev/sdaX'这样的典型路径.这种情况下,可以尝试使用[https://bbs.archlinux.org/viewtopic.php?pid&#61;1225067#p1225067 arch-chroot].}}<br />
<br />
====额外的参数====<br />
<br />
在{{ic|/etc/default/grub}}中设置{{ic|GRUB_CMDLINE_LINUX}}和{{ic|GRUB_CMDLINE_LINUX_DEFAULT}}变量可以实现将向Linux镜像传递额外的参数.生成{{ic|grub.cfg}}时,如果遇到普通启动项,这两个参数会一起使用,遇到''recovery''启动项,就只使用{{ic|GRUB_CMDLINE_LINUX}}参数.<br />
<br />
没有必要两者一起使用.这两个参数很有用.比如,如果要系统支持休眠后恢复,需要使用{{ic|<nowiki>GRUB_CMDLINE_LINUX_DEFAULT="resume=/dev/sdaX</nowiki> quiet"}} ({{ic|sda'''X'''}}是交换分区).这个选项会生成一个recovery启动项,这个启动项没有resume和quiet参数.其他的普通启动项也可能使用它们.(GRUB会为每个内核生成两个启动项,一个默认的一个recovery的,GRUB_CMDLINE_LINUX指定的参数会传递给这两个启动项.GRUB_CMDLINE_LINUX_DEFAULT指定的参数只会传递给默认启动项)<br />
<br />
当生成GRUB recovery启动项时,你必须在{{ic|/etc/default/grub}}中将{{ic|<nowiki>#GRUB_DISABLE_RECOVERY=true</nowiki>}} 注释掉.<br />
<br />
你也可以使用{{ic|<nowiki>GRUB_CMDLINE_LINUX="resume=/dev/disk/by-uuid/${swap_uuid}"</nowiki>}}, {{ic|${swap_uuid} }}是交换分区的[[Persistent_block_device_naming|UUID]] <br />
<br />
不同的启动项使用双引号引起来并用空格隔开.所以,对于即想使用resume又想使用systemd的用户来说,这个参数的设置可能是:<br />
{{ic|<nowiki>GRUB_CMDLINE_LINUX="resume=/dev/sdaX init=/usr/lib/systemd/systemd"</nowiki>}}<br />
更多信息请参考[[Kernel parameters]].<br />
<br />
=== 手动创建 grub.cfg ===<br />
<br />
{{Warning|''不推荐''编辑这个文件.这个文件由{{ic|grub-mkconfig}}生成,最好编辑{{ic|/etc/default/grub}}和{{ic|/etc/grub.d}}文件夹下的脚本以实现修改.}}<br />
<br />
基本的GRUB配置文件使用如下选项:<br />
* {{ic|(hd''X'',''Y'')}} 是''X''磁盘的''Y''分区,分区从1开始计数,磁盘从0开始计数.<br />
* {{ic|1=set default=''N''}}设定用户选择超时时间过后的默认启动项<br />
* {{ic|1=set timeout=''M''}}设定用户选择超时时间(秒).<br />
* {{ic|<nowiki>menuentry "title" {entry options}</nowiki>}}设置一个名为{{ic|title}}的启动项<br />
* {{ic|1=set root=(hd''X'',''Y'')}}设定启动分区(kernel和GRUB模组所在磁盘),/boot没被要求独占一个分区,有可能就是root分区下的一个文件夹<br />
示例配置如下:<br />
<br />
{{hc|/boot/grub/grub.cfg|<nowiki><br />
# Config file for GRUB - The GNU GRand Unified Bootloader<br />
# /boot/grub/grub.cfg<br />
<br />
# DEVICE NAME CONVERSIONS<br />
#<br />
# Linux Grub<br />
# -------------------------<br />
# /dev/fd0 (fd0)<br />
# /dev/sda (hd0)<br />
# /dev/sdb2 (hd1,2)<br />
# /dev/sda3 (hd0,3)<br />
#<br />
<br />
# Timeout for menu<br />
set timeout=5<br />
<br />
# Set default boot entry as Entry 0<br />
set default=0<br />
<br />
# (0) Arch Linux<br />
menuentry "Arch Linux" {<br />
set root=(hd0,1)<br />
linux /vmlinuz-linux root=/dev/sda3 ro<br />
initrd /initramfs-linux.img<br />
}<br />
<br />
## (1) Windows<br />
#menuentry "Windows" {<br />
# set root=(hd0,3)<br />
# chainloader +1<br />
#}<br />
</nowiki>}}<br />
<br />
=== 双启动 ===<br />
<br />
{{Note|如果你希望GRUB自动搜寻其他系统, 可以安装{{Pkg|os-prober}}.}}<br />
<br />
==== 使用/etc/grub.d/40_custom 和 grub-mkconfig自动生成 ====<br />
<br />
添加其他启动项的最好方法是编辑{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}.运行{{ic|grub-mkconfig}}后,这些文件中的启动项会被自动添加到grub.cfg中.<br />
更新了这些文件后,运行:<br />
{{bc|<nowiki># grub-mkconfig -o /boot/grub/grub.cfg</nowiki>}}<br />
在UEFI-GPT模式下,运行:<br />
{{bc|<nowiki># grub-mkconfig -o /boot/efi/EFI/GRUB/grub.cfg</nowiki>}}<br />
这样会更新{{ic|grub.cfg}}<br />
<br />
<br />
一个典型{{ic|/etc/grub.d/40_custom}}文件类似于下面这个为[http://h10025.www1.hp.com/ewfrf/wc/product?cc=us&destPage=product&lc=en&product=5402703&tmp_docname= HP Pavilion 15-e056sl Notebook PC|预装WIN8的HP Pavilion 15-e056sl Notebook PC]而作的配置.每个启动项的结构都应该和下面的类似.请注意UEFI分区{{ic|/dev/sda2}} 被命名为{{ic|hd0,gpt2}} 和{{ic|ahci0,gpt2}}(请参考[[GRUB#Windows_Installed_in_UEFI-GPT_Mode_menu_entry|here]]获取更多信息)<br />
<br />
'''/etc/grub.d/40_custom''':<br />
<br />
{{bc|<nowiki>#!/bin/sh<br />
exec tail -n +3 $0<br />
# This file provides an easy way to add custom menu entries.&nbsp; Simply type the<br />
# menu entries you want to add after this comment.&nbsp; Be careful not to change<br />
# the 'exec tail' line above.<br />
<br />
menuentry "HP / Microsoft Windows 8.1" {<br />
echo "Loading HP / Microsoft Windows 8.1..."<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 763A-9CB6<br />
chainloader (${root})/EFI/Microsoft/Boot/bootmgfw.efi<br />
}<br />
<br />
menuentry "HP / Microsoft Control Center" {<br />
echo "Loading HP / Microsoft Control Center..."<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 763A-9CB6<br />
chainloader (${root})/EFI/HP/boot/bootmgfw.efi<br />
}<br />
<br />
menuentry "System shutdown" {<br />
echo "System shutting down..."<br />
halt<br />
}<br />
<br />
menuentry "System restart" {<br />
echo "System rebooting..."<br />
reboot<br />
}</nowiki>}}<br />
<br />
===== GNU/Linux 启动项 =====<br />
<br />
假设另一个发行版位于{{ic|sda2}}:<br />
<br />
{{bc|<nowiki>menuentry "Other Linux" {<br />
set root=(hd0,2)<br />
linux /boot/vmlinuz (add other options here as required)<br />
initrd /boot/initrd.img (if the other kernel uses/needs one)<br />
}</nowiki>}}<br />
<br />
===== FreeBSD 启动项 =====<br />
<br />
要求FreeBSD以UFS格式被安装在单独的分区上,假设安装在{{ic|sda4}}上:<br />
<br />
{{bc|<nowiki>menuentry "FreeBSD" {<br />
set root=(hd0,4)<br />
chainloader +1<br />
}</nowiki>}}<br />
<br />
===== Windows XP 启动项=====<br />
<br />
假设Windows分区是{{ic|sda3}}.请记住需要将root设置到Windows安装时建立的启动分区上,然后链式加载它,而不是Windows系统所在分区.下面的例子就是假设启动分区就是{{ic|sda3}}.<br />
{{bc|<nowiki># (2) Windows XP<br />
menuentry "Windows XP" {<br />
set root="(hd0,3)"<br />
chainloader +1<br />
}</nowiki>}}<br />
<br />
如果Windows的bootloader和GRUB不在一个硬盘上,那么需要让Windows认为它是在第一硬盘上.使用{{ic|drivemap}}可以实现这点.假设GRUB在{{ic|hd0}}而windows在{{ic|hd2}}上,需要在设定root后执行:<br />
<br />
{{bc|<nowiki>drivemap -s hd0 hd2</nowiki>}}<br />
<br />
===== UEFI-GPT 模式下安装的Windows的启动项 =====<br />
<br />
{{bc|<nowiki>if [ "${grub_platform}" == "efi" ]; then<br />
menuentry "Microsoft Windows Vista/7/8 x86_64 UEFI-GPT" {<br />
insmod part_gpt<br />
insmod fat<br />
insmod search_fs_uuid<br />
insmod chain<br />
search --fs-uuid --set=root $hints_string $uuid<br />
chainloader /EFI/Microsoft/Boot/bootmgfw.efi<br />
}<br />
fi</nowiki>}}<br />
<br />
{{ic|$hints_string}} 和 {{ic|$uuid}}可以通过以下命令获取:<br />
<br />
{{ic|$uuid}}:<br />
<br />
{{bc|<nowiki># grub-probe --target=fs_uuid $esp/EFI/Microsoft/Boot/bootmgfw.efi<br />
1ce5-7f28</nowiki>}}<br />
<br />
{{ic|$hints_string}}:<br />
<br />
{{bc|<nowiki># grub-probe --target=hints_string $esp/EFI/Microsoft/Boot/bootmgfw.efi<br />
--hint-bios=hd0,gpt1 --hint-efi=hd0,gpt1 --hint-baremetal=ahci0,gpt1</nowiki>}}<br />
<br />
这两个命令都是假设ESP挂载在{{ic|$esp}}上.当然,Windows的EFI文件路径可能有变,因为这就是Windows....<br />
<br />
===== "Shutdown" 启动项 =====<br />
<br />
{{bc|<nowiki>menuentry "System shutdown" {<br />
echo "System shutting down..."<br />
halt<br />
}</nowiki>}}<br />
<br />
===== "Restart" 启动项 =====<br />
<br />
{{bc|<nowiki>menuentry "System restart" {<br />
echo "System rebooting..."<br />
reboot<br />
}</nowiki>}}<br />
<br />
==== 通过EasyBCD NeoGRUB 和Windows共存 ====<br />
<br />
现在EasyBCD的NeoGRUB还不能识别GRUB的目录格式,在 {{ic|C:\NST\menu.lst}} 中添加如下行以链式加载到GRUB:<br />
<br />
default 0<br />
timeout 1<br />
<br />
title Chainload into GRUB v2<br />
root (hd0,7)<br />
kernel /boot/grub/i386-pc/core.img<br />
<br />
最后,使用{{ic|grub-mkconfig}}重建{{ic|grub.cfg}}<br />
<br />
===可视化配置===<br />
<br />
GRUB默认就支持定制启动目录的外观.不过要确保使用合适的视频模式初始化GRUB的图形化终端gfxterm.在[[#"No suitable mode found" error]]一节中有介绍.GRUB通过'gfxpayload'来将视频模式传递给linux内核,所以任何可视化配置都需要这个模式的信息以正确工作.<br />
<br />
==== 设定帧缓冲的分辨率 ====<br />
<br />
<br />
GRUB既可以为自己,也可以为内核设定帧缓冲.现在已经不使用老的{{ic|1=vga=}}配置了.推荐方法是在{{ic|/etc/default/grub}}进行如下编辑:<br />
<br />
GRUB_GFXMODE=1024x768x32<br />
GRUB_GFXPAYLOAD_LINUX=keep<br />
<br />
运行以下命令使配置生效: <br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|gfxpayload}}属性会确保内核也保持这个分辨率<br />
<br />
<br />
{{Note|<br />
* 如果示例不起作用,请尝试用{{ic|1=vbemode="0x105"}}代替{{ic|1=gfxmode="1024x768x32"}}.请使用适合你屏幕的分辨率.<br />
* 可以通过{{ic|1=# hwinfo --framebuffer}}命令来显示所有可以使用的分辨率模式(hwinfo在[[community]]里),在GRUB命令行下,可以使用{{ic|1=vbeinfo}} 命令<br />
}}<br />
<br />
这种方法不管用的话,可以试试老的{{ic|1=vga=}}方法.将它添加到{{ic|1="GRUB_CMDLINE_LINUX_DEFAULT="}}里面就行了,比如<br />
{{ic|1="GRUB_CMDLINE_LINUX_DEFAULT="quiet splash vga=792"}} <br />
这会将系统的分辨率设定为1024*768<br />
<br />
可以选择以下分辨率中的一种:{{ic|640×480}}, {{ic|800×600}}, {{ic|1024×768}}, {{ic|1280×1024}}, {{ic|1600×1200}}, {{ic|1920×1200}}<br />
<br />
==== 915resolution hack ====<br />
<br />
有些时候,Intel显卡无法通过{{ic|1=# hwinfo --framebuffer}} 或{{ic|1=vbeinfo}}显示你需要的分辨率.这种情况下,你可以使用{{ic|915resolution hack}}.915resolution hack会临时性的修改显卡BIOS来添加所需的分辨率.详情请参考[http://915resolution.mango-lang.org/ 915resolution's home page] <br />
<br />
首先,找一个你想要替代的视频模式.例如在GRUB命令行模式下运行:<br />
{{hc|sh:grub> 915resolution -l|<br />
Intel 800/900 Series VBIOS Hack : version 0.5.3<br />
[...]<br />
'''Mode 30''' : 640x480, 8 bits/pixel<br />
[...]<br />
}}<br />
然后,使用{{ic|1440x900}} 分辨率覆盖{{ic|Mode 30}}<br />
{{hc|/etc/grub.d/00_header|<br />
[...]<br />
'''915resolution 30 1440 900 # Inserted line'''<br />
set gfxmode&#61;${GRUB_GFXMODE}<br />
[...]<br />
}}<br />
最后,设置{{ic|GRUB_GFXMODE}},重新生成GRUB配置文件,重启并测试是否生效:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
# reboot<br />
<br />
==== 背景图像和点阵字体 ====<br />
<br />
GRUB原生支持设置背景图像和点阵字体(以pf2格式).{{Pkg|grub}}包含unifont字体,名为{{ic|unicode.pf2}}.(也有可能只包含名为{{ic|ascii.pf2}}的ASCII字符字体)<br />
<br />
GRUB支持的图像格式有tga,png,jpeg.所支持的最大图像分辨率跟硬件有关.<br />
<br />
Make sure you have set up the proper [[#Setting the framebuffer resolution|framebuffer resolution]].<br />
请确保你已经设定了合适的[[#设定帧缓冲的分辨率|帧缓冲分辨率]]<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
GRUB_BACKGROUND="/boot/grub/myimage"<br />
#GRUB_THEME="/path/to/gfxtheme"<br />
GRUB_FONT="/path/to/font.pf2"<br />
<br />
{{Note|如果你将GRUB安装在单独的分区上, {{ic|/boot/grub/myimage}} 应该改为 {{ic|/grub/myimage}}.}}<br />
<br />
重新生成配置文件:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
如果成功的添加了背景图片,那么用户会在命令行中看到{{ic|"Found background image..."}}.<br />
如果没有看到{{ic|"Found background image..."}},图像信息就可能没有嵌入{{ic|grub.cfg}}中了.<br />
<br />
如果图像没有正确显示,执行如下检查:<br />
* 在{{ic|/etc/default/grub}}里,图像的路径和名字是否正确<br />
* 图像的大小和格式是否合适(tga,png,8-bit jpg)<br />
* 图像是不是以RGB模式存储,是不是没有索引<br />
* {{ic|/etc/default/grub}}里面是不是没有开启console模式<br />
* 是否执行{{ic|grub-mkconfig}}以重新生成配置文件<br />
<br />
==== 主题====<br />
<br />
下面的例子将展示如何使用GRUB包重的starfield主题:<br />
<br />
编辑 {{ic|/etc/default/grub}}<br />
GRUB_THEME="/usr/share/grub/themes/starfield/theme.txt"<br />
<br />
重生成配置:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
配置成功的话,在重生成配置过程中,会出现{{ic|Found theme: /usr/share/grub/themes/starfield/theme.txt}}.使用主题就不会使用之前的背景图像<br />
<br />
====目录颜色====<br />
<br />
GRUB支持设置目录颜色.可使用的颜色能从[https://www.gnu.org/software/grub/manual/html_node/Theme-file-format.html the GRUB Manual]里面找到.示例如下:<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
GRUB_COLOR_NORMAL="light-blue/black"<br />
GRUB_COLOR_HIGHLIGHT="light-cyan/blue"<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
====隐藏目录====<br />
<br />
GRUB特性之一就是支持隐藏/跳过目录,同时支持按{{ic|Esc}}来打断隐藏/跳过.同时还支持设置是否显示timeout计时器<br />
下面的例子设置5s钟内没有按Esc键就启动默认的启动项,并且在屏幕上显示倒计时:<br />
<br />
编辑{{ic|/etc/default/grub}}:<br />
<br />
GRUB_HIDDEN_TIMEOUT=5<br />
GRUB_HIDDEN_TIMEOUT_QUIET=false<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
==== 禁用framebuffer ====<br />
<br />
Users who use NVIDIA proprietary driver might wish to disable GRUB's framebuffer as it can cause problems with the binary driver.<br />
使用NVIDIA私有驱动的用户可能希望禁用GRUB的framebuffer,因为会导致驱动错误.<br />
<br />
在{{ic|/etc/default/grub}}中添加(如果已经有背注释掉的这行,就取消注释):<br />
GRUB_TERMINAL_OUTPUT=console<br />
<br />
重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
如果你想保留GRUB的framebuffer,解决方法是在GRUB载入内核前进入文字模式.可以通过在{{ic|/etc/default/grub}}设置如下:<br />
GRUB_GFXPAYLOAD_LINUX=text<br />
然后重建配置档.<br />
<br />
=== 其他选项 ===<br />
<br />
==== LVM ====<br />
<br />
如果使用[[LVM]]做为启动设备,那么在启动项里添加:<br />
<br />
insmod lvm<br />
<br />
然后在启动项里指定root:<br />
<br />
set root=lvm/''lvm_group_name''-''lvm_logical_boot_partition_name''<br />
<br />
示例如下:<br />
<br />
# (0) Arch Linux<br />
menuentry "Arch Linux" {<br />
insmod lvm<br />
set root=lvm/VolumeGroup-lv_boot<br />
# you can only set following two lines<br />
linux /vmlinuz-linux root=/dev/mapper/VolumeGroup-root ro<br />
initrd /initramfs-linux.img<br />
}<br />
<br />
==== 阵列====<br />
<br />
通过添加{{ic|insmod mdraid}},GRUB能够很方便的处理磁盘阵列.比如{{ic|/dev/md0}}:<br />
<br />
set root=(md0)<br />
<br />
阵列上的分区(比如{{ic|/dev/md0p1}})则为:<br />
<br />
set root=(md0,1)<br />
<br />
如果启动分区在raid1上,想要安装grub,只需要在两个磁盘上分别运行grub-install即可:<br />
<br />
grub-install --target=i386-pc --recheck --debug /dev/sda && grub-install --target=i386-pc --recheck --debug /dev/sdb<br />
<br />
这时/dev/sda和/dev/sdb就都有了raid1的启动文件夹/boot了<br />
<br />
==== 持久块设备命名法 ====<br />
<br />
[[Persistent block device naming|持久块设备命名法]](Persistent block device naming)的一个目的是使用全局的UUID来区分分区,而不是用老的{{ic|/dev/sd*}}表示法.好处显而易见.<br />
<br />
GRUB默认使用持久块设备命名法<br />
<br />
{{Note|每次文件系统调整过后,就需要用新的UUID来更新{{ic|/boot/grub.cfg}}.通过Live-CD调整分区和文件系统后要特别注意这点}}<br />
<br />
是否使用UUID由{{ic|/etc/default/grub}}里的这个选项控制:<br />
<br />
# GRUB_DISABLE_LINUX_UUID=true<br />
<br />
无论如何,变更配置后请重建配置档:<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
==== 使用卷标 ====<br />
<br />
GRUB支持以卷标识别文件系统(通过{{ic|search}}命令的{{ic|--label参数}}).<br />
<br />
首先,给文件系统设置一个卷标:<br />
<br />
# tune2fs -L ''LABEL'' ''PARTITION''<br />
<br />
然后在启动项中使用这个卷标:<br />
<br />
menuentry "Arch Linux, session texte" {<br />
search --label --set=root archroot<br />
linux /boot/vmlinuz-linux root=/dev/disk/by-label/archroot ro<br />
initrd /boot/initramfs-linux.img<br />
}<br />
<br />
====调用之前的启动项====<br />
<br />
GRUB能够记住你当前使用的启动项并且在下次启动时将其作为默认项.当你使用多个内核或操作系统时,这个特性很有用.<br />
编辑{{ic|/etc/default/grub}}中的{{ic|GRUB_DEFAULT}}选项:<br />
<br />
GRUB_DEFAULT=saved<br />
<br />
上面的命令会告诉GRUB使用记住的启动项为默认启动项.<br />
将下面的行添加到{{ic|/etc/default/grub}}会让GRUB记住当前的启动项:<br />
<br />
GRUB_SAVEDEFAULT=true<br />
<br />
{{Note|手动添加启动项到{{ic|/etc/grub.d/40_custom}}或{{ic|/boot/grub/custom.cfg}}中,比如Windows启动项,需要添加{{ic|savedefault}}}}<br />
请记住重建配置档.<br />
<br />
==== 改变默认启动项 ====<br />
<br />
可以通过修改{{ic|/etc/default/grub}}中的{{ic|GRUB_DEFAULT}}值来改变默认启动项<br />
<br />
GRUB_DEFAULT=0<br />
<br />
GRUB启动项序号从0开始计数.0代表第一个启动项.<br />
<br />
除了使用启动项序号,也可以使用启动项名:<br />
<br />
GRUB_DEFAULT='Arch Linux, with Linux core repo kernel'<br />
<br />
{{Note|请记住重建配置档}}<br />
<br />
==== 安全====<br />
<br />
如果你想禁止其他人改变启动参数或者使用GRUB命令行,可以给GRUB配置添加用户名/密码.<br />
运行 {{ic|grub-mkpasswd-pbkdf2}},输入密码:<br />
{{hc|grub-mkpasswd-pbkdf2|<br />
[...]<br />
Your PBKDF2 is grub.pbkdf2.sha512.10000.C8ABD3E93C4DFC83138B0C7A3D719BC650E6234310DA069E6FDB0DD4156313DA3D0D9BFFC2846C21D5A2DDA515114CF6378F8A064C94198D0618E70D23717E82.509BFA8A4217EAD0B33C87432524C0B6B64B34FBAD22D3E6E6874D9B101996C5F98AB1746FE7C7199147ECF4ABD8661C222EEEDB7D14A843261FFF2C07B1269A<br />
}}<br />
然后将下面的内容添加到{{ic|/etc/grub.d/00_header}}:<br />
{{hc|/etc/grub.d/00_header|<br />
cat << EOF<br />
<br />
set superusers<nowiki>=</nowiki>"'''username'''"<br />
password_pbkdf2 '''username''' '''<password>'''<br />
<br />
EOF<br />
}}<br />
{{Note|不能直接将<br />
set superusers<nowiki>=</nowiki>"'''username'''"<br />
password_pbkdf2 '''username''' '''<password>'''<br />
添加到/etc/grub.d/00_header中去,而必须使用上述方法.否则会报错<br />
password_pbkdf2: not found<br />
}}<br />
{{ic|<password>}}是{{ic|grub-mkpasswd_pbkdf2}}生成的那个加密过后密码.<br />
<br />
然后重建配置档.其他用户没有密码就不能变更GRUB配置或者使用GRUB命令行了.<br />
<br />
可以参考[https://www.gnu.org/software/grub/manual/grub.html#Security the GRUB manual]中的"Security"部分来进行更多的客制化安全设定<br />
<br />
==== root加密 ====<br />
<br />
将{{ic|1=cryptdevice=/dev/yourdevice:label}}添加到{{ic|/etc/default/grub}}中的{{ic|GRUB_CMDLINE_LINUX}}配置项,可以让GRUB传递参数给内核,让内核对root加密:<br />
<br />
{{Tip|如果你使用从GRUB Legacy升级而来,检查{{ic|/boot/grub/menu.lst.pacsave}}以获取正确的device/label. 在{{ic|kernel /vmlinuz-linux}}后去找label.}}<br />
<br />
将root映射到{{ic|/dev/mapper/root}}:<br />
<br />
GRUB_CMDLINE_LINUX="cryptdevice=/dev/sda2:root"<br />
<br />
当然,需要在rootfs上禁用UUID:<br />
<br />
GRUB_DISABLE_LINUX_UUID=true<br />
<br />
Regenerate the configuration.<br />
<br />
==== 设定下次启动的启动项(一次性,非持久) ====<br />
<br />
命令{{ic|grub-reboot}}可以设置下次启动时启动哪个启动项而不必修改配置文件或者在启动时手动选择.这个设置是一次性的,即不会改变GRUB的默认启动项.<br />
<br />
{{Note|需要在{{ic|/etc/default/grub}}中设定 {{ic|1=GRUB_DEFAULT=saved}},然后重建配置档.在手动生成的 {{ic|grub.cfg}}中, 使用 {{ic|1=set default="${saved_entry}"}}.}}<br />
<br />
==== 启动时隐藏GRUB界面,除非按着SHIFT键 ====<br />
<br />
为了获取更快的启动速度,而不用等GRUB倒计时,可以命令GRUB在启动时隐藏目录,除非SHIFT被按着.<br />
将如下行添加到{{ic|/etc/default/grub}}:<br />
<br />
GRUB_FORCE_HIDDEN_MENU="true"<br />
<br />
然后创建如下文件:<br />
<br />
{{hc|/etc/grub.d/31_hold_shift|<nowiki><br />
#! /bin/sh<br />
set -e<br />
<br />
# grub-mkconfig helper script.<br />
# Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.<br />
#<br />
# GRUB is free software: you can redistribute it and/or modify<br />
# it under the terms of the GNU General Public License as published by<br />
# the Free Software Foundation, either version 3 of the License, or<br />
# (at your option) any later version.<br />
#<br />
# GRUB is distributed in the hope that it will be useful,<br />
# but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />
# GNU General Public License for more details.<br />
#<br />
# You should have received a copy of the GNU General Public License<br />
# along with GRUB. If not, see <http://www.gnu.org/licenses/>.<br />
<br />
prefix="/usr"<br />
exec_prefix="${prefix}"<br />
datarootdir="${prefix}/share"<br />
<br />
export TEXTDOMAIN=grub<br />
export TEXTDOMAINDIR="${datarootdir}/locale"<br />
source "${datarootdir}/grub/grub-mkconfig_lib"<br />
<br />
found_other_os=<br />
<br />
make_timeout () {<br />
<br />
if [ "x${GRUB_FORCE_HIDDEN_MENU}" = "xtrue" ] ; then <br />
if [ "x${1}" != "x" ] ; then<br />
if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then<br />
verbose=<br />
else<br />
verbose=" --verbose"<br />
fi<br />
<br />
if [ "x${1}" = "x0" ] ; then<br />
cat <<EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if keystatus; then<br />
if keystatus --shift; then<br />
set timeout=-1<br />
else<br />
set timeout=0<br />
fi<br />
else<br />
if sleep$verbose --interruptible 3 ; then<br />
set timeout=0<br />
fi<br />
fi<br />
fi<br />
EOF<br />
else<br />
cat << EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if sleep$verbose --interruptible ${GRUB_HIDDEN_TIMEOUT} ; then<br />
set timeout=0<br />
fi<br />
fi<br />
EOF<br />
fi<br />
fi<br />
fi<br />
}<br />
<br />
adjust_timeout () {<br />
if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then<br />
cat <<EOF<br />
if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then<br />
EOF<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_BUTTON}"<br />
echo else<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}"<br />
echo fi<br />
else<br />
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}"<br />
fi<br />
}<br />
<br />
adjust_timeout<br />
<br />
cat <<EOF<br />
if [ "x\${timeout}" != "x-1" ]; then<br />
if keystatus; then<br />
if keystatus --shift; then<br />
set timeout=-1<br />
else<br />
set timeout=0<br />
fi<br />
else<br />
if sleep$verbose --interruptible 3 ; then<br />
set timeout=0<br />
fi<br />
fi<br />
fi<br />
EOF<br />
</nowiki>}}<br />
<br />
===在GRUB中直接从ISO启动===<br />
<br />
编辑{{ic|/etc/grub.d/40_custom}} 或 {{ic|/boot/grub/custom.cfg}},给目标ISO添加一个启动项.然后使用{{ic|grub-mkconfig -o /boot/grub/grub.cfg}}更新配置档<br />
<br />
==== Arch ISO ====<br />
<br />
{{Note|下面的例子都是假设ISO文件位于{{ic|hd0,6}}分区上的{{ic|/archives}}文件夹里}}<br />
{{Tip|For thumbdrives, use something like {{ic|(hd1,$partition)}} and either {{ic|/dev/sdb'''Y'''}} for the {{ic|img_dev}} parameter or [[Persistent block device naming|a persistent name]], e.g. {{ic|img_dev&#61;/dev/disk/by-label/CORSAIR}}.}}<br />
{{Tip|对于闪存,使用{{ic|(hd1,$partition)}}或{{ic|/dev/sdb'''Y'''}}作为{{ic|img_dev}}参数的值,或者使用持久块设备命名法命名的设备,比如{{ic|img_dev&#61;/dev/disk/by-label/CORSAIR}}.}}<br />
===== x86_64 =====<br />
<br />
menuentry "Archlinux-2013.05.01-dual.iso" --class iso {<br />
set isofile="/archives/archlinux-2013.05.01-dual.iso"<br />
set partition="6"<br />
loopback loop (hd0,$partition)/$isofile<br />
linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop<br />
initrd (loop)/arch/boot/x86_64/archiso.img<br />
}<br />
<br />
===== i686 =====<br />
<br />
menuentry "Archlinux-2013.05.01-dual.iso" --class iso {<br />
set isofile="/archives/archlinux-2013.05.01-dual.iso"<br />
set partition="6"<br />
loopback loop (hd0,$partition)/$isofile<br />
linux (loop)/arch/boot/i686/vmlinuz archisolabel=ARCH_201305 img_dev=/dev/sda$partition img_loop=$isofile earlymodules=loop<br />
initrd (loop)/arch/boot/i686/archiso.img<br />
}<br />
<br />
==== Ubuntu ISO ====<br />
<br />
{{Note|下面的例子都是假设ISO文件位于{{ic|hd0,6}}分区上的{{ic|/archives}}文件夹里. 用户需要根据自己系统的实际情况调整.}}<br />
<br />
menuentry "ubuntu-13.04-desktop-amd64.iso" {<br />
set isofile="/archives/ubuntu-13.04-desktop-amd64.iso"<br />
loopback loop (hd0,6)/$isofile<br />
linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash --<br />
initrd (loop)/casper/initrd.lz<br />
}<br />
<br />
menuentry "ubuntu-12.04-desktop-amd64.iso" {<br />
set isofile="/archives/ubuntu-12.04-desktop-amd64.iso"<br />
loopback loop (hd0,6)/$isofile<br />
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash --<br />
initrd (loop)/casper/initrd.lz<br />
}<br />
<br />
==== Other ISOs ====<br />
<br />
可从[http://askubuntu.com/questions/141940/how-to-boot-live-iso-images 这里]获取其他ISO的配置.<br />
<br />
== 使用GRUB命令行 ==<br />
<br />
MBR太小,所以不足以存储所有的GRUB模组.MBR里面只有启动目录配置和一些很基本的命令.GRUB的主要功能通过{{ic|/boot/grub}}里的模组实现,而且可以按需加载.出现错误时,GRUB可能不能引导启动(比如,磁盘分区发生了变化).这时候,一般会出现命令行界面.<br />
<br />
GRUB不止提供一个shell.如果GRUB不能读取到启动目录配置,但是能找到磁盘,你很可能需要进入"normal" shell:<br />
sh:grub><br />
如果有更严重的问题(比如,GRUB找不到必须的文件了),你就可能需要进入"rescue" shell:<br />
grub rescue><br />
<br />
"rescue" shell是"normal" shell的一个子集,其支持的功能更少.如果不幸进入了"rescue" shell里,首先记载"normal"模组,然后启动"normal" shell:<br />
grub rescue> set prefix=(hdX,Y)/boot/grub<br />
grub rescue> insmod (hdX,Y)/boot/grub/i386-pc/normal.mod<br />
rescue:grub> normal<br />
<br />
=== 分页支持 ===<br />
<br />
GRUB支持对长输出进行分页(比如运行{{ic|help}}的输出).不过只有normal shell支持分页,而rescue shell不支持.开启分页支持的方法如下:<br />
<br />
sh:grub> set pager=1<br />
<br />
===使用命令行引导操作系统 ===<br />
<br />
grub> <br />
<br />
可以使用GRUB命令行引导操作系统,一个典型的应用场景是通过'''chainloading'''来引导另一个Windows或Linux<br />
<br />
''ChainLoading''的意思是用当前的bootloader去载入另一个bootloader,所以叫做'''链式'''加载.这个bootloader可能位于MBR,也可能在另一个分区的引导扇区上.<br />
<br />
==== 链式加载一个分区 ====<br />
<br />
set root=(hdX,Y)<br />
chainloader +1<br />
boot<br />
<br />
X=0,1,2...<br />
Y=1,2,3...<br />
<br />
比如链式加载一个位于首磁盘,首分区上的Windows:<br />
<br />
set root=(hd0,1)<br />
chainloader +1<br />
boot<br />
<br />
也可以使用GRUB链式加载另一个分区引导扇区上的GRUB.<br />
<br />
==== 链式加载磁盘====<br />
<br />
set root=hdX<br />
chainloader +1<br />
boot<br />
<br />
====正常载入 ====<br />
<br />
请参考[[#使用应急命令行]]<br />
<br />
==图形化配置工具==<br />
<br />
* {{App|grub-customizer|定制bootloader(GRUB or BURG)|https://launchpad.net/grub-customizer|{{AUR|grub-customizer}}}}<br />
* {{App|grub2-editor|KDE4上配置GRUB的控制模组|http://kde-apps.org/content/show.php?content&#61;139643|{{AUR|grub2-editor}}{{Broken package link|{{aur-mirror|grub2-editor}}}}}}<br />
* {{App|kcm-grub2|可以管理大部分常用GRUB配置的kcm模组|http://kde-apps.org/content/show.php?content&#61;137886|{{AUR|kcm-grub2}}{{Broken package link|{{aur-mirror|kcm-grub2}}}}}}<br />
* {{App|startupmanager|GRUB Legacy, GRUB, Usplash and Splashy的图形化配置工具([https://launchpad.net/startup-manager/+announcement/8300 abandonned])|http://sourceforge.net/projects/startup-manager/|{{AUR|startupmanager}}}}<br />
<br />
== parttool ==<br />
<br />
如果你安装了Windows 9x系列操作系统,而它们隐藏了磁盘(比如C盘),GRUB能够使用parttool来设置是否隐藏.比如,想从三个Windows 9x系统的第三个启动,可以这样:<br />
parttool hd0,1 hidden+ boot-<br />
parttool hd0,2 hidden+ boot-<br />
parttool hd0,3 hidden- boot+<br />
set root=hd0,3<br />
chainloader +1<br />
boot<br />
<br />
==使用应急命令行==<br />
<br />
请先阅读[[#使用GRUB命令行]].如果无法进入命令行,请尝试使用Live CD或者其他rescue磁盘引导,然后修正错误.不过有些时候我们手上没有此类rescue磁盘,这时,应急命令行(rescue console)就可以派上用场了.<br />
<br />
GRUB应急命令行里可用的命令有{{ic|insmod}}, {{ic|ls}}, {{ic|set}}, {{ic|unset}}.可以使用set/unset修改变量,使用insmod来载入模组.<br />
<br />
首先,用户必须知道启动分区({{ic|/boot}}所在位置然后设置:<br />
<br />
grub rescue> set prefix=(hdX,Y)/boot/grub<br />
<br />
通过加载{{ic|linux}}模组来扩展命令行的功能:<br />
<br />
grub rescue> insmod (hdX,Y)/boot/grub/linux.mod<br />
<br />
{{Note|如果/boot是在单独的分区上,请从路径中移除/boot.(即. 输入 {{ic|1=set prefix=(hdX,Y)/grub}} 然后{{ic|insmod (hdX,Y)/grub/linux.mod}})}}<br />
<br />
这个模组会启动对我们熟悉的{{ic|linux}} 和 {{ic|initrd}} 命令的支持 (请参考[[#配置]]).<br />
<br />
比如:<br />
set root=(hd0,5)<br />
linux /boot/vmlinuz-linux root=/dev/sda5<br />
initrd /boot/initramfs-linux.img<br />
boot<br />
<br />
如果/boot在单独分区上:<br />
set root=(hd0,5)<br />
linux /vmlinuz-linux root=/dev/sda6<br />
initrd /initramfs-linux.img<br />
boot<br />
<br />
成功启动Arch后,用户可以修正配置的错误或者重新安装GRUB.<br />
<br />
关于修正配置和重新安装GRUB,请参考[[#配置]]和[[#安装]]章节.<br />
==使用UUID的基础脚本 ==<br />
<br />
如果你想要使用UUID来避免不可靠的BIOS设备命名或者正在研究GRUB语法,这里有个使用UUID的示例性的启动项配置脚本.如果你想要将其移植到自己的系统上,只需要修改UUID就行了.这个例子假设系统的boot和root文件系统是在不同的分区上.如果你还有其他分区,请做相应修改.<br />
<br />
menuentry "Arch Linux 64" {<br />
# Set the UUIDs for your boot and root partition respectively<br />
set the_boot_uuid=ece0448f-bb08-486d-9864-ac3271bd8d07<br />
set the_root_uuid=c55da16f-e2af-4603-9e0b-03f5f565ec4a<br />
<br />
# (Note: This may be the same as your boot partition)<br />
<br />
# Get the boot/root devices and set them in the root and grub_boot variables<br />
search --fs-uuid $the_root_uuid --set=root<br />
search --fs-uuid $the_boot_uuid --set=grub_boot<br />
<br />
# Check to see if boot and root are equal.<br />
# If they are, then append /boot to $grub_boot (Since $grub_boot is actually the root partition)<br />
if [ $the_boot_uuid == $the_root_uuid ] ; then<br />
set grub_boot=($grub_boot)/boot<br />
else<br />
set grub_boot=($grub_boot)<br />
fi<br />
<br />
# $grub_boot now points to the correct location, so the following will properly find the kernel and initrd<br />
linux $grub_boot/vmlinuz-linux root=/dev/disk/by-uuid/$the_root_uuid ro<br />
initrd $grub_boot/initramfs-linux.img<br />
}<br />
== 异常处理==<br />
<br />
=== Intel BIOS不能引导GPT ===<br />
<br />
一些Intel的BIOS要求至少要一个可启动的分区(MBR方案下的),导致GTP方案下的分区GRUB无法启动.<br />
<br />
可以通过fdisk来将一个GPT分区标志为'boot'((最好就设在你为GRUB创建的那个1007KiB分区上)),这样就可以绕过这个问题了:<br />
<br />
1.对目标磁盘(比如/dev/sda)运行fdisk<br />
2.按{{ic|a}}键,然后选择想要设置'boot'标志的分区<br />
3.最后按{{ic|w}}键,将变更写入磁盘<br />
<br />
<br />
{{Note|必须使用fdisk或者类似于它的工具来设置'boot flag',不能用Gparted等,因为它们不会在MBR里面设置'boot flag'.}}<br />
<br />
请参考 [http://www.rodsbooks.com/gdisk/bios.html| http://www.rodsbooks.com/gdisk/bios.html]<br />
<br />
=== 启用调试信息 ===<br />
<br />
在{{ic|grub.cfg}}里添加:<br />
<br />
set pager=1<br />
set debug=all<br />
<br />
=== "No suitable mode found" error ===<br />
<br />
启动时可能出现如下提示信息:<br />
<br />
error: no suitable mode found<br />
Booting however<br />
<br />
然后你需要以合适的视频模式({{ic|gfxmode}})启动 GRUB 图形化终端({{ic|gfxterm}}).视频模式由GRUB通过'gfxpayload'变量传递给linux内核.在UEFI系统下,如果没有初始化视频模式的值,终端上就不会显示内核启动消息(至少直到KMS开始运行)<br />
<br />
将{{ic|/usr/share/grub/unicode.pf2}}复制到 ${GRUB_PREFIX_DIR}({{ic|/boot/grub/}} .如果GRUB UEFI安装时设定了{{ic|1=--boot-directory=$esp/EFI}},那么复制的目的文件夹是{{ic|$esp/EFI/grub/}}:<br />
<br />
# cp /usr/share/grub/unicode.pf2 ${GRUB_PREFIX_DIR}<br />
<br />
如果{{ic|/usr/share/grub/unicode.pf2}} 不存在, 安装{{Pkg|bdf-unifont}}, 创建 {{ic|unifont.pf2}} 文件然后将其复制到 {{ic|${GRUB_PREFIX_DIR<nowiki>}</nowiki>}}:<br />
<br />
# grub-mkfont -o unicode.pf2 /usr/share/fonts/misc/unifont.bdf<br />
<br />
然后,在{{ic|grub.cfg}}文件中添加如下行:<br />
(会给linux内核传递合适的视频模式,否则你只会得到一个黑屏,虽然系统还是会启动成功)<br />
BIOS 系统:<br />
<br />
insmod vbe<br />
<br />
UEFI 系统:<br />
<br />
insmod efi_gop<br />
insmod efi_uga<br />
<br />
在这些行后添加(BIOS&&UEFI):<br />
<br />
insmod font<br />
<br />
if loadfont ${prefix}/fonts/unicode.pf2<br />
then<br />
insmod gfxterm<br />
set gfxmode=auto<br />
set gfxpayload=keep<br />
terminal_output gfxterm<br />
fi<br />
<br />
如果要gfxterm(图形化终端)工作正常,{{ic|${GRUB_PREFIX_DIR<nowiki>}</nowiki>}}文件夹里面应该要有{{ic|unicode.pf2}} .<br />
<br />
=== 出现"msdos-style"错误消息 ===<br />
<br />
以下错误可能出现在你将GRUB安装到VMware上时.请阅读[https://bbs.archlinux.org/viewtopic.php?pid=581760#p581760 相关链接].这种情况是因为首分区直接从MBR后开始(即第64个扇区),而不是和正常的那样有1到2M post-MBR gap.请参阅[[#MBR专用指令]]<br />
<br />
grub-setup: warn: This msdos-style partition label has no post-MBR gap; embedding will not be possible!<br />
grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.<br />
However, blocklists are UNRELIABLE and its use is discouraged.<br />
grub-setup: error: If you really want blocklists, use --force.<br />
<br />
=== GRUB UEFI 启动到了rescue shell下 ===<br />
<br />
如果GRUB直接就启动到了rescue shell下,而且没报错,这可能是因为{{ic|grub.cfg}}丢失或者位置不对.如果GRUB UEFI 安装时设定了{{ic|--boot-directory}}参数,而{{ic|grub.cfg}}文件丢失,会出现这个问题.如果启动分区的分区号发生了变化(这个分区号会被直接编码到{{ic|grubx64.efi}}文件中),也会出现这个问题.<br />
<br />
=== GRUB UEFI 无法被载入 ===<br />
<br />
下面是一个EFI启动项信息示例:<br />
{{hc|# efibootmgr -v|<br />
BootCurrent: 0000<br />
Timeout: 3 seconds<br />
BootOrder: 0000,0001,0002<br />
Boot0000* Grub HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\efi\grub\grub.efi)<br />
Boot0001* Shell HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\EfiShell.efi)<br />
Boot0002* Festplatte BIOS(2,0,00)P0: SAMSUNG HD204UI<br />
}}<br />
<br />
如果启动后,屏幕直接变黑,几秒后就跳到了下一个启动项,根据[https://bbs.archlinux.org/viewtopic.php?pid=981560#p981560 相关链接]的说法是,将GRUB移动到root分区上可能会解决这个问题.必须先删除启动项,然后重建,变化如下:<br />
Boot0000* Grub HD(1,800,32000,23532fbb-1bfa-4e46-851a-b494bfe9478c)File(\grub.efi)<br />
<br />
=== "Invalid signature"错误 ===<br />
<br />
如果在启动Windows时出现了"invalid signature"错误(比如在重新分区或者添加了新硬盘后),删除GRUB的磁盘mapping,然后重建:<br />
<br />
# mv /boot/grub/device.map /boot/grub/device.map-old<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
{{ic|grub-mkconfig}}此时就应该生成了新的启动项了,包括Windows.确认能启动成功后,再将备份文件{{ic|/boot/grub/device.map-old}}删除.<br />
<br />
=== 引导过程卡死 ===<br />
<br />
如果在GRUB载入内核并初始化ramdisk后引导过程卡死了,请尝试移除{{ic|add_efi_memmap}}这个内核参数<br />
<br />
=== 回滚到 GRUB Legacy ===<br />
<br />
* 将GRUB2相关文件改名以为备份:<br />
<br />
# mv /boot/grub /boot/grub.nonfunctional<br />
<br />
* 将GRUB Legacy 移回{{ic|/boot}}:<br />
<br />
# cp -af /boot/grub-legacy /boot/grub<br />
<br />
* 恢复备份的MBR<br />
<br />
{{Warning|这个命令同时会恢复分区表,所以请要非常注意.}}<br />
<br />
# dd if=/path/to/backup/first-sectors of=/dev/sdX bs=512 count=1<br />
<br />
安全的方法是只恢复MBR中的启动代码:<br />
<br />
# dd if=/path/to/backup/mbr-boot-code of=/dev/sdX bs=446 count=1<br />
<br />
=== 其他系统不能自动发现Arch Linux===<br />
<br />
有人发现有些发行版不能使用{{ic|os-prober}}自动发现Arch Linux.据称先使用{{ic|/etc/lsb-release}}可能会解决这个问题.相关文件和工具可以在 [[official repositories|官方仓库]]的{{Pkg|lsb-release}}包中找到<br />
<br />
== 参阅 ==<br />
<br />
* 官方GRUB说明 - https://www.gnu.org/software/grub/manual/grub.html<br />
* Ubuntu GRUB Wiki - https://help.ubuntu.com/community/Grub2<br />
* 在UEFI系统上编译GRUB步骤描述- https://help.ubuntu.com/community/UEFIBooting<br />
* 维基百科[[Wikipedia:BIOS Boot partition|BIOS启动分区]]<br />
* 配置GRUB的完整说明 - http://members.iinet.net/~herman546/p20/GRUB2%20Configuration%20File%20Commands.html</div>
残月舞流殇