GNU GRUB(GRand Unified Bootloader)是一个来自GNU项目的启动引导程序。GRUB是多启动规范的实现,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统。目前绝大部分现代 linux 发行版都采用 grub 来引导系统,并且 windows 和 mac 也可以用 grub 来引导。这使得用 grub 引导多系统非常方便。


Table of Contents

  1. grub 安装指南
    1. 安装到硬盘
    2. 安装到 U 盘
  2. grub 引导 iso
  3. grub 一般配置
  4. grub2 引导 LiveCD
  5. grub 美化教程

grub 安装指南

GRUB的一个重要的特性是安装它不需依附一个操作系统;但是,这种安装需要一个Linux副本,如果没有linux环境, 可以安装一个linux虚拟机来安装grub. 当前在 Windows 下有一个实验性版本可以使用。
GRUB实质上是一个微型系统,通过链式启动的方式,它可以启动所有安装的主流操作系统。除了安装到硬盘外,GRUB也可安装到光盘、软盘和闪存盘等移动介质中,这样就可以带起一台无法从硬盘启动的系统。


安装到硬盘

这个一般不需要手动安装,在安装系统时,grub 会自动安装到硬盘上,如果需要手动安装,可参考下面安装到 u盘的方法。

安装到 U 盘

制作一个可启动优盘会带在身边有时候会很方便,grub 是非常好的选择。 一般我们使用的就是 i386-pc 和 x86_64-efi 两种模式。

  • i386-pc platform

如果使用 ubuntu,一般是安装的这个模式。 可以在传统 bios 模式下工作。

  • x86_64-efi platform

如果使用 fedora 或 opensuse, 一般是 x86_64-efi 模式, 可以在 uefi 模式下工作。

生成引导文件:

将其放到 u盘 的/EFI/BOOT/下面,替换掉原来的 BOOTX64.EFI(做好备份).

当然 fedora/opensuse 下也可以安装 i386-pc platform,只是相应地要改一下命令。安装到硬盘的话,只需将–removable 去掉就好了。

当然你需要一台已经装了 linux 的电脑,windows 下暂时没有试过官方提供的 grub for windows。


grub 引导 iso

一般的 linux 发行版都会有 livecd, 使用 grub 可以轻松引导 iso 并进行体验或者安装. 或者可以使用 syslinux 中的 引导 winpe 等iso, memdisk 可以从这里下载 编译. 使用方法按照下面的配置就可以.


grub 一般配置

下面是一份 GPT/UEFI 分区的 USB 上 grub 的通用配置,用来做引导 u盘, MBR 模式的可以相应修改, 通常存放在 /EFI/grub2/grug.cfg。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
### grub configuration
### Author: Darkshell
### Email: byujiang@163.com
### Home: https://www.darkshell.me

### Basic Setting ###
### Partition USB to two parts,and uuid1 and uuid2 are uuids of two partitions.
set uuid1=1F83-3F65
set uuid2=1F83-3F65
search --no-floppy --fs-uuid --set root $uuid2
set default=0
set timeout=5

nsmod terminal
set font=/efi/grub2/fonts/unicode.pf2
loadfont $font
set gfxmode=auto
insmod gfxterm
insmod gfxmenu
insmod gfxterm_menu
# terminal_input gfxterm
# terminal gfxterm
terminal_output gfxterm
insmod fat
insmod ext2
insmod part_gpt
insmod efi_gop
insmod iso9660
insmod chain
insmod linux
insmod exfat
insmod linuxefi
insmod echo
insmod configfile
insmod boot
insmod search_label
insmod search_fs_file
insmod search
insmod search_fs_uuid
insmod loadbios
insmod linux16
insmod lsefi
insmod lspci
insmod memdisk
insmod ls
insmod video
insmod video_fb
insmod normal
insmod test
insmod sleep
insmod probe
insmod png
insmod jpeg
insmod gettext
insmod video_bochs
insmod video_cirrus
insmod gzio
insmod efi_uga

if loadfont /efi/grub2/themes/ascii.pf2;then
loadfont /efi/grub2/themes/DejaVuSans-Bold14.pf2
loadfont /efi/grub2/themes/DejaVuSans10.pf2
loadfont /efi/grub2/themes/DejaVuSans12.pf2
loadfont /efi/grub2/themes/asciii.pf2
set theme=/efi/grub2/themes/theme.txt
background_image -m stretch /efi/grub2/themes/background.jpg
fi

# 引导 LiveCD ISO 和 DVD files entry, iso 文件都在 (hd,gpt1)/boot/下面
# Most from wiki.archlinux.org/index.php/GRUB
### Ubuntu LiveCD ###
menuentry "Ubuntu LiveCD" {
GRUB_DISTRIBUTOR=ubuntu
setearch --no-floppy --fs-uuid --set=root $uuid1
set gfxpayload=keep
loopback loop /boot/ubuntu.iso
echo Loading vmlinuz.efi...
linux (loop)/linuxcasper/vmlinuz.efi boot=casper iso-scan/filename=/boot/ubuntu.iso locale=en_US.UTF-8
echo Loading initrd.lz...
initrd (loop)/casper/initrd.lz
echo booting...
boot
}
menuentry "Fedora WS LiveCD 34"{
insmod part_gpt
insmod ext2
insmod fat
search --no-floppy --fs--uuid --set=root 069a4906-42aa-44d3-9910-171eabea9a6e
set isofile=Fedora-Workstation-Live-x86_64-34-1.2.iso
loopback loop $(root)/$isofile
probe --set isolabel --label (loop)
linuxefi $(loop)/images/pxeboot/vmlinuz root=live:CDLABEL=Fedora-WS-Live-34-1-2 rd.live.image quiet rhgb
initrdefi $(loop)/images/pxeboot/initrd.img
echo booting...
boot
}

### local opensuse ###
menuentry "Local Fedora"{
set gfxpayload=keep
set root=(hd1,gpt1)
echo loading linux...
chainloader /EFI/fedora/shimx64-fedora.efi
boot
}
#### Local Windows ####
menuentry "Local Windows 10"{
set gfxpayload=keep
search --no-floppy --file --set root /EFI/Microsoft/Boot/bootmgfw.efi
#set root=(hd1,gpt1)
echo Starting Local Windows
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
boot
}

menuentry "Local openSUSE"{
set gfxpayload=keep
set root=(hd1,gpt3)
echo loading linux...
chainloaderinloader /EFI/opensuse/grubx64.efi
boot
}
### CentOS ###
submenu "CentOS --->>> "{
### Centos DVD ###
menuentry "CentOS DVD)"{
GRUB_DISTRIBUTOR=fedora
# set root=(hd1,gpt1)
search --no-floppy --fs-uuid --set=root $uuid1
insmod ext2
insmod exfat
set isofile='/centos/CentOS-8.1.1911-x86_64-dvd1.iso'
loopback loop $isofile
set gfxpayload=keep
echo Loading linux...
linux (loop)/images/pxeboot/vmlinuz inst.stage2=hd:/dev/sda2:/$isofile
initrd (loop)/images/pxeboot/initrd.img
}

### CentOS LiveCD ###
menuentry "CentOS-Live-Gnome-x86_64"{
GRUB_DISTRIBUTOR=fedora
search --no-floppy --fs-uuid --set= root $uuid1
set isofile='/centos/CentOS-7-x86_64-LiveGNOME-1908.iso'
loopback loop /centos/${isofile}
set gfxpayload=keep
echo Loading linux...
probe --set isolabel --label (loop)
linuxefi (loop)/isolinux/vmlinuz0 root=live:CDLABEL=${isolabel} iso-scan/filename=/${isofile} ro rd.live.image quiet rhgb rd.live.check
echo Loading initial ramdisk ...
initrdefi (loop)/isolinux/initrd0.img
echo booting...
}
### CentOS LiveCD ###
menuentry "CentOS-Netinstall"{
GRUB_DISTRIBUTOR=fedora
# set root='(hd3,gpt1)'
search --no-floppy --fs-uuid --set=root $uuid1
set isofile='/centos/CentOS-7-x86_64-NetInstall-1908.iso'
loopback loop $isofile
set gfxpayload=keep
echo Loading linux...
probe --set isolabel --label (loop)
linuxefi (loop)/isolinux/vmlinuz0 root=live:CDLABEL=${isolabel} iso-scan/filename=$isofile ro rd.live.image quiet rhgb rd.live.check
echo Loading initial ramdisk ...
initrdefi (loop)/isolinux/initrd0.img
echo booting...
}
menuentry "CentOS8-Netinstall HTTP"{
GRUB_DISTRIBUTOR=fedora
search --no-floppy --fs-uuid --set root $uuid1
set gfxpayload=keep
insmod gzio
echo Loading linux...
linux /centos/8/vmlinuz ip=dhcp nameserver=202.38.128.58 inst.gpt inst.vnc inst.vncpassword=12345678 inst.headless inst.repo=http://mirror.ihep.ac.cn/centos/8/os/x86_64 inst.lang=en_US inst.keymap=us
#linux /centos/7/vmlinuz ip=dhcp nameserver=114.114.114.114 inst.gpt inst.vnc inst.vncpassword=12345678 inst.headless inst.repo=https://mirrors.ustc.edu.cn/centos/7/os/x86_64 inst.lang=en_US inst.keymap=us
echo Loading initial ramdisk ...
initrd /centos/8/initrd.img
echo booting...
}
}

## Fedora ###
submenu "Fedora Installation >>> "{
### Fedora KDE LiveCD ###
menuentry "Fedora-Live-KDE-x86_64"{
GRUB_DISTRIBUTOR=fedora
search --no-floppy --fs-uuid --set root $uuid1
set isofile='/fedora/fedora-kde.iso'
loopback loop (hd0,gpt1)$isofile
sset gfxpayload=keep
echo Loading linux...
linuxefi (loop)/isolinux/vmlinuz root=live:CDLABEL=Fedora-Live-KDE-x86_64-20-1 iso-scan/filename=$isofile ro rd.live.image quiet rhgb rd.live.check
echo Loading initial ramdisk ...
initrdefi (loop)/isolinux/initrd.img
}
### Fedora DVD ###
menuentry "Fedora DVD on (hd1,gpt4)"{
GRUB_DISTRIBUTOR=fedora
set root=(hd1,gpt4)
set isofile='/fedora/fedora-dvd.iso'
loopback loop $isofile
set gfxpayload=keep
echo Loading linux...
linux (loop)/images/preboot/vmlinuz inst.stage2=hd:/dev/sda4:/$isofile
initrd (loop)/images/preboot/initrd.img
}
}
### openSUSEE
submenu "openSUSE Installation >>>" {
#### openSUSE KDE ###
menuentry "openSUSE KDE liveCD" {
GRUB_DISTRIBUTOR=opensuse
set gfxpayload=keep
#set root=(hd0,gpt1)
search --no-floppy --fs-uuid --set root $uuid1
set isofile=/opensuse/opensuse-kde.iso
loopback loop ${isofile}
echo Loading linux...
linuxefi (loop)/boot/x86_64/loader/linux isofrom_device=/dev/sda2 isofrom_system=$isofile LANG=en_US.UTF-8
echo Loading initrd...
initrdefi (loop)/boot/x86_64/loader/initrd
echo booting...
boot
}
}

### Scientific Linux ###
submenu "Scientific Linux --->>>"{
menuentry "SL-LiveCD"{
GRUB_DISTRIBUTOR=fedora
insmod ntfs
#set root='(hd0,gpt1)'
search --no-floppy --fs-uuid --set root $uuid1
set isofile='/boot/SL-70-x86_64-2014-10-12-LiveCD.iso'
loopback loop $isofile
set gfxpayload=keep
echo Loading linux...
linuxefi (loop)/isolinux/vmlinuz0 root=live:CDLABEL=SL-70-x86_64-LiveCD iso-scan/filename=$isofile ro rd.live.image quiet rhgb rd.live.check
echo Loading initial ramdisk ...
initrdefi (loop)/isolinux/initrd0.img
}
menuentry "SL-KDE-x86_64"{
GRUB_DISTRIBUTOR=fedora
insmod ntfs
#set root='(hd0,gpt1)'
search --no-floppy --fs-uuid --set root $uuid1
set isofile='/boot/SL-70-x86_64-2014-10-12-LiveDVDkde.iso'
loopback loop $isofile
set gfxpayload=keep
echo Loading linux...
linuxefi (loop)/isolinux/vmlinuz0 root=live:CDLABEL=SL-70-x86_64-LiveDVDkde iso-scan/filename=$isofile ro rd.live.image quiet rhgb rd.live.check
echo Loading initial ramdisk ...
initrdefi (loop)/isolinux/initrd0.img
}
menuentry "SL-Gnome-x86_64"{
GRUB_DISTRIBUTOR=fedora
insmod ntfs
#set root='(hd0,gpt1)'
search --no-floppy --fs-uuid --set root $uuid1
set isofile='/boot/SL-70-x86_64-2014-10-12-LiveDVDgnome.iso'
loopback loop $isofile
set gfxpayload=keep
echo Loading linux...
linuxefi (loop)/isolinux/vmlinuz0 root=live:CDLABEL=SL-70-x86_64-LiveDVDgnome iso-scan/filename=$isofile ro rd.live.image quiet rhgb rd.live.check
echo Loading initial ramdisk ...
initrdefi (loop)/isolinux/initrd0.img
}
}
### Debian
submenu "Debian --->>> "{
### Debian DVD ###
menuentry "Debian x86_64 DVD"{
GRUB_DISTRIBUTOR=debian
set root=(hd0,1)
insmod ext2
insmod ntfs
set isofile='/boot/debian-dvd.iso'
### debian.hdd.initrd.gz need to be downloaded from:
### https://mirrors.kernel.org/debian/dists/stable/main/installer-amd64/current/images/hd-media/initrd.gz
set initrd.gz="/boot/debian.hdd.initrd.gz"
loopback loop (hd0,1)$isofile
set gfxpayload=keep
echo Loading vmlinuz...
linux (loop)/install.amd/vmlinuz vga=791 iso-scan/ask_second_pass=true iso-scan/filename=$isofile
echo Loading initrd
initrd (hd0,gpt1)$initrd.gz
}
### debian livecd ###
menuentry "Debian LiveCD x86_64"{
search --no-floppy --fs-uuid --set root $uuid1
set isofile='/boot/debian.iso'
loopback loop $isofile
echo loading vmlinuz
linux (loop)/live/vmlinuz boot=live config fromiso=/dev/sdb1$isofile
echo loading initrd.img
initrd (loop)/live/initrd.img
}
}
### Windows ###
menuentry "win8pe"{
insmod ext2
insmod part_msdos
search --no-floppy --fs-uuid --set root $uuid2
linux16 /efi/grub2/memdisk iso raw
echo Coping WinPE to Ram, Please Wait For a While.
initrd16 /boot/win8pe.iso
echo booting
}
######## Others
submenu "Other Linux Distro" {
### Archlinux ###
menuentry "Archlinux x86_64" {
insmod part_msdos
insmod part_gpt
insmod ext2
search --no-floppy --fs-uuid --set root $uuid1
set isofile=/archlinux/archlinux.iso
probe --set label --label (loop)
set imgdevpath=/dev/sda2
loopback loop $isofile
linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=$label img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
initrd (loop)/arch/boot/intel_ucode.img (loop)/arch/boot/amd_ucode.img (loop)/arch/boot/x86_64/archiso.img
boot
}
### Slackware ###
menuentry 'slackware linux DVD'{
search --no-floppy --fs-uuid --set root $uuid1
set isofile='/others/slackware-dvd.iso'
loopback loop $isofile
linux (loop)/kernels/huge.s/bzImage printk.time=0
initrd (loop)/isolinux/initrd.img
}

### slax ###
menuentry "slax" {
#set root=(hd0,gpt2)
search --no-floppy --fs-uuid --set=root $uuid1
set isofile='/boot/slax.iso'
search --set -f $isofile
loopback loop $isofile
linux (loop)/slax/boot/vmlinuz from=$isofile ramdisk_size=6666 root=/dev/ram0 rw changes=/boot/slax/
initrd (loop)/slax/boot/initrfs.img
}
### Kali ###
menuentry 'Kali'{
set isofile='/boot/iso/kali-linux.iso'
search --no-floppy --fs-uuid --set root $uuid1
loopback loop $root$isofile
linux (loop)/live/vmlinuz boot=live findiso=$isofile noconfig=sudo username=sudo hostname=kali
initrd (loop)/live/initrd.img
}

### deepin cd ###
menuentry "Deepin Live X86_64" {
GRUB_DISTRIBUTOR=ubuntu
#set root=(hd0,1)
search --no-floppy --fs-uuid --set root $uuid1
set gfxpayload=keep
echo Loading vmlinuz...
loopback loop /boot/deepin.iso
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=/boot/deepin.iso locale=zh_CN.UTF-8
echo Loading initrd.lz...
initrd (loop)/casper/initrd.lz
}
### Linux Mint ###
menuentry "Linux Mint" {
#set root=(hd0,1)
search --no-floppy --fs-uuid --set root $uuid1
set isofile='/boot/linuxmint.iso'
loopback loop $root$isofile
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile noeject noprompt splash --
initrd (loop)/casper/initrd.lz
}
### OS X ###
menuentry "OS X"{
insmod hfs
insmod chain
insmod chainloader
set root=(hd1,gpt2)
chainloader /usr/standalone/i386/boot.efi
}
### CDLinux ###
menuentry "CDLinux 0.9.7" {
set gfxpayload=keep
search --no-floppy --fs-uuid --set root $uuid1
loopback loop /boot/cdlinux.iso
echo loading bzImage...
linux (loop)/CDlinux/bzImage quiet CDL_LANG=zh_CN.UTF-8 CDL_DIR=/boot/ CDL_IMG=cdlinux.iso vga=791
echo Loading initrd...
initrd (loop)/CDlinux/initrd
}
}

### Restart ###
menuentry "Restart"{
set timeout=2
echo The Computer Will Restart Soon.
reboot
}
### Shutdown ###
menuentry "Shutdown"{
set timeout=2
echo The Computer is Shuting Down.
halt
}
menuentry "BIOS Setup" {
fwsetup
}

当系统引导遭到破坏只能进入 grub 界面时,可以手动引导系统,然后再修复引导。

grub2 引导 LiveCD


grub 美化教程

grub 是高度可定制的, 可以按照喜好,美化引导界面。 也可以套用现成的主题。


水水更健康…<( ̄▽ ̄)> 哇哈哈…