Grub

Grub

简介

GRUB(Grand Unified Bootloader)是一个GNU下的启动引导程序,其通过链式启动的方式,启动所有安装的主流操作系统;

grub磁盘布局

  • grub2 on mbr

grub_on_mbr

  • grub on gpt

grub_on_gpt

grub2主要img

  • boot.img:严格占有446字节的大小,grub安装时,写入MBR(sector 0),执行启动stage1阶段, 加载后续stage到内存中;

  • cdboot.img:当从CD引导情况下被写入内核镜像第一个扇区到内容,它负责加载其余的内核镜像到内存;

  • diskboot.img:当从磁盘引导情况下被写入内核镜像第一个扇区到内容,它负责加载其余的内核镜像到内存;

  • pxeboot.img:当从网络启动时使用到的;

  • kernel.img:包含GRUB2运行时包含的基本工具,框架驱动、文件句柄、环境变量、安全模式命令行解析器等,他可以直接使用但是通常它会被编译进所有的内核镜像中使用。;

  • core.img:GRUB2的内核镜像,它由grub-mkimage程序将kernel.img和一些模块动态编译而成,一般情况下他已经包含足够的模块去访问/boot/grub,模块机制使得内核镜像能保持很小的尺寸。在某种程度上,它可以被视为 GRUB 中的 stage2。;

  • *.mod:这是一些可以动态加载的一些模块,当我们需要时,可以将它们可以被动态加载编译进内核镜像,也可以使用insmod手动加载。他们就代替 GRUB 中的 stage1_5 之类到镜像。

安装grub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## 挂载u盘
[linux-host]$ sudo mount /dev/<udisk> /mnt/udisk1
## 在u盘中创建boot目录
[linux-host]$ mkdir -p /mnt/udisk1/boot

## 安装grub2引导头
## grub
[linux-host]$ sudo grub-install --boot-directory=/mnt/udisk1/boot /dev/<udisk>

## 或者grub2
# BIOS
[linux-host]$ sudo grub2-install --target=i386-pc --recheck --boot-directory=/mnt/boot /dev/sdX

# UEFI
[linux-host]$ sudo grub2-install --target x86_64-efi --efi-directory /mnt --boot-directory=/mnt/boot --removable

2. grub2本地启动配置文件

 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
## 加载Ubuntu USB Live
menuentry "Ubuntu Live" {
    search --file /casper/ubuntu.squashfs --set root
    linux /casper/vmlinux boot=casper ro noacpid ignore_uuid quiet splash
    initrd /casper/initrd.img
}

## 加载Ubuntu ISO Live
menuentry "Ubuntu ISO" {
set isoname=/boot/iso/ubuntu.iso
    search --file ${isoname} --set root
    loopback loop0 ${isoname}
    linux (loop0)/casper/vmlinux boot=casper ro locale=zh_CN.TFT-8 rtc_cmos=localtime ignore_uuid quiet splash iso-scan/filename=${isoname}
    initrd (loop0)/casper/initrd.gz
}

## 加载Clonezilla
menuentry "Clonezilla" {
    search --file /live/clonezilla.squashfs --set root
    linux /live/vmlinux boot=live union=aufs live-config video=normal ro splash quiet ocs_live_extra_param="" ocs_live_keymap="NONE" ocs_live_batch="no" ocs_lang="zh_CN.UTF-8"
    initrd /live/initrd.img
}

## 加载WinXP
menuentry "WinXP" {
    set imgname=/boot/winpe.iso
    search --file ${imgname} --set
    insmod memdisk
    linux16 /boot/memdisk iso raw
    initrd16 ${imgname}
}

## 加载WinPE.iso
menuentry "WinPE.iso" {
    search --file /ntldr --set root
    chainloader +1
    boot
}

3. pxe远程启动

 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
$ cat /etc/dhcpd.conf
    allow booting;
    allow bootp;
    ddns-update-style none;
    log-facility local7;
    default-lease-time -1;
    max-lease-time 7200;
    ddns-update-style interim;
    ignore client-updates;

subnet 192.168.0.0 netmask 255.255.255.0 {
    use-host-decl-names        on;
    option routers                  192.168.0.2;
    option subnet-mask              255.255.0.0;
    option domain-name-servers      192.168.0.2;
    range dynamic-bootp             192.168.254.1 192.168.254.254;
    default-lease-time              21600;
    #option root-path "iscsi:192.168.0.84:::iqn.2010-02.casic706:iscsiboot";
    next-server 192.168.0.2;
    server-name "netstore.casic";
    server-identifier 192.168.0.2; 
    filename "pxelinux.0";  # pxelinux.0
    #filename "grub2pxe.0";         # pxelinux.0
    #filename "grldr";              # grub4dos
    #host fusion {
    #        hardware ethernet 00:1E:67:65:9E:D6;
    #        fixed-address 192.168.42.201;
    #}

}

5. PXE启动配置文件

 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
$ cat /tftpboot/pxelinux.cfg/default

## 从本机硬盘启动    
LABEL "Local BOOT"
    MENU LABEL ^0. Local Boot
    MENU DEFAULT
    KERNEL boot/chain.c32
    APPEND hd0
    TEXT HELP
hd0 boot
    ENDTEXT

## 启动Ubuntu    
LABEL "Ubuntu 12.10"
    MENU LABEL ^1. Ubuntu 12.10
    KERNEL boot/ubuntu_12.10/vmlinuz
    APPEND initrd=boot/ubuntu_12.10/initrd.img boot=casper quiet noacpi only-ubiquity splash netboot=nfs nfsroot=192.168.0.2:/tftpboot/iso/ubuntu_12.10 -
    TEXT HELP
ubuntu 12.10 boot
    ENDTEXT

## 启动Clonezilla
LABEL "clonezilla"
    MENU LABEL ^2. Clonezilla
    KERNEL boot/clonezilla/vmlinuz
    APPEND initrd=boot/clonezilla/initrd.img boot=live union=aufs noswap edd=on nomodeset noprompt config only-ubiquity nosplash ip=frommedia video=uvesafb:mode_option=1024x768-32 ocs_live_keymap="NONE" ocs_live_batch="no" ocs_lang="zh_CN.UTF-8" fetch=tftp://192.168.0.2/iso/clonezilla/clonezilla.squashfs -
    TEXT HELP
clonezilla boot
    ENDTEXT

## 启动Redhat
LABEL "RedHat"
    MENU LABEL ^3. RedHat
    KERNEL boot/redhat/vmlinuz
    APPEND initrd=boot/redhat/initrd.img ramdisk_size=8196 ks=nfs:192.168.0.2:/tftpboot/iso/redhat/ks.cfg
    TEXT HELP
redhat boot
    ENDTEXT

参考

  1. https://en.wikipedia.org/wiki/GNU_GRUB

  2. GRUB2 启动引导器 – 完全教程 - Google 文档

updatedupdated2024-05-102024-05-10